@@ -489,26 +489,37 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
489
489
490
490
case CK_BooleanToSignedIntegral:
491
491
case CK_IntegralCast: {
492
- std::optional<PrimType> FromT = classify (SubExpr->getType ());
493
492
std::optional<PrimType> ToT = classify (CE->getType ());
494
- if (!FromT || ! ToT)
493
+ if (!ToT)
495
494
return false ;
496
495
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
+ }
507
511
}
508
512
513
+
514
+ std::optional<PrimType> FromT = classify (SubExpr->getType ());
515
+ if (!FromT)
516
+ return false ;
517
+
518
+ if (!this ->visit (SubExpr))
519
+ return false ;
509
520
// Possibly diagnose casts to enum types if the target type does not
510
521
// have a fixed size.
511
- if (Ctx.getLangOpts ().CPlusPlus && CE-> getType ()-> isEnumeralType () ) {
522
+ if (Ctx.getLangOpts ().CPlusPlus && SourceIsEnum ) {
512
523
if (const auto *ET = CE->getType ().getCanonicalType ()->castAs <EnumType>();
513
524
!ET->getDecl ()->isFixed ()) {
514
525
if (!this ->emitCheckEnumValue (*FromT, ET->getDecl (), CE))
0 commit comments