llcppsigfetch:builtin type convert
This commit is contained in:
@@ -3,6 +3,7 @@ package parse
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
"github.com/goplus/llgo/c"
|
"github.com/goplus/llgo/c"
|
||||||
@@ -124,6 +125,9 @@ func (ct *Converter) ProcessType(t clang.Type) ast.Expr {
|
|||||||
// return cache
|
// return cache
|
||||||
// }
|
// }
|
||||||
var expr ast.Expr
|
var expr ast.Expr
|
||||||
|
if t.Kind >= clang.TypeFirstBuiltin && t.Kind <= clang.TypeLastBuiltin {
|
||||||
|
return ct.ProcessBuiltinType(t)
|
||||||
|
}
|
||||||
switch t.Kind {
|
switch t.Kind {
|
||||||
case clang.TypePointer:
|
case clang.TypePointer:
|
||||||
expr = &ast.PointerType{X: ct.ProcessType(t.PointeeType())}
|
expr = &ast.PointerType{X: ct.ProcessType(t.PointeeType())}
|
||||||
@@ -168,3 +172,72 @@ func (ct *Converter) ProcessFunc(cursor clang.Cursor) {
|
|||||||
func (ct *Converter) ProcessClass(cursor clang.Cursor) {
|
func (ct *Converter) ProcessClass(cursor clang.Cursor) {
|
||||||
println("todo: Process class")
|
println("todo: Process class")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ct *Converter) ProcessBuiltinType(t clang.Type) *ast.BuiltinType {
|
||||||
|
kind := ast.Void
|
||||||
|
var flags ast.TypeFlag
|
||||||
|
|
||||||
|
switch t.Kind {
|
||||||
|
case clang.TypeVoid:
|
||||||
|
kind = ast.Void
|
||||||
|
case clang.TypeBool:
|
||||||
|
kind = ast.Bool
|
||||||
|
case clang.TypeCharU, clang.TypeUChar, clang.TypeCharS, clang.TypeSChar:
|
||||||
|
kind = ast.Char
|
||||||
|
case clang.TypeChar16:
|
||||||
|
kind = ast.Char16
|
||||||
|
case clang.TypeChar32:
|
||||||
|
kind = ast.Char32
|
||||||
|
case clang.TypeWChar:
|
||||||
|
kind = ast.WChar
|
||||||
|
case clang.TypeShort, clang.TypeUShort:
|
||||||
|
kind = ast.Int
|
||||||
|
flags |= ast.Short
|
||||||
|
case clang.TypeInt, clang.TypeUInt:
|
||||||
|
kind = ast.Int
|
||||||
|
case clang.TypeLong, clang.TypeULong:
|
||||||
|
kind = ast.Int
|
||||||
|
flags |= ast.Long
|
||||||
|
case clang.TypeLongLong, clang.TypeULongLong:
|
||||||
|
kind = ast.Int
|
||||||
|
flags |= ast.LongLong
|
||||||
|
case clang.TypeInt128, clang.TypeUInt128:
|
||||||
|
kind = ast.Int128
|
||||||
|
case clang.TypeFloat:
|
||||||
|
kind = ast.Float
|
||||||
|
case clang.TypeHalf, clang.TypeFloat16:
|
||||||
|
kind = ast.Float16
|
||||||
|
case clang.TypeDouble:
|
||||||
|
kind = ast.Float
|
||||||
|
flags |= ast.Double
|
||||||
|
case clang.TypeLongDouble:
|
||||||
|
kind = ast.Float
|
||||||
|
flags |= ast.Long | ast.Double
|
||||||
|
case clang.TypeFloat128:
|
||||||
|
kind = ast.Float128
|
||||||
|
default:
|
||||||
|
// like IBM128,NullPtr,Accum
|
||||||
|
fmt.Fprintln(os.Stderr, "todo: unknown builtin type:", c.GoString(t.Kind.String().CStr()))
|
||||||
|
}
|
||||||
|
|
||||||
|
if IsExplicitSigned(t) {
|
||||||
|
flags |= ast.Signed
|
||||||
|
} else if IsExplicitUnsigned(t) {
|
||||||
|
flags |= ast.Unsigned
|
||||||
|
}
|
||||||
|
|
||||||
|
return &ast.BuiltinType{
|
||||||
|
Kind: kind,
|
||||||
|
Flags: flags,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func IsExplicitSigned(t clang.Type) bool {
|
||||||
|
return t.Kind == clang.TypeCharS || t.Kind == clang.TypeSChar
|
||||||
|
}
|
||||||
|
|
||||||
|
func IsExplicitUnsigned(t clang.Type) bool {
|
||||||
|
return t.Kind == clang.TypeCharU || t.Kind == clang.TypeUChar ||
|
||||||
|
t.Kind == clang.TypeUShort || t.Kind == clang.TypeUInt ||
|
||||||
|
t.Kind == clang.TypeULong || t.Kind == clang.TypeULongLong ||
|
||||||
|
t.Kind == clang.TypeUInt128
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,23 +2,57 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/goplus/llgo/c"
|
"github.com/goplus/llgo/c/clang"
|
||||||
"github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse"
|
"github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse"
|
||||||
|
"github.com/goplus/llgo/chore/llcppg/ast"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if c.Argc != 2 {
|
TestBuiltinType()
|
||||||
fmt.Fprintln(os.Stderr, "Usage: cvt <headerFile>")
|
}
|
||||||
return
|
func TestBuiltinType() {
|
||||||
}
|
tests := []struct {
|
||||||
sourceFile := *c.Advance(c.Argv, 1)
|
name string
|
||||||
c.Printf(c.Str("sourceFile: %s\n"), sourceFile)
|
typeKind clang.TypeKind
|
||||||
converter, err := parse.NewConverter(c.GoString(sourceFile))
|
expected ast.BuiltinType
|
||||||
if err != nil {
|
}{
|
||||||
panic(err)
|
{"Void", clang.TypeVoid, ast.BuiltinType{Kind: ast.Void}},
|
||||||
}
|
{"Bool", clang.TypeBool, ast.BuiltinType{Kind: ast.Bool}},
|
||||||
defer converter.Dispose()
|
{"Char_S", clang.TypeCharS, ast.BuiltinType{Kind: ast.Char, Flags: ast.Signed}},
|
||||||
converter.Convert()
|
{"Char_U", clang.TypeCharU, ast.BuiltinType{Kind: ast.Char, Flags: ast.Unsigned}},
|
||||||
|
{"Char16", clang.TypeChar16, ast.BuiltinType{Kind: ast.Char16}},
|
||||||
|
{"Char32", clang.TypeChar32, ast.BuiltinType{Kind: ast.Char32}},
|
||||||
|
{"WChar", clang.TypeWChar, ast.BuiltinType{Kind: ast.WChar}},
|
||||||
|
{"Short", clang.TypeShort, ast.BuiltinType{Kind: ast.Int, Flags: ast.Short}},
|
||||||
|
{"UShort", clang.TypeUShort, ast.BuiltinType{Kind: ast.Int, Flags: ast.Short | ast.Unsigned}},
|
||||||
|
{"Int", clang.TypeInt, ast.BuiltinType{Kind: ast.Int}},
|
||||||
|
{"UInt", clang.TypeUInt, ast.BuiltinType{Kind: ast.Int, Flags: ast.Unsigned}},
|
||||||
|
{"Long", clang.TypeLong, ast.BuiltinType{Kind: ast.Int, Flags: ast.Long}},
|
||||||
|
{"ULong", clang.TypeULong, ast.BuiltinType{Kind: ast.Int, Flags: ast.Long | ast.Unsigned}},
|
||||||
|
{"LongLong", clang.TypeLongLong, ast.BuiltinType{Kind: ast.Int, Flags: ast.LongLong}},
|
||||||
|
{"ULongLong", clang.TypeULongLong, ast.BuiltinType{Kind: ast.Int, Flags: ast.LongLong | ast.Unsigned}},
|
||||||
|
{"Int128", clang.TypeInt128, ast.BuiltinType{Kind: ast.Int128}},
|
||||||
|
{"UInt128", clang.TypeUInt128, ast.BuiltinType{Kind: ast.Int128, Flags: ast.Unsigned}},
|
||||||
|
{"Float", clang.TypeFloat, ast.BuiltinType{Kind: ast.Float}},
|
||||||
|
{"Half", clang.TypeHalf, ast.BuiltinType{Kind: ast.Float16}},
|
||||||
|
{"Float16", clang.TypeFloat16, ast.BuiltinType{Kind: ast.Float16}},
|
||||||
|
{"Double", clang.TypeDouble, ast.BuiltinType{Kind: ast.Float, Flags: ast.Double}},
|
||||||
|
{"LongDouble", clang.TypeLongDouble, ast.BuiltinType{Kind: ast.Float, Flags: ast.Long | ast.Double}},
|
||||||
|
{"Float128", clang.TypeFloat128, ast.BuiltinType{Kind: ast.Float128}},
|
||||||
|
{"Unknown", clang.TypeIbm128, ast.BuiltinType{Kind: ast.Void}},
|
||||||
|
}
|
||||||
|
converter := &parse.Converter{}
|
||||||
|
converter.Convert()
|
||||||
|
for _, bt := range tests {
|
||||||
|
t := clang.Type{Kind: bt.typeKind}
|
||||||
|
res := converter.ProcessBuiltinType(t)
|
||||||
|
if res.Kind != bt.expected.Kind {
|
||||||
|
fmt.Printf("%s Kind mismatch:got %d want %d, \n", bt.name, res.Kind, bt.expected.Kind)
|
||||||
|
}
|
||||||
|
if res.Flags != bt.expected.Flags {
|
||||||
|
fmt.Printf("%s Flags mismatch:got %d,want %d\n", bt.name, res.Flags, bt.expected.Flags)
|
||||||
|
}
|
||||||
|
fmt.Printf("%s:flags:%d kind:%d\n", bt.name, res.Flags, res.Kind)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
30
chore/_xtool/llcppsigfetch/parse/cvt_test/llgo.expect
Normal file
30
chore/_xtool/llcppsigfetch/parse/cvt_test/llgo.expect
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
#stdout
|
||||||
|
Void:flags:0 kind:0
|
||||||
|
Bool:flags:0 kind:1
|
||||||
|
Char_S:flags:1 kind:2
|
||||||
|
Char_U:flags:2 kind:2
|
||||||
|
Char16:flags:0 kind:3
|
||||||
|
Char32:flags:0 kind:4
|
||||||
|
WChar:flags:0 kind:5
|
||||||
|
Short:flags:32 kind:6
|
||||||
|
UShort:flags:34 kind:6
|
||||||
|
Int:flags:0 kind:6
|
||||||
|
UInt:flags:2 kind:6
|
||||||
|
Long:flags:4 kind:6
|
||||||
|
ULong:flags:6 kind:6
|
||||||
|
LongLong:flags:8 kind:6
|
||||||
|
ULongLong:flags:10 kind:6
|
||||||
|
Int128:flags:0 kind:7
|
||||||
|
UInt128:flags:2 kind:7
|
||||||
|
Float:flags:0 kind:8
|
||||||
|
Half:flags:0 kind:9
|
||||||
|
Float16:flags:0 kind:9
|
||||||
|
Double:flags:16 kind:8
|
||||||
|
LongDouble:flags:20 kind:8
|
||||||
|
Float128:flags:0 kind:10
|
||||||
|
Unknown:flags:0 kind:0
|
||||||
|
|
||||||
|
#stderr
|
||||||
|
todo: unknown builtin type: Ibm128
|
||||||
|
|
||||||
|
#exit 0
|
||||||
Reference in New Issue
Block a user