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;...@@ -10,6 +10,7 @@ const ctok = std.c.tokenizer;
10const CToken = std.c.Token;10const CToken = std.c.Token;
11const mem = std.mem;11const mem = std.mem;
12const math = std.math;12const math = std.math;
13const Type = @import("type.zig").Type;
1314
14const CallingConvention = std.builtin.CallingConvention;15const CallingConvention = std.builtin.CallingConvention;
1516
...@@ -5178,6 +5179,176 @@ fn transType(rp: RestorePoint, ty: *const clang.Type, source_loc: clang.SourceLo...@@ -5178,6 +5179,176 @@ fn transType(rp: RestorePoint, ty: *const clang.Type, source_loc: clang.SourceLo
5178 }5179 }
5179}5180}
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
5181fn qualTypeWasDemotedToOpaque(c: *Context, qt: clang.QualType) bool {5352fn qualTypeWasDemotedToOpaque(c: *Context, qt: clang.QualType) bool {
5182 const ty = qt.getTypePtr();5353 const ty = qt.getTypePtr();
5183 switch (qt.getTypeClass()) {5354 switch (qt.getTypeClass()) {
...@@ -5474,6 +5645,17 @@ fn emitWarning(c: *Context, loc: clang.SourceLocation, comptime format: []const...@@ -5474,6 +5645,17 @@ fn emitWarning(c: *Context, loc: clang.SourceLocation, comptime format: []const
5474 _ = try appendTokenFmt(c, .LineComment, "// {s}: warning: " ++ format, args_prefix ++ args);5645 _ = try appendTokenFmt(c, .LineComment, "// {s}: warning: " ++ format, args_prefix ++ args);
5475}5646}
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
5477pub fn failDecl(c: *Context, loc: clang.SourceLocation, name: []const u8, comptime format: []const u8, args: anytype) !void {5659pub fn failDecl(c: *Context, loc: clang.SourceLocation, name: []const u8, comptime format: []const u8, args: anytype) !void {
5478 // pub const name = @compileError(msg);5660 // pub const name = @compileError(msg);
5479 const pub_tok = try appendToken(c, .Keyword_pub, "pub");5661 const pub_tok = try appendToken(c, .Keyword_pub, "pub");
src/type.zig+56
...@@ -28,6 +28,8 @@ pub const Type = extern union {...@@ -28,6 +28,8 @@ pub const Type = extern union {
28 .i32,28 .i32,
29 .u64,29 .u64,
30 .i64,30 .i64,
31 .u128,
32 .i128,
31 .usize,33 .usize,
32 .isize,34 .isize,
33 .c_short,35 .c_short,
...@@ -357,6 +359,8 @@ pub const Type = extern union {...@@ -357,6 +359,8 @@ pub const Type = extern union {
357 .i32,359 .i32,
358 .u64,360 .u64,
359 .i64,361 .i64,
362 .u128,
363 .i128,
360 .usize,364 .usize,
361 .isize,365 .isize,
362 .c_short,366 .c_short,
...@@ -506,6 +510,8 @@ pub const Type = extern union {...@@ -506,6 +510,8 @@ pub const Type = extern union {
506 .i32,510 .i32,
507 .u64,511 .u64,
508 .i64,512 .i64,
513 .u128,
514 .i128,
509 .usize,515 .usize,
510 .isize,516 .isize,
511 .c_short,517 .c_short,
...@@ -772,6 +778,8 @@ pub const Type = extern union {...@@ -772,6 +778,8 @@ pub const Type = extern union {
772 .i32,778 .i32,
773 .u64,779 .u64,
774 .i64,780 .i64,
781 .u128,
782 .i128,
775 .usize,783 .usize,
776 .isize,784 .isize,
777 .c_short,785 .c_short,
...@@ -868,6 +876,7 @@ pub const Type = extern union {...@@ -868,6 +876,7 @@ pub const Type = extern union {
868 .i16, .u16 => return 2,876 .i16, .u16 => return 2,
869 .i32, .u32 => return 4,877 .i32, .u32 => return 4,
870 .i64, .u64 => return 8,878 .i64, .u64 => return 8,
879 .u128, .i128 => return 16,
871880
872 .isize,881 .isize,
873 .usize,882 .usize,
...@@ -1010,6 +1019,7 @@ pub const Type = extern union {...@@ -1010,6 +1019,7 @@ pub const Type = extern union {
1010 .i16, .u16 => return 2,1019 .i16, .u16 => return 2,
1011 .i32, .u32 => return 4,1020 .i32, .u32 => return 4,
1012 .i64, .u64 => return 8,1021 .i64, .u64 => return 8,
1022 .u128, .i128 => return 16,
10131023
1014 .@"anyframe", .anyframe_T, .isize, .usize => return @divExact(target.cpu.arch.ptrBitWidth(), 8),1024 .@"anyframe", .anyframe_T, .isize, .usize => return @divExact(target.cpu.arch.ptrBitWidth(), 8),
10151025
...@@ -1109,6 +1119,8 @@ pub const Type = extern union {...@@ -1109,6 +1119,8 @@ pub const Type = extern union {
1109 .i32,1119 .i32,
1110 .u64,1120 .u64,
1111 .i64,1121 .i64,
1122 .u128,
1123 .i128,
1112 .usize,1124 .usize,
1113 .isize,1125 .isize,
1114 .c_short,1126 .c_short,
...@@ -1191,6 +1203,8 @@ pub const Type = extern union {...@@ -1191,6 +1203,8 @@ pub const Type = extern union {
1191 .i32,1203 .i32,
1192 .u64,1204 .u64,
1193 .i64,1205 .i64,
1206 .u128,
1207 .i128,
1194 .usize,1208 .usize,
1195 .isize,1209 .isize,
1196 .c_short,1210 .c_short,
...@@ -1278,6 +1292,8 @@ pub const Type = extern union {...@@ -1278,6 +1292,8 @@ pub const Type = extern union {
1278 .i32,1292 .i32,
1279 .u64,1293 .u64,
1280 .i64,1294 .i64,
1295 .u128,
1296 .i128,
1281 .usize,1297 .usize,
1282 .isize,1298 .isize,
1283 .c_short,1299 .c_short,
...@@ -1359,6 +1375,8 @@ pub const Type = extern union {...@@ -1359,6 +1375,8 @@ pub const Type = extern union {
1359 .i32,1375 .i32,
1360 .u64,1376 .u64,
1361 .i64,1377 .i64,
1378 .u128,
1379 .i128,
1362 .usize,1380 .usize,
1363 .isize,1381 .isize,
1364 .c_short,1382 .c_short,
...@@ -1440,6 +1458,8 @@ pub const Type = extern union {...@@ -1440,6 +1458,8 @@ pub const Type = extern union {
1440 .i32,1458 .i32,
1441 .u64,1459 .u64,
1442 .i64,1460 .i64,
1461 .u128,
1462 .i128,
1443 .usize,1463 .usize,
1444 .isize,1464 .isize,
1445 .c_short,1465 .c_short,
...@@ -1522,6 +1542,8 @@ pub const Type = extern union {...@@ -1522,6 +1542,8 @@ pub const Type = extern union {
1522 .i32,1542 .i32,
1523 .u64,1543 .u64,
1524 .i64,1544 .i64,
1545 .u128,
1546 .i128,
1525 .usize,1547 .usize,
1526 .isize,1548 .isize,
1527 .c_short,1549 .c_short,
...@@ -1776,6 +1798,8 @@ pub const Type = extern union {...@@ -1776,6 +1798,8 @@ pub const Type = extern union {
1776 .i32,1798 .i32,
1777 .u64,1799 .u64,
1778 .i64,1800 .i64,
1801 .u128,
1802 .i128,
1779 .usize,1803 .usize,
1780 .isize,1804 .isize,
1781 .c_short,1805 .c_short,
...@@ -1856,6 +1880,8 @@ pub const Type = extern union {...@@ -1856,6 +1880,8 @@ pub const Type = extern union {
1856 .i32,1880 .i32,
1857 .u64,1881 .u64,
1858 .i64,1882 .i64,
1883 .u128,
1884 .i128,
1859 .usize,1885 .usize,
1860 .isize,1886 .isize,
1861 .c_short,1887 .c_short,
...@@ -2009,6 +2035,8 @@ pub const Type = extern union {...@@ -2009,6 +2035,8 @@ pub const Type = extern union {
2009 .i16,2035 .i16,
2010 .i32,2036 .i32,
2011 .i64,2037 .i64,
2038 .u128,
2039 .i128,
2012 => true,2040 => true,
2013 };2041 };
2014 }2042 }
...@@ -2061,6 +2089,8 @@ pub const Type = extern union {...@@ -2061,6 +2089,8 @@ pub const Type = extern union {
2061 .i16,2089 .i16,
2062 .i32,2090 .i32,
2063 .i64,2091 .i64,
2092 .u128,
2093 .i128,
2064 .optional,2094 .optional,
2065 .optional_single_mut_pointer,2095 .optional_single_mut_pointer,
2066 .optional_single_const_pointer,2096 .optional_single_const_pointer,
...@@ -2227,6 +2257,8 @@ pub const Type = extern union {...@@ -2227,6 +2257,8 @@ pub const Type = extern union {
2227 .i32,2257 .i32,
2228 .u64,2258 .u64,
2229 .i64,2259 .i64,
2260 .u128,
2261 .i128,
2230 .optional,2262 .optional,
2231 .optional_single_mut_pointer,2263 .optional_single_mut_pointer,
2232 .optional_single_const_pointer,2264 .optional_single_const_pointer,
...@@ -2333,6 +2365,8 @@ pub const Type = extern union {...@@ -2333,6 +2365,8 @@ pub const Type = extern union {
2333 .i32,2365 .i32,
2334 .u64,2366 .u64,
2335 .i64,2367 .i64,
2368 .u128,
2369 .i128,
2336 .usize,2370 .usize,
2337 .isize,2371 .isize,
2338 .c_short,2372 .c_short,
...@@ -2417,6 +2451,8 @@ pub const Type = extern union {...@@ -2417,6 +2451,8 @@ pub const Type = extern union {
2417 .i32,2451 .i32,
2418 .u64,2452 .u64,
2419 .i64,2453 .i64,
2454 .u128,
2455 .i128,
2420 .usize,2456 .usize,
2421 .isize,2457 .isize,
2422 .c_short,2458 .c_short,
...@@ -2500,6 +2536,8 @@ pub const Type = extern union {...@@ -2500,6 +2536,8 @@ pub const Type = extern union {
2500 .i32,2536 .i32,
2501 .u64,2537 .u64,
2502 .i64,2538 .i64,
2539 .u128,
2540 .i128,
2503 .usize,2541 .usize,
2504 .isize,2542 .isize,
2505 .c_short,2543 .c_short,
...@@ -2583,6 +2621,8 @@ pub const Type = extern union {...@@ -2583,6 +2621,8 @@ pub const Type = extern union {
2583 .i32,2621 .i32,
2584 .u64,2622 .u64,
2585 .i64,2623 .i64,
2624 .u128,
2625 .i128,
2586 .usize,2626 .usize,
2587 .isize,2627 .isize,
2588 .c_short,2628 .c_short,
...@@ -2663,6 +2703,8 @@ pub const Type = extern union {...@@ -2663,6 +2703,8 @@ pub const Type = extern union {
2663 .i32,2703 .i32,
2664 .u64,2704 .u64,
2665 .i64,2705 .i64,
2706 .u128,
2707 .i128,
2666 .usize,2708 .usize,
2667 .isize,2709 .isize,
2668 .c_short,2710 .c_short,
...@@ -2743,6 +2785,8 @@ pub const Type = extern union {...@@ -2743,6 +2785,8 @@ pub const Type = extern union {
2743 .i32,2785 .i32,
2744 .u64,2786 .u64,
2745 .i64,2787 .i64,
2788 .u128,
2789 .i128,
2746 .usize,2790 .usize,
2747 .isize,2791 .isize,
2748 .c_short,2792 .c_short,
...@@ -2793,6 +2837,8 @@ pub const Type = extern union {...@@ -2793,6 +2837,8 @@ pub const Type = extern union {
2793 .i32,2837 .i32,
2794 .u64,2838 .u64,
2795 .i64,2839 .i64,
2840 .u128,
2841 .i128,
2796 .usize,2842 .usize,
2797 .isize,2843 .isize,
2798 .c_short,2844 .c_short,
...@@ -2874,6 +2920,8 @@ pub const Type = extern union {...@@ -2874,6 +2920,8 @@ pub const Type = extern union {
2874 .i32,2920 .i32,
2875 .u64,2921 .u64,
2876 .i64,2922 .i64,
2923 .u128,
2924 .i128,
2877 .usize,2925 .usize,
2878 .isize,2926 .isize,
2879 .c_short,2927 .c_short,
...@@ -2971,6 +3019,8 @@ pub const Type = extern union {...@@ -2971,6 +3019,8 @@ pub const Type = extern union {
2971 .i32,3019 .i32,
2972 .u64,3020 .u64,
2973 .i64,3021 .i64,
3022 .u128,
3023 .i128,
2974 .usize,3024 .usize,
2975 .isize,3025 .isize,
2976 .c_short,3026 .c_short,
...@@ -3060,6 +3110,8 @@ pub const Type = extern union {...@@ -3060,6 +3110,8 @@ pub const Type = extern union {
3060 .i32,3110 .i32,
3061 .u64,3111 .u64,
3062 .i64,3112 .i64,
3113 .u128,
3114 .i128,
3063 .usize,3115 .usize,
3064 .isize,3116 .isize,
3065 .c_short,3117 .c_short,
...@@ -3193,6 +3245,8 @@ pub const Type = extern union {...@@ -3193,6 +3245,8 @@ pub const Type = extern union {
3193 i32,3245 i32,
3194 u64,3246 u64,
3195 i64,3247 i64,
3248 u128,
3249 i128,
3196 usize,3250 usize,
3197 isize,3251 isize,
3198 c_short,3252 c_short,
...@@ -3277,6 +3331,8 @@ pub const Type = extern union {...@@ -3277,6 +3331,8 @@ pub const Type = extern union {
3277 .i32,3331 .i32,
3278 .u64,3332 .u64,
3279 .i64,3333 .i64,
3334 .u128,
3335 .i128,
3280 .usize,3336 .usize,
3281 .isize,3337 .isize,
3282 .c_short,3338 .c_short,