Compiler

compiler/sources

github.com/gopherjs/gopherjs/compiler/sources

import "github.com/gopherjs/gopherjs/compiler/sources"

Functions

func SortedSourcesSlice(sourcesSlice []*Sources)

Source: compiler/sources/sources.go:269

SortedSourcesSlice in place sorts the given slice of Sources by ImportPath. This will not change the order of the files within any Sources.

Types

type Importer

Source: compiler/sources/sources.go:62

type Importer func(path, srcDir string) (*Sources, error)

type Sources

Source: compiler/sources/sources.go:25

Sources is a slice of parsed Go sources and additional data for a package.

Note that the sources would normally belong to a single logical Go package, but they don't have to be a real Go package (i.e. found on the file system) or represent a complete package (i.e. it could be only a few source files compiled by gopherjs build foo.go bar.go).

type Sources struct {
	// ImportPath representing the sources, if exists.
	//
	// May be empty for "virtual"
	// packages like testmain or playground-generated package.
	// Otherwise this must be the absolute import path for a package.
	ImportPath string

	// Dir is the directory containing package sources
	Dir string

	// Files is the parsed and augmented Go AST files for the package.
	Files []*ast.File

	// FileSet is the file set for the parsed files for this source.
	FileSet *token.FileSet

	// JSFiles is the JavaScript files that are part of the package.
	JSFiles []incjs.File

	// TypeInfo is the type information for this package.
	// This is nil until set by Analyze.
	TypeInfo *analysis.Info

	// Package is the types package for these source files.
	// This is nil until set by TypeCheck.
	Package *types.Package

	// GoLinknames is the set of Go linknames for this package.
	// This is nil until set by ParseGoLinknames.
	GoLinknames []linkname.GoLinkname
	// contains filtered or unexported fields
}
Fields
  • ImportPath string

    ImportPath representing the sources, if exists.

    May be empty for "virtual" packages like testmain or playground-generated package. Otherwise this must be the absolute import path for a package.

  • Dir string

    Dir is the directory containing package sources

  • Files []*ast.File

    Files is the parsed and augmented Go AST files for the package.

  • FileSet *token.FileSet

    FileSet is the file set for the parsed files for this source.

  • JSFiles []incjs.File

    JSFiles is the JavaScript files that are part of the package.

  • TypeInfo *analysis.Info

    TypeInfo is the type information for this package. This is nil until set by Analyze.

  • Package *types.Package

    Package is the types package for these source files. This is nil until set by TypeCheck.

  • GoLinknames []linkname.GoLinkname

    GoLinknames is the set of Go linknames for this package. This is nil until set by ParseGoLinknames.

func Analyze(importer Importer, tContext *types.Context, instances *typeparams.PackageInstanceSets)

Source: compiler/sources/sources.go:174

Analyze will determine the type parameters instances, blocking, and other type information for the package. This will set the TypeInfo and Instances fields on the Sources.

This must be called after to simplify to ensure the pointers in the AST are still valid. The instances must be collected prior to this call.

Note that at the end of this call the analysis information has NOT been propagated across packages yet.

func CollectInstances(tc *typeparams.Collector)

Source: compiler/sources/sources.go:160

CollectInstances will determine the type parameters instances for the package.

This must be called before Analyze to have the type parameters instances needed during analysis.

Note that once all the sources are collected, the collector needs to be finished to ensure all the instances are collected.

func ParseGoLinknames() error

Source: compiler/sources/sources.go:188

ParseGoLinknames extracts all //go:linkname compiler directive from the sources.

This will set the GoLinknames field on the Sources.

func Read(decode func(any) error) error

Source: compiler/sources/serializer.go:53

Read will call decode multiple times to read the various fields of the sources. The order of the calls must match the order of the calls in encodeSources.

The given srcModTime is used to determine if the serialized sources are out-of-date. If they are then this returns a nil sources and nil error, and the build time that was read.

func Simplify()

Source: compiler/sources/sources.go:85

Simplify processed each Files entry with astrewrite.Simplify.

Note this function mutates the original Files slice. This must be called after TypeCheck and before analyze since this will change the pointers in the AST. For example, the pointers to function literals will change, making it impossible to find them in the type information, if analyze is called first.

func Sort()

Source: compiler/sources/sources.go:68

Sort sorts the Go files slice by the original source name to ensure consistent order of processing. This is required for reproducible JavaScript output.

Note this function mutates the original Files slice.

func TypeCheck(importer Importer, sizes types.Sizes, tContext *types.Context) error

Source: compiler/sources/sources.go:99

TypeCheck the sources. Returns information about declared package types and type information for the supplied AST. This will set the Package field on the Sources.

If the Package field is not nil, e.g. this function has already been run, this will be a no-op.

This must be called prior to simplify to get the types.Info used by simplify.

func UnresolvedImports(skip ...string) []string

Source: compiler/sources/sources.go:213

UnresolvedImports calculates the import paths of the package's dependencies based on all the imports in the augmented Go AST files.

This is used to determine the unresolved imports that weren't in the PackageData.Imports slice since they were added during augmentation or during template generation.

The given skip paths (typically those imports from PackageData.Imports) will not be returned in the results. This will not return any *_test packages in the results.

func Write(encode func(any) error) error

Source: compiler/sources/serializer.go:18

Write will call encode multiple times to write the various fields of the sources. This is designed to be used with a gob.Encoder.

The order of the calls must match the order of the calls in Read.

s.TypeInfo, s.baseInfo, s.Package, and s.GoLinknames are intentionally omitted from encoding since they must be constructed in the context of the full program to be able to handle generics and cross-package references.