Skip to content

Commit c3c92ca

Browse files
committed
translate-c: 4 more functions using C decls
See #1964
1 parent 2dfa76a commit c3c92ca

File tree

3 files changed

+68
-15
lines changed

3 files changed

+68
-15
lines changed

src/translate_c.cpp

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ struct Context {
9090
bool warnings_on;
9191

9292
CodeGen *codegen;
93-
clang::ASTContext *ctx;
93+
ZigClangASTContext *ctx;
9494

9595
TransScopeRoot *global_scope;
9696
HashMap<Buf *, bool, buf_hash, buf_eql_buf> ptr_params;
@@ -132,6 +132,16 @@ static ZigClangSourceLocation bitcast(clang::SourceLocation src) {
132132
memcpy(&dest, &src, sizeof(ZigClangSourceLocation));
133133
return dest;
134134
}
135+
static ZigClangQualType bitcast(clang::QualType src) {
136+
ZigClangQualType dest;
137+
memcpy(&dest, &src, sizeof(ZigClangQualType));
138+
return dest;
139+
}
140+
static clang::QualType bitcast(ZigClangQualType src) {
141+
clang::QualType dest;
142+
memcpy(&dest, &src, sizeof(ZigClangQualType));
143+
return dest;
144+
}
135145

136146
ATTRIBUTE_PRINTF(3, 4)
137147
static 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) {
509519
const clang::ArrayType *array_type = static_cast<const clang::ArrayType *>(array_qt.getTypePtr());
510520
clang::QualType pointee_qt = array_type->getElementType();
511521
pointee_qt.addConst();
512-
return c->ctx->getPointerType(pointee_qt);
522+
return bitcast(ZigClangASTContext_getPointerType(c->ctx, bitcast(pointee_qt)));
513523
}
514524
}
515525
}
@@ -1221,7 +1231,7 @@ static AstNode *trans_return_stmt(Context *c, TransScope *scope, const clang::Re
12211231

12221232
static AstNode *trans_integer_literal(Context *c, const clang::IntegerLiteral *stmt) {
12231233
llvm::APSInt result;
1224-
if (!stmt->EvaluateAsInt(result, *c->ctx)) {
1234+
if (!stmt->EvaluateAsInt(result, *reinterpret_cast<clang::ASTContext *>(c->ctx))) {
12251235
emit_warning(c, stmt->getLocStart(), "invalid integer literal");
12261236
return nullptr;
12271237
}
@@ -4240,7 +4250,8 @@ static void visit_var_decl(Context *c, const clang::VarDecl *var_decl) {
42404250
return;
42414251
}
42424252

4243-
static bool decl_visitor(void *context, const clang::Decl *decl) {
4253+
static bool decl_visitor(void *context, const ZigClangDecl *zdecl) {
4254+
const clang::Decl *decl = reinterpret_cast<const clang::Decl *>(zdecl);
42444255
Context *c = (Context*)context;
42454256

42464257
switch (decl->getKind()) {
@@ -4678,12 +4689,13 @@ static void process_macro(Context *c, CTokenize *ctok, Buf *name, const char *ch
46784689
c->macro_table.put(name, result_node);
46794690
}
46804691

4681-
static void process_preprocessor_entities(Context *c, clang::ASTUnit &unit) {
4692+
static void process_preprocessor_entities(Context *c, ZigClangASTUnit *zunit) {
4693+
clang::ASTUnit *unit = reinterpret_cast<clang::ASTUnit *>(zunit);
46824694
CTokenize ctok = {{0}};
46834695

46844696
// 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()) {
46874699
switch (entity->getKind()) {
46884700
case clang::PreprocessedEntity::InvalidKind:
46894701
case clang::PreprocessedEntity::InclusionDirectiveKind:
@@ -4832,7 +4844,7 @@ Error parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const
48324844
bool for_serialization = false;
48334845
const char *resources_path = buf_ptr(codegen->zig_c_headers_dir);
48344846
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(
48364848
&clang_argv.at(0), &clang_argv.last(),
48374849
pch_container_ops, diags, resources_path,
48384850
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
48474859

48484860
if (diags->getClient()->getNumErrors() > 0) {
48494861
if (ast_unit) {
4850-
err_unit = std::move(ast_unit);
4862+
err_unit = std::unique_ptr<clang::ASTUnit>(reinterpret_cast<clang::ASTUnit *>(ast_unit));
48514863
}
48524864

48534865
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
48944906
return ErrorCCompileErrors;
48954907
}
48964908

4897-
c->ctx = &ast_unit->getASTContext();
4898-
c->source_manager = reinterpret_cast<ZigClangSourceManager *>(&ast_unit->getSourceManager());
4909+
c->ctx = ZigClangASTUnit_getASTContext(ast_unit);
4910+
c->source_manager = ZigClangASTUnit_getSourceManager(ast_unit);
48994911
c->root = trans_create_node(c, NodeTypeContainerDecl);
49004912
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

49064918
render_macros(c);
49074919
render_aliases(c);

src/zig_clang.cpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,19 +138,29 @@ static_assert((clang::UnaryOperatorKind)ZigClangUO_PreInc == clang::UO_PreInc, "
138138
static_assert((clang::UnaryOperatorKind)ZigClangUO_Real == clang::UO_Real, "");
139139

140140
static_assert(sizeof(ZigClangSourceLocation) == sizeof(clang::SourceLocation), "");
141-
142141
static ZigClangSourceLocation bitcast(clang::SourceLocation src) {
143142
ZigClangSourceLocation dest;
144143
memcpy(&dest, &src, sizeof(ZigClangSourceLocation));
145144
return dest;
146145
}
147-
148146
static clang::SourceLocation bitcast(ZigClangSourceLocation src) {
149147
clang::SourceLocation dest;
150148
memcpy(&dest, &src, sizeof(ZigClangSourceLocation));
151149
return dest;
152150
}
153151

152+
static_assert(sizeof(ZigClangQualType) == sizeof(clang::QualType), "");
153+
static ZigClangQualType bitcast(clang::QualType src) {
154+
ZigClangQualType dest;
155+
memcpy(&dest, &src, sizeof(ZigClangQualType));
156+
return dest;
157+
}
158+
static clang::QualType bitcast(ZigClangQualType src) {
159+
clang::QualType dest;
160+
memcpy(&dest, &src, sizeof(ZigClangQualType));
161+
return dest;
162+
}
163+
154164
ZigClangSourceLocation ZigClangSourceManager_getSpellingLoc(const ZigClangSourceManager *self,
155165
ZigClangSourceLocation Loc)
156166
{
@@ -181,3 +191,24 @@ const char* ZigClangSourceManager_getCharacterData(const ZigClangSourceManager *
181191
{
182192
return reinterpret_cast<const clang::SourceManager *>(self)->getCharacterData(bitcast(SL));
183193
}
194+
195+
ZigClangQualType ZigClangASTContext_getPointerType(const ZigClangASTContext* self, ZigClangQualType T) {
196+
return bitcast(reinterpret_cast<const clang::ASTContext *>(self)->getPointerType(bitcast(T)));
197+
}
198+
199+
ZigClangASTContext *ZigClangASTUnit_getASTContext(ZigClangASTUnit *self) {
200+
clang::ASTContext *result = &reinterpret_cast<clang::ASTUnit *>(self)->getASTContext();
201+
return reinterpret_cast<ZigClangASTContext *>(result);
202+
}
203+
204+
ZigClangSourceManager *ZigClangASTUnit_getSourceManager(ZigClangASTUnit *self) {
205+
clang::SourceManager *result = &reinterpret_cast<clang::ASTUnit *>(self)->getSourceManager();
206+
return reinterpret_cast<ZigClangSourceManager *>(result);
207+
}
208+
209+
bool 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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ struct ZigClangSourceLocation {
2121
unsigned ID;
2222
};
2323

24+
struct ZigClangQualType {
25+
void *ptr;
26+
};
27+
2428
struct ZigClangAPValue;
2529
struct ZigClangASTContext;
2630
struct ZigClangASTUnit;
@@ -71,7 +75,6 @@ struct ZigClangParenType;
7175
struct ZigClangParmVarDecl;
7276
struct ZigClangPointerType;
7377
struct ZigClangPreprocessedEntity;
74-
struct ZigClangQualType;
7578
struct ZigClangRecordDecl;
7679
struct ZigClangRecordType;
7780
struct ZigClangReturnStmt;
@@ -246,4 +249,11 @@ ZIG_EXTERN_C unsigned ZigClangSourceManager_getSpellingColumnNumber(const ZigCla
246249
ZigClangSourceLocation Loc);
247250
ZIG_EXTERN_C const char* ZigClangSourceManager_getCharacterData(const ZigClangSourceManager *,
248251
ZigClangSourceLocation SL);
252+
253+
ZIG_EXTERN_C ZigClangQualType ZigClangASTContext_getPointerType(const ZigClangASTContext*, ZigClangQualType T);
254+
255+
ZIG_EXTERN_C ZigClangASTContext *ZigClangASTUnit_getASTContext(ZigClangASTUnit *);
256+
ZIG_EXTERN_C ZigClangSourceManager *ZigClangASTUnit_getSourceManager(ZigClangASTUnit *);
257+
ZIG_EXTERN_C bool ZigClangASTUnit_visitLocalTopLevelDecls(ZigClangASTUnit *, void *context,
258+
bool (*Fn)(void *context, const ZigClangDecl *decl));
249259
#endif

0 commit comments

Comments
 (0)