Skip to content

Commit

Permalink
gogensig:anonymous enum
Browse files Browse the repository at this point in the history
  • Loading branch information
luoliwoshang committed Oct 28, 2024
1 parent bf47ff1 commit e7b8263
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 37 deletions.
10 changes: 5 additions & 5 deletions chore/gogensig/convert/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,13 @@ const (
Feline_tiger Feline = 12
)
type Piecetype c.Int
type PieceType c.Int
const (
Piecetype_King Piecetype = 1
Piecetype_Queen Piecetype = 2
Piecetype_Rook Piecetype = 10
Piecetype_Pawn Piecetype = 11
PieceType_King PieceType = 1
PieceType_Queen PieceType = 2
PieceType_Rook PieceType = 10
PieceType_Pawn PieceType = 11
)
`, nil, func(t *testing.T, expected, content string) {
eq, diff := cmp.EqualStringIgnoreSpace(expected, content)
Expand Down
68 changes: 43 additions & 25 deletions chore/gogensig/convert/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (p *Package) linkLib(lib string) error {

func (p *Package) NewFuncDecl(funcDecl *ast.FuncDecl) error {
if debug {
log.Printf("NewFuncDecl: %s\n", funcDecl.Name.Name)
log.Printf("NewFuncDecl: %v\n", funcDecl.Name)
}
goFuncName, err := p.cvt.LookupSymbol(config.MangleNameType(funcDecl.MangledName))
if err != nil {
Expand Down Expand Up @@ -157,7 +157,7 @@ func (p *Package) NewTypeDecl(typeDecl *ast.TypeDecl) error {
return fmt.Errorf("type %s already defined", name)
}
if debug {
log.Printf("NewTypeDecl: %s\n", typeDecl.Name.Name)
log.Printf("NewTypeDecl: %v\n", typeDecl.Name)
}

structType, err := p.cvt.RecordTypeToStruct(typeDecl.Type)
Expand Down Expand Up @@ -213,37 +213,55 @@ func (p *Package) NewTypedefs(name string, typ types.Type) *types.Named {
}

func (p *Package) NewEnumTypeDecl(enumTypeDecl *ast.EnumTypeDecl) error {
if enumTypeDecl.Name == nil {
if debug {
log.Println("todo:anonymous enum")
}
return nil
}
if debug {
log.Printf("NewEnumTypeDecl: %s\n", enumTypeDecl.Name.Name)
log.Printf("NewEnumTypeDecl: %v\n", enumTypeDecl.Name)
}

enumName := ToTitle(p.cvt.RemovePrefixedName(enumTypeDecl.Name.Name))
if obj := p.p.Types.Scope().Lookup(enumName); obj != nil {
return fmt.Errorf("enum type %s already defined", enumName)
enumType, enumTypeName, err := p.createEnumType(enumTypeDecl.Name)
if err != nil {
return err
}

if len(enumTypeDecl.Type.Items) > 0 {
//default enum type is int
typ, err := p.cvt.ToDefaultEnumType(enumTypeDecl.Name)
err = p.createEnumItems(enumTypeDecl.Type.Items, enumType, enumTypeName)
if err != nil {
return err
}

Check warning on line 227 in chore/gogensig/convert/package.go

View check run for this annotation

Codecov / codecov/patch

chore/gogensig/convert/package.go#L226-L227

Added lines #L226 - L227 were not covered by tests
enumType := p.NewTypedefs(enumName, typ)
constDefs := p.p.NewConstDefs(p.p.CB().Scope())
for _, item := range enumTypeDecl.Type.Items {
name := enumName + "_" + item.Name.Name
val, _ := Expr(item.Value).ToInt()
constDefs.New(func(cb *gogen.CodeBuilder) int {
cb.Val(val)
return 1
}, 0, token.NoPos, enumType, name)
}
return nil
}

func (p *Package) createEnumType(enumName *ast.Ident) (types.Type, string, error) {
var name string
if enumName != nil {
name = ToTitle(p.cvt.RemovePrefixedName(enumName.Name))
}
if obj := p.p.Types.Scope().Lookup(name); obj != nil {
return nil, "", fmt.Errorf("enum type %s already defined", name)
}
enumType := p.cvt.ToDefaultEnumType()
if name != "" {
enumType = p.NewTypedefs(name, enumType)
}
return enumType, name, nil
}

func (p *Package) createEnumItems(items []*ast.EnumItem, enumType types.Type, enumTypeName string) error {
constDefs := p.p.NewConstDefs(p.p.Types.Scope())
for _, item := range items {
var constName string
if enumTypeName != "" {
constName = enumTypeName + "_" + item.Name.Name
} else {
constName = item.Name.Name
}
// maybe get a new name,because the after executed name,have lots situation will found same name
if obj := p.p.Types.Scope().Lookup(constName); obj != nil {
return fmt.Errorf("enum item %s already defined", constName)
}

Check warning on line 259 in chore/gogensig/convert/package.go

View check run for this annotation

Codecov / codecov/patch

chore/gogensig/convert/package.go#L258-L259

Added lines #L258 - L259 were not covered by tests
val, _ := Expr(item.Value).ToInt()
constDefs.New(func(cb *gogen.CodeBuilder) int {
cb.Val(val)
return 1
}, 0, token.NoPos, enumType, constName)
}
return nil
}
Expand Down
27 changes: 26 additions & 1 deletion chore/gogensig/convert/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1306,8 +1306,33 @@ const (
Color_Red Color = 0
Color_Green Color = 1
Color_Blue Color = 2
)`,
},
{
name: "anonymous enum",
decl: &ast.EnumTypeDecl{
Name: nil,
Type: &ast.EnumType{
Items: []*ast.EnumItem{
{Name: &ast.Ident{Name: "Red"}, Value: &ast.BasicLit{Kind: ast.IntLit, Value: "0"}},
{Name: &ast.Ident{Name: "Green"}, Value: &ast.BasicLit{Kind: ast.IntLit, Value: "1"}},
{Name: &ast.Ident{Name: "Blue"}, Value: &ast.BasicLit{Kind: ast.IntLit, Value: "2"}},
},
},
},
expected: `
package testpkg
import (
"github.com/goplus/llgo/c"
_ "unsafe"
)
`,
const (
Red c.Int = 0
Green c.Int = 1
Blue c.Int = 2
)`,
},
}
for _, tc := range testCases {
Expand Down
9 changes: 3 additions & 6 deletions chore/gogensig/convert/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,8 @@ func (p *TypeConv) RecordTypeToStruct(recordType *ast.RecordType) (types.Type, e
return types.NewStruct(fields, nil), nil
}

func (p *TypeConv) ToDefaultEnumType(name *ast.Ident) (types.Type, error) {
if name == nil {
return nil, fmt.Errorf("nil enum type name")
}
return p.typeMap.CType("Int"), nil
func (p *TypeConv) ToDefaultEnumType() types.Type {
return p.typeMap.CType("Int")
}

func (p *TypeConv) LookupSymbol(mangleName config.MangleNameType) (config.GoNameType, error) {
Expand Down Expand Up @@ -315,5 +312,5 @@ func (c *TypeConv) getRelativeHeaderPath(headerFile string) string {
}

func ToTitle(s string) string {
return strings.ToUpper(s[:1]) + strings.ToLower(s[1:])
return strings.ToUpper(s[:1]) + s[1:]
}

0 comments on commit e7b8263

Please sign in to comment.