Skip to content

Commit

Permalink
cl: types record scope
Browse files Browse the repository at this point in the history
  • Loading branch information
visualfc committed Feb 21, 2024
1 parent b015d6f commit d78d079
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
15 changes: 13 additions & 2 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gox.Package,
c2goBase: c2goBase(conf.C2goBase), imports: make(map[string]pkgImp), isGopFile: true,
}
if rec := ctx.rec; rec != nil {
rec.Scope(f, fileScope)
rec.Scope(f.File, fileScope)
}
preloadGopFile(p, ctx, f.path, f.File, conf)
}
Expand Down Expand Up @@ -1235,7 +1235,7 @@ func loadFunc(ctx *blockCtx, recv *types.Var, d *ast.FuncDecl, genBody bool) {
commentFunc(ctx, fn, d)
if rec := ctx.recorder(); rec != nil {
rec.Def(d.Name, fn.Func)
if recv == nil {
if recv == nil && d.Name.Name != "_" {
ctx.fileScope.Insert(fn.Func)
}
}
Expand Down Expand Up @@ -1308,6 +1308,14 @@ var unaryGopNames = map[string]string{
func loadFuncBody(ctx *blockCtx, fn *gox.Func, body *ast.BlockStmt, src ast.Node) {
cb := fn.BodyStart(ctx.pkg, body)
compileStmts(ctx, body.List)
if rec := ctx.recorder(); rec != nil {
switch fn := src.(type) {
case *ast.FuncDecl:
rec.Scope(fn.Type, cb.Scope())
case *ast.FuncLit:
rec.Scope(fn.Type, cb.Scope())
}
}
cb.End(src)
}

Expand Down Expand Up @@ -1354,6 +1362,9 @@ func loadImport(ctx *blockCtx, spec *ast.ImportSpec) {
} else {
rec.Implicit(spec, pkgName)
}
if name != "_" {
ctx.fileScope.Insert(pkgName)
}
}

if specName != nil {
Expand Down
36 changes: 36 additions & 0 deletions cl/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ func compileStmt(ctx *blockCtx, stmt ast.Stmt) {
case *ast.BlockStmt:
ctx.cb.Block()
compileStmts(ctx, v.List)
if rec := ctx.recorder(); rec != nil {
rec.Scope(v, ctx.cb.Scope())
}
ctx.cb.End()
return
case *ast.SelectStmt:
Expand Down Expand Up @@ -363,8 +366,14 @@ func compileRangeStmt(ctx *blockCtx, v *ast.RangeStmt) {
if len(defineNames) > 0 {
defNames(ctx, defineNames, cb.Scope())
}
if rec := ctx.recorder(); rec != nil {
rec.Scope(v, cb.Scope())
}
cb.VBlock()
compileStmts(ctx, v.Body.List)
if rec := ctx.recorder(); rec != nil {
rec.Scope(v.Body, cb.Scope())
}
cb.End(v.Body)
cb.SetComments(comments, once)
setBodyHandler(ctx)
Expand Down Expand Up @@ -509,6 +518,9 @@ func compileForStmt(ctx *blockCtx, v *ast.ForStmt) {
cb := ctx.cb
comments, once := cb.BackupComments()
cb.For(v)
if rec := ctx.recorder(); rec != nil {
rec.Scope(v, cb.Scope())
}
if v.Init != nil {
compileStmt(ctx, v.Init)
}
Expand All @@ -518,6 +530,9 @@ func compileForStmt(ctx *blockCtx, v *ast.ForStmt) {
cb.None()
}
cb.Then(v.Body)
if rec := ctx.recorder(); rec != nil {
rec.Scope(v.Body, cb.Scope())
}
compileStmts(ctx, v.Body.List)
if v.Post != nil {
cb.Post()
Expand All @@ -541,6 +556,9 @@ func compileIfStmt(ctx *blockCtx, v *ast.IfStmt) {
compileStmt(ctx, v.Init)
}
compileExpr(ctx, v.Cond)
if rec := ctx.recorder(); rec != nil {
rec.Scope(v, cb.Scope())
}
cb.Then(v.Body)
compileStmts(ctx, v.Body.List)
if e := v.Else; e != nil {
Expand All @@ -552,6 +570,9 @@ func compileIfStmt(ctx *blockCtx, v *ast.IfStmt) {
}
}
cb.SetComments(comments, once)
if rec := ctx.recorder(); rec != nil {
rec.Scope(v.Body, cb.Scope())
}
cb.End(v)
}

Expand Down Expand Up @@ -586,6 +607,9 @@ func compileTypeSwitchStmt(ctx *blockCtx, v *ast.TypeSwitchStmt) {
panic("TODO: type switch syntax error, please use x.(type)")
}
cb.TypeSwitch(name, v)
if rec := ctx.recorder(); rec != nil {
rec.Scope(v, cb.Scope())
}
if v.Init != nil {
compileStmt(ctx, v.Init)
}
Expand Down Expand Up @@ -636,6 +660,9 @@ func compileTypeSwitchStmt(ctx *blockCtx, v *ast.TypeSwitchStmt) {
cb.Then()
compileStmts(ctx, c.Body)
commentStmt(ctx, stmt)
if rec := ctx.recorder(); rec != nil {
rec.Scope(c, cb.Scope())
}
cb.End(c)
}
cb.SetComments(comments, once)
Expand Down Expand Up @@ -666,6 +693,9 @@ func compileSwitchStmt(ctx *blockCtx, v *ast.SwitchStmt) {
} else {
cb.None() // switch {...}
}
if rec := ctx.recorder(); rec != nil {
rec.Scope(v, cb.Scope())
}
cb.Then(v.Body)
seen := make(valueMap)
var firstDefault ast.Stmt
Expand Down Expand Up @@ -715,6 +745,9 @@ func compileSwitchStmt(ctx *blockCtx, v *ast.SwitchStmt) {
cb.Fallthrough()
}
commentStmt(ctx, stmt)
if rec := ctx.recorder(); rec != nil {
rec.Scope(c, cb.Scope())
}
cb.End(c)
}
cb.SetComments(comments, once)
Expand Down Expand Up @@ -764,6 +797,9 @@ func compileSelectStmt(ctx *blockCtx, v *ast.SelectStmt) {
cb.Then()
compileStmts(ctx, c.Body)
commentStmt(ctx, stmt)
if rec := ctx.recorder(); rec != nil {
rec.Scope(c, cb.Scope())
}
cb.End(c)
}
cb.SetComments(comments, once)
Expand Down

0 comments on commit d78d079

Please sign in to comment.