Skip to content

Commit

Permalink
Merge pull request #1812 from xushiwei/q
Browse files Browse the repository at this point in the history
astEmptyEntrypoint
  • Loading branch information
xushiwei committed Mar 9, 2024
2 parents 7d1567d + e00fcee commit 1523b64
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 30 deletions.
33 changes: 33 additions & 0 deletions cl/classfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,4 +377,37 @@ func astEmptyFunc(entry string) *ast.FuncDecl {
}
}

func astEmptyEntrypoint(f *ast.File) {
var entry = getEntrypoint(f)
var hasEntry bool
for _, decl := range f.Decls {
switch d := decl.(type) {
case *ast.FuncDecl:
if d.Name.Name == entry {
hasEntry = true
}
}
}
if !hasEntry {
f.Decls = append(f.Decls, astEmptyFunc(entry))
}
}

func getEntrypoint(f *ast.File) string {
switch {
case f.IsProj:
return "MainEntry"
case f.IsClass:
return "Main"
case inMainPkg(f):
return "main"
default:
return "init"
}
}

func inMainPkg(f *ast.File) bool {
return f.Name.Name == "main"
}

// -----------------------------------------------------------------------------
31 changes: 1 addition & 30 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,23 +657,6 @@ func initGopPkg(ctx *pkgCtx, pkg *gogen.Package, gopSyms map[string]bool) {
gogen.InitThisGopPkg(pkg.Types)
}

func inMainPkg(f *ast.File) bool {
return f.Name.Name == "main"
}

func getEntrypoint(f *ast.File) string {
switch {
case f.IsProj:
return "MainEntry"
case f.IsClass:
return "Main"
case inMainPkg(f):
return "main"
default:
return "init"
}
}

func loadFile(ctx *pkgCtx, f *ast.File) {
for _, decl := range f.Decls {
switch d := decl.(type) {
Expand Down Expand Up @@ -860,19 +843,7 @@ func preloadGopFile(p *gogen.Package, ctx *blockCtx, file string, f *ast.File, c
if d := f.ShadowEntry; d != nil {
d.Name.Name = getEntrypoint(f)
} else if f.IsProj && !conf.NoAutoGenMain && inMainPkg(f) {
var entry = getEntrypoint(f)
var hasEntry bool
for _, decl := range f.Decls {
switch d := decl.(type) {
case *ast.FuncDecl:
if d.Name.Name == entry {
hasEntry = true
}
}
}
if !hasEntry {
f.Decls = append(f.Decls, astEmptyFunc(entry))
}
astEmptyEntrypoint(f)
}
preloadFile(p, ctx, f, goFile, !conf.Outline)
if goxTestFile {
Expand Down

0 comments on commit 1523b64

Please sign in to comment.