Skip to content

Commit

Permalink
gogensig:ensure consistent param name in func sig
Browse files Browse the repository at this point in the history
  • Loading branch information
luoliwoshang committed Dec 23, 2024
1 parent 3a7391f commit 3fa2fd8
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
"c++": "exec",
"go": "Exec"
},
{
"mangle": "mprintf",
"c++": "mprintf",
"go": "Mprintf"
},
{
"mangle": "OSSL_provider_init2",
"c++": "OSSL_provider_init2",
Expand Down
4 changes: 3 additions & 1 deletion cmd/gogensig/convert/_testdata/funcrefer/gogensig.expect
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
type CallBack func(unsafe.Pointer) c.Int
//go:linkname Exec C.exec
func Exec(L unsafe.Pointer, cb CallBack)
//go:linkname Mprintf C.mprintf
func Mprintf(__llgo_arg_0 *int8, __llgo_va_list ...interface{}) *int8

type Hooks struct {
MallocFn unsafe.Pointer
Expand Down Expand Up @@ -41,7 +43,7 @@ type OsslLibCtxSt struct {
}
type OSSLLIBCTX OsslLibCtxSt
//go:linkname ProviderAddBuiltin C.OSSL_PROVIDER_add_builtin
func ProviderAddBuiltin(*OSSLLIBCTX, name *int8, init_fn OSSLProviderInitFn2) c.Int
func ProviderAddBuiltin(__llgo_arg_0 *OSSLLIBCTX, name *int8, init_fn OSSLProviderInitFn2) c.Int

===== llcppg.pub =====
CallBack
Expand Down
1 change: 1 addition & 0 deletions cmd/gogensig/convert/_testdata/funcrefer/hfile/temp.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <stdio.h>
typedef int (*CallBack)(void *L);
void exec(void *L, CallBack cb);
char *mprintf(const char*,...);

typedef struct Hooks
{
Expand Down
2 changes: 1 addition & 1 deletion cmd/gogensig/convert/_testdata/sqlite/gogensig.expect
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (p *Sqlite3) CloseV2() c.Int {
// llgo:type C
type Callback func(unsafe.Pointer, c.Int, **int8, **int8) c.Int
// llgo:link (*Sqlite3).Exec C.sqlite3_exec
func (p *Sqlite3) Exec(sql *int8, callback func(unsafe.Pointer, c.Int, **int8, **int8) c.Int, unsafe.Pointer, errmsg **int8) c.Int {
func (p *Sqlite3) Exec(sql *int8, callback func(unsafe.Pointer, c.Int, **int8, **int8) c.Int, __llgo_arg_2 unsafe.Pointer, errmsg **int8) c.Int {
return 0
}

Expand Down
45 changes: 30 additions & 15 deletions cmd/gogensig/convert/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,23 @@ func (p *TypeConv) fieldListToParams(params *ast.FieldList) (*types.Tuple, bool,
if params == nil {
return types.NewTuple(), false, nil
}
vars, err := p.fieldListToVars(params)

hasNamedParam := false
for _, field := range params.List {
if field == nil {
continue
}
if len(field.Names) > 0 {
hasNamedParam = true
break
}
if _, ok := field.Type.(*ast.Variadic); ok {
hasNamedParam = true
break
}
}

vars, err := p.fieldListToVars(params, hasNamedParam)
if err != nil {
return nil, false, err
}
Expand Down Expand Up @@ -258,13 +274,14 @@ func (p *TypeConv) retToResult(ret ast.Expr) (*types.Tuple, error) {
}

// Convert ast.FieldList to []types.Var
func (p *TypeConv) fieldListToVars(params *ast.FieldList) ([]*types.Var, error) {
func (p *TypeConv) fieldListToVars(params *ast.FieldList, hasNamedParam bool) ([]*types.Var, error) {
var vars []*types.Var
if params == nil || params.List == nil {
return vars, nil
}
for _, field := range params.List {
fieldVar, err := p.fieldToVar(field)

for index, field := range params.List {
fieldVar, err := p.fieldToVar(field, hasNamedParam, index)
if err != nil {
return nil, err
}
Expand All @@ -282,7 +299,7 @@ func (p *TypeConv) defaultRecordField() []*types.Var {
}
}

func (p *TypeConv) fieldToVar(field *ast.Field) (*types.Var, error) {
func (p *TypeConv) fieldToVar(field *ast.Field, hasNamedParam bool, argIndex int) (*types.Var, error) {
if field == nil {
return nil, fmt.Errorf("%w: unexpected nil field", ErrTypeConv)
}
Expand All @@ -291,6 +308,8 @@ func (p *TypeConv) fieldToVar(field *ast.Field) (*types.Var, error) {
var name string
if len(field.Names) > 0 {
name = field.Names[0].Name
} else if hasNamedParam {
name = fmt.Sprintf("__llgo_arg_%d", argIndex)
}

typ, err := p.ToType(field.Type)
Expand All @@ -302,7 +321,11 @@ func (p *TypeConv) fieldToVar(field *ast.Field) (*types.Var, error) {
name = getFieldName(name)
} else {
_, isVariadic := field.Type.(*ast.Variadic)
name = getParamName(name, isVariadic)
if isVariadic && hasNamedParam {
name = "__llgo_va_list"
} else {
name = avoidKeyword(name)
}
}
return types.NewVar(token.NoPos, p.Types, name, typ), nil
}
Expand All @@ -312,7 +335,7 @@ func (p *TypeConv) RecordTypeToStruct(recordType *ast.RecordType) (types.Type, e
p.ctx = Record
defer func() { p.ctx = ctx }()
var fields []*types.Var
flds, err := p.fieldListToVars(recordType.Fields)
flds, err := p.fieldListToVars(recordType.Fields, false)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -398,14 +421,6 @@ func (p *TypeConv) LookupSymbol(mangleName config.MangleNameType) (config.GoName
return e.GoName, nil
}

// isVariadic determines if the field is a variadic parameter
func getParamName(name string, isVaradic bool) string {
if isVaradic {
return "__llgo_va_list"
}
return avoidKeyword(name)
}

// The field name should be public if it's a record field
func getFieldName(name string) string {
return names.PubName(name)
Expand Down

0 comments on commit 3fa2fd8

Please sign in to comment.