Skip to content

Commit 146735d

Browse files
committed
compiler: break functions related to Decls out of Compile().
For each package-level entity we emit one or more Decl struct, which contains all JS code fragments and metadata required to produce the final executable script. For each decl type (imports, vars, functions and types) I creates a separate function that contains the logic responsible for its creation (and some auxiliary functions). The main objective is to keep the Compile() function very high-level and clearly reflecting various compilation stages we go through. I tried to add comments to make the code more accessible for future contributors (and future self...), although there are still some aspects I don't fully grasp. Ideally, we would have tests for all these new functions, but that's way more work than I'm able to take on right now.
1 parent 568f311 commit 146735d

File tree

7 files changed

+734
-422
lines changed

7 files changed

+734
-422
lines changed

compiler/analysis/info.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,23 @@ func (info *Info) newFuncInfo(n ast.Node) *FuncInfo {
9292
return funcInfo
9393
}
9494

95+
// IsBlocking returns true if the function may contain blocking calls or operations.
9596
func (info *Info) IsBlocking(fun *types.Func) bool {
9697
return len(info.FuncDeclInfos[fun].Blocking) > 0
9798
}
9899

100+
// VarsWithInitializers returns a set of package-level variables that have
101+
// explicit initializers.
102+
func (info *Info) VarsWithInitializers() map[*types.Var]bool {
103+
result := map[*types.Var]bool{}
104+
for _, init := range info.InitOrder {
105+
for _, o := range init.Lhs {
106+
result[o] = true
107+
}
108+
}
109+
return result
110+
}
111+
99112
func AnalyzePkg(files []*ast.File, fileSet *token.FileSet, typesInfo *types.Info, typesPkg *types.Package, isBlocking func(*types.Func) bool) *Info {
100113
info := &Info{
101114
Info: typesInfo,

compiler/compiler.go

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -82,52 +82,6 @@ func (a *Archive) RegisterTypes(packages map[string]*types.Package) error {
8282
return err
8383
}
8484

85-
// Decl represents a package-level symbol (e.g. a function, variable or type).
86-
//
87-
// It contains code generated by the compiler for this specific symbol, which is
88-
// grouped by the execution stage it belongs to in the JavaScript runtime.
89-
type Decl struct {
90-
// The package- or receiver-type-qualified name of function or method obj.
91-
// See go/types.Func.FullName().
92-
FullName string
93-
// A logical equivalent of a symbol name in an object file in the traditional
94-
// Go compiler/linker toolchain. Used by GopherJS to support go:linkname
95-
// directives. Must be set for decls that are supported by go:linkname
96-
// implementation.
97-
LinkingName SymName
98-
// A list of package-level JavaScript variable names this symbol needs to declare.
99-
Vars []string
100-
// NamedRecvType is method named recv declare.
101-
NamedRecvType string
102-
// JavaScript code that declares basic information about a symbol. For a type
103-
// it configures basic information about the type and its identity. For a function
104-
// or method it contains its compiled body.
105-
DeclCode []byte
106-
// JavaScript code that initializes reflection metadata about type's method list.
107-
MethodListCode []byte
108-
// JavaScript code that initializes the rest of reflection metadata about a type
109-
// (e.g. struct fields, array type sizes, element types, etc.).
110-
TypeInitCode []byte
111-
// JavaScript code that needs to be executed during the package init phase to
112-
// set the symbol up (e.g. initialize package-level variable value).
113-
InitCode []byte
114-
// Symbol's identifier used by the dead-code elimination logic, not including
115-
// package path. If empty, the symbol is assumed to be alive and will not be
116-
// eliminated. For methods it is the same as its receiver type identifier.
117-
DceObjectFilter string
118-
// The second part of the identified used by dead-code elimination for methods.
119-
// Empty for other types of symbols.
120-
DceMethodFilter string
121-
// List of fully qualified (including package path) DCE symbol identifiers the
122-
// symbol depends on for dead code elimination purposes.
123-
DceDeps []string
124-
// Set to true if a function performs a blocking operation (I/O or
125-
// synchronization). The compiler will have to generate function code such
126-
// that it can be resumed after a blocking operation completes without
127-
// blocking the main thread in the meantime.
128-
Blocking bool
129-
}
130-
13185
type Dependency struct {
13286
Pkg string
13387
Type string

0 commit comments

Comments
 (0)