Skip to content

Commit

Permalink
translate-c: move some code to the C API
Browse files Browse the repository at this point in the history
See #1964
  • Loading branch information
andrewrk committed Apr 11, 2019
1 parent 4172fe9 commit b960f1d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 24 deletions.
46 changes: 24 additions & 22 deletions src/translate_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ static TransScopeSwitch *trans_scope_switch_create(Context *c, TransScope *paren
static TransScopeBlock *trans_scope_block_find(TransScope *scope);

static AstNode *resolve_record_decl(Context *c, const ZigClangRecordDecl *record_decl);
static AstNode *resolve_enum_decl(Context *c, const clang::EnumDecl *enum_decl);
static AstNode *resolve_enum_decl(Context *c, const ZigClangEnumDecl *enum_decl);
static AstNode *resolve_typedef_decl(Context *c, const clang::TypedefNameDecl *typedef_decl);

static int trans_stmt_extra(Context *c, TransScope *scope, const clang::Stmt *stmt,
Expand Down Expand Up @@ -1128,8 +1128,8 @@ static AstNode *trans_type(Context *c, const clang::Type *ty, ZigClangSourceLoca
}
case clang::Type::Enum:
{
const clang::EnumType *enum_ty = static_cast<const clang::EnumType*>(ty);
return resolve_enum_decl(c, enum_ty->getDecl());
const ZigClangEnumType *enum_ty = reinterpret_cast<const ZigClangEnumType*>(ty);
return resolve_enum_decl(c, ZigClangEnumType_getDecl(enum_ty));
}
case clang::Type::ConstantArray:
{
Expand Down Expand Up @@ -2673,8 +2673,8 @@ static AstNode *trans_bool_expr(Context *c, ResultUsed result_used, TransScope *

case clang::Type::Enum:
{
const clang::EnumType *enum_ty = static_cast<const clang::EnumType*>(ty);
AstNode *enum_type = resolve_enum_decl(c, enum_ty->getDecl());
const ZigClangEnumType *enum_ty = reinterpret_cast<const ZigClangEnumType *>(ty);
AstNode *enum_type = resolve_enum_decl(c, ZigClangEnumType_getDecl(enum_ty));
return to_enum_zero_cmp(c, res, enum_type);
}

Expand Down Expand Up @@ -4013,23 +4013,23 @@ static AstNode *resolve_typedef_decl(Context *c, const clang::TypedefNameDecl *t
return symbol_node;
}

struct AstNode *demote_enum_to_opaque(Context *c, const clang::EnumDecl *enum_decl,
Buf *full_type_name, Buf *bare_name)
struct AstNode *demote_enum_to_opaque(Context *c, const ZigClangEnumDecl *enum_decl, Buf *full_type_name,
Buf *bare_name)
{
AstNode *opaque_node = trans_create_node_opaque(c);
if (full_type_name == nullptr) {
c->decl_table.put(enum_decl->getCanonicalDecl(), opaque_node);
c->decl_table.put(ZigClangEnumDecl_getCanonicalDecl(enum_decl), opaque_node);
return opaque_node;
}
AstNode *symbol_node = trans_create_node_symbol(c, full_type_name);
add_global_weak_alias(c, bare_name, full_type_name);
add_global_var(c, full_type_name, opaque_node);
c->decl_table.put(enum_decl->getCanonicalDecl(), symbol_node);
c->decl_table.put(ZigClangEnumDecl_getCanonicalDecl(enum_decl), symbol_node);
return symbol_node;
}

static AstNode *resolve_enum_decl(Context *c, const clang::EnumDecl *enum_decl) {
auto existing_entry = c->decl_table.maybe_get((void*)enum_decl->getCanonicalDecl());
static AstNode *resolve_enum_decl(Context *c, const ZigClangEnumDecl *enum_decl) {
auto existing_entry = c->decl_table.maybe_get(ZigClangEnumDecl_getCanonicalDecl(enum_decl));
if (existing_entry) {
return existing_entry->value;
}
Expand All @@ -4039,24 +4039,26 @@ static AstNode *resolve_enum_decl(Context *c, const clang::EnumDecl *enum_decl)
Buf *bare_name = is_anonymous ? nullptr : buf_create_from_str(raw_name);
Buf *full_type_name = is_anonymous ? nullptr : buf_sprintf("enum_%s", buf_ptr(bare_name));

const clang::EnumDecl *enum_def = enum_decl->getDefinition();
const ZigClangEnumDecl *enum_def = ZigClangEnumDecl_getDefinition(enum_decl);
if (!enum_def) {
return demote_enum_to_opaque(c, enum_decl, full_type_name, bare_name);
}


bool pure_enum = true;
uint32_t field_count = 0;
for (auto it = enum_def->enumerator_begin(),
it_end = enum_def->enumerator_end();
for (auto it = reinterpret_cast<const clang::EnumDecl *>(enum_def)->enumerator_begin(),
it_end = reinterpret_cast<const clang::EnumDecl *>(enum_def)->enumerator_end();
it != it_end; ++it, field_count += 1)
{
const clang::EnumConstantDecl *enum_const = *it;
if (enum_const->getInitExpr()) {
pure_enum = false;
}
}
AstNode *tag_int_type = trans_qual_type(c, enum_decl->getIntegerType(), bitcast(enum_decl->getLocation()));
AstNode *tag_int_type = trans_qual_type(c,
bitcast(ZigClangEnumDecl_getIntegerType(enum_decl)),
ZigClangEnumDecl_getLocation(enum_decl));
assert(tag_int_type);

AstNode *enum_node = trans_create_node(c, NodeTypeContainerDecl);
Expand All @@ -4065,15 +4067,15 @@ static AstNode *resolve_enum_decl(Context *c, const clang::EnumDecl *enum_decl)
// TODO only emit this tag type if the enum tag type is not the default.
// I don't know what the default is, need to figure out how clang is deciding.
// it appears to at least be different across gcc/msvc
if (!c_is_builtin_type(c, enum_decl->getIntegerType(), clang::BuiltinType::UInt) &&
!c_is_builtin_type(c, enum_decl->getIntegerType(), clang::BuiltinType::Int))
if (!c_is_builtin_type(c, bitcast(ZigClangEnumDecl_getIntegerType(enum_decl)), clang::BuiltinType::UInt) &&
!c_is_builtin_type(c, bitcast(ZigClangEnumDecl_getIntegerType(enum_decl)), clang::BuiltinType::Int))
{
enum_node->data.container_decl.init_arg_expr = tag_int_type;
}
enum_node->data.container_decl.fields.resize(field_count);
uint32_t i = 0;
for (auto it = enum_def->enumerator_begin(),
it_end = enum_def->enumerator_end();
for (auto it = reinterpret_cast<const clang::EnumDecl *>(enum_def)->enumerator_begin(),
it_end = reinterpret_cast<const clang::EnumDecl *>(enum_def)->enumerator_end();
it != it_end; ++it, i += 1)
{
const clang::EnumConstantDecl *enum_const = *it;
Expand Down Expand Up @@ -4106,13 +4108,13 @@ static AstNode *resolve_enum_decl(Context *c, const clang::EnumDecl *enum_decl)
}

if (is_anonymous) {
c->decl_table.put(enum_decl->getCanonicalDecl(), enum_node);
c->decl_table.put(ZigClangEnumDecl_getCanonicalDecl(enum_decl), enum_node);
return enum_node;
} else {
AstNode *symbol_node = trans_create_node_symbol(c, full_type_name);
add_global_weak_alias(c, bare_name, full_type_name);
add_global_var(c, full_type_name, enum_node);
c->decl_table.put(enum_decl->getCanonicalDecl(), symbol_node);
c->decl_table.put(ZigClangEnumDecl_getCanonicalDecl(enum_decl), symbol_node);
return enum_node;
}
}
Expand Down Expand Up @@ -4398,7 +4400,7 @@ static bool decl_visitor(void *context, const ZigClangDecl *zdecl) {
resolve_typedef_decl(c, static_cast<const clang::TypedefNameDecl *>(decl));
break;
case clang::Decl::Enum:
resolve_enum_decl(c, static_cast<const clang::EnumDecl *>(decl));
resolve_enum_decl(c, reinterpret_cast<const ZigClangEnumDecl *>(decl));
break;
case clang::Decl::Record:
resolve_record_decl(c, (const ZigClangRecordDecl *)(decl));
Expand Down
25 changes: 25 additions & 0 deletions src/zig_clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,17 +218,33 @@ const ZigClangRecordDecl *ZigClangRecordType_getDecl(const ZigClangRecordType *r
return reinterpret_cast<const ZigClangRecordDecl *>(record_decl);
}

const ZigClangEnumDecl *ZigClangEnumType_getDecl(const ZigClangEnumType *enum_ty) {
const clang::EnumDecl *enum_decl = reinterpret_cast<const clang::EnumType *>(enum_ty)->getDecl();
return reinterpret_cast<const ZigClangEnumDecl *>(enum_decl);
}

const ZigClangTagDecl *ZigClangRecordDecl_getCanonicalDecl(const ZigClangRecordDecl *record_decl) {
const clang::TagDecl *tag_decl = reinterpret_cast<const clang::RecordDecl*>(record_decl)->getCanonicalDecl();
return reinterpret_cast<const ZigClangTagDecl *>(tag_decl);
}

const ZigClangTagDecl *ZigClangEnumDecl_getCanonicalDecl(const ZigClangEnumDecl *enum_decl) {
const clang::TagDecl *tag_decl = reinterpret_cast<const clang::EnumDecl*>(enum_decl)->getCanonicalDecl();
return reinterpret_cast<const ZigClangTagDecl *>(tag_decl);
}

const ZigClangRecordDecl *ZigClangRecordDecl_getDefinition(const ZigClangRecordDecl *zig_record_decl) {
const clang::RecordDecl *record_decl = reinterpret_cast<const clang::RecordDecl *>(zig_record_decl);
const clang::RecordDecl *definition = record_decl->getDefinition();
return reinterpret_cast<const ZigClangRecordDecl *>(definition);
}

const ZigClangEnumDecl *ZigClangEnumDecl_getDefinition(const ZigClangEnumDecl *zig_enum_decl) {
const clang::EnumDecl *enum_decl = reinterpret_cast<const clang::EnumDecl *>(zig_enum_decl);
const clang::EnumDecl *definition = enum_decl->getDefinition();
return reinterpret_cast<const ZigClangEnumDecl *>(definition);
}

bool ZigClangRecordDecl_isUnion(const ZigClangRecordDecl *record_decl) {
return reinterpret_cast<const clang::RecordDecl*>(record_decl)->isUnion();
}
Expand All @@ -252,8 +268,17 @@ ZigClangSourceLocation ZigClangRecordDecl_getLocation(const ZigClangRecordDecl *
return bitcast(record_decl->getLocation());
}

ZigClangSourceLocation ZigClangEnumDecl_getLocation(const ZigClangEnumDecl *self) {
auto casted = reinterpret_cast<const clang::EnumDecl *>(self);
return bitcast(casted->getLocation());
}

bool ZigClangSourceLocation_eq(ZigClangSourceLocation zig_a, ZigClangSourceLocation zig_b) {
clang::SourceLocation a = bitcast(zig_a);
clang::SourceLocation b = bitcast(zig_b);
return a == b;
}

ZigClangQualType ZigClangEnumDecl_getIntegerType(const ZigClangEnumDecl *self) {
return bitcast(reinterpret_cast<const clang::EnumDecl *>(self)->getIntegerType());
}
13 changes: 11 additions & 2 deletions src/zig_clang.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,22 @@ ZIG_EXTERN_C bool ZigClangASTUnit_visitLocalTopLevelDecls(ZigClangASTUnit *, voi
bool (*Fn)(void *context, const ZigClangDecl *decl));

ZIG_EXTERN_C const ZigClangRecordDecl *ZigClangRecordType_getDecl(const ZigClangRecordType *record_ty);
ZIG_EXTERN_C const ZigClangEnumDecl *ZigClangEnumType_getDecl(const ZigClangEnumType *record_ty);

ZIG_EXTERN_C const ZigClangTagDecl *ZigClangRecordDecl_getCanonicalDecl(const ZigClangRecordDecl *record_decl);
ZIG_EXTERN_C const ZigClangTagDecl *ZigClangEnumDecl_getCanonicalDecl(const ZigClangEnumDecl *);

ZIG_EXTERN_C const ZigClangRecordDecl *ZigClangRecordDecl_getDefinition(const ZigClangRecordDecl *);
ZIG_EXTERN_C const ZigClangEnumDecl *ZigClangEnumDecl_getDefinition(const ZigClangEnumDecl *);

ZIG_EXTERN_C ZigClangSourceLocation ZigClangRecordDecl_getLocation(const ZigClangRecordDecl *);
ZIG_EXTERN_C ZigClangSourceLocation ZigClangEnumDecl_getLocation(const ZigClangEnumDecl *);

ZIG_EXTERN_C bool ZigClangRecordDecl_isUnion(const ZigClangRecordDecl *record_decl);
ZIG_EXTERN_C bool ZigClangRecordDecl_isStruct(const ZigClangRecordDecl *record_decl);
ZIG_EXTERN_C bool ZigClangRecordDecl_isAnonymousStructOrUnion(const ZigClangRecordDecl *record_decl);
ZIG_EXTERN_C const ZigClangRecordDecl *ZigClangRecordDecl_getDefinition(const ZigClangRecordDecl *);
ZIG_EXTERN_C ZigClangSourceLocation ZigClangRecordDecl_getLocation(const ZigClangRecordDecl *);

ZIG_EXTERN_C ZigClangQualType ZigClangEnumDecl_getIntegerType(const ZigClangEnumDecl *);

ZIG_EXTERN_C const char *ZigClangDecl_getName_bytes_begin(const ZigClangDecl *decl);

Expand Down

0 comments on commit b960f1d

Please sign in to comment.