authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-01-31 12:19:09+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-02-16 16:37:07+02:00
log6ecec4c8b761c9f8f272602ccb2abdfd9656c71c
tree48be497ec96b7a4da2f0c2c966f79bf10a1a069f
parent7051ef32bf8e1a16cfd73f2bfa09fdbdf39ffc54
signature Commit is signed but in an unrecognized format.

translate-c: translate C types to stage2 types


2 files changed, 238 insertions(+), 0 deletions(-)

src/translate_c.zig+182
......@@ -10,6 +10,7 @@ const ctok = std.c.tokenizer;
1010const CToken = std.c.Token;
1111const mem = std.mem;
1212const math = std.math;
13const Type = @import("type.zig").Type;
1314
1415const CallingConvention = std.builtin.CallingConvention;
1516
......@@ -5178,6 +5179,176 @@ fn transType(rp: RestorePoint, ty: *const clang.Type, source_loc: clang.SourceLo
51785179 }
51795180}
51805181
5182fn transType1(c: *Context, ty: *const clang.Type, source_loc: clang.SourceLocation) TypeError!Type {
5183 switch (ty.getTypeClass()) {
5184 .Builtin => {
5185 const builtin_ty = @ptrCast(*const clang.BuiltinType, ty);
5186 return Type.initTag(switch (builtin_ty.getKind()) {
5187 .Void => .c_void,
5188 .Bool => .bool,
5189 .Char_U, .UChar, .Char_S, .Char8 => .u8,
5190 .SChar => .i8,
5191 .UShort => .c_ushort,
5192 .UInt => .c_uint,
5193 .ULong => .c_ulong,
5194 .ULongLong => .c_ulonglong,
5195 .Short => .c_short,
5196 .Int => .c_int,
5197 .Long => .c_long,
5198 .LongLong => .c_longlong,
5199 .UInt128 => .u128,
5200 .Int128 => .i128,
5201 .Float => .f32,
5202 .Double => .f64,
5203 .Float128 => .f128,
5204 .Float16 => .f16,
5205 .LongDouble => .c_longdouble,
5206 else => return fail(c, error.UnsupportedType, source_loc, "unsupported builtin type", .{}),
5207 });
5208 },
5209 .FunctionProto => {
5210 const fn_proto_ty = @ptrCast(*const clang.FunctionProtoType, ty);
5211 return transFnProto(c, null, fn_proto_ty, source_loc, null, false);
5212 },
5213 .FunctionNoProto => {
5214 const fn_no_proto_ty = @ptrCast(*const clang.FunctionType, ty);
5215 return transFnNoProto(c, fn_no_proto_ty, source_loc, null, false);
5216 },
5217 .Paren => {
5218 const paren_ty = @ptrCast(*const clang.ParenType, ty);
5219 return transQualType(c, paren_ty.getInnerType(), source_loc);
5220 },
5221 .Pointer => {
5222 const child_qt = ty.getPointeeType();
5223 if (qualTypeChildIsFnProto(child_qt)) {
5224 return Type.optional_single_mut_pointer.create(c.arena, try transQualType(c, child_qt, source_loc));
5225 }
5226 const is_const = child_qt.isConstQualified();
5227 const is_volatile = child_qt.isVolatileQualified();
5228 const elem_type = try transQualType(c, child_qt, source_loc);
5229 if (elem_type.zigTypeTag() == .Opaque) {
5230 if (!is_volatile) {
5231 if (is_const) {
5232 return Type.optional_single_const_pointer.create(c.arena, elem_type);
5233 } else {
5234 return Type.optional_single_mut_pointer.create(c.arena, elem_type);
5235 }
5236 }
5237
5238 return Type.pointer.create(c.arena, .{
5239 .pointee_type = elem_type,
5240 .sentinel = null,
5241 .@"align" = 0,
5242 .bit_offset = 0,
5243 .host_size = 0,
5244 .@"allowzero" = false,
5245 .mutable = !is_const,
5246 .@"volatile" = true,
5247 .size = .Single,
5248 });
5249 }
5250
5251 if (!is_volatile) {
5252 if (is_const) {
5253 return Type.c_const_pointer.create(c.arena, elem_type);
5254 } else {
5255 return Type.c_mut_pointer.create(c.arena, elem_type);
5256 }
5257 }
5258
5259 return Type.pointer.create(c.arena, .{
5260 .pointee_type = elem_type,
5261 .sentinel = null,
5262 .@"align" = 0,
5263 .bit_offset = 0,
5264 .host_size = 0,
5265 .@"allowzero" = false,
5266 .mutable = !is_const,
5267 .@"volatile" = true,
5268 .size = .C,
5269 });
5270 },
5271 .ConstantArray => {
5272 const const_arr_ty = @ptrCast(*const clang.ConstantArrayType, ty);
5273
5274 const size_ap_int = const_arr_ty.getSize();
5275 const size = size_ap_int.getLimitedValue(math.maxInt(usize));
5276 const elem_type = try transType1(c, const_arr_ty.getElementType().getTypePtr(), source_loc);
5277
5278 return Type.array.create(c.arena, .{ .len = size, .elem_type = elem_type });
5279 },
5280 .IncompleteArray => {
5281 const incomplete_array_ty = @ptrCast(*const clang.IncompleteArrayType, ty);
5282
5283 const child_qt = incomplete_array_ty.getElementType();
5284 const is_const = child_qt.isConstQualified();
5285 const is_volatile = child_qt.isVolatileQualified();
5286 const elem_type = try transQualType(c, child_qt, source_loc);
5287
5288 if (!is_volatile) {
5289 if (is_const) {
5290 return Type.c_const_pointer.create(c.arena, elem_type);
5291 } else {
5292 return Type.c_mut_pointer.create(c.arena, elem_type);
5293 }
5294 }
5295
5296 return Type.pointer.create(c.arena, .{
5297 .pointee_type = elem_type,
5298 .sentinel = null,
5299 .@"align" = 0,
5300 .bit_offset = 0,
5301 .host_size = 0,
5302 .@"allowzero" = false,
5303 .mutable = !is_const,
5304 .@"volatile" = true,
5305 .size = .C,
5306 });
5307 },
5308 .Typedef => {
5309 const typedef_ty = @ptrCast(*const clang.TypedefType, ty);
5310
5311 const typedef_decl = typedef_ty.getDecl();
5312 return (try transTypeDef(c, typedef_decl, false)) orelse
5313 fail(c, error.UnsupportedType, source_loc, "unable to translate typedef declaration", .{});
5314 },
5315 .Record => {
5316 const record_ty = @ptrCast(*const clang.RecordType, ty);
5317
5318 const record_decl = record_ty.getDecl();
5319 return (try transRecordDecl(c, record_decl)) orelse
5320 fail(c, error.UnsupportedType, source_loc, "unable to resolve record declaration", .{});
5321 },
5322 .Enum => {
5323 const enum_ty = @ptrCast(*const clang.EnumType, ty);
5324
5325 const enum_decl = enum_ty.getDecl();
5326 return (try transEnumDecl(c, enum_decl)) orelse
5327 fail(c, error.UnsupportedType, source_loc, "unable to translate enum declaration", .{});
5328 },
5329 .Elaborated => {
5330 const elaborated_ty = @ptrCast(*const clang.ElaboratedType, ty);
5331 return transQualType(c, elaborated_ty.getNamedType(), source_loc);
5332 },
5333 .Decayed => {
5334 const decayed_ty = @ptrCast(*const clang.DecayedType, ty);
5335 return transQualType(c, decayed_ty.getDecayedType(), source_loc);
5336 },
5337 .Attributed => {
5338 const attributed_ty = @ptrCast(*const clang.AttributedType, ty);
5339 return transQualType(c, attributed_ty.getEquivalentType(), source_loc);
5340 },
5341 .MacroQualified => {
5342 const macroqualified_ty = @ptrCast(*const clang.MacroQualifiedType, ty);
5343 return transQualType(c, macroqualified_ty.getModifiedType(), source_loc);
5344 },
5345 else => {
5346 const type_name = c.str(ty.getTypeClassName());
5347 return fail(c, error.UnsupportedType, source_loc, "unsupported type: '{}'", .{type_name});
5348 },
5349 }
5350}
5351
51815352fn qualTypeWasDemotedToOpaque(c: *Context, qt: clang.QualType) bool {
51825353 const ty = qt.getTypePtr();
51835354 switch (qt.getTypeClass()) {
......@@ -5474,6 +5645,17 @@ fn emitWarning(c: *Context, loc: clang.SourceLocation, comptime format: []const
54745645 _ = try appendTokenFmt(c, .LineComment, "// {s}: warning: " ++ format, args_prefix ++ args);
54755646}
54765647
5648fn fail(
5649 rp: RestorePoint,
5650 err: anytype,
5651 source_loc: clang.SourceLocation,
5652 comptime format: []const u8,
5653 args: anytype,
5654) (@TypeOf(err) || error{OutOfMemory}) {
5655 try emitWarning(c, source_loc, format, args);
5656 return err;
5657}
5658
54775659pub fn failDecl(c: *Context, loc: clang.SourceLocation, name: []const u8, comptime format: []const u8, args: anytype) !void {
54785660 // pub const name = @compileError(msg);
54795661 const pub_tok = try appendToken(c, .Keyword_pub, "pub");
src/type.zig+56
......@@ -28,6 +28,8 @@ pub const Type = extern union {
2828 .i32,
2929 .u64,
3030 .i64,
31 .u128,
32 .i128,
3133 .usize,
3234 .isize,
3335 .c_short,
......@@ -357,6 +359,8 @@ pub const Type = extern union {
357359 .i32,
358360 .u64,
359361 .i64,
362 .u128,
363 .i128,
360364 .usize,
361365 .isize,
362366 .c_short,
......@@ -506,6 +510,8 @@ pub const Type = extern union {
506510 .i32,
507511 .u64,
508512 .i64,
513 .u128,
514 .i128,
509515 .usize,
510516 .isize,
511517 .c_short,
......@@ -772,6 +778,8 @@ pub const Type = extern union {
772778 .i32,
773779 .u64,
774780 .i64,
781 .u128,
782 .i128,
775783 .usize,
776784 .isize,
777785 .c_short,
......@@ -868,6 +876,7 @@ pub const Type = extern union {
868876 .i16, .u16 => return 2,
869877 .i32, .u32 => return 4,
870878 .i64, .u64 => return 8,
879 .u128, .i128 => return 16,
871880
872881 .isize,
873882 .usize,
......@@ -1010,6 +1019,7 @@ pub const Type = extern union {
10101019 .i16, .u16 => return 2,
10111020 .i32, .u32 => return 4,
10121021 .i64, .u64 => return 8,
1022 .u128, .i128 => return 16,
10131023
10141024 .@"anyframe", .anyframe_T, .isize, .usize => return @divExact(target.cpu.arch.ptrBitWidth(), 8),
10151025
......@@ -1109,6 +1119,8 @@ pub const Type = extern union {
11091119 .i32,
11101120 .u64,
11111121 .i64,
1122 .u128,
1123 .i128,
11121124 .usize,
11131125 .isize,
11141126 .c_short,
......@@ -1191,6 +1203,8 @@ pub const Type = extern union {
11911203 .i32,
11921204 .u64,
11931205 .i64,
1206 .u128,
1207 .i128,
11941208 .usize,
11951209 .isize,
11961210 .c_short,
......@@ -1278,6 +1292,8 @@ pub const Type = extern union {
12781292 .i32,
12791293 .u64,
12801294 .i64,
1295 .u128,
1296 .i128,
12811297 .usize,
12821298 .isize,
12831299 .c_short,
......@@ -1359,6 +1375,8 @@ pub const Type = extern union {
13591375 .i32,
13601376 .u64,
13611377 .i64,
1378 .u128,
1379 .i128,
13621380 .usize,
13631381 .isize,
13641382 .c_short,
......@@ -1440,6 +1458,8 @@ pub const Type = extern union {
14401458 .i32,
14411459 .u64,
14421460 .i64,
1461 .u128,
1462 .i128,
14431463 .usize,
14441464 .isize,
14451465 .c_short,
......@@ -1522,6 +1542,8 @@ pub const Type = extern union {
15221542 .i32,
15231543 .u64,
15241544 .i64,
1545 .u128,
1546 .i128,
15251547 .usize,
15261548 .isize,
15271549 .c_short,
......@@ -1776,6 +1798,8 @@ pub const Type = extern union {
17761798 .i32,
17771799 .u64,
17781800 .i64,
1801 .u128,
1802 .i128,
17791803 .usize,
17801804 .isize,
17811805 .c_short,
......@@ -1856,6 +1880,8 @@ pub const Type = extern union {
18561880 .i32,
18571881 .u64,
18581882 .i64,
1883 .u128,
1884 .i128,
18591885 .usize,
18601886 .isize,
18611887 .c_short,
......@@ -2009,6 +2035,8 @@ pub const Type = extern union {
20092035 .i16,
20102036 .i32,
20112037 .i64,
2038 .u128,
2039 .i128,
20122040 => true,
20132041 };
20142042 }
......@@ -2061,6 +2089,8 @@ pub const Type = extern union {
20612089 .i16,
20622090 .i32,
20632091 .i64,
2092 .u128,
2093 .i128,
20642094 .optional,
20652095 .optional_single_mut_pointer,
20662096 .optional_single_const_pointer,
......@@ -2227,6 +2257,8 @@ pub const Type = extern union {
22272257 .i32,
22282258 .u64,
22292259 .i64,
2260 .u128,
2261 .i128,
22302262 .optional,
22312263 .optional_single_mut_pointer,
22322264 .optional_single_const_pointer,
......@@ -2333,6 +2365,8 @@ pub const Type = extern union {
23332365 .i32,
23342366 .u64,
23352367 .i64,
2368 .u128,
2369 .i128,
23362370 .usize,
23372371 .isize,
23382372 .c_short,
......@@ -2417,6 +2451,8 @@ pub const Type = extern union {
24172451 .i32,
24182452 .u64,
24192453 .i64,
2454 .u128,
2455 .i128,
24202456 .usize,
24212457 .isize,
24222458 .c_short,
......@@ -2500,6 +2536,8 @@ pub const Type = extern union {
25002536 .i32,
25012537 .u64,
25022538 .i64,
2539 .u128,
2540 .i128,
25032541 .usize,
25042542 .isize,
25052543 .c_short,
......@@ -2583,6 +2621,8 @@ pub const Type = extern union {
25832621 .i32,
25842622 .u64,
25852623 .i64,
2624 .u128,
2625 .i128,
25862626 .usize,
25872627 .isize,
25882628 .c_short,
......@@ -2663,6 +2703,8 @@ pub const Type = extern union {
26632703 .i32,
26642704 .u64,
26652705 .i64,
2706 .u128,
2707 .i128,
26662708 .usize,
26672709 .isize,
26682710 .c_short,
......@@ -2743,6 +2785,8 @@ pub const Type = extern union {
27432785 .i32,
27442786 .u64,
27452787 .i64,
2788 .u128,
2789 .i128,
27462790 .usize,
27472791 .isize,
27482792 .c_short,
......@@ -2793,6 +2837,8 @@ pub const Type = extern union {
27932837 .i32,
27942838 .u64,
27952839 .i64,
2840 .u128,
2841 .i128,
27962842 .usize,
27972843 .isize,
27982844 .c_short,
......@@ -2874,6 +2920,8 @@ pub const Type = extern union {
28742920 .i32,
28752921 .u64,
28762922 .i64,
2923 .u128,
2924 .i128,
28772925 .usize,
28782926 .isize,
28792927 .c_short,
......@@ -2971,6 +3019,8 @@ pub const Type = extern union {
29713019 .i32,
29723020 .u64,
29733021 .i64,
3022 .u128,
3023 .i128,
29743024 .usize,
29753025 .isize,
29763026 .c_short,
......@@ -3060,6 +3110,8 @@ pub const Type = extern union {
30603110 .i32,
30613111 .u64,
30623112 .i64,
3113 .u128,
3114 .i128,
30633115 .usize,
30643116 .isize,
30653117 .c_short,
......@@ -3193,6 +3245,8 @@ pub const Type = extern union {
31933245 i32,
31943246 u64,
31953247 i64,
3248 u128,
3249 i128,
31963250 usize,
31973251 isize,
31983252 c_short,
......@@ -3277,6 +3331,8 @@ pub const Type = extern union {
32773331 .i32,
32783332 .u64,
32793333 .i64,
3334 .u128,
3335 .i128,
32803336 .usize,
32813337 .isize,
32823338 .c_short,