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
signature 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 {...@@ -806,13 +806,13 @@ pub const enum_ZigClangAPValueKind = extern enum {
806 ZigClangAPValueAddrLabelDiff,806 ZigClangAPValueAddrLabelDiff,
807};807};
808pub extern fn ZigClangSourceManager_getSpellingLoc(arg0: ?*const struct_ZigClangSourceManager, Loc: struct_ZigClangSourceLocation) struct_ZigClangSourceLocation;808pub 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;
810pub extern fn ZigClangSourceManager_getSpellingLineNumber(arg0: ?*const struct_ZigClangSourceManager, Loc: struct_ZigClangSourceLocation) c_uint;810pub extern fn ZigClangSourceManager_getSpellingLineNumber(arg0: ?*const struct_ZigClangSourceManager, Loc: struct_ZigClangSourceLocation) c_uint;
811pub extern fn ZigClangSourceManager_getSpellingColumnNumber(arg0: ?*const struct_ZigClangSourceManager, Loc: struct_ZigClangSourceLocation) c_uint;811pub extern fn ZigClangSourceManager_getSpellingColumnNumber(arg0: ?*const struct_ZigClangSourceManager, Loc: struct_ZigClangSourceLocation) c_uint;
812pub extern fn ZigClangSourceManager_getCharacterData(arg0: ?*const struct_ZigClangSourceManager, SL: struct_ZigClangSourceLocation) [*c]const u8;812pub extern fn ZigClangSourceManager_getCharacterData(arg0: ?*const struct_ZigClangSourceManager, SL: struct_ZigClangSourceLocation) [*c]const u8;
813pub extern fn ZigClangASTContext_getPointerType(arg0: ?*const struct_ZigClangASTContext, T: struct_ZigClangQualType) struct_ZigClangQualType;813pub extern fn ZigClangASTContext_getPointerType(arg0: ?*const struct_ZigClangASTContext, T: struct_ZigClangQualType) struct_ZigClangQualType;
814pub extern fn ZigClangASTUnit_getASTContext(arg0: ?*struct_ZigClangASTUnit) ?*struct_ZigClangASTContext;814pub 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;
816pub extern fn ZigClangASTUnit_visitLocalTopLevelDecls(self: *struct_ZigClangASTUnit, context: ?*c_void, Fn: ?extern fn (?*c_void, *const struct_ZigClangDecl) bool) bool;816pub extern fn ZigClangASTUnit_visitLocalTopLevelDecls(self: *struct_ZigClangASTUnit, context: ?*c_void, Fn: ?extern fn (?*c_void, *const struct_ZigClangDecl) bool) bool;
817pub extern fn ZigClangRecordType_getDecl(record_ty: ?*const struct_ZigClangRecordType) ?*const struct_ZigClangRecordDecl;817pub extern fn ZigClangRecordType_getDecl(record_ty: ?*const struct_ZigClangRecordType) ?*const struct_ZigClangRecordDecl;
818pub extern fn ZigClangEnumType_getDecl(record_ty: ?*const struct_ZigClangEnumType) ?*const struct_ZigClangEnumDecl;818pub 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...@@ -824,6 +824,7 @@ pub extern fn ZigClangEnumDecl_getDefinition(arg0: ?*const struct_ZigClangEnumDe
824pub extern fn ZigClangRecordDecl_getLocation(arg0: ?*const struct_ZigClangRecordDecl) struct_ZigClangSourceLocation;824pub extern fn ZigClangRecordDecl_getLocation(arg0: ?*const struct_ZigClangRecordDecl) struct_ZigClangSourceLocation;
825pub extern fn ZigClangEnumDecl_getLocation(arg0: ?*const struct_ZigClangEnumDecl) struct_ZigClangSourceLocation;825pub extern fn ZigClangEnumDecl_getLocation(arg0: ?*const struct_ZigClangEnumDecl) struct_ZigClangSourceLocation;
826pub extern fn ZigClangTypedefNameDecl_getLocation(arg0: ?*const struct_ZigClangTypedefNameDecl) struct_ZigClangSourceLocation;826pub extern fn ZigClangTypedefNameDecl_getLocation(arg0: ?*const struct_ZigClangTypedefNameDecl) struct_ZigClangSourceLocation;
827pub extern fn ZigClangDecl_getLocation(self: *const ZigClangDecl) ZigClangSourceLocation;
827pub extern fn ZigClangRecordDecl_isUnion(record_decl: ?*const struct_ZigClangRecordDecl) bool;828pub extern fn ZigClangRecordDecl_isUnion(record_decl: ?*const struct_ZigClangRecordDecl) bool;
828pub extern fn ZigClangRecordDecl_isStruct(record_decl: ?*const struct_ZigClangRecordDecl) bool;829pub extern fn ZigClangRecordDecl_isStruct(record_decl: ?*const struct_ZigClangRecordDecl) bool;
829pub extern fn ZigClangRecordDecl_isAnonymousStructOrUnion(record_decl: ?*const struct_ZigClangRecordDecl) bool;830pub extern fn ZigClangRecordDecl_isAnonymousStructOrUnion(record_decl: ?*const struct_ZigClangRecordDecl) bool;
...@@ -833,7 +834,7 @@ pub extern fn ZigClangSourceLocation_eq(a: struct_ZigClangSourceLocation, b: str...@@ -833,7 +834,7 @@ pub extern fn ZigClangSourceLocation_eq(a: struct_ZigClangSourceLocation, b: str
833pub extern fn ZigClangTypedefType_getDecl(arg0: ?*const struct_ZigClangTypedefType) ?*const struct_ZigClangTypedefNameDecl;834pub extern fn ZigClangTypedefType_getDecl(arg0: ?*const struct_ZigClangTypedefType) ?*const struct_ZigClangTypedefNameDecl;
834pub extern fn ZigClangTypedefNameDecl_getUnderlyingType(arg0: ?*const struct_ZigClangTypedefNameDecl) struct_ZigClangQualType;835pub extern fn ZigClangTypedefNameDecl_getUnderlyingType(arg0: ?*const struct_ZigClangTypedefNameDecl) struct_ZigClangQualType;
835pub extern fn ZigClangQualType_getCanonicalType(arg0: struct_ZigClangQualType) struct_ZigClangQualType;836pub 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;
837pub extern fn ZigClangQualType_addConst(arg0: [*c]struct_ZigClangQualType) void;838pub extern fn ZigClangQualType_addConst(arg0: [*c]struct_ZigClangQualType) void;
838pub extern fn ZigClangQualType_eq(arg0: struct_ZigClangQualType, arg1: struct_ZigClangQualType) bool;839pub extern fn ZigClangQualType_eq(arg0: struct_ZigClangQualType, arg1: struct_ZigClangQualType) bool;
839pub extern fn ZigClangQualType_isConstQualified(arg0: struct_ZigClangQualType) bool;840pub extern fn ZigClangQualType_isConstQualified(arg0: struct_ZigClangQualType) bool;
...@@ -841,7 +842,7 @@ pub extern fn ZigClangQualType_isVolatileQualified(arg0: struct_ZigClangQualType...@@ -841,7 +842,7 @@ pub extern fn ZigClangQualType_isVolatileQualified(arg0: struct_ZigClangQualType
841pub extern fn ZigClangQualType_isRestrictQualified(arg0: struct_ZigClangQualType) bool;842pub extern fn ZigClangQualType_isRestrictQualified(arg0: struct_ZigClangQualType) bool;
842pub extern fn ZigClangType_getTypeClass(self: ?*const struct_ZigClangType) enum_ZigClangTypeClass;843pub extern fn ZigClangType_getTypeClass(self: ?*const struct_ZigClangType) enum_ZigClangTypeClass;
843pub extern fn ZigClangType_isVoidType(self: ?*const struct_ZigClangType) bool;844pub 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;
845pub extern fn ZigClangStmt_getBeginLoc(self: ?*const struct_ZigClangStmt) struct_ZigClangSourceLocation;846pub extern fn ZigClangStmt_getBeginLoc(self: ?*const struct_ZigClangStmt) struct_ZigClangSourceLocation;
846pub extern fn ZigClangStmt_getStmtClass(self: ?*const struct_ZigClangStmt) enum_ZigClangStmtClass;847pub extern fn ZigClangStmt_getStmtClass(self: ?*const struct_ZigClangStmt) enum_ZigClangStmtClass;
847pub extern fn ZigClangStmt_classof_Expr(self: ?*const struct_ZigClangStmt) bool;848pub extern fn ZigClangStmt_classof_Expr(self: ?*const struct_ZigClangStmt) bool;
...@@ -863,6 +864,10 @@ pub extern fn ZigClangAPSInt_getRawData(self: ?*const struct_ZigClangAPSInt) [*c...@@ -863,6 +864,10 @@ pub extern fn ZigClangAPSInt_getRawData(self: ?*const struct_ZigClangAPSInt) [*c
863pub extern fn ZigClangAPSInt_getNumWords(self: ?*const struct_ZigClangAPSInt) c_uint;864pub extern fn ZigClangAPSInt_getNumWords(self: ?*const struct_ZigClangAPSInt) c_uint;
864pub extern fn ZigClangAPValueLValueBase_dyn_cast_Expr(self: struct_ZigClangAPValueLValueBase) ?*const struct_ZigClangExpr;865pub extern fn ZigClangAPValueLValueBase_dyn_cast_Expr(self: struct_ZigClangAPValueLValueBase) ?*const struct_ZigClangExpr;
865pub extern fn ZigClangASTUnit_delete(arg0: ?*struct_ZigClangASTUnit) void;866pub 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
866pub const ZigClangSourceLocation = struct_ZigClangSourceLocation;871pub const ZigClangSourceLocation = struct_ZigClangSourceLocation;
867pub const ZigClangQualType = struct_ZigClangQualType;872pub const ZigClangQualType = struct_ZigClangQualType;
868pub const ZigClangAPValueLValueBase = struct_ZigClangAPValueLValueBase;873pub const ZigClangAPValueLValueBase = struct_ZigClangAPValueLValueBase;
...@@ -944,6 +949,10 @@ pub const ZigClangStmtClass = enum_ZigClangStmtClass;...@@ -944,6 +949,10 @@ pub const ZigClangStmtClass = enum_ZigClangStmtClass;
944pub const ZigClangCK = enum_ZigClangCK;949pub const ZigClangCK = enum_ZigClangCK;
945pub const ZigClangAPValueKind = enum_ZigClangAPValueKind;950pub const ZigClangAPValueKind = enum_ZigClangAPValueKind;
946951
952pub const struct_ZigClangSourceLocation = extern struct {
953 ID: c_uint,
954};
955
947pub const Stage2ErrorMsg = extern struct {956pub const Stage2ErrorMsg = extern struct {
948 filename_ptr: ?[*]const u8,957 filename_ptr: ?[*]const u8,
949 filename_len: usize,958 filename_len: usize,
...@@ -968,8 +977,8 @@ pub extern fn ZigClangLoadFromCommandLine(...@@ -968,8 +977,8 @@ pub extern fn ZigClangLoadFromCommandLine(
968 resources_path: [*c]const u8,977 resources_path: [*c]const u8,
969) ?*ZigClangASTUnit;978) ?*ZigClangASTUnit;
970979
971
972pub extern fn ZigClangDecl_getKind(decl: *const ZigClangDecl) ZigClangDeclKind;980pub extern fn ZigClangDecl_getKind(decl: *const ZigClangDecl) ZigClangDeclKind;
981pub extern fn ZigClangDecl_getDeclKindName(decl: *const struct_ZigClangDecl) [*]const u8;
973982
974pub const ZigClangDeclKind = extern enum {983pub const ZigClangDeclKind = extern enum {
975 AccessSpec,984 AccessSpec,
...@@ -1047,3 +1056,7 @@ pub const ZigClangDeclKind = extern enum {...@@ -1047,3 +1056,7 @@ pub const ZigClangDeclKind = extern enum {
1047 StaticAssert,1056 StaticAssert,
1048 TranslationUnit,1057 TranslationUnit,
1049};1058};
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(...@@ -90,7 +90,8 @@ export fn stage2_translate_c(
90 .import => translate_c.Mode.import,90 .import => translate_c.Mode.import,
91 .translate => translate_c.Mode.translate,91 .translate => translate_c.Mode.translate,
92 }, &errors, resources_path) catch |err| switch (err) {92 }, &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 => {
94 out_errors_ptr.* = errors.ptr;95 out_errors_ptr.* = errors.ptr;
95 out_errors_len.* = errors.len;96 out_errors_len.* = errors.len;
96 return Error.CCompileErrors;97 return Error.CCompileErrors;
src-self-hosted/translate_c.zig+54-8
...@@ -13,12 +13,16 @@ pub const Mode = enum {...@@ -13,12 +13,16 @@ pub const Mode = enum {
1313
14pub const ClangErrMsg = Stage2ErrorMsg;14pub const ClangErrMsg = Stage2ErrorMsg;
1515
16pub const Error = error{OutOfMemory};16pub const Error = error{
17 OutOfMemory,
18 UnsupportedType,
19};
1720
18const Context = struct {21const Context = struct {
19 tree: *ast.Tree,22 tree: *ast.Tree,
20 source_buffer: *std.Buffer,23 source_buffer: *std.Buffer,
21 err: Error,24 err: Error,
25 source_manager: *ZigClangSourceManager,
2226
23 fn a(c: *Context) *std.mem.Allocator {27 fn a(c: *Context) *std.mem.Allocator {
24 return &c.tree.arena_allocator.allocator;28 return &c.tree.arena_allocator.allocator;
...@@ -28,6 +32,17 @@ const Context = struct {...@@ -28,6 +32,17 @@ const Context = struct {
28 fn str(c: *Context, s: [*]const u8) ![]u8 {32 fn str(c: *Context, s: [*]const u8) ![]u8 {
29 return std.mem.dupe(c.a(), u8, std.mem.toSliceConst(u8, s));33 return std.mem.dupe(c.a(), u8, std.mem.toSliceConst(u8, s));
30 }34 }
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 }
31};46};
3247
33pub fn translate(48pub fn translate(
...@@ -78,6 +93,7 @@ pub fn translate(...@@ -78,6 +93,7 @@ pub fn translate(
78 var context = Context{93 var context = Context{
79 .tree = tree,94 .tree = tree,
80 .source_buffer = &source_buffer,95 .source_buffer = &source_buffer,
96 .source_manager = ZigClangASTUnit_getSourceManager(ast_unit),
81 .err = undefined,97 .err = undefined,
82 };98 };
8399
...@@ -105,27 +121,57 @@ fn declVisitor(c: *Context, decl: *const ZigClangDecl) Error!void {...@@ -105,27 +121,57 @@ fn declVisitor(c: *Context, decl: *const ZigClangDecl) Error!void {
105 return visitFnDecl(c, @ptrCast(*const ZigClangFunctionDecl, decl));121 return visitFnDecl(c, @ptrCast(*const ZigClangFunctionDecl, decl));
106 },122 },
107 .Typedef => {123 .Typedef => {
108 try appendToken(c, .LineComment, "// TODO translate typedef");124 try emitWarning(c, ZigClangDecl_getLocation(decl), "TODO implement translate-c for typedefs");
109 },125 },
110 .Enum => {126 .Enum => {
111 try appendToken(c, .LineComment, "// TODO translate enum");127 try emitWarning(c, ZigClangDecl_getLocation(decl), "TODO implement translate-c for enums");
112 },128 },
113 .Record => {129 .Record => {
114 try appendToken(c, .LineComment, "// TODO translate struct");130 try emitWarning(c, ZigClangDecl_getLocation(decl), "TODO implement translate-c for structs");
115 },131 },
116 .Var => {132 .Var => {
117 try appendToken(c, .LineComment, "// TODO translate variable");133 try emitWarning(c, ZigClangDecl_getLocation(decl), "TODO implement translate-c for variables");
118 },134 },
119 else => {135 else => {
120 // TODO emit_warning(c, bitcast(decl->getLocation()), "ignoring %s decl", decl->getDeclKindName());136 const decl_name = try c.str(ZigClangDecl_getDeclKindName(decl));
121 try appendToken(c, .LineComment, "// TODO translate unknown decl");137 try emitWarning(c, ZigClangDecl_getLocation(decl), "ignoring {} declaration", decl_name);
122 },138 },
123 }139 }
124}140}
125141
126fn visitFnDecl(c: *Context, fn_decl: *const ZigClangFunctionDecl) Error!void {142fn visitFnDecl(c: *Context, fn_decl: *const ZigClangFunctionDecl) Error!void {
127 const fn_name = c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, fn_decl)));143 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);
129}175}
130176
131fn appendToken(c: *Context, token_id: Token.Id, comptime format: []const u8, args: ...) !void {177fn 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...@@ -4009,7 +4009,7 @@ static TransScope *trans_stmt(Context *c, TransScope *scope, const ZigClangStmt
4009 return child_scope;4009 return child_scope;
4010}4010}
40114011
4012static void visit_fn_decl(Context *c, const clang::FunctionDecl *fn_decl) {4012static void visit_fn_decl(Context *c, const ZigClangFunctionDecl *fn_decl) {
4013 Buf *fn_name = buf_create_from_str(ZigClangDecl_getName_bytes_begin((const ZigClangDecl *)fn_decl));4013 Buf *fn_name = buf_create_from_str(ZigClangDecl_getName_bytes_begin((const ZigClangDecl *)fn_decl));
40144014
4015 if (get_global(c, fn_name)) {4015 if (get_global(c, fn_name)) {
...@@ -4017,26 +4017,28 @@ static void visit_fn_decl(Context *c, const clang::FunctionDecl *fn_decl) {...@@ -4017,26 +4017,28 @@ static void visit_fn_decl(Context *c, const clang::FunctionDecl *fn_decl) {
4017 return;4017 return;
4018 }4018 }
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));
4021 if (proto_node == nullptr) {4022 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));
4023 return;4025 return;
4024 }4026 }
40254027
4026 proto_node->data.fn_proto.name = fn_name;4028 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();
4030 if (sc == clang::SC_None) {4032 if (sc == clang::SC_None) {
4031 proto_node->data.fn_proto.visib_mod = c->visib_mod;4033 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;
4033 } else if (sc == clang::SC_Extern || sc == clang::SC_Static) {4035 } else if (sc == clang::SC_Extern || sc == clang::SC_Static) {
4034 proto_node->data.fn_proto.visib_mod = c->visib_mod;4036 proto_node->data.fn_proto.visib_mod = c->visib_mod;
4035 } else if (sc == clang::SC_PrivateExtern) {4037 } 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");
4037 return;4039 return;
4038 } else {4040 } 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");
4040 return;4042 return;
4041 }4043 }
40424044
...@@ -4044,7 +4046,7 @@ static void visit_fn_decl(Context *c, const clang::FunctionDecl *fn_decl) {...@@ -4044,7 +4046,7 @@ static void visit_fn_decl(Context *c, const clang::FunctionDecl *fn_decl) {
40444046
4045 for (size_t i = 0; i < proto_node->data.fn_proto.params.length; i += 1) {4047 for (size_t i = 0; i < proto_node->data.fn_proto.params.length; i += 1) {
4046 AstNode *param_node = proto_node->data.fn_proto.params.at(i);4048 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);
4048 const char *name = ZigClangDecl_getName_bytes_begin((const ZigClangDecl *)param);4050 const char *name = ZigClangDecl_getName_bytes_begin((const ZigClangDecl *)param);
40494051
4050 Buf *proto_param_name;4052 Buf *proto_param_name;
...@@ -4063,7 +4065,7 @@ static void visit_fn_decl(Context *c, const clang::FunctionDecl *fn_decl) {...@@ -4063,7 +4065,7 @@ static void visit_fn_decl(Context *c, const clang::FunctionDecl *fn_decl) {
4063 param_node->data.param_decl.name = scope_var->zig_name;4065 param_node->data.param_decl.name = scope_var->zig_name;
4064 }4066 }
40654067
4066 if (!fn_decl->hasBody()) {4068 if (!((const clang::FunctionDecl*)fn_decl)->hasBody()) {
4067 // just a prototype4069 // just a prototype
4068 add_top_level_decl(c, proto_node->data.fn_proto.name, proto_node);4070 add_top_level_decl(c, proto_node->data.fn_proto.name, proto_node);
4069 return;4071 return;
...@@ -4071,11 +4073,11 @@ static void visit_fn_decl(Context *c, const clang::FunctionDecl *fn_decl) {...@@ -4071,11 +4073,11 @@ static void visit_fn_decl(Context *c, const clang::FunctionDecl *fn_decl) {
40714073
4072 // actual function definition with body4074 // actual function definition with body
4073 c->ptr_params.clear();4075 c->ptr_params.clear();
4074 const ZigClangStmt *body = bitcast(fn_decl->getBody());4076 const ZigClangStmt *body = bitcast(((const clang::FunctionDecl*)fn_decl)->getBody());
4075 AstNode *actual_body_node;4077 AstNode *actual_body_node;
4076 TransScope *result_scope = trans_stmt(c, scope, body, &actual_body_node);4078 TransScope *result_scope = trans_stmt(c, scope, body, &actual_body_node);
4077 if (result_scope == nullptr) {4079 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");
4079 return;4081 return;
4080 }4082 }
4081 assert(actual_body_node != nullptr);4083 assert(actual_body_node != nullptr);
...@@ -4558,7 +4560,7 @@ static bool decl_visitor(void *context, const ZigClangDecl *decl) {...@@ -4558,7 +4560,7 @@ static bool decl_visitor(void *context, const ZigClangDecl *decl) {
45584560
4559 switch (ZigClangDecl_getKind(decl)) {4561 switch (ZigClangDecl_getKind(decl)) {
4560 case ZigClangDeclFunction:4562 case ZigClangDeclFunction:
4561 visit_fn_decl(c, reinterpret_cast<const clang::FunctionDecl*>(decl));4563 visit_fn_decl(c, reinterpret_cast<const ZigClangFunctionDecl*>(decl));
4562 break;4564 break;
4563 case ZigClangDeclTypedef:4565 case ZigClangDeclTypedef:
4564 resolve_typedef_decl(c, reinterpret_cast<const ZigClangTypedefNameDecl *>(decl));4566 resolve_typedef_decl(c, reinterpret_cast<const ZigClangTypedefNameDecl *>(decl));
src/zig_clang.cpp+10
...@@ -1142,6 +1142,16 @@ ZigClangQualType ZigClangEnumDecl_getIntegerType(const ZigClangEnumDecl *self) {...@@ -1142,6 +1142,16 @@ ZigClangQualType ZigClangEnumDecl_getIntegerType(const ZigClangEnumDecl *self) {
1142 return bitcast(reinterpret_cast<const clang::EnumDecl *>(self)->getIntegerType());1142 return bitcast(reinterpret_cast<const clang::EnumDecl *>(self)->getIntegerType());
1143}1143}
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
1145const ZigClangTypedefNameDecl *ZigClangTypedefType_getDecl(const ZigClangTypedefType *self) {1155const ZigClangTypedefNameDecl *ZigClangTypedefType_getDecl(const ZigClangTypedefType *self) {
1146 auto casted = reinterpret_cast<const clang::TypedefType *>(self);1156 auto casted = reinterpret_cast<const clang::TypedefType *>(self);
1147 const clang::TypedefNameDecl *name_decl = casted->getDecl();1157 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...@@ -599,6 +599,9 @@ ZIG_EXTERN_C struct ZigClangSourceLocation ZigClangEnumDecl_getLocation(const st
599ZIG_EXTERN_C struct ZigClangSourceLocation ZigClangTypedefNameDecl_getLocation(const struct ZigClangTypedefNameDecl *);599ZIG_EXTERN_C struct ZigClangSourceLocation ZigClangTypedefNameDecl_getLocation(const struct ZigClangTypedefNameDecl *);
600ZIG_EXTERN_C struct ZigClangSourceLocation ZigClangDecl_getLocation(const struct ZigClangDecl *);600ZIG_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
602ZIG_EXTERN_C bool ZigClangRecordDecl_isUnion(const struct ZigClangRecordDecl *record_decl);605ZIG_EXTERN_C bool ZigClangRecordDecl_isUnion(const struct ZigClangRecordDecl *record_decl);
603ZIG_EXTERN_C bool ZigClangRecordDecl_isStruct(const struct ZigClangRecordDecl *record_decl);606ZIG_EXTERN_C bool ZigClangRecordDecl_isStruct(const struct ZigClangRecordDecl *record_decl);
604ZIG_EXTERN_C bool ZigClangRecordDecl_isAnonymousStructOrUnion(const struct ZigClangRecordDecl *record_decl);607ZIG_EXTERN_C bool ZigClangRecordDecl_isAnonymousStructOrUnion(const struct ZigClangRecordDecl *record_decl);