diff --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp index f3f4ae7748861..91b9809861d8b 100644 --- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp +++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp @@ -160,9 +160,7 @@ bool ByteCodeExprGen::VisitIntegerLiteral(const IntegerLiteral *LE) { if (DiscardResult) return true; - if (Optional T = classify(LE->getType())) - return emitConst(*T, LE->getValue(), LE); - return this->bail(LE); + return this->emitConst(LE->getValue(), LE); } template @@ -380,7 +378,7 @@ bool ByteCodeExprGen::VisitUnaryExprOrTypeTraitExpr( Size = Ctx.getASTContext().getTypeSizeInChars(ArgType); } - return this->emitConst(E, Size.getQuantity()); + return this->emitConst(Size.getQuantity(), E); } return false; @@ -420,9 +418,7 @@ bool ByteCodeExprGen::VisitArrayInitIndexExpr( // stand-alone, e.g. via EvaluateAsInt(). if (!ArrayIndex) return false; - QualType IndexType = E->getType(); - APInt Value(getIntWidth(IndexType), *ArrayIndex); - return this->emitConst(classifyPrim(IndexType), Value, E); + return this->emitConst(*ArrayIndex, E); } template @@ -470,7 +466,7 @@ bool ByteCodeExprGen::VisitStringLiteral(const StringLiteral *E) { template bool ByteCodeExprGen::VisitCharacterLiteral( const CharacterLiteral *E) { - return this->emitConst(E, E->getValue()); + return this->emitConst(E->getValue(), E); } template @@ -715,27 +711,27 @@ bool ByteCodeExprGen::dereferenceVar( } template -bool ByteCodeExprGen::emitConst(PrimType T, const APInt &Value, - const Expr *E) { - switch (T) { +template +bool ByteCodeExprGen::emitConst(T Value, const Expr *E) { + switch (classifyPrim(E->getType())) { case PT_Sint8: - return this->emitConstSint8(Value.getSExtValue(), E); + return this->emitConstSint8(Value, E); case PT_Uint8: - return this->emitConstUint8(Value.getZExtValue(), E); + return this->emitConstUint8(Value, E); case PT_Sint16: - return this->emitConstSint16(Value.getSExtValue(), E); + return this->emitConstSint16(Value, E); case PT_Uint16: - return this->emitConstUint16(Value.getZExtValue(), E); + return this->emitConstUint16(Value, E); case PT_Sint32: - return this->emitConstSint32(Value.getSExtValue(), E); + return this->emitConstSint32(Value, E); case PT_Uint32: - return this->emitConstUint32(Value.getZExtValue(), E); + return this->emitConstUint32(Value, E); case PT_Sint64: - return this->emitConstSint64(Value.getSExtValue(), E); + return this->emitConstSint64(Value, E); case PT_Uint64: - return this->emitConstUint64(Value.getZExtValue(), E); + return this->emitConstUint64(Value, E); case PT_Bool: - return this->emitConstBool(Value.getBoolValue(), E); + return this->emitConstBool(Value, E); case PT_Ptr: llvm_unreachable("Invalid integral type"); break; @@ -743,6 +739,13 @@ bool ByteCodeExprGen::emitConst(PrimType T, const APInt &Value, llvm_unreachable("unknown primitive type"); } +template +bool ByteCodeExprGen::emitConst(const APSInt &Value, const Expr *E) { + if (Value.isSigned()) + return this->emitConst(Value.getSExtValue(), E); + return this->emitConst(Value.getZExtValue(), E); +} + template unsigned ByteCodeExprGen::allocateLocalPrimitive(DeclTy &&Src, PrimType Ty, @@ -1198,7 +1201,7 @@ bool ByteCodeExprGen::VisitUnaryOperator(const UnaryOperator *E) { return this->emitIncPop(*T, E); this->emitLoad(*T, E); - this->emitConst(E, 1); + this->emitConst(1, E); this->emitAdd(*T, E); return this->emitStore(*T, E); } @@ -1211,7 +1214,7 @@ bool ByteCodeExprGen::VisitUnaryOperator(const UnaryOperator *E) { return this->emitDecPop(*T, E); this->emitLoad(*T, E); - this->emitConst(E, 1); + this->emitConst(1, E); this->emitSub(*T, E); return this->emitStore(*T, E); } @@ -1284,9 +1287,7 @@ bool ByteCodeExprGen::VisitDeclRefExpr(const DeclRefExpr *E) { return this->emitGetPtrParam(It->second, E); } } else if (const auto *ECD = dyn_cast(Decl)) { - PrimType T = *classify(ECD->getType()); - - return this->emitConst(T, ECD->getInitVal(), E); + return this->emitConst(ECD->getInitVal(), E); } return false; diff --git a/clang/lib/AST/Interp/ByteCodeExprGen.h b/clang/lib/AST/Interp/ByteCodeExprGen.h index 27261cb130dce..9b53065945173 100644 --- a/clang/lib/AST/Interp/ByteCodeExprGen.h +++ b/clang/lib/AST/Interp/ByteCodeExprGen.h @@ -229,16 +229,14 @@ class ByteCodeExprGen : public ConstStmtVisitor, bool>, DerefKind AK, llvm::function_ref Direct, llvm::function_ref Indirect); - /// Emits an APInt constant. - bool emitConst(PrimType T, const llvm::APInt &Value, const Expr *E); + /// Emits an APSInt constant. + bool emitConst(const APSInt &Value, const Expr *E); + bool emitConst(const APInt &Value, const Expr *E) { + return emitConst(static_cast(Value), E); + } /// Emits an integer constant. - template bool emitConst(const Expr *E, T Value) { - QualType Ty = E->getType(); - APInt WrappedValue(getIntWidth(Ty), static_cast(Value), - std::is_signed::value); - return emitConst(*Ctx.classify(Ty), WrappedValue, E); - } + template bool emitConst(T Value, const Expr *E); /// Emits the initialized pointer. bool emitInitFn() {