authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-08 22:04:51-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-08 22:05:59-04:00
log46e1c34fcfe0973735c75a0c210db314030798f0
tree85d3b12619d3621ba7924537cf2f96b5ecfb4715
parent9bbd71c9ab3915b8bd8619a319a0eb6de2e2d152
signaturelock-open Commit is signed but in an unrecognized format.

self-hosted translate-c progress on function decls

See #1964

6 files changed, 102 insertions(+), 27 deletions(-)

src-self-hosted/clang.zig+18-5
......@@ -806,13 +806,13 @@ pub const enum_ZigClangAPValueKind = extern enum {
806806 ZigClangAPValueAddrLabelDiff,
807807};
808808pub extern fn ZigClangSourceManager_getSpellingLoc(arg0: ?*const struct_ZigClangSourceManager, Loc: struct_ZigClangSourceLocation) struct_ZigClangSourceLocation;
809pub extern fn ZigClangSourceManager_getFilename(arg0: ?*const struct_ZigClangSourceManager, SpellingLoc: struct_ZigClangSourceLocation) [*c]const u8;
809pub extern fn ZigClangSourceManager_getFilename(self: *const struct_ZigClangSourceManager, SpellingLoc: struct_ZigClangSourceLocation) ?[*]const u8;
810810pub extern fn ZigClangSourceManager_getSpellingLineNumber(arg0: ?*const struct_ZigClangSourceManager, Loc: struct_ZigClangSourceLocation) c_uint;
811811pub extern fn ZigClangSourceManager_getSpellingColumnNumber(arg0: ?*const struct_ZigClangSourceManager, Loc: struct_ZigClangSourceLocation) c_uint;
812812pub extern fn ZigClangSourceManager_getCharacterData(arg0: ?*const struct_ZigClangSourceManager, SL: struct_ZigClangSourceLocation) [*c]const u8;
813813pub extern fn ZigClangASTContext_getPointerType(arg0: ?*const struct_ZigClangASTContext, T: struct_ZigClangQualType) struct_ZigClangQualType;
814814pub extern fn ZigClangASTUnit_getASTContext(arg0: ?*struct_ZigClangASTUnit) ?*struct_ZigClangASTContext;
815pub extern fn ZigClangASTUnit_getSourceManager(arg0: ?*struct_ZigClangASTUnit) ?*struct_ZigClangSourceManager;
815pub extern fn ZigClangASTUnit_getSourceManager(self: *struct_ZigClangASTUnit) *struct_ZigClangSourceManager;
816816pub extern fn ZigClangASTUnit_visitLocalTopLevelDecls(self: *struct_ZigClangASTUnit, context: ?*c_void, Fn: ?extern fn (?*c_void, *const struct_ZigClangDecl) bool) bool;
817817pub extern fn ZigClangRecordType_getDecl(record_ty: ?*const struct_ZigClangRecordType) ?*const struct_ZigClangRecordDecl;
818818pub extern fn ZigClangEnumType_getDecl(record_ty: ?*const struct_ZigClangEnumType) ?*const struct_ZigClangEnumDecl;
......@@ -824,6 +824,7 @@ pub extern fn ZigClangEnumDecl_getDefinition(arg0: ?*const struct_ZigClangEnumDe
824824pub extern fn ZigClangRecordDecl_getLocation(arg0: ?*const struct_ZigClangRecordDecl) struct_ZigClangSourceLocation;
825825pub extern fn ZigClangEnumDecl_getLocation(arg0: ?*const struct_ZigClangEnumDecl) struct_ZigClangSourceLocation;
826826pub extern fn ZigClangTypedefNameDecl_getLocation(arg0: ?*const struct_ZigClangTypedefNameDecl) struct_ZigClangSourceLocation;
827pub extern fn ZigClangDecl_getLocation(self: *const ZigClangDecl) ZigClangSourceLocation;
827828pub extern fn ZigClangRecordDecl_isUnion(record_decl: ?*const struct_ZigClangRecordDecl) bool;
828829pub extern fn ZigClangRecordDecl_isStruct(record_decl: ?*const struct_ZigClangRecordDecl) bool;
829830pub extern fn ZigClangRecordDecl_isAnonymousStructOrUnion(record_decl: ?*const struct_ZigClangRecordDecl) bool;
......@@ -833,7 +834,7 @@ pub extern fn ZigClangSourceLocation_eq(a: struct_ZigClangSourceLocation, b: str
833834pub extern fn ZigClangTypedefType_getDecl(arg0: ?*const struct_ZigClangTypedefType) ?*const struct_ZigClangTypedefNameDecl;
834835pub extern fn ZigClangTypedefNameDecl_getUnderlyingType(arg0: ?*const struct_ZigClangTypedefNameDecl) struct_ZigClangQualType;
835836pub extern fn ZigClangQualType_getCanonicalType(arg0: struct_ZigClangQualType) struct_ZigClangQualType;
836pub extern fn ZigClangQualType_getTypePtr(arg0: struct_ZigClangQualType) ?*const struct_ZigClangType;
837pub extern fn ZigClangQualType_getTypePtr(self: struct_ZigClangQualType) *const struct_ZigClangType;
837838pub extern fn ZigClangQualType_addConst(arg0: [*c]struct_ZigClangQualType) void;
838839pub extern fn ZigClangQualType_eq(arg0: struct_ZigClangQualType, arg1: struct_ZigClangQualType) bool;
839840pub extern fn ZigClangQualType_isConstQualified(arg0: struct_ZigClangQualType) bool;
......@@ -841,7 +842,7 @@ pub extern fn ZigClangQualType_isVolatileQualified(arg0: struct_ZigClangQualType
841842pub extern fn ZigClangQualType_isRestrictQualified(arg0: struct_ZigClangQualType) bool;
842843pub extern fn ZigClangType_getTypeClass(self: ?*const struct_ZigClangType) enum_ZigClangTypeClass;
843844pub extern fn ZigClangType_isVoidType(self: ?*const struct_ZigClangType) bool;
844pub extern fn ZigClangType_getTypeClassName(self: ?*const struct_ZigClangType) [*c]const u8;
845pub extern fn ZigClangType_getTypeClassName(self: *const struct_ZigClangType) [*]const u8;
845846pub extern fn ZigClangStmt_getBeginLoc(self: ?*const struct_ZigClangStmt) struct_ZigClangSourceLocation;
846847pub extern fn ZigClangStmt_getStmtClass(self: ?*const struct_ZigClangStmt) enum_ZigClangStmtClass;
847848pub extern fn ZigClangStmt_classof_Expr(self: ?*const struct_ZigClangStmt) bool;
......@@ -863,6 +864,10 @@ pub extern fn ZigClangAPSInt_getRawData(self: ?*const struct_ZigClangAPSInt) [*c
863864pub extern fn ZigClangAPSInt_getNumWords(self: ?*const struct_ZigClangAPSInt) c_uint;
864865pub extern fn ZigClangAPValueLValueBase_dyn_cast_Expr(self: struct_ZigClangAPValueLValueBase) ?*const struct_ZigClangExpr;
865866pub extern fn ZigClangASTUnit_delete(arg0: ?*struct_ZigClangASTUnit) void;
867
868pub extern fn ZigClangFunctionDecl_getType(self: *const struct_ZigClangFunctionDecl) struct_ZigClangQualType;
869pub extern fn ZigClangFunctionDecl_getLocation(self: *const struct_ZigClangFunctionDecl) struct_ZigClangSourceLocation;
870
866871pub const ZigClangSourceLocation = struct_ZigClangSourceLocation;
867872pub const ZigClangQualType = struct_ZigClangQualType;
868873pub const ZigClangAPValueLValueBase = struct_ZigClangAPValueLValueBase;
......@@ -944,6 +949,10 @@ pub const ZigClangStmtClass = enum_ZigClangStmtClass;
944949pub const ZigClangCK = enum_ZigClangCK;
945950pub const ZigClangAPValueKind = enum_ZigClangAPValueKind;
946951
952pub const struct_ZigClangSourceLocation = extern struct {
953 ID: c_uint,
954};
955
947956pub const Stage2ErrorMsg = extern struct {
948957 filename_ptr: ?[*]const u8,
949958 filename_len: usize,
......@@ -968,8 +977,8 @@ pub extern fn ZigClangLoadFromCommandLine(
968977 resources_path: [*c]const u8,
969978) ?*ZigClangASTUnit;
970979
971
972980pub extern fn ZigClangDecl_getKind(decl: *const ZigClangDecl) ZigClangDeclKind;
981pub extern fn ZigClangDecl_getDeclKindName(decl: *const struct_ZigClangDecl) [*]const u8;
973982
974983pub const ZigClangDeclKind = extern enum {
975984 AccessSpec,
......@@ -1047,3 +1056,7 @@ pub const ZigClangDeclKind = extern enum {
10471056 StaticAssert,
10481057 TranslationUnit,
10491058};
1059
1060pub const struct_ZigClangQualType = extern struct {
1061 ptr: ?*c_void,
1062};
src-self-hosted/stage1.zig+2-1
......@@ -90,7 +90,8 @@ export fn stage2_translate_c(
9090 .import => translate_c.Mode.import,
9191 .translate => translate_c.Mode.translate,
9292 }, &errors, resources_path) catch |err| switch (err) {
93 error.SemanticAnalyzeFail => {
93 // TODO after https://github.com/ziglang/zig/issues/769 we can remove error.UnsupportedType
94 error.SemanticAnalyzeFail, error.UnsupportedType => {
9495 out_errors_ptr.* = errors.ptr;
9596 out_errors_len.* = errors.len;
9697 return Error.CCompileErrors;
src-self-hosted/translate_c.zig+54-8
......@@ -13,12 +13,16 @@ pub const Mode = enum {
1313
1414pub const ClangErrMsg = Stage2ErrorMsg;
1515
16pub const Error = error{OutOfMemory};
16pub const Error = error{
17 OutOfMemory,
18 UnsupportedType,
19};
1720
1821const Context = struct {
1922 tree: *ast.Tree,
2023 source_buffer: *std.Buffer,
2124 err: Error,
25 source_manager: *ZigClangSourceManager,
2226
2327 fn a(c: *Context) *std.mem.Allocator {
2428 return &c.tree.arena_allocator.allocator;
......@@ -28,6 +32,17 @@ const Context = struct {
2832 fn str(c: *Context, s: [*]const u8) ![]u8 {
2933 return std.mem.dupe(c.a(), u8, std.mem.toSliceConst(u8, s));
3034 }
35
36 /// Convert a clang source location to a file:line:column string
37 fn locStr(c: *Context, loc: ZigClangSourceLocation) ![]u8 {
38 const spelling_loc = ZigClangSourceManager_getSpellingLoc(c.source_manager, loc);
39 const filename_c = ZigClangSourceManager_getFilename(c.source_manager, spelling_loc);
40 const filename = if (filename_c) |s| try c.str(s) else ([]const u8)("(no file)");
41
42 const line = ZigClangSourceManager_getSpellingLineNumber(c.source_manager, spelling_loc);
43 const column = ZigClangSourceManager_getSpellingColumnNumber(c.source_manager, spelling_loc);
44 return std.fmt.allocPrint(c.a(), "{}:{}:{}", filename, line, column);
45 }
3146};
3247
3348pub fn translate(
......@@ -78,6 +93,7 @@ pub fn translate(
7893 var context = Context{
7994 .tree = tree,
8095 .source_buffer = &source_buffer,
96 .source_manager = ZigClangASTUnit_getSourceManager(ast_unit),
8197 .err = undefined,
8298 };
8399
......@@ -105,27 +121,57 @@ fn declVisitor(c: *Context, decl: *const ZigClangDecl) Error!void {
105121 return visitFnDecl(c, @ptrCast(*const ZigClangFunctionDecl, decl));
106122 },
107123 .Typedef => {
108 try appendToken(c, .LineComment, "// TODO translate typedef");
124 try emitWarning(c, ZigClangDecl_getLocation(decl), "TODO implement translate-c for typedefs");
109125 },
110126 .Enum => {
111 try appendToken(c, .LineComment, "// TODO translate enum");
127 try emitWarning(c, ZigClangDecl_getLocation(decl), "TODO implement translate-c for enums");
112128 },
113129 .Record => {
114 try appendToken(c, .LineComment, "// TODO translate struct");
130 try emitWarning(c, ZigClangDecl_getLocation(decl), "TODO implement translate-c for structs");
115131 },
116132 .Var => {
117 try appendToken(c, .LineComment, "// TODO translate variable");
133 try emitWarning(c, ZigClangDecl_getLocation(decl), "TODO implement translate-c for variables");
118134 },
119135 else => {
120 // TODO emit_warning(c, bitcast(decl->getLocation()), "ignoring %s decl", decl->getDeclKindName());
121 try appendToken(c, .LineComment, "// TODO translate unknown decl");
136 const decl_name = try c.str(ZigClangDecl_getDeclKindName(decl));
137 try emitWarning(c, ZigClangDecl_getLocation(decl), "ignoring {} declaration", decl_name);
122138 },
123139 }
124140}
125141
126142fn visitFnDecl(c: *Context, fn_decl: *const ZigClangFunctionDecl) Error!void {
127143 const fn_name = c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, fn_decl)));
128 try appendToken(c, .LineComment, "// TODO translate function '{}'", fn_name);
144
145 // TODO The C++ code has this:
146 //if (get_global(c, fn_name)) {
147 // // we already saw this function
148 // return;
149 //}
150
151 const fn_decl_loc = ZigClangFunctionDecl_getLocation(fn_decl);
152 const proto_node = transQualType(c, ZigClangFunctionDecl_getType(fn_decl), fn_decl_loc) catch |e| switch (e) {
153 error.UnsupportedType => {
154 try emitWarning(c, fn_decl_loc, "unable to resolve prototype of function '{}'", fn_name);
155 return;
156 },
157 else => return e,
158 };
159
160 try emitWarning(c, fn_decl_loc, "TODO implement translate-c for function decls");
161}
162
163fn transQualType(c: *Context, qt: ZigClangQualType, source_loc: ZigClangSourceLocation) !*ast.Node {
164 return transType(c, ZigClangQualType_getTypePtr(qt), source_loc);
165}
166
167fn transType(c: *Context, ty: *const ZigClangType, source_loc: ZigClangSourceLocation) !*ast.Node {
168 const type_name = c.str(ZigClangType_getTypeClassName(ty));
169 try emitWarning(c, source_loc, "unsupported type: '{}'", type_name);
170 return error.UnsupportedType;
171}
172
173fn emitWarning(c: *Context, loc: ZigClangSourceLocation, comptime format: []const u8, args: ...) !void {
174 try appendToken(c, .LineComment, "// {}: warning: " ++ format, c.locStr(loc), args);
129175}
130176
131177fn appendToken(c: *Context, token_id: Token.Id, comptime format: []const u8, args: ...) !void {
src/translate_c.cpp+15-13
......@@ -4009,7 +4009,7 @@ static TransScope *trans_stmt(Context *c, TransScope *scope, const ZigClangStmt
40094009 return child_scope;
40104010}
40114011
4012static void visit_fn_decl(Context *c, const clang::FunctionDecl *fn_decl) {
4012static void visit_fn_decl(Context *c, const ZigClangFunctionDecl *fn_decl) {
40134013 Buf *fn_name = buf_create_from_str(ZigClangDecl_getName_bytes_begin((const ZigClangDecl *)fn_decl));
40144014
40154015 if (get_global(c, fn_name)) {
......@@ -4017,26 +4017,28 @@ static void visit_fn_decl(Context *c, const clang::FunctionDecl *fn_decl) {
40174017 return;
40184018 }
40194019
4020 AstNode *proto_node = trans_qual_type(c, bitcast(fn_decl->getType()), bitcast(fn_decl->getLocation()));
4020 AstNode *proto_node = trans_qual_type(c, ZigClangFunctionDecl_getType(fn_decl),
4021 ZigClangFunctionDecl_getLocation(fn_decl));
40214022 if (proto_node == nullptr) {
4022 emit_warning(c, bitcast(fn_decl->getLocation()), "unable to resolve prototype of function '%s'", buf_ptr(fn_name));
4023 emit_warning(c, ZigClangFunctionDecl_getLocation(fn_decl),
4024 "unable to resolve prototype of function '%s'", buf_ptr(fn_name));
40234025 return;
40244026 }
40254027
40264028 proto_node->data.fn_proto.name = fn_name;
4027 proto_node->data.fn_proto.is_extern = !fn_decl->hasBody();
4029 proto_node->data.fn_proto.is_extern = !((const clang::FunctionDecl*)fn_decl)->hasBody();
40284030
4029 clang::StorageClass sc = fn_decl->getStorageClass();
4031 clang::StorageClass sc = ((const clang::FunctionDecl*)fn_decl)->getStorageClass();
40304032 if (sc == clang::SC_None) {
40314033 proto_node->data.fn_proto.visib_mod = c->visib_mod;
4032 proto_node->data.fn_proto.is_export = fn_decl->hasBody() ? c->want_export : false;
4034 proto_node->data.fn_proto.is_export = ((const clang::FunctionDecl*)fn_decl)->hasBody() ? c->want_export : false;
40334035 } else if (sc == clang::SC_Extern || sc == clang::SC_Static) {
40344036 proto_node->data.fn_proto.visib_mod = c->visib_mod;
40354037 } else if (sc == clang::SC_PrivateExtern) {
4036 emit_warning(c, bitcast(fn_decl->getLocation()), "unsupported storage class: private extern");
4038 emit_warning(c, ZigClangFunctionDecl_getLocation(fn_decl), "unsupported storage class: private extern");
40374039 return;
40384040 } else {
4039 emit_warning(c, bitcast(fn_decl->getLocation()), "unsupported storage class: unknown");
4041 emit_warning(c, ZigClangFunctionDecl_getLocation(fn_decl), "unsupported storage class: unknown");
40404042 return;
40414043 }
40424044
......@@ -4044,7 +4046,7 @@ static void visit_fn_decl(Context *c, const clang::FunctionDecl *fn_decl) {
40444046
40454047 for (size_t i = 0; i < proto_node->data.fn_proto.params.length; i += 1) {
40464048 AstNode *param_node = proto_node->data.fn_proto.params.at(i);
4047 const clang::ParmVarDecl *param = fn_decl->getParamDecl(i);
4049 const clang::ParmVarDecl *param = ((const clang::FunctionDecl*)fn_decl)->getParamDecl(i);
40484050 const char *name = ZigClangDecl_getName_bytes_begin((const ZigClangDecl *)param);
40494051
40504052 Buf *proto_param_name;
......@@ -4063,7 +4065,7 @@ static void visit_fn_decl(Context *c, const clang::FunctionDecl *fn_decl) {
40634065 param_node->data.param_decl.name = scope_var->zig_name;
40644066 }
40654067
4066 if (!fn_decl->hasBody()) {
4068 if (!((const clang::FunctionDecl*)fn_decl)->hasBody()) {
40674069 // just a prototype
40684070 add_top_level_decl(c, proto_node->data.fn_proto.name, proto_node);
40694071 return;
......@@ -4071,11 +4073,11 @@ static void visit_fn_decl(Context *c, const clang::FunctionDecl *fn_decl) {
40714073
40724074 // actual function definition with body
40734075 c->ptr_params.clear();
4074 const ZigClangStmt *body = bitcast(fn_decl->getBody());
4076 const ZigClangStmt *body = bitcast(((const clang::FunctionDecl*)fn_decl)->getBody());
40754077 AstNode *actual_body_node;
40764078 TransScope *result_scope = trans_stmt(c, scope, body, &actual_body_node);
40774079 if (result_scope == nullptr) {
4078 emit_warning(c, bitcast(fn_decl->getLocation()), "unable to translate function");
4080 emit_warning(c, ZigClangFunctionDecl_getLocation(fn_decl), "unable to translate function");
40794081 return;
40804082 }
40814083 assert(actual_body_node != nullptr);
......@@ -4558,7 +4560,7 @@ static bool decl_visitor(void *context, const ZigClangDecl *decl) {
45584560
45594561 switch (ZigClangDecl_getKind(decl)) {
45604562 case ZigClangDeclFunction:
4561 visit_fn_decl(c, reinterpret_cast<const clang::FunctionDecl*>(decl));
4563 visit_fn_decl(c, reinterpret_cast<const ZigClangFunctionDecl*>(decl));
45624564 break;
45634565 case ZigClangDeclTypedef:
45644566 resolve_typedef_decl(c, reinterpret_cast<const ZigClangTypedefNameDecl *>(decl));
src/zig_clang.cpp+10
......@@ -1142,6 +1142,16 @@ ZigClangQualType ZigClangEnumDecl_getIntegerType(const ZigClangEnumDecl *self) {
11421142 return bitcast(reinterpret_cast<const clang::EnumDecl *>(self)->getIntegerType());
11431143}
11441144
1145struct ZigClangQualType ZigClangFunctionDecl_getType(const struct ZigClangFunctionDecl *self) {
1146 auto casted = reinterpret_cast<const clang::FunctionDecl *>(self);
1147 return bitcast(casted->getType());
1148}
1149
1150struct ZigClangSourceLocation ZigClangFunctionDecl_getLocation(const struct ZigClangFunctionDecl *self) {
1151 auto casted = reinterpret_cast<const clang::FunctionDecl *>(self);
1152 return bitcast(casted->getLocation());
1153}
1154
11451155const ZigClangTypedefNameDecl *ZigClangTypedefType_getDecl(const ZigClangTypedefType *self) {
11461156 auto casted = reinterpret_cast<const clang::TypedefType *>(self);
11471157 const clang::TypedefNameDecl *name_decl = casted->getDecl();
src/zig_clang.h+3
......@@ -599,6 +599,9 @@ ZIG_EXTERN_C struct ZigClangSourceLocation ZigClangEnumDecl_getLocation(const st
599599ZIG_EXTERN_C struct ZigClangSourceLocation ZigClangTypedefNameDecl_getLocation(const struct ZigClangTypedefNameDecl *);
600600ZIG_EXTERN_C struct ZigClangSourceLocation ZigClangDecl_getLocation(const struct ZigClangDecl *);
601601
602ZIG_EXTERN_C struct ZigClangQualType ZigClangFunctionDecl_getType(const struct ZigClangFunctionDecl *);
603ZIG_EXTERN_C struct ZigClangSourceLocation ZigClangFunctionDecl_getLocation(const struct ZigClangFunctionDecl *);
604
602605ZIG_EXTERN_C bool ZigClangRecordDecl_isUnion(const struct ZigClangRecordDecl *record_decl);
603606ZIG_EXTERN_C bool ZigClangRecordDecl_isStruct(const struct ZigClangRecordDecl *record_decl);
604607ZIG_EXTERN_C bool ZigClangRecordDecl_isAnonymousStructOrUnion(const struct ZigClangRecordDecl *record_decl);