From 173dc73d1e16f617bccbb1c5911c167a7eb14485 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Tue, 19 Jul 2022 17:09:43 +0800 Subject: [PATCH] support CompoundLiteralExpr --- cl/expr.go | 12 ++++++++++ clang/ast/ast.go | 1 + testdata/compoundlit/compoundlit.c | 10 +++++++++ testdata/compoundlit/libc.go | 35 ++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+) create mode 100644 testdata/compoundlit/compoundlit.c create mode 100644 testdata/compoundlit/libc.go diff --git a/cl/expr.go b/cl/expr.go index fbbb84a..4b6d923 100644 --- a/cl/expr.go +++ b/cl/expr.go @@ -69,6 +69,8 @@ func compileExprEx(ctx *blockCtx, expr *ast.Node, prompt string, flags int) { case ast.OffsetOfExpr: compileOffsetOfExpr(ctx, expr) case ast.VisibilityAttr: + case ast.CompoundLiteralExpr: + compileCompoundLiteralExpr(ctx, expr) default: log.Panicln(prompt, expr.Kind) } @@ -82,6 +84,16 @@ func compileExprLHS(ctx *blockCtx, expr *ast.Node) { compileExprEx(ctx, expr, unknownExprPrompt, flagLHS) } +func compileCompoundLiteralExpr(ctx *blockCtx, expr *ast.Node) { + typ := toType(ctx, expr.Type, 0) + switch inner := expr.Inner[0]; inner.Kind { + case ast.InitListExpr: + initLit(ctx, typ, inner) + default: + log.Panicln("compileCompoundLiteralExpr unexpected:", inner.Kind) + } +} + func compileIntegerLiteral(ctx *blockCtx, expr *ast.Node) { typ := toType(ctx, expr.Type, 0) ctx.cb.Typ(typ).Val(literal(token.INT, expr), ctx.goNode(expr)).Call(1) diff --git a/clang/ast/ast.go b/clang/ast/ast.go index 2666357..9239e45 100644 --- a/clang/ast/ast.go +++ b/clang/ast/ast.go @@ -118,6 +118,7 @@ const ( BinaryOperator Kind = "BinaryOperator" UnaryOperator Kind = "UnaryOperator" ConditionalOperator Kind = "ConditionalOperator" + CompoundLiteralExpr Kind = "CompoundLiteralExpr" CharacterLiteral Kind = "CharacterLiteral" IntegerLiteral Kind = "IntegerLiteral" StringLiteral Kind = "StringLiteral" diff --git a/testdata/compoundlit/compoundlit.c b/testdata/compoundlit/compoundlit.c new file mode 100644 index 0000000..0b62d4a --- /dev/null +++ b/testdata/compoundlit/compoundlit.c @@ -0,0 +1,10 @@ +#include + +void f(unsigned short a[]) { + printf("%d, %d, %d\n", a[0], a[1], a[2]); +} + +int main() { + f((unsigned short [3]){ 0x330e, 0, 16 }); + return 0; +} diff --git a/testdata/compoundlit/libc.go b/testdata/compoundlit/libc.go new file mode 100644 index 0000000..ffbce4a --- /dev/null +++ b/testdata/compoundlit/libc.go @@ -0,0 +1,35 @@ +package main + +import ( + "fmt" + "unsafe" +) + +func gostring(s *int8) string { + n, arr := 0, (*[1 << 20]byte)(unsafe.Pointer(s)) + for arr[n] != 0 { + n++ + } + return string(arr[:n]) +} + +func printf(format *int8, args ...interface{}) int32 { + goformat := gostring(format) + for i, arg := range args { + if v, ok := arg.(*int8); ok { + args[i] = gostring(v) + } + } + fmt.Printf(goformat, args...) + return 0 +} + +func __swbuf(_c int32, _p *FILE) int32 { + return _c +} + +type struct___sFILEX struct{} + +type struct__IO_marker struct{} +type struct__IO_codecvt struct{} +type struct__IO_wide_data struct{}