Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions cl/internal/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,16 @@ func (p *Converter) Process() error {
}
return fmt.Errorf("ConvMacro: %w", err)
}
ctx.setGoFile(goFile)

ctx.setGoFile(goFile.FileName, func() {
// todo(zzy):write build condition to file
// var os, arch string
// if goFile.Condition != nil {
// os = goFile.Condition.OS
// arch = goFile.Condition.Arch
// }
})

err = ctx.NewMacro(goName, macro)
if err != nil {
return err
Expand All @@ -124,7 +133,9 @@ func (p *Converter) Process() error {
}
return fmt.Errorf("ConvDecl: %w", err)
}
ctx.setGoFile(goFile)
ctx.setGoFile(goFile.FileName, func() {
// todo(zzy):write build condition to file
})
switch decl := decl.(type) {
case *ast.TypeDecl:
err = ctx.NewTypeDecl(goName, decl, pnc)
Expand Down
4 changes: 3 additions & 1 deletion cl/internal/convert/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,10 @@ func (p *Package) LookupFunc(goName string, fn *ast.FuncDecl) (*GoFuncSpec, erro
return NewGoFuncSpec(goName), nil
}

// If not have goFile in current package,it will create a new file,and the init function will be execute
// to keep the unsafe package load to use go:linkname command
func (p *Package) setGoFile(goFile string) {
func (p *Package) setGoFile(goFile string, init func()) {
// todo(zzy):support write build condition to file
p.p.SetCurFile(goFile, true)
// todo(zzy):avoid remark
p.p.Unsafe().MarkForceUsed(p.p)
Expand Down
11 changes: 7 additions & 4 deletions cl/nc/ncimpl/ncimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type Converter struct {
KeepUnderScore bool
}

func (p *Converter) convFile(file string, obj *ast.Object) (goFile string, ok bool) {
func (p *Converter) convFile(file string, obj *ast.Object) (goFile *nc.GoFile, ok bool) {
info, exist := p.FileMap[file]
if !exist {
var availableFiles []string
Expand All @@ -58,10 +58,13 @@ func (p *Converter) convFile(file string, obj *ast.Object) (goFile string, ok bo
if obj != nil && obj.Name != nil && hf.FileType == llconfig.Third {
p.locMap.Add(obj.Name, obj.Loc)
}
return hf.ToGoFileName(p.PkgName), hf.InCurPkg()
return &nc.GoFile{
FileName: hf.ToGoFileName(p.PkgName),
// todo(zzy):support condition
}, hf.InCurPkg()
}

func (p *Converter) ConvDecl(file string, decl ast.Decl) (goName, goFile string, err error) {
func (p *Converter) ConvDecl(file string, decl ast.Decl) (goName string, goFile *nc.GoFile, err error) {
obj := ast.ObjectOf(decl)
goFile, ok := p.convFile(file, obj)
if !ok {
Expand All @@ -88,7 +91,7 @@ func (p *Converter) ConvDecl(file string, decl ast.Decl) (goName, goFile string,
return
}

func (p *Converter) ConvMacro(file string, macro *ast.Macro) (goName, goFile string, err error) {
func (p *Converter) ConvMacro(file string, macro *ast.Macro) (goName string, goFile *nc.GoFile, err error) {
goFile, ok := p.convFile(file, nil)
if !ok {
err = nc.ErrSkip
Expand Down
30 changes: 15 additions & 15 deletions cl/nc/ncimpl/ncimpl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ func TestConverterConvFile(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
goFile, ok := converter.convFile(tc.file, nil)

if goFile != tc.expected {
t.Errorf("Expected file %s, got %s", tc.expected, goFile)
if goFile.FileName != tc.expected {
t.Errorf("Expected file %s, got %s", tc.expected, goFile.FileName)
}

if ok != tc.ok {
Expand Down Expand Up @@ -226,7 +226,7 @@ func TestConv(t *testing.T) {
trimPrefixes []string
expectFile string
expectName string
exec func(conv *Converter, convNode ast.Node) (string, string, error)
exec func(conv *Converter, convNode ast.Node) (string, *nc.GoFile, error)
expectErr error
}{
{
Expand All @@ -235,7 +235,7 @@ func TestConv(t *testing.T) {
Name: "MACRO_NAME",
Loc: &ast.Location{File: interFile},
},
exec: func(conv *Converter, convNode ast.Node) (string, string, error) {
exec: func(conv *Converter, convNode ast.Node) (string, *nc.GoFile, error) {
return conv.ConvMacro(interFile, convNode.(*ast.Macro))
},
expectFile: "inter.go",
Expand All @@ -248,7 +248,7 @@ func TestConv(t *testing.T) {
Name: "MACRO_NAME",
Loc: &ast.Location{File: interFile},
},
exec: func(conv *Converter, convNode ast.Node) (string, string, error) {
exec: func(conv *Converter, convNode ast.Node) (string, *nc.GoFile, error) {
return conv.ConvMacro(interFile, convNode.(*ast.Macro))
},
trimPrefixes: []string{"MACRO_"},
Expand All @@ -263,7 +263,7 @@ func TestConv(t *testing.T) {
Loc: &ast.Location{File: interFile},
},
pubs: map[string]string{"MACRO_CONST": "CustomMacro"},
exec: func(conv *Converter, convNode ast.Node) (string, string, error) {
exec: func(conv *Converter, convNode ast.Node) (string, *nc.GoFile, error) {
return conv.ConvMacro(interFile, convNode.(*ast.Macro))
},
expectFile: "inter.go",
Expand All @@ -276,7 +276,7 @@ func TestConv(t *testing.T) {
Name: "MACRO_NAME",
Loc: &ast.Location{File: thirdFile},
},
exec: func(conv *Converter, convNode ast.Node) (string, string, error) {
exec: func(conv *Converter, convNode ast.Node) (string, *nc.GoFile, error) {
return conv.ConvMacro(thirdFile, convNode.(*ast.Macro))
},
expectFile: "testpkg_autogen.go",
Expand All @@ -292,7 +292,7 @@ func TestConv(t *testing.T) {
MangledName: "validFunc",
},
convSym: mockSymConv,
exec: func(conv *Converter, convNode ast.Node) (string, string, error) {
exec: func(conv *Converter, convNode ast.Node) (string, *nc.GoFile, error) {
return conv.ConvDecl(interFile, convNode.(*ast.FuncDecl))
},
expectFile: "inter.go",
Expand All @@ -307,7 +307,7 @@ func TestConv(t *testing.T) {
Loc: &ast.Location{File: thirdFile},
},
},
exec: func(conv *Converter, convNode ast.Node) (string, string, error) {
exec: func(conv *Converter, convNode ast.Node) (string, *nc.GoFile, error) {
return conv.ConvDecl(thirdFile, convNode.(*ast.FuncDecl))
},
expectFile: "testpkg_autogen.go",
Expand All @@ -322,7 +322,7 @@ func TestConv(t *testing.T) {
},
MangledName: "noSymbolFunc",
},
exec: func(conv *Converter, convNode ast.Node) (string, string, error) {
exec: func(conv *Converter, convNode ast.Node) (string, *nc.GoFile, error) {
return conv.ConvDecl(interFile, convNode.(*ast.FuncDecl))
},
convSym: mockSymConv,
Expand All @@ -337,7 +337,7 @@ func TestConv(t *testing.T) {
Loc: &ast.Location{File: interFile},
},
},
exec: func(conv *Converter, convNode ast.Node) (string, string, error) {
exec: func(conv *Converter, convNode ast.Node) (string, *nc.GoFile, error) {
return conv.ConvDecl(interFile, convNode.(*ast.EnumTypeDecl))
},
expectFile: "inter.go",
Expand All @@ -352,7 +352,7 @@ func TestConv(t *testing.T) {
Loc: &ast.Location{File: interFile},
},
},
exec: func(conv *Converter, convNode ast.Node) (string, string, error) {
exec: func(conv *Converter, convNode ast.Node) (string, *nc.GoFile, error) {
return conv.ConvDecl(interFile, convNode.(*ast.EnumTypeDecl))
},
expectFile: "inter.go",
Expand All @@ -366,7 +366,7 @@ func TestConv(t *testing.T) {
Loc: &ast.Location{File: interFile},
},
},
exec: func(conv *Converter, convNode ast.Node) (string, string, error) {
exec: func(conv *Converter, convNode ast.Node) (string, *nc.GoFile, error) {
return conv.ConvDecl(interFile, convNode.(*ast.TypeDecl))
},
expectFile: "inter.go",
Expand All @@ -388,8 +388,8 @@ func TestConv(t *testing.T) {
if goName != tc.expectName {
t.Errorf("Expected %s, got %s", tc.expectName, goName)
}
if goFile != tc.expectFile {
t.Errorf("Expected %s, got %s", tc.expectFile, goFile)
if goFile.FileName != tc.expectFile {
t.Errorf("Expected %s, got %s", tc.expectFile, goFile.FileName)
}
if err != tc.expectErr {
t.Errorf("Expected %v, got %v", tc.expectErr, err)
Expand Down
14 changes: 12 additions & 2 deletions cl/nc/nodeconv.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,19 @@ var (
ErrSkip = errors.New("skip this node")
)

type Condition struct {
OS string // OS,like darwin,linux,windows
Arch string // Architecture,like amd64,arm64
}

type GoFile struct {
FileName string // Go file name,like cJSON.go ini_darwin_amd64.go
Condition *Condition // Condition for the given file,if no condition,it is nil
}

type NodeConverter interface {
ConvDecl(file string, decl ast.Decl) (goName, goFile string, err error)
ConvMacro(file string, macro *ast.Macro) (goName, goFile string, err error)
ConvDecl(file string, decl ast.Decl) (goName string, goFile *GoFile, err error)
ConvMacro(file string, macro *ast.Macro) (goName string, goFile *GoFile, err error)
ConvEnumItem(decl *ast.EnumTypeDecl, item *ast.EnumItem) (goName string, err error)
ConvTagExpr(cname string) string
Lookup(name string) (locFile string, ok bool)
Expand Down
Loading