Skip to content
This repository has been archived by the owner on May 18, 2024. It is now read-only.

Commit

Permalink
mv varDecls at switch beginning
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Apr 14, 2022
1 parent c1875c3 commit caf8e99
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
19 changes: 15 additions & 4 deletions cl/blockctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ type switchCtx struct {
done *gox.Label
next *gox.Label
defau *gox.Label
vdefs *gox.VarDefs
scope *types.Scope
tag types.Object
notmat types.Object // notMatched
}
Expand Down Expand Up @@ -157,14 +159,23 @@ func (p *blockCtx) getSwitchCtx() *switchCtx {
return nil
}

func (p *blockCtx) enterLoop() *loopCtx {
f := &loopCtx{parent: p.curflow}
func (p *blockCtx) getVarDefs(scope *types.Scope) (vdefs *gox.VarDefs, inSwitch bool) {
if sw := p.getSwitchCtx(); sw != nil && sw.scope == scope {
return sw.vdefs, true
}
return p.pkg.NewVarDefs(scope), false
}

func (p *blockCtx) enterSwitch() *switchCtx {
scope := p.cb.Scope()
vdefs := p.pkg.NewVarDefs(scope)
f := &switchCtx{parent: p.curflow, vdefs: vdefs, scope: scope}
p.curflow = f
return f
}

func (p *blockCtx) enterSwitch() *switchCtx {
f := &switchCtx{parent: p.curflow}
func (p *blockCtx) enterLoop() *loopCtx {
f := &loopCtx{parent: p.curflow}
p.curflow = f
return f
}
Expand Down
16 changes: 12 additions & 4 deletions cl/type_and_var.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,16 +247,24 @@ func newVarAndInit(ctx *blockCtx, scope *types.Scope, typ types.Type, decl *ast.
if debugCompileDecl {
log.Println("var", decl.Name, typ, "-", decl.Kind)
}
varDecl := ctx.pkg.NewVarDefs(scope).New(goNodePos(decl), typ, decl.Name)
vdefs, inSwitch := ctx.getVarDefs(scope)
varDecl := vdefs.New(goNodePos(decl), typ, decl.Name)
if len(decl.Inner) > 0 {
initExpr := decl.Inner[0]
if ufs, ok := checkUnion(ctx, typ); ok {
initUnionVar(ctx, decl.Name, ufs, initExpr)
return
}
cb := varDecl.InitStart(ctx.pkg)
varInit(ctx, typ, initExpr)
cb.EndInit(1)
if inSwitch {
addr := varDecl.Ref(decl.Name)
cb := ctx.cb.VarRef(addr)
varInit(ctx, typ, initExpr)
cb.Assign(1)
} else {
cb := varDecl.InitStart(ctx.pkg)
varInit(ctx, typ, initExpr)
cb.EndInit(1)
}
}
}

Expand Down

0 comments on commit caf8e99

Please sign in to comment.