Skip to content
This repository has been archived by the owner on May 18, 2024. It is now read-only.

Commit

Permalink
support CompoundLiteralExpr
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Jul 19, 2022
1 parent 27e01bc commit 173dc73
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
12 changes: 12 additions & 0 deletions cl/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions clang/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 10 additions & 0 deletions testdata/compoundlit/compoundlit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <stdio.h>

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;
}
35 changes: 35 additions & 0 deletions testdata/compoundlit/libc.go
Original file line number Diff line number Diff line change
@@ -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{}

0 comments on commit 173dc73

Please sign in to comment.