diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2019-05-10 16:03:54 -0400 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2019-05-10 16:03:54 -0400 |
| commit | a6f7a9ce2b008ff66ab32c8162d64dd77ffe952f (patch) | |
| tree | 7e0f1e80cabf9e2869949035aa5ff95e06749d4c /src | |
| parent | d065f297ab6de5ca35636d169195ce5da6235786 (diff) | |
| download | zig-a6f7a9ce2b008ff66ab32c8162d64dd77ffe952f.tar.gz zig-a6f7a9ce2b008ff66ab32c8162d64dd77ffe952f.zip | |
translate-c: we have our first test of self-hosted
See #1964
Diffstat (limited to 'src')
| -rw-r--r-- | src/translate_c.cpp | 18 | ||||
| -rw-r--r-- | src/zig_clang.cpp | 43 | ||||
| -rw-r--r-- | src/zig_clang.h | 17 |
3 files changed, 68 insertions, 10 deletions
diff --git a/src/translate_c.cpp b/src/translate_c.cpp index 64bf995cdd..71c0376130 100644 --- a/src/translate_c.cpp +++ b/src/translate_c.cpp @@ -4038,15 +4038,15 @@ static void visit_fn_decl(Context *c, const ZigClangFunctionDecl *fn_decl) { } proto_node->data.fn_proto.name = fn_name; - proto_node->data.fn_proto.is_extern = !((const clang::FunctionDecl*)fn_decl)->hasBody(); + proto_node->data.fn_proto.is_extern = !ZigClangFunctionDecl_hasBody(fn_decl); - clang::StorageClass sc = ((const clang::FunctionDecl*)fn_decl)->getStorageClass(); - if (sc == clang::SC_None) { + ZigClangStorageClass sc = ZigClangFunctionDecl_getStorageClass(fn_decl); + if (sc == ZigClangStorageClass_None) { proto_node->data.fn_proto.visib_mod = c->visib_mod; - proto_node->data.fn_proto.is_export = ((const clang::FunctionDecl*)fn_decl)->hasBody() ? c->want_export : false; - } else if (sc == clang::SC_Extern || sc == clang::SC_Static) { + proto_node->data.fn_proto.is_export = ZigClangFunctionDecl_hasBody(fn_decl) ? c->want_export : false; + } else if (sc == ZigClangStorageClass_Extern || sc == ZigClangStorageClass_Static) { proto_node->data.fn_proto.visib_mod = c->visib_mod; - } else if (sc == clang::SC_PrivateExtern) { + } else if (sc == ZigClangStorageClass_PrivateExtern) { emit_warning(c, ZigClangFunctionDecl_getLocation(fn_decl), "unsupported storage class: private extern"); return; } else { @@ -4058,7 +4058,7 @@ static void visit_fn_decl(Context *c, const ZigClangFunctionDecl *fn_decl) { for (size_t i = 0; i < proto_node->data.fn_proto.params.length; i += 1) { AstNode *param_node = proto_node->data.fn_proto.params.at(i); - const clang::ParmVarDecl *param = ((const clang::FunctionDecl*)fn_decl)->getParamDecl(i); + const ZigClangParmVarDecl *param = ZigClangFunctionDecl_getParamDecl(fn_decl, i); const char *name = ZigClangDecl_getName_bytes_begin((const ZigClangDecl *)param); Buf *proto_param_name; @@ -4077,7 +4077,7 @@ static void visit_fn_decl(Context *c, const ZigClangFunctionDecl *fn_decl) { param_node->data.param_decl.name = scope_var->zig_name; } - if (!((const clang::FunctionDecl*)fn_decl)->hasBody()) { + if (!ZigClangFunctionDecl_hasBody(fn_decl)) { // just a prototype add_top_level_decl(c, proto_node->data.fn_proto.name, proto_node); return; @@ -4085,7 +4085,7 @@ static void visit_fn_decl(Context *c, const ZigClangFunctionDecl *fn_decl) { // actual function definition with body c->ptr_params.clear(); - const ZigClangStmt *body = bitcast(((const clang::FunctionDecl*)fn_decl)->getBody()); + const ZigClangStmt *body = ZigClangFunctionDecl_getBody(fn_decl); AstNode *actual_body_node; TransScope *result_scope = trans_stmt(c, scope, body, &actual_body_node); if (result_scope == nullptr) { diff --git a/src/zig_clang.cpp b/src/zig_clang.cpp index b1457fd817..25e309a63e 100644 --- a/src/zig_clang.cpp +++ b/src/zig_clang.cpp @@ -1236,6 +1236,25 @@ static_assert((clang::CallingConv)ZigClangCallingConv_PreserveMost == clang::CC_ static_assert((clang::CallingConv)ZigClangCallingConv_PreserveAll == clang::CC_PreserveAll, ""); static_assert((clang::CallingConv)ZigClangCallingConv_AArch64VectorCall == clang::CC_AArch64VectorCall, ""); +void ZigClang_detect_enum_StorageClass(clang::StorageClass x) { + switch (x) { + case clang::SC_None: + case clang::SC_Extern: + case clang::SC_Static: + case clang::SC_PrivateExtern: + case clang::SC_Auto: + case clang::SC_Register: + break; + } +} + +static_assert((clang::StorageClass)ZigClangStorageClass_None == clang::SC_None, ""); +static_assert((clang::StorageClass)ZigClangStorageClass_Extern == clang::SC_Extern, ""); +static_assert((clang::StorageClass)ZigClangStorageClass_Static == clang::SC_Static, ""); +static_assert((clang::StorageClass)ZigClangStorageClass_PrivateExtern == clang::SC_PrivateExtern, ""); +static_assert((clang::StorageClass)ZigClangStorageClass_Auto == clang::SC_Auto, ""); +static_assert((clang::StorageClass)ZigClangStorageClass_Register == clang::SC_Register, ""); + static_assert(sizeof(ZigClangSourceLocation) == sizeof(clang::SourceLocation), ""); static ZigClangSourceLocation bitcast(clang::SourceLocation src) { @@ -1434,6 +1453,30 @@ struct ZigClangSourceLocation ZigClangFunctionDecl_getLocation(const struct ZigC return bitcast(casted->getLocation()); } +bool ZigClangFunctionDecl_hasBody(const struct ZigClangFunctionDecl *self) { + auto casted = reinterpret_cast<const clang::FunctionDecl *>(self); + return casted->hasBody(); +} + +enum ZigClangStorageClass ZigClangFunctionDecl_getStorageClass(const struct ZigClangFunctionDecl *self) { + auto casted = reinterpret_cast<const clang::FunctionDecl *>(self); + return (ZigClangStorageClass)casted->getStorageClass(); +} + +const struct ZigClangParmVarDecl *ZigClangFunctionDecl_getParamDecl(const struct ZigClangFunctionDecl *self, + unsigned i) +{ + auto casted = reinterpret_cast<const clang::FunctionDecl *>(self); + const clang::ParmVarDecl *parm_var_decl = casted->getParamDecl(i); + return reinterpret_cast<const ZigClangParmVarDecl *>(parm_var_decl); +} + +const struct ZigClangStmt *ZigClangFunctionDecl_getBody(const struct ZigClangFunctionDecl *self) { + auto casted = reinterpret_cast<const clang::FunctionDecl *>(self); + const clang::Stmt *stmt = casted->getBody(); + return reinterpret_cast<const ZigClangStmt *>(stmt); +} + const ZigClangTypedefNameDecl *ZigClangTypedefType_getDecl(const ZigClangTypedefType *self) { auto casted = reinterpret_cast<const clang::TypedefType *>(self); const clang::TypedefNameDecl *name_decl = casted->getDecl(); diff --git a/src/zig_clang.h b/src/zig_clang.h index 6a67ec85c1..8e0d2f9f98 100644 --- a/src/zig_clang.h +++ b/src/zig_clang.h @@ -88,7 +88,6 @@ struct ZigClangSkipFunctionBodiesScope; struct ZigClangSourceManager; struct ZigClangSourceRange; struct ZigClangStmt; -struct ZigClangStorageClass; struct ZigClangStringLiteral; struct ZigClangStringRef; struct ZigClangSwitchStmt; @@ -700,6 +699,18 @@ enum ZigClangCallingConv { ZigClangCallingConv_AArch64VectorCall, // __attribute__((aarch64_vector_pcs)) }; +enum ZigClangStorageClass { + // These are legal on both functions and variables. + ZigClangStorageClass_None, + ZigClangStorageClass_Extern, + ZigClangStorageClass_Static, + ZigClangStorageClass_PrivateExtern, + + // These are only legal on variables. + ZigClangStorageClass_Auto, + ZigClangStorageClass_Register, +}; + ZIG_EXTERN_C struct ZigClangSourceLocation ZigClangSourceManager_getSpellingLoc(const struct ZigClangSourceManager *, struct ZigClangSourceLocation Loc); ZIG_EXTERN_C const char *ZigClangSourceManager_getFilename(const struct ZigClangSourceManager *, @@ -742,6 +753,10 @@ ZIG_EXTERN_C struct ZigClangSourceLocation ZigClangDecl_getLocation(const struct ZIG_EXTERN_C struct ZigClangQualType ZigClangFunctionDecl_getType(const struct ZigClangFunctionDecl *); ZIG_EXTERN_C struct ZigClangSourceLocation ZigClangFunctionDecl_getLocation(const struct ZigClangFunctionDecl *); +ZIG_EXTERN_C bool ZigClangFunctionDecl_hasBody(const struct ZigClangFunctionDecl *); +ZIG_EXTERN_C enum ZigClangStorageClass ZigClangFunctionDecl_getStorageClass(const struct ZigClangFunctionDecl *); +ZIG_EXTERN_C const struct ZigClangParmVarDecl *ZigClangFunctionDecl_getParamDecl(const struct ZigClangFunctionDecl *, unsigned i); +ZIG_EXTERN_C const struct ZigClangStmt *ZigClangFunctionDecl_getBody(const struct ZigClangFunctionDecl *); ZIG_EXTERN_C bool ZigClangRecordDecl_isUnion(const struct ZigClangRecordDecl *record_decl); ZIG_EXTERN_C bool ZigClangRecordDecl_isStruct(const struct ZigClangRecordDecl *record_decl); |
