diff options
author | Ayke van Laethem <[email protected]> | 2019-05-16 22:16:58 +0200 |
---|---|---|
committer | Ron Evans <[email protected]> | 2019-05-17 19:37:20 +0200 |
commit | dfa713040a7b3be012dc229996440005734aa9b4 (patch) | |
tree | b9e6085df2788c1c10e792739ea6d79b7adf2aa8 /cgo | |
parent | 82dc14b74174bc5114271773d6eb7d3858ad862d (diff) | |
download | tinygo-dfa713040a7b3be012dc229996440005734aa9b4.tar.gz tinygo-dfa713040a7b3be012dc229996440005734aa9b4.zip |
cgo: add support for enum types
Enum types are implemented as named types (with possible accompanying
typedefs as type aliases). The constants inside the enums are treated as
Go constants like in the Go toolchain.
Diffstat (limited to 'cgo')
-rw-r--r-- | cgo/cgo.go | 59 | ||||
-rw-r--r-- | cgo/libclang.go | 44 | ||||
-rw-r--r-- | cgo/libclang_stubs.c | 8 |
3 files changed, 111 insertions, 0 deletions
diff --git a/cgo/cgo.go b/cgo/cgo.go index 0ef32127d..d31fe57a4 100644 --- a/cgo/cgo.go +++ b/cgo/cgo.go @@ -35,6 +35,7 @@ type cgoPackage struct { globals map[string]globalInfo typedefs map[string]*typedefInfo elaboratedTypes map[string]*elaboratedTypeInfo + enums map[string]enumInfo } // constantInfo stores some information about a CGo constant found by libclang @@ -71,6 +72,12 @@ type elaboratedTypeInfo struct { pos token.Pos } +// enumInfo contains information about an enum in the C. +type enumInfo struct { + typeExpr ast.Expr + pos token.Pos +} + // globalInfo contains information about a declared global variable in C. type globalInfo struct { typeExpr ast.Expr @@ -140,6 +147,7 @@ func Process(files []*ast.File, dir string, fset *token.FileSet, cflags []string globals: map[string]globalInfo{}, typedefs: map[string]*typedefInfo{}, elaboratedTypes: map[string]*elaboratedTypeInfo{}, + enums: map[string]enumInfo{}, } // Add a new location for the following file. @@ -243,6 +251,9 @@ func Process(files []*ast.File, dir string, fset *token.FileSet, cflags []string // Add elaborated types for C structs and unions. p.addElaboratedTypes() + // Add enum types and enum constants for C enums. + p.addEnumTypes() + // Patch the AST to use the declared types and functions. for _, f := range files { astutil.Apply(f, p.walker, nil) @@ -574,6 +585,54 @@ func (p *cgoPackage) addElaboratedTypes() { p.generated.Decls = append(p.generated.Decls, gen) } +// addEnumTypes adds C enums to the AST. For example, the following C code: +// +// enum option { +// optionA, +// optionB = 5, +// }; +// +// is translated to the following Go code equivalent: +// +// type C.enum_option int32 +// +// The constants are treated just like macros so are inserted into the AST by +// addConstDecls. +// See also: https://en.cppreference.com/w/c/language/enum +func (p *cgoPackage) addEnumTypes() { + if len(p.enums) == 0 { + return + } + gen := &ast.GenDecl{ + TokPos: token.NoPos, + Tok: token.TYPE, + } + names := make([]string, 0, len(p.enums)) + for name := range p.enums { + names = append(names, name) + } + sort.Strings(names) + for _, name := range names { + typ := p.enums[name] + typeName := "C.enum_" + name + obj := &ast.Object{ + Kind: ast.Typ, + Name: typeName, + } + typeSpec := &ast.TypeSpec{ + Name: &ast.Ident{ + NamePos: typ.pos, + Name: typeName, + Obj: obj, + }, + Type: typ.typeExpr, + } + obj.Decl = typeSpec + gen.Specs = append(gen.Specs, typeSpec) + } + p.generated.Decls = append(p.generated.Decls, gen) +} + // findMissingCGoNames traverses the AST and finds all C.something names. Only // these symbols are extracted from the parsed C AST and converted to the Go // equivalent. diff --git a/cgo/libclang.go b/cgo/libclang.go index 24dac8c3c..cca4487ec 100644 --- a/cgo/libclang.go +++ b/cgo/libclang.go @@ -49,9 +49,12 @@ GoCXCursor tinygo_clang_Cursor_getArgument(GoCXCursor c, unsigned i); CXSourceLocation tinygo_clang_getCursorLocation(GoCXCursor c); CXSourceRange tinygo_clang_getCursorExtent(GoCXCursor c); CXTranslationUnit tinygo_clang_Cursor_getTranslationUnit(GoCXCursor c); +long long tinygo_clang_getEnumConstantDeclValue(GoCXCursor c); +CXType tinygo_clang_getEnumDeclIntegerType(GoCXCursor c); int tinygo_clang_globals_visitor(GoCXCursor c, GoCXCursor parent, CXClientData client_data); int tinygo_clang_struct_visitor(GoCXCursor c, GoCXCursor parent, CXClientData client_data); +int tinygo_clang_enum_visitor(GoCXCursor c, GoCXCursor parent, CXClientData client_data); */ import "C" @@ -501,6 +504,8 @@ func (p *cgoPackage) makeASTType(typ C.CXType, pos token.Pos) ast.Expr { switch underlying.kind { case C.CXType_Record: return p.makeASTType(underlying, pos) + case C.CXType_Enum: + return p.makeASTType(underlying, pos) default: panic("unknown elaborated type") } @@ -580,6 +585,32 @@ func (p *cgoPackage) makeASTType(typ C.CXType, pos token.Pos) ast.Expr { NamePos: pos, Name: "C." + cgoName, } + case C.CXType_Enum: + cursor := C.tinygo_clang_getTypeDeclaration(typ) + name := getString(C.tinygo_clang_getCursorSpelling(cursor)) + underlying := C.tinygo_clang_getEnumDeclIntegerType(cursor) + if name == "" { + // anonymous enum + ref := storedRefs.Put(p) + defer storedRefs.Remove(ref) + C.tinygo_clang_visitChildren(cursor, C.CXCursorVisitor(C.tinygo_clang_enum_visitor), C.CXClientData(ref)) + return p.makeASTType(underlying, pos) + } else { + // named enum + if _, ok := p.enums[name]; !ok { + ref := storedRefs.Put(p) + defer storedRefs.Remove(ref) + C.tinygo_clang_visitChildren(cursor, C.CXCursorVisitor(C.tinygo_clang_enum_visitor), C.CXClientData(ref)) + p.enums[name] = enumInfo{ + typeExpr: p.makeASTType(underlying, pos), + pos: pos, + } + } + return &ast.Ident{ + NamePos: pos, + Name: "C.enum_" + name, + } + } } if typeName == "" { // Fallback, probably incorrect but at least the error points to an odd @@ -622,3 +653,16 @@ func tinygo_clang_struct_visitor(c, parent C.GoCXCursor, client_data C.CXClientD fieldList.List = append(fieldList.List, field) return C.CXChildVisit_Continue } + +//export tinygo_clang_enum_visitor +func tinygo_clang_enum_visitor(c, parent C.GoCXCursor, client_data C.CXClientData) C.int { + p := storedRefs.Get(unsafe.Pointer(client_data)).(*cgoPackage) + name := getString(C.tinygo_clang_getCursorSpelling(c)) + pos := p.getCursorPosition(c) + value := C.tinygo_clang_getEnumConstantDeclValue(c) + p.constants[name] = constantInfo{ + expr: &ast.BasicLit{pos, token.INT, strconv.FormatInt(int64(value), 10)}, + pos: pos, + } + return C.CXChildVisit_Continue +} diff --git a/cgo/libclang_stubs.c b/cgo/libclang_stubs.c index c282540a4..0ccfd4ab1 100644 --- a/cgo/libclang_stubs.c +++ b/cgo/libclang_stubs.c @@ -56,3 +56,11 @@ CXSourceRange tinygo_clang_getCursorExtent(CXCursor c) { CXTranslationUnit tinygo_clang_Cursor_getTranslationUnit(CXCursor c) { return clang_Cursor_getTranslationUnit(c); } + +long long tinygo_clang_getEnumConstantDeclValue(CXCursor c) { + return clang_getEnumConstantDeclValue(c); +} + +CXType tinygo_clang_getEnumDeclIntegerType(CXCursor c) { + return clang_getEnumDeclIntegerType(c); +} |