Skip to content

Commit 998e3bb

Browse files
committed
me
1 parent b2233d7 commit 998e3bb

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -489,26 +489,37 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
489489

490490
case CK_BooleanToSignedIntegral:
491491
case CK_IntegralCast: {
492-
std::optional<PrimType> FromT = classify(SubExpr->getType());
493492
std::optional<PrimType> ToT = classify(CE->getType());
494-
if (!FromT || !ToT)
493+
if (!ToT)
495494
return false;
496495

497-
// Try to emit a casted known constant value directly.
498-
if (const auto *IL = dyn_cast<IntegerLiteral>(SubExpr)) {
499-
if (ToT != PT_IntAP && ToT != PT_IntAPS && FromT != PT_IntAP &&
500-
FromT != PT_IntAPS && !CE->getType()->isEnumeralType())
501-
return this->emitConst(IL->getValue(), CE);
502-
if (!this->emitConst(IL->getValue(), SubExpr))
503-
return false;
504-
} else {
505-
if (!this->visit(SubExpr))
506-
return false;
496+
bool SourceIsEnum = CE->getType()->isEnumeralType();
497+
// Try to emit a casted literal value directly.
498+
if (!SourceIsEnum) {
499+
if (const auto *IL = dyn_cast<IntegerLiteral>(SubExpr)) {
500+
if (ToT == PT_IntAP) {
501+
llvm::APInt I = IL->getValue().zextOrTrunc(Ctx.getBitWidth(CE->getType()));
502+
return this->emitConstIntAP(I, CE);
503+
}
504+
if (ToT == PT_IntAPS) {
505+
llvm::APInt I = IL->getValue().sextOrTrunc(Ctx.getBitWidth(CE->getType()));
506+
return this->emitConstIntAPS(I, CE);
507+
}
508+
return this->emitConst(IL->getValue(), *ToT, CE);
509+
510+
}
507511
}
508512

513+
514+
std::optional<PrimType> FromT = classify(SubExpr->getType());
515+
if (!FromT)
516+
return false;
517+
518+
if (!this->visit(SubExpr))
519+
return false;
509520
// Possibly diagnose casts to enum types if the target type does not
510521
// have a fixed size.
511-
if (Ctx.getLangOpts().CPlusPlus && CE->getType()->isEnumeralType()) {
522+
if (Ctx.getLangOpts().CPlusPlus && SourceIsEnum) {
512523
if (const auto *ET = CE->getType().getCanonicalType()->castAs<EnumType>();
513524
!ET->getDecl()->isFixed()) {
514525
if (!this->emitCheckEnumValue(*FromT, ET->getDecl(), CE))

clang/lib/AST/ByteCode/Compiler.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,10 @@ class Compiler : public ConstStmtVisitor<Compiler<Emitter>, bool>,
349349
bool emitConst(const llvm::APInt &Value, const Expr *E) {
350350
return emitConst(static_cast<llvm::APSInt>(Value), E);
351351
}
352+
bool emitConst(const llvm::APInt &Value, PrimType Ty, const Expr *E) {
353+
return emitConst(static_cast<llvm::APSInt>(Value), Ty, E);
354+
}
355+
352356

353357
/// Emits an integer constant.
354358
template <typename T> bool emitConst(T Value, PrimType Ty, const Expr *E);

0 commit comments

Comments
 (0)