diff --git a/src/translate_c.cpp b/src/translate_c.cpp index 13d9251e6..659b5ef0c 100644 --- a/src/translate_c.cpp +++ b/src/translate_c.cpp @@ -125,7 +125,16 @@ static AstNode *trans_expr(Context *c, ResultUsed result_used, TransScope *scope static AstNode *trans_qual_type(Context *c, ZigClangQualType qt, ZigClangSourceLocation source_loc); static AstNode *trans_bool_expr(Context *c, ResultUsed result_used, TransScope *scope, const ZigClangExpr *expr, TransLRValue lrval); -static AstNode *trans_ap_value(Context *c, clang::APValue *ap_value, ZigClangQualType qt, ZigClangSourceLocation source_loc); +static AstNode *trans_ap_value(Context *c, const ZigClangAPValue *ap_value, ZigClangQualType qt, + ZigClangSourceLocation source_loc); + +static const ZigClangAPSInt *bitcast(const llvm::APSInt *src) { + return reinterpret_cast(src); +} + +static const ZigClangAPValue *bitcast(const clang::APValue *src) { + return reinterpret_cast(src); +} static const ZigClangStmt *bitcast(const clang::Stmt *src) { return reinterpret_cast(src); @@ -497,16 +506,21 @@ static Buf *string_ref_to_buf(llvm::StringRef string_ref) { return buf_create_from_mem((const char *)string_ref.bytes_begin(), string_ref.size()); } -static AstNode *trans_create_node_apint(Context *c, const llvm::APSInt &aps_int) { +static AstNode *trans_create_node_apint(Context *c, const ZigClangAPSInt *aps_int) { AstNode *node = trans_create_node(c, NodeTypeIntLiteral); node->data.int_literal.bigint = allocate(1); - bool is_negative = aps_int.isSigned() && aps_int.isNegative(); + bool is_negative = ZigClangAPSInt_isSigned(aps_int) && ZigClangAPSInt_isNegative(aps_int); if (!is_negative) { - bigint_init_data(node->data.int_literal.bigint, aps_int.getRawData(), aps_int.getNumWords(), false); + bigint_init_data(node->data.int_literal.bigint, + ZigClangAPSInt_getRawData(aps_int), + ZigClangAPSInt_getNumWords(aps_int), + false); return node; } - llvm::APSInt negated = -aps_int; - bigint_init_data(node->data.int_literal.bigint, negated.getRawData(), negated.getNumWords(), true); + const ZigClangAPSInt *negated = ZigClangAPSInt_negate(aps_int); + bigint_init_data(node->data.int_literal.bigint, ZigClangAPSInt_getRawData(negated), + ZigClangAPSInt_getNumWords(negated), true); + ZigClangAPSInt_free(negated); return node; } @@ -1295,7 +1309,7 @@ static AstNode *trans_integer_literal(Context *c, ResultUsed result_used, const emit_warning(c, bitcast(stmt->getBeginLoc()), "invalid integer literal"); return nullptr; } - AstNode *node = trans_create_node_apint(c, result.Val.getInt()); + AstNode *node = trans_create_node_apint(c, bitcast(&result.Val.getInt())); return maybe_suppress_result(c, result_used, node); } @@ -1307,7 +1321,7 @@ static AstNode *trans_constant_expr(Context *c, ResultUsed result_used, const cl emit_warning(c, bitcast(expr->getBeginLoc()), "invalid constant expression"); return nullptr; } - AstNode *node = trans_ap_value(c, &result.Val, bitcast(expr->getType()), bitcast(expr->getBeginLoc())); + AstNode *node = trans_ap_value(c, bitcast(&result.Val), bitcast(expr->getType()), bitcast(expr->getBeginLoc())); return maybe_suppress_result(c, result_used, node); } @@ -4103,7 +4117,8 @@ static AstNode *resolve_enum_decl(Context *c, const ZigClangEnumDecl *enum_decl) field_name = enum_val_name; } - AstNode *int_node = pure_enum && !is_anonymous ? nullptr : trans_create_node_apint(c, enum_const->getInitVal()); + AstNode *int_node = pure_enum && !is_anonymous ? + nullptr : trans_create_node_apint(c, bitcast(&enum_const->getInitVal())); AstNode *field_node = trans_create_node(c, NodeTypeStructField); field_node->data.struct_field.name = field_name; field_node->data.struct_field.type = nullptr; @@ -4245,17 +4260,19 @@ static AstNode *resolve_record_decl(Context *c, const ZigClangRecordDecl *record } } -static AstNode *trans_ap_value(Context *c, clang::APValue *ap_value, ZigClangQualType qt, ZigClangSourceLocation source_loc) { - switch (ap_value->getKind()) { - case clang::APValue::Int: - return trans_create_node_apint(c, ap_value->getInt()); - case clang::APValue::Uninitialized: +static AstNode *trans_ap_value(Context *c, const ZigClangAPValue *ap_value, ZigClangQualType qt, + ZigClangSourceLocation source_loc) +{ + switch (ZigClangAPValue_getKind(ap_value)) { + case ZigClangAPValueInt: + return trans_create_node_apint(c, ZigClangAPValue_getInt(ap_value)); + case ZigClangAPValueUninitialized: return trans_create_node(c, NodeTypeUndefinedLiteral); - case clang::APValue::Array: { + case ZigClangAPValueArray: { emit_warning(c, source_loc, "TODO add a test case for this code"); - unsigned init_count = ap_value->getArrayInitializedElts(); - unsigned all_count = ap_value->getArraySize(); + unsigned init_count = ZigClangAPValue_getArrayInitializedElts(ap_value); + unsigned all_count = ZigClangAPValue_getArraySize(ap_value); unsigned leftover_count = all_count - init_count; AstNode *init_node = trans_create_node(c, NodeTypeContainerInitExpr); AstNode *arr_type_node = trans_qual_type(c, qt, source_loc); @@ -4269,8 +4286,8 @@ static AstNode *trans_ap_value(Context *c, clang::APValue *ap_value, ZigClangQua ZigClangQualType child_qt = bitcast(qt_type->getAsArrayTypeUnsafe()->getElementType()); for (size_t i = 0; i < init_count; i += 1) { - clang::APValue &elem_ap_val = ap_value->getArrayInitializedElt(i); - AstNode *elem_node = trans_ap_value(c, &elem_ap_val, child_qt, source_loc); + const ZigClangAPValue *elem_ap_val = ZigClangAPValue_getArrayInitializedElt(ap_value, i); + AstNode *elem_node = trans_ap_value(c, elem_ap_val, child_qt, source_loc); if (elem_node == nullptr) return nullptr; init_node->data.container_init_expr.entries.append(elem_node); @@ -4279,8 +4296,8 @@ static AstNode *trans_ap_value(Context *c, clang::APValue *ap_value, ZigClangQua return init_node; } - clang::APValue &filler_ap_val = ap_value->getArrayFiller(); - AstNode *filler_node = trans_ap_value(c, &filler_ap_val, child_qt, source_loc); + const ZigClangAPValue *filler_ap_val = ZigClangAPValue_getArrayFiller(ap_value); + AstNode *filler_node = trans_ap_value(c, filler_ap_val, child_qt, source_loc); if (filler_node == nullptr) return nullptr; @@ -4307,37 +4324,37 @@ static AstNode *trans_ap_value(Context *c, clang::APValue *ap_value, ZigClangQua return trans_create_node_bin_op(c, init_node, BinOpTypeArrayCat, rhs_node); } - case clang::APValue::LValue: { - const clang::APValue::LValueBase lval_base = ap_value->getLValueBase(); - if (const clang::Expr *expr = lval_base.dyn_cast()) { - return trans_expr(c, ResultUsedYes, &c->global_scope->base, bitcast(expr), TransRValue); + case ZigClangAPValueLValue: { + const ZigClangAPValueLValueBase lval_base = ZigClangAPValue_getLValueBase(ap_value); + if (const ZigClangExpr *expr = ZigClangAPValueLValueBase_dyn_cast_Expr(lval_base)) { + return trans_expr(c, ResultUsedYes, &c->global_scope->base, expr, TransRValue); } //const clang::ValueDecl *value_decl = lval_base.get(); emit_warning(c, source_loc, "TODO handle initializer LValue clang::ValueDecl"); return nullptr; } - case clang::APValue::Float: + case ZigClangAPValueFloat: emit_warning(c, source_loc, "unsupported initializer value kind: Float"); return nullptr; - case clang::APValue::ComplexInt: + case ZigClangAPValueComplexInt: emit_warning(c, source_loc, "unsupported initializer value kind: ComplexInt"); return nullptr; - case clang::APValue::ComplexFloat: + case ZigClangAPValueComplexFloat: emit_warning(c, source_loc, "unsupported initializer value kind: ComplexFloat"); return nullptr; - case clang::APValue::Vector: + case ZigClangAPValueVector: emit_warning(c, source_loc, "unsupported initializer value kind: Vector"); return nullptr; - case clang::APValue::Struct: + case ZigClangAPValueStruct: emit_warning(c, source_loc, "unsupported initializer value kind: Struct"); return nullptr; - case clang::APValue::Union: + case ZigClangAPValueUnion: emit_warning(c, source_loc, "unsupported initializer value kind: Union"); return nullptr; - case clang::APValue::MemberPointer: + case ZigClangAPValueMemberPointer: emit_warning(c, source_loc, "unsupported initializer value kind: MemberPointer"); return nullptr; - case clang::APValue::AddrLabelDiff: + case ZigClangAPValueAddrLabelDiff: emit_warning(c, source_loc, "unsupported initializer value kind: AddrLabelDiff"); return nullptr; } @@ -4374,7 +4391,7 @@ static void visit_var_decl(Context *c, const clang::VarDecl *var_decl) { if (is_static && !is_extern) { AstNode *init_node; if (var_decl->hasInit()) { - clang::APValue *ap_value = var_decl->evaluateValue(); + const ZigClangAPValue *ap_value = bitcast(var_decl->evaluateValue()); if (ap_value == nullptr) { emit_warning(c, bitcast(var_decl->getLocation()), "ignoring variable '%s' - unable to evaluate initializer", buf_ptr(name)); diff --git a/src/zig_clang.cpp b/src/zig_clang.cpp index 49385ae35..6142c808a 100644 --- a/src/zig_clang.cpp +++ b/src/zig_clang.cpp @@ -28,7 +28,7 @@ #endif // Detect additions to the enum -void zig2clang_BO(clang::BinaryOperatorKind op) { +void ZigClang_detect_enum_BO(clang::BinaryOperatorKind op) { switch (op) { case clang::BO_PtrMemD: case clang::BO_PtrMemI: @@ -102,7 +102,7 @@ static_assert((clang::BinaryOperatorKind)ZigClangBO_Xor == clang::BO_Xor, ""); static_assert((clang::BinaryOperatorKind)ZigClangBO_XorAssign == clang::BO_XorAssign, ""); // Detect additions to the enum -void zig2clang_UO(clang::UnaryOperatorKind op) { +void ZigClang_detect_enum_UO(clang::UnaryOperatorKind op) { switch (op) { case clang::UO_AddrOf: case clang::UO_Coawait: @@ -138,7 +138,7 @@ static_assert((clang::UnaryOperatorKind)ZigClangUO_PreInc == clang::UO_PreInc, " static_assert((clang::UnaryOperatorKind)ZigClangUO_Real == clang::UO_Real, ""); // Detect additions to the enum -void zig2clang_CK(clang::CastKind x) { +void ZigClang_detect_enum_CK(clang::CastKind x) { switch (x) { case clang::CK_ARCConsumeObject: case clang::CK_ARCExtendBlockObject: @@ -264,7 +264,7 @@ static_assert((clang::CastKind)ZigClangCK_AddressSpaceConversion == clang::CK_Ad static_assert((clang::CastKind)ZigClangCK_IntToOCLSampler == clang::CK_IntToOCLSampler, ""); // Detect additions to the enum -void zig2clang_TypeClass(clang::Type::TypeClass ty) { +void ZigClang_detect_enum_TypeClass(clang::Type::TypeClass ty) { switch (ty) { case clang::Type::Builtin: case clang::Type::Complex: @@ -366,7 +366,7 @@ static_assert((clang::Type::TypeClass)ZigClangType_Pipe == clang::Type::Pipe, "" static_assert((clang::Type::TypeClass)ZigClangType_Atomic == clang::Type::Atomic, ""); // Detect additions to the enum -void zig2clang_StmtClass(clang::Stmt::StmtClass x) { +void ZigClang_detect_enum_StmtClass(clang::Stmt::StmtClass x) { switch (x) { case clang::Stmt::NoStmtClass: case clang::Stmt::NullStmtClass: @@ -767,6 +767,37 @@ static_assert((clang::Stmt::StmtClass)ZigClangStmt_OMPTargetTeamsDistributeParal static_assert((clang::Stmt::StmtClass)ZigClangStmt_OMPTargetTeamsDistributeParallelForSimdDirectiveClass == clang::Stmt::OMPTargetTeamsDistributeParallelForSimdDirectiveClass, ""); static_assert((clang::Stmt::StmtClass)ZigClangStmt_OMPTargetTeamsDistributeSimdDirectiveClass == clang::Stmt::OMPTargetTeamsDistributeSimdDirectiveClass, ""); +void ZigClang_detect_enum_APValueKind(clang::APValue::ValueKind x) { + switch (x) { + case clang::APValue::Uninitialized: + case clang::APValue::Int: + case clang::APValue::Float: + case clang::APValue::ComplexInt: + case clang::APValue::ComplexFloat: + case clang::APValue::LValue: + case clang::APValue::Vector: + case clang::APValue::Array: + case clang::APValue::Struct: + case clang::APValue::Union: + case clang::APValue::MemberPointer: + case clang::APValue::AddrLabelDiff: + break; + } +} + +static_assert((clang::APValue::ValueKind)ZigClangAPValueUninitialized == clang::APValue::Uninitialized, ""); +static_assert((clang::APValue::ValueKind)ZigClangAPValueInt == clang::APValue::Int, ""); +static_assert((clang::APValue::ValueKind)ZigClangAPValueFloat == clang::APValue::Float, ""); +static_assert((clang::APValue::ValueKind)ZigClangAPValueComplexInt == clang::APValue::ComplexInt, ""); +static_assert((clang::APValue::ValueKind)ZigClangAPValueComplexFloat == clang::APValue::ComplexFloat, ""); +static_assert((clang::APValue::ValueKind)ZigClangAPValueLValue == clang::APValue::LValue, ""); +static_assert((clang::APValue::ValueKind)ZigClangAPValueVector == clang::APValue::Vector, ""); +static_assert((clang::APValue::ValueKind)ZigClangAPValueArray == clang::APValue::Array, ""); +static_assert((clang::APValue::ValueKind)ZigClangAPValueStruct == clang::APValue::Struct, ""); +static_assert((clang::APValue::ValueKind)ZigClangAPValueUnion == clang::APValue::Union, ""); +static_assert((clang::APValue::ValueKind)ZigClangAPValueMemberPointer == clang::APValue::MemberPointer, ""); +static_assert((clang::APValue::ValueKind)ZigClangAPValueAddrLabelDiff == clang::APValue::AddrLabelDiff, ""); + static_assert(sizeof(ZigClangSourceLocation) == sizeof(clang::SourceLocation), ""); static ZigClangSourceLocation bitcast(clang::SourceLocation src) { @@ -792,6 +823,18 @@ static clang::QualType bitcast(ZigClangQualType src) { return dest; } +static_assert(sizeof(ZigClangAPValueLValueBase) == sizeof(clang::APValue::LValueBase), ""); +static ZigClangAPValueLValueBase bitcast(clang::APValue::LValueBase src) { + ZigClangAPValueLValueBase dest; + memcpy(&dest, static_cast(&src), sizeof(ZigClangAPValueLValueBase)); + return dest; +} +static clang::APValue::LValueBase bitcast(ZigClangAPValueLValueBase src) { + clang::APValue::LValueBase dest; + memcpy(&dest, static_cast(&src), sizeof(ZigClangAPValueLValueBase)); + return dest; +} + ZigClangSourceLocation ZigClangSourceManager_getSpellingLoc(const ZigClangSourceManager *self, ZigClangSourceLocation Loc) { @@ -1026,3 +1069,81 @@ ZigClangSourceLocation ZigClangExpr_getBeginLoc(const ZigClangExpr *self) { auto casted = reinterpret_cast(self); return bitcast(casted->getBeginLoc()); } + +ZigClangAPValueKind ZigClangAPValue_getKind(const ZigClangAPValue *self) { + auto casted = reinterpret_cast(self); + return (ZigClangAPValueKind)casted->getKind(); +} + +const ZigClangAPSInt *ZigClangAPValue_getInt(const ZigClangAPValue *self) { + auto casted = reinterpret_cast(self); + const llvm::APSInt *result = &casted->getInt(); + return reinterpret_cast(result); +} + +unsigned ZigClangAPValue_getArrayInitializedElts(const ZigClangAPValue *self) { + auto casted = reinterpret_cast(self); + return casted->getArrayInitializedElts(); +} + +const ZigClangAPValue *ZigClangAPValue_getArrayInitializedElt(const ZigClangAPValue *self, unsigned i) { + auto casted = reinterpret_cast(self); + const clang::APValue *result = &casted->getArrayInitializedElt(i); + return reinterpret_cast(result); +} + +const ZigClangAPValue *ZigClangAPValue_getArrayFiller(const ZigClangAPValue *self) { + auto casted = reinterpret_cast(self); + const clang::APValue *result = &casted->getArrayFiller(); + return reinterpret_cast(result); +} + +unsigned ZigClangAPValue_getArraySize(const ZigClangAPValue *self) { + auto casted = reinterpret_cast(self); + return casted->getArraySize(); +} + +const ZigClangAPSInt *ZigClangAPSInt_negate(const ZigClangAPSInt *self) { + auto casted = reinterpret_cast(self); + llvm::APSInt *result = new llvm::APSInt(); + *result = *casted; + *result = -*result; + return reinterpret_cast(result); +} + +void ZigClangAPSInt_free(const ZigClangAPSInt *self) { + auto casted = reinterpret_cast(self); + delete casted; +} + +bool ZigClangAPSInt_isSigned(const ZigClangAPSInt *self) { + auto casted = reinterpret_cast(self); + return casted->isSigned(); +} + +bool ZigClangAPSInt_isNegative(const ZigClangAPSInt *self) { + auto casted = reinterpret_cast(self); + return casted->isNegative(); +} + +const uint64_t *ZigClangAPSInt_getRawData(const ZigClangAPSInt *self) { + auto casted = reinterpret_cast(self); + return casted->getRawData(); +} + +unsigned ZigClangAPSInt_getNumWords(const ZigClangAPSInt *self) { + auto casted = reinterpret_cast(self); + return casted->getNumWords(); +} + +const ZigClangExpr *ZigClangAPValueLValueBase_dyn_cast_Expr(ZigClangAPValueLValueBase self) { + clang::APValue::LValueBase casted = bitcast(self); + const clang::Expr *expr = casted.dyn_cast(); + return reinterpret_cast(expr); +} + +ZigClangAPValueLValueBase ZigClangAPValue_getLValueBase(const ZigClangAPValue *self) { + auto casted = reinterpret_cast(self); + clang::APValue::LValueBase lval_base = casted->getLValueBase(); + return bitcast(lval_base); +} diff --git a/src/zig_clang.h b/src/zig_clang.h index a02cdee45..78d6e1589 100644 --- a/src/zig_clang.h +++ b/src/zig_clang.h @@ -8,6 +8,8 @@ #ifndef ZIG_ZIG_CLANG_H #define ZIG_ZIG_CLANG_H +#include + #ifdef __cplusplus #define ZIG_EXTERN_C extern "C" #else @@ -26,7 +28,14 @@ struct ZigClangQualType { void *ptr; }; +struct ZigClangAPValueLValueBase { + void *Ptr; + unsigned CallIndex; + unsigned Version; +}; + struct ZigClangAPValue; +struct ZigClangAPSInt; struct ZigClangASTContext; struct ZigClangASTUnit; struct ZigClangArraySubscriptExpr; @@ -400,24 +409,6 @@ enum ZigClangStmtClass { ZigClangStmt_WhileStmtClass, }; -//struct ZigClangCC_AAPCS; -//struct ZigClangCC_AAPCS_VFP; -//struct ZigClangCC_C; -//struct ZigClangCC_IntelOclBicc; -//struct ZigClangCC_OpenCLKernel; -//struct ZigClangCC_PreserveAll; -//struct ZigClangCC_PreserveMost; -//struct ZigClangCC_SpirFunction; -//struct ZigClangCC_Swift; -//struct ZigClangCC_Win64; -//struct ZigClangCC_X86FastCall; -//struct ZigClangCC_X86Pascal; -//struct ZigClangCC_X86RegCall; -//struct ZigClangCC_X86StdCall; -//struct ZigClangCC_X86ThisCall; -//struct ZigClangCC_X86VectorCall; -//struct ZigClangCC_X86_64SysV; - enum ZigClangCK { ZigClangCK_Dependent, ZigClangCK_BitCast, @@ -480,19 +471,20 @@ enum ZigClangCK { ZigClangCK_IntToOCLSampler, }; -//struct ZigClangETK_Class; -//struct ZigClangETK_Enum; -//struct ZigClangETK_Interface; -//struct ZigClangETK_None; -//struct ZigClangETK_Struct; -//struct ZigClangETK_Typename; -//struct ZigClangETK_Union; - -//struct ZigClangSC_None; -//struct ZigClangSC_PrivateExtern; -//struct ZigClangSC_Static; - -//struct ZigClangTU_Complete; +enum ZigClangAPValueKind { + ZigClangAPValueUninitialized, + ZigClangAPValueInt, + ZigClangAPValueFloat, + ZigClangAPValueComplexInt, + ZigClangAPValueComplexFloat, + ZigClangAPValueLValue, + ZigClangAPValueVector, + ZigClangAPValueArray, + ZigClangAPValueStruct, + ZigClangAPValueUnion, + ZigClangAPValueMemberPointer, + ZigClangAPValueAddrLabelDiff, +}; ZIG_EXTERN_C ZigClangSourceLocation ZigClangSourceManager_getSpellingLoc(const ZigClangSourceManager *, ZigClangSourceLocation Loc); @@ -558,4 +550,22 @@ ZIG_EXTERN_C bool ZigClangStmt_classof_Expr(const ZigClangStmt *self); ZIG_EXTERN_C ZigClangStmtClass ZigClangExpr_getStmtClass(const ZigClangExpr *self); ZIG_EXTERN_C ZigClangQualType ZigClangExpr_getType(const ZigClangExpr *self); ZIG_EXTERN_C ZigClangSourceLocation ZigClangExpr_getBeginLoc(const ZigClangExpr *self); + +ZIG_EXTERN_C ZigClangAPValueKind ZigClangAPValue_getKind(const ZigClangAPValue *self); +ZIG_EXTERN_C const ZigClangAPSInt *ZigClangAPValue_getInt(const ZigClangAPValue *self); +ZIG_EXTERN_C unsigned ZigClangAPValue_getArrayInitializedElts(const ZigClangAPValue *self); +ZIG_EXTERN_C const ZigClangAPValue *ZigClangAPValue_getArrayInitializedElt(const ZigClangAPValue *self, unsigned i); +ZIG_EXTERN_C const ZigClangAPValue *ZigClangAPValue_getArrayFiller(const ZigClangAPValue *self); +ZIG_EXTERN_C unsigned ZigClangAPValue_getArraySize(const ZigClangAPValue *self); +ZIG_EXTERN_C ZigClangAPValueLValueBase ZigClangAPValue_getLValueBase(const ZigClangAPValue *self); + +ZIG_EXTERN_C bool ZigClangAPSInt_isSigned(const ZigClangAPSInt *self); +ZIG_EXTERN_C bool ZigClangAPSInt_isNegative(const ZigClangAPSInt *self); +ZIG_EXTERN_C const ZigClangAPSInt *ZigClangAPSInt_negate(const ZigClangAPSInt *self); +ZIG_EXTERN_C void ZigClangAPSInt_free(const ZigClangAPSInt *self); +ZIG_EXTERN_C const uint64_t *ZigClangAPSInt_getRawData(const ZigClangAPSInt *self); +ZIG_EXTERN_C unsigned ZigClangAPSInt_getNumWords(const ZigClangAPSInt *self); + +ZIG_EXTERN_C const ZigClangExpr *ZigClangAPValueLValueBase_dyn_cast_Expr(ZigClangAPValueLValueBase self); + #endif