-
Notifications
You must be signed in to change notification settings - Fork 11
cl compileFunc: cPubName, lookup manglingName #707
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
Changes from all commits
a71dc51
dcece70
62bcf34
aec817e
3b84f3a
4499b59
7b36540
6a76321
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| unsigned f(int a); | ||
|
|
||
| void g(); | ||
| void _g(); | ||
|
|
||
| signed int xprintf(const char* fmt, ...); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,15 @@ | ||
| package foo | ||
|
|
||
| import "github.com/goplus/lib/c" | ||
| import ( | ||
| "github.com/goplus/lib/c" | ||
| _ "unsafe" | ||
| ) | ||
|
|
||
| func f(a c.Int) c.Uint | ||
| func g() | ||
| func xprintf(fmt *c.Char, __llgo_va_list ...any) c.Int | ||
| //go:linkname F C.f | ||
| func F(a c.Int) c.Uint | ||
|
|
||
| //go:linkname X_g C._g | ||
| func X_g() | ||
|
|
||
| //go:linkname Xprintf C.xprintf | ||
| func Xprintf(fmt *c.Char, __llgo_va_list ...any) c.Int |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| package cl | ||
|
|
||
| import ( | ||
| "go/ast" | ||
| "go/token" | ||
| "go/types" | ||
| "log" | ||
|
|
@@ -55,7 +56,8 @@ type Package struct { | |
| pi *PkgInfo | ||
| } | ||
|
|
||
| // Reused specifies to reuse the Package instance between processing multiple C/C++ header files. | ||
| // Reused specifies to reuse the Package instance between processing multiple C/C++ | ||
| // header files. | ||
| type Reused struct { | ||
| pkg Package | ||
| } | ||
|
|
@@ -74,10 +76,12 @@ type Config struct { | |
| // Include specifies include searching directories. | ||
| Include []string | ||
|
|
||
| // Reused specifies to reuse the Package instance between processing multiple C/C++ header files. | ||
| // Reused specifies to reuse the Package instance between processing multiple C/C++ | ||
| // header files. | ||
| *Reused | ||
|
|
||
| // NameLookup looks up the archive path for a given mangling name. It returns the archive path and a boolean indicating whether the lookup was successful. | ||
| // NameLookup looks up the archive path for a given mangling name. It returns the | ||
| // archive path and a boolean indicating whether the lookup was successful. | ||
| NameLookup func(manglingName string) (archivePath string, ok bool) | ||
| } | ||
|
|
||
|
|
@@ -127,9 +131,9 @@ func loadFile(p *gogen.Package, conf *Config, file Source) (pi *PkgInfo, err err | |
| c := p.Import("github.com/goplus/lib/c") | ||
| ctx := &blockCtx{ | ||
| pkg: p, cb: p.CB(), fset: p.Fset, c: c, | ||
| nameLookup: conf.NameLookup, | ||
| } | ||
| ctx.initFile(file) | ||
| _ = conf | ||
| clang.VisitChildren(file.TU.Cursor(), func(decl, parent clang.Cursor) clang.ChildVisitResult { | ||
| compileDecl(ctx, decl) | ||
| return clang.Continue | ||
|
|
@@ -150,52 +154,7 @@ func compileDecl(ctx *blockCtx, decl clang.Cursor) { | |
| case lc.CursorVarDecl: | ||
| // compileVarDecl(ctx, decl, global) | ||
| case lc.CursorTypedefDecl: | ||
| /* origName, pub := decl.Name, false | ||
| if global { | ||
| pub = ctx.getPubName(&decl.Name) | ||
| } | ||
| compileTypedef(ctx, decl, global, pub) | ||
| if pub { | ||
| substObj(ctx.pkg.Types, scope, origName, scope.Lookup(decl.Name)) | ||
| } | ||
| case ast.RecordDecl: | ||
| pub := false | ||
| name, suKind := ctx.getSuName(decl, decl.TagUsed) | ||
| origName := name | ||
| if global { | ||
| if suKind == suAnonymous { | ||
| // pub = true if this is a public typedef | ||
| pub = i+1 < n && isPubTypedef(ctx, node.Inner[i+1]) | ||
| } else { | ||
| pub = ctx.getPubName(&name) | ||
| if decl.CompleteDefinition && ctx.checkExists(name) { | ||
| continue | ||
| } | ||
| } | ||
| } | ||
| typ, del := compileStructOrUnion(ctx, name, decl, pub) | ||
| if suKind != suAnonymous { | ||
| if pub { | ||
| substObj(ctx.pkg.Types, scope, origName, scope.Lookup(name)) | ||
| } | ||
| break | ||
| } | ||
| ctx.unnameds[decl.ID] = unnamedType{typ: typ, del: del} | ||
| for i+1 < n { | ||
| next := node.Inner[i+1] | ||
| if next.Kind == ast.VarDecl { | ||
| if ret, ok := checkAnonymous(ctx, scope, typ, next); ok { | ||
| compileVarWith(ctx, ret, next) | ||
| i++ | ||
| continue | ||
| } | ||
| } | ||
| break | ||
| } | ||
| case ast.EmptyDecl: | ||
| case ast.StaticAssertDecl: | ||
| continue | ||
| */ | ||
| // TODO(xsw) | ||
| case lc.CursorEnumDecl: | ||
| // compileEnum(ctx, decl, global) | ||
| default: | ||
|
|
@@ -205,6 +164,14 @@ func compileDecl(ctx *blockCtx, decl clang.Cursor) { | |
|
|
||
| // TODO(xsw): method support | ||
| func compileFunc(ctx *blockCtx, fn clang.Cursor) { | ||
| manglingName := clang.Mangling(fn) | ||
| if _, ok := ctx.nameLookup(manglingName); !ok { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P1] nil NameLookup panics compileFunc for callers that omit it
|
||
| if debugCompileDecl { | ||
| log.Println("func", clang.String(fn), "- skipped") | ||
| } | ||
| return | ||
| } | ||
|
|
||
| fnName := clang.String(fn) | ||
| if debugCompileDecl { | ||
| log.Println("func", fnName, "-", clang.String(fn.Type())) | ||
|
|
@@ -234,93 +201,16 @@ func compileFunc(ctx *blockCtx, fn clang.Cursor) { | |
| if err != nil { | ||
| log.Panicln("compileFunc:", fnName, err) | ||
| } | ||
| // ctx.addExternFunc(fnName) | ||
| ctx.forceImportUnsafe() | ||
| f.SetComments(pkg, &ast.CommentGroup{ | ||
| List: []*ast.Comment{ | ||
| {Text: "\n//go:linkname " + fnName + " C." + manglingName[1:]}, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P1] manglingName[1:] assumes a leading underscore (not portable to ELF)
|
||
| }, | ||
| }) | ||
| if rewritten { | ||
| scope := pkg.Types.Scope() | ||
| substObj(pkg.Types, scope, origName, f) | ||
| } | ||
| /* origName, rewritten := fnName, false | ||
| if !ctx.inHeader && fn.StorageClass == ast.Static { | ||
| fnName, rewritten = ctx.autoStaticName(origName), true | ||
| } else { | ||
| rewritten = ctx.getPubName(&fnName) | ||
| } | ||
| if body != nil { | ||
| if ctx.checkExists(fnName) { | ||
| return | ||
| } | ||
| isMain := false | ||
| if fnName == "main" && (results != nil || params != nil) { | ||
| fnName, isMain = "_cgo_main", true | ||
| } | ||
| f, err := pkg.NewFuncWith(ctx.goNodePos(fn), fnName, sig, nil) | ||
| if err != nil { | ||
| log.Panicln("compileFunc:", err) | ||
| } | ||
| if rewritten { // for fnName is a recursive function | ||
| scope := pkg.Types.Scope() | ||
| substObj(pkg.Types, scope, origName, f.Obj()) | ||
| rewritten = false | ||
| } | ||
| cb := f.BodyStart(pkg) | ||
| ctx.curfn = newFuncCtx(pkg, ctx.markComplicated(fnName, body), origName) | ||
| compileSub(ctx, body) | ||
| checkNeedReturn(ctx, body) | ||
| ctx.curfn = nil | ||
| cb.End() | ||
| if isMain { | ||
| var t *types.Var | ||
| var entryParams *types.Tuple | ||
| var entry = "main" | ||
| var testMain = ctx.testMain | ||
| if testMain { | ||
| entry = "TestMain" | ||
| testing := pkg.Import("testing") | ||
| t = pkg.NewParam(token.NoPos, "t", types.NewPointer(testing.Ref("T").Type())) | ||
| entryParams = types.NewTuple(t) | ||
| } | ||
| pkg.NewFunc(nil, entry, entryParams, nil, false).BodyStart(pkg) | ||
| if results != nil { | ||
| if testMain { | ||
| // if _cgo_ret := _cgo_main(); _cgo_ret != 0 { | ||
| // t.Fatal("exit status", _cgo_ret) | ||
| // } | ||
| cb.If().DefineVarStart(token.NoPos, retName) | ||
| } else { | ||
| // os.Exit(int(_cgo_main())) | ||
| cb.Val(pkg.Import("os").Ref("Exit")).Typ(types.Typ[types.Int]) | ||
| } | ||
| } | ||
| cb.Val(f.Obj()) | ||
| if params != nil { | ||
| panic("TODO: main func with params") | ||
| } | ||
| cb.Call(len(params)) | ||
| if results != nil { | ||
| if testMain { | ||
| cb.EndInit(1) | ||
| ret := cb.Scope().Lookup(retName) | ||
| cb.Val(ret).Val(0).BinaryOp(token.NEQ).Then(). | ||
| Val(t).MemberVal("Fatal").Val("exit status").Val(ret).Call(2).EndStmt(). | ||
| End() | ||
| } else { | ||
| cb.Call(1).Call(1) | ||
| } | ||
| } | ||
| cb.EndStmt().End() | ||
| } else { | ||
| delete(ctx.extfns, fnName) | ||
| } | ||
| } else if fn.IsUsed { | ||
| f := types.NewFunc(ctx.goNodePos(fn), pkg.Types, fnName, sig) | ||
| if pkg.Types.Scope().Insert(f) == nil { | ||
| ctx.addExternFunc(fnName) | ||
| } | ||
| } | ||
| if rewritten { | ||
| scope := pkg.Types.Scope() | ||
| substObj(pkg.Types, scope, origName, scope.Lookup(fnName)) | ||
| } */ | ||
| } | ||
|
|
||
| var ( | ||
|
|
@@ -341,7 +231,7 @@ func newParam(ctx *blockCtx, decl clang.Cursor, i c.Int) *types.Var { | |
| if declName != "" { | ||
| avoidKeyword(&declName) | ||
| } else { | ||
| declName = "__llcppg_param" + strconv.Itoa(int(i)+1) | ||
| declName = "_llcppg_param" + strconv.Itoa(int(i)+1) | ||
| } | ||
| return types.NewParam(goNodePos(ctx, decl), ctx.pkg.Types, declName, typ) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| //go:build linux | ||
|
|
||
| /* | ||
| * Copyright (c) 2026 The XGo Authors (xgo.dev). All rights reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package clang | ||
|
|
||
| import ( | ||
| "github.com/goplus/lib/c" | ||
| ) | ||
|
|
||
| // ----------------------------------------------------------------------------- | ||
|
|
||
| /** | ||
| * Retrieve a name for the entity referenced by this cursor. | ||
| */ | ||
| func Mangling(fn Cursor) string { | ||
| m := fn.Mangling() | ||
| manglingName := c.GoString(m.CStr()) | ||
| m.Dispose() | ||
| return "_" + manglingName | ||
| } | ||
|
|
||
| // ----------------------------------------------------------------------------- |
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.
[P2] cPubName indexes name[0] with no empty-string guard
cPubNamereadsname[0]without checking for an empty string;name == ""panics with index-out-of-range.fnNameoriginates fromclang.String(fn)on parsed headers, so an anonymous/empty name (or an unexpected clang result) triggers the panic viagetPubName. Cheap to guard:if name == "" { return name }at the top. (The ASCII byte-arithmetic uppercasing and theXprefix for leading_are otherwise correct and match the expected output.)