diff options
Diffstat (limited to 'clang/lib/AST')
| -rw-r--r-- | clang/lib/AST/ASTContext.cpp | 50 | ||||
| -rw-r--r-- | clang/lib/AST/ExprConstant.cpp | 35 |
2 files changed, 46 insertions, 39 deletions
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp index fe962806091..bd5ca93e1c1 100644 --- a/clang/lib/AST/ASTContext.cpp +++ b/clang/lib/AST/ASTContext.cpp @@ -814,10 +814,10 @@ ASTContext::getTypeInfo(const Type *T) { /// getTypeSizeInChars - Return the size of the specified type, in characters. /// This method does not work on incomplete types. CharUnits ASTContext::getTypeSizeInChars(QualType T) { - return CharUnits::fromRaw(getTypeSize(T) / getCharWidth()); + return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth()); } CharUnits ASTContext::getTypeSizeInChars(const Type *T) { - return CharUnits::fromRaw(getTypeSize(T) / getCharWidth()); + return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth()); } /// getPreferredTypeAlign - Return the "preferred" alignment of the specified @@ -3138,15 +3138,20 @@ static bool isTypeTypedefedAsBOOL(QualType T) { /// getObjCEncodingTypeSize returns size of type for objective-c encoding /// purpose. int ASTContext::getObjCEncodingTypeSize(QualType type) { - uint64_t sz = getTypeSize(type); + CharUnits sz = getTypeSizeInChars(type); // Make all integer and enum types at least as large as an int - if (sz > 0 && type->isIntegralType()) - sz = std::max(sz, getTypeSize(IntTy)); + if (sz.isPositive() && type->isIntegralType()) + sz = std::max(sz, getTypeSizeInChars(IntTy)); // Treat arrays as pointers, since that's how they're passed in. else if (type->isArrayType()) - sz = getTypeSize(VoidPtrTy); - return sz / getTypeSize(CharTy); + sz = getTypeSizeInChars(VoidPtrTy); + return sz.getQuantity(); +} + +static inline +std::string charUnitsToString(const CharUnits &CU) { + return llvm::itostr(CU.getQuantity()); } /// getObjCEncodingForBlockDecl - Return the encoded type for this method @@ -3162,17 +3167,17 @@ void ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr, // Start with computing size of a pointer in number of bytes. // FIXME: There might(should) be a better way of doing this computation! SourceLocation Loc; - int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy); - int ParmOffset = PtrSize; + CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); + CharUnits ParmOffset = PtrSize; for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), E = Decl->param_end(); PI != E; ++PI) { QualType PType = (*PI)->getType(); - int sz = getObjCEncodingTypeSize(PType); - assert (sz > 0 && "BlockExpr - Incomplete param type"); + CharUnits sz = CharUnits::fromQuantity(getObjCEncodingTypeSize(PType)); + assert (sz.isPositive() && "BlockExpr - Incomplete param type"); ParmOffset += sz; } // Size of the argument frame - S += llvm::utostr(ParmOffset); + S += charUnitsToString(ParmOffset); // Block pointer and offset. S += "@?0"; ParmOffset = PtrSize; @@ -3192,8 +3197,8 @@ void ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr, } else if (PType->isFunctionType()) PType = PVDecl->getType(); getObjCEncodingForType(PType, S); - S += llvm::utostr(ParmOffset); - ParmOffset += getObjCEncodingTypeSize(PType); + S += charUnitsToString(ParmOffset); + ParmOffset += CharUnits::fromQuantity(getObjCEncodingTypeSize(PType)); } } @@ -3210,20 +3215,21 @@ void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, // Start with computing size of a pointer in number of bytes. // FIXME: There might(should) be a better way of doing this computation! SourceLocation Loc; - int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy); + CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); // The first two arguments (self and _cmd) are pointers; account for // their size. - int ParmOffset = 2 * PtrSize; + CharUnits ParmOffset = 2 * PtrSize; for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), E = Decl->param_end(); PI != E; ++PI) { QualType PType = (*PI)->getType(); - int sz = getObjCEncodingTypeSize(PType); - assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type"); + CharUnits sz = CharUnits::fromQuantity(getObjCEncodingTypeSize(PType)); + assert (sz.isPositive() && + "getObjCEncodingForMethodDecl - Incomplete param type"); ParmOffset += sz; } - S += llvm::utostr(ParmOffset); + S += charUnitsToString(ParmOffset); S += "@0:"; - S += llvm::utostr(PtrSize); + S += charUnitsToString(PtrSize); // Argument types. ParmOffset = 2 * PtrSize; @@ -3243,8 +3249,8 @@ void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, // 'in', 'inout', etc. getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S); getObjCEncodingForType(PType, S); - S += llvm::utostr(ParmOffset); - ParmOffset += getObjCEncodingTypeSize(PType); + S += charUnitsToString(ParmOffset); + ParmOffset += CharUnits::fromQuantity(getObjCEncodingTypeSize(PType)); } } diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index cc86b243424..c40feb4c280 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -13,6 +13,7 @@ #include "clang/AST/APValue.h" #include "clang/AST/ASTContext.h" +#include "clang/AST/CharUnits.h" #include "clang/AST/RecordLayout.h" #include "clang/AST/StmtVisitor.h" #include "clang/AST/ASTDiagnostic.h" @@ -325,11 +326,11 @@ APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) { if (!EvaluateInteger(E->getIdx(), Index, Info)) return APValue(); - uint64_t ElementSize = Info.Ctx.getTypeSize(E->getType()) / 8; + CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(E->getType()); - uint64_t Offset = Index.getSExtValue() * ElementSize; + CharUnits Offset = Index.getSExtValue() * ElementSize; Result.setLValue(Result.getLValueBase(), - Result.getLValueOffset() + Offset); + Result.getLValueOffset() + Offset.getQuantity()); return Result; } @@ -410,22 +411,22 @@ APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { return APValue(); QualType PointeeType = PExp->getType()->getAs<PointerType>()->getPointeeType(); - uint64_t SizeOfPointee; + CharUnits SizeOfPointee; // Explicitly handle GNU void* and function pointer arithmetic extensions. if (PointeeType->isVoidType() || PointeeType->isFunctionType()) - SizeOfPointee = 1; + SizeOfPointee = CharUnits::One(); else - SizeOfPointee = Info.Ctx.getTypeSize(PointeeType) / 8; + SizeOfPointee = Info.Ctx.getTypeSizeInChars(PointeeType); - uint64_t Offset = ResultLValue.getLValueOffset(); + CharUnits Offset = CharUnits::fromQuantity(ResultLValue.getLValueOffset()); if (E->getOpcode() == BinaryOperator::Add) Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee; else Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee; - return APValue(ResultLValue.getLValueBase(), Offset); + return APValue(ResultLValue.getLValueBase(), Offset.getQuantity()); } APValue PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { @@ -977,13 +978,14 @@ bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { && VD->getType()->isObjectType() && !VD->getType()->isVariablyModifiedType() && !VD->getType()->isDependentType()) { - uint64_t Size = Info.Ctx.getTypeSize(VD->getType()) / 8; - uint64_t Offset = Base.Val.getLValueOffset(); - if (Offset <= Size) - Size -= Base.Val.getLValueOffset(); + CharUnits Size = Info.Ctx.getTypeSizeInChars(VD->getType()); + CharUnits Offset = + CharUnits::fromQuantity(Base.Val.getLValueOffset()); + if (!Offset.isNegative() && Offset <= Size) + Size -= Offset; else - Size = 0; - return Success(Size, E); + Size = CharUnits::Zero(); + return Success(Size.getQuantity(), E); } } } @@ -1175,7 +1177,7 @@ bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset(); if (!ElementType->isVoidType() && !ElementType->isFunctionType()) - D /= Info.Ctx.getTypeSize(ElementType) / 8; + D /= Info.Ctx.getTypeSizeInChars(ElementType).getQuantity(); return Success(D, E); } @@ -1335,8 +1337,7 @@ bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) { return false; // Get information about the size. - unsigned BitWidth = Info.Ctx.getTypeSize(SrcTy); - return Success(BitWidth / Info.Ctx.Target.getCharWidth(), E); + return Success(Info.Ctx.getTypeSizeInChars(SrcTy).getQuantity(), E); } bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |

