Skip to content

Commit d672c20

Browse files
authored
Merge pull request #7479 from ziglang/translate-c-ast
Make translate-c use intermediate AST
2 parents 914540d + 974a1c5 commit d672c20

File tree

13 files changed

+4939
-4054
lines changed

13 files changed

+4939
-4054
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,7 @@ set(ZIG_STAGE2_SOURCES
577577
"${CMAKE_SOURCE_DIR}/src/windows_sdk.zig"
578578
"${CMAKE_SOURCE_DIR}/src/zir.zig"
579579
"${CMAKE_SOURCE_DIR}/src/zir_sema.zig"
580+
"${CMAKE_SOURCE_DIR}/src/translate_c/ast.zig"
580581
)
581582

582583
if(MSVC)

lib/std/zig/parser_test.zig

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3695,12 +3695,12 @@ test "zig fmt: hexadeciaml float literals with underscore separators" {
36953695
);
36963696
}
36973697

3698-
//test "zig fmt: C var args" {
3699-
// try testCanonical(
3700-
// \\pub extern "c" fn printf(format: [*:0]const u8, ...) c_int;
3701-
// \\
3702-
// );
3703-
//}
3698+
test "zig fmt: C var args" {
3699+
try testCanonical(
3700+
\\pub extern "c" fn printf(format: [*:0]const u8, ...) c_int;
3701+
\\
3702+
);
3703+
}
37043704

37053705
//test "zig fmt: Only indent multiline string literals in function calls" {
37063706
// try testCanonical(

