Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 84 additions & 44 deletions clang/lib/AST/ByteCode/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@
using namespace clang;
using namespace clang::interp;

Context::Context(ASTContext &Ctx) : Ctx(Ctx), P(new Program(*this)) {}
Context::Context(ASTContext &Ctx) : Ctx(Ctx), P(new Program(*this)) {
this->IntWidth = Ctx.getTargetInfo().getIntWidth();
this->LongWidth = Ctx.getTargetInfo().getLongWidth();
assert(Ctx.getTargetInfo().getCharWidth() == 8 &&
"We're assuming 8 bit chars");
}

Context::~Context() {}

Expand Down Expand Up @@ -216,65 +221,100 @@ bool Context::evaluateCharRange(State &Parent, const Expr *SizeExpr,

const LangOptions &Context::getLangOpts() const { return Ctx.getLangOpts(); }

std::optional<PrimType> Context::classify(QualType T) const {
if (T->isBooleanType())
return PT_Bool;

if (T->isSignedIntegerOrEnumerationType()) {
switch (Ctx.getIntWidth(T)) {
case 64:
return PT_Sint64;
case 32:
return PT_Sint32;
case 16:
return PT_Sint16;
case 8:
return PT_Sint8;
default:
return PT_IntAPS;
}
static PrimType integralTypeToPrimTypeS(unsigned BitWidth) {
switch (BitWidth) {
case 64:
return PT_Sint64;
case 32:
return PT_Sint32;
case 16:
return PT_Sint16;
case 8:
return PT_Sint8;
default:
return PT_IntAPS;
}
llvm_unreachable("Unhandled BitWidth");
}

static PrimType integralTypeToPrimTypeU(unsigned BitWidth) {
switch (BitWidth) {
case 64:
return PT_Uint64;
case 32:
return PT_Uint32;
case 16:
return PT_Uint16;
case 8:
return PT_Uint8;
default:
return PT_IntAP;
}
llvm_unreachable("Unhandled BitWidth");
}

std::optional<PrimType> Context::classify(QualType T) const {

if (T->isUnsignedIntegerOrEnumerationType()) {
switch (Ctx.getIntWidth(T)) {
case 64:
return PT_Uint64;
case 32:
return PT_Uint32;
case 16:
return PT_Uint16;
case 8:
return PT_Uint8;
case 1:
// Might happen for enum types.
if (const auto *BT = dyn_cast<BuiltinType>(T.getCanonicalType())) {
auto Kind = BT->getKind();
if (Kind == BuiltinType::Bool)
return PT_Bool;
default:
return PT_IntAP;
}
if (Kind == BuiltinType::NullPtr)
return PT_Ptr;
if (Kind == BuiltinType::BoundMember)
return PT_MemberPtr;

// Just trying to avoid the ASTContext::getIntWidth call below.
if (Kind == BuiltinType::Int)
return integralTypeToPrimTypeS(this->IntWidth);
if (Kind == BuiltinType::UInt)
return integralTypeToPrimTypeU(this->IntWidth);
if (Kind == BuiltinType::Long)
return integralTypeToPrimTypeS(this->LongWidth);
if (Kind == BuiltinType::ULong)
return integralTypeToPrimTypeU(this->LongWidth);
if (Kind == BuiltinType::SChar || Kind == BuiltinType::Char_S)
return integralTypeToPrimTypeS(8);
if (Kind == BuiltinType::UChar || Kind == BuiltinType::Char_U ||
Kind == BuiltinType::Char8)
return integralTypeToPrimTypeU(8);

if (BT->isSignedInteger())
return integralTypeToPrimTypeS(Ctx.getIntWidth(T));
if (BT->isUnsignedInteger())
return integralTypeToPrimTypeU(Ctx.getIntWidth(T));

if (BT->isFloatingPoint())
return PT_Float;
}

if (T->isNullPtrType())
if (T->isPointerOrReferenceType())
return PT_Ptr;

if (T->isRealFloatingType())
return PT_Float;
if (T->isMemberPointerType())
return PT_MemberPtr;

if (T->isFunctionPointerType() || T->isFunctionReferenceType() ||
T->isFunctionType() || T->isBlockPointerType())
return PT_Ptr;
if (const auto *BT = T->getAs<BitIntType>()) {
if (BT->isSigned())
return integralTypeToPrimTypeS(BT->getNumBits());
return integralTypeToPrimTypeU(BT->getNumBits());
}

if (T->isPointerOrReferenceType() || T->isObjCObjectPointerType())
return PT_Ptr;
if (const auto *ET = T->getAs<EnumType>()) {
const auto *D = ET->getDecl();
if (!D->isComplete())
return std::nullopt;
return classify(D->getIntegerType());
}

if (const auto *AT = T->getAs<AtomicType>())
return classify(AT->getValueType());

if (const auto *DT = dyn_cast<DecltypeType>(T))
return classify(DT->getUnderlyingType());

if (T->isSpecificBuiltinType(BuiltinType::BoundMember) ||
T->isMemberPointerType())
return PT_MemberPtr;
if (T->isObjCObjectPointerType() || T->isBlockPointerType())
return PT_Ptr;

if (T->isFixedPointType())
return PT_FixedPoint;
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/AST/ByteCode/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ class Context final {
std::unique_ptr<Program> P;
/// ID identifying an evaluation.
unsigned EvalID = 0;
/// Cached widths (in bits) of common types, for a faster classify().
unsigned IntWidth;
unsigned LongWidth;
};

} // namespace interp
Expand Down
Loading