Skip to content

Commit

Permalink
translate-c: 4 more functions using C decls
Browse files Browse the repository at this point in the history
See #1964
  • Loading branch information
andrewrk committed Feb 17, 2019
1 parent 2dfa76a commit c3c92ca
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 15 deletions.
36 changes: 24 additions & 12 deletions src/translate_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ struct Context {
bool warnings_on;

CodeGen *codegen;
clang::ASTContext *ctx;
ZigClangASTContext *ctx;

TransScopeRoot *global_scope;
HashMap<Buf *, bool, buf_hash, buf_eql_buf> ptr_params;
Expand Down Expand Up @@ -132,6 +132,16 @@ static ZigClangSourceLocation bitcast(clang::SourceLocation src) {
memcpy(&dest, &src, sizeof(ZigClangSourceLocation));
return dest;
}
static ZigClangQualType bitcast(clang::QualType src) {
ZigClangQualType dest;
memcpy(&dest, &src, sizeof(ZigClangQualType));
return dest;
}
static clang::QualType bitcast(ZigClangQualType src) {
clang::QualType dest;
memcpy(&dest, &src, sizeof(ZigClangQualType));
return dest;
}

ATTRIBUTE_PRINTF(3, 4)
static void emit_warning(Context *c, const clang::SourceLocation &clang_sl, const char *format, ...) {
Expand Down Expand Up @@ -509,7 +519,7 @@ static clang::QualType get_expr_qual_type(Context *c, const clang::Expr *expr) {
const clang::ArrayType *array_type = static_cast<const clang::ArrayType *>(array_qt.getTypePtr());
clang::QualType pointee_qt = array_type->getElementType();
pointee_qt.addConst();
return c->ctx->getPointerType(pointee_qt);
return bitcast(ZigClangASTContext_getPointerType(c->ctx, bitcast(pointee_qt)));
}
}
}
Expand Down Expand Up @@ -1221,7 +1231,7 @@ static AstNode *trans_return_stmt(Context *c, TransScope *scope, const clang::Re

static AstNode *trans_integer_literal(Context *c, const clang::IntegerLiteral *stmt) {
llvm::APSInt result;
if (!stmt->EvaluateAsInt(result, *c->ctx)) {
if (!stmt->EvaluateAsInt(result, *reinterpret_cast<clang::ASTContext *>(c->ctx))) {
emit_warning(c, stmt->getLocStart(), "invalid integer literal");
return nullptr;
}
Expand Down Expand Up @@ -4240,7 +4250,8 @@ static void visit_var_decl(Context *c, const clang::VarDecl *var_decl) {
return;
}

static bool decl_visitor(void *context, const clang::Decl *decl) {
static bool decl_visitor(void *context, const ZigClangDecl *zdecl) {
const clang::Decl *decl = reinterpret_cast<const clang::Decl *>(zdecl);
Context *c = (Context*)context;

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

static void process_preprocessor_entities(Context *c, clang::ASTUnit &unit) {
static void process_preprocessor_entities(Context *c, ZigClangASTUnit *zunit) {
clang::ASTUnit *unit = reinterpret_cast<clang::ASTUnit *>(zunit);
CTokenize ctok = {{0}};

// TODO if we see #undef, delete it from the table

for (clang::PreprocessedEntity *entity : unit.getLocalPreprocessingEntities()) {
for (clang::PreprocessedEntity *entity : unit->getLocalPreprocessingEntities()) {
switch (entity->getKind()) {
case clang::PreprocessedEntity::InvalidKind:
case clang::PreprocessedEntity::InclusionDirectiveKind:
Expand Down Expand Up @@ -4832,7 +4844,7 @@ Error parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const
bool for_serialization = false;
const char *resources_path = buf_ptr(codegen->zig_c_headers_dir);
std::unique_ptr<clang::ASTUnit> err_unit;
std::unique_ptr<clang::ASTUnit> ast_unit(clang::ASTUnit::LoadFromCommandLine(
ZigClangASTUnit *ast_unit = reinterpret_cast<ZigClangASTUnit *>(clang::ASTUnit::LoadFromCommandLine(
&clang_argv.at(0), &clang_argv.last(),
pch_container_ops, diags, resources_path,
only_local_decls, capture_diagnostics, clang::None, true, 0, clang::TU_Complete,
Expand All @@ -4847,7 +4859,7 @@ Error parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const

if (diags->getClient()->getNumErrors() > 0) {
if (ast_unit) {
err_unit = std::move(ast_unit);
err_unit = std::unique_ptr<clang::ASTUnit>(reinterpret_cast<clang::ASTUnit *>(ast_unit));
}

for (clang::ASTUnit::stored_diag_iterator it = err_unit->stored_diag_begin(),
Expand Down Expand Up @@ -4894,14 +4906,14 @@ Error parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const
return ErrorCCompileErrors;
}

c->ctx = &ast_unit->getASTContext();
c->source_manager = reinterpret_cast<ZigClangSourceManager *>(&ast_unit->getSourceManager());
c->ctx = ZigClangASTUnit_getASTContext(ast_unit);
c->source_manager = ZigClangASTUnit_getSourceManager(ast_unit);
c->root = trans_create_node(c, NodeTypeContainerDecl);
c->root->data.container_decl.is_root = true;

ast_unit->visitLocalTopLevelDecls(c, decl_visitor);
ZigClangASTUnit_visitLocalTopLevelDecls(ast_unit, c, decl_visitor);

process_preprocessor_entities(c, *ast_unit);
process_preprocessor_entities(c, ast_unit);

render_macros(c);
render_aliases(c);
Expand Down
35 changes: 33 additions & 2 deletions src/zig_clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,19 +138,29 @@ static_assert((clang::UnaryOperatorKind)ZigClangUO_PreInc == clang::UO_PreInc, "
static_assert((clang::UnaryOperatorKind)ZigClangUO_Real == clang::UO_Real, "");

static_assert(sizeof(ZigClangSourceLocation) == sizeof(clang::SourceLocation), "");

static ZigClangSourceLocation bitcast(clang::SourceLocation src) {
ZigClangSourceLocation dest;
memcpy(&dest, &src, sizeof(ZigClangSourceLocation));
return dest;
}

static clang::SourceLocation bitcast(ZigClangSourceLocation src) {
clang::SourceLocation dest;
memcpy(&dest, &src, sizeof(ZigClangSourceLocation));
return dest;
}

static_assert(sizeof(ZigClangQualType) == sizeof(clang::QualType), "");
static ZigClangQualType bitcast(clang::QualType src) {
ZigClangQualType dest;
memcpy(&dest, &src, sizeof(ZigClangQualType));
return dest;
}
static clang::QualType bitcast(ZigClangQualType src) {
clang::QualType dest;
memcpy(&dest, &src, sizeof(ZigClangQualType));
return dest;
}

ZigClangSourceLocation ZigClangSourceManager_getSpellingLoc(const ZigClangSourceManager *self,
ZigClangSourceLocation Loc)
{
Expand Down Expand Up @@ -181,3 +191,24 @@ const char* ZigClangSourceManager_getCharacterData(const ZigClangSourceManager *
{
return reinterpret_cast<const clang::SourceManager *>(self)->getCharacterData(bitcast(SL));
}

ZigClangQualType ZigClangASTContext_getPointerType(const ZigClangASTContext* self, ZigClangQualType T) {
return bitcast(reinterpret_cast<const clang::ASTContext *>(self)->getPointerType(bitcast(T)));
}

ZigClangASTContext *ZigClangASTUnit_getASTContext(ZigClangASTUnit *self) {
clang::ASTContext *result = &reinterpret_cast<clang::ASTUnit *>(self)->getASTContext();
return reinterpret_cast<ZigClangASTContext *>(result);
}

ZigClangSourceManager *ZigClangASTUnit_getSourceManager(ZigClangASTUnit *self) {
clang::SourceManager *result = &reinterpret_cast<clang::ASTUnit *>(self)->getSourceManager();
return reinterpret_cast<ZigClangSourceManager *>(result);
}

bool ZigClangASTUnit_visitLocalTopLevelDecls(ZigClangASTUnit *self, void *context,
bool (*Fn)(void *context, const ZigClangDecl *decl))
{
return reinterpret_cast<clang::ASTUnit *>(self)->visitLocalTopLevelDecls(context,
reinterpret_cast<bool (*)(void *, const clang::Decl *)>(Fn));
}
12 changes: 11 additions & 1 deletion src/zig_clang.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ struct ZigClangSourceLocation {
unsigned ID;
};

struct ZigClangQualType {
void *ptr;
};

struct ZigClangAPValue;
struct ZigClangASTContext;
struct ZigClangASTUnit;
Expand Down Expand Up @@ -71,7 +75,6 @@ struct ZigClangParenType;
struct ZigClangParmVarDecl;
struct ZigClangPointerType;
struct ZigClangPreprocessedEntity;
struct ZigClangQualType;
struct ZigClangRecordDecl;
struct ZigClangRecordType;
struct ZigClangReturnStmt;
Expand Down Expand Up @@ -246,4 +249,11 @@ ZIG_EXTERN_C unsigned ZigClangSourceManager_getSpellingColumnNumber(const ZigCla
ZigClangSourceLocation Loc);
ZIG_EXTERN_C const char* ZigClangSourceManager_getCharacterData(const ZigClangSourceManager *,
ZigClangSourceLocation SL);

ZIG_EXTERN_C ZigClangQualType ZigClangASTContext_getPointerType(const ZigClangASTContext*, ZigClangQualType T);

ZIG_EXTERN_C ZigClangASTContext *ZigClangASTUnit_getASTContext(ZigClangASTUnit *);
ZIG_EXTERN_C ZigClangSourceManager *ZigClangASTUnit_getSourceManager(ZigClangASTUnit *);
ZIG_EXTERN_C bool ZigClangASTUnit_visitLocalTopLevelDecls(ZigClangASTUnit *, void *context,
bool (*Fn)(void *context, const ZigClangDecl *decl));
#endif

0 comments on commit c3c92ca

Please sign in to comment.