authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-16 19:48:39-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-16 19:48:39-05:00
logc3c92ca8b1ada4faed14a9770ab7ed6536edaa15
tree0343f88be76e3f6741588f8d4924f6f7c1b9d5ad
parent2dfa76a1a754d2537b9cc6faf7b98461de0b1429
signaturelock-open Commit is signed but in an unrecognized format.

translate-c: 4 more functions using C decls

See #1964

3 files changed, 68 insertions(+), 15 deletions(-)

src/translate_c.cpp+24-12
...@@ -90,7 +90,7 @@ struct Context {...@@ -90,7 +90,7 @@ struct Context {
90 bool warnings_on;90 bool warnings_on;
9191
92 CodeGen *codegen;92 CodeGen *codegen;
93 clang::ASTContext *ctx;93 ZigClangASTContext *ctx;
9494
95 TransScopeRoot *global_scope;95 TransScopeRoot *global_scope;
96 HashMap<Buf *, bool, buf_hash, buf_eql_buf> ptr_params;96 HashMap<Buf *, bool, buf_hash, buf_eql_buf> ptr_params;
...@@ -132,6 +132,16 @@ static ZigClangSourceLocation bitcast(clang::SourceLocation src) {...@@ -132,6 +132,16 @@ static ZigClangSourceLocation bitcast(clang::SourceLocation src) {
132 memcpy(&dest, &src, sizeof(ZigClangSourceLocation));132 memcpy(&dest, &src, sizeof(ZigClangSourceLocation));
133 return dest;133 return dest;
134}134}
135static ZigClangQualType bitcast(clang::QualType src) {
136 ZigClangQualType dest;
137 memcpy(&dest, &src, sizeof(ZigClangQualType));
138 return dest;
139}
140static clang::QualType bitcast(ZigClangQualType src) {
141 clang::QualType dest;
142 memcpy(&dest, &src, sizeof(ZigClangQualType));
143 return dest;
144}
135145
136ATTRIBUTE_PRINTF(3, 4)146ATTRIBUTE_PRINTF(3, 4)
137static void emit_warning(Context *c, const clang::SourceLocation &clang_sl, const char *format, ...) {147static void emit_warning(Context *c, const clang::SourceLocation &clang_sl, const char *format, ...) {
...@@ -509,7 +519,7 @@ static clang::QualType get_expr_qual_type(Context *c, const clang::Expr *expr) {...@@ -509,7 +519,7 @@ static clang::QualType get_expr_qual_type(Context *c, const clang::Expr *expr) {
509 const clang::ArrayType *array_type = static_cast<const clang::ArrayType *>(array_qt.getTypePtr());519 const clang::ArrayType *array_type = static_cast<const clang::ArrayType *>(array_qt.getTypePtr());
510 clang::QualType pointee_qt = array_type->getElementType();520 clang::QualType pointee_qt = array_type->getElementType();
511 pointee_qt.addConst();521 pointee_qt.addConst();
512 return c->ctx->getPointerType(pointee_qt);522 return bitcast(ZigClangASTContext_getPointerType(c->ctx, bitcast(pointee_qt)));
513 }523 }
514 }524 }
515 }525 }
...@@ -1221,7 +1231,7 @@ static AstNode *trans_return_stmt(Context *c, TransScope *scope, const clang::Re...@@ -1221,7 +1231,7 @@ static AstNode *trans_return_stmt(Context *c, TransScope *scope, const clang::Re
12211231
1222static AstNode *trans_integer_literal(Context *c, const clang::IntegerLiteral *stmt) {1232static AstNode *trans_integer_literal(Context *c, const clang::IntegerLiteral *stmt) {
1223 llvm::APSInt result;1233 llvm::APSInt result;
1224 if (!stmt->EvaluateAsInt(result, *c->ctx)) {1234 if (!stmt->EvaluateAsInt(result, *reinterpret_cast<clang::ASTContext *>(c->ctx))) {
1225 emit_warning(c, stmt->getLocStart(), "invalid integer literal");1235 emit_warning(c, stmt->getLocStart(), "invalid integer literal");
1226 return nullptr;1236 return nullptr;
1227 }1237 }
...@@ -4240,7 +4250,8 @@ static void visit_var_decl(Context *c, const clang::VarDecl *var_decl) {...@@ -4240,7 +4250,8 @@ static void visit_var_decl(Context *c, const clang::VarDecl *var_decl) {
4240 return;4250 return;
4241}4251}
42424252
4243static bool decl_visitor(void *context, const clang::Decl *decl) {4253static bool decl_visitor(void *context, const ZigClangDecl *zdecl) {
4254 const clang::Decl *decl = reinterpret_cast<const clang::Decl *>(zdecl);
4244 Context *c = (Context*)context;4255 Context *c = (Context*)context;
42454256
4246 switch (decl->getKind()) {4257 switch (decl->getKind()) {
...@@ -4678,12 +4689,13 @@ static void process_macro(Context *c, CTokenize *ctok, Buf *name, const char *ch...@@ -4678,12 +4689,13 @@ static void process_macro(Context *c, CTokenize *ctok, Buf *name, const char *ch
4678 c->macro_table.put(name, result_node);4689 c->macro_table.put(name, result_node);
4679}4690}
46804691
4681static void process_preprocessor_entities(Context *c, clang::ASTUnit &unit) {4692static void process_preprocessor_entities(Context *c, ZigClangASTUnit *zunit) {
4693 clang::ASTUnit *unit = reinterpret_cast<clang::ASTUnit *>(zunit);
4682 CTokenize ctok = {{0}};4694 CTokenize ctok = {{0}};
46834695
4684 // TODO if we see #undef, delete it from the table4696 // TODO if we see #undef, delete it from the table
46854697
4686 for (clang::PreprocessedEntity *entity : unit.getLocalPreprocessingEntities()) {4698 for (clang::PreprocessedEntity *entity : unit->getLocalPreprocessingEntities()) {
4687 switch (entity->getKind()) {4699 switch (entity->getKind()) {
4688 case clang::PreprocessedEntity::InvalidKind:4700 case clang::PreprocessedEntity::InvalidKind:
4689 case clang::PreprocessedEntity::InclusionDirectiveKind:4701 case clang::PreprocessedEntity::InclusionDirectiveKind:
...@@ -4832,7 +4844,7 @@ Error parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const...@@ -4832,7 +4844,7 @@ Error parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const
4832 bool for_serialization = false;4844 bool for_serialization = false;
4833 const char *resources_path = buf_ptr(codegen->zig_c_headers_dir);4845 const char *resources_path = buf_ptr(codegen->zig_c_headers_dir);
4834 std::unique_ptr<clang::ASTUnit> err_unit;4846 std::unique_ptr<clang::ASTUnit> err_unit;
4835 std::unique_ptr<clang::ASTUnit> ast_unit(clang::ASTUnit::LoadFromCommandLine(4847 ZigClangASTUnit *ast_unit = reinterpret_cast<ZigClangASTUnit *>(clang::ASTUnit::LoadFromCommandLine(
4836 &clang_argv.at(0), &clang_argv.last(),4848 &clang_argv.at(0), &clang_argv.last(),
4837 pch_container_ops, diags, resources_path,4849 pch_container_ops, diags, resources_path,
4838 only_local_decls, capture_diagnostics, clang::None, true, 0, clang::TU_Complete,4850 only_local_decls, capture_diagnostics, clang::None, true, 0, clang::TU_Complete,
...@@ -4847,7 +4859,7 @@ Error parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const...@@ -4847,7 +4859,7 @@ Error parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const
48474859
4848 if (diags->getClient()->getNumErrors() > 0) {4860 if (diags->getClient()->getNumErrors() > 0) {
4849 if (ast_unit) {4861 if (ast_unit) {
4850 err_unit = std::move(ast_unit);4862 err_unit = std::unique_ptr<clang::ASTUnit>(reinterpret_cast<clang::ASTUnit *>(ast_unit));
4851 }4863 }
48524864
4853 for (clang::ASTUnit::stored_diag_iterator it = err_unit->stored_diag_begin(),4865 for (clang::ASTUnit::stored_diag_iterator it = err_unit->stored_diag_begin(),
...@@ -4894,14 +4906,14 @@ Error parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const...@@ -4894,14 +4906,14 @@ Error parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const
4894 return ErrorCCompileErrors;4906 return ErrorCCompileErrors;
4895 }4907 }
48964908
4897 c->ctx = &ast_unit->getASTContext();4909 c->ctx = ZigClangASTUnit_getASTContext(ast_unit);
4898 c->source_manager = reinterpret_cast<ZigClangSourceManager *>(&ast_unit->getSourceManager());4910 c->source_manager = ZigClangASTUnit_getSourceManager(ast_unit);
4899 c->root = trans_create_node(c, NodeTypeContainerDecl);4911 c->root = trans_create_node(c, NodeTypeContainerDecl);
4900 c->root->data.container_decl.is_root = true;4912 c->root->data.container_decl.is_root = true;
49014913
4902 ast_unit->visitLocalTopLevelDecls(c, decl_visitor);4914 ZigClangASTUnit_visitLocalTopLevelDecls(ast_unit, c, decl_visitor);
49034915
4904 process_preprocessor_entities(c, *ast_unit);4916 process_preprocessor_entities(c, ast_unit);
49054917
4906 render_macros(c);4918 render_macros(c);
4907 render_aliases(c);4919 render_aliases(c);
src/zig_clang.cpp+33-2
...@@ -138,19 +138,29 @@ static_assert((clang::UnaryOperatorKind)ZigClangUO_PreInc == clang::UO_PreInc, "...@@ -138,19 +138,29 @@ static_assert((clang::UnaryOperatorKind)ZigClangUO_PreInc == clang::UO_PreInc, "
138static_assert((clang::UnaryOperatorKind)ZigClangUO_Real == clang::UO_Real, "");138static_assert((clang::UnaryOperatorKind)ZigClangUO_Real == clang::UO_Real, "");
139139
140static_assert(sizeof(ZigClangSourceLocation) == sizeof(clang::SourceLocation), "");140static_assert(sizeof(ZigClangSourceLocation) == sizeof(clang::SourceLocation), "");
141
142static ZigClangSourceLocation bitcast(clang::SourceLocation src) {141static ZigClangSourceLocation bitcast(clang::SourceLocation src) {
143 ZigClangSourceLocation dest;142 ZigClangSourceLocation dest;
144 memcpy(&dest, &src, sizeof(ZigClangSourceLocation));143 memcpy(&dest, &src, sizeof(ZigClangSourceLocation));
145 return dest;144 return dest;
146}145}
147
148static clang::SourceLocation bitcast(ZigClangSourceLocation src) {146static clang::SourceLocation bitcast(ZigClangSourceLocation src) {
149 clang::SourceLocation dest;147 clang::SourceLocation dest;
150 memcpy(&dest, &src, sizeof(ZigClangSourceLocation));148 memcpy(&dest, &src, sizeof(ZigClangSourceLocation));
151 return dest;149 return dest;
152}150}
153151
152static_assert(sizeof(ZigClangQualType) == sizeof(clang::QualType), "");
153static ZigClangQualType bitcast(clang::QualType src) {
154 ZigClangQualType dest;
155 memcpy(&dest, &src, sizeof(ZigClangQualType));
156 return dest;
157}
158static clang::QualType bitcast(ZigClangQualType src) {
159 clang::QualType dest;
160 memcpy(&dest, &src, sizeof(ZigClangQualType));
161 return dest;
162}
163
154ZigClangSourceLocation ZigClangSourceManager_getSpellingLoc(const ZigClangSourceManager *self,164ZigClangSourceLocation ZigClangSourceManager_getSpellingLoc(const ZigClangSourceManager *self,
155 ZigClangSourceLocation Loc)165 ZigClangSourceLocation Loc)
156{166{
...@@ -181,3 +191,24 @@ const char* ZigClangSourceManager_getCharacterData(const ZigClangSourceManager *...@@ -181,3 +191,24 @@ const char* ZigClangSourceManager_getCharacterData(const ZigClangSourceManager *
181{191{
182 return reinterpret_cast<const clang::SourceManager *>(self)->getCharacterData(bitcast(SL));192 return reinterpret_cast<const clang::SourceManager *>(self)->getCharacterData(bitcast(SL));
183}193}
194
195ZigClangQualType ZigClangASTContext_getPointerType(const ZigClangASTContext* self, ZigClangQualType T) {
196 return bitcast(reinterpret_cast<const clang::ASTContext *>(self)->getPointerType(bitcast(T)));
197}
198
199ZigClangASTContext *ZigClangASTUnit_getASTContext(ZigClangASTUnit *self) {
200 clang::ASTContext *result = &reinterpret_cast<clang::ASTUnit *>(self)->getASTContext();
201 return reinterpret_cast<ZigClangASTContext *>(result);
202}
203
204ZigClangSourceManager *ZigClangASTUnit_getSourceManager(ZigClangASTUnit *self) {
205 clang::SourceManager *result = &reinterpret_cast<clang::ASTUnit *>(self)->getSourceManager();
206 return reinterpret_cast<ZigClangSourceManager *>(result);
207}
208
209bool ZigClangASTUnit_visitLocalTopLevelDecls(ZigClangASTUnit *self, void *context,
210 bool (*Fn)(void *context, const ZigClangDecl *decl))
211{
212 return reinterpret_cast<clang::ASTUnit *>(self)->visitLocalTopLevelDecls(context,
213 reinterpret_cast<bool (*)(void *, const clang::Decl *)>(Fn));
214}
src/zig_clang.h+11-1
...@@ -21,6 +21,10 @@ struct ZigClangSourceLocation {...@@ -21,6 +21,10 @@ struct ZigClangSourceLocation {
21 unsigned ID;21 unsigned ID;
22};22};
2323
24struct ZigClangQualType {
25 void *ptr;
26};
27
24struct ZigClangAPValue;28struct ZigClangAPValue;
25struct ZigClangASTContext;29struct ZigClangASTContext;
26struct ZigClangASTUnit;30struct ZigClangASTUnit;
...@@ -71,7 +75,6 @@ struct ZigClangParenType;...@@ -71,7 +75,6 @@ struct ZigClangParenType;
71struct ZigClangParmVarDecl;75struct ZigClangParmVarDecl;
72struct ZigClangPointerType;76struct ZigClangPointerType;
73struct ZigClangPreprocessedEntity;77struct ZigClangPreprocessedEntity;
74struct ZigClangQualType;
75struct ZigClangRecordDecl;78struct ZigClangRecordDecl;
76struct ZigClangRecordType;79struct ZigClangRecordType;
77struct ZigClangReturnStmt;80struct ZigClangReturnStmt;
...@@ -246,4 +249,11 @@ ZIG_EXTERN_C unsigned ZigClangSourceManager_getSpellingColumnNumber(const ZigCla...@@ -246,4 +249,11 @@ ZIG_EXTERN_C unsigned ZigClangSourceManager_getSpellingColumnNumber(const ZigCla
246 ZigClangSourceLocation Loc);249 ZigClangSourceLocation Loc);
247ZIG_EXTERN_C const char* ZigClangSourceManager_getCharacterData(const ZigClangSourceManager *,250ZIG_EXTERN_C const char* ZigClangSourceManager_getCharacterData(const ZigClangSourceManager *,
248 ZigClangSourceLocation SL);251 ZigClangSourceLocation SL);
252
253ZIG_EXTERN_C ZigClangQualType ZigClangASTContext_getPointerType(const ZigClangASTContext*, ZigClangQualType T);
254
255ZIG_EXTERN_C ZigClangASTContext *ZigClangASTUnit_getASTContext(ZigClangASTUnit *);
256ZIG_EXTERN_C ZigClangSourceManager *ZigClangASTUnit_getSourceManager(ZigClangASTUnit *);
257ZIG_EXTERN_C bool ZigClangASTUnit_visitLocalTopLevelDecls(ZigClangASTUnit *, void *context,
258 bool (*Fn)(void *context, const ZigClangDecl *decl));
249#endif259#endif