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 12, 2019
1 parent 5f8dbca commit 6f34d08
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 22 deletions.
47 changes: 25 additions & 22 deletions src/translate_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ 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 ZigClangEnumDecl *enum_decl);
static AstNode *resolve_typedef_decl(Context *c, const clang::TypedefNameDecl *typedef_decl);
static AstNode *resolve_typedef_decl(Context *c, const ZigClangTypedefNameDecl *typedef_decl);

static int trans_stmt_extra(Context *c, TransScope *scope, const clang::Stmt *stmt,
ResultUsed result_used, TransLRValue lrval,
Expand Down Expand Up @@ -592,7 +592,7 @@ static bool qual_type_is_fn_ptr(clang::QualType qt) {
return false;
}

static uint32_t qual_type_int_bit_width(Context *c, const clang::QualType &qt, ZigClangSourceLocation source_loc) {
static uint32_t qual_type_int_bit_width(Context *c, const clang::QualType qt, ZigClangSourceLocation source_loc) {
const clang::Type *ty = qt.getTypePtr();
switch (ty->getTypeClass()) {
case clang::Type::Builtin:
Expand All @@ -614,8 +614,8 @@ static uint32_t qual_type_int_bit_width(Context *c, const clang::QualType &qt, Z
}
case clang::Type::Typedef:
{
const clang::TypedefType *typedef_ty = static_cast<const clang::TypedefType*>(ty);
const clang::TypedefNameDecl *typedef_decl = typedef_ty->getDecl();
const ZigClangTypedefType *typedef_ty = reinterpret_cast<const ZigClangTypedefType*>(ty);
const ZigClangTypedefNameDecl *typedef_decl = ZigClangTypedefType_getDecl(typedef_ty);
const char *type_name = ZigClangDecl_getName_bytes_begin((const ZigClangDecl *)typedef_decl);
if (strcmp(type_name, "uint8_t") == 0 || strcmp(type_name, "int8_t") == 0) {
return 8;
Expand All @@ -636,7 +636,7 @@ static uint32_t qual_type_int_bit_width(Context *c, const clang::QualType &qt, Z
}


static AstNode *qual_type_to_log2_int_ref(Context *c, const clang::QualType &qt,
static AstNode *qual_type_to_log2_int_ref(Context *c, const clang::QualType qt,
ZigClangSourceLocation source_loc)
{
uint32_t int_bit_width = qual_type_int_bit_width(c, qt, source_loc);
Expand Down Expand Up @@ -669,7 +669,7 @@ static AstNode *qual_type_to_log2_int_ref(Context *c, const clang::QualType &qt,
return log2int_fn_call;
}

static bool qual_type_child_is_fn_proto(const clang::QualType &qt) {
static bool qual_type_child_is_fn_proto(const clang::QualType qt) {
if (qt.getTypePtr()->getTypeClass() == clang::Type::Paren) {
const clang::ParenType *paren_type = static_cast<const clang::ParenType *>(qt.getTypePtr());
if (paren_type->getInnerType()->getTypeClass() == clang::Type::FunctionProto) {
Expand Down Expand Up @@ -797,9 +797,11 @@ static bool type_is_opaque(Context *c, const clang::Type *ty, ZigClangSourceLoca
return type_is_opaque(c, elaborated_ty->getNamedType().getTypePtr(), source_loc);
}
case clang::Type::Typedef: {
const clang::TypedefType *typedef_ty = static_cast<const clang::TypedefType*>(ty);
const clang::TypedefNameDecl *typedef_decl = typedef_ty->getDecl();
return type_is_opaque(c, typedef_decl->getUnderlyingType().getTypePtr(), source_loc);
const ZigClangTypedefType *typedef_ty = reinterpret_cast<const ZigClangTypedefType*>(ty);
const ZigClangTypedefNameDecl *typedef_decl = ZigClangTypedefType_getDecl(typedef_ty);
ZigClangQualType underlying_type = ZigClangTypedefNameDecl_getUnderlyingType(typedef_decl);
clang::QualType qt = bitcast(underlying_type);
return type_is_opaque(c, qt.getTypePtr(), source_loc);
}
default:
return false;
Expand Down Expand Up @@ -978,8 +980,8 @@ static AstNode *trans_type(Context *c, const clang::Type *ty, ZigClangSourceLoca
}
case clang::Type::Typedef:
{
const clang::TypedefType *typedef_ty = static_cast<const clang::TypedefType*>(ty);
const clang::TypedefNameDecl *typedef_decl = typedef_ty->getDecl();
const ZigClangTypedefType *typedef_ty = reinterpret_cast<const ZigClangTypedefType*>(ty);
const ZigClangTypedefNameDecl *typedef_decl = ZigClangTypedefType_getDecl(typedef_ty);
return resolve_typedef_decl(c, typedef_decl);
}
case clang::Type::Elaborated:
Expand Down Expand Up @@ -2661,9 +2663,9 @@ static AstNode *trans_bool_expr(Context *c, ResultUsed result_used, TransScope *

case clang::Type::Typedef:
{
const clang::TypedefType *typedef_ty = static_cast<const clang::TypedefType*>(ty);
const clang::TypedefNameDecl *typedef_decl = typedef_ty->getDecl();
auto existing_entry = c->decl_table.maybe_get((void*)typedef_decl->getCanonicalDecl());
const ZigClangTypedefType *typedef_ty = reinterpret_cast<const ZigClangTypedefType*>(ty);
const ZigClangTypedefNameDecl *typedef_decl = ZigClangTypedefType_getDecl(typedef_ty);
auto existing_entry = c->decl_table.maybe_get((void*)ZigClangTypedefNameDecl_getCanonicalDecl(typedef_decl));
if (existing_entry) {
return existing_entry->value;
}
Expand Down Expand Up @@ -3952,19 +3954,19 @@ static void visit_fn_decl(Context *c, const clang::FunctionDecl *fn_decl) {
add_top_level_decl(c, fn_def_node->data.fn_def.fn_proto->data.fn_proto.name, fn_def_node);
}

static AstNode *resolve_typdef_as_builtin(Context *c, const clang::TypedefNameDecl *typedef_decl, const char *primitive_name) {
static AstNode *resolve_typdef_as_builtin(Context *c, const ZigClangTypedefNameDecl *typedef_decl, const char *primitive_name) {
AstNode *node = trans_create_node_symbol_str(c, primitive_name);
c->decl_table.put(typedef_decl, node);
return node;
}

static AstNode *resolve_typedef_decl(Context *c, const clang::TypedefNameDecl *typedef_decl) {
auto existing_entry = c->decl_table.maybe_get((void*)typedef_decl->getCanonicalDecl());
static AstNode *resolve_typedef_decl(Context *c, const ZigClangTypedefNameDecl *typedef_decl) {
auto existing_entry = c->decl_table.maybe_get((void*)ZigClangTypedefNameDecl_getCanonicalDecl(typedef_decl));
if (existing_entry) {
return existing_entry->value;
}

clang::QualType child_qt = typedef_decl->getUnderlyingType();
ZigClangQualType child_qt = ZigClangTypedefNameDecl_getUnderlyingType(typedef_decl);
Buf *type_name = buf_create_from_str(ZigClangDecl_getName_bytes_begin((const ZigClangDecl *)typedef_decl));

if (buf_eql_str(type_name, "uint8_t")) {
Expand Down Expand Up @@ -3999,11 +4001,12 @@ static AstNode *resolve_typedef_decl(Context *c, const clang::TypedefNameDecl *t

// trans_qual_type here might cause us to look at this typedef again so we put the item in the map first
AstNode *symbol_node = trans_create_node_symbol(c, type_name);
c->decl_table.put(typedef_decl->getCanonicalDecl(), symbol_node);
c->decl_table.put(ZigClangTypedefNameDecl_getCanonicalDecl(typedef_decl), symbol_node);

AstNode *type_node = trans_qual_type(c, child_qt, bitcast(typedef_decl->getLocation()));
AstNode *type_node = trans_qual_type(c, bitcast(child_qt), ZigClangTypedefNameDecl_getLocation(typedef_decl));
if (type_node == nullptr) {
emit_warning(c, bitcast(typedef_decl->getLocation()), "typedef %s - unresolved child type", buf_ptr(type_name));
emit_warning(c, ZigClangTypedefNameDecl_getLocation(typedef_decl),
"typedef %s - unresolved child type", buf_ptr(type_name));
c->decl_table.put(typedef_decl, nullptr);
// TODO add global var with type_name equal to @compileError("unable to resolve C type")
return nullptr;
Expand Down Expand Up @@ -4397,7 +4400,7 @@ static bool decl_visitor(void *context, const ZigClangDecl *zdecl) {
visit_fn_decl(c, static_cast<const clang::FunctionDecl*>(decl));
break;
case clang::Decl::Typedef:
resolve_typedef_decl(c, static_cast<const clang::TypedefNameDecl *>(decl));
resolve_typedef_decl(c, reinterpret_cast<const ZigClangTypedefNameDecl *>(decl));
break;
case clang::Decl::Enum:
resolve_enum_decl(c, reinterpret_cast<const ZigClangEnumDecl *>(decl));
Expand Down
22 changes: 22 additions & 0 deletions src/zig_clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ const ZigClangTagDecl *ZigClangEnumDecl_getCanonicalDecl(const ZigClangEnumDecl
return reinterpret_cast<const ZigClangTagDecl *>(tag_decl);
}

const ZigClangTypedefNameDecl *ZigClangTypedefNameDecl_getCanonicalDecl(const ZigClangTypedefNameDecl *self) {
const clang::TypedefNameDecl *decl = reinterpret_cast<const clang::TypedefNameDecl*>(self)->getCanonicalDecl();
return reinterpret_cast<const ZigClangTypedefNameDecl *>(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();
Expand Down Expand Up @@ -273,6 +278,11 @@ ZigClangSourceLocation ZigClangEnumDecl_getLocation(const ZigClangEnumDecl *self
return bitcast(casted->getLocation());
}

ZigClangSourceLocation ZigClangTypedefNameDecl_getLocation(const ZigClangTypedefNameDecl *self) {
auto casted = reinterpret_cast<const clang::TypedefNameDecl *>(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);
Expand All @@ -282,3 +292,15 @@ bool ZigClangSourceLocation_eq(ZigClangSourceLocation zig_a, ZigClangSourceLocat
ZigClangQualType ZigClangEnumDecl_getIntegerType(const ZigClangEnumDecl *self) {
return bitcast(reinterpret_cast<const clang::EnumDecl *>(self)->getIntegerType());
}

const ZigClangTypedefNameDecl *ZigClangTypedefType_getDecl(const ZigClangTypedefType *self) {
auto casted = reinterpret_cast<const clang::TypedefType *>(self);
const clang::TypedefNameDecl *name_decl = casted->getDecl();
return reinterpret_cast<const ZigClangTypedefNameDecl *>(name_decl);
}

ZigClangQualType ZigClangTypedefNameDecl_getUnderlyingType(const ZigClangTypedefNameDecl *self) {
auto casted = reinterpret_cast<const clang::TypedefNameDecl *>(self);
clang::QualType ty = casted->getUnderlyingType();
return bitcast(ty);
}
5 changes: 5 additions & 0 deletions src/zig_clang.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,14 @@ ZIG_EXTERN_C const ZigClangEnumDecl *ZigClangEnumType_getDecl(const ZigClangEnum

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

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 ZigClangSourceLocation ZigClangTypedefNameDecl_getLocation(const ZigClangTypedefNameDecl *);

ZIG_EXTERN_C bool ZigClangRecordDecl_isUnion(const ZigClangRecordDecl *record_decl);
ZIG_EXTERN_C bool ZigClangRecordDecl_isStruct(const ZigClangRecordDecl *record_decl);
Expand All @@ -280,4 +282,7 @@ ZIG_EXTERN_C ZigClangQualType ZigClangEnumDecl_getIntegerType(const ZigClangEnum
ZIG_EXTERN_C const char *ZigClangDecl_getName_bytes_begin(const ZigClangDecl *decl);

ZIG_EXTERN_C bool ZigClangSourceLocation_eq(ZigClangSourceLocation a, ZigClangSourceLocation b);

ZIG_EXTERN_C const ZigClangTypedefNameDecl *ZigClangTypedefType_getDecl(const ZigClangTypedefType *);
ZIG_EXTERN_C ZigClangQualType ZigClangTypedefNameDecl_getUnderlyingType(const ZigClangTypedefNameDecl *);
#endif

0 comments on commit 6f34d08

Please sign in to comment.