@@ -289,8 +289,7 @@ pub fn translate(
289
289
tree .errors = ast .Tree .ErrorList .init (arena );
290
290
291
291
tree .root_node = try arena .create (ast .Node .Root );
292
- tree .root_node .* = ast.Node.Root {
293
- .base = ast.Node { .id = ast .Node .Id .Root },
292
+ tree .root_node .* = .{
294
293
.decls = ast .Node .Root .DeclList .init (arena ),
295
294
// initialized with the eof token at the end
296
295
.eof_token = undefined ,
@@ -440,7 +439,6 @@ fn visitFnDecl(c: *Context, fn_decl: *const ZigClangFunctionDecl) Error!void {
440
439
.PrivateExtern = > return failDecl (c , fn_decl_loc , fn_name , "unsupported storage class: private extern" , .{}),
441
440
.Auto = > unreachable , // Not legal on functions
442
441
.Register = > unreachable , // Not legal on functions
443
- else = > unreachable ,
444
442
},
445
443
};
446
444
@@ -877,25 +875,23 @@ fn transEnumDecl(c: *Context, enum_decl: *const ZigClangEnumDecl) Error!?*ast.No
877
875
// types, while that's not ISO-C compliant many compilers allow this and
878
876
// default to the usual integer type used for all the enums.
879
877
880
- // TODO only emit this tag type if the enum tag type is not the default.
881
- // I don't know what the default is, need to figure out how clang is deciding.
882
- // it appears to at least be different across gcc/msvc
883
- if (int_type .ptr != null and
884
- ! isCBuiltinType (int_type , .UInt ) and
885
- ! isCBuiltinType (int_type , .Int ))
886
- {
887
- _ = try appendToken (c , .LParen , "(" );
888
- container_node .init_arg_expr = .{
889
- .Type = transQualType (rp , int_type , enum_loc ) catch | err | switch (err ) {
878
+ // default to c_int since msvc and gcc default to different types
879
+ _ = try appendToken (c , .LParen , "(" );
880
+ container_node .init_arg_expr = .{
881
+ .Type = if (int_type .ptr != null and
882
+ ! isCBuiltinType (int_type , .UInt ) and
883
+ ! isCBuiltinType (int_type , .Int ))
884
+ transQualType (rp , int_type , enum_loc ) catch | err | switch (err ) {
890
885
error .UnsupportedType = > {
891
886
try failDecl (c , enum_loc , name , "unable to translate enum tag type" , .{});
892
887
return null ;
893
888
},
894
889
else = > | e | return e ,
895
- },
896
- };
897
- _ = try appendToken (c , .RParen , ")" );
898
- }
890
+ }
891
+ else
892
+ try transCreateNodeIdentifier (c , "c_int" ),
893
+ };
894
+ _ = try appendToken (c , .RParen , ")" );
899
895
900
896
container_node .lbrace_token = try appendToken (c , .LBrace , "{" );
901
897
@@ -953,6 +949,19 @@ fn transEnumDecl(c: *Context, enum_decl: *const ZigClangEnumDecl) Error!?*ast.No
953
949
tld_node .semicolon_token = try appendToken (c , .Semicolon , ";" );
954
950
try addTopLevelDecl (c , field_name , & tld_node .base );
955
951
}
952
+ // make non exhaustive
953
+ const field_node = try c .a ().create (ast .Node .ContainerField );
954
+ field_node .* = .{
955
+ .doc_comments = null ,
956
+ .comptime_token = null ,
957
+ .name_token = try appendIdentifier (c , "_" ),
958
+ .type_expr = null ,
959
+ .value_expr = null ,
960
+ .align_expr = null ,
961
+ };
962
+
963
+ try container_node .fields_and_decls .push (& field_node .base );
964
+ _ = try appendToken (c , .Comma , "," );
956
965
container_node .rbrace_token = try appendToken (c , .RBrace , "}" );
957
966
958
967
break :blk & container_node .base ;
@@ -1231,18 +1240,6 @@ fn transBinaryOperator(
1231
1240
op_id = .BitOr ;
1232
1241
op_token = try appendToken (rp .c , .Pipe , "|" );
1233
1242
},
1234
- .Assign ,
1235
- .MulAssign ,
1236
- .DivAssign ,
1237
- .RemAssign ,
1238
- .AddAssign ,
1239
- .SubAssign ,
1240
- .ShlAssign ,
1241
- .ShrAssign ,
1242
- .AndAssign ,
1243
- .XorAssign ,
1244
- .OrAssign ,
1245
- = > unreachable ,
1246
1243
else = > unreachable ,
1247
1244
}
1248
1245
@@ -1678,7 +1675,6 @@ fn transStringLiteral(
1678
1675
"TODO: support string literal kind {}" ,
1679
1676
.{kind },
1680
1677
),
1681
- else = > unreachable ,
1682
1678
}
1683
1679
}
1684
1680
@@ -2206,6 +2202,19 @@ fn transDoWhileLoop(
2206
2202
.id = .Loop ,
2207
2203
};
2208
2204
2205
+ // if (!cond) break;
2206
+ const if_node = try transCreateNodeIf (rp .c );
2207
+ var cond_scope = Scope {
2208
+ .parent = scope ,
2209
+ .id = .Condition ,
2210
+ };
2211
+ const prefix_op = try transCreateNodePrefixOp (rp .c , .BoolNot , .Bang , "!" );
2212
+ prefix_op .rhs = try transBoolExpr (rp , & cond_scope , @ptrCast (* const ZigClangExpr , ZigClangDoStmt_getCond (stmt )), .used , .r_value , true );
2213
+ _ = try appendToken (rp .c , .RParen , ")" );
2214
+ if_node .condition = & prefix_op .base ;
2215
+ if_node .body = &(try transCreateNodeBreak (rp .c , null )).base ;
2216
+ _ = try appendToken (rp .c , .Semicolon , ";" );
2217
+
2209
2218
const body_node = if (ZigClangStmt_getStmtClass (ZigClangDoStmt_getBody (stmt )) == .CompoundStmtClass ) blk : {
2210
2219
// there's already a block in C, so we'll append our condition to it.
2211
2220
// c: do {
@@ -2217,10 +2226,7 @@ fn transDoWhileLoop(
2217
2226
// zig: b;
2218
2227
// zig: if (!cond) break;
2219
2228
// zig: }
2220
- const body = (try transStmt (rp , & loop_scope , ZigClangDoStmt_getBody (stmt ), .unused , .r_value )).cast (ast .Node .Block ).? ;
2221
- // if this is used as an expression in Zig it needs to be immediately followed by a semicolon
2222
- _ = try appendToken (rp .c , .Semicolon , ";" );
2223
- break :blk body ;
2229
+ break :blk (try transStmt (rp , & loop_scope , ZigClangDoStmt_getBody (stmt ), .unused , .r_value )).cast (ast .Node .Block ).? ;
2224
2230
} else blk : {
2225
2231
// the C statement is without a block, so we need to create a block to contain it.
2226
2232
// c: do
@@ -2236,19 +2242,6 @@ fn transDoWhileLoop(
2236
2242
break :blk block ;
2237
2243
};
2238
2244
2239
- // if (!cond) break;
2240
- const if_node = try transCreateNodeIf (rp .c );
2241
- var cond_scope = Scope {
2242
- .parent = scope ,
2243
- .id = .Condition ,
2244
- };
2245
- const prefix_op = try transCreateNodePrefixOp (rp .c , .BoolNot , .Bang , "!" );
2246
- prefix_op .rhs = try transBoolExpr (rp , & cond_scope , @ptrCast (* const ZigClangExpr , ZigClangDoStmt_getCond (stmt )), .used , .r_value , true );
2247
- _ = try appendToken (rp .c , .RParen , ")" );
2248
- if_node .condition = & prefix_op .base ;
2249
- if_node .body = &(try transCreateNodeBreak (rp .c , null )).base ;
2250
- _ = try appendToken (rp .c , .Semicolon , ";" );
2251
-
2252
2245
try body_node .statements .push (& if_node .base );
2253
2246
if (new )
2254
2247
body_node .rbrace = try appendToken (rp .c , .RBrace , "}" );
@@ -4783,8 +4776,7 @@ fn appendIdentifier(c: *Context, name: []const u8) !ast.TokenIndex {
4783
4776
fn transCreateNodeIdentifier (c : * Context , name : []const u8 ) ! * ast.Node {
4784
4777
const token_index = try appendIdentifier (c , name );
4785
4778
const identifier = try c .a ().create (ast .Node .Identifier );
4786
- identifier .* = ast.Node.Identifier {
4787
- .base = ast.Node { .id = ast .Node .Id .Identifier },
4779
+ identifier .* = .{
4788
4780
.token = token_index ,
4789
4781
};
4790
4782
return & identifier .base ;
@@ -4923,8 +4915,7 @@ fn transMacroFnDefine(c: *Context, it: *ctok.TokenList.Iterator, name: []const u
4923
4915
4924
4916
const token_index = try appendToken (c , .Keyword_var , "var" );
4925
4917
const identifier = try c .a ().create (ast .Node .Identifier );
4926
- identifier .* = ast.Node.Identifier {
4927
- .base = ast.Node { .id = ast .Node .Id .Identifier },
4918
+ identifier .* = .{
4928
4919
.token = token_index ,
4929
4920
};
4930
4921
0 commit comments