Skip to content

Commit 525b19b

Browse files
Adding instance tracking to the DCE
1 parent 3b94762 commit 525b19b

File tree

14 files changed

+1766
-128
lines changed

14 files changed

+1766
-128
lines changed

compiler/decls.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ func (fc *funcContext) newFuncDecl(fun *ast.FuncDecl, inst typeparams.Instance)
320320
Blocking: fc.pkgCtx.IsBlocking(o),
321321
LinkingName: symbol.New(o),
322322
}
323-
d.Dce().SetName(o)
323+
d.Dce().SetName(o, inst.TArgs...)
324324

325325
if typesutil.IsMethod(o) {
326326
recv := typesutil.RecvType(o.Type().(*types.Signature)).Obj()
@@ -450,7 +450,7 @@ func (fc *funcContext) newNamedTypeInstDecl(inst typeparams.Instance) (*Decl, er
450450

451451
underlying := instanceType.Underlying()
452452
d := &Decl{}
453-
d.Dce().SetName(inst.Object)
453+
d.Dce().SetName(inst.Object, inst.TArgs...)
454454
fc.pkgCtx.CollectDCEDeps(d, func() {
455455
// Code that declares a JS type (i.e. prototype) for each Go type.
456456
d.DeclCode = fc.CatchOutput(0, func() {

compiler/expressions.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -591,9 +591,7 @@ func (fc *funcContext) translateExpr(expr ast.Expr) *expression {
591591
case types.MethodVal:
592592
return fc.formatExpr(`$methodVal(%s, "%s")`, fc.makeReceiver(e), sel.Obj().(*types.Func).Name())
593593
case types.MethodExpr:
594-
if !sel.Obj().Exported() {
595-
fc.pkgCtx.DeclareDCEDep(sel.Obj())
596-
}
594+
fc.pkgCtx.DeclareDCEDep(sel.Obj(), inst.TArgs...)
597595
if _, ok := sel.Recv().Underlying().(*types.Interface); ok {
598596
return fc.formatExpr(`$ifaceMethodExpr("%s")`, sel.Obj().(*types.Func).Name())
599597
}

compiler/internal/dce/README.md

Lines changed: 600 additions & 0 deletions
Large diffs are not rendered by default.

compiler/internal/dce/collector.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,33 @@ type Decl interface {
1414
// Collector is a tool to collect dependencies for a declaration
1515
// that'll be used in dead-code elimination (DCE).
1616
type Collector struct {
17-
dependencies map[types.Object]struct{}
17+
dce *Info
1818
}
1919

2020
// CollectDCEDeps captures a list of Go objects (types, functions, etc.)
2121
// the code translated inside f() depends on. Then sets those objects
2222
// as dependencies of the given dead-code elimination info.
2323
//
2424
// Only one CollectDCEDeps call can be active at a time.
25-
// This will overwrite any previous dependencies collected for the given DCE.
2625
func (c *Collector) CollectDCEDeps(decl Decl, f func()) {
27-
if c.dependencies != nil {
26+
if c.dce != nil {
2827
panic(errors.New(`called CollectDCEDeps inside another CollectDCEDeps call`))
2928
}
3029

31-
c.dependencies = make(map[types.Object]struct{})
32-
defer func() { c.dependencies = nil }()
30+
c.dce = decl.Dce()
31+
defer func() { c.dce = nil }()
3332

3433
f()
35-
36-
decl.Dce().setDeps(c.dependencies)
3734
}
3835

3936
// DeclareDCEDep records that the code that is currently being transpiled
40-
// depends on a given Go object.
41-
func (c *Collector) DeclareDCEDep(o types.Object) {
42-
if c.dependencies == nil {
43-
return // Dependencies are not being collected.
37+
// depends on a given Go object with optional type arguments.
38+
//
39+
// The given optional type arguments are used to when the object is a
40+
// function with type parameters or anytime the object doesn't carry them.
41+
// If not given, this attempts to get the type arguments from the object.
42+
func (c *Collector) DeclareDCEDep(o types.Object, tArgs ...types.Type) {
43+
if c.dce != nil {
44+
c.dce.addDep(o, tArgs)
4445
}
45-
c.dependencies[o] = struct{}{}
4646
}

0 commit comments

Comments
 (0)