-
Notifications
You must be signed in to change notification settings - Fork 11
cl: compileFunc; clang: FileContents #705
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
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 |
|---|---|---|
|
|
@@ -17,7 +17,6 @@ | |
| package cl | ||
|
|
||
| import ( | ||
| "go/ast" | ||
| "go/token" | ||
| "go/types" | ||
| "log" | ||
|
|
@@ -47,20 +46,6 @@ func SetDebug(flags int) { | |
|
|
||
| // ----------------------------------------------------------------------------- | ||
|
|
||
| type nodeInterp struct { | ||
| fset *token.FileSet | ||
| } | ||
|
|
||
| func (p *nodeInterp) Position(start token.Pos) token.Position { | ||
| return p.fset.Position(start) | ||
| } | ||
|
|
||
| func (p *nodeInterp) LoadExpr(v ast.Node) string { | ||
| panic("todo: nodeInterp.LoadExpr") | ||
| } | ||
|
|
||
| // ----------------------------------------------------------------------------- | ||
|
|
||
| type PkgInfo struct { | ||
| } | ||
|
|
||
|
|
@@ -101,6 +86,7 @@ type Config struct { | |
| // Source represents a C/C++ header to compile. | ||
| type Source struct { | ||
| TU clang.TranslationUnit | ||
| Handle clang.File | ||
| PresumedFile *c.Char | ||
| } | ||
|
|
||
|
|
@@ -138,10 +124,11 @@ func NewPackage(pkgPath, pkgName string, file Source, conf *Config) (pkg Package | |
| // ----------------------------------------------------------------------------- | ||
|
|
||
| func loadFile(p *gogen.Package, conf *Config, file Source) (pi *PkgInfo, err error) { | ||
| c := p.Import("github.com/lib/c") | ||
| c := p.Import("github.com/goplus/lib/c") | ||
| ctx := &blockCtx{ | ||
| pkg: p, cb: p.CB(), fset: p.Fset, c: c, | ||
| } | ||
| ctx.initFile(file) | ||
| _ = conf | ||
| clang.VisitChildren(file.TU.Cursor(), func(decl, parent clang.Cursor) clang.ChildVisitResult { | ||
| compileDecl(ctx, decl) | ||
|
|
@@ -243,9 +230,9 @@ func compileFunc(ctx *blockCtx, fn clang.Cursor) { | |
| results = types.NewTuple(pkg.NewParam(token.NoPos, "", tyRet, false)) | ||
| } | ||
| sig := types.NewSignatureType(nil, nil, nil, types.NewTuple(params...), results, variadic) | ||
| f := types.NewFunc(ctx.goNodePos(fn), pkg.Types, fnName, sig) | ||
| if old := pkg.Types.Scope().Insert(f); old != nil { | ||
| log.Panicln("Go func", fnName, "redefined") | ||
| f, err := pkg.NewFuncWith(goNodePos(ctx, fn), fnName, sig, nil) | ||
|
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. [P2] compileFunc: redeclaration will panic on repeated C decls This calls |
||
| if err != nil { | ||
| log.Panicln("compileFunc:", fnName, err) | ||
| } | ||
| // ctx.addExternFunc(fnName) | ||
| if rewritten { | ||
|
|
@@ -356,7 +343,7 @@ func newParam(ctx *blockCtx, decl clang.Cursor, i c.Int) *types.Var { | |
| } else { | ||
| declName = "__llcppg_param" + strconv.Itoa(int(i)+1) | ||
| } | ||
| return types.NewParam(ctx.goNodePos(decl), ctx.pkg.Types, declName, typ) | ||
| return types.NewParam(goNodePos(ctx, decl), ctx.pkg.Types, declName, typ) | ||
| } | ||
|
|
||
| // ----------------------------------------------------------------------------- | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,13 +62,17 @@ func newPointer(typ types.Type) types.Type { | |
|
|
||
| func toType(ctx *blockCtx, typ lc.Type, flags int) types.Type { | ||
| switch typ.Kind { | ||
| case lc.TypeCharS: | ||
| return ctx.c.Ref("Char").Type() | ||
| case lc.TypeInt: | ||
| return ctx.c.Ref("Int").Type() | ||
| case lc.TypeUInt: | ||
| return ctx.c.Ref("UInt").Type() | ||
| return ctx.c.Ref("Uint").Type() | ||
| case lc.TypePointer: | ||
| pointee := toType(ctx, typ.PointeeType(), flags) | ||
| return newPointer(pointee) | ||
| default: | ||
| log.Println("==> toType: unknown Kind -", typ.Kind) | ||
|
Comment on lines
+74
to
+75
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. [P3] toType: default branch logs then falls through to panic The new |
||
| } | ||
| panic("todo: toType " + clang.String(typ)) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -90,6 +90,13 @@ func (i Index) ParseTranslationUnit(options uint, filename string, args ...strin | |
|
|
||
| // ----------------------------------------------------------------------------- | ||
|
|
||
| /** | ||
| * A particular source file that is part of a translation unit. | ||
| */ | ||
| type File = clang.File | ||
|
|
||
| // ----------------------------------------------------------------------------- | ||
|
|
||
| /** | ||
| * A single translation unit, which resides in an index. | ||
| */ | ||
|
|
@@ -104,6 +111,18 @@ func (u TranslationUnit) Dispose() { | |
| u.impl.Dispose() | ||
| } | ||
|
|
||
| // File returns the File object corresponding to the given filename in the translation unit. | ||
| func (u TranslationUnit) File(filename string) File { | ||
| return u.impl.File(c.AllocaCStr(filename)) | ||
| } | ||
|
|
||
| // FileContents returns the contents of the specified file in the translation unit. | ||
| func (u TranslationUnit) FileContents(file File) []byte { | ||
| var size c.SizeT | ||
| data := u.impl.FileContents(file, &size) | ||
| return unsafe.Slice((*byte)(unsafe.Pointer(data)), int(size)) | ||
|
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] FileContents: guard nil pointer and unsigned->int size narrowing
Note also that the returned |
||
| } | ||
|
|
||
| /** | ||
| * Retrieve the cursor that represents the given translation unit. | ||
| * | ||
|
|
||
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.
[P3] Source.PresumedFile: unused, undocumented exported field
PresumedFile *c.Charis an exported field that is never populated or read anywhere in the repo. Either add a doc comment describing its intended use, or drop it to avoid an undocumented public API surface. Raw*c.Charvalues (e.g. from aclang.String) also carry a use-after-free footgun once their source isDispose()d, so an ownership note would help if it's kept.