authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-01-05 11:39:01+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-05 17:39:29-05:00
log6ff70d3c31607abfab7b0641bb903859972a6a8b
treef63bdaa8df9d75570402044a9c52a2929ee428c7
parent5888d84ea362a81a313450d988425abba29dd728

Better InitListExpr translation


5 files changed, 67 insertions(+), 22 deletions(-)

src-self-hosted/clang.zig+2
...@@ -802,6 +802,8 @@ pub extern fn ZigClangQualType_isRestrictQualified(self: struct_ZigClangQualType...@@ -802,6 +802,8 @@ pub extern fn ZigClangQualType_isRestrictQualified(self: struct_ZigClangQualType
802pub extern fn ZigClangType_getTypeClass(self: ?*const struct_ZigClangType) ZigClangTypeClass;802pub extern fn ZigClangType_getTypeClass(self: ?*const struct_ZigClangType) ZigClangTypeClass;
803pub extern fn ZigClangType_getPointeeType(self: ?*const struct_ZigClangType) struct_ZigClangQualType;803pub extern fn ZigClangType_getPointeeType(self: ?*const struct_ZigClangType) struct_ZigClangQualType;
804pub extern fn ZigClangType_isVoidType(self: ?*const struct_ZigClangType) bool;804pub extern fn ZigClangType_isVoidType(self: ?*const struct_ZigClangType) bool;
805pub extern fn ZigClangType_isRecordType(self: ?*const struct_ZigClangType) bool;
806pub extern fn ZigClangType_isArrayType(self: ?*const struct_ZigClangType) bool;
805pub extern fn ZigClangType_getTypeClassName(self: *const struct_ZigClangType) [*:0]const u8;807pub extern fn ZigClangType_getTypeClassName(self: *const struct_ZigClangType) [*:0]const u8;
806pub extern fn ZigClangType_getAsArrayTypeUnsafe(self: *const ZigClangType) *const ZigClangArrayType;808pub extern fn ZigClangType_getAsArrayTypeUnsafe(self: *const ZigClangType) *const ZigClangArrayType;
807pub extern fn ZigClangType_getAsRecordType(self: *const ZigClangType) ?*const ZigClangRecordType;809pub extern fn ZigClangType_getAsRecordType(self: *const ZigClangType) ?*const ZigClangRecordType;
src-self-hosted/translate_c.zig+39-17
...@@ -1851,30 +1851,18 @@ fn transInitListExprRecord(...@@ -1851,30 +1851,18 @@ fn transInitListExprRecord(
1851 return &init_node.base;1851 return &init_node.base;
1852}1852}
18531853
1854fn transInitListExpr(1854fn transInitListExprArray(
1855 rp: RestorePoint,1855 rp: RestorePoint,
1856 scope: *Scope,1856 scope: *Scope,
1857 loc: ZigClangSourceLocation,
1857 expr: *const ZigClangInitListExpr,1858 expr: *const ZigClangInitListExpr,
1859 ty: *const ZigClangType,
1858 used: ResultUsed,1860 used: ResultUsed,
1859) TransError!*ast.Node {1861) TransError!*ast.Node {
1860 const qt = getExprQualType(rp.c, @ptrCast(*const ZigClangExpr, expr));1862 const arr_type = ZigClangType_getAsArrayTypeUnsafe(ty);
1861 const qual_type = ZigClangQualType_getTypePtr(qt);
1862 const source_loc = ZigClangExpr_getBeginLoc(@ptrCast(*const ZigClangExpr, expr));
1863 switch (ZigClangType_getTypeClass(qual_type)) {
1864 .ConstantArray => {},
1865 .Record, .Elaborated => {
1866 return transInitListExprRecord(rp, scope, source_loc, expr, qual_type, used);
1867 },
1868 else => {
1869 const type_name = rp.c.str(ZigClangType_getTypeClassName(qual_type));
1870 return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported initlist type: '{}'", .{type_name});
1871 },
1872 }
1873
1874 const arr_type = ZigClangType_getAsArrayTypeUnsafe(qual_type);
1875 const child_qt = ZigClangArrayType_getElementType(arr_type);1863 const child_qt = ZigClangArrayType_getElementType(arr_type);
1876 const init_count = ZigClangInitListExpr_getNumInits(expr);1864 const init_count = ZigClangInitListExpr_getNumInits(expr);
1877 const const_arr_ty = @ptrCast(*const ZigClangConstantArrayType, qual_type);1865 const const_arr_ty = @ptrCast(*const ZigClangConstantArrayType, ty);
1878 const size_ap_int = ZigClangConstantArrayType_getSize(const_arr_ty);1866 const size_ap_int = ZigClangConstantArrayType_getSize(const_arr_ty);
1879 const all_count = ZigClangAPInt_getLimitedValue(size_ap_int, math.maxInt(usize));1867 const all_count = ZigClangAPInt_getLimitedValue(size_ap_int, math.maxInt(usize));
1880 const leftover_count = all_count - init_count;1868 const leftover_count = all_count - init_count;
...@@ -1931,6 +1919,40 @@ fn transInitListExpr(...@@ -1931,6 +1919,40 @@ fn transInitListExpr(
1931 return &cat_node.base;1919 return &cat_node.base;
1932}1920}
19331921
1922fn transInitListExpr(
1923 rp: RestorePoint,
1924 scope: *Scope,
1925 expr: *const ZigClangInitListExpr,
1926 used: ResultUsed,
1927) TransError!*ast.Node {
1928 const qt = getExprQualType(rp.c, @ptrCast(*const ZigClangExpr, expr));
1929 var qual_type = ZigClangQualType_getTypePtr(qt);
1930 const source_loc = ZigClangExpr_getBeginLoc(@ptrCast(*const ZigClangExpr, expr));
1931
1932 if (ZigClangType_isRecordType(qual_type)) {
1933 return transInitListExprRecord(
1934 rp,
1935 scope,
1936 source_loc,
1937 expr,
1938 qual_type,
1939 used,
1940 );
1941 } else if (ZigClangType_isArrayType(qual_type)) {
1942 return transInitListExprArray(
1943 rp,
1944 scope,
1945 source_loc,
1946 expr,
1947 qual_type,
1948 used,
1949 );
1950 } else {
1951 const type_name = rp.c.str(ZigClangType_getTypeClassName(qual_type));
1952 return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported initlist type: '{}'", .{type_name});
1953 }
1954}
1955
1934fn transZeroInitExpr(1956fn transZeroInitExpr(
1935 rp: RestorePoint,1957 rp: RestorePoint,
1936 scope: *Scope,1958 scope: *Scope,
src/zig_clang.cpp+10
...@@ -1814,6 +1814,16 @@ bool ZigClangType_isVoidType(const ZigClangType *self) {...@@ -1814,6 +1814,16 @@ bool ZigClangType_isVoidType(const ZigClangType *self) {
1814 return casted->isVoidType();1814 return casted->isVoidType();
1815}1815}
18161816
1817bool ZigClangType_isArrayType(const ZigClangType *self) {
1818 auto casted = reinterpret_cast<const clang::Type *>(self);
1819 return casted->isArrayType();
1820}
1821
1822bool ZigClangType_isRecordType(const ZigClangType *self) {
1823 auto casted = reinterpret_cast<const clang::Type *>(self);
1824 return casted->isRecordType();
1825}
1826
1817const char *ZigClangType_getTypeClassName(const ZigClangType *self) {1827const char *ZigClangType_getTypeClassName(const ZigClangType *self) {
1818 auto casted = reinterpret_cast<const clang::Type *>(self);1828 auto casted = reinterpret_cast<const clang::Type *>(self);
1819 return casted->getTypeClassName();1829 return casted->getTypeClassName();
src/zig_clang.h+2
...@@ -932,6 +932,8 @@ ZIG_EXTERN_C bool ZigClangQualType_isRestrictQualified(struct ZigClangQualType);...@@ -932,6 +932,8 @@ ZIG_EXTERN_C bool ZigClangQualType_isRestrictQualified(struct ZigClangQualType);
932ZIG_EXTERN_C enum ZigClangTypeClass ZigClangType_getTypeClass(const struct ZigClangType *self);932ZIG_EXTERN_C enum ZigClangTypeClass ZigClangType_getTypeClass(const struct ZigClangType *self);
933ZIG_EXTERN_C struct ZigClangQualType ZigClangType_getPointeeType(const struct ZigClangType *self);933ZIG_EXTERN_C struct ZigClangQualType ZigClangType_getPointeeType(const struct ZigClangType *self);
934ZIG_EXTERN_C bool ZigClangType_isVoidType(const struct ZigClangType *self);934ZIG_EXTERN_C bool ZigClangType_isVoidType(const struct ZigClangType *self);
935ZIG_EXTERN_C bool ZigClangType_isArrayType(const struct ZigClangType *self);
936ZIG_EXTERN_C bool ZigClangType_isRecordType(const struct ZigClangType *self);
935ZIG_EXTERN_C const char *ZigClangType_getTypeClassName(const struct ZigClangType *self);937ZIG_EXTERN_C const char *ZigClangType_getTypeClassName(const struct ZigClangType *self);
936ZIG_EXTERN_C const struct ZigClangArrayType *ZigClangType_getAsArrayTypeUnsafe(const struct ZigClangType *self);938ZIG_EXTERN_C const struct ZigClangArrayType *ZigClangType_getAsArrayTypeUnsafe(const struct ZigClangType *self);
937ZIG_EXTERN_C const ZigClangRecordType *ZigClangType_getAsRecordType(const ZigClangType *self);939ZIG_EXTERN_C const ZigClangRecordType *ZigClangType_getAsRecordType(const ZigClangType *self);
test/translate_c.zig+14-5
...@@ -31,21 +31,27 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -31,21 +31,27 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
31 });31 });
3232
33 cases.add("struct initializer - simple",33 cases.add("struct initializer - simple",
34 \\typedef struct { int x; } foo;
34 \\struct {double x,y,z;} s0 = {1.2, 1.3};35 \\struct {double x,y,z;} s0 = {1.2, 1.3};
35 \\struct {int sec,min,hour,day,mon,year;} s1 = {.day=31,12,2014,.sec=30,15,17};36 \\struct {int sec,min,hour,day,mon,year;} s1 = {.day=31,12,2014,.sec=30,15,17};
36 \\struct {int x,y;} s2 = {.y = 2, .x=1};37 \\struct {int x,y;} s2 = {.y = 2, .x=1};
38 \\foo s3 = { 123 };
37 , &[_][]const u8{39 , &[_][]const u8{
38 \\const struct_unnamed_1 = extern struct {40 \\const struct_unnamed_1 = extern struct {
41 \\ x: c_int,
42 \\};
43 \\pub const foo = struct_unnamed_1;
44 \\const struct_unnamed_2 = extern struct {
39 \\ x: f64,45 \\ x: f64,
40 \\ y: f64,46 \\ y: f64,
41 \\ z: f64,47 \\ z: f64,
42 \\};48 \\};
43 \\pub export var s0: struct_unnamed_1 = struct_unnamed_1{49 \\pub export var s0: struct_unnamed_2 = struct_unnamed_2{
44 \\ .x = 1.2,50 \\ .x = 1.2,
45 \\ .y = 1.3,51 \\ .y = 1.3,
46 \\ .z = 0,52 \\ .z = 0,
47 \\};53 \\};
48 \\const struct_unnamed_2 = extern struct {54 \\const struct_unnamed_3 = extern struct {
49 \\ sec: c_int,55 \\ sec: c_int,
50 \\ min: c_int,56 \\ min: c_int,
51 \\ hour: c_int,57 \\ hour: c_int,
...@@ -53,7 +59,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -53,7 +59,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
53 \\ mon: c_int,59 \\ mon: c_int,
54 \\ year: c_int,60 \\ year: c_int,
55 \\};61 \\};
56 \\pub export var s1: struct_unnamed_2 = struct_unnamed_2{62 \\pub export var s1: struct_unnamed_3 = struct_unnamed_3{
57 \\ .sec = @as(c_int, 30),63 \\ .sec = @as(c_int, 30),
58 \\ .min = @as(c_int, 15),64 \\ .min = @as(c_int, 15),
59 \\ .hour = @as(c_int, 17),65 \\ .hour = @as(c_int, 17),
...@@ -61,14 +67,17 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -61,14 +67,17 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
61 \\ .mon = @as(c_int, 12),67 \\ .mon = @as(c_int, 12),
62 \\ .year = @as(c_int, 2014),68 \\ .year = @as(c_int, 2014),
63 \\};69 \\};
64 \\const struct_unnamed_3 = extern struct {70 \\const struct_unnamed_4 = extern struct {
65 \\ x: c_int,71 \\ x: c_int,
66 \\ y: c_int,72 \\ y: c_int,
67 \\};73 \\};
68 \\pub export var s2: struct_unnamed_3 = struct_unnamed_3{74 \\pub export var s2: struct_unnamed_4 = struct_unnamed_4{
69 \\ .x = @as(c_int, 1),75 \\ .x = @as(c_int, 1),
70 \\ .y = @as(c_int, 2),76 \\ .y = @as(c_int, 2),
71 \\};77 \\};
78 \\pub export var s3: foo = foo{
79 \\ .x = @as(c_int, 123),
80 \\};
72 });81 });
7382
74 cases.add("simple ptrCast for casts between opaque types",83 cases.add("simple ptrCast for casts between opaque types",