-
Notifications
You must be signed in to change notification settings - Fork 562
[WIP] cl: ast.OverloadFuncDecl preload overload func #2469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1005,7 +1005,7 @@ func preloadFile(p *gogen.Package, ctx *blockCtx, f *ast.File, goFile string, ge | |||||||||||||||||||||||||||||||||||||
| old, _ := p.SetCurFile(goFile, true) | ||||||||||||||||||||||||||||||||||||||
| defer p.RestoreCurFile(old) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| preloadFuncDecl := func(d *ast.FuncDecl) { | ||||||||||||||||||||||||||||||||||||||
| preloadFuncDecl := func(d *ast.FuncDecl, overload bool) { | ||||||||||||||||||||||||||||||||||||||
| if ctx.classRecv != nil { // in class file (.spx/.gmx) | ||||||||||||||||||||||||||||||||||||||
| if recv := d.Recv; recv == nil || len(recv.List) == 0 { | ||||||||||||||||||||||||||||||||||||||
| d.Recv = ctx.classRecv | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -1018,7 +1018,13 @@ func preloadFile(p *gogen.Package, ctx *blockCtx, f *ast.File, goFile string, ge | |||||||||||||||||||||||||||||||||||||
| fn := func() { | ||||||||||||||||||||||||||||||||||||||
| old, _ := p.SetCurFile(goFile, true) | ||||||||||||||||||||||||||||||||||||||
| defer p.RestoreCurFile(old) | ||||||||||||||||||||||||||||||||||||||
| loadFunc(ctx, nil, fname, d, genFnBody) | ||||||||||||||||||||||||||||||||||||||
| if overload { | ||||||||||||||||||||||||||||||||||||||
| pkg := ctx.pkg.Types | ||||||||||||||||||||||||||||||||||||||
| fn := gogen.NewOverloadFunc(d.Name.Pos(), pkg, fname) | ||||||||||||||||||||||||||||||||||||||
| pkg.Scope().Insert(fn) | ||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||
| loadFunc(ctx, nil, fname, d, genFnBody) | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| if fname == "init" { | ||||||||||||||||||||||||||||||||||||||
| if genFnBody { | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -1060,7 +1066,13 @@ func preloadFile(p *gogen.Package, ctx *blockCtx, f *ast.File, goFile string, ge | |||||||||||||||||||||||||||||||||||||
| defer p.RestoreCurFile(old) | ||||||||||||||||||||||||||||||||||||||
| doInitType(ld) | ||||||||||||||||||||||||||||||||||||||
| recv := toRecv(ctx, d.Recv) | ||||||||||||||||||||||||||||||||||||||
| loadFunc(ctx, recv, fname, d, genFnBody) | ||||||||||||||||||||||||||||||||||||||
| if overload { | ||||||||||||||||||||||||||||||||||||||
| pkg := ctx.pkg.Types | ||||||||||||||||||||||||||||||||||||||
| typ := pkg.Scope().Lookup(tname).Type().(*types.Named) | ||||||||||||||||||||||||||||||||||||||
| gogen.NewOverloadMethod(typ, d.Name.Pos(), pkg, fname) | ||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||
| loadFunc(ctx, recv, fname, d, genFnBody) | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| ld.methods = append(ld.methods, fn) | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -1194,7 +1206,7 @@ func preloadFile(p *gogen.Package, ctx *blockCtx, f *ast.File, goFile string, ge | |||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| case *ast.FuncDecl: | ||||||||||||||||||||||||||||||||||||||
| preloadFuncDecl(d) | ||||||||||||||||||||||||||||||||||||||
| preloadFuncDecl(d, false) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| case *ast.OverloadFuncDecl: | ||||||||||||||||||||||||||||||||||||||
| var recv *ast.Ident | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -1282,12 +1294,33 @@ func preloadFile(p *gogen.Package, ctx *blockCtx, f *ast.File, goFile string, ge | |||||||||||||||||||||||||||||||||||||
| Name: id, | ||||||||||||||||||||||||||||||||||||||
| Type: expr.Type, | ||||||||||||||||||||||||||||||||||||||
| Body: expr.Body, | ||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||
| }, false) | ||||||||||||||||||||||||||||||||||||||
| default: | ||||||||||||||||||||||||||||||||||||||
| ctx.handleErrorf(expr.Pos(), expr.End(), "unknown func %v", ctx.LoadExpr(expr)) | ||||||||||||||||||||||||||||||||||||||
| break LoopFunc | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| if d.Recv == nil { | ||||||||||||||||||||||||||||||||||||||
| ctx.lbinames = append(ctx.lbinames, d.Name.Name) | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| preloadFuncDecl(&ast.FuncDecl{ | ||||||||||||||||||||||||||||||||||||||
| Doc: d.Doc, | ||||||||||||||||||||||||||||||||||||||
| Recv: d.Recv, | ||||||||||||||||||||||||||||||||||||||
| Name: d.Name, | ||||||||||||||||||||||||||||||||||||||
| Type: &ast.FuncType{ | ||||||||||||||||||||||||||||||||||||||
| Params: &ast.FieldList{ | ||||||||||||||||||||||||||||||||||||||
| List: []*ast.Field{ | ||||||||||||||||||||||||||||||||||||||
| &ast.Field{ | ||||||||||||||||||||||||||||||||||||||
| Names: []*ast.Ident{ | ||||||||||||||||||||||||||||||||||||||
| ast.NewIdent(overloadArgs), | ||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||
| Type: ast.NewIdent("any"), | ||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
| List: []*ast.Field{ | |
| &ast.Field{ | |
| Names: []*ast.Ident{ | |
| ast.NewIdent(overloadArgs), | |
| }, | |
| Type: ast.NewIdent("any"), | |
| }, | |
| }, | |
| // Create placeholder function signature for overload preloading | |
| // with single parameter (__xgo_overload_args__: any) | |
| Params: &ast.FieldList{ | |
| List: []*ast.Field{ | |
| { | |
| Names: []*ast.Ident{ast.NewIdent(overloadArgs)}, | |
| Type: ast.NewIdent("any"), | |
| }, | |
| }, | |
| }, |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Type field of this ast.FuncDecl is constructed with a specific signature, but it appears to be unused. When preloadFuncDecl is called with overload=true, as it is here, the code path that uses the Type field is not executed. This makes the Type definition unnecessary and potentially confusing.
To improve clarity and remove this dead code, you can replace this block with Type: nil.
Type: nil,There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Documentation: Add doc comment explaining this constant's purpose as placeholder parameter name for preloading overload signatures.
| overloadArgs = "__xgo_overload_args__" | |
| const ( | |
| // overloadArgs is the placeholder parameter name used when preloading overload function | |
| // signatures. This allows overload functions to be registered in the package scope | |
| // during the preload phase before actual implementations are processed. | |
| overloadArgs = "__xgo_overload_args__" | |
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Security & Quality Issue: Unsafe type lookup/assertion chain. This will panic if
Lookup(tname)returns nil or type is not*types.Named.