lib/std/zig/render.zig

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -556,14 +556,11 @@ fn renderExpression(ais: *Ais, tree: ast.Tree, node: ast.Node.Index, space: Spac
556556

557557
.builtin_call_two, .builtin_call_two_comma => {
558558
if (datas[node].lhs == 0) {
559-
const params = [_]ast.Node.Index{};
560-
return renderBuiltinCall(ais, tree, main_tokens[node], &params, space);
559+
return renderBuiltinCall(ais, tree, main_tokens[node], &.{}, space);
561560
} else if (datas[node].rhs == 0) {
562-
const params = [_]ast.Node.Index{datas[node].lhs};
563-
return renderBuiltinCall(ais, tree, main_tokens[node], &params, space);
561+
return renderBuiltinCall(ais, tree, main_tokens[node], &.{datas[node].lhs}, space);
564562
} else {
565-
const params = [_]ast.Node.Index{ datas[node].lhs, datas[node].rhs };
566-
return renderBuiltinCall(ais, tree, main_tokens[node], &params, space);
563+
return renderBuiltinCall(ais, tree, main_tokens[node], &.{ datas[node].lhs, datas[node].rhs }, space);
567564
}
568565
},
569566
.builtin_call, .builtin_call_comma => {
@@ -1319,7 +1316,7 @@ fn renderFnProto(ais: *Ais, tree: ast.Tree, fn_proto: ast.full.FnProto, space: S
13191316
.r_paren => break,
13201317
.comma => {
13211318
try renderToken(ais, tree, last_param_token, .space); // ,
1322-
last_param_token += 1;
1319+
continue;
13231320
},
13241321
else => {}, // Parameter type without a name.
13251322
}

src/Compilation.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1334,7 +1334,7 @@ pub fn update(self: *Compilation) !void {
13341334
self.c_object_work_queue.writeItemAssumeCapacity(entry.key);
13351335
}
13361336

1337-
const use_stage1 = build_options.is_stage1 and self.bin_file.options.use_llvm;
1337+
const use_stage1 = build_options.omit_stage2 or build_options.is_stage1 and self.bin_file.options.use_llvm;
13381338
if (!use_stage1) {
13391339
if (self.bin_file.options.module) |module| {
13401340
module.compile_log_text.shrinkAndFree(module.gpa, 0);

src/astgen.zig

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2935,8 +2935,11 @@ fn identifier(
29352935
return mod.failNode(scope, ident, "TODO implement '_' identifier", .{});
29362936
}
29372937

2938-
if (getSimplePrimitiveValue(ident_name)) |typed_value| {
2939-
const result = try addZIRInstConst(mod, scope, src, typed_value);
2938+
if (simple_types.get(ident_name)) |val_tag| {
2939+
const result = try addZIRInstConst(mod, scope, src, TypedValue{
2940+
.ty = Type.initTag(.type),
2941+
.val = Value.initTag(val_tag),
2942+
});
29402943
return rvalue(mod, scope, rl, result);
29412944
}
29422945

@@ -3617,42 +3620,33 @@ fn callExpr(
36173620
return rvalue(mod, scope, rl, result);
36183621
}
36193622

3620-
fn getSimplePrimitiveValue(name: []const u8) ?TypedValue {
3621-
const simple_types = std.ComptimeStringMap(Value.Tag, .{
3622-
.{ "u8", .u8_type },
3623-
.{ "i8", .i8_type },
3624-
.{ "isize", .isize_type },
3625-
.{ "usize", .usize_type },
3626-
.{ "c_short", .c_short_type },
3627-
.{ "c_ushort", .c_ushort_type },
3628-
.{ "c_int", .c_int_type },
3629-
.{ "c_uint", .c_uint_type },
3630-
.{ "c_long", .c_long_type },
3631-
.{ "c_ulong", .c_ulong_type },
3632-
.{ "c_longlong", .c_longlong_type },
3633-
.{ "c_ulonglong", .c_ulonglong_type },
3634-
.{ "c_longdouble", .c_longdouble_type },
3635-
.{ "f16", .f16_type },
3636-
.{ "f32", .f32_type },
3637-
.{ "f64", .f64_type },
3638-
.{ "f128", .f128_type },
3639-
.{ "c_void", .c_void_type },
3640-
.{ "bool", .bool_type },
3641-
.{ "void", .void_type },
3642-
.{ "type", .type_type },
3643-
.{ "anyerror", .anyerror_type },
3644-
.{ "comptime_int", .comptime_int_type },
3645-
.{ "comptime_float", .comptime_float_type },
3646-
.{ "noreturn", .noreturn_type },
3647-
});
3648-
if (simple_types.get(name)) |tag| {
3649-
return TypedValue{
3650-
.ty = Type.initTag(.type),
3651-
.val = Value.initTag(tag),
3652-
};
3653-
}
3654-
return null;
3655-
}
3623+
pub const simple_types = std.ComptimeStringMap(Value.Tag, .{
3624+
.{ "u8", .u8_type },
3625+
.{ "i8", .i8_type },
3626+
.{ "isize", .isize_type },
3627+
.{ "usize", .usize_type },
3628+
.{ "c_short", .c_short_type },
3629+
.{ "c_ushort", .c_ushort_type },
3630+
.{ "c_int", .c_int_type },
3631+
.{ "c_uint", .c_uint_type },
3632+
.{ "c_long", .c_long_type },
3633+
.{ "c_ulong", .c_ulong_type },
3634+
.{ "c_longlong", .c_longlong_type },
3635+
.{ "c_ulonglong", .c_ulonglong_type },
3636+
.{ "c_longdouble", .c_longdouble_type },
3637+
.{ "f16", .f16_type },
3638+
.{ "f32", .f32_type },
3639+
.{ "f64", .f64_type },
3640+
.{ "f128", .f128_type },
3641+
.{ "c_void", .c_void_type },
3642+
.{ "bool", .bool_type },
3643+
.{ "void", .void_type },
3644+
.{ "type", .type_type },
3645+
.{ "anyerror", .anyerror_type },
3646+
.{ "comptime_int", .comptime_int_type },
3647+
.{ "comptime_float", .comptime_float_type },
3648+
.{ "noreturn", .noreturn_type },
3649+
});
36563650

36573651
fn nodeMayNeedMemoryLocation(scope: *Scope, start_node: ast.Node.Index) bool {
36583652
const tree = scope.tree();

src/clang.zig

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ pub const APSInt = opaque {
127127

128128
pub const getNumWords = ZigClangAPSInt_getNumWords;
129129
extern fn ZigClangAPSInt_getNumWords(*const APSInt) c_uint;
130+
131+
pub const lessThanEqual = ZigClangAPSInt_lessThanEqual;
132+
extern fn ZigClangAPSInt_lessThanEqual(*const APSInt, rhs: u64) bool;
130133
};
131134

132135
pub const ASTContext = opaque {
@@ -270,12 +273,12 @@ pub const CompoundAssignOperator = opaque {
270273

271274
pub const CompoundStmt = opaque {
272275
pub const body_begin = ZigClangCompoundStmt_body_begin;
273-
extern fn ZigClangCompoundStmt_body_begin(*const CompoundStmt) const_body_iterator;
276+
extern fn ZigClangCompoundStmt_body_begin(*const CompoundStmt) ConstBodyIterator;
274277

275278
pub const body_end = ZigClangCompoundStmt_body_end;
276-
extern fn ZigClangCompoundStmt_body_end(*const CompoundStmt) const_body_iterator;
279+
extern fn ZigClangCompoundStmt_body_end(*const CompoundStmt) ConstBodyIterator;
277280

278-
pub const const_body_iterator = [*]const *Stmt;
281+
pub const ConstBodyIterator = [*]const *Stmt;
279282
};
280283

281284
pub const ConditionalOperator = opaque {};
@@ -407,7 +410,7 @@ pub const Expr = opaque {
407410
pub const getBeginLoc = ZigClangExpr_getBeginLoc;
408411
extern fn ZigClangExpr_getBeginLoc(*const Expr) SourceLocation;
409412

410-
pub const EvaluateAsConstantExpr = ZigClangExpr_EvaluateAsConstantExpr;
413+
pub const evaluateAsConstantExpr = ZigClangExpr_EvaluateAsConstantExpr;
411414
extern fn ZigClangExpr_EvaluateAsConstantExpr(*const Expr, *ExprEvalResult, Expr_ConstExprUsage, *const ASTContext) bool;
412415
};
413416

0 commit comments

Comments
 (0)