Skip to content

cl: basic framework - #701

Merged
xushiwei merged 3 commits into
goplus:devfrom
xushiwei:q
Sep 10, 2026
Merged

cl: basic framework#701
xushiwei merged 3 commits into
goplus:devfrom
xushiwei:q

Conversation

@xushiwei

Copy link
Copy Markdown
Member

No description provided.

@xushiwei
xushiwei merged commit fa640e3 into goplus:dev Sep 10, 2026
2 checks passed

@fennoai fennoai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review: cl package skeleton + clang method renames

This PR lays down the C-to-Go compiler front-end skeleton (compileDecl/compileFunc, blockCtx, type/var helpers, a test helper) and mechanically renames many clang.Cursor methods in lib/clang/clang.go. The structure follows the established XGo/llcppg conventions and builds/vets cleanly. Most panic("todo") and commented-out blocks are expected WIP scaffolding.

Verified as correct (not flagged):

  • The lib/clang/clang.go renames are consistent: every reordered doc comment stayed matched to the correct // llgo:link C.clang_* target and function body. The double-space comment typo fixes are also fine.
  • for i := range n with newParam(i c.Int) is correct under Go 1.27 (range-over-int yields the operand's type c.Int), and DbgFlagAll computes to 3 as intended.

A few noteworthy items are left as inline comments below. The most important is the void-return handling (already TODO-flagged) and the inaccurate Config.Fset doc comment.

Comment thread cl/compile.go
pkg := ctx.pkg
retType := fn.ResultType() // TODO(xsw): return void
tyRet := toType(ctx, retType, flagRetType)
results = types.NewTuple(pkg.NewParam(token.NoPos, "", tyRet, false))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Void return produces an invalid signature. results is unconditionally built as a single unnamed result: for a C function returning void, toType will yield a void tyRet and this generates a Go func with one unnamed result rather than no results. The author already flagged this with // TODO(xsw): return void (line 236); worth resolving before toType is implemented, since void-returning functions are common and would otherwise emit broken output.

Comment thread cl/compile.go
// Config specifies the configuration for compiling C/C++ header files.
type Config struct {
// Fset provides source position information for syntax trees and types.
// If Fset is nil, Load will use a new fileset, but preserve Fset's value.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doc comment is inaccurate and self-contradictory: there is no Load function in this package (the entry point is NewPackage), and "If Fset is nil ... preserve Fset's value" cannot be true when Fset is nil. NewPackage passes conf.Fset to gogen and never writes it back. Suggest: // If Fset is nil, NewPackage will create a new fileset.

Comment thread cl/compile.go
origName := fnName
rewritten := ctx.getPubName(&fnName)
n := fn.NumArguments()
var params []*types.Var

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exact argument count is already known from n := fn.NumArguments(), so the append loop below reallocates the backing array as it grows. Preallocate to avoid the copies:

params := make([]*types.Var, 0, n+1) // +1 for the optional variadic param

Comment thread cl/compile.go
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")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NewPackage/loadFile carry an err error return, but this path (and compileDecl's log.Panicln("compileDecl: unknown kind ...") at line 211, and substObj) crash the process instead of propagating errors. Duplicate symbols and unhandled declaration kinds are input-controlled, so any header with a currently-unhandled top-level kind aborts. Consider deciding the error strategy — propagate err vs. panic-and-recover at the top level — since the public API advertises error.

Comment thread cl/compile_test.go
continue
}
t.Run(name, func(t *testing.T) {
pkgDir := dir + "/" + name

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: dir was built with path.Join above, but this joins manually. Prefer pkgDir := path.Join(dir, name) for consistency.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant