Repository: elliotchance/c2go
Pull request #871 (2020-07-26 20:32:24)

Add UnaryExprOrTypeTraitExpr first child types to transpiler


<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/elliotchance/c2go/871)
<!-- Reviewable:end -->



Patch:
diff --git a/ast/var_decl.go b/ast/var_decl.go
index 29ec13b..9414521 100644
--- a/ast/var_decl.go
+++ b/ast/var_decl.go
@@ -15,6 +15,7 @@ type VarDecl struct {
 	Type2        string
 	IsExtern     bool
 	IsUsed       bool
+	IsNRVO       bool
 	IsCInit      bool
 	IsReferenced bool
 	IsStatic     bool
@@ -34,6 +35,7 @@ func parseVarDecl(line string) *VarDecl {
 		(?P<type2>:'.*?')?
 		(?P<extern> extern)?
 		(?P<static> static)?
+		(?P<nrvo> nrvo)?
 		(?P<cinit> cinit)?
 		(?P<register> register)?
 		`,
@@ -55,6 +57,7 @@ func parseVarDecl(line string) *VarDecl {
 		Type2:        type2,
 		IsExtern:     len(groups["extern"]) > 0,
 		IsUsed:       len(groups["used"]) > 0,
+		IsNRVO:       len(groups["nrvo"]) > 0,
 		IsCInit:      len(groups["cinit"]) > 0,
 		IsReferenced: len(groups["referenced"]) > 0,
 		IsStatic:     len(groups["static"]) > 0,
diff --git a/transpiler/unary.go b/transpiler/unary.go
index 8b75915..0ed4ab7 100644
--- a/transpiler/unary.go
+++ b/transpiler/unary.go
@@ -717,6 +717,21 @@ func transpileUnaryExprOrTypeTraitExpr(n *ast.UnaryExprOrTypeTraitExpr, p *progr
 			case *ast.CallExpr:
 				t = ty.Type
 
+			case *ast.CStyleCastExpr:
+				t = ty.Type
+
+			case *ast.ConditionalOperator:
+				t = ty.Type
+
+			case *ast.BinaryOperator:
+				t = ty.Type
+
+			case *ast.IntegerLiteral:
+				t = ty.Type
+
+			case *ast.StringLiteral:
+				t = ty.Type
+
 			default:
 				panic(fmt.Sprintf("cannot do unary on: %#v", ty))
 			}


Test patch:
diff --git a/ast/var_decl_test.go b/ast/var_decl_test.go
index 8d81114..a8e43ad 100644
--- a/ast/var_decl_test.go
+++ b/ast/var_decl_test.go
@@ -15,6 +15,7 @@ func TestVarDecl(t *testing.T) {
 			Type2:        "",
 			IsExtern:     false,
 			IsUsed:       false,
+			IsNRVO:       false,
 			IsCInit:      false,
 			IsReferenced: false,
 			IsStatic:     false,
@@ -31,6 +32,7 @@ func TestVarDecl(t *testing.T) {
 			Type2:        "",
 			IsExtern:     true,
 			IsUsed:       false,
+			IsNRVO:       false,
 			IsCInit:      false,
 			IsReferenced: false,
 			IsStatic:     false,
@@ -47,6 +49,7 @@ func TestVarDecl(t *testing.T) {
 			Type2:        "unsigned long",
 			IsExtern:     false,
 			IsUsed:       false,
+			IsNRVO:       false,
 			IsCInit:      false,
 			IsReferenced: false,
 			IsStatic:     false,
@@ -63,6 +66,7 @@ func TestVarDecl(t *testing.T) {
 			Type2:        "",
 			IsExtern:     false,
 			IsUsed:       true,
+			IsNRVO:       false,
 			IsCInit:      false,
 			IsReferenced: false,
 			IsStatic:     false,
@@ -79,6 +83,7 @@ func TestVarDecl(t *testing.T) {
 			Type2:        "",
 			IsExtern:     false,
 			IsUsed:       true,
+			IsNRVO:       false,
 			IsCInit:      true,
 			IsReferenced: false,
 			IsStatic:     false,
@@ -95,6 +100,7 @@ func TestVarDecl(t *testing.T) {
 			Type2:        "",
 			IsExtern:     false,
 			IsUsed:       false,
+			IsNRVO:       false,
 			IsCInit:      false,
 			IsReferenced: true,
 			IsStatic:     false,
@@ -111,6 +117,7 @@ func TestVarDecl(t *testing.T) {
 			Type2:        "",
 			IsExtern:     false,
 			IsUsed:       true,
+			IsNRVO:       false,
 			IsCInit:      true,
 			IsReferenced: false,
 			IsStatic:     true,
@@ -127,6 +134,7 @@ func TestVarDecl(t *testing.T) {
 			Type2:        "",
 			IsExtern:     false,
 			IsUsed:       true,
+			IsNRVO:       false,
 			IsCInit:      false,
 			IsReferenced: false,
 			IsStatic:     false,
@@ -143,6 +151,7 @@ func TestVarDecl(t *testing.T) {
 			Type2:        "extCoord",
 			IsExtern:     false,
 			IsUsed:       true,
+			IsNRVO:       false,
 			IsCInit:      true,
 			IsReferenced: false,
 			IsStatic:     false,
@@ -159,6 +168,7 @@ func TestVarDecl(t *testing.T) {
 			Type2:        "struct cmark_mem",
 			IsExtern:     true,
 			IsUsed:       true,
+			IsNRVO:       false,
 			IsCInit:      false,
 			IsReferenced: false,
 			IsStatic:     false,
@@ -166,6 +176,22 @@ func TestVarDecl(t *testing.T) {
 			Parent:       0x7f985e0246d0,
 			ChildNodes:   []Node{},
 		},
+		`0x55d681e87428 <col:5, col:23> col:23 used variable 'union programming':'union programming' nrvo`: &VarDecl{
+			Addr:         0x55d681e87428,
+			Pos:          NewPositionFromString("col:5, col:23"),
+			Position2:    "col:23",
+			Name:         "variable",
+			Type:         "union programming",
+			Type2:        "union programming",
+			IsExtern:     false,
+			IsUsed:       true,
+			IsNRVO:       true,
+			IsCInit:      false,
+			IsReferenced: false,
+			IsStatic:     false,
+			IsRegister:   false,
+			ChildNodes:   []Node{},
+		},
 	}
 
 	runNodeTests(t, nodes)