diff options
author | rdivacky <rdivacky@FreeBSD.org> | 2010-05-27 15:17:06 +0000 |
---|---|---|
committer | rdivacky <rdivacky@FreeBSD.org> | 2010-05-27 15:17:06 +0000 |
commit | 53992adde3eda3ccf9da63bc7e45673f043de18f (patch) | |
tree | 3558f327a6f9ab59c5d7a06528d84e1560445247 /lib/AST | |
parent | 7e411337c0ed226dace6e07f1420486768161308 (diff) | |
download | FreeBSD-src-53992adde3eda3ccf9da63bc7e45673f043de18f.zip FreeBSD-src-53992adde3eda3ccf9da63bc7e45673f043de18f.tar.gz |
Update clang to r104832.
Diffstat (limited to 'lib/AST')
-rw-r--r-- | lib/AST/ASTContext.cpp | 550 | ||||
-rw-r--r-- | lib/AST/ASTDiagnostic.cpp | 139 | ||||
-rw-r--r-- | lib/AST/ASTImporter.cpp | 123 | ||||
-rw-r--r-- | lib/AST/AttrImpl.cpp | 10 | ||||
-rw-r--r-- | lib/AST/CMakeLists.txt | 2 | ||||
-rw-r--r-- | lib/AST/CXXInheritance.cpp | 5 | ||||
-rw-r--r-- | lib/AST/Decl.cpp | 62 | ||||
-rw-r--r-- | lib/AST/DeclBase.cpp | 4 | ||||
-rw-r--r-- | lib/AST/DeclCXX.cpp | 67 | ||||
-rw-r--r-- | lib/AST/DeclTemplate.cpp | 111 | ||||
-rw-r--r-- | lib/AST/DeclarationName.cpp | 44 | ||||
-rw-r--r-- | lib/AST/Expr.cpp | 419 | ||||
-rw-r--r-- | lib/AST/ExprCXX.cpp | 101 | ||||
-rw-r--r-- | lib/AST/ExprConstant.cpp | 1167 | ||||
-rw-r--r-- | lib/AST/NestedNameSpecifier.cpp | 6 | ||||
-rw-r--r-- | lib/AST/RecordLayout.cpp | 7 | ||||
-rw-r--r-- | lib/AST/RecordLayoutBuilder.cpp | 907 | ||||
-rw-r--r-- | lib/AST/RecordLayoutBuilder.h | 170 | ||||
-rw-r--r-- | lib/AST/Stmt.cpp | 10 | ||||
-rw-r--r-- | lib/AST/StmtDumper.cpp | 2 | ||||
-rw-r--r-- | lib/AST/StmtPrinter.cpp | 2 | ||||
-rw-r--r-- | lib/AST/StmtProfile.cpp | 210 | ||||
-rw-r--r-- | lib/AST/TemplateBase.cpp | 42 | ||||
-rw-r--r-- | lib/AST/Type.cpp | 259 | ||||
-rw-r--r-- | lib/AST/TypeLoc.cpp | 59 | ||||
-rw-r--r-- | lib/AST/TypePrinter.cpp | 76 |
26 files changed, 2817 insertions, 1737 deletions
diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp index eea727d..851f8d1 100644 --- a/lib/AST/ASTContext.cpp +++ b/lib/AST/ASTContext.cpp @@ -18,6 +18,7 @@ #include "clang/AST/DeclTemplate.h" #include "clang/AST/TypeLoc.h" #include "clang/AST/Expr.h" +#include "clang/AST/ExprCXX.h" #include "clang/AST/ExternalASTSource.h" #include "clang/AST/RecordLayout.h" #include "clang/Basic/Builtins.h" @@ -27,7 +28,6 @@ #include "llvm/ADT/StringExtras.h" #include "llvm/Support/MathExtras.h" #include "llvm/Support/raw_ostream.h" -#include "RecordLayoutBuilder.h" using namespace clang; @@ -46,7 +46,9 @@ ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM, sigjmp_bufDecl(0), BlockDescriptorType(0), BlockDescriptorExtendedType(0), SourceMgr(SM), LangOpts(LOpts), FreeMemory(FreeMem), Target(t), Idents(idents), Selectors(sels), - BuiltinInfo(builtins), ExternalSource(0), PrintingPolicy(LOpts), + BuiltinInfo(builtins), + DeclarationNames(*this), + ExternalSource(0), PrintingPolicy(LOpts), LastSDM(0, 0) { ObjCIdRedefinitionType = QualType(); ObjCClassRedefinitionType = QualType(); @@ -61,6 +63,12 @@ ASTContext::~ASTContext() { // FIXME: Is this the ideal solution? ReleaseDeclContextMaps(); + if (!FreeMemory) { + // Call all of the deallocation functions. + for (unsigned I = 0, N = Deallocations.size(); I != N; ++I) + Deallocations[I].first(Deallocations[I].second); + } + // Release all of the memory associated with overridden C++ methods. for (llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::iterator OM = OverriddenMethods.begin(), OMEnd = OverriddenMethods.end(); @@ -111,6 +119,10 @@ ASTContext::~ASTContext() { TUDecl->Destroy(*this); } +void ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) { + Deallocations.push_back(std::make_pair(Callback, Data)); +} + void ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) { ExternalSource.reset(Source.take()); @@ -419,6 +431,18 @@ CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) { return CharUnits::fromQuantity(Align / Target.getCharWidth()); } +std::pair<CharUnits, CharUnits> +ASTContext::getTypeInfoInChars(const Type *T) { + std::pair<uint64_t, unsigned> Info = getTypeInfo(T); + return std::make_pair(CharUnits::fromQuantity(Info.first / getCharWidth()), + CharUnits::fromQuantity(Info.second / getCharWidth())); +} + +std::pair<CharUnits, CharUnits> +ASTContext::getTypeInfoInChars(QualType T) { + return getTypeInfoInChars(T.getTypePtr()); +} + /// getTypeSize - Return the size of the specified type, in bits. This method /// does not work on incomplete types. /// @@ -593,6 +617,8 @@ ASTContext::getTypeInfo(const Type *T) { Align = EltInfo.second; break; } + case Type::ObjCObject: + return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr()); case Type::ObjCInterface: { const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); @@ -624,10 +650,6 @@ ASTContext::getTypeInfo(const Type *T) { return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> getReplacementType().getTypePtr()); - case Type::Elaborated: - return getTypeInfo(cast<ElaboratedType>(T)->getUnderlyingType() - .getTypePtr()); - case Type::Typedef: { const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl(); if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) { @@ -650,8 +672,8 @@ ASTContext::getTypeInfo(const Type *T) { return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType() .getTypePtr()); - case Type::QualifiedName: - return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr()); + case Type::Elaborated: + return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr()); case Type::TemplateSpecialization: assert(getCanonicalType(T) != T && @@ -875,40 +897,6 @@ TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, return DI; } -/// getInterfaceLayoutImpl - Get or compute information about the -/// layout of the given interface. -/// -/// \param Impl - If given, also include the layout of the interface's -/// implementation. This may differ by including synthesized ivars. -const ASTRecordLayout & -ASTContext::getObjCLayout(const ObjCInterfaceDecl *D, - const ObjCImplementationDecl *Impl) { - assert(!D->isForwardDecl() && "Invalid interface decl!"); - - // Look up this layout, if already laid out, return what we have. - ObjCContainerDecl *Key = - Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D; - if (const ASTRecordLayout *Entry = ObjCLayouts[Key]) - return *Entry; - - // Add in synthesized ivar count if laying out an implementation. - if (Impl) { - unsigned SynthCount = CountNonClassIvars(D); - // If there aren't any sythesized ivars then reuse the interface - // entry. Note we can't cache this because we simply free all - // entries later; however we shouldn't look up implementations - // frequently. - if (SynthCount == 0) - return getObjCLayout(D, 0); - } - - const ASTRecordLayout *NewEntry = - ASTRecordLayoutBuilder::ComputeLayout(*this, D, Impl); - ObjCLayouts[Key] = NewEntry; - - return *NewEntry; -} - const ASTRecordLayout & ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) { return getObjCLayout(D, 0); @@ -919,45 +907,6 @@ ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) { return getObjCLayout(D->getClassInterface(), D); } -/// getASTRecordLayout - Get or compute information about the layout of the -/// specified record (struct/union/class), which indicates its size and field -/// position information. -const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) { - D = D->getDefinition(); - assert(D && "Cannot get layout of forward declarations!"); - - // Look up this layout, if already laid out, return what we have. - // Note that we can't save a reference to the entry because this function - // is recursive. - const ASTRecordLayout *Entry = ASTRecordLayouts[D]; - if (Entry) return *Entry; - - const ASTRecordLayout *NewEntry = - ASTRecordLayoutBuilder::ComputeLayout(*this, D); - ASTRecordLayouts[D] = NewEntry; - - if (getLangOptions().DumpRecordLayouts) { - llvm::errs() << "\n*** Dumping AST Record Layout\n"; - DumpRecordLayout(D, llvm::errs()); - } - - return *NewEntry; -} - -const CXXMethodDecl *ASTContext::getKeyFunction(const CXXRecordDecl *RD) { - RD = cast<CXXRecordDecl>(RD->getDefinition()); - assert(RD && "Cannot get key function for forward declarations!"); - - const CXXMethodDecl *&Entry = KeyFunctions[RD]; - if (!Entry) - Entry = ASTRecordLayoutBuilder::ComputeKeyFunction(RD); - else - assert(Entry == ASTRecordLayoutBuilder::ComputeKeyFunction(RD) && - "Key function changed!"); - - return Entry; -} - //===----------------------------------------------------------------------===// // Type creation/memoization methods //===----------------------------------------------------------------------===// @@ -1343,9 +1292,17 @@ QualType ASTContext::getVariableArrayType(QualType EltTy, SourceRange Brackets) { // Since we don't unique expressions, it isn't possible to unique VLA's // that have an expression provided for their size. - + QualType CanonType; + + if (!EltTy.isCanonical()) { + if (NumElts) + NumElts->Retain(); + CanonType = getVariableArrayType(getCanonicalType(EltTy), NumElts, ASM, + EltTypeQuals, Brackets); + } + VariableArrayType *New = new(*this, TypeAlignment) - VariableArrayType(EltTy, QualType(), NumElts, ASM, EltTypeQuals, Brackets); + VariableArrayType(EltTy, CanonType, NumElts, ASM, EltTypeQuals, Brackets); VariableArrayTypes.push_back(New); Types.push_back(New); @@ -1694,10 +1651,6 @@ QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) { if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl)) return getTypedefType(Typedef); - if (const ObjCInterfaceDecl *ObjCInterface - = dyn_cast<ObjCInterfaceDecl>(Decl)) - return getObjCInterfaceType(ObjCInterface); - assert(!isa<TemplateTypeParmDecl>(Decl) && "Template type parameter types are always available."); @@ -1888,29 +1841,28 @@ ASTContext::getTemplateSpecializationType(TemplateName Template, } QualType -ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS, - QualType NamedType) { +ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword, + NestedNameSpecifier *NNS, + QualType NamedType) { llvm::FoldingSetNodeID ID; - QualifiedNameType::Profile(ID, NNS, NamedType); + ElaboratedType::Profile(ID, Keyword, NNS, NamedType); void *InsertPos = 0; - QualifiedNameType *T - = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos); + ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); if (T) return QualType(T, 0); QualType Canon = NamedType; if (!Canon.isCanonical()) { Canon = getCanonicalType(NamedType); - QualifiedNameType *CheckT - = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos); - assert(!CheckT && "Qualified name canonical type broken"); + ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); + assert(!CheckT && "Elaborated canonical type broken"); (void)CheckT; } - T = new (*this) QualifiedNameType(NNS, NamedType, Canon); + T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon); Types.push_back(T); - QualifiedNameTypes.InsertNode(T, InsertPos); + ElaboratedTypes.InsertNode(T, InsertPos); return QualType(T, 0); } @@ -1987,30 +1939,6 @@ ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, return QualType(T, 0); } -QualType -ASTContext::getElaboratedType(QualType UnderlyingType, - ElaboratedType::TagKind Tag) { - llvm::FoldingSetNodeID ID; - ElaboratedType::Profile(ID, UnderlyingType, Tag); - - void *InsertPos = 0; - ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); - if (T) - return QualType(T, 0); - - QualType Canon = UnderlyingType; - if (!Canon.isCanonical()) { - Canon = getCanonicalType(Canon); - ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); - assert(!CheckT && "Elaborated canonical type is broken"); (void)CheckT; - } - - T = new (*this) ElaboratedType(UnderlyingType, Tag, Canon); - Types.push_back(T); - ElaboratedTypes.InsertNode(T, InsertPos); - return QualType(T, 0); -} - /// CmpProtocolNames - Comparison predicate for sorting protocols /// alphabetically. static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, @@ -2018,7 +1946,7 @@ static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, return LHS->getDeclName() < RHS->getDeclName(); } -static bool areSortedAndUniqued(ObjCProtocolDecl **Protocols, +static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols, unsigned NumProtocols) { if (NumProtocols == 0) return true; @@ -2040,96 +1968,98 @@ static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols, NumProtocols = ProtocolsEnd-Protocols; } -/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for -/// the given interface decl and the conforming protocol list. -QualType ASTContext::getObjCObjectPointerType(QualType InterfaceT, - ObjCProtocolDecl **Protocols, - unsigned NumProtocols, - unsigned Quals) { - llvm::FoldingSetNodeID ID; - ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols); - Qualifiers Qs = Qualifiers::fromCVRMask(Quals); +QualType ASTContext::getObjCObjectType(QualType BaseType, + ObjCProtocolDecl * const *Protocols, + unsigned NumProtocols) { + // If the base type is an interface and there aren't any protocols + // to add, then the interface type will do just fine. + if (!NumProtocols && isa<ObjCInterfaceType>(BaseType)) + return BaseType; + // Look in the folding set for an existing type. + llvm::FoldingSetNodeID ID; + ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols); void *InsertPos = 0; - if (ObjCObjectPointerType *QT = - ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) - return getQualifiedType(QualType(QT, 0), Qs); + if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos)) + return QualType(QT, 0); - // Sort the protocol list alphabetically to canonicalize it. + // Build the canonical type, which has the canonical base type and + // a sorted-and-uniqued list of protocols. QualType Canonical; - if (!InterfaceT.isCanonical() || - !areSortedAndUniqued(Protocols, NumProtocols)) { - if (!areSortedAndUniqued(Protocols, NumProtocols)) { + bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols); + if (!ProtocolsSorted || !BaseType.isCanonical()) { + if (!ProtocolsSorted) { llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols, Protocols + NumProtocols); unsigned UniqueCount = NumProtocols; SortAndUniqueProtocols(&Sorted[0], UniqueCount); - - Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT), - &Sorted[0], UniqueCount); + Canonical = getObjCObjectType(getCanonicalType(BaseType), + &Sorted[0], UniqueCount); } else { - Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT), - Protocols, NumProtocols); + Canonical = getObjCObjectType(getCanonicalType(BaseType), + Protocols, NumProtocols); } // Regenerate InsertPos. - ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); + ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos); } - // No match. - unsigned Size = sizeof(ObjCObjectPointerType) - + NumProtocols * sizeof(ObjCProtocolDecl *); + unsigned Size = sizeof(ObjCObjectTypeImpl); + Size += NumProtocols * sizeof(ObjCProtocolDecl *); void *Mem = Allocate(Size, TypeAlignment); - ObjCObjectPointerType *QType = new (Mem) ObjCObjectPointerType(Canonical, - InterfaceT, - Protocols, - NumProtocols); + ObjCObjectTypeImpl *T = + new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols); - Types.push_back(QType); - ObjCObjectPointerTypes.InsertNode(QType, InsertPos); - return getQualifiedType(QualType(QType, 0), Qs); + Types.push_back(T); + ObjCObjectTypes.InsertNode(T, InsertPos); + return QualType(T, 0); } -/// getObjCInterfaceType - Return the unique reference to the type for the -/// specified ObjC interface decl. The list of protocols is optional. -QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl, - ObjCProtocolDecl **Protocols, unsigned NumProtocols) { +/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for +/// the given object type. +QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) { llvm::FoldingSetNodeID ID; - ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols); + ObjCObjectPointerType::Profile(ID, ObjectT); void *InsertPos = 0; - if (ObjCInterfaceType *QT = - ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos)) + if (ObjCObjectPointerType *QT = + ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) return QualType(QT, 0); - // Sort the protocol list alphabetically to canonicalize it. + // Find the canonical object type. QualType Canonical; - if (NumProtocols && !areSortedAndUniqued(Protocols, NumProtocols)) { - llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols, - Protocols + NumProtocols); - - unsigned UniqueCount = NumProtocols; - SortAndUniqueProtocols(&Sorted[0], UniqueCount); + if (!ObjectT.isCanonical()) { + Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT)); - Canonical = getObjCInterfaceType(Decl, &Sorted[0], UniqueCount); - - ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos); + // Regenerate InsertPos. + ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); } - unsigned Size = sizeof(ObjCInterfaceType) - + NumProtocols * sizeof(ObjCProtocolDecl *); - void *Mem = Allocate(Size, TypeAlignment); - ObjCInterfaceType *QType = new (Mem) ObjCInterfaceType(Canonical, - const_cast<ObjCInterfaceDecl*>(Decl), - Protocols, - NumProtocols); + // No match. + void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment); + ObjCObjectPointerType *QType = + new (Mem) ObjCObjectPointerType(Canonical, ObjectT); Types.push_back(QType); - ObjCInterfaceTypes.InsertNode(QType, InsertPos); + ObjCObjectPointerTypes.InsertNode(QType, InsertPos); return QualType(QType, 0); } +/// getObjCInterfaceType - Return the unique reference to the type for the +/// specified ObjC interface decl. The list of protocols is optional. +QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) { + if (Decl->TypeForDecl) + return QualType(Decl->TypeForDecl, 0); + + // FIXME: redeclarations? + void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment); + ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl); + Decl->TypeForDecl = T; + Types.push_back(T); + return QualType(T, 0); +} + /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique /// TypeOfExprType AST's (since expression's are never shared). For example, /// multiple declarations that refer to "typeof(x)" all contain different @@ -2362,26 +2292,35 @@ CanQualType ASTContext::getCanonicalType(QualType T) { QualType ASTContext::getUnqualifiedArrayType(QualType T, Qualifiers &Quals) { Quals = T.getQualifiers(); - if (!isa<ArrayType>(T)) { + const ArrayType *AT = getAsArrayType(T); + if (!AT) { return T.getUnqualifiedType(); } - const ArrayType *AT = cast<ArrayType>(T); QualType Elt = AT->getElementType(); QualType UnqualElt = getUnqualifiedArrayType(Elt, Quals); if (Elt == UnqualElt) return T; - if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T)) { + if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { return getConstantArrayType(UnqualElt, CAT->getSize(), CAT->getSizeModifier(), 0); } - if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(T)) { + if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { return getIncompleteArrayType(UnqualElt, IAT->getSizeModifier(), 0); } - const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(T); + if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) { + return getVariableArrayType(UnqualElt, + VAT->getSizeExpr() ? + VAT->getSizeExpr()->Retain() : 0, + VAT->getSizeModifier(), + VAT->getIndexTypeCVRQualifiers(), + VAT->getBracketsRange()); + } + + const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT); return getDependentSizedArrayType(UnqualElt, DSAT->getSizeExpr()->Retain(), DSAT->getSizeModifier(), 0, SourceRange()); @@ -2716,6 +2655,9 @@ unsigned ASTContext::getIntegerRank(Type *T) { /// \returns the type this bit-field will promote to, or NULL if no /// promotion occurs. QualType ASTContext::isPromotableBitField(Expr *E) { + if (E->isTypeDependent() || E->isValueDependent()) + return QualType(); + FieldDecl *Field = E->getBitField(); if (!Field) return QualType(); @@ -2811,7 +2753,7 @@ CreateRecordDecl(ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC, QualType ASTContext::getCFConstantStringType() { if (!CFConstantStringTypeDecl) { CFConstantStringTypeDecl = - CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), + CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), &Idents.get("NSConstantString")); CFConstantStringTypeDecl->startDefinition(); @@ -2853,7 +2795,7 @@ void ASTContext::setCFConstantStringType(QualType T) { QualType ASTContext::getNSConstantStringType() { if (!NSConstantStringTypeDecl) { NSConstantStringTypeDecl = - CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), + CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), &Idents.get("__builtin_NSString")); NSConstantStringTypeDecl->startDefinition(); @@ -2892,7 +2834,7 @@ void ASTContext::setNSConstantStringType(QualType T) { QualType ASTContext::getObjCFastEnumerationStateType() { if (!ObjCFastEnumerationStateTypeDecl) { ObjCFastEnumerationStateTypeDecl = - CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), + CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), &Idents.get("__objcFastEnumerationState")); ObjCFastEnumerationStateTypeDecl->startDefinition(); @@ -2927,7 +2869,7 @@ QualType ASTContext::getBlockDescriptorType() { RecordDecl *T; // FIXME: Needs the FlagAppleBlock bit. - T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), + T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), &Idents.get("__block_descriptor")); T->startDefinition(); @@ -2972,7 +2914,7 @@ QualType ASTContext::getBlockDescriptorExtendedType() { RecordDecl *T; // FIXME: Needs the FlagAppleBlock bit. - T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), + T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), &Idents.get("__block_descriptor_withcopydispose")); T->startDefinition(); @@ -3044,7 +2986,7 @@ QualType ASTContext::BuildByRefType(const char *DeclName, QualType Ty) { llvm::raw_svector_ostream(Name) << "__Block_byref_" << ++UniqueBlockByRefTypeID << '_' << DeclName; RecordDecl *T; - T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), + T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), &Idents.get(Name.str())); T->startDefinition(); QualType Int32Ty = IntTy; @@ -3088,14 +3030,15 @@ QualType ASTContext::BuildByRefType(const char *DeclName, QualType Ty) { QualType ASTContext::getBlockParmType( bool BlockHasCopyDispose, - llvm::SmallVector<const Expr *, 8> &BlockDeclRefDecls) { + llvm::SmallVectorImpl<const Expr *> &Layout) { + // FIXME: Move up static unsigned int UniqueBlockParmTypeID = 0; llvm::SmallString<36> Name; llvm::raw_svector_ostream(Name) << "__block_literal_" << ++UniqueBlockParmTypeID; RecordDecl *T; - T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), + T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), &Idents.get(Name.str())); T->startDefinition(); QualType FieldTypes[] = { @@ -3125,22 +3068,28 @@ QualType ASTContext::getBlockParmType( T->addDecl(Field); } - for (size_t i = 0; i < BlockDeclRefDecls.size(); ++i) { - const Expr *E = BlockDeclRefDecls[i]; - const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); - clang::IdentifierInfo *Name = 0; - if (BDRE) { + for (unsigned i = 0; i < Layout.size(); ++i) { + const Expr *E = Layout[i]; + + QualType FieldType = E->getType(); + IdentifierInfo *FieldName = 0; + if (isa<CXXThisExpr>(E)) { + FieldName = &Idents.get("this"); + } else if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E)) { const ValueDecl *D = BDRE->getDecl(); - Name = &Idents.get(D->getName()); + FieldName = D->getIdentifier(); + if (BDRE->isByRef()) + FieldType = BuildByRefType(D->getNameAsCString(), FieldType); + } else { + // Padding. + assert(isa<ConstantArrayType>(FieldType) && + isa<DeclRefExpr>(E) && + !cast<DeclRefExpr>(E)->getDecl()->getDeclName() && + "doesn't match characteristics of padding decl"); } - QualType FieldType = E->getType(); - - if (BDRE && BDRE->isByRef()) - FieldType = BuildByRefType(BDRE->getDecl()->getNameAsCString(), - FieldType); FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), - Name, FieldType, /*TInfo=*/0, + FieldName, FieldType, /*TInfo=*/0, /*BitWidth=*/0, /*Mutable=*/false); Field->setAccess(AS_public); T->addDecl(Field); @@ -3593,6 +3542,17 @@ void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, // Anonymous structures print as '?' if (const IdentifierInfo *II = RDecl->getIdentifier()) { S += II->getName(); + if (ClassTemplateSpecializationDecl *Spec + = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) { + const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); + std::string TemplateArgsStr + = TemplateSpecializationType::PrintTemplateArgumentList( + TemplateArgs.getFlatArgumentList(), + TemplateArgs.flat_size(), + (*this).PrintingPolicy); + + S += TemplateArgsStr; + } } else { S += '?'; } @@ -3636,6 +3596,10 @@ void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, return; } + // Ignore protocol qualifiers when mangling at this level. + if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>()) + T = OT->getBaseType(); + if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) { // @encode(class_name) ObjCInterfaceDecl *OI = OIT->getDecl(); @@ -3718,6 +3682,11 @@ void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, return; } + // gcc just blithely ignores member pointers. + // TODO: maybe there should be a mangling for these + if (T->getAs<MemberPointerType>()) + return; + assert(0 && "@encode for type not implemented!"); } @@ -4106,18 +4075,21 @@ bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, /// bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, const ObjCObjectPointerType *RHSOPT) { + const ObjCObjectType* LHS = LHSOPT->getObjectType(); + const ObjCObjectType* RHS = RHSOPT->getObjectType(); + // If either type represents the built-in 'id' or 'Class' types, return true. - if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType()) + if (LHS->isObjCUnqualifiedIdOrClass() || + RHS->isObjCUnqualifiedIdOrClass()) return true; - if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) + if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId()) return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), QualType(RHSOPT,0), false); - const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); - const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); - if (LHS && RHS) // We have 2 user-defined types. + // If we have 2 user-defined types, fall into that path. + if (LHS->getInterface() && RHS->getInterface()) return canAssignObjCInterfaces(LHS, RHS); return false; @@ -4168,8 +4140,10 @@ void getIntersectionOfProtocols(ASTContext &Context, const ObjCObjectPointerType *RHSOPT, llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) { - const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); - const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); + const ObjCObjectType* LHS = LHSOPT->getObjectType(); + const ObjCObjectType* RHS = RHSOPT->getObjectType(); + assert(LHS->getInterface() && "LHS must have an interface base"); + assert(RHS->getInterface() && "RHS must have an interface base"); llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet; unsigned LHSNumProtocols = LHS->getNumProtocols(); @@ -4177,7 +4151,8 @@ void getIntersectionOfProtocols(ASTContext &Context, InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end()); else { llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; - Context.CollectInheritedProtocols(LHS->getDecl(), LHSInheritedProtocols); + Context.CollectInheritedProtocols(LHS->getInterface(), + LHSInheritedProtocols); InheritedProtocolSet.insert(LHSInheritedProtocols.begin(), LHSInheritedProtocols.end()); } @@ -4192,7 +4167,8 @@ void getIntersectionOfProtocols(ASTContext &Context, } else { llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols; - Context.CollectInheritedProtocols(RHS->getDecl(), RHSInheritedProtocols); + Context.CollectInheritedProtocols(RHS->getInterface(), + RHSInheritedProtocols); for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = RHSInheritedProtocols.begin(), E = RHSInheritedProtocols.end(); I != E; ++I) @@ -4206,37 +4182,40 @@ void getIntersectionOfProtocols(ASTContext &Context, /// last type comparison in a ?-exp of ObjC pointer types before a /// warning is issued. So, its invokation is extremely rare. QualType ASTContext::areCommonBaseCompatible( - const ObjCObjectPointerType *LHSOPT, - const ObjCObjectPointerType *RHSOPT) { - const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); - const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); - if (!LHS || !RHS) + const ObjCObjectPointerType *Lptr, + const ObjCObjectPointerType *Rptr) { + const ObjCObjectType *LHS = Lptr->getObjectType(); + const ObjCObjectType *RHS = Rptr->getObjectType(); + const ObjCInterfaceDecl* LDecl = LHS->getInterface(); + const ObjCInterfaceDecl* RDecl = RHS->getInterface(); + if (!LDecl || !RDecl) return QualType(); - while (const ObjCInterfaceDecl *LHSIDecl = LHS->getDecl()->getSuperClass()) { - QualType LHSTy = getObjCInterfaceType(LHSIDecl); - LHS = LHSTy->getAs<ObjCInterfaceType>(); + while ((LDecl = LDecl->getSuperClass())) { + LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl)); if (canAssignObjCInterfaces(LHS, RHS)) { - llvm::SmallVector<ObjCProtocolDecl *, 8> IntersectionOfProtocols; - getIntersectionOfProtocols(*this, - LHSOPT, RHSOPT, IntersectionOfProtocols); - if (IntersectionOfProtocols.empty()) - LHSTy = getObjCObjectPointerType(LHSTy); - else - LHSTy = getObjCObjectPointerType(LHSTy, &IntersectionOfProtocols[0], - IntersectionOfProtocols.size()); - return LHSTy; + llvm::SmallVector<ObjCProtocolDecl *, 8> Protocols; + getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols); + + QualType Result = QualType(LHS, 0); + if (!Protocols.empty()) + Result = getObjCObjectType(Result, Protocols.data(), Protocols.size()); + Result = getObjCObjectPointerType(Result); + return Result; } } return QualType(); } -bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS, - const ObjCInterfaceType *RHS) { +bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS, + const ObjCObjectType *RHS) { + assert(LHS->getInterface() && "LHS is not an interface type"); + assert(RHS->getInterface() && "RHS is not an interface type"); + // Verify that the base decls are compatible: the RHS must be a subclass of // the LHS. - if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl())) + if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface())) return false; // RHS must have a superset of the protocols in the LHS. If the LHS is not @@ -4249,15 +4228,15 @@ bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS, if (RHS->getNumProtocols() == 0) return true; // FIXME: should return false! - for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(), - LHSPE = LHS->qual_end(); + for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), + LHSPE = LHS->qual_end(); LHSPI != LHSPE; LHSPI++) { bool RHSImplementsProtocol = false; // If the RHS doesn't implement the protocol on the left, the types // are incompatible. - for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(), - RHSPE = RHS->qual_end(); + for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(), + RHSPE = RHS->qual_end(); RHSPI != RHSPE; RHSPI++) { if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) { RHSImplementsProtocol = true; @@ -4483,6 +4462,10 @@ QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) RHSClass = Type::ConstantArray; + // ObjCInterfaces are just specialized ObjCObjects. + if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject; + if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject; + // Canonicalize ExtVector -> Vector. if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; @@ -4522,6 +4505,7 @@ QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, assert(false && "C++ should never be in mergeTypes"); return QualType(); + case Type::ObjCInterface: case Type::IncompleteArray: case Type::VariableArray: case Type::FunctionProto: @@ -4614,14 +4598,13 @@ QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, RHSCan->getAs<VectorType>())) return LHS; return QualType(); - case Type::ObjCInterface: { - // Check if the interfaces are assignment compatible. + case Type::ObjCObject: { + // Check if the types are assignment compatible. // FIXME: This should be type compatibility, e.g. whether // "LHS x; RHS x;" at global scope is legal. - const ObjCInterfaceType* LHSIface = LHS->getAs<ObjCInterfaceType>(); - const ObjCInterfaceType* RHSIface = RHS->getAs<ObjCInterfaceType>(); - if (LHSIface && RHSIface && - canAssignObjCInterfaces(LHSIface, RHSIface)) + const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>(); + const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>(); + if (canAssignObjCInterfaces(LHSIface, RHSIface)) return LHS; return QualType(); @@ -4645,6 +4628,87 @@ QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, return QualType(); } +/// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and +/// 'RHS' attributes and returns the merged version; including for function +/// return types. +QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) { + QualType LHSCan = getCanonicalType(LHS), + RHSCan = getCanonicalType(RHS); + // If two types are identical, they are compatible. + if (LHSCan == RHSCan) + return LHS; + if (RHSCan->isFunctionType()) { + if (!LHSCan->isFunctionType()) + return QualType(); + QualType OldReturnType = + cast<FunctionType>(RHSCan.getTypePtr())->getResultType(); + QualType NewReturnType = + cast<FunctionType>(LHSCan.getTypePtr())->getResultType(); + QualType ResReturnType = + mergeObjCGCQualifiers(NewReturnType, OldReturnType); + if (ResReturnType.isNull()) + return QualType(); + if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) { + // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo(); + // In either case, use OldReturnType to build the new function type. + const FunctionType *F = LHS->getAs<FunctionType>(); + if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) { + FunctionType::ExtInfo Info = getFunctionExtInfo(LHS); + QualType ResultType + = getFunctionType(OldReturnType, FPT->arg_type_begin(), + FPT->getNumArgs(), FPT->isVariadic(), + FPT->getTypeQuals(), + FPT->hasExceptionSpec(), + FPT->hasAnyExceptionSpec(), + FPT->getNumExceptions(), + FPT->exception_begin(), + Info); + return ResultType; + } + } + return QualType(); + } + + // If the qualifiers are different, the types can still be merged. + Qualifiers LQuals = LHSCan.getLocalQualifiers(); + Qualifiers RQuals = RHSCan.getLocalQualifiers(); + if (LQuals != RQuals) { + // If any of these qualifiers are different, we have a type mismatch. + if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || + LQuals.getAddressSpace() != RQuals.getAddressSpace()) + return QualType(); + + // Exactly one GC qualifier difference is allowed: __strong is + // okay if the other type has no GC qualifier but is an Objective + // C object pointer (i.e. implicitly strong by default). We fix + // this by pretending that the unqualified type was actually + // qualified __strong. + Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); + Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); + assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); + + if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) + return QualType(); + + if (GC_L == Qualifiers::Strong) + return LHS; + if (GC_R == Qualifiers::Strong) + return RHS; + return QualType(); + } + + if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) { + QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType(); + QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType(); + QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT); + if (ResQT == LHSBaseQT) + return LHS; + if (ResQT == RHSBaseQT) + return RHS; + } + return QualType(); +} + //===----------------------------------------------------------------------===// // Integer Predicates //===----------------------------------------------------------------------===// diff --git a/lib/AST/ASTDiagnostic.cpp b/lib/AST/ASTDiagnostic.cpp index e4cd2a9..0d609bf 100644 --- a/lib/AST/ASTDiagnostic.cpp +++ b/lib/AST/ASTDiagnostic.cpp @@ -19,65 +19,41 @@ using namespace clang; -/// Determines whether we should have an a.k.a. clause when -/// pretty-printing a type. There are three main criteria: -/// -/// 1) Some types provide very minimal sugar that doesn't impede the -/// user's understanding --- for example, elaborated type -/// specifiers. If this is all the sugar we see, we don't want an -/// a.k.a. clause. -/// 2) Some types are technically sugared but are much more familiar -/// when seen in their sugared form --- for example, va_list, -/// vector types, and the magic Objective C types. We don't -/// want to desugar these, even if we do produce an a.k.a. clause. -/// 3) Some types may have already been desugared previously in this diagnostic. -/// if this is the case, doing another "aka" would just be clutter. -/// -static bool ShouldAKA(ASTContext &Context, QualType QT, - const Diagnostic::ArgumentValue *PrevArgs, - unsigned NumPrevArgs, - QualType &DesugaredQT) { - QualType InputTy = QT; - - bool AKA = false; - QualifierCollector Qc; - +// Returns a desugared version of the QualType, and marks ShouldAKA as true +// whenever we remove significant sugar from the type. +static QualType Desugar(ASTContext &Context, QualType QT, bool &ShouldAKA) { + QualifierCollector QC; + while (true) { - const Type *Ty = Qc.strip(QT); - + const Type *Ty = QC.strip(QT); + // Don't aka just because we saw an elaborated type... if (isa<ElaboratedType>(Ty)) { QT = cast<ElaboratedType>(Ty)->desugar(); continue; } - - // ...or a qualified name type... - if (isa<QualifiedNameType>(Ty)) { - QT = cast<QualifiedNameType>(Ty)->desugar(); - continue; - } // ...or a substituted template type parameter. if (isa<SubstTemplateTypeParmType>(Ty)) { QT = cast<SubstTemplateTypeParmType>(Ty)->desugar(); continue; } - + // Don't desugar template specializations. if (isa<TemplateSpecializationType>(Ty)) break; - + // Don't desugar magic Objective-C types. if (QualType(Ty,0) == Context.getObjCIdType() || QualType(Ty,0) == Context.getObjCClassType() || QualType(Ty,0) == Context.getObjCSelType() || QualType(Ty,0) == Context.getObjCProtoType()) break; - + // Don't desugar va_list. if (QualType(Ty,0) == Context.getBuiltinVaListType()) break; - + // Otherwise, do a single-step desugar. QualType Underlying; bool IsSugar = false; @@ -94,50 +70,56 @@ break; \ } #include "clang/AST/TypeNodes.def" } - + // If it wasn't sugared, we're done. if (!IsSugar) break; - + // If the desugared type is a vector type, we don't want to expand // it, it will turn into an attribute mess. People want their "vec4". if (isa<VectorType>(Underlying)) break; - + // Don't desugar through the primary typedef of an anonymous type. if (isa<TagType>(Underlying) && isa<TypedefType>(QT)) if (cast<TagType>(Underlying)->getDecl()->getTypedefForAnonDecl() == cast<TypedefType>(QT)->getDecl()) break; - - // Otherwise, we're tearing through something opaque; note that - // we'll eventually need an a.k.a. clause and keep going. - AKA = true; + + // Record that we actually looked through an opaque type here. + ShouldAKA = true; QT = Underlying; - continue; } - - // If we never tore through opaque sugar, don't print aka. - if (!AKA) return false; - - // If we did, check to see if we already desugared this type in this - // diagnostic. If so, don't do it again. - for (unsigned i = 0; i != NumPrevArgs; ++i) { - // TODO: Handle ak_declcontext case. - if (PrevArgs[i].first == Diagnostic::ak_qualtype) { - void *Ptr = (void*)PrevArgs[i].second; - QualType PrevTy(QualType::getFromOpaquePtr(Ptr)); - if (PrevTy == InputTy) - return false; - } + + // If we have a pointer-like type, desugar the pointee as well. + // FIXME: Handle other pointer-like types. + if (const PointerType *Ty = QT->getAs<PointerType>()) { + QT = Context.getPointerType(Desugar(Context, Ty->getPointeeType(), + ShouldAKA)); + } else if (const LValueReferenceType *Ty = QT->getAs<LValueReferenceType>()) { + QT = Context.getLValueReferenceType(Desugar(Context, Ty->getPointeeType(), + ShouldAKA)); } - - DesugaredQT = Qc.apply(QT); - return true; + + return QC.apply(QT); } /// \brief Convert the given type to a string suitable for printing as part of -/// a diagnostic. +/// a diagnostic. +/// +/// There are three main criteria when determining whether we should have an +/// a.k.a. clause when pretty-printing a type: +/// +/// 1) Some types provide very minimal sugar that doesn't impede the +/// user's understanding --- for example, elaborated type +/// specifiers. If this is all the sugar we see, we don't want an +/// a.k.a. clause. +/// 2) Some types are technically sugared but are much more familiar +/// when seen in their sugared form --- for example, va_list, +/// vector types, and the magic Objective C types. We don't +/// want to desugar these, even if we do produce an a.k.a. clause. +/// 3) Some types may have already been desugared previously in this diagnostic. +/// if this is the case, doing another "aka" would just be clutter. /// /// \param Context the context in which the type was allocated /// \param Ty the type to print @@ -147,18 +129,35 @@ ConvertTypeToDiagnosticString(ASTContext &Context, QualType Ty, unsigned NumPrevArgs) { // FIXME: Playing with std::string is really slow. std::string S = Ty.getAsString(Context.PrintingPolicy); - + + // Check to see if we already desugared this type in this + // diagnostic. If so, don't do it again. + bool Repeated = false; + for (unsigned i = 0; i != NumPrevArgs; ++i) { + // TODO: Handle ak_declcontext case. + if (PrevArgs[i].first == Diagnostic::ak_qualtype) { + void *Ptr = (void*)PrevArgs[i].second; + QualType PrevTy(QualType::getFromOpaquePtr(Ptr)); + if (PrevTy == Ty) { + Repeated = true; + break; + } + } + } + // Consider producing an a.k.a. clause if removing all the direct // sugar gives us something "significantly different". - - QualType DesugaredTy; - if (ShouldAKA(Context, Ty, PrevArgs, NumPrevArgs, DesugaredTy)) { - S = "'"+S+"' (aka '"; - S += DesugaredTy.getAsString(Context.PrintingPolicy); - S += "')"; - return S; + if (!Repeated) { + bool ShouldAKA = false; + QualType DesugaredTy = Desugar(Context, Ty, ShouldAKA); + if (ShouldAKA) { + S = "'"+S+"' (aka '"; + S += DesugaredTy.getAsString(Context.PrintingPolicy); + S += "')"; + return S; + } } - + S = "'" + S + "'"; return S; } diff --git a/lib/AST/ASTImporter.cpp b/lib/AST/ASTImporter.cpp index ae09d79..6ed08d1 100644 --- a/lib/AST/ASTImporter.cpp +++ b/lib/AST/ASTImporter.cpp @@ -68,13 +68,13 @@ namespace { // FIXME: DependentDecltypeType QualType VisitRecordType(RecordType *T); QualType VisitEnumType(EnumType *T); - QualType VisitElaboratedType(ElaboratedType *T); // FIXME: TemplateTypeParmType // FIXME: SubstTemplateTypeParmType // FIXME: TemplateSpecializationType - QualType VisitQualifiedNameType(QualifiedNameType *T); + QualType VisitElaboratedType(ElaboratedType *T); // FIXME: DependentNameType QualType VisitObjCInterfaceType(ObjCInterfaceType *T); + QualType VisitObjCObjectType(ObjCObjectType *T); QualType VisitObjCObjectPointerType(ObjCObjectPointerType *T); // Importing declarations @@ -532,19 +532,7 @@ static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, cast<TagType>(T2)->getDecl())) return false; break; - - case Type::Elaborated: { - const ElaboratedType *Elab1 = cast<ElaboratedType>(T1); - const ElaboratedType *Elab2 = cast<ElaboratedType>(T2); - if (Elab1->getTagKind() != Elab2->getTagKind()) - return false; - if (!IsStructurallyEquivalent(Context, - Elab1->getUnderlyingType(), - Elab2->getUnderlyingType())) - return false; - break; - } - + case Type::TemplateTypeParm: { const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1); const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2); @@ -594,16 +582,19 @@ static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, break; } - case Type::QualifiedName: { - const QualifiedNameType *Qual1 = cast<QualifiedNameType>(T1); - const QualifiedNameType *Qual2 = cast<QualifiedNameType>(T2); + case Type::Elaborated: { + const ElaboratedType *Elab1 = cast<ElaboratedType>(T1); + const ElaboratedType *Elab2 = cast<ElaboratedType>(T2); + // CHECKME: what if a keyword is ETK_None or ETK_typename ? + if (Elab1->getKeyword() != Elab2->getKeyword()) + return false; if (!IsStructurallyEquivalent(Context, - Qual1->getQualifier(), - Qual2->getQualifier())) + Elab1->getQualifier(), + Elab2->getQualifier())) return false; if (!IsStructurallyEquivalent(Context, - Qual1->getNamedType(), - Qual2->getNamedType())) + Elab1->getNamedType(), + Elab2->getNamedType())) return false; break; } @@ -642,12 +633,22 @@ static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, if (!IsStructurallyEquivalent(Context, Iface1->getDecl(), Iface2->getDecl())) return false; - if (Iface1->getNumProtocols() != Iface2->getNumProtocols()) + break; + } + + case Type::ObjCObject: { + const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1); + const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2); + if (!IsStructurallyEquivalent(Context, + Obj1->getBaseType(), + Obj2->getBaseType())) + return false; + if (Obj1->getNumProtocols() != Obj2->getNumProtocols()) return false; - for (unsigned I = 0, N = Iface1->getNumProtocols(); I != N; ++I) { + for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) { if (!IsStructurallyEquivalent(Context, - Iface1->getProtocol(I), - Iface2->getProtocol(I))) + Obj1->getProtocol(I), + Obj2->getProtocol(I))) return false; } break; @@ -660,14 +661,6 @@ static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, Ptr1->getPointeeType(), Ptr2->getPointeeType())) return false; - if (Ptr1->getNumProtocols() != Ptr2->getNumProtocols()) - return false; - for (unsigned I = 0, N = Ptr1->getNumProtocols(); I != N; ++I) { - if (!IsStructurallyEquivalent(Context, - Ptr1->getProtocol(I), - Ptr2->getProtocol(I))) - return false; - } break; } @@ -1293,24 +1286,20 @@ QualType ASTNodeImporter::VisitEnumType(EnumType *T) { } QualType ASTNodeImporter::VisitElaboratedType(ElaboratedType *T) { - QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType()); - if (ToUnderlyingType.isNull()) - return QualType(); - - return Importer.getToContext().getElaboratedType(ToUnderlyingType, - T->getTagKind()); -} - -QualType ASTNodeImporter::VisitQualifiedNameType(QualifiedNameType *T) { - NestedNameSpecifier *ToQualifier = Importer.Import(T->getQualifier()); - if (!ToQualifier) - return QualType(); + NestedNameSpecifier *ToQualifier = 0; + // Note: the qualifier in an ElaboratedType is optional. + if (T->getQualifier()) { + ToQualifier = Importer.Import(T->getQualifier()); + if (!ToQualifier) + return QualType(); + } QualType ToNamedType = Importer.Import(T->getNamedType()); if (ToNamedType.isNull()) return QualType(); - return Importer.getToContext().getQualifiedNameType(ToQualifier, ToNamedType); + return Importer.getToContext().getElaboratedType(T->getKeyword(), + ToQualifier, ToNamedType); } QualType ASTNodeImporter::VisitObjCInterfaceType(ObjCInterfaceType *T) { @@ -1319,8 +1308,16 @@ QualType ASTNodeImporter::VisitObjCInterfaceType(ObjCInterfaceType *T) { if (!Class) return QualType(); + return Importer.getToContext().getObjCInterfaceType(Class); +} + +QualType ASTNodeImporter::VisitObjCObjectType(ObjCObjectType *T) { + QualType ToBaseType = Importer.Import(T->getBaseType()); + if (ToBaseType.isNull()) + return QualType(); + llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols; - for (ObjCInterfaceType::qual_iterator P = T->qual_begin(), + for (ObjCObjectType::qual_iterator P = T->qual_begin(), PEnd = T->qual_end(); P != PEnd; ++P) { ObjCProtocolDecl *Protocol @@ -1330,9 +1327,9 @@ QualType ASTNodeImporter::VisitObjCInterfaceType(ObjCInterfaceType *T) { Protocols.push_back(Protocol); } - return Importer.getToContext().getObjCInterfaceType(Class, - Protocols.data(), - Protocols.size()); + return Importer.getToContext().getObjCObjectType(ToBaseType, + Protocols.data(), + Protocols.size()); } QualType ASTNodeImporter::VisitObjCObjectPointerType(ObjCObjectPointerType *T) { @@ -1340,20 +1337,7 @@ QualType ASTNodeImporter::VisitObjCObjectPointerType(ObjCObjectPointerType *T) { if (ToPointeeType.isNull()) return QualType(); - llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols; - for (ObjCObjectPointerType::qual_iterator P = T->qual_begin(), - PEnd = T->qual_end(); - P != PEnd; ++P) { - ObjCProtocolDecl *Protocol - = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P)); - if (!Protocol) - return QualType(); - Protocols.push_back(Protocol); - } - - return Importer.getToContext().getObjCObjectPointerType(ToPointeeType, - Protocols.data(), - Protocols.size()); + return Importer.getToContext().getObjCObjectPointerType(ToPointeeType); } //---------------------------------------------------------------------------- @@ -1617,7 +1601,12 @@ Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) { D2->startDefinition(); ImportDeclContext(D); - D2->completeDefinition(T, ToPromotionType); + + // FIXME: we might need to merge the number of positive or negative bits + // if the enumerator lists don't match. + D2->completeDefinition(T, ToPromotionType, + D->getNumPositiveBits(), + D->getNumNegativeBits()); } return D2; @@ -2961,7 +2950,7 @@ TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) { return 0; return ToContext.getTrivialTypeSourceInfo(T, - FromTSI->getTypeLoc().getFullSourceRange().getBegin()); + FromTSI->getTypeLoc().getSourceRange().getBegin()); } Decl *ASTImporter::Import(Decl *FromD) { diff --git a/lib/AST/AttrImpl.cpp b/lib/AST/AttrImpl.cpp index 423aa06..0fab22c 100644 --- a/lib/AST/AttrImpl.cpp +++ b/lib/AST/AttrImpl.cpp @@ -74,6 +74,7 @@ void NonNullAttr::Destroy(ASTContext &C) { // FIXME: Can we use variadic macro to define DEF_SIMPLE_ATTR_CLONE for // "non-simple" classes? +DEF_SIMPLE_ATTR_CLONE(AlignMac68k) DEF_SIMPLE_ATTR_CLONE(AlwaysInline) DEF_SIMPLE_ATTR_CLONE(AnalyzerNoReturn) DEF_SIMPLE_ATTR_CLONE(BaseCheck) @@ -100,6 +101,7 @@ DEF_SIMPLE_ATTR_CLONE(Override) DEF_SIMPLE_ATTR_CLONE(Packed) DEF_SIMPLE_ATTR_CLONE(Pure) DEF_SIMPLE_ATTR_CLONE(StdCall) +DEF_SIMPLE_ATTR_CLONE(ThisCall) DEF_SIMPLE_ATTR_CLONE(TransparentUnion) DEF_SIMPLE_ATTR_CLONE(Unavailable) DEF_SIMPLE_ATTR_CLONE(Unused) @@ -110,8 +112,8 @@ DEF_SIMPLE_ATTR_CLONE(WeakImport) DEF_SIMPLE_ATTR_CLONE(WeakRef) DEF_SIMPLE_ATTR_CLONE(X86ForceAlignArgPointer) -Attr* PragmaPackAttr::clone(ASTContext &C) const { - return ::new (C) PragmaPackAttr(Alignment); +Attr* MaxFieldAlignmentAttr::clone(ASTContext &C) const { + return ::new (C) MaxFieldAlignmentAttr(Alignment); } Attr* AlignedAttr::clone(ASTContext &C) const { @@ -142,6 +144,10 @@ Attr *IBOutletAttr::clone(ASTContext &C) const { return ::new (C) IBOutletAttr; } +Attr *IBOutletCollectionAttr::clone(ASTContext &C) const { + return ::new (C) IBOutletCollectionAttr(D); +} + Attr *IBActionAttr::clone(ASTContext &C) const { return ::new (C) IBActionAttr; } diff --git a/lib/AST/CMakeLists.txt b/lib/AST/CMakeLists.txt index 91aaddc..bce3646 100644 --- a/lib/AST/CMakeLists.txt +++ b/lib/AST/CMakeLists.txt @@ -39,4 +39,4 @@ add_clang_library(clangAST TypePrinter.cpp ) -add_dependencies(clangAST ClangDiagnosticAST) +add_dependencies(clangAST ClangDiagnosticAST ClangStmtNodes) diff --git a/lib/AST/CXXInheritance.cpp b/lib/AST/CXXInheritance.cpp index a9f2230..d616e42 100644 --- a/lib/AST/CXXInheritance.cpp +++ b/lib/AST/CXXInheritance.cpp @@ -49,9 +49,8 @@ CXXBasePaths::decl_iterator CXXBasePaths::found_decls_end() { /// ambiguous, i.e., there are two or more paths that refer to /// different base class subobjects of the same type. BaseType must be /// an unqualified, canonical class type. -bool CXXBasePaths::isAmbiguous(QualType BaseType) { - assert(BaseType.isCanonical() && "Base type must be the canonical type"); - assert(BaseType.hasQualifiers() == 0 && "Base type must be unqualified"); +bool CXXBasePaths::isAmbiguous(CanQualType BaseType) { + BaseType = BaseType.getUnqualifiedType(); std::pair<bool, unsigned>& Subobjects = ClassSubobjects[BaseType]; return Subobjects.second + (Subobjects.first? 1 : 0) > 1; } diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp index ffe4967..ffdcb47 100644 --- a/lib/AST/Decl.cpp +++ b/lib/AST/Decl.cpp @@ -23,16 +23,11 @@ #include "clang/AST/PrettyPrinter.h" #include "clang/Basic/Builtins.h" #include "clang/Basic/IdentifierTable.h" -#include "clang/Parse/DeclSpec.h" +#include "clang/Basic/Specifiers.h" #include "llvm/Support/ErrorHandling.h" using namespace clang; -/// \brief Return the TypeLoc wrapper for the type source info. -TypeLoc TypeSourceInfo::getTypeLoc() const { - return TypeLoc(Ty, (void*)(this + 1)); -} - //===----------------------------------------------------------------------===// // NamedDecl Implementation //===----------------------------------------------------------------------===// @@ -541,7 +536,7 @@ SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const { while (true) { TypeLoc NextTL = TL.getNextTypeLoc(); if (!NextTL) - return TL.getSourceRange().getBegin(); + return TL.getLocalSourceRange().getBegin(); TL = NextTL; } } @@ -1224,9 +1219,8 @@ FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD, } bool FunctionDecl::isImplicitlyInstantiable() const { - // If this function already has a definition or is invalid, it can't be - // implicitly instantiated. - if (isInvalidDecl() || getBody()) + // If the function is invalid, it can't be implicitly instantiated. + if (isInvalidDecl()) return false; switch (getTemplateSpecializationKind()) { @@ -1295,11 +1289,22 @@ FunctionDecl::getTemplateSpecializationArgs() const { return 0; } +const TemplateArgumentListInfo * +FunctionDecl::getTemplateSpecializationArgsAsWritten() const { + if (FunctionTemplateSpecializationInfo *Info + = TemplateOrSpecialization + .dyn_cast<FunctionTemplateSpecializationInfo*>()) { + return Info->TemplateArgumentsAsWritten; + } + return 0; +} + void FunctionDecl::setFunctionTemplateSpecialization(FunctionTemplateDecl *Template, const TemplateArgumentList *TemplateArgs, void *InsertPos, - TemplateSpecializationKind TSK) { + TemplateSpecializationKind TSK, + const TemplateArgumentListInfo *TemplateArgsAsWritten) { assert(TSK != TSK_Undeclared && "Must specify the type of function template specialization"); FunctionTemplateSpecializationInfo *Info @@ -1311,6 +1316,7 @@ FunctionDecl::setFunctionTemplateSpecialization(FunctionTemplateDecl *Template, Info->Template.setPointer(Template); Info->Template.setInt(TSK - 1); Info->TemplateArguments = TemplateArgs; + Info->TemplateArgumentsAsWritten = TemplateArgsAsWritten; TemplateOrSpecialization = Info; // Insert this function template specialization into the set of known @@ -1475,6 +1481,12 @@ TagDecl* TagDecl::getCanonicalDecl() { return getFirstDeclaration(); } +void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) { + TypedefDeclOrQualifier = TDD; + if (TypeForDecl) + TypeForDecl->ClearLinkageCache(); +} + void TagDecl::startDefinition() { if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) { TagT->decl.setPointer(this); @@ -1509,6 +1521,7 @@ void TagDecl::completeDefinition() { TypeForDecl->getAs<InjectedClassNameType>())) { assert(Injected->Decl == this && "Attempt to redefine a class template definition?"); + (void)Injected; } } @@ -1524,16 +1537,6 @@ TagDecl* TagDecl::getDefinition() const { return 0; } -TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) { - switch (TypeSpec) { - default: llvm_unreachable("unexpected type specifier"); - case DeclSpec::TST_struct: return TK_struct; - case DeclSpec::TST_class: return TK_class; - case DeclSpec::TST_union: return TK_union; - case DeclSpec::TST_enum: return TK_enum; - } -} - void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier, SourceRange QualifierRange) { if (Qualifier) { @@ -1571,10 +1574,14 @@ void EnumDecl::Destroy(ASTContext& C) { } void EnumDecl::completeDefinition(QualType NewType, - QualType NewPromotionType) { + QualType NewPromotionType, + unsigned NumPositiveBits, + unsigned NumNegativeBits) { assert(!isDefinition() && "Cannot redefine enums!"); IntegerType = NewType; PromotionType = NewPromotionType; + setNumPositiveBits(NumPositiveBits); + setNumNegativeBits(NumNegativeBits); TagDecl::completeDefinition(); } @@ -1620,6 +1627,17 @@ void RecordDecl::completeDefinition() { TagDecl::completeDefinition(); } +ValueDecl *RecordDecl::getAnonymousStructOrUnionObject() { + // Force the decl chain to come into existence properly. + if (!getNextDeclInContext()) getParent()->decls_begin(); + + assert(isAnonymousStructOrUnion()); + ValueDecl *D = cast<ValueDecl>(getNextDeclInContext()); + assert(D->getType()->isRecordType()); + assert(D->getType()->getAs<RecordType>()->getDecl() == this); + return D; +} + //===----------------------------------------------------------------------===// // BlockDecl Implementation //===----------------------------------------------------------------------===// diff --git a/lib/AST/DeclBase.cpp b/lib/AST/DeclBase.cpp index b5aec0c..42a3726 100644 --- a/lib/AST/DeclBase.cpp +++ b/lib/AST/DeclBase.cpp @@ -811,12 +811,12 @@ DeclContext::lookup(DeclarationName Name) { buildLookup(this); if (!LookupPtr) - return lookup_result(0, 0); + return lookup_result(lookup_iterator(0), lookup_iterator(0)); } StoredDeclsMap::iterator Pos = LookupPtr->find(Name); if (Pos == LookupPtr->end()) - return lookup_result(0, 0); + return lookup_result(lookup_iterator(0), lookup_iterator(0)); return Pos->second.getLookupResult(getParentASTContext()); } diff --git a/lib/AST/DeclCXX.cpp b/lib/AST/DeclCXX.cpp index 68f4a82..cd7afd9 100644 --- a/lib/AST/DeclCXX.cpp +++ b/lib/AST/DeclCXX.cpp @@ -137,7 +137,7 @@ CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases, data().VBases[I] = CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true, - VBaseClassDecl->getTagKind() == RecordDecl::TK_class, + VBaseClassDecl->getTagKind() == TTK_Class, VBases[I]->getAccessSpecifier(), VBaseType); } } @@ -700,8 +700,9 @@ CXXBaseOrMemberInitializer:: CXXBaseOrMemberInitializer(ASTContext &Context, TypeSourceInfo *TInfo, bool IsVirtual, SourceLocation L, Expr *Init, SourceLocation R) - : BaseOrMember(TInfo), Init(Init), AnonUnionMember(0), IsVirtual(IsVirtual), - LParenLoc(L), RParenLoc(R) + : BaseOrMember(TInfo), Init(Init), AnonUnionMember(0), + LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false), + SourceOrderOrNumArrayIndices(0) { } @@ -709,14 +710,46 @@ CXXBaseOrMemberInitializer:: CXXBaseOrMemberInitializer(ASTContext &Context, FieldDecl *Member, SourceLocation MemberLoc, SourceLocation L, Expr *Init, SourceLocation R) + : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init), + AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false), + IsWritten(false), SourceOrderOrNumArrayIndices(0) +{ +} + +CXXBaseOrMemberInitializer:: +CXXBaseOrMemberInitializer(ASTContext &Context, + FieldDecl *Member, SourceLocation MemberLoc, + SourceLocation L, Expr *Init, SourceLocation R, + VarDecl **Indices, + unsigned NumIndices) : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init), - AnonUnionMember(0), LParenLoc(L), RParenLoc(R) + AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false), + IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices) { + VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1); + memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *)); +} + +CXXBaseOrMemberInitializer * +CXXBaseOrMemberInitializer::Create(ASTContext &Context, + FieldDecl *Member, + SourceLocation MemberLoc, + SourceLocation L, + Expr *Init, + SourceLocation R, + VarDecl **Indices, + unsigned NumIndices) { + void *Mem = Context.Allocate(sizeof(CXXBaseOrMemberInitializer) + + sizeof(VarDecl *) * NumIndices, + llvm::alignof<CXXBaseOrMemberInitializer>()); + return new (Mem) CXXBaseOrMemberInitializer(Context, Member, MemberLoc, + L, Init, R, Indices, NumIndices); } void CXXBaseOrMemberInitializer::Destroy(ASTContext &Context) { if (Init) Init->Destroy(Context); + // FIXME: Destroy indices this->~CXXBaseOrMemberInitializer(); } @@ -745,7 +778,7 @@ SourceLocation CXXBaseOrMemberInitializer::getSourceLocation() const { if (isMemberInitializer()) return getMemberLocation(); - return getBaseClassLoc().getSourceRange().getBegin(); + return getBaseClassLoc().getLocalSourceRange().getBegin(); } SourceRange CXXBaseOrMemberInitializer::getSourceRange() const { @@ -753,6 +786,12 @@ SourceRange CXXBaseOrMemberInitializer::getSourceRange() const { } CXXConstructorDecl * +CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) { + return new (C) CXXConstructorDecl(0, SourceLocation(), DeclarationName(), + QualType(), 0, false, false, false); +} + +CXXConstructorDecl * CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD, SourceLocation L, DeclarationName N, QualType T, TypeSourceInfo *TInfo, @@ -855,6 +894,12 @@ bool CXXConstructorDecl::isCopyConstructorLikeSpecialization() const { } CXXDestructorDecl * +CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) { + return new (C) CXXDestructorDecl(0, SourceLocation(), DeclarationName(), + QualType(), false, false); +} + +CXXDestructorDecl * CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD, SourceLocation L, DeclarationName N, QualType T, bool isInline, @@ -871,6 +916,12 @@ CXXConstructorDecl::Destroy(ASTContext& C) { } CXXConversionDecl * +CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) { + return new (C) CXXConversionDecl(0, SourceLocation(), DeclarationName(), + QualType(), 0, false, false); +} + +CXXConversionDecl * CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD, SourceLocation L, DeclarationName N, QualType T, TypeSourceInfo *TInfo, @@ -908,6 +959,12 @@ NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() { return cast_or_null<NamespaceDecl>(NominatedNamespace); } +void UsingDirectiveDecl::setNominatedNamespace(NamedDecl* ND) { + assert((isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) && + "expected a NamespaceDecl or NamespaceAliasDecl"); + NominatedNamespace = ND; +} + NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, SourceLocation AliasLoc, diff --git a/lib/AST/DeclTemplate.cpp b/lib/AST/DeclTemplate.cpp index c498dea..26e291c 100644 --- a/lib/AST/DeclTemplate.cpp +++ b/lib/AST/DeclTemplate.cpp @@ -94,6 +94,10 @@ TemplateDecl::~TemplateDecl() { // FunctionTemplateDecl Implementation //===----------------------------------------------------------------------===// +void FunctionTemplateDecl::DeallocateCommon(void *Ptr) { + static_cast<Common *>(Ptr)->~Common(); +} + FunctionTemplateDecl *FunctionTemplateDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, @@ -129,8 +133,9 @@ FunctionTemplateDecl::Common *FunctionTemplateDecl::getCommonPtr() { First = First->getPreviousDeclaration(); if (First->CommonOrPrev.isNull()) { - // FIXME: Allocate with the ASTContext - First->CommonOrPrev = new Common; + Common *CommonPtr = new (getASTContext()) Common; + getASTContext().AddDeallocation(DeallocateCommon, CommonPtr); + First->CommonOrPrev = CommonPtr; } return First->CommonOrPrev.get<Common*>(); } @@ -139,6 +144,10 @@ FunctionTemplateDecl::Common *FunctionTemplateDecl::getCommonPtr() { // ClassTemplateDecl Implementation //===----------------------------------------------------------------------===// +void ClassTemplateDecl::DeallocateCommon(void *Ptr) { + static_cast<Common *>(Ptr)->~Common(); +} + ClassTemplateDecl *ClassTemplateDecl::getCanonicalDecl() { ClassTemplateDecl *Template = this; while (Template->getPreviousDeclaration()) @@ -156,8 +165,10 @@ ClassTemplateDecl *ClassTemplateDecl::Create(ASTContext &C, Common *CommonPtr; if (PrevDecl) CommonPtr = PrevDecl->CommonPtr; - else + else { CommonPtr = new (C) Common; + C.AddDeallocation(DeallocateCommon, CommonPtr); + } return new (C) ClassTemplateDecl(DC, L, Name, Params, Decl, PrevDecl, CommonPtr); @@ -259,7 +270,7 @@ TemplateTypeParmDecl::Create(ASTContext &C, DeclContext *DC, } SourceLocation TemplateTypeParmDecl::getDefaultArgumentLoc() const { - return DefaultArgument->getTypeLoc().getFullSourceRange().getBegin(); + return DefaultArgument->getTypeLoc().getSourceRange().getBegin(); } unsigned TemplateTypeParmDecl::getDepth() const { @@ -303,22 +314,14 @@ TemplateTemplateParmDecl::Create(ASTContext &C, DeclContext *DC, // TemplateArgumentListBuilder Implementation //===----------------------------------------------------------------------===// -void TemplateArgumentListBuilder::Append(const TemplateArgument& Arg) { - switch (Arg.getKind()) { - default: break; - case TemplateArgument::Type: - assert(Arg.getAsType().isCanonical() && "Type must be canonical!"); - break; - } - - assert(NumFlatArgs < MaxFlatArgs && "Argument list builder is full!"); +void TemplateArgumentListBuilder::Append(const TemplateArgument &Arg) { + assert((Arg.getKind() != TemplateArgument::Type || + Arg.getAsType().isCanonical()) && "Type must be canonical!"); + assert(FlatArgs.size() < MaxFlatArgs && "Argument list builder is full!"); assert(!StructuredArgs && "Can't append arguments when an argument pack has been added!"); - if (!FlatArgs) - FlatArgs = new TemplateArgument[MaxFlatArgs]; - - FlatArgs[NumFlatArgs++] = Arg; + FlatArgs.push_back(Arg); } void TemplateArgumentListBuilder::BeginPack() { @@ -326,7 +329,7 @@ void TemplateArgumentListBuilder::BeginPack() { assert(!StructuredArgs && "Argument list already contains a pack!"); AddingToPack = true; - PackBeginIndex = NumFlatArgs; + PackBeginIndex = FlatArgs.size(); } void TemplateArgumentListBuilder::EndPack() { @@ -335,6 +338,7 @@ void TemplateArgumentListBuilder::EndPack() { AddingToPack = false; + // FIXME: This is a memory leak! StructuredArgs = new TemplateArgument[MaxStructuredArgs]; // First copy the flat entries over to the list (if any) @@ -346,22 +350,14 @@ void TemplateArgumentListBuilder::EndPack() { // Next, set the pack. TemplateArgument *PackArgs = 0; unsigned NumPackArgs = NumFlatArgs - PackBeginIndex; + // FIXME: NumPackArgs shouldn't be negative here??? if (NumPackArgs) - PackArgs = &FlatArgs[PackBeginIndex]; + PackArgs = FlatArgs.data()+PackBeginIndex; StructuredArgs[NumStructuredArgs++].setArgumentPack(PackArgs, NumPackArgs, /*CopyArgs=*/false); } -void TemplateArgumentListBuilder::ReleaseArgs() { - FlatArgs = 0; - NumFlatArgs = 0; - MaxFlatArgs = 0; - StructuredArgs = 0; - NumStructuredArgs = 0; - MaxStructuredArgs = 0; -} - //===----------------------------------------------------------------------===// // TemplateArgumentList Implementation //===----------------------------------------------------------------------===// @@ -376,35 +372,56 @@ TemplateArgumentList::TemplateArgumentList(ASTContext &Context, if (!TakeArgs) return; - if (Builder.getStructuredArguments() == Builder.getFlatArguments()) + // If this does take ownership of the arguments, then we have to new them + // and copy over. + TemplateArgument *NewArgs = + new (Context) TemplateArgument[Builder.flatSize()]; + std::copy(Builder.getFlatArguments(), + Builder.getFlatArguments()+Builder.flatSize(), NewArgs); + FlatArguments.setPointer(NewArgs); + + // Just reuse the structured and flat arguments array if possible. + if (Builder.getStructuredArguments() == Builder.getFlatArguments()) { + StructuredArguments.setPointer(NewArgs); StructuredArguments.setInt(0); - Builder.ReleaseArgs(); + } else { + TemplateArgument *NewSArgs = + new (Context) TemplateArgument[Builder.flatSize()]; + std::copy(Builder.getFlatArguments(), + Builder.getFlatArguments()+Builder.flatSize(), NewSArgs); + StructuredArguments.setPointer(NewSArgs); + } } -TemplateArgumentList::TemplateArgumentList(const TemplateArgumentList &Other) - : FlatArguments(Other.FlatArguments.getPointer(), 1), - NumFlatArguments(Other.flat_size()), - StructuredArguments(Other.StructuredArguments.getPointer(), 1), - NumStructuredArguments(Other.NumStructuredArguments) { } +/// Produces a shallow copy of the given template argument list. This +/// assumes that the input argument list outlives it. This takes the list as +/// a pointer to avoid looking like a copy constructor, since this really +/// really isn't safe to use that way. +TemplateArgumentList::TemplateArgumentList(const TemplateArgumentList *Other) + : FlatArguments(Other->FlatArguments.getPointer(), false), + NumFlatArguments(Other->flat_size()), + StructuredArguments(Other->StructuredArguments.getPointer(), false), + NumStructuredArguments(Other->NumStructuredArguments) { } -TemplateArgumentList::~TemplateArgumentList() { - // FIXME: Deallocate template arguments +void TemplateArgumentList::Destroy(ASTContext &C) { + if (FlatArguments.getInt()) + C.Deallocate((void*)FlatArguments.getPointer()); + if (StructuredArguments.getInt()) + C.Deallocate((void*)StructuredArguments.getPointer()); } +TemplateArgumentList::~TemplateArgumentList() {} + //===----------------------------------------------------------------------===// // ClassTemplateSpecializationDecl Implementation //===----------------------------------------------------------------------===// ClassTemplateSpecializationDecl:: -ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, +ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK, DeclContext *DC, SourceLocation L, ClassTemplateDecl *SpecializedTemplate, TemplateArgumentListBuilder &Builder, ClassTemplateSpecializationDecl *PrevDecl) - : CXXRecordDecl(DK, - SpecializedTemplate->getTemplatedDecl()->getTagKind(), - DC, L, - // FIXME: Should we use DeclarationName for the name of - // class template specializations? + : CXXRecordDecl(DK, TK, DC, L, SpecializedTemplate->getIdentifier(), PrevDecl), SpecializedTemplate(SpecializedTemplate), @@ -414,7 +431,7 @@ ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, } ClassTemplateSpecializationDecl * -ClassTemplateSpecializationDecl::Create(ASTContext &Context, +ClassTemplateSpecializationDecl::Create(ASTContext &Context, TagKind TK, DeclContext *DC, SourceLocation L, ClassTemplateDecl *SpecializedTemplate, TemplateArgumentListBuilder &Builder, @@ -422,7 +439,7 @@ ClassTemplateSpecializationDecl::Create(ASTContext &Context, ClassTemplateSpecializationDecl *Result = new (Context)ClassTemplateSpecializationDecl(Context, ClassTemplateSpecialization, - DC, L, + TK, DC, L, SpecializedTemplate, Builder, PrevDecl); @@ -464,7 +481,7 @@ ClassTemplateSpecializationDecl::getSpecializedTemplate() const { //===----------------------------------------------------------------------===// ClassTemplatePartialSpecializationDecl * ClassTemplatePartialSpecializationDecl:: -Create(ASTContext &Context, DeclContext *DC, SourceLocation L, +Create(ASTContext &Context, TagKind TK,DeclContext *DC, SourceLocation L, TemplateParameterList *Params, ClassTemplateDecl *SpecializedTemplate, TemplateArgumentListBuilder &Builder, @@ -478,7 +495,7 @@ Create(ASTContext &Context, DeclContext *DC, SourceLocation L, ClonedArgs[I] = ArgInfos[I]; ClassTemplatePartialSpecializationDecl *Result - = new (Context)ClassTemplatePartialSpecializationDecl(Context, + = new (Context)ClassTemplatePartialSpecializationDecl(Context, TK, DC, L, Params, SpecializedTemplate, Builder, diff --git a/lib/AST/DeclarationName.cpp b/lib/AST/DeclarationName.cpp index 4f85fca..343d403 100644 --- a/lib/AST/DeclarationName.cpp +++ b/lib/AST/DeclarationName.cpp @@ -11,10 +11,11 @@ // classes. // //===----------------------------------------------------------------------===// +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" #include "clang/AST/DeclarationName.h" #include "clang/AST/Type.h" #include "clang/AST/TypeOrdering.h" -#include "clang/AST/Decl.h" #include "clang/Basic/IdentifierTable.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/FoldingSet.h" @@ -383,12 +384,12 @@ void DeclarationName::dump() const { llvm::errs() << '\n'; } -DeclarationNameTable::DeclarationNameTable() { +DeclarationNameTable::DeclarationNameTable(ASTContext &C) : Ctx(C) { CXXSpecialNamesImpl = new llvm::FoldingSet<CXXSpecialName>; CXXLiteralOperatorNames = new llvm::FoldingSet<CXXLiteralOperatorIdName>; // Initialize the overloaded operator names. - CXXOperatorNames = new CXXOperatorIdName[NUM_OVERLOADED_OPERATORS]; + CXXOperatorNames = new (Ctx) CXXOperatorIdName[NUM_OVERLOADED_OPERATORS]; for (unsigned Op = 0; Op < NUM_OVERLOADED_OPERATORS; ++Op) { CXXOperatorNames[Op].ExtraKindOrNumArgs = Op + DeclarationNameExtra::CXXConversionFunction; @@ -399,29 +400,32 @@ DeclarationNameTable::DeclarationNameTable() { DeclarationNameTable::~DeclarationNameTable() { llvm::FoldingSet<CXXSpecialName> *SpecialNames = static_cast<llvm::FoldingSet<CXXSpecialName>*>(CXXSpecialNamesImpl); - llvm::FoldingSetIterator<CXXSpecialName> - SI = SpecialNames->begin(), SE = SpecialNames->end(); + llvm::FoldingSet<CXXLiteralOperatorIdName> *LiteralNames + = static_cast<llvm::FoldingSet<CXXLiteralOperatorIdName>*> + (CXXLiteralOperatorNames); - while (SI != SE) { - CXXSpecialName *n = &*SI++; - delete n; - } + if (Ctx.FreeMemory) { + llvm::FoldingSetIterator<CXXSpecialName> + SI = SpecialNames->begin(), SE = SpecialNames->end(); + while (SI != SE) { + CXXSpecialName *n = &*SI++; + Ctx.Deallocate(n); + } - llvm::FoldingSet<CXXLiteralOperatorIdName> *LiteralNames - = static_cast<llvm::FoldingSet<CXXLiteralOperatorIdName>*> - (CXXLiteralOperatorNames); - llvm::FoldingSetIterator<CXXLiteralOperatorIdName> - LI = LiteralNames->begin(), LE = LiteralNames->end(); + llvm::FoldingSetIterator<CXXLiteralOperatorIdName> + LI = LiteralNames->begin(), LE = LiteralNames->end(); + + while (LI != LE) { + CXXLiteralOperatorIdName *n = &*LI++; + Ctx.Deallocate(n); + } - while (LI != LE) { - CXXLiteralOperatorIdName *n = &*LI++; - delete n; + Ctx.Deallocate(CXXOperatorNames); } delete SpecialNames; delete LiteralNames; - delete [] CXXOperatorNames; } DeclarationName @@ -459,7 +463,7 @@ DeclarationNameTable::getCXXSpecialName(DeclarationName::NameKind Kind, if (CXXSpecialName *Name = SpecialNames->FindNodeOrInsertPos(ID, InsertPos)) return DeclarationName(Name); - CXXSpecialName *SpecialName = new CXXSpecialName; + CXXSpecialName *SpecialName = new (Ctx) CXXSpecialName; SpecialName->ExtraKindOrNumArgs = EKind; SpecialName->Type = Ty; SpecialName->FETokenInfo = 0; @@ -487,7 +491,7 @@ DeclarationNameTable::getCXXLiteralOperatorName(IdentifierInfo *II) { LiteralNames->FindNodeOrInsertPos(ID, InsertPos)) return DeclarationName (Name); - CXXLiteralOperatorIdName *LiteralName = new CXXLiteralOperatorIdName; + CXXLiteralOperatorIdName *LiteralName = new (Ctx) CXXLiteralOperatorIdName; LiteralName->ExtraKindOrNumArgs = DeclarationNameExtra::CXXLiteralOperator; LiteralName->ID = II; diff --git a/lib/AST/Expr.cpp b/lib/AST/Expr.cpp index 00662a5..c38cec3 100644 --- a/lib/AST/Expr.cpp +++ b/lib/AST/Expr.cpp @@ -27,6 +27,8 @@ #include <algorithm> using namespace clang; +void Expr::ANCHOR() {} // key function for Expr class. + /// isKnownToHaveBooleanValue - Return true if this is an integer expression /// that is known to return 0 or 1. This happens for _Bool/bool expressions /// but also int expressions which are produced by things like comparisons in @@ -161,8 +163,19 @@ void DeclRefExpr::computeDependence() { if (const Expr *Init = Var->getAnyInitializer()) if (Init->isValueDependent()) ValueDependent = true; - } - } + } + // (VD) - FIXME: Missing from the standard: + // - a member function or a static data member of the current + // instantiation + else if (Var->isStaticDataMember() && + Var->getDeclContext()->isDependentContext()) + ValueDependent = true; + } + // (VD) - FIXME: Missing from the standard: + // - a member function or a static data member of the current + // instantiation + else if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) + ValueDependent = true; // (TD) - a nested-name-specifier or a qualified-id that names a // member of an unknown specialization. // (handled by DependentScopeDeclRefExpr) @@ -976,6 +989,7 @@ bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1, return true; } case CompoundAssignOperatorClass: + case VAArgExprClass: return false; case ConditionalOperatorClass: { @@ -1557,6 +1571,18 @@ Expr *Expr::IgnoreParenCasts() { } } +Expr *Expr::IgnoreParenImpCasts() { + Expr *E = this; + while (true) { + if (ParenExpr *P = dyn_cast<ParenExpr>(E)) + E = P->getSubExpr(); + else if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) + E = P->getSubExpr(); + else + return E; + } +} + /// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the /// value (including ptr->int casts of the same size). Strip off any /// ParenExpr or CastExprs, returning their operand. @@ -1757,385 +1783,6 @@ bool Expr::isConstantInitializer(ASTContext &Ctx) const { return isEvaluatable(Ctx); } -/// isIntegerConstantExpr - this recursive routine will test if an expression is -/// an integer constant expression. - -/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, -/// comma, etc -/// -/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof -/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer -/// cast+dereference. - -// CheckICE - This function does the fundamental ICE checking: the returned -// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation. -// Note that to reduce code duplication, this helper does no evaluation -// itself; the caller checks whether the expression is evaluatable, and -// in the rare cases where CheckICE actually cares about the evaluated -// value, it calls into Evalute. -// -// Meanings of Val: -// 0: This expression is an ICE if it can be evaluated by Evaluate. -// 1: This expression is not an ICE, but if it isn't evaluated, it's -// a legal subexpression for an ICE. This return value is used to handle -// the comma operator in C99 mode. -// 2: This expression is not an ICE, and is not a legal subexpression for one. - -struct ICEDiag { - unsigned Val; - SourceLocation Loc; - - public: - ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {} - ICEDiag() : Val(0) {} -}; - -ICEDiag NoDiag() { return ICEDiag(); } - -static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) { - Expr::EvalResult EVResult; - if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects || - !EVResult.Val.isInt()) { - return ICEDiag(2, E->getLocStart()); - } - return NoDiag(); -} - -static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) { - assert(!E->isValueDependent() && "Should not see value dependent exprs!"); - if (!E->getType()->isIntegralType()) { - return ICEDiag(2, E->getLocStart()); - } - - switch (E->getStmtClass()) { -#define STMT(Node, Base) case Expr::Node##Class: -#define EXPR(Node, Base) -#include "clang/AST/StmtNodes.def" - case Expr::PredefinedExprClass: - case Expr::FloatingLiteralClass: - case Expr::ImaginaryLiteralClass: - case Expr::StringLiteralClass: - case Expr::ArraySubscriptExprClass: - case Expr::MemberExprClass: - case Expr::CompoundAssignOperatorClass: - case Expr::CompoundLiteralExprClass: - case Expr::ExtVectorElementExprClass: - case Expr::InitListExprClass: - case Expr::DesignatedInitExprClass: - case Expr::ImplicitValueInitExprClass: - case Expr::ParenListExprClass: - case Expr::VAArgExprClass: - case Expr::AddrLabelExprClass: - case Expr::StmtExprClass: - case Expr::CXXMemberCallExprClass: - case Expr::CXXDynamicCastExprClass: - case Expr::CXXTypeidExprClass: - case Expr::CXXNullPtrLiteralExprClass: - case Expr::CXXThisExprClass: - case Expr::CXXThrowExprClass: - case Expr::CXXNewExprClass: - case Expr::CXXDeleteExprClass: - case Expr::CXXPseudoDestructorExprClass: - case Expr::UnresolvedLookupExprClass: - case Expr::DependentScopeDeclRefExprClass: - case Expr::CXXConstructExprClass: - case Expr::CXXBindTemporaryExprClass: - case Expr::CXXBindReferenceExprClass: - case Expr::CXXExprWithTemporariesClass: - case Expr::CXXTemporaryObjectExprClass: - case Expr::CXXUnresolvedConstructExprClass: - case Expr::CXXDependentScopeMemberExprClass: - case Expr::UnresolvedMemberExprClass: - case Expr::ObjCStringLiteralClass: - case Expr::ObjCEncodeExprClass: - case Expr::ObjCMessageExprClass: - case Expr::ObjCSelectorExprClass: - case Expr::ObjCProtocolExprClass: - case Expr::ObjCIvarRefExprClass: - case Expr::ObjCPropertyRefExprClass: - case Expr::ObjCImplicitSetterGetterRefExprClass: - case Expr::ObjCSuperExprClass: - case Expr::ObjCIsaExprClass: - case Expr::ShuffleVectorExprClass: - case Expr::BlockExprClass: - case Expr::BlockDeclRefExprClass: - case Expr::NoStmtClass: - return ICEDiag(2, E->getLocStart()); - - case Expr::GNUNullExprClass: - // GCC considers the GNU __null value to be an integral constant expression. - return NoDiag(); - - case Expr::ParenExprClass: - return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx); - case Expr::IntegerLiteralClass: - case Expr::CharacterLiteralClass: - case Expr::CXXBoolLiteralExprClass: - case Expr::CXXZeroInitValueExprClass: - case Expr::TypesCompatibleExprClass: - case Expr::UnaryTypeTraitExprClass: - return NoDiag(); - case Expr::CallExprClass: - case Expr::CXXOperatorCallExprClass: { - const CallExpr *CE = cast<CallExpr>(E); - if (CE->isBuiltinCall(Ctx)) - return CheckEvalInICE(E, Ctx); - return ICEDiag(2, E->getLocStart()); - } - case Expr::DeclRefExprClass: - if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl())) - return NoDiag(); - if (Ctx.getLangOptions().CPlusPlus && - E->getType().getCVRQualifiers() == Qualifiers::Const) { - const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl(); - - // Parameter variables are never constants. Without this check, - // getAnyInitializer() can find a default argument, which leads - // to chaos. - if (isa<ParmVarDecl>(D)) - return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); - - // C++ 7.1.5.1p2 - // A variable of non-volatile const-qualified integral or enumeration - // type initialized by an ICE can be used in ICEs. - if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) { - Qualifiers Quals = Ctx.getCanonicalType(Dcl->getType()).getQualifiers(); - if (Quals.hasVolatile() || !Quals.hasConst()) - return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); - - // Look for a declaration of this variable that has an initializer. - const VarDecl *ID = 0; - const Expr *Init = Dcl->getAnyInitializer(ID); - if (Init) { - if (ID->isInitKnownICE()) { - // We have already checked whether this subexpression is an - // integral constant expression. - if (ID->isInitICE()) - return NoDiag(); - else - return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); - } - - // It's an ICE whether or not the definition we found is - // out-of-line. See DR 721 and the discussion in Clang PR - // 6206 for details. - - if (Dcl->isCheckingICE()) { - return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); - } - - Dcl->setCheckingICE(); - ICEDiag Result = CheckICE(Init, Ctx); - // Cache the result of the ICE test. - Dcl->setInitKnownICE(Result.Val == 0); - return Result; - } - } - } - return ICEDiag(2, E->getLocStart()); - case Expr::UnaryOperatorClass: { - const UnaryOperator *Exp = cast<UnaryOperator>(E); - switch (Exp->getOpcode()) { - case UnaryOperator::PostInc: - case UnaryOperator::PostDec: - case UnaryOperator::PreInc: - case UnaryOperator::PreDec: - case UnaryOperator::AddrOf: - case UnaryOperator::Deref: - return ICEDiag(2, E->getLocStart()); - case UnaryOperator::Extension: - case UnaryOperator::LNot: - case UnaryOperator::Plus: - case UnaryOperator::Minus: - case UnaryOperator::Not: - case UnaryOperator::Real: - case UnaryOperator::Imag: - return CheckICE(Exp->getSubExpr(), Ctx); - case UnaryOperator::OffsetOf: - break; - } - - // OffsetOf falls through here. - } - case Expr::OffsetOfExprClass: { - // Note that per C99, offsetof must be an ICE. And AFAIK, using - // Evaluate matches the proposed gcc behavior for cases like - // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect - // compliance: we should warn earlier for offsetof expressions with - // array subscripts that aren't ICEs, and if the array subscripts - // are ICEs, the value of the offsetof must be an integer constant. - return CheckEvalInICE(E, Ctx); - } - case Expr::SizeOfAlignOfExprClass: { - const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E); - if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType()) - return ICEDiag(2, E->getLocStart()); - return NoDiag(); - } - case Expr::BinaryOperatorClass: { - const BinaryOperator *Exp = cast<BinaryOperator>(E); - switch (Exp->getOpcode()) { - case BinaryOperator::PtrMemD: - case BinaryOperator::PtrMemI: - case BinaryOperator::Assign: - case BinaryOperator::MulAssign: - case BinaryOperator::DivAssign: - case BinaryOperator::RemAssign: - case BinaryOperator::AddAssign: - case BinaryOperator::SubAssign: - case BinaryOperator::ShlAssign: - case BinaryOperator::ShrAssign: - case BinaryOperator::AndAssign: - case BinaryOperator::XorAssign: - case BinaryOperator::OrAssign: - return ICEDiag(2, E->getLocStart()); - - case BinaryOperator::Mul: - case BinaryOperator::Div: - case BinaryOperator::Rem: - case BinaryOperator::Add: - case BinaryOperator::Sub: - case BinaryOperator::Shl: - case BinaryOperator::Shr: - case BinaryOperator::LT: - case BinaryOperator::GT: - case BinaryOperator::LE: - case BinaryOperator::GE: - case BinaryOperator::EQ: - case BinaryOperator::NE: - case BinaryOperator::And: - case BinaryOperator::Xor: - case BinaryOperator::Or: - case BinaryOperator::Comma: { - ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); - ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); - if (Exp->getOpcode() == BinaryOperator::Div || - Exp->getOpcode() == BinaryOperator::Rem) { - // Evaluate gives an error for undefined Div/Rem, so make sure - // we don't evaluate one. - if (LHSResult.Val != 2 && RHSResult.Val != 2) { - llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx); - if (REval == 0) - return ICEDiag(1, E->getLocStart()); - if (REval.isSigned() && REval.isAllOnesValue()) { - llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx); - if (LEval.isMinSignedValue()) - return ICEDiag(1, E->getLocStart()); - } - } - } - if (Exp->getOpcode() == BinaryOperator::Comma) { - if (Ctx.getLangOptions().C99) { - // C99 6.6p3 introduces a strange edge case: comma can be in an ICE - // if it isn't evaluated. - if (LHSResult.Val == 0 && RHSResult.Val == 0) - return ICEDiag(1, E->getLocStart()); - } else { - // In both C89 and C++, commas in ICEs are illegal. - return ICEDiag(2, E->getLocStart()); - } - } - if (LHSResult.Val >= RHSResult.Val) - return LHSResult; - return RHSResult; - } - case BinaryOperator::LAnd: - case BinaryOperator::LOr: { - ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); - ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); - if (LHSResult.Val == 0 && RHSResult.Val == 1) { - // Rare case where the RHS has a comma "side-effect"; we need - // to actually check the condition to see whether the side - // with the comma is evaluated. - if ((Exp->getOpcode() == BinaryOperator::LAnd) != - (Exp->getLHS()->EvaluateAsInt(Ctx) == 0)) - return RHSResult; - return NoDiag(); - } - - if (LHSResult.Val >= RHSResult.Val) - return LHSResult; - return RHSResult; - } - } - } - case Expr::ImplicitCastExprClass: - case Expr::CStyleCastExprClass: - case Expr::CXXFunctionalCastExprClass: - case Expr::CXXStaticCastExprClass: - case Expr::CXXReinterpretCastExprClass: - case Expr::CXXConstCastExprClass: { - const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr(); - if (SubExpr->getType()->isIntegralType()) - return CheckICE(SubExpr, Ctx); - if (isa<FloatingLiteral>(SubExpr->IgnoreParens())) - return NoDiag(); - return ICEDiag(2, E->getLocStart()); - } - case Expr::ConditionalOperatorClass: { - const ConditionalOperator *Exp = cast<ConditionalOperator>(E); - // If the condition (ignoring parens) is a __builtin_constant_p call, - // then only the true side is actually considered in an integer constant - // expression, and it is fully evaluated. This is an important GNU - // extension. See GCC PR38377 for discussion. - if (const CallExpr *CallCE - = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts())) - if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) { - Expr::EvalResult EVResult; - if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects || - !EVResult.Val.isInt()) { - return ICEDiag(2, E->getLocStart()); - } - return NoDiag(); - } - ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx); - ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx); - ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); - if (CondResult.Val == 2) - return CondResult; - if (TrueResult.Val == 2) - return TrueResult; - if (FalseResult.Val == 2) - return FalseResult; - if (CondResult.Val == 1) - return CondResult; - if (TrueResult.Val == 0 && FalseResult.Val == 0) - return NoDiag(); - // Rare case where the diagnostics depend on which side is evaluated - // Note that if we get here, CondResult is 0, and at least one of - // TrueResult and FalseResult is non-zero. - if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) { - return FalseResult; - } - return TrueResult; - } - case Expr::CXXDefaultArgExprClass: - return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx); - case Expr::ChooseExprClass: { - return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx); - } - } - - // Silence a GCC warning - return ICEDiag(2, E->getLocStart()); -} - -bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx, - SourceLocation *Loc, bool isEvaluated) const { - ICEDiag d = CheckICE(this, Ctx); - if (d.Val != 0) { - if (Loc) *Loc = d.Loc; - return false; - } - EvalResult EvalResult; - if (!Evaluate(EvalResult, Ctx)) - llvm_unreachable("ICE cannot be evaluated!"); - assert(!EvalResult.HasSideEffects && "ICE with side effects!"); - assert(EvalResult.Val.isInt() && "ICE that isn't integer!"); - Result = EvalResult.Val.getInt(); - return true; -} - /// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an /// integer constant expression with the value zero, or if this is one that is /// cast to void*. @@ -2433,9 +2080,9 @@ ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const { break; case Class: - if (const ObjCInterfaceType *Iface - = getClassReceiver()->getAs<ObjCInterfaceType>()) - return Iface->getDecl(); + if (const ObjCObjectType *Ty + = getClassReceiver()->getAs<ObjCObjectType>()) + return Ty->getInterface(); break; case SuperInstance: @@ -2712,7 +2359,9 @@ Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; } // ObjCImplicitSetterGetterRefExpr Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() { - return &Base; + // If this is accessing a class member, skip that entry. + if (Base) return &Base; + return &Base+1; } Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() { return &Base+1; diff --git a/lib/AST/ExprCXX.cpp b/lib/AST/ExprCXX.cpp index 2e03beb..d1a2b26 100644 --- a/lib/AST/ExprCXX.cpp +++ b/lib/AST/ExprCXX.cpp @@ -92,12 +92,11 @@ CXXNewExpr::CXXNewExpr(ASTContext &C, bool globalNew, FunctionDecl *operatorNew, SourceLocation startLoc, SourceLocation endLoc) : Expr(CXXNewExprClass, ty, ty->isDependentType(), ty->isDependentType()), GlobalNew(globalNew), ParenTypeId(parenTypeId), - Initializer(initializer), Array(arraySize), NumPlacementArgs(numPlaceArgs), - NumConstructorArgs(numConsArgs), OperatorNew(operatorNew), + Initializer(initializer), SubExprs(0), OperatorNew(operatorNew), OperatorDelete(operatorDelete), Constructor(constructor), StartLoc(startLoc), EndLoc(endLoc) { - unsigned TotalSize = Array + NumPlacementArgs + NumConstructorArgs; - SubExprs = new (C) Stmt*[TotalSize]; + + AllocateArgsArray(C, arraySize != 0, numPlaceArgs, numConsArgs); unsigned i = 0; if (Array) SubExprs[i++] = arraySize; @@ -105,9 +104,20 @@ CXXNewExpr::CXXNewExpr(ASTContext &C, bool globalNew, FunctionDecl *operatorNew, SubExprs[i++] = placementArgs[j]; for (unsigned j = 0; j < NumConstructorArgs; ++j) SubExprs[i++] = constructorArgs[j]; - assert(i == TotalSize); } +void CXXNewExpr::AllocateArgsArray(ASTContext &C, bool isArray, + unsigned numPlaceArgs, unsigned numConsArgs){ + assert(SubExprs == 0 && "SubExprs already allocated"); + Array = isArray; + NumPlacementArgs = numPlaceArgs; + NumConstructorArgs = numConsArgs; + + unsigned TotalSize = Array + NumPlacementArgs + NumConstructorArgs; + SubExprs = new (C) Stmt*[TotalSize]; +} + + void CXXNewExpr::DoDestroy(ASTContext &C) { DestroyChildren(C); if (SubExprs) @@ -134,7 +144,7 @@ Stmt::child_iterator CXXPseudoDestructorExpr::child_end() { PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info) : Type(Info) { - Location = Info->getTypeLoc().getSourceRange().getBegin(); + Location = Info->getTypeLoc().getLocalSourceRange().getBegin(); } QualType CXXPseudoDestructorExpr::getDestroyedType() const { @@ -147,7 +157,7 @@ QualType CXXPseudoDestructorExpr::getDestroyedType() const { SourceRange CXXPseudoDestructorExpr::getSourceRange() const { SourceLocation End = DestroyedType.getLocation(); if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo()) - End = TInfo->getTypeLoc().getSourceRange().getEnd(); + End = TInfo->getTypeLoc().getLocalSourceRange().getEnd(); return SourceRange(Base->getLocStart(), End); } @@ -159,23 +169,47 @@ UnresolvedLookupExpr::Create(ASTContext &C, bool Dependent, NestedNameSpecifier *Qualifier, SourceRange QualifierRange, DeclarationName Name, SourceLocation NameLoc, bool ADL, - const TemplateArgumentListInfo &Args) + const TemplateArgumentListInfo &Args, + UnresolvedSetIterator Begin, + UnresolvedSetIterator End) { void *Mem = C.Allocate(sizeof(UnresolvedLookupExpr) + ExplicitTemplateArgumentList::sizeFor(Args)); UnresolvedLookupExpr *ULE - = new (Mem) UnresolvedLookupExpr(Dependent ? C.DependentTy : C.OverloadTy, + = new (Mem) UnresolvedLookupExpr(C, + Dependent ? C.DependentTy : C.OverloadTy, Dependent, NamingClass, Qualifier, QualifierRange, Name, NameLoc, ADL, /*Overload*/ true, - /*ExplicitTemplateArgs*/ true); + /*ExplicitTemplateArgs*/ true, + Begin, End); reinterpret_cast<ExplicitTemplateArgumentList*>(ULE+1)->initializeFrom(Args); return ULE; } +OverloadExpr::OverloadExpr(StmtClass K, ASTContext &C, QualType T, + bool Dependent, NestedNameSpecifier *Qualifier, + SourceRange QRange, DeclarationName Name, + SourceLocation NameLoc, bool HasTemplateArgs, + UnresolvedSetIterator Begin, + UnresolvedSetIterator End) + : Expr(K, T, Dependent, Dependent), + Results(0), NumResults(End - Begin), Name(Name), Qualifier(Qualifier), + QualifierRange(QRange), NameLoc(NameLoc), + HasExplicitTemplateArgs(HasTemplateArgs) +{ + if (NumResults) { + Results = static_cast<DeclAccessPair *>( + C.Allocate(sizeof(DeclAccessPair) * NumResults, + llvm::alignof<DeclAccessPair>())); + memcpy(Results, &*Begin.getIterator(), + (End - Begin) * sizeof(DeclAccessPair)); + } +} + bool OverloadExpr::ComputeDependence(UnresolvedSetIterator Begin, UnresolvedSetIterator End, const TemplateArgumentListInfo *Args) { @@ -517,35 +551,43 @@ void CXXConstructExpr::DoDestroy(ASTContext &C) { C.Deallocate(this); } -CXXExprWithTemporaries::CXXExprWithTemporaries(Expr *subexpr, +CXXExprWithTemporaries::CXXExprWithTemporaries(ASTContext &C, + Expr *subexpr, CXXTemporary **temps, unsigned numtemps) -: Expr(CXXExprWithTemporariesClass, subexpr->getType(), + : Expr(CXXExprWithTemporariesClass, subexpr->getType(), subexpr->isTypeDependent(), subexpr->isValueDependent()), - SubExpr(subexpr), Temps(0), NumTemps(numtemps) { - if (NumTemps > 0) { - Temps = new CXXTemporary*[NumTemps]; - for (unsigned i = 0; i < NumTemps; ++i) + SubExpr(subexpr), Temps(0), NumTemps(0) { + if (numtemps) { + setNumTemporaries(C, numtemps); + for (unsigned i = 0; i != numtemps; ++i) Temps[i] = temps[i]; } } +void CXXExprWithTemporaries::setNumTemporaries(ASTContext &C, unsigned N) { + assert(Temps == 0 && "Cannot resize with this"); + NumTemps = N; + Temps = new (C) CXXTemporary*[NumTemps]; +} + + CXXExprWithTemporaries *CXXExprWithTemporaries::Create(ASTContext &C, Expr *SubExpr, CXXTemporary **Temps, unsigned NumTemps) { - return new (C) CXXExprWithTemporaries(SubExpr, Temps, NumTemps); + return new (C) CXXExprWithTemporaries(C, SubExpr, Temps, NumTemps); } void CXXExprWithTemporaries::DoDestroy(ASTContext &C) { DestroyChildren(C); + if (Temps) + C.Deallocate(Temps); this->~CXXExprWithTemporaries(); C.Deallocate(this); } -CXXExprWithTemporaries::~CXXExprWithTemporaries() { - delete[] Temps; -} +CXXExprWithTemporaries::~CXXExprWithTemporaries() {} // CXXBindTemporaryExpr Stmt::child_iterator CXXBindTemporaryExpr::child_begin() { @@ -682,7 +724,8 @@ Stmt::child_iterator CXXDependentScopeMemberExpr::child_end() { return child_iterator(&Base + 1); } -UnresolvedMemberExpr::UnresolvedMemberExpr(QualType T, bool Dependent, +UnresolvedMemberExpr::UnresolvedMemberExpr(ASTContext &C, QualType T, + bool Dependent, bool HasUnresolvedUsing, Expr *Base, QualType BaseType, bool IsArrow, @@ -691,10 +734,12 @@ UnresolvedMemberExpr::UnresolvedMemberExpr(QualType T, bool Dependent, SourceRange QualifierRange, DeclarationName MemberName, SourceLocation MemberLoc, - const TemplateArgumentListInfo *TemplateArgs) - : OverloadExpr(UnresolvedMemberExprClass, T, Dependent, + const TemplateArgumentListInfo *TemplateArgs, + UnresolvedSetIterator Begin, + UnresolvedSetIterator End) + : OverloadExpr(UnresolvedMemberExprClass, C, T, Dependent, Qualifier, QualifierRange, MemberName, MemberLoc, - TemplateArgs != 0), + TemplateArgs != 0, Begin, End), IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing), Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) { if (TemplateArgs) @@ -710,17 +755,19 @@ UnresolvedMemberExpr::Create(ASTContext &C, bool Dependent, SourceRange QualifierRange, DeclarationName Member, SourceLocation MemberLoc, - const TemplateArgumentListInfo *TemplateArgs) { + const TemplateArgumentListInfo *TemplateArgs, + UnresolvedSetIterator Begin, + UnresolvedSetIterator End) { std::size_t size = sizeof(UnresolvedMemberExpr); if (TemplateArgs) size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs); void *Mem = C.Allocate(size, llvm::alignof<UnresolvedMemberExpr>()); - return new (Mem) UnresolvedMemberExpr( + return new (Mem) UnresolvedMemberExpr(C, Dependent ? C.DependentTy : C.OverloadTy, Dependent, HasUnresolvedUsing, Base, BaseType, IsArrow, OperatorLoc, Qualifier, QualifierRange, - Member, MemberLoc, TemplateArgs); + Member, MemberLoc, TemplateArgs, Begin, End); } CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const { diff --git a/lib/AST/ExprConstant.cpp b/lib/AST/ExprConstant.cpp index c1a42d8..dc61401 100644 --- a/lib/AST/ExprConstant.cpp +++ b/lib/AST/ExprConstant.cpp @@ -48,31 +48,110 @@ struct EvalInfo { /// EvalResult - Contains information about the evaluation. Expr::EvalResult &EvalResult; - /// AnyLValue - Stack based LValue results are not discarded. - bool AnyLValue; - - EvalInfo(ASTContext &ctx, Expr::EvalResult& evalresult, - bool anylvalue = false) - : Ctx(ctx), EvalResult(evalresult), AnyLValue(anylvalue) {} + EvalInfo(ASTContext &ctx, Expr::EvalResult& evalresult) + : Ctx(ctx), EvalResult(evalresult) {} }; +namespace { + struct ComplexValue { + private: + bool IsInt; -static bool EvaluateLValue(const Expr *E, APValue &Result, EvalInfo &Info); -static bool EvaluatePointer(const Expr *E, APValue &Result, EvalInfo &Info); + public: + APSInt IntReal, IntImag; + APFloat FloatReal, FloatImag; + + ComplexValue() : FloatReal(APFloat::Bogus), FloatImag(APFloat::Bogus) {} + + void makeComplexFloat() { IsInt = false; } + bool isComplexFloat() const { return !IsInt; } + APFloat &getComplexFloatReal() { return FloatReal; } + APFloat &getComplexFloatImag() { return FloatImag; } + + void makeComplexInt() { IsInt = true; } + bool isComplexInt() const { return IsInt; } + APSInt &getComplexIntReal() { return IntReal; } + APSInt &getComplexIntImag() { return IntImag; } + + void moveInto(APValue &v) { + if (isComplexFloat()) + v = APValue(FloatReal, FloatImag); + else + v = APValue(IntReal, IntImag); + } + }; + + struct LValue { + Expr *Base; + CharUnits Offset; + + Expr *getLValueBase() { return Base; } + CharUnits getLValueOffset() { return Offset; } + + void moveInto(APValue &v) { + v = APValue(Base, Offset); + } + }; +} + +static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info); +static bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info); static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info); static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, EvalInfo &Info); static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info); -static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info); +static bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info); //===----------------------------------------------------------------------===// // Misc utilities //===----------------------------------------------------------------------===// -static bool EvalPointerValueAsBool(APValue& Value, bool& Result) { - // FIXME: Is this accurate for all kinds of bases? If not, what would - // the check look like? - Result = Value.getLValueBase() || !Value.getLValueOffset().isZero(); +static bool IsGlobalLValue(const Expr* E) { + if (!E) return true; + + if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { + if (isa<FunctionDecl>(DRE->getDecl())) + return true; + if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) + return VD->hasGlobalStorage(); + return false; + } + + if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(E)) + return CLE->isFileScope(); + + return true; +} + +static bool EvalPointerValueAsBool(LValue& Value, bool& Result) { + const Expr* Base = Value.Base; + + // A null base expression indicates a null pointer. These are always + // evaluatable, and they are false unless the offset is zero. + if (!Base) { + Result = !Value.Offset.isZero(); + return true; + } + + // Require the base expression to be a global l-value. + if (!IsGlobalLValue(Base)) return false; + + // We have a non-null base expression. These are generally known to + // be true, but if it'a decl-ref to a weak symbol it can be null at + // runtime. + Result = true; + + const DeclRefExpr* DeclRef = dyn_cast<DeclRefExpr>(Base); + if (!DeclRef) + return true; + + // If it's a weak symbol, it isn't constant-evaluable. + const ValueDecl* Decl = DeclRef->getDecl(); + if (Decl->hasAttr<WeakAttr>() || + Decl->hasAttr<WeakRefAttr>() || + Decl->hasAttr<WeakImportAttr>()) + return false; + return true; } @@ -91,12 +170,12 @@ static bool HandleConversionToBool(const Expr* E, bool& Result, Result = !FloatResult.isZero(); return true; } else if (E->getType()->hasPointerRepresentation()) { - APValue PointerResult; + LValue PointerResult; if (!EvaluatePointer(E, PointerResult, Info)) return false; return EvalPointerValueAsBool(PointerResult, Result); } else if (E->getType()->isAnyComplexType()) { - APValue ComplexResult; + ComplexValue ComplexResult; if (!EvaluateComplex(E, ComplexResult, Info)) return false; if (ComplexResult.isComplexFloat()) { @@ -221,34 +300,42 @@ public: //===----------------------------------------------------------------------===// namespace { class LValueExprEvaluator - : public StmtVisitor<LValueExprEvaluator, APValue> { + : public StmtVisitor<LValueExprEvaluator, bool> { EvalInfo &Info; + LValue &Result; + + bool Success(Expr *E) { + Result.Base = E; + Result.Offset = CharUnits::Zero(); + return true; + } public: - LValueExprEvaluator(EvalInfo &info) : Info(info) {} + LValueExprEvaluator(EvalInfo &info, LValue &Result) : + Info(info), Result(Result) {} - APValue VisitStmt(Stmt *S) { - return APValue(); + bool VisitStmt(Stmt *S) { + return false; } - APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } - APValue VisitDeclRefExpr(DeclRefExpr *E); - APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E); } - APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E); - APValue VisitMemberExpr(MemberExpr *E); - APValue VisitStringLiteral(StringLiteral *E) { return APValue(E); } - APValue VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return APValue(E); } - APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E); - APValue VisitUnaryDeref(UnaryOperator *E); - APValue VisitUnaryExtension(const UnaryOperator *E) + bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } + bool VisitDeclRefExpr(DeclRefExpr *E); + bool VisitPredefinedExpr(PredefinedExpr *E) { return Success(E); } + bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E); + bool VisitMemberExpr(MemberExpr *E); + bool VisitStringLiteral(StringLiteral *E) { return Success(E); } + bool VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return Success(E); } + bool VisitArraySubscriptExpr(ArraySubscriptExpr *E); + bool VisitUnaryDeref(UnaryOperator *E); + bool VisitUnaryExtension(const UnaryOperator *E) { return Visit(E->getSubExpr()); } - APValue VisitChooseExpr(const ChooseExpr *E) + bool VisitChooseExpr(const ChooseExpr *E) { return Visit(E->getChosenSubExpr(Info.Ctx)); } - APValue VisitCastExpr(CastExpr *E) { + bool VisitCastExpr(CastExpr *E) { switch (E->getCastKind()) { default: - return APValue(); + return false; case CastExpr::CK_NoOp: return Visit(E->getSubExpr()); @@ -258,44 +345,41 @@ public: }; } // end anonymous namespace -static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) { - Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E)); - return Result.isLValue(); +static bool EvaluateLValue(const Expr* E, LValue& Result, EvalInfo &Info) { + return LValueExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E)); } -APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) { +bool LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) { if (isa<FunctionDecl>(E->getDecl())) { - return APValue(E); + return Success(E); } else if (VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) { - if (!Info.AnyLValue && !VD->hasGlobalStorage()) - return APValue(); if (!VD->getType()->isReferenceType()) - return APValue(E); + return Success(E); + // Reference parameters can refer to anything even if they have an + // "initializer" in the form of a default argument. + if (isa<ParmVarDecl>(VD)) + return false; // FIXME: Check whether VD might be overridden! if (const Expr *Init = VD->getAnyInitializer()) return Visit(const_cast<Expr *>(Init)); } - return APValue(); + return false; } -APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { - if (!Info.AnyLValue && !E->isFileScope()) - return APValue(); - return APValue(E); +bool LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { + return Success(E); } -APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) { - APValue result; +bool LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) { QualType Ty; if (E->isArrow()) { - if (!EvaluatePointer(E->getBase(), result, Info)) - return APValue(); + if (!EvaluatePointer(E->getBase(), Result, Info)) + return false; Ty = E->getBase()->getType()->getAs<PointerType>()->getPointeeType(); } else { - result = Visit(E->getBase()); - if (result.isUninit()) - return APValue(); + if (!Visit(E->getBase())) + return false; Ty = E->getBase()->getType(); } @@ -304,10 +388,10 @@ APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) { FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); if (!FD) // FIXME: deal with other kinds of member expressions - return APValue(); + return false; if (FD->getType()->isReferenceType()) - return APValue(); + return false; // FIXME: This is linear time. unsigned i = 0; @@ -318,36 +402,25 @@ APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) { break; } - result.setLValue(result.getLValueBase(), - result.getLValueOffset() + - CharUnits::fromQuantity(RL.getFieldOffset(i) / 8)); - - return result; + Result.Offset += CharUnits::fromQuantity(RL.getFieldOffset(i) / 8); + return true; } -APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) { - APValue Result; - +bool LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) { if (!EvaluatePointer(E->getBase(), Result, Info)) - return APValue(); + return false; APSInt Index; if (!EvaluateInteger(E->getIdx(), Index, Info)) - return APValue(); + return false; CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(E->getType()); - - CharUnits Offset = Index.getSExtValue() * ElementSize; - Result.setLValue(Result.getLValueBase(), - Result.getLValueOffset() + Offset); - return Result; + Result.Offset += Index.getSExtValue() * ElementSize; + return true; } -APValue LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) { - APValue Result; - if (!EvaluatePointer(E->getSubExpr(), Result, Info)) - return APValue(); - return Result; +bool LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) { + return EvaluatePointer(E->getSubExpr(), Result, Info); } //===----------------------------------------------------------------------===// @@ -356,104 +429,103 @@ APValue LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) { namespace { class PointerExprEvaluator - : public StmtVisitor<PointerExprEvaluator, APValue> { + : public StmtVisitor<PointerExprEvaluator, bool> { EvalInfo &Info; + LValue &Result; + + bool Success(Expr *E) { + Result.Base = E; + Result.Offset = CharUnits::Zero(); + return true; + } public: - PointerExprEvaluator(EvalInfo &info) : Info(info) {} + PointerExprEvaluator(EvalInfo &info, LValue &Result) + : Info(info), Result(Result) {} - APValue VisitStmt(Stmt *S) { - return APValue(); + bool VisitStmt(Stmt *S) { + return false; } - APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } + bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } - APValue VisitBinaryOperator(const BinaryOperator *E); - APValue VisitCastExpr(CastExpr* E); - APValue VisitUnaryExtension(const UnaryOperator *E) + bool VisitBinaryOperator(const BinaryOperator *E); + bool VisitCastExpr(CastExpr* E); + bool VisitUnaryExtension(const UnaryOperator *E) { return Visit(E->getSubExpr()); } - APValue VisitUnaryAddrOf(const UnaryOperator *E); - APValue VisitObjCStringLiteral(ObjCStringLiteral *E) - { return APValue(E); } - APValue VisitAddrLabelExpr(AddrLabelExpr *E) - { return APValue(E); } - APValue VisitCallExpr(CallExpr *E); - APValue VisitBlockExpr(BlockExpr *E) { + bool VisitUnaryAddrOf(const UnaryOperator *E); + bool VisitObjCStringLiteral(ObjCStringLiteral *E) + { return Success(E); } + bool VisitAddrLabelExpr(AddrLabelExpr *E) + { return Success(E); } + bool VisitCallExpr(CallExpr *E); + bool VisitBlockExpr(BlockExpr *E) { if (!E->hasBlockDeclRefExprs()) - return APValue(E); - return APValue(); + return Success(E); + return false; } - APValue VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) - { return APValue((Expr*)0); } - APValue VisitConditionalOperator(ConditionalOperator *E); - APValue VisitChooseExpr(ChooseExpr *E) + bool VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) + { return Success((Expr*)0); } + bool VisitConditionalOperator(ConditionalOperator *E); + bool VisitChooseExpr(ChooseExpr *E) { return Visit(E->getChosenSubExpr(Info.Ctx)); } - APValue VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) - { return APValue((Expr*)0); } + bool VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) + { return Success((Expr*)0); } // FIXME: Missing: @protocol, @selector }; } // end anonymous namespace -static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) { - if (!E->getType()->hasPointerRepresentation()) - return false; - Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E)); - return Result.isLValue(); +static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info) { + assert(E->getType()->hasPointerRepresentation()); + return PointerExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E)); } -APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { +bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { if (E->getOpcode() != BinaryOperator::Add && E->getOpcode() != BinaryOperator::Sub) - return APValue(); + return false; const Expr *PExp = E->getLHS(); const Expr *IExp = E->getRHS(); if (IExp->getType()->isPointerType()) std::swap(PExp, IExp); - APValue ResultLValue; - if (!EvaluatePointer(PExp, ResultLValue, Info)) - return APValue(); + if (!EvaluatePointer(PExp, Result, Info)) + return false; - llvm::APSInt AdditionalOffset; - if (!EvaluateInteger(IExp, AdditionalOffset, Info)) - return APValue(); + llvm::APSInt Offset; + if (!EvaluateInteger(IExp, Offset, Info)) + return false; + int64_t AdditionalOffset + = Offset.isSigned() ? Offset.getSExtValue() + : static_cast<int64_t>(Offset.getZExtValue()); // Compute the new offset in the appropriate width. QualType PointeeType = PExp->getType()->getAs<PointerType>()->getPointeeType(); - llvm::APSInt SizeOfPointee(AdditionalOffset); + 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.getTypeSizeInChars(PointeeType).getQuantity(); + SizeOfPointee = Info.Ctx.getTypeSizeInChars(PointeeType); - llvm::APSInt Offset(AdditionalOffset); - Offset = ResultLValue.getLValueOffset().getQuantity(); if (E->getOpcode() == BinaryOperator::Add) - Offset += AdditionalOffset * SizeOfPointee; + Result.Offset += AdditionalOffset * SizeOfPointee; else - Offset -= AdditionalOffset * SizeOfPointee; + Result.Offset -= AdditionalOffset * SizeOfPointee; - // Sign extend prior to converting back to a char unit. - if (Offset.getBitWidth() < 64) - Offset.extend(64); - return APValue(ResultLValue.getLValueBase(), - CharUnits::fromQuantity(Offset.getLimitedValue())); + return true; } -APValue PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { - APValue result; - if (EvaluateLValue(E->getSubExpr(), result, Info)) - return result; - return APValue(); +bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { + return EvaluateLValue(E->getSubExpr(), Result, Info); } -APValue PointerExprEvaluator::VisitCastExpr(CastExpr* E) { +bool PointerExprEvaluator::VisitCastExpr(CastExpr* E) { Expr* SubExpr = E->getSubExpr(); switch (E->getCastKind()) { @@ -471,18 +543,20 @@ APValue PointerExprEvaluator::VisitCastExpr(CastExpr* E) { return Visit(SubExpr); if (SubExpr->getType()->isIntegralType()) { - APValue Result; - if (!EvaluateIntegerOrLValue(SubExpr, Result, Info)) + APValue Value; + if (!EvaluateIntegerOrLValue(SubExpr, Value, Info)) break; - if (Result.isInt()) { - Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType())); - return APValue(0, - CharUnits::fromQuantity(Result.getInt().getZExtValue())); + if (Value.isInt()) { + Value.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType())); + Result.Base = 0; + Result.Offset = CharUnits::fromQuantity(Value.getInt().getZExtValue()); + return true; + } else { + Result.Base = Value.getLValueBase(); + Result.Offset = Value.getLValueOffset(); + return true; } - - // Cast is of an lvalue, no need to change value. - return Result; } break; } @@ -494,51 +568,46 @@ APValue PointerExprEvaluator::VisitCastExpr(CastExpr* E) { return Visit(SubExpr); case CastExpr::CK_IntegralToPointer: { - APValue Result; - if (!EvaluateIntegerOrLValue(SubExpr, Result, Info)) + APValue Value; + if (!EvaluateIntegerOrLValue(SubExpr, Value, Info)) break; - if (Result.isInt()) { - Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType())); - return APValue(0, - CharUnits::fromQuantity(Result.getInt().getZExtValue())); + if (Value.isInt()) { + Value.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType())); + Result.Base = 0; + Result.Offset = CharUnits::fromQuantity(Value.getInt().getZExtValue()); + return true; + } else { + // Cast is of an lvalue, no need to change value. + Result.Base = Value.getLValueBase(); + Result.Offset = Value.getLValueOffset(); + return true; } - - // Cast is of an lvalue, no need to change value. - return Result; } case CastExpr::CK_ArrayToPointerDecay: - case CastExpr::CK_FunctionToPointerDecay: { - APValue Result; - if (EvaluateLValue(SubExpr, Result, Info)) - return Result; - break; - } + case CastExpr::CK_FunctionToPointerDecay: + return EvaluateLValue(SubExpr, Result, Info); } - return APValue(); + return false; } -APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) { +bool PointerExprEvaluator::VisitCallExpr(CallExpr *E) { if (E->isBuiltinCall(Info.Ctx) == Builtin::BI__builtin___CFStringMakeConstantString || E->isBuiltinCall(Info.Ctx) == Builtin::BI__builtin___NSStringMakeConstantString) - return APValue(E); - return APValue(); + return Success(E); + return false; } -APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) { +bool PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) { bool BoolResult; if (!HandleConversionToBool(E->getCond(), BoolResult, Info)) - return APValue(); + return false; Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); - - APValue Result; - if (EvaluatePointer(EvalExpr, Result, Info)) - return Result; - return APValue(); + return Visit(EvalExpr); } //===----------------------------------------------------------------------===// @@ -867,18 +936,20 @@ public: private: CharUnits GetAlignOfExpr(const Expr *E); CharUnits GetAlignOfType(QualType T); + static QualType GetObjectType(const Expr *E); + bool TryEvaluateBuiltinObjectSize(CallExpr *E); // FIXME: Missing: array subscript of vector, member of vector }; } // end anonymous namespace static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) { - if (!E->getType()->isIntegralType()) - return false; - + assert(E->getType()->isIntegralType()); return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E)); } static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) { + assert(E->getType()->isIntegralType()); + APValue Val; if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt()) return false; @@ -984,36 +1055,55 @@ static int EvaluateBuiltinClassifyType(const CallExpr *E) { return -1; } +/// Retrieves the "underlying object type" of the given expression, +/// as used by __builtin_object_size. +QualType IntExprEvaluator::GetObjectType(const Expr *E) { + if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { + if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) + return VD->getType(); + } else if (isa<CompoundLiteralExpr>(E)) { + return E->getType(); + } + + return QualType(); +} + +bool IntExprEvaluator::TryEvaluateBuiltinObjectSize(CallExpr *E) { + // TODO: Perhaps we should let LLVM lower this? + LValue Base; + if (!EvaluatePointer(E->getArg(0), Base, Info)) + return false; + + // If we can prove the base is null, lower to zero now. + const Expr *LVBase = Base.getLValueBase(); + if (!LVBase) return Success(0, E); + + QualType T = GetObjectType(LVBase); + if (T.isNull() || + T->isIncompleteType() || + !T->isObjectType() || + T->isVariablyModifiedType() || + T->isDependentType()) + return false; + + CharUnits Size = Info.Ctx.getTypeSizeInChars(T); + CharUnits Offset = Base.getLValueOffset(); + + if (!Offset.isNegative() && Offset <= Size) + Size -= Offset; + else + Size = CharUnits::Zero(); + return Success(Size.getQuantity(), E); +} + bool IntExprEvaluator::VisitCallExpr(CallExpr *E) { switch (E->isBuiltinCall(Info.Ctx)) { default: return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); case Builtin::BI__builtin_object_size: { - const Expr *Arg = E->getArg(0)->IgnoreParens(); - Expr::EvalResult Base; - - // TODO: Perhaps we should let LLVM lower this? - if (Arg->EvaluateAsAny(Base, Info.Ctx) - && Base.Val.getKind() == APValue::LValue - && !Base.HasSideEffects) - if (const Expr *LVBase = Base.Val.getLValueBase()) - if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LVBase)) { - if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) { - if (!VD->getType()->isIncompleteType() - && VD->getType()->isObjectType() - && !VD->getType()->isVariablyModifiedType() - && !VD->getType()->isDependentType()) { - CharUnits Size = Info.Ctx.getTypeSizeInChars(VD->getType()); - CharUnits Offset = Base.Val.getLValueOffset(); - if (!Offset.isNegative() && Offset <= Size) - Size -= Offset; - else - Size = CharUnits::Zero(); - return Success(Size.getQuantity(), E); - } - } - } + if (TryEvaluateBuiltinObjectSize(E)) + return true; // If evaluating the argument has side-effects we can't determine // the size of the object and lower it to unknown now. @@ -1098,7 +1188,7 @@ bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { if (LHSTy->isAnyComplexType()) { assert(RHSTy->isAnyComplexType() && "Invalid comparison"); - APValue LHS, RHS; + ComplexValue LHS, RHS; if (!EvaluateComplex(E->getLHS(), LHS, Info)) return false; @@ -1173,11 +1263,11 @@ bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { if (LHSTy->isPointerType() && RHSTy->isPointerType()) { if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) { - APValue LHSValue; + LValue LHSValue; if (!EvaluatePointer(E->getLHS(), LHSValue, Info)) return false; - APValue RHSValue; + LValue RHSValue; if (!EvaluatePointer(E->getRHS(), RHSValue, Info)) return false; @@ -1463,7 +1553,7 @@ bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { if (E->isOffsetOfOp()) { // The AST for offsetof is defined in such a way that we can just // directly Evaluate it as an l-value. - APValue LV; + LValue LV; if (!EvaluateLValue(E->getSubExpr(), LV, Info)) return false; if (LV.getLValueBase()) @@ -1538,7 +1628,7 @@ bool IntExprEvaluator::VisitCastExpr(CastExpr *E) { // FIXME: Clean this up! if (SrcType->isPointerType()) { - APValue LV; + LValue LV; if (!EvaluatePointer(SubExpr, LV, Info)) return false; @@ -1547,7 +1637,7 @@ bool IntExprEvaluator::VisitCastExpr(CastExpr *E) { if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType)) return false; - Result = LV; + LV.moveInto(Result); return true; } @@ -1559,19 +1649,19 @@ bool IntExprEvaluator::VisitCastExpr(CastExpr *E) { if (SrcType->isArrayType() || SrcType->isFunctionType()) { // This handles double-conversion cases, where there's both // an l-value promotion and an implicit conversion to int. - APValue LV; + LValue LV; if (!EvaluateLValue(SubExpr, LV, Info)) return false; if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy)) return false; - Result = LV; + LV.moveInto(Result); return true; } if (SrcType->isAnyComplexType()) { - APValue C; + ComplexValue C; if (!EvaluateComplex(SubExpr, C, Info)) return false; if (C.isComplexFloat()) @@ -1596,7 +1686,7 @@ bool IntExprEvaluator::VisitCastExpr(CastExpr *E) { bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { if (E->getSubExpr()->getType()->isAnyComplexType()) { - APValue LV; + ComplexValue LV; if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt()) return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); return Success(LV.getComplexIntReal(), E); @@ -1607,7 +1697,7 @@ bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { if (E->getSubExpr()->getType()->isComplexIntegerType()) { - APValue LV; + ComplexValue LV; if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt()) return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); return Success(LV.getComplexIntImag(), E); @@ -1649,13 +1739,16 @@ public: { return Visit(E->getChosenSubExpr(Info.Ctx)); } bool VisitUnaryExtension(const UnaryOperator *E) { return Visit(E->getSubExpr()); } + bool VisitUnaryReal(const UnaryOperator *E); + bool VisitUnaryImag(const UnaryOperator *E); - // FIXME: Missing: __real__/__imag__, array subscript of vector, - // member of vector, ImplicitValueInitExpr + // FIXME: Missing: array subscript of vector, member of vector, + // ImplicitValueInitExpr }; } // end anonymous namespace static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { + assert(E->getType()->isRealFloatingType()); return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E)); } @@ -1736,6 +1829,22 @@ bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { } } +bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { + ComplexValue CV; + if (!EvaluateComplex(E->getSubExpr(), CV, Info)) + return false; + Result = CV.FloatReal; + return true; +} + +bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { + ComplexValue CV; + if (!EvaluateComplex(E->getSubExpr(), CV, Info)) + return false; + Result = CV.FloatImag; + return true; +} + bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { if (E->getOpcode() == UnaryOperator::Deref) return false; @@ -1838,166 +1947,170 @@ bool FloatExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) { namespace { class ComplexExprEvaluator - : public StmtVisitor<ComplexExprEvaluator, APValue> { + : public StmtVisitor<ComplexExprEvaluator, bool> { EvalInfo &Info; + ComplexValue &Result; public: - ComplexExprEvaluator(EvalInfo &info) : Info(info) {} + ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result) + : Info(info), Result(Result) {} //===--------------------------------------------------------------------===// // Visitor Methods //===--------------------------------------------------------------------===// - APValue VisitStmt(Stmt *S) { - return APValue(); + bool VisitStmt(Stmt *S) { + return false; } - APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } + bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } - APValue VisitImaginaryLiteral(ImaginaryLiteral *E) { + bool VisitImaginaryLiteral(ImaginaryLiteral *E) { Expr* SubExpr = E->getSubExpr(); if (SubExpr->getType()->isRealFloatingType()) { - APFloat Result(0.0); - - if (!EvaluateFloat(SubExpr, Result, Info)) - return APValue(); + Result.makeComplexFloat(); + APFloat &Imag = Result.FloatImag; + if (!EvaluateFloat(SubExpr, Imag, Info)) + return false; - return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false), - Result); + Result.FloatReal = APFloat(Imag.getSemantics()); + return true; } else { assert(SubExpr->getType()->isIntegerType() && "Unexpected imaginary literal."); - llvm::APSInt Result; - if (!EvaluateInteger(SubExpr, Result, Info)) - return APValue(); + Result.makeComplexInt(); + APSInt &Imag = Result.IntImag; + if (!EvaluateInteger(SubExpr, Imag, Info)) + return false; - llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned()); - Zero = 0; - return APValue(Zero, Result); + Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned()); + return true; } } - APValue VisitCastExpr(CastExpr *E) { + bool VisitCastExpr(CastExpr *E) { Expr* SubExpr = E->getSubExpr(); QualType EltType = E->getType()->getAs<ComplexType>()->getElementType(); QualType SubType = SubExpr->getType(); if (SubType->isRealFloatingType()) { - APFloat Result(0.0); - - if (!EvaluateFloat(SubExpr, Result, Info)) - return APValue(); + APFloat &Real = Result.FloatReal; + if (!EvaluateFloat(SubExpr, Real, Info)) + return false; if (EltType->isRealFloatingType()) { - Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx); - return APValue(Result, - APFloat(Result.getSemantics(), APFloat::fcZero, false)); + Result.makeComplexFloat(); + Real = HandleFloatToFloatCast(EltType, SubType, Real, Info.Ctx); + Result.FloatImag = APFloat(Real.getSemantics()); + return true; } else { - llvm::APSInt IResult; - IResult = HandleFloatToIntCast(EltType, SubType, Result, Info.Ctx); - llvm::APSInt Zero(IResult.getBitWidth(), !IResult.isSigned()); - Zero = 0; - return APValue(IResult, Zero); + Result.makeComplexInt(); + Result.IntReal = HandleFloatToIntCast(EltType, SubType, Real, Info.Ctx); + Result.IntImag = APSInt(Result.IntReal.getBitWidth(), + !Result.IntReal.isSigned()); + return true; } } else if (SubType->isIntegerType()) { - APSInt Result; - - if (!EvaluateInteger(SubExpr, Result, Info)) - return APValue(); + APSInt &Real = Result.IntReal; + if (!EvaluateInteger(SubExpr, Real, Info)) + return false; if (EltType->isRealFloatingType()) { - APFloat FResult = - HandleIntToFloatCast(EltType, SubType, Result, Info.Ctx); - return APValue(FResult, - APFloat(FResult.getSemantics(), APFloat::fcZero, false)); + Result.makeComplexFloat(); + Result.FloatReal + = HandleIntToFloatCast(EltType, SubType, Real, Info.Ctx); + Result.FloatImag = APFloat(Result.FloatReal.getSemantics()); + return true; } else { - Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx); - llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned()); - Zero = 0; - return APValue(Result, Zero); + Result.makeComplexInt(); + Real = HandleIntToIntCast(EltType, SubType, Real, Info.Ctx); + Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned()); + return true; } } else if (const ComplexType *CT = SubType->getAs<ComplexType>()) { - APValue Src; - - if (!EvaluateComplex(SubExpr, Src, Info)) - return APValue(); + if (!Visit(SubExpr)) + return false; QualType SrcType = CT->getElementType(); - if (Src.isComplexFloat()) { + if (Result.isComplexFloat()) { if (EltType->isRealFloatingType()) { - return APValue(HandleFloatToFloatCast(EltType, SrcType, - Src.getComplexFloatReal(), - Info.Ctx), - HandleFloatToFloatCast(EltType, SrcType, - Src.getComplexFloatImag(), - Info.Ctx)); + Result.makeComplexFloat(); + Result.FloatReal = HandleFloatToFloatCast(EltType, SrcType, + Result.FloatReal, + Info.Ctx); + Result.FloatImag = HandleFloatToFloatCast(EltType, SrcType, + Result.FloatImag, + Info.Ctx); + return true; } else { - return APValue(HandleFloatToIntCast(EltType, SrcType, - Src.getComplexFloatReal(), - Info.Ctx), - HandleFloatToIntCast(EltType, SrcType, - Src.getComplexFloatImag(), - Info.Ctx)); + Result.makeComplexInt(); + Result.IntReal = HandleFloatToIntCast(EltType, SrcType, + Result.FloatReal, + Info.Ctx); + Result.IntImag = HandleFloatToIntCast(EltType, SrcType, + Result.FloatImag, + Info.Ctx); + return true; } } else { - assert(Src.isComplexInt() && "Invalid evaluate result."); + assert(Result.isComplexInt() && "Invalid evaluate result."); if (EltType->isRealFloatingType()) { - return APValue(HandleIntToFloatCast(EltType, SrcType, - Src.getComplexIntReal(), - Info.Ctx), - HandleIntToFloatCast(EltType, SrcType, - Src.getComplexIntImag(), - Info.Ctx)); + Result.makeComplexFloat(); + Result.FloatReal = HandleIntToFloatCast(EltType, SrcType, + Result.IntReal, + Info.Ctx); + Result.FloatImag = HandleIntToFloatCast(EltType, SrcType, + Result.IntImag, + Info.Ctx); + return true; } else { - return APValue(HandleIntToIntCast(EltType, SrcType, - Src.getComplexIntReal(), - Info.Ctx), - HandleIntToIntCast(EltType, SrcType, - Src.getComplexIntImag(), - Info.Ctx)); + Result.makeComplexInt(); + Result.IntReal = HandleIntToIntCast(EltType, SrcType, + Result.IntReal, + Info.Ctx); + Result.IntImag = HandleIntToIntCast(EltType, SrcType, + Result.IntImag, + Info.Ctx); + return true; } } } // FIXME: Handle more casts. - return APValue(); + return false; } - APValue VisitBinaryOperator(const BinaryOperator *E); - APValue VisitChooseExpr(const ChooseExpr *E) + bool VisitBinaryOperator(const BinaryOperator *E); + bool VisitChooseExpr(const ChooseExpr *E) { return Visit(E->getChosenSubExpr(Info.Ctx)); } - APValue VisitUnaryExtension(const UnaryOperator *E) + bool VisitUnaryExtension(const UnaryOperator *E) { return Visit(E->getSubExpr()); } // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr, // conditional ?:, comma }; } // end anonymous namespace -static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) { - Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E)); - assert((!Result.isComplexFloat() || - (&Result.getComplexFloatReal().getSemantics() == - &Result.getComplexFloatImag().getSemantics())) && - "Invalid complex evaluation."); - return Result.isComplexFloat() || Result.isComplexInt(); +static bool EvaluateComplex(const Expr *E, ComplexValue &Result, + EvalInfo &Info) { + assert(E->getType()->isAnyComplexType()); + return ComplexExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E)); } -APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { - APValue Result, RHS; - - if (!EvaluateComplex(E->getLHS(), Result, Info)) - return APValue(); +bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { + if (!Visit(E->getLHS())) + return false; + ComplexValue RHS; if (!EvaluateComplex(E->getRHS(), RHS, Info)) - return APValue(); + return false; assert(Result.isComplexFloat() == RHS.isComplexFloat() && "Invalid operands to binary operator."); switch (E->getOpcode()) { - default: return APValue(); + default: return false; case BinaryOperator::Add: if (Result.isComplexFloat()) { Result.getComplexFloatReal().add(RHS.getComplexFloatReal(), @@ -2022,7 +2135,7 @@ APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { break; case BinaryOperator::Mul: if (Result.isComplexFloat()) { - APValue LHS = Result; + ComplexValue LHS = Result; APFloat &LHS_r = LHS.getComplexFloatReal(); APFloat &LHS_i = LHS.getComplexFloatImag(); APFloat &RHS_r = RHS.getComplexFloatReal(); @@ -2042,7 +2155,7 @@ APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven); } else { - APValue LHS = Result; + ComplexValue LHS = Result; Result.getComplexIntReal() = (LHS.getComplexIntReal() * RHS.getComplexIntReal() - LHS.getComplexIntImag() * RHS.getComplexIntImag()); @@ -2053,7 +2166,7 @@ APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { break; } - return Result; + return true; } //===----------------------------------------------------------------------===// @@ -2065,53 +2178,32 @@ APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { /// we want to. If this function returns true, it returns the folded constant /// in Result. bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const { + const Expr *E = this; EvalInfo Info(Ctx, Result); - - if (getType()->isVectorType()) { - if (!EvaluateVector(this, Result.Val, Info)) + if (E->getType()->isVectorType()) { + if (!EvaluateVector(E, Info.EvalResult.Val, Info)) return false; - } else if (getType()->isIntegerType()) { - if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this))) + } else if (E->getType()->isIntegerType()) { + if (!IntExprEvaluator(Info, Info.EvalResult.Val).Visit(const_cast<Expr*>(E))) return false; - } else if (getType()->hasPointerRepresentation()) { - if (!EvaluatePointer(this, Result.Val, Info)) - return false; - } else if (getType()->isRealFloatingType()) { - llvm::APFloat f(0.0); - if (!EvaluateFloat(this, f, Info)) - return false; - - Result.Val = APValue(f); - } else if (getType()->isAnyComplexType()) { - if (!EvaluateComplex(this, Result.Val, Info)) - return false; - } else - return false; - - return true; -} - -bool Expr::EvaluateAsAny(EvalResult &Result, ASTContext &Ctx) const { - EvalInfo Info(Ctx, Result, true); - - if (getType()->isVectorType()) { - if (!EvaluateVector(this, Result.Val, Info)) - return false; - } else if (getType()->isIntegerType()) { - if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this))) + } else if (E->getType()->hasPointerRepresentation()) { + LValue LV; + if (!EvaluatePointer(E, LV, Info)) return false; - } else if (getType()->hasPointerRepresentation()) { - if (!EvaluatePointer(this, Result.Val, Info)) + if (!IsGlobalLValue(LV.Base)) return false; - } else if (getType()->isRealFloatingType()) { - llvm::APFloat f(0.0); - if (!EvaluateFloat(this, f, Info)) + LV.moveInto(Info.EvalResult.Val); + } else if (E->getType()->isRealFloatingType()) { + llvm::APFloat F(0.0); + if (!EvaluateFloat(E, F, Info)) return false; - Result.Val = APValue(f); - } else if (getType()->isAnyComplexType()) { - if (!EvaluateComplex(this, Result.Val, Info)) + Info.EvalResult.Val = APValue(F); + } else if (E->getType()->isAnyComplexType()) { + ComplexValue C; + if (!EvaluateComplex(E, C, Info)) return false; + C.moveInto(Info.EvalResult.Val); } else return false; @@ -2128,13 +2220,25 @@ bool Expr::EvaluateAsBooleanCondition(bool &Result, ASTContext &Ctx) const { bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const { EvalInfo Info(Ctx, Result); - return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects; + LValue LV; + if (EvaluateLValue(this, LV, Info) && + !Result.HasSideEffects && + IsGlobalLValue(LV.Base)) { + LV.moveInto(Result.Val); + return true; + } + return false; } bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const { - EvalInfo Info(Ctx, Result, true); + EvalInfo Info(Ctx, Result); - return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects; + LValue LV; + if (EvaluateLValue(this, LV, Info)) { + LV.moveInto(Result.Val); + return true; + } + return false; } /// isEvaluatable - Call Evaluate to see if this expression can be constant @@ -2159,3 +2263,388 @@ APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const { return EvalResult.Val.getInt(); } + + bool Expr::EvalResult::isGlobalLValue() const { + assert(Val.isLValue()); + return IsGlobalLValue(Val.getLValueBase()); + } + + +/// isIntegerConstantExpr - this recursive routine will test if an expression is +/// an integer constant expression. + +/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, +/// comma, etc +/// +/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof +/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer +/// cast+dereference. + +// CheckICE - This function does the fundamental ICE checking: the returned +// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation. +// Note that to reduce code duplication, this helper does no evaluation +// itself; the caller checks whether the expression is evaluatable, and +// in the rare cases where CheckICE actually cares about the evaluated +// value, it calls into Evalute. +// +// Meanings of Val: +// 0: This expression is an ICE if it can be evaluated by Evaluate. +// 1: This expression is not an ICE, but if it isn't evaluated, it's +// a legal subexpression for an ICE. This return value is used to handle +// the comma operator in C99 mode. +// 2: This expression is not an ICE, and is not a legal subexpression for one. + +struct ICEDiag { + unsigned Val; + SourceLocation Loc; + + public: + ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {} + ICEDiag() : Val(0) {} +}; + +ICEDiag NoDiag() { return ICEDiag(); } + +static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) { + Expr::EvalResult EVResult; + if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects || + !EVResult.Val.isInt()) { + return ICEDiag(2, E->getLocStart()); + } + return NoDiag(); +} + +static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) { + assert(!E->isValueDependent() && "Should not see value dependent exprs!"); + if (!E->getType()->isIntegralType()) { + return ICEDiag(2, E->getLocStart()); + } + + switch (E->getStmtClass()) { +#define STMT(Node, Base) case Expr::Node##Class: +#define EXPR(Node, Base) +#include "clang/AST/StmtNodes.inc" + case Expr::PredefinedExprClass: + case Expr::FloatingLiteralClass: + case Expr::ImaginaryLiteralClass: + case Expr::StringLiteralClass: + case Expr::ArraySubscriptExprClass: + case Expr::MemberExprClass: + case Expr::CompoundAssignOperatorClass: + case Expr::CompoundLiteralExprClass: + case Expr::ExtVectorElementExprClass: + case Expr::InitListExprClass: + case Expr::DesignatedInitExprClass: + case Expr::ImplicitValueInitExprClass: + case Expr::ParenListExprClass: + case Expr::VAArgExprClass: + case Expr::AddrLabelExprClass: + case Expr::StmtExprClass: + case Expr::CXXMemberCallExprClass: + case Expr::CXXDynamicCastExprClass: + case Expr::CXXTypeidExprClass: + case Expr::CXXNullPtrLiteralExprClass: + case Expr::CXXThisExprClass: + case Expr::CXXThrowExprClass: + case Expr::CXXNewExprClass: + case Expr::CXXDeleteExprClass: + case Expr::CXXPseudoDestructorExprClass: + case Expr::UnresolvedLookupExprClass: + case Expr::DependentScopeDeclRefExprClass: + case Expr::CXXConstructExprClass: + case Expr::CXXBindTemporaryExprClass: + case Expr::CXXBindReferenceExprClass: + case Expr::CXXExprWithTemporariesClass: + case Expr::CXXTemporaryObjectExprClass: + case Expr::CXXUnresolvedConstructExprClass: + case Expr::CXXDependentScopeMemberExprClass: + case Expr::UnresolvedMemberExprClass: + case Expr::ObjCStringLiteralClass: + case Expr::ObjCEncodeExprClass: + case Expr::ObjCMessageExprClass: + case Expr::ObjCSelectorExprClass: + case Expr::ObjCProtocolExprClass: + case Expr::ObjCIvarRefExprClass: + case Expr::ObjCPropertyRefExprClass: + case Expr::ObjCImplicitSetterGetterRefExprClass: + case Expr::ObjCSuperExprClass: + case Expr::ObjCIsaExprClass: + case Expr::ShuffleVectorExprClass: + case Expr::BlockExprClass: + case Expr::BlockDeclRefExprClass: + case Expr::NoStmtClass: + return ICEDiag(2, E->getLocStart()); + + case Expr::GNUNullExprClass: + // GCC considers the GNU __null value to be an integral constant expression. + return NoDiag(); + + case Expr::ParenExprClass: + return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx); + case Expr::IntegerLiteralClass: + case Expr::CharacterLiteralClass: + case Expr::CXXBoolLiteralExprClass: + case Expr::CXXZeroInitValueExprClass: + case Expr::TypesCompatibleExprClass: + case Expr::UnaryTypeTraitExprClass: + return NoDiag(); + case Expr::CallExprClass: + case Expr::CXXOperatorCallExprClass: { + const CallExpr *CE = cast<CallExpr>(E); + if (CE->isBuiltinCall(Ctx)) + return CheckEvalInICE(E, Ctx); + return ICEDiag(2, E->getLocStart()); + } + case Expr::DeclRefExprClass: + if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl())) + return NoDiag(); + if (Ctx.getLangOptions().CPlusPlus && + E->getType().getCVRQualifiers() == Qualifiers::Const) { + const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl(); + + // Parameter variables are never constants. Without this check, + // getAnyInitializer() can find a default argument, which leads + // to chaos. + if (isa<ParmVarDecl>(D)) + return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); + + // C++ 7.1.5.1p2 + // A variable of non-volatile const-qualified integral or enumeration + // type initialized by an ICE can be used in ICEs. + if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) { + Qualifiers Quals = Ctx.getCanonicalType(Dcl->getType()).getQualifiers(); + if (Quals.hasVolatile() || !Quals.hasConst()) + return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); + + // Look for a declaration of this variable that has an initializer. + const VarDecl *ID = 0; + const Expr *Init = Dcl->getAnyInitializer(ID); + if (Init) { + if (ID->isInitKnownICE()) { + // We have already checked whether this subexpression is an + // integral constant expression. + if (ID->isInitICE()) + return NoDiag(); + else + return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); + } + + // It's an ICE whether or not the definition we found is + // out-of-line. See DR 721 and the discussion in Clang PR + // 6206 for details. + + if (Dcl->isCheckingICE()) { + return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); + } + + Dcl->setCheckingICE(); + ICEDiag Result = CheckICE(Init, Ctx); + // Cache the result of the ICE test. + Dcl->setInitKnownICE(Result.Val == 0); + return Result; + } + } + } + return ICEDiag(2, E->getLocStart()); + case Expr::UnaryOperatorClass: { + const UnaryOperator *Exp = cast<UnaryOperator>(E); + switch (Exp->getOpcode()) { + case UnaryOperator::PostInc: + case UnaryOperator::PostDec: + case UnaryOperator::PreInc: + case UnaryOperator::PreDec: + case UnaryOperator::AddrOf: + case UnaryOperator::Deref: + return ICEDiag(2, E->getLocStart()); + case UnaryOperator::Extension: + case UnaryOperator::LNot: + case UnaryOperator::Plus: + case UnaryOperator::Minus: + case UnaryOperator::Not: + case UnaryOperator::Real: + case UnaryOperator::Imag: + return CheckICE(Exp->getSubExpr(), Ctx); + case UnaryOperator::OffsetOf: + break; + } + + // OffsetOf falls through here. + } + case Expr::OffsetOfExprClass: { + // Note that per C99, offsetof must be an ICE. And AFAIK, using + // Evaluate matches the proposed gcc behavior for cases like + // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect + // compliance: we should warn earlier for offsetof expressions with + // array subscripts that aren't ICEs, and if the array subscripts + // are ICEs, the value of the offsetof must be an integer constant. + return CheckEvalInICE(E, Ctx); + } + case Expr::SizeOfAlignOfExprClass: { + const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E); + if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType()) + return ICEDiag(2, E->getLocStart()); + return NoDiag(); + } + case Expr::BinaryOperatorClass: { + const BinaryOperator *Exp = cast<BinaryOperator>(E); + switch (Exp->getOpcode()) { + case BinaryOperator::PtrMemD: + case BinaryOperator::PtrMemI: + case BinaryOperator::Assign: + case BinaryOperator::MulAssign: + case BinaryOperator::DivAssign: + case BinaryOperator::RemAssign: + case BinaryOperator::AddAssign: + case BinaryOperator::SubAssign: + case BinaryOperator::ShlAssign: + case BinaryOperator::ShrAssign: + case BinaryOperator::AndAssign: + case BinaryOperator::XorAssign: + case BinaryOperator::OrAssign: + return ICEDiag(2, E->getLocStart()); + + case BinaryOperator::Mul: + case BinaryOperator::Div: + case BinaryOperator::Rem: + case BinaryOperator::Add: + case BinaryOperator::Sub: + case BinaryOperator::Shl: + case BinaryOperator::Shr: + case BinaryOperator::LT: + case BinaryOperator::GT: + case BinaryOperator::LE: + case BinaryOperator::GE: + case BinaryOperator::EQ: + case BinaryOperator::NE: + case BinaryOperator::And: + case BinaryOperator::Xor: + case BinaryOperator::Or: + case BinaryOperator::Comma: { + ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); + ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); + if (Exp->getOpcode() == BinaryOperator::Div || + Exp->getOpcode() == BinaryOperator::Rem) { + // Evaluate gives an error for undefined Div/Rem, so make sure + // we don't evaluate one. + if (LHSResult.Val != 2 && RHSResult.Val != 2) { + llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx); + if (REval == 0) + return ICEDiag(1, E->getLocStart()); + if (REval.isSigned() && REval.isAllOnesValue()) { + llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx); + if (LEval.isMinSignedValue()) + return ICEDiag(1, E->getLocStart()); + } + } + } + if (Exp->getOpcode() == BinaryOperator::Comma) { + if (Ctx.getLangOptions().C99) { + // C99 6.6p3 introduces a strange edge case: comma can be in an ICE + // if it isn't evaluated. + if (LHSResult.Val == 0 && RHSResult.Val == 0) + return ICEDiag(1, E->getLocStart()); + } else { + // In both C89 and C++, commas in ICEs are illegal. + return ICEDiag(2, E->getLocStart()); + } + } + if (LHSResult.Val >= RHSResult.Val) + return LHSResult; + return RHSResult; + } + case BinaryOperator::LAnd: + case BinaryOperator::LOr: { + ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); + ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); + if (LHSResult.Val == 0 && RHSResult.Val == 1) { + // Rare case where the RHS has a comma "side-effect"; we need + // to actually check the condition to see whether the side + // with the comma is evaluated. + if ((Exp->getOpcode() == BinaryOperator::LAnd) != + (Exp->getLHS()->EvaluateAsInt(Ctx) == 0)) + return RHSResult; + return NoDiag(); + } + + if (LHSResult.Val >= RHSResult.Val) + return LHSResult; + return RHSResult; + } + } + } + case Expr::ImplicitCastExprClass: + case Expr::CStyleCastExprClass: + case Expr::CXXFunctionalCastExprClass: + case Expr::CXXStaticCastExprClass: + case Expr::CXXReinterpretCastExprClass: + case Expr::CXXConstCastExprClass: { + const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr(); + if (SubExpr->getType()->isIntegralType()) + return CheckICE(SubExpr, Ctx); + if (isa<FloatingLiteral>(SubExpr->IgnoreParens())) + return NoDiag(); + return ICEDiag(2, E->getLocStart()); + } + case Expr::ConditionalOperatorClass: { + const ConditionalOperator *Exp = cast<ConditionalOperator>(E); + // If the condition (ignoring parens) is a __builtin_constant_p call, + // then only the true side is actually considered in an integer constant + // expression, and it is fully evaluated. This is an important GNU + // extension. See GCC PR38377 for discussion. + if (const CallExpr *CallCE + = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts())) + if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) { + Expr::EvalResult EVResult; + if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects || + !EVResult.Val.isInt()) { + return ICEDiag(2, E->getLocStart()); + } + return NoDiag(); + } + ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx); + ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx); + ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); + if (CondResult.Val == 2) + return CondResult; + if (TrueResult.Val == 2) + return TrueResult; + if (FalseResult.Val == 2) + return FalseResult; + if (CondResult.Val == 1) + return CondResult; + if (TrueResult.Val == 0 && FalseResult.Val == 0) + return NoDiag(); + // Rare case where the diagnostics depend on which side is evaluated + // Note that if we get here, CondResult is 0, and at least one of + // TrueResult and FalseResult is non-zero. + if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) { + return FalseResult; + } + return TrueResult; + } + case Expr::CXXDefaultArgExprClass: + return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx); + case Expr::ChooseExprClass: { + return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx); + } + } + + // Silence a GCC warning + return ICEDiag(2, E->getLocStart()); +} + +bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx, + SourceLocation *Loc, bool isEvaluated) const { + ICEDiag d = CheckICE(this, Ctx); + if (d.Val != 0) { + if (Loc) *Loc = d.Loc; + return false; + } + EvalResult EvalResult; + if (!Evaluate(EvalResult, Ctx)) + llvm_unreachable("ICE cannot be evaluated!"); + assert(!EvalResult.HasSideEffects && "ICE with side effects!"); + assert(EvalResult.Val.isInt() && "ICE that isn't integer!"); + Result = EvalResult.Val.getInt(); + return true; +} diff --git a/lib/AST/NestedNameSpecifier.cpp b/lib/AST/NestedNameSpecifier.cpp index 45518e9..d6594cd 100644 --- a/lib/AST/NestedNameSpecifier.cpp +++ b/lib/AST/NestedNameSpecifier.cpp @@ -145,14 +145,14 @@ NestedNameSpecifier::print(llvm::raw_ostream &OS, InnerPolicy.SuppressScope = true; // Nested-name-specifiers are intended to contain minimally-qualified - // types. An actual QualifiedNameType will not occur, since we'll store + // types. An actual ElaboratedType will not occur, since we'll store // just the type that is referred to in the nested-name-specifier (e.g., // a TypedefType, TagType, etc.). However, when we are dealing with // dependent template-id types (e.g., Outer<T>::template Inner<U>), // the type requires its own nested-name-specifier for uniqueness, so we // suppress that nested-name-specifier during printing. - assert(!isa<QualifiedNameType>(T) && - "Qualified name type in nested-name-specifier"); + assert(!isa<ElaboratedType>(T) && + "Elaborated type in nested-name-specifier"); if (const TemplateSpecializationType *SpecType = dyn_cast<TemplateSpecializationType>(T)) { // Print the template name without its corresponding diff --git a/lib/AST/RecordLayout.cpp b/lib/AST/RecordLayout.cpp index ade2483..262c459 100644 --- a/lib/AST/RecordLayout.cpp +++ b/lib/AST/RecordLayout.cpp @@ -44,7 +44,9 @@ ASTRecordLayout::ASTRecordLayout(ASTContext &Ctx, unsigned fieldcount, uint64_t nonvirtualsize, unsigned nonvirtualalign, - const PrimaryBaseInfo &PrimaryBase, + uint64_t SizeOfLargestEmptySubobject, + const CXXRecordDecl *PrimaryBase, + bool PrimaryBaseIsVirtual, const BaseOffsetsMapTy& BaseOffsets, const BaseOffsetsMapTy& VBaseOffsets) : Size(size), DataSize(datasize), FieldOffsets(0), Alignment(alignment), @@ -55,9 +57,10 @@ ASTRecordLayout::ASTRecordLayout(ASTContext &Ctx, memcpy(FieldOffsets, fieldoffsets, FieldCount * sizeof(*FieldOffsets)); } - CXXInfo->PrimaryBase = PrimaryBase; + CXXInfo->PrimaryBase = PrimaryBaseInfo(PrimaryBase, PrimaryBaseIsVirtual); CXXInfo->NonVirtualSize = nonvirtualsize; CXXInfo->NonVirtualAlign = nonvirtualalign; + CXXInfo->SizeOfLargestEmptySubobject = SizeOfLargestEmptySubobject; CXXInfo->BaseOffsets = BaseOffsets; CXXInfo->VBaseOffsets = VBaseOffsets; diff --git a/lib/AST/RecordLayoutBuilder.cpp b/lib/AST/RecordLayoutBuilder.cpp index 3782985..983a287 100644 --- a/lib/AST/RecordLayoutBuilder.cpp +++ b/lib/AST/RecordLayoutBuilder.cpp @@ -1,4 +1,4 @@ -//=== ASTRecordLayoutBuilder.cpp - Helper class for building record layouts ==// +//=== RecordLayoutBuilder.cpp - Helper class for building record layouts ---==// // // The LLVM Compiler Infrastructure // @@ -7,28 +7,443 @@ // //===----------------------------------------------------------------------===// -#include "RecordLayoutBuilder.h" - #include "clang/AST/Attr.h" #include "clang/AST/Decl.h" #include "clang/AST/DeclCXX.h" #include "clang/AST/DeclObjC.h" #include "clang/AST/Expr.h" +#include "clang/AST/RecordLayout.h" #include "clang/Basic/TargetInfo.h" #include "llvm/Support/Format.h" #include "llvm/ADT/SmallSet.h" #include "llvm/Support/MathExtras.h" +#include <map> using namespace clang; -ASTRecordLayoutBuilder::ASTRecordLayoutBuilder(ASTContext &Context) - : Context(Context), Size(0), Alignment(8), Packed(false), - UnfilledBitsInLastByte(0), MaxFieldAlignment(0), DataSize(0), IsUnion(false), - NonVirtualSize(0), NonVirtualAlignment(8), FirstNearlyEmptyVBase(0) { } +namespace { + +/// EmptySubobjectMap - Keeps track of which empty subobjects exist at different +/// offsets while laying out a C++ class. +class EmptySubobjectMap { + ASTContext &Context; + + /// Class - The class whose empty entries we're keeping track of. + const CXXRecordDecl *Class; + + /// EmptyClassOffsets - A map from offsets to empty record decls. + typedef llvm::SmallVector<const CXXRecordDecl *, 1> ClassVectorTy; + typedef llvm::DenseMap<uint64_t, ClassVectorTy> EmptyClassOffsetsMapTy; + EmptyClassOffsetsMapTy EmptyClassOffsets; + + /// ComputeEmptySubobjectSizes - Compute the size of the largest base or + /// member subobject that is empty. + void ComputeEmptySubobjectSizes(); + + struct BaseInfo { + const CXXRecordDecl *Class; + bool IsVirtual; + + const CXXRecordDecl *PrimaryVirtualBase; + + llvm::SmallVector<BaseInfo*, 4> Bases; + const BaseInfo *Derived; + }; + + llvm::DenseMap<const CXXRecordDecl *, BaseInfo *> VirtualBaseInfo; + llvm::DenseMap<const CXXRecordDecl *, BaseInfo *> NonVirtualBaseInfo; + + BaseInfo *ComputeBaseInfo(const CXXRecordDecl *RD, bool IsVirtual, + const BaseInfo *Derived); + void ComputeBaseInfo(); + + bool CanPlaceBaseSubobjectAtOffset(const BaseInfo *Info, uint64_t Offset); + void UpdateEmptyBaseSubobjects(const BaseInfo *Info, uint64_t Offset); + +public: + /// This holds the size of the largest empty subobject (either a base + /// or a member). Will be zero if the record being built doesn't contain + /// any empty classes. + uint64_t SizeOfLargestEmptySubobject; + + EmptySubobjectMap(ASTContext &Context, const CXXRecordDecl *Class) + : Context(Context), Class(Class), SizeOfLargestEmptySubobject(0) { + ComputeEmptySubobjectSizes(); + + ComputeBaseInfo(); + } + + /// CanPlaceBaseAtOffset - Return whether the given base class can be placed + /// at the given offset. + /// Returns false if placing the record will result in two components + /// (direct or indirect) of the same type having the same offset. + bool CanPlaceBaseAtOffset(const CXXRecordDecl *RD, bool BaseIsVirtual, + uint64_t Offset); +}; + +void EmptySubobjectMap::ComputeEmptySubobjectSizes() { + // Check the bases. + for (CXXRecordDecl::base_class_const_iterator I = Class->bases_begin(), + E = Class->bases_end(); I != E; ++I) { + const CXXRecordDecl *BaseDecl = + cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); + + uint64_t EmptySize = 0; + const ASTRecordLayout &Layout = Context.getASTRecordLayout(BaseDecl); + if (BaseDecl->isEmpty()) { + // If the class decl is empty, get its size. + EmptySize = Layout.getSize(); + } else { + // Otherwise, we get the largest empty subobject for the decl. + EmptySize = Layout.getSizeOfLargestEmptySubobject(); + } + + SizeOfLargestEmptySubobject = std::max(SizeOfLargestEmptySubobject, + EmptySize); + } + + // Check the fields. + for (CXXRecordDecl::field_iterator I = Class->field_begin(), + E = Class->field_end(); I != E; ++I) { + const FieldDecl *FD = *I; + + const RecordType *RT = + Context.getBaseElementType(FD->getType())->getAs<RecordType>(); + + // We only care about record types. + if (!RT) + continue; + + uint64_t EmptySize = 0; + const CXXRecordDecl *MemberDecl = cast<CXXRecordDecl>(RT->getDecl()); + const ASTRecordLayout &Layout = Context.getASTRecordLayout(MemberDecl); + if (MemberDecl->isEmpty()) { + // If the class decl is empty, get its size. + EmptySize = Layout.getSize(); + } else { + // Otherwise, we get the largest empty subobject for the decl. + EmptySize = Layout.getSizeOfLargestEmptySubobject(); + } + + SizeOfLargestEmptySubobject = std::max(SizeOfLargestEmptySubobject, + EmptySize); + } +} + +EmptySubobjectMap::BaseInfo * +EmptySubobjectMap::ComputeBaseInfo(const CXXRecordDecl *RD, bool IsVirtual, + const BaseInfo *Derived) { + BaseInfo *Info; + + if (IsVirtual) { + BaseInfo *&InfoSlot = VirtualBaseInfo[RD]; + if (InfoSlot) { + assert(InfoSlot->Class == RD && "Wrong class for virtual base info!"); + return InfoSlot; + } + + InfoSlot = new (Context) BaseInfo; + Info = InfoSlot; + } else { + Info = new (Context) BaseInfo; + } + + Info->Class = RD; + Info->IsVirtual = IsVirtual; + Info->Derived = Derived; + Info->PrimaryVirtualBase = 0; + + if (RD->getNumVBases()) { + // Check if this class has a primary virtual base. + const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); + if (Layout.getPrimaryBaseWasVirtual()) { + Info->PrimaryVirtualBase = Layout.getPrimaryBase(); + assert(Info->PrimaryVirtualBase && + "Didn't have a primary virtual base!"); + } + } + + for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), + E = RD->bases_end(); I != E; ++I) { + bool IsVirtual = I->isVirtual(); + + const CXXRecordDecl *BaseDecl = + cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); + + Info->Bases.push_back(ComputeBaseInfo(BaseDecl, IsVirtual, Info)); + } + + return Info; +} + +void EmptySubobjectMap::ComputeBaseInfo() { + for (CXXRecordDecl::base_class_const_iterator I = Class->bases_begin(), + E = Class->bases_end(); I != E; ++I) { + bool IsVirtual = I->isVirtual(); + + const CXXRecordDecl *BaseDecl = + cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); + + BaseInfo *Info = ComputeBaseInfo(BaseDecl, IsVirtual, /*Derived=*/0); + if (IsVirtual) { + // ComputeBaseInfo has already added this base for us. + continue; + } + + // Add the base info to the map of non-virtual bases. + assert(!NonVirtualBaseInfo.count(BaseDecl) && + "Non-virtual base already exists!"); + NonVirtualBaseInfo.insert(std::make_pair(BaseDecl, Info)); + } +} + +bool +EmptySubobjectMap::CanPlaceBaseSubobjectAtOffset(const BaseInfo *Info, + uint64_t Offset) { + // Traverse all non-virtual bases. + for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) { + BaseInfo* Base = Info->Bases[I]; + if (Base->IsVirtual) + continue; + + const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class); + uint64_t BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class); + + if (!CanPlaceBaseSubobjectAtOffset(Base, BaseOffset)) + return false; + } + + if (Info->PrimaryVirtualBase) { + BaseInfo *PrimaryVirtualBaseInfo = + VirtualBaseInfo.lookup(Info->PrimaryVirtualBase); + assert(PrimaryVirtualBaseInfo && "Didn't find base info!"); + + if (Info == PrimaryVirtualBaseInfo->Derived) { + if (!CanPlaceBaseSubobjectAtOffset(PrimaryVirtualBaseInfo, Offset)) + return false; + } + } + + // FIXME: Member variables. + return true; +} + +void EmptySubobjectMap::UpdateEmptyBaseSubobjects(const BaseInfo *Info, + uint64_t Offset) { + if (Info->Class->isEmpty()) { + // FIXME: Record that there is an empty class at this offset. + } + + // Traverse all non-virtual bases. + for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) { + BaseInfo* Base = Info->Bases[I]; + if (Base->IsVirtual) + continue; + + const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class); + uint64_t BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class); + + UpdateEmptyBaseSubobjects(Base, BaseOffset); + } + + if (Info->PrimaryVirtualBase) { + BaseInfo *PrimaryVirtualBaseInfo = + VirtualBaseInfo.lookup(Info->PrimaryVirtualBase); + assert(PrimaryVirtualBaseInfo && "Didn't find base info!"); + + if (Info == PrimaryVirtualBaseInfo->Derived) + UpdateEmptyBaseSubobjects(PrimaryVirtualBaseInfo, Offset); + } + + // FIXME: Member variables. +} + +bool EmptySubobjectMap::CanPlaceBaseAtOffset(const CXXRecordDecl *RD, + bool BaseIsVirtual, + uint64_t Offset) { + // If we know this class doesn't have any empty subobjects we don't need to + // bother checking. + if (!SizeOfLargestEmptySubobject) + return true; + + BaseInfo *Info; + + if (BaseIsVirtual) + Info = VirtualBaseInfo.lookup(RD); + else + Info = NonVirtualBaseInfo.lookup(RD); + + if (!CanPlaceBaseSubobjectAtOffset(Info, Offset)) + return false; + + UpdateEmptyBaseSubobjects(Info, Offset); + return true; +} + +class RecordLayoutBuilder { + // FIXME: Remove this and make the appropriate fields public. + friend class clang::ASTContext; + + ASTContext &Context; + + EmptySubobjectMap *EmptySubobjects; + + /// Size - The current size of the record layout. + uint64_t Size; + + /// Alignment - The current alignment of the record layout. + unsigned Alignment; + + llvm::SmallVector<uint64_t, 16> FieldOffsets; + + /// Packed - Whether the record is packed or not. + unsigned Packed : 1; + + unsigned IsUnion : 1; + + unsigned IsMac68kAlign : 1; + + /// UnfilledBitsInLastByte - If the last field laid out was a bitfield, + /// this contains the number of bits in the last byte that can be used for + /// an adjacent bitfield if necessary. + unsigned char UnfilledBitsInLastByte; + + /// MaxFieldAlignment - The maximum allowed field alignment. This is set by + /// #pragma pack. + unsigned MaxFieldAlignment; + + /// DataSize - The data size of the record being laid out. + uint64_t DataSize; + + uint64_t NonVirtualSize; + unsigned NonVirtualAlignment; + + /// PrimaryBase - the primary base class (if one exists) of the class + /// we're laying out. + const CXXRecordDecl *PrimaryBase; + + /// PrimaryBaseIsVirtual - Whether the primary base of the class we're laying + /// out is virtual. + bool PrimaryBaseIsVirtual; + + typedef llvm::DenseMap<const CXXRecordDecl *, uint64_t> BaseOffsetsMapTy; + + /// Bases - base classes and their offsets in the record. + BaseOffsetsMapTy Bases; + + // VBases - virtual base classes and their offsets in the record. + BaseOffsetsMapTy VBases; + + /// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are + /// primary base classes for some other direct or indirect base class. + llvm::SmallSet<const CXXRecordDecl*, 32> IndirectPrimaryBases; + + /// FirstNearlyEmptyVBase - The first nearly empty virtual base class in + /// inheritance graph order. Used for determining the primary base class. + const CXXRecordDecl *FirstNearlyEmptyVBase; + + /// VisitedVirtualBases - A set of all the visited virtual bases, used to + /// avoid visiting virtual bases more than once. + llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBases; + + /// EmptyClassOffsets - A map from offsets to empty record decls. + typedef std::multimap<uint64_t, const CXXRecordDecl *> EmptyClassOffsetsTy; + EmptyClassOffsetsTy EmptyClassOffsets; + + RecordLayoutBuilder(ASTContext &Context, EmptySubobjectMap *EmptySubobjects) + : Context(Context), EmptySubobjects(EmptySubobjects), Size(0), Alignment(8), + Packed(false), IsUnion(false), IsMac68kAlign(false), + UnfilledBitsInLastByte(0), MaxFieldAlignment(0), DataSize(0), + NonVirtualSize(0), NonVirtualAlignment(8), PrimaryBase(0), + PrimaryBaseIsVirtual(false), FirstNearlyEmptyVBase(0) { } + + void Layout(const RecordDecl *D); + void Layout(const CXXRecordDecl *D); + void Layout(const ObjCInterfaceDecl *D); + + void LayoutFields(const RecordDecl *D); + void LayoutField(const FieldDecl *D); + void LayoutWideBitField(uint64_t FieldSize, uint64_t TypeSize); + void LayoutBitField(const FieldDecl *D); + + /// ComputeEmptySubobjectSizes - Compute the size of the largest base or + /// member subobject that is empty. + void ComputeEmptySubobjectSizes(const CXXRecordDecl *RD); + + /// DeterminePrimaryBase - Determine the primary base of the given class. + void DeterminePrimaryBase(const CXXRecordDecl *RD); + + void SelectPrimaryVBase(const CXXRecordDecl *RD); + + /// IdentifyPrimaryBases - Identify all virtual base classes, direct or + /// indirect, that are primary base classes for some other direct or indirect + /// base class. + void IdentifyPrimaryBases(const CXXRecordDecl *RD); + + bool IsNearlyEmpty(const CXXRecordDecl *RD) const; + + /// LayoutNonVirtualBases - Determines the primary base class (if any) and + /// lays it out. Will then proceed to lay out all non-virtual base clasess. + void LayoutNonVirtualBases(const CXXRecordDecl *RD); + + /// LayoutNonVirtualBase - Lays out a single non-virtual base. + void LayoutNonVirtualBase(const CXXRecordDecl *Base); + + void AddPrimaryVirtualBaseOffsets(const CXXRecordDecl *RD, uint64_t Offset, + const CXXRecordDecl *MostDerivedClass); + + /// LayoutVirtualBases - Lays out all the virtual bases. + void LayoutVirtualBases(const CXXRecordDecl *RD, + const CXXRecordDecl *MostDerivedClass); + + /// LayoutVirtualBase - Lays out a single virtual base. + void LayoutVirtualBase(const CXXRecordDecl *Base); + + /// LayoutBase - Will lay out a base and return the offset where it was + /// placed, in bits. + uint64_t LayoutBase(const CXXRecordDecl *Base, bool BaseIsVirtual); + + /// canPlaceRecordAtOffset - Return whether a record (either a base class + /// or a field) can be placed at the given offset. + /// Returns false if placing the record will result in two components + /// (direct or indirect) of the same type having the same offset. + bool canPlaceRecordAtOffset(const CXXRecordDecl *RD, uint64_t Offset, + bool CheckVBases) const; + + /// canPlaceFieldAtOffset - Return whether a field can be placed at the given + /// offset. + bool canPlaceFieldAtOffset(const FieldDecl *FD, uint64_t Offset) const; + + /// UpdateEmptyClassOffsets - Called after a record (either a base class + /// or a field) has been placed at the given offset. Will update the + /// EmptyClassOffsets map if the class is empty or has any empty bases or + /// fields. + void UpdateEmptyClassOffsets(const CXXRecordDecl *RD, uint64_t Offset, + bool UpdateVBases); + + /// UpdateEmptyClassOffsets - Called after a field has been placed at the + /// given offset. + void UpdateEmptyClassOffsets(const FieldDecl *FD, uint64_t Offset); + + /// InitializeLayout - Initialize record layout for the given record decl. + void InitializeLayout(const Decl *D); + + /// FinishLayout - Finalize record layout. Adjust record size based on the + /// alignment. + void FinishLayout(); + + void UpdateAlignment(unsigned NewAlignment); + + RecordLayoutBuilder(const RecordLayoutBuilder&); // DO NOT IMPLEMENT + void operator=(const RecordLayoutBuilder&); // DO NOT IMPLEMENT +public: + static const CXXMethodDecl *ComputeKeyFunction(const CXXRecordDecl *RD); +}; +} // end anonymous namespace /// IsNearlyEmpty - Indicates when a class has a vtable pointer, but /// no other data. -bool ASTRecordLayoutBuilder::IsNearlyEmpty(const CXXRecordDecl *RD) const { +bool RecordLayoutBuilder::IsNearlyEmpty(const CXXRecordDecl *RD) const { // FIXME: Audit the corners if (!RD->isDynamicClass()) return false; @@ -38,7 +453,7 @@ bool ASTRecordLayoutBuilder::IsNearlyEmpty(const CXXRecordDecl *RD) const { return false; } -void ASTRecordLayoutBuilder::IdentifyPrimaryBases(const CXXRecordDecl *RD) { +void RecordLayoutBuilder::IdentifyPrimaryBases(const CXXRecordDecl *RD) { const ASTRecordLayout::PrimaryBaseInfo &BaseInfo = Context.getASTRecordLayout(RD).getPrimaryBaseInfo(); @@ -63,7 +478,7 @@ void ASTRecordLayoutBuilder::IdentifyPrimaryBases(const CXXRecordDecl *RD) { } void -ASTRecordLayoutBuilder::SelectPrimaryVBase(const CXXRecordDecl *RD) { +RecordLayoutBuilder::SelectPrimaryVBase(const CXXRecordDecl *RD) { for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), E = RD->bases_end(); I != E; ++I) { assert(!I->getType()->isDependentType() && @@ -77,8 +492,8 @@ ASTRecordLayoutBuilder::SelectPrimaryVBase(const CXXRecordDecl *RD) { // If it's not an indirect primary base, then we've found our primary // base. if (!IndirectPrimaryBases.count(Base)) { - PrimaryBase = ASTRecordLayout::PrimaryBaseInfo(Base, - /*IsVirtual=*/true); + PrimaryBase = Base; + PrimaryBaseIsVirtual = true; return; } @@ -88,13 +503,13 @@ ASTRecordLayoutBuilder::SelectPrimaryVBase(const CXXRecordDecl *RD) { } SelectPrimaryVBase(Base); - if (PrimaryBase.getBase()) + if (PrimaryBase) return; } } /// DeterminePrimaryBase - Determine the primary base of the given class. -void ASTRecordLayoutBuilder::DeterminePrimaryBase(const CXXRecordDecl *RD) { +void RecordLayoutBuilder::DeterminePrimaryBase(const CXXRecordDecl *RD) { // If the class isn't dynamic, it won't have a primary base. if (!RD->isDynamicClass()) return; @@ -124,7 +539,8 @@ void ASTRecordLayoutBuilder::DeterminePrimaryBase(const CXXRecordDecl *RD) { if (Base->isDynamicClass()) { // We found it. - PrimaryBase = ASTRecordLayout::PrimaryBaseInfo(Base, /*IsVirtual=*/false); + PrimaryBase = Base; + PrimaryBaseIsVirtual = false; return; } } @@ -133,20 +549,20 @@ void ASTRecordLayoutBuilder::DeterminePrimaryBase(const CXXRecordDecl *RD) { // indirect primary virtual base class, if one exists. if (RD->getNumVBases() != 0) { SelectPrimaryVBase(RD); - if (PrimaryBase.getBase()) + if (PrimaryBase) return; } // Otherwise, it is the first nearly empty virtual base that is not an // indirect primary virtual base class, if one exists. if (FirstNearlyEmptyVBase) { - PrimaryBase = ASTRecordLayout::PrimaryBaseInfo(FirstNearlyEmptyVBase, - /*IsVirtual=*/true); + PrimaryBase = FirstNearlyEmptyVBase; + PrimaryBaseIsVirtual = true; return; } // Otherwise there is no primary base class. - assert(!PrimaryBase.getBase() && "Should not get here with a primary base!"); + assert(!PrimaryBase && "Should not get here with a primary base!"); // Allocate the virtual table pointer at offset zero. assert(DataSize == 0 && "Vtable pointer must be at offset zero!"); @@ -160,22 +576,23 @@ void ASTRecordLayoutBuilder::DeterminePrimaryBase(const CXXRecordDecl *RD) { } void -ASTRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD) { +RecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD) { // First, determine the primary base class. DeterminePrimaryBase(RD); // If we have a primary base class, lay it out. - if (const CXXRecordDecl *Base = PrimaryBase.getBase()) { - if (PrimaryBase.isVirtual()) { + if (PrimaryBase) { + if (PrimaryBaseIsVirtual) { // We have a virtual primary base, insert it as an indirect primary base. - IndirectPrimaryBases.insert(Base); + IndirectPrimaryBases.insert(PrimaryBase); - assert(!VisitedVirtualBases.count(Base) && "vbase already visited!"); - VisitedVirtualBases.insert(Base); - - LayoutVirtualBase(Base); + assert(!VisitedVirtualBases.count(PrimaryBase) && + "vbase already visited!"); + VisitedVirtualBases.insert(PrimaryBase); + + LayoutVirtualBase(PrimaryBase); } else - LayoutNonVirtualBase(Base); + LayoutNonVirtualBase(PrimaryBase); } // Now lay out the non-virtual bases. @@ -190,7 +607,7 @@ ASTRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD) { cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); // Skip the primary base. - if (Base == PrimaryBase.getBase() && !PrimaryBase.isVirtual()) + if (Base == PrimaryBase && !PrimaryBaseIsVirtual) continue; // Lay out the base. @@ -198,17 +615,17 @@ ASTRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD) { } } -void ASTRecordLayoutBuilder::LayoutNonVirtualBase(const CXXRecordDecl *RD) { +void RecordLayoutBuilder::LayoutNonVirtualBase(const CXXRecordDecl *Base) { // Layout the base. - uint64_t Offset = LayoutBase(RD); + uint64_t Offset = LayoutBase(Base, /*BaseIsVirtual=*/false); // Add its base class offset. - if (!Bases.insert(std::make_pair(RD, Offset)).second) + if (!Bases.insert(std::make_pair(Base, Offset)).second) assert(false && "Added same base offset more than once!"); } void -ASTRecordLayoutBuilder::AddPrimaryVirtualBaseOffsets(const CXXRecordDecl *RD, +RecordLayoutBuilder::AddPrimaryVirtualBaseOffsets(const CXXRecordDecl *RD, uint64_t Offset, const CXXRecordDecl *MostDerivedClass) { // We already have the offset for the primary base of the most derived class. @@ -226,7 +643,7 @@ ASTRecordLayoutBuilder::AddPrimaryVirtualBaseOffsets(const CXXRecordDecl *RD, E = RD->bases_end(); I != E; ++I) { assert(!I->getType()->isDependentType() && "Cannot layout class with dependent bases."); - + const CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); @@ -234,17 +651,17 @@ ASTRecordLayoutBuilder::AddPrimaryVirtualBaseOffsets(const CXXRecordDecl *RD, // This base isn't interesting since it doesn't have any virtual bases. continue; } - + // Compute the offset of this base. uint64_t BaseOffset; - + if (I->isVirtual()) { // If we don't know this vbase yet, don't visit it. It will be visited // later. if (!VBases.count(BaseDecl)) { continue; } - + // Check if we've already visited this base. if (!VisitedVirtualBases.insert(BaseDecl)) continue; @@ -265,14 +682,14 @@ ASTRecordLayoutBuilder::AddPrimaryVirtualBaseOffsets(const CXXRecordDecl *RD, } void -ASTRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD, +RecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD, const CXXRecordDecl *MostDerivedClass) { const CXXRecordDecl *PrimaryBase; bool PrimaryBaseIsVirtual; if (MostDerivedClass == RD) { - PrimaryBase = this->PrimaryBase.getBase(); - PrimaryBaseIsVirtual = this->PrimaryBase.isVirtual(); + PrimaryBase = this->PrimaryBase; + PrimaryBaseIsVirtual = this->PrimaryBaseIsVirtual; } else { const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); PrimaryBase = Layout.getPrimaryBase(); @@ -296,7 +713,7 @@ ASTRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD, // Only visit virtual bases once. if (!VisitedVirtualBases.insert(Base)) continue; - + LayoutVirtualBase(Base); } } @@ -311,58 +728,64 @@ ASTRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD, } } -void ASTRecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *RD) { +void RecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *Base) { // Layout the base. - uint64_t Offset = LayoutBase(RD); + uint64_t Offset = LayoutBase(Base, /*BaseIsVirtual=*/true); // Add its base class offset. - if (!VBases.insert(std::make_pair(RD, Offset)).second) + if (!VBases.insert(std::make_pair(Base, Offset)).second) assert(false && "Added same vbase offset more than once!"); } -uint64_t ASTRecordLayoutBuilder::LayoutBase(const CXXRecordDecl *RD) { - const ASTRecordLayout &BaseInfo = Context.getASTRecordLayout(RD); +uint64_t RecordLayoutBuilder::LayoutBase(const CXXRecordDecl *Base, + bool BaseIsVirtual) { + const ASTRecordLayout &Layout = Context.getASTRecordLayout(Base); // If we have an empty base class, try to place it at offset 0. - if (RD->isEmpty() && canPlaceRecordAtOffset(RD, 0)) { + if (Base->isEmpty() && + EmptySubobjects->CanPlaceBaseAtOffset(Base, BaseIsVirtual, 0) && + canPlaceRecordAtOffset(Base, 0, /*CheckVBases=*/false)) { // We were able to place the class at offset 0. - UpdateEmptyClassOffsets(RD, 0); + UpdateEmptyClassOffsets(Base, 0, /*UpdateVBases=*/false); - Size = std::max(Size, BaseInfo.getSize()); + Size = std::max(Size, Layout.getSize()); return 0; } - unsigned BaseAlign = BaseInfo.getNonVirtualAlign(); + unsigned BaseAlign = Layout.getNonVirtualAlign(); // Round up the current record size to the base's alignment boundary. uint64_t Offset = llvm::RoundUpToAlignment(DataSize, BaseAlign); // Try to place the base. while (true) { - if (canPlaceRecordAtOffset(RD, Offset)) + if (EmptySubobjects->CanPlaceBaseAtOffset(Base, BaseIsVirtual, Offset) && + canPlaceRecordAtOffset(Base, Offset, /*CheckVBases=*/false)) break; Offset += BaseAlign; } - if (!RD->isEmpty()) { + if (!Base->isEmpty()) { // Update the data size. - DataSize = Offset + BaseInfo.getNonVirtualSize(); + DataSize = Offset + Layout.getNonVirtualSize(); Size = std::max(Size, DataSize); } else - Size = std::max(Size, Offset + BaseInfo.getSize()); + Size = std::max(Size, Offset + Layout.getSize()); // Remember max struct/class alignment. UpdateAlignment(BaseAlign); - UpdateEmptyClassOffsets(RD, Offset); + UpdateEmptyClassOffsets(Base, Offset, /*UpdateVBases=*/false); return Offset; } -bool ASTRecordLayoutBuilder::canPlaceRecordAtOffset(const CXXRecordDecl *RD, - uint64_t Offset) const { +bool +RecordLayoutBuilder::canPlaceRecordAtOffset(const CXXRecordDecl *RD, + uint64_t Offset, + bool CheckVBases) const { // Look for an empty class with the same type at the same offset. for (EmptyClassOffsetsTy::const_iterator I = EmptyClassOffsets.lower_bound(Offset), @@ -372,7 +795,7 @@ bool ASTRecordLayoutBuilder::canPlaceRecordAtOffset(const CXXRecordDecl *RD, return false; } - const ASTRecordLayout &Info = Context.getASTRecordLayout(RD); + const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); // Check bases. for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), @@ -382,12 +805,13 @@ bool ASTRecordLayoutBuilder::canPlaceRecordAtOffset(const CXXRecordDecl *RD, if (I->isVirtual()) continue; - const CXXRecordDecl *Base = + const CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); - uint64_t BaseClassOffset = Info.getBaseClassOffset(Base); + uint64_t BaseOffset = Layout.getBaseClassOffset(BaseDecl); - if (!canPlaceRecordAtOffset(Base, Offset + BaseClassOffset)) + if (!canPlaceRecordAtOffset(BaseDecl, Offset + BaseOffset, + /*CheckVBases=*/false)) return false; } @@ -397,22 +821,25 @@ bool ASTRecordLayoutBuilder::canPlaceRecordAtOffset(const CXXRecordDecl *RD, I != E; ++I, ++FieldNo) { const FieldDecl *FD = *I; - uint64_t FieldOffset = Info.getFieldOffset(FieldNo); + uint64_t FieldOffset = Layout.getFieldOffset(FieldNo); if (!canPlaceFieldAtOffset(FD, Offset + FieldOffset)) return false; } - // FIXME: virtual bases. + if (CheckVBases) { + // FIXME: virtual bases. + } + return true; } -bool ASTRecordLayoutBuilder::canPlaceFieldAtOffset(const FieldDecl *FD, +bool RecordLayoutBuilder::canPlaceFieldAtOffset(const FieldDecl *FD, uint64_t Offset) const { QualType T = FD->getType(); if (const RecordType *RT = T->getAs<RecordType>()) { if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) - return canPlaceRecordAtOffset(RD, Offset); + return canPlaceRecordAtOffset(RD, Offset, /*CheckVBases=*/true); } if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) { @@ -424,27 +851,28 @@ bool ASTRecordLayoutBuilder::canPlaceFieldAtOffset(const FieldDecl *FD, if (!RD) return true; - const ASTRecordLayout &Info = Context.getASTRecordLayout(RD); + const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); uint64_t NumElements = Context.getConstantArrayElementCount(AT); uint64_t ElementOffset = Offset; for (uint64_t I = 0; I != NumElements; ++I) { - if (!canPlaceRecordAtOffset(RD, ElementOffset)) + if (!canPlaceRecordAtOffset(RD, ElementOffset, /*CheckVBases=*/true)) return false; - ElementOffset += Info.getSize(); + ElementOffset += Layout.getSize(); } } return true; } -void ASTRecordLayoutBuilder::UpdateEmptyClassOffsets(const CXXRecordDecl *RD, - uint64_t Offset) { +void RecordLayoutBuilder::UpdateEmptyClassOffsets(const CXXRecordDecl *RD, + uint64_t Offset, + bool UpdateVBases) { if (RD->isEmpty()) EmptyClassOffsets.insert(std::make_pair(Offset, RD)); - const ASTRecordLayout &Info = Context.getASTRecordLayout(RD); + const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); // Update bases. for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), @@ -457,8 +885,9 @@ void ASTRecordLayoutBuilder::UpdateEmptyClassOffsets(const CXXRecordDecl *RD, const CXXRecordDecl *Base = cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); - uint64_t BaseClassOffset = Info.getBaseClassOffset(Base); - UpdateEmptyClassOffsets(Base, Offset + BaseClassOffset); + uint64_t BaseClassOffset = Layout.getBaseClassOffset(Base); + UpdateEmptyClassOffsets(Base, Offset + BaseClassOffset, + /*UpdateVBases=*/false); } // Update fields. @@ -467,21 +896,30 @@ void ASTRecordLayoutBuilder::UpdateEmptyClassOffsets(const CXXRecordDecl *RD, I != E; ++I, ++FieldNo) { const FieldDecl *FD = *I; - uint64_t FieldOffset = Info.getFieldOffset(FieldNo); + uint64_t FieldOffset = Layout.getFieldOffset(FieldNo); UpdateEmptyClassOffsets(FD, Offset + FieldOffset); } - // FIXME: Update virtual bases. + const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); + + if (UpdateVBases) { + // FIXME: Update virtual bases. + } else if (PrimaryBase && Layout.getPrimaryBaseWasVirtual()) { + // We always want to update the offsets of a primary virtual base. + assert(Layout.getVBaseClassOffset(PrimaryBase) == 0 && + "primary base class offset must always be 0!"); + UpdateEmptyClassOffsets(PrimaryBase, Offset, /*UpdateVBases=*/false); + } } void -ASTRecordLayoutBuilder::UpdateEmptyClassOffsets(const FieldDecl *FD, +RecordLayoutBuilder::UpdateEmptyClassOffsets(const FieldDecl *FD, uint64_t Offset) { QualType T = FD->getType(); if (const RecordType *RT = T->getAs<RecordType>()) { if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { - UpdateEmptyClassOffsets(RD, Offset); + UpdateEmptyClassOffsets(RD, Offset, /*UpdateVBases=*/true); return; } } @@ -501,76 +939,90 @@ ASTRecordLayoutBuilder::UpdateEmptyClassOffsets(const FieldDecl *FD, uint64_t ElementOffset = Offset; for (uint64_t I = 0; I != NumElements; ++I) { - UpdateEmptyClassOffsets(RD, ElementOffset); + UpdateEmptyClassOffsets(RD, ElementOffset, /*UpdateVBases=*/true); ElementOffset += Info.getSize(); } } } -void ASTRecordLayoutBuilder::Layout(const RecordDecl *D) { - IsUnion = D->isUnion(); +void RecordLayoutBuilder::InitializeLayout(const Decl *D) { + if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) + IsUnion = RD->isUnion(); Packed = D->hasAttr<PackedAttr>(); - // The #pragma pack attribute specifies the maximum field alignment. - if (const PragmaPackAttr *PPA = D->getAttr<PragmaPackAttr>()) - MaxFieldAlignment = PPA->getAlignment(); - - if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) - UpdateAlignment(AA->getMaxAlignment()); + // mac68k alignment supersedes maximum field alignment and attribute aligned, + // and forces all structures to have 2-byte alignment. The IBM docs on it + // allude to additional (more complicated) semantics, especially with regard + // to bit-fields, but gcc appears not to follow that. + if (D->hasAttr<AlignMac68kAttr>()) { + IsMac68kAlign = true; + MaxFieldAlignment = 2 * 8; + Alignment = 2 * 8; + } else { + if (const MaxFieldAlignmentAttr *MFAA = D->getAttr<MaxFieldAlignmentAttr>()) + MaxFieldAlignment = MFAA->getAlignment(); - // If this is a C++ class, lay out the vtable and the non-virtual bases. - const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D); - if (RD) - LayoutNonVirtualBases(RD); + if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) + UpdateAlignment(AA->getMaxAlignment()); + } +} +void RecordLayoutBuilder::Layout(const RecordDecl *D) { + InitializeLayout(D); LayoutFields(D); + // Finally, round the size of the total struct up to the alignment of the + // struct itself. + FinishLayout(); +} + +void RecordLayoutBuilder::Layout(const CXXRecordDecl *RD) { + InitializeLayout(RD); + + // Lay out the vtable and the non-virtual bases. + LayoutNonVirtualBases(RD); + + LayoutFields(RD); + NonVirtualSize = Size; NonVirtualAlignment = Alignment; - // If this is a C++ class, lay out its virtual bases and add its primary - // virtual base offsets. - if (RD) { - LayoutVirtualBases(RD, RD); + // Lay out the virtual bases and add the primary virtual base offsets. + LayoutVirtualBases(RD, RD); - VisitedVirtualBases.clear(); - AddPrimaryVirtualBaseOffsets(RD, 0, RD); - } + VisitedVirtualBases.clear(); + AddPrimaryVirtualBaseOffsets(RD, 0, RD); // Finally, round the size of the total struct up to the alignment of the // struct itself. FinishLayout(); - + #ifndef NDEBUG - if (RD) { - // Check that we have base offsets for all bases. - for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), - E = RD->bases_end(); I != E; ++I) { - if (I->isVirtual()) - continue; - - const CXXRecordDecl *BaseDecl = - cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); - - assert(Bases.count(BaseDecl) && "Did not find base offset!"); - } - - // And all virtual bases. - for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(), - E = RD->vbases_end(); I != E; ++I) { - const CXXRecordDecl *BaseDecl = - cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); - - assert(VBases.count(BaseDecl) && "Did not find base offset!"); - } + // Check that we have base offsets for all bases. + for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), + E = RD->bases_end(); I != E; ++I) { + if (I->isVirtual()) + continue; + + const CXXRecordDecl *BaseDecl = + cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); + + assert(Bases.count(BaseDecl) && "Did not find base offset!"); + } + + // And all virtual bases. + for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(), + E = RD->vbases_end(); I != E; ++I) { + const CXXRecordDecl *BaseDecl = + cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); + + assert(VBases.count(BaseDecl) && "Did not find base offset!"); } #endif } -// FIXME. Impl is no longer needed. -void ASTRecordLayoutBuilder::Layout(const ObjCInterfaceDecl *D, - const ObjCImplementationDecl *Impl) { +void RecordLayoutBuilder::Layout(const ObjCInterfaceDecl *D) { if (ObjCInterfaceDecl *SD = D->getSuperClass()) { const ASTRecordLayout &SL = Context.getASTObjCInterfaceLayout(SD); @@ -582,14 +1034,8 @@ void ASTRecordLayoutBuilder::Layout(const ObjCInterfaceDecl *D, DataSize = Size; } - Packed = D->hasAttr<PackedAttr>(); - - // The #pragma pack attribute specifies the maximum field alignment. - if (const PragmaPackAttr *PPA = D->getAttr<PragmaPackAttr>()) - MaxFieldAlignment = PPA->getAlignment(); + InitializeLayout(D); - if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) - UpdateAlignment(AA->getMaxAlignment()); // Layout each ivar sequentially. llvm::SmallVector<ObjCIvarDecl*, 16> Ivars; Context.ShallowCollectObjCIvars(D, Ivars); @@ -601,7 +1047,7 @@ void ASTRecordLayoutBuilder::Layout(const ObjCInterfaceDecl *D, FinishLayout(); } -void ASTRecordLayoutBuilder::LayoutFields(const RecordDecl *D) { +void RecordLayoutBuilder::LayoutFields(const RecordDecl *D) { // Layout each field, for now, just sequentially, respecting alignment. In // the future, this will need to be tweakable by targets. for (RecordDecl::field_iterator Field = D->field_begin(), @@ -609,17 +1055,17 @@ void ASTRecordLayoutBuilder::LayoutFields(const RecordDecl *D) { LayoutField(*Field); } -void ASTRecordLayoutBuilder::LayoutWideBitField(uint64_t FieldSize, +void RecordLayoutBuilder::LayoutWideBitField(uint64_t FieldSize, uint64_t TypeSize) { assert(Context.getLangOptions().CPlusPlus && "Can only have wide bit-fields in C++!"); - + // Itanium C++ ABI 2.4: - // If sizeof(T)*8 < n, let T' be the largest integral POD type with + // If sizeof(T)*8 < n, let T' be the largest integral POD type with // sizeof(T')*8 <= n. - + QualType IntegralPODTypes[] = { - Context.UnsignedCharTy, Context.UnsignedShortTy, Context.UnsignedIntTy, + Context.UnsignedCharTy, Context.UnsignedShortTy, Context.UnsignedIntTy, Context.UnsignedLongTy, Context.UnsignedLongLongTy }; @@ -634,24 +1080,24 @@ void ASTRecordLayoutBuilder::LayoutWideBitField(uint64_t FieldSize, Type = IntegralPODTypes[I]; } assert(!Type.isNull() && "Did not find a type!"); - + unsigned TypeAlign = Context.getTypeAlign(Type); // We're not going to use any of the unfilled bits in the last byte. UnfilledBitsInLastByte = 0; uint64_t FieldOffset; - + if (IsUnion) { DataSize = std::max(DataSize, FieldSize); FieldOffset = 0; } else { // The bitfield is allocated starting at the next offset aligned appropriately - // for T', with length n bits. + // for T', with length n bits. FieldOffset = llvm::RoundUpToAlignment(DataSize, TypeAlign); - + uint64_t NewSizeInBits = FieldOffset + FieldSize; - + DataSize = llvm::RoundUpToAlignment(NewSizeInBits, 8); UnfilledBitsInLastByte = DataSize - NewSizeInBits; } @@ -661,12 +1107,12 @@ void ASTRecordLayoutBuilder::LayoutWideBitField(uint64_t FieldSize, // Update the size. Size = std::max(Size, DataSize); - + // Remember max struct/class alignment. UpdateAlignment(TypeAlign); } -void ASTRecordLayoutBuilder::LayoutBitField(const FieldDecl *D) { +void RecordLayoutBuilder::LayoutBitField(const FieldDecl *D) { bool FieldPacked = Packed || D->hasAttr<PackedAttr>(); uint64_t FieldOffset = IsUnion ? 0 : (DataSize - UnfilledBitsInLastByte); uint64_t FieldSize = D->getBitWidth()->EvaluateAsInt(Context).getZExtValue(); @@ -718,7 +1164,7 @@ void ASTRecordLayoutBuilder::LayoutBitField(const FieldDecl *D) { UpdateAlignment(FieldAlign); } -void ASTRecordLayoutBuilder::LayoutField(const FieldDecl *D) { +void RecordLayoutBuilder::LayoutField(const FieldDecl *D) { if (D->isBitField()) { LayoutBitField(D); return; @@ -791,7 +1237,7 @@ void ASTRecordLayoutBuilder::LayoutField(const FieldDecl *D) { UpdateAlignment(FieldAlign); } -void ASTRecordLayoutBuilder::FinishLayout() { +void RecordLayoutBuilder::FinishLayout() { // In C++, records cannot be of size 0. if (Context.getLangOptions().CPlusPlus && Size == 0) Size = 8; @@ -800,7 +1246,11 @@ void ASTRecordLayoutBuilder::FinishLayout() { Size = llvm::RoundUpToAlignment(Size, Alignment); } -void ASTRecordLayoutBuilder::UpdateAlignment(unsigned NewAlignment) { +void RecordLayoutBuilder::UpdateAlignment(unsigned NewAlignment) { + // The alignment is not modified when using 'mac68k' alignment. + if (IsMac68kAlign) + return; + if (NewAlignment <= Alignment) return; @@ -809,55 +1259,8 @@ void ASTRecordLayoutBuilder::UpdateAlignment(unsigned NewAlignment) { Alignment = NewAlignment; } -const ASTRecordLayout * -ASTRecordLayoutBuilder::ComputeLayout(ASTContext &Ctx, - const RecordDecl *D) { - ASTRecordLayoutBuilder Builder(Ctx); - - Builder.Layout(D); - - if (!isa<CXXRecordDecl>(D)) - return new (Ctx) ASTRecordLayout(Ctx, Builder.Size, Builder.Alignment, - Builder.Size, - Builder.FieldOffsets.data(), - Builder.FieldOffsets.size()); - - // FIXME: This is not always correct. See the part about bitfields at - // http://www.codesourcery.com/public/cxx-abi/abi.html#POD for more info. - // FIXME: IsPODForThePurposeOfLayout should be stored in the record layout. - bool IsPODForThePurposeOfLayout = cast<CXXRecordDecl>(D)->isPOD(); - - // FIXME: This should be done in FinalizeLayout. - uint64_t DataSize = - IsPODForThePurposeOfLayout ? Builder.Size : Builder.DataSize; - uint64_t NonVirtualSize = - IsPODForThePurposeOfLayout ? DataSize : Builder.NonVirtualSize; - - return new (Ctx) ASTRecordLayout(Ctx, Builder.Size, Builder.Alignment, - DataSize, Builder.FieldOffsets.data(), - Builder.FieldOffsets.size(), - NonVirtualSize, - Builder.NonVirtualAlignment, - Builder.PrimaryBase, - Builder.Bases, Builder.VBases); -} - -const ASTRecordLayout * -ASTRecordLayoutBuilder::ComputeLayout(ASTContext &Ctx, - const ObjCInterfaceDecl *D, - const ObjCImplementationDecl *Impl) { - ASTRecordLayoutBuilder Builder(Ctx); - - Builder.Layout(D, Impl); - - return new (Ctx) ASTRecordLayout(Ctx, Builder.Size, Builder.Alignment, - Builder.DataSize, - Builder.FieldOffsets.data(), - Builder.FieldOffsets.size()); -} - const CXXMethodDecl * -ASTRecordLayoutBuilder::ComputeKeyFunction(const CXXRecordDecl *RD) { +RecordLayoutBuilder::ComputeKeyFunction(const CXXRecordDecl *RD) { assert(RD->isDynamicClass() && "Class does not have any virtual methods!"); // If a class isn't polymorphic it doesn't have a key function. @@ -898,6 +1301,124 @@ ASTRecordLayoutBuilder::ComputeKeyFunction(const CXXRecordDecl *RD) { return 0; } +/// getASTRecordLayout - Get or compute information about the layout of the +/// specified record (struct/union/class), which indicates its size and field +/// position information. +const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) { + D = D->getDefinition(); + assert(D && "Cannot get layout of forward declarations!"); + + // Look up this layout, if already laid out, return what we have. + // Note that we can't save a reference to the entry because this function + // is recursive. + const ASTRecordLayout *Entry = ASTRecordLayouts[D]; + if (Entry) return *Entry; + + const ASTRecordLayout *NewEntry; + + if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { + EmptySubobjectMap EmptySubobjects(*this, RD); + + RecordLayoutBuilder Builder(*this, &EmptySubobjects); + Builder.Layout(RD); + + // FIXME: This is not always correct. See the part about bitfields at + // http://www.codesourcery.com/public/cxx-abi/abi.html#POD for more info. + // FIXME: IsPODForThePurposeOfLayout should be stored in the record layout. + bool IsPODForThePurposeOfLayout = cast<CXXRecordDecl>(D)->isPOD(); + + // FIXME: This should be done in FinalizeLayout. + uint64_t DataSize = + IsPODForThePurposeOfLayout ? Builder.Size : Builder.DataSize; + uint64_t NonVirtualSize = + IsPODForThePurposeOfLayout ? DataSize : Builder.NonVirtualSize; + + NewEntry = + new (*this) ASTRecordLayout(*this, Builder.Size, Builder.Alignment, + DataSize, Builder.FieldOffsets.data(), + Builder.FieldOffsets.size(), + NonVirtualSize, + Builder.NonVirtualAlignment, + EmptySubobjects.SizeOfLargestEmptySubobject, + Builder.PrimaryBase, + Builder.PrimaryBaseIsVirtual, + Builder.Bases, Builder.VBases); + } else { + RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/0); + Builder.Layout(D); + + NewEntry = + new (*this) ASTRecordLayout(*this, Builder.Size, Builder.Alignment, + Builder.Size, + Builder.FieldOffsets.data(), + Builder.FieldOffsets.size()); + } + + ASTRecordLayouts[D] = NewEntry; + + if (getLangOptions().DumpRecordLayouts) { + llvm::errs() << "\n*** Dumping AST Record Layout\n"; + DumpRecordLayout(D, llvm::errs()); + } + + return *NewEntry; +} + +const CXXMethodDecl *ASTContext::getKeyFunction(const CXXRecordDecl *RD) { + RD = cast<CXXRecordDecl>(RD->getDefinition()); + assert(RD && "Cannot get key function for forward declarations!"); + + const CXXMethodDecl *&Entry = KeyFunctions[RD]; + if (!Entry) + Entry = RecordLayoutBuilder::ComputeKeyFunction(RD); + else + assert(Entry == RecordLayoutBuilder::ComputeKeyFunction(RD) && + "Key function changed!"); + + return Entry; +} + +/// getInterfaceLayoutImpl - Get or compute information about the +/// layout of the given interface. +/// +/// \param Impl - If given, also include the layout of the interface's +/// implementation. This may differ by including synthesized ivars. +const ASTRecordLayout & +ASTContext::getObjCLayout(const ObjCInterfaceDecl *D, + const ObjCImplementationDecl *Impl) { + assert(!D->isForwardDecl() && "Invalid interface decl!"); + + // Look up this layout, if already laid out, return what we have. + ObjCContainerDecl *Key = + Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D; + if (const ASTRecordLayout *Entry = ObjCLayouts[Key]) + return *Entry; + + // Add in synthesized ivar count if laying out an implementation. + if (Impl) { + unsigned SynthCount = CountNonClassIvars(D); + // If there aren't any sythesized ivars then reuse the interface + // entry. Note we can't cache this because we simply free all + // entries later; however we shouldn't look up implementations + // frequently. + if (SynthCount == 0) + return getObjCLayout(D, 0); + } + + RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/0); + Builder.Layout(D); + + const ASTRecordLayout *NewEntry = + new (*this) ASTRecordLayout(*this, Builder.Size, Builder.Alignment, + Builder.DataSize, + Builder.FieldOffsets.data(), + Builder.FieldOffsets.size()); + + ObjCLayouts[Key] = NewEntry; + + return *NewEntry; +} + static void PrintOffset(llvm::raw_ostream &OS, uint64_t Offset, unsigned IndentLevel) { OS << llvm::format("%4d | ", Offset); diff --git a/lib/AST/RecordLayoutBuilder.h b/lib/AST/RecordLayoutBuilder.h deleted file mode 100644 index f277c29..0000000 --- a/lib/AST/RecordLayoutBuilder.h +++ /dev/null @@ -1,170 +0,0 @@ -//===- ASTRecordLayoutBuilder.h - Helper class for building record layouts ===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#ifndef LLVM_CLANG_AST_RECORDLAYOUTBUILDER_H -#define LLVM_CLANG_AST_RECORDLAYOUTBUILDER_H - -#include "clang/AST/RecordLayout.h" -#include "llvm/ADT/SmallVector.h" -#include "llvm/ADT/SmallSet.h" -#include "llvm/System/DataTypes.h" -#include <map> - -namespace clang { - class ASTContext; - class ASTRecordLayout; - class CXXRecordDecl; - class FieldDecl; - class ObjCImplementationDecl; - class ObjCInterfaceDecl; - class RecordDecl; - -class ASTRecordLayoutBuilder { - ASTContext &Context; - - /// Size - The current size of the record layout. - uint64_t Size; - - /// Alignment - The current alignment of the record layout. - unsigned Alignment; - - llvm::SmallVector<uint64_t, 16> FieldOffsets; - - /// Packed - Whether the record is packed or not. - bool Packed; - - /// UnfilledBitsInLastByte - If the last field laid out was a bitfield, - /// this contains the number of bits in the last byte that can be used for - /// an adjacent bitfield if necessary. - unsigned char UnfilledBitsInLastByte; - - /// MaxFieldAlignment - The maximum allowed field alignment. This is set by - /// #pragma pack. - unsigned MaxFieldAlignment; - - /// DataSize - The data size of the record being laid out. - uint64_t DataSize; - - bool IsUnion; - - uint64_t NonVirtualSize; - unsigned NonVirtualAlignment; - - /// PrimaryBase - the primary base class (if one exists) of the class - /// we're laying out. - ASTRecordLayout::PrimaryBaseInfo PrimaryBase; - - /// Bases - base classes and their offsets in the record. - ASTRecordLayout::BaseOffsetsMapTy Bases; - - // VBases - virtual base classes and their offsets in the record. - ASTRecordLayout::BaseOffsetsMapTy VBases; - - /// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are - /// primary base classes for some other direct or indirect base class. - llvm::SmallSet<const CXXRecordDecl*, 32> IndirectPrimaryBases; - - /// FirstNearlyEmptyVBase - The first nearly empty virtual base class in - /// inheritance graph order. Used for determining the primary base class. - const CXXRecordDecl *FirstNearlyEmptyVBase; - - /// VisitedVirtualBases - A set of all the visited virtual bases, used to - /// avoid visiting virtual bases more than once. - llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBases; - - /// EmptyClassOffsets - A map from offsets to empty record decls. - typedef std::multimap<uint64_t, const CXXRecordDecl *> EmptyClassOffsetsTy; - EmptyClassOffsetsTy EmptyClassOffsets; - - ASTRecordLayoutBuilder(ASTContext &Ctx); - - void Layout(const RecordDecl *D); - void Layout(const CXXRecordDecl *D); - void Layout(const ObjCInterfaceDecl *D, - const ObjCImplementationDecl *Impl); - - void LayoutFields(const RecordDecl *D); - void LayoutField(const FieldDecl *D); - void LayoutWideBitField(uint64_t FieldSize, uint64_t TypeSize); - void LayoutBitField(const FieldDecl *D); - - /// DeterminePrimaryBase - Determine the primary base of the given class. - void DeterminePrimaryBase(const CXXRecordDecl *RD); - - void SelectPrimaryVBase(const CXXRecordDecl *RD); - - /// IdentifyPrimaryBases - Identify all virtual base classes, direct or - /// indirect, that are primary base classes for some other direct or indirect - /// base class. - void IdentifyPrimaryBases(const CXXRecordDecl *RD); - - bool IsNearlyEmpty(const CXXRecordDecl *RD) const; - - /// LayoutNonVirtualBases - Determines the primary base class (if any) and - /// lays it out. Will then proceed to lay out all non-virtual base clasess. - void LayoutNonVirtualBases(const CXXRecordDecl *RD); - - /// LayoutNonVirtualBase - Lays out a single non-virtual base. - void LayoutNonVirtualBase(const CXXRecordDecl *RD); - - void AddPrimaryVirtualBaseOffsets(const CXXRecordDecl *RD, uint64_t Offset, - const CXXRecordDecl *MostDerivedClass); - - /// LayoutVirtualBases - Lays out all the virtual bases. - void LayoutVirtualBases(const CXXRecordDecl *RD, - const CXXRecordDecl *MostDerivedClass); - - /// LayoutVirtualBase - Lays out a single virtual base. - void LayoutVirtualBase(const CXXRecordDecl *RD); - - /// LayoutBase - Will lay out a base and return the offset where it was - /// placed, in bits. - uint64_t LayoutBase(const CXXRecordDecl *RD); - - /// canPlaceRecordAtOffset - Return whether a record (either a base class - /// or a field) can be placed at the given offset. - /// Returns false if placing the record will result in two components - /// (direct or indirect) of the same type having the same offset. - bool canPlaceRecordAtOffset(const CXXRecordDecl *RD, uint64_t Offset) const; - - /// canPlaceFieldAtOffset - Return whether a field can be placed at the given - /// offset. - bool canPlaceFieldAtOffset(const FieldDecl *FD, uint64_t Offset) const; - - /// UpdateEmptyClassOffsets - Called after a record (either a base class - /// or a field) has been placed at the given offset. Will update the - /// EmptyClassOffsets map if the class is empty or has any empty bases or - /// fields. - void UpdateEmptyClassOffsets(const CXXRecordDecl *RD, uint64_t Offset); - - /// UpdateEmptyClassOffsets - Called after a field has been placed at the - /// given offset. - void UpdateEmptyClassOffsets(const FieldDecl *FD, uint64_t Offset); - - /// FinishLayout - Finalize record layout. Adjust record size based on the - /// alignment. - void FinishLayout(); - - void UpdateAlignment(unsigned NewAlignment); - - ASTRecordLayoutBuilder(const ASTRecordLayoutBuilder&); // DO NOT IMPLEMENT - void operator=(const ASTRecordLayoutBuilder&); // DO NOT IMPLEMENT -public: - static const ASTRecordLayout *ComputeLayout(ASTContext &Ctx, - const RecordDecl *RD); - static const ASTRecordLayout *ComputeLayout(ASTContext &Ctx, - const ObjCInterfaceDecl *D, - const ObjCImplementationDecl *Impl); - static const CXXMethodDecl *ComputeKeyFunction(const CXXRecordDecl *RD); -}; - -} // end namespace clang - -#endif - diff --git a/lib/AST/Stmt.cpp b/lib/AST/Stmt.cpp index 67fd74c..80f5695 100644 --- a/lib/AST/Stmt.cpp +++ b/lib/AST/Stmt.cpp @@ -27,7 +27,7 @@ static struct StmtClassNameTable { const char *Name; unsigned Counter; unsigned Size; -} StmtClassInfo[Stmt::lastExprConstant+1]; +} StmtClassInfo[Stmt::lastStmtConstant+1]; static StmtClassNameTable &getStmtInfoTableEntry(Stmt::StmtClass E) { static bool Initialized = false; @@ -36,11 +36,11 @@ static StmtClassNameTable &getStmtInfoTableEntry(Stmt::StmtClass E) { // Intialize the table on the first use. Initialized = true; -#define ABSTRACT_EXPR(CLASS, PARENT) +#define ABSTRACT_STMT(STMT) #define STMT(CLASS, PARENT) \ StmtClassInfo[(unsigned)Stmt::CLASS##Class].Name = #CLASS; \ StmtClassInfo[(unsigned)Stmt::CLASS##Class].Size = sizeof(CLASS); -#include "clang/AST/StmtNodes.def" +#include "clang/AST/StmtNodes.inc" return StmtClassInfo[E]; } @@ -55,13 +55,13 @@ void Stmt::PrintStats() { unsigned sum = 0; fprintf(stderr, "*** Stmt/Expr Stats:\n"); - for (int i = 0; i != Stmt::lastExprConstant+1; i++) { + for (int i = 0; i != Stmt::lastStmtConstant+1; i++) { if (StmtClassInfo[i].Name == 0) continue; sum += StmtClassInfo[i].Counter; } fprintf(stderr, " %d stmts/exprs total.\n", sum); sum = 0; - for (int i = 0; i != Stmt::lastExprConstant+1; i++) { + for (int i = 0; i != Stmt::lastStmtConstant+1; i++) { if (StmtClassInfo[i].Name == 0) continue; if (StmtClassInfo[i].Counter == 0) continue; fprintf(stderr, " %d %s, %d each (%d bytes)\n", diff --git a/lib/AST/StmtDumper.cpp b/lib/AST/StmtDumper.cpp index a11e313..b388a3b1 100644 --- a/lib/AST/StmtDumper.cpp +++ b/lib/AST/StmtDumper.cpp @@ -66,8 +66,8 @@ namespace { DumpSubTree(*CI++); } } - OS << ')'; } + OS << ')'; } else { Indent(); OS << "<<<NULL>>>"; diff --git a/lib/AST/StmtPrinter.cpp b/lib/AST/StmtPrinter.cpp index 52f627d..9bef49c 100644 --- a/lib/AST/StmtPrinter.cpp +++ b/lib/AST/StmtPrinter.cpp @@ -89,7 +89,7 @@ namespace { void VisitStmt(Stmt *Node); #define STMT(CLASS, PARENT) \ void Visit##CLASS(CLASS *Node); -#include "clang/AST/StmtNodes.def" +#include "clang/AST/StmtNodes.inc" }; } diff --git a/lib/AST/StmtProfile.cpp b/lib/AST/StmtProfile.cpp index d45bb2f..ac3a9ee 100644 --- a/lib/AST/StmtProfile.cpp +++ b/lib/AST/StmtProfile.cpp @@ -36,7 +36,7 @@ namespace { void VisitStmt(Stmt *S); #define STMT(Node, Base) void Visit##Node(Node *S); -#include "clang/AST/StmtNodes.def" +#include "clang/AST/StmtNodes.inc" /// \brief Visit a declaration that is referenced within an expression /// or statement. @@ -430,7 +430,215 @@ void StmtProfiler::VisitBlockDeclRefExpr(BlockDeclRefExpr *S) { ID.AddBoolean(S->isConstQualAdded()); } +static Stmt::StmtClass DecodeOperatorCall(CXXOperatorCallExpr *S, + UnaryOperator::Opcode &UnaryOp, + BinaryOperator::Opcode &BinaryOp) { + switch (S->getOperator()) { + case OO_None: + case OO_New: + case OO_Delete: + case OO_Array_New: + case OO_Array_Delete: + case OO_Arrow: + case OO_Call: + case OO_Conditional: + case NUM_OVERLOADED_OPERATORS: + llvm_unreachable("Invalid operator call kind"); + return Stmt::ArraySubscriptExprClass; + + case OO_Plus: + if (S->getNumArgs() == 1) { + UnaryOp = UnaryOperator::Plus; + return Stmt::UnaryOperatorClass; + } + + BinaryOp = BinaryOperator::Add; + return Stmt::BinaryOperatorClass; + + case OO_Minus: + if (S->getNumArgs() == 1) { + UnaryOp = UnaryOperator::Minus; + return Stmt::UnaryOperatorClass; + } + + BinaryOp = BinaryOperator::Sub; + return Stmt::BinaryOperatorClass; + + case OO_Star: + if (S->getNumArgs() == 1) { + UnaryOp = UnaryOperator::Minus; + return Stmt::UnaryOperatorClass; + } + + BinaryOp = BinaryOperator::Sub; + return Stmt::BinaryOperatorClass; + + case OO_Slash: + BinaryOp = BinaryOperator::Div; + return Stmt::BinaryOperatorClass; + + case OO_Percent: + BinaryOp = BinaryOperator::Rem; + return Stmt::BinaryOperatorClass; + + case OO_Caret: + BinaryOp = BinaryOperator::Xor; + return Stmt::BinaryOperatorClass; + + case OO_Amp: + if (S->getNumArgs() == 1) { + UnaryOp = UnaryOperator::AddrOf; + return Stmt::UnaryOperatorClass; + } + + BinaryOp = BinaryOperator::And; + return Stmt::BinaryOperatorClass; + + case OO_Pipe: + BinaryOp = BinaryOperator::Or; + return Stmt::BinaryOperatorClass; + + case OO_Tilde: + UnaryOp = UnaryOperator::Not; + return Stmt::UnaryOperatorClass; + + case OO_Exclaim: + UnaryOp = UnaryOperator::LNot; + return Stmt::UnaryOperatorClass; + + case OO_Equal: + BinaryOp = BinaryOperator::Assign; + return Stmt::BinaryOperatorClass; + + case OO_Less: + BinaryOp = BinaryOperator::LT; + return Stmt::BinaryOperatorClass; + + case OO_Greater: + BinaryOp = BinaryOperator::GT; + return Stmt::BinaryOperatorClass; + + case OO_PlusEqual: + BinaryOp = BinaryOperator::AddAssign; + return Stmt::CompoundAssignOperatorClass; + + case OO_MinusEqual: + BinaryOp = BinaryOperator::SubAssign; + return Stmt::CompoundAssignOperatorClass; + + case OO_StarEqual: + BinaryOp = BinaryOperator::MulAssign; + return Stmt::CompoundAssignOperatorClass; + + case OO_SlashEqual: + BinaryOp = BinaryOperator::DivAssign; + return Stmt::CompoundAssignOperatorClass; + + case OO_PercentEqual: + BinaryOp = BinaryOperator::RemAssign; + return Stmt::CompoundAssignOperatorClass; + + case OO_CaretEqual: + BinaryOp = BinaryOperator::XorAssign; + return Stmt::CompoundAssignOperatorClass; + + case OO_AmpEqual: + BinaryOp = BinaryOperator::AndAssign; + return Stmt::CompoundAssignOperatorClass; + + case OO_PipeEqual: + BinaryOp = BinaryOperator::OrAssign; + return Stmt::CompoundAssignOperatorClass; + + case OO_LessLess: + BinaryOp = BinaryOperator::Shl; + return Stmt::BinaryOperatorClass; + + case OO_GreaterGreater: + BinaryOp = BinaryOperator::Shr; + return Stmt::BinaryOperatorClass; + + case OO_LessLessEqual: + BinaryOp = BinaryOperator::ShlAssign; + return Stmt::CompoundAssignOperatorClass; + + case OO_GreaterGreaterEqual: + BinaryOp = BinaryOperator::ShrAssign; + return Stmt::CompoundAssignOperatorClass; + + case OO_EqualEqual: + BinaryOp = BinaryOperator::EQ; + return Stmt::BinaryOperatorClass; + + case OO_ExclaimEqual: + BinaryOp = BinaryOperator::NE; + return Stmt::BinaryOperatorClass; + + case OO_LessEqual: + BinaryOp = BinaryOperator::LE; + return Stmt::BinaryOperatorClass; + + case OO_GreaterEqual: + BinaryOp = BinaryOperator::GE; + return Stmt::BinaryOperatorClass; + + case OO_AmpAmp: + BinaryOp = BinaryOperator::LAnd; + return Stmt::BinaryOperatorClass; + + case OO_PipePipe: + BinaryOp = BinaryOperator::LOr; + return Stmt::BinaryOperatorClass; + + case OO_PlusPlus: + UnaryOp = S->getNumArgs() == 1? UnaryOperator::PreInc + : UnaryOperator::PostInc; + return Stmt::UnaryOperatorClass; + + case OO_MinusMinus: + UnaryOp = S->getNumArgs() == 1? UnaryOperator::PreDec + : UnaryOperator::PostDec; + return Stmt::UnaryOperatorClass; + + case OO_Comma: + BinaryOp = BinaryOperator::Comma; + return Stmt::BinaryOperatorClass; + + + case OO_ArrowStar: + BinaryOp = BinaryOperator::PtrMemI; + return Stmt::BinaryOperatorClass; + + case OO_Subscript: + return Stmt::ArraySubscriptExprClass; + } + + llvm_unreachable("Invalid overloaded operator expression"); +} + + void StmtProfiler::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *S) { + if (S->isTypeDependent()) { + // Type-dependent operator calls are profiled like their underlying + // syntactic operator. + UnaryOperator::Opcode UnaryOp = UnaryOperator::Extension; + BinaryOperator::Opcode BinaryOp = BinaryOperator::Comma; + Stmt::StmtClass SC = DecodeOperatorCall(S, UnaryOp, BinaryOp); + + ID.AddInteger(SC); + for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I) + Visit(S->getArg(I)); + if (SC == Stmt::UnaryOperatorClass) + ID.AddInteger(UnaryOp); + else if (SC == Stmt::BinaryOperatorClass || + SC == Stmt::CompoundAssignOperatorClass) + ID.AddInteger(BinaryOp); + else + assert(SC == Stmt::ArraySubscriptExprClass); + + return; + } + VisitCallExpr(S); ID.AddInteger(S->getOperator()); } diff --git a/lib/AST/TemplateBase.cpp b/lib/AST/TemplateBase.cpp index e9b1725..1c775ef 100644 --- a/lib/AST/TemplateBase.cpp +++ b/lib/AST/TemplateBase.cpp @@ -18,6 +18,7 @@ #include "clang/AST/DeclTemplate.h" #include "clang/AST/Expr.h" #include "clang/AST/TypeLoc.h" +#include "clang/Basic/Diagnostic.h" using namespace clang; @@ -102,7 +103,7 @@ SourceRange TemplateArgumentLoc::getSourceRange() const { return getSourceDeclExpression()->getSourceRange(); case TemplateArgument::Type: - return getTypeSourceInfo()->getTypeLoc().getFullSourceRange(); + return getTypeSourceInfo()->getTypeLoc().getSourceRange(); case TemplateArgument::Template: if (getTemplateQualifierRange().isValid()) @@ -119,3 +120,42 @@ SourceRange TemplateArgumentLoc::getSourceRange() const { // Silence bonus gcc warning. return SourceRange(); } + +const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB, + const TemplateArgument &Arg) { + switch (Arg.getKind()) { + case TemplateArgument::Null: + return DB; + + case TemplateArgument::Type: + return DB << Arg.getAsType(); + + case TemplateArgument::Declaration: + return DB << Arg.getAsDecl(); + + case TemplateArgument::Integral: + return DB << Arg.getAsIntegral()->toString(10); + + case TemplateArgument::Template: + return DB << Arg.getAsTemplate(); + + case TemplateArgument::Expression: { + // This shouldn't actually ever happen, so it's okay that we're + // regurgitating an expression here. + // FIXME: We're guessing at LangOptions! + llvm::SmallString<32> Str; + llvm::raw_svector_ostream OS(Str); + LangOptions LangOpts; + LangOpts.CPlusPlus = true; + PrintingPolicy Policy(LangOpts); + Arg.getAsExpr()->printPretty(OS, 0, Policy); + return DB << OS.str(); + } + + case TemplateArgument::Pack: + // FIXME: Format arguments in a list! + return DB << "<parameter pack>"; + } + + return DB; +} diff --git a/lib/AST/Type.cpp b/lib/AST/Type.cpp index 05e7fdc..1aab65e 100644 --- a/lib/AST/Type.cpp +++ b/lib/AST/Type.cpp @@ -18,6 +18,7 @@ #include "clang/AST/DeclTemplate.h" #include "clang/AST/Expr.h" #include "clang/AST/PrettyPrinter.h" +#include "clang/Basic/Specifiers.h" #include "llvm/ADT/StringExtras.h" #include "llvm/Support/raw_ostream.h" using namespace clang; @@ -276,6 +277,9 @@ QualType Type::getPointeeType() const { /// array types and types that contain variable array types in their /// declarator bool Type::isVariablyModifiedType() const { + // FIXME: We should really keep a "variably modified" bit in Type, rather + // than walking the type hierarchy to recompute it. + // A VLA is a variably modified type. if (isVariableArrayType()) return true; @@ -344,29 +348,36 @@ const RecordType *Type::getAsUnionType() const { return 0; } -ObjCInterfaceType::ObjCInterfaceType(QualType Canonical, - ObjCInterfaceDecl *D, - ObjCProtocolDecl **Protos, unsigned NumP) : - Type(ObjCInterface, Canonical, /*Dependent=*/false), - Decl(D), NumProtocols(NumP) -{ +void ObjCInterfaceType::Destroy(ASTContext& C) { + this->~ObjCInterfaceType(); + C.Deallocate(this); +} + +ObjCObjectType::ObjCObjectType(QualType Canonical, QualType Base, + ObjCProtocolDecl * const *Protocols, + unsigned NumProtocols) + : Type(ObjCObject, Canonical, false), + NumProtocols(NumProtocols), + BaseType(Base) { + assert(this->NumProtocols == NumProtocols && + "bitfield overflow in protocol count"); if (NumProtocols) - memcpy(reinterpret_cast<ObjCProtocolDecl**>(this + 1), Protos, - NumProtocols * sizeof(*Protos)); + memcpy(getProtocolStorage(), Protocols, + NumProtocols * sizeof(ObjCProtocolDecl*)); } -void ObjCInterfaceType::Destroy(ASTContext& C) { - this->~ObjCInterfaceType(); +void ObjCObjectTypeImpl::Destroy(ASTContext& C) { + this->~ObjCObjectTypeImpl(); C.Deallocate(this); } -const ObjCInterfaceType *Type::getAsObjCQualifiedInterfaceType() const { - // There is no sugar for ObjCInterfaceType's, just return the canonical +const ObjCObjectType *Type::getAsObjCQualifiedInterfaceType() const { + // There is no sugar for ObjCObjectType's, just return the canonical // type pointer if it is the right class. There is no typedef information to // return and these cannot be Address-space qualified. - if (const ObjCInterfaceType *OIT = getAs<ObjCInterfaceType>()) - if (OIT->getNumProtocols()) - return OIT; + if (const ObjCObjectType *T = getAs<ObjCObjectType>()) + if (T->getNumProtocols() && T->getInterface()) + return T; return 0; } @@ -374,17 +385,6 @@ bool Type::isObjCQualifiedInterfaceType() const { return getAsObjCQualifiedInterfaceType() != 0; } -ObjCObjectPointerType::ObjCObjectPointerType(QualType Canonical, QualType T, - ObjCProtocolDecl **Protos, - unsigned NumP) : - Type(ObjCObjectPointer, Canonical, /*Dependent=*/false), - PointeeType(T), NumProtocols(NumP) -{ - if (NumProtocols) - memcpy(reinterpret_cast<ObjCProtocolDecl **>(this + 1), Protos, - NumProtocols * sizeof(*Protos)); -} - void ObjCObjectPointerType::Destroy(ASTContext& C) { this->~ObjCObjectPointerType(); C.Deallocate(this); @@ -637,6 +637,8 @@ bool Type::isIncompleteType() const { case IncompleteArray: // An array of unknown size is an incomplete type (C99 6.2.5p22). return true; + case ObjCObject: + return cast<ObjCObjectType>(this)->getBaseType()->isIncompleteType(); case ObjCInterface: // ObjC interfaces are incomplete if they are @class, not @interface. return cast<ObjCInterfaceType>(this)->getDecl()->isForwardDecl(); @@ -764,36 +766,106 @@ bool Type::isSpecifierType() const { case TemplateTypeParm: case SubstTemplateTypeParm: case TemplateSpecialization: - case QualifiedName: + case Elaborated: case DependentName: case ObjCInterface: - case ObjCObjectPointer: - case Elaborated: + case ObjCObject: + case ObjCObjectPointer: // FIXME: object pointers aren't really specifiers return true; default: return false; } } -bool Type::isElaboratedTypeSpecifier() const { - if (getTypeClass() == Elaborated) +TypeWithKeyword::~TypeWithKeyword() { +} + +ElaboratedTypeKeyword +TypeWithKeyword::getKeywordForTypeSpec(unsigned TypeSpec) { + switch (TypeSpec) { + default: return ETK_None; + case TST_typename: return ETK_Typename; + case TST_class: return ETK_Class; + case TST_struct: return ETK_Struct; + case TST_union: return ETK_Union; + case TST_enum: return ETK_Enum; + } +} + +TagTypeKind +TypeWithKeyword::getTagTypeKindForTypeSpec(unsigned TypeSpec) { + switch(TypeSpec) { + case TST_class: return TTK_Class; + case TST_struct: return TTK_Struct; + case TST_union: return TTK_Union; + case TST_enum: return TTK_Enum; + default: llvm_unreachable("Type specifier is not a tag type kind."); + } +} + +ElaboratedTypeKeyword +TypeWithKeyword::getKeywordForTagTypeKind(TagTypeKind Kind) { + switch (Kind) { + case TTK_Class: return ETK_Class; + case TTK_Struct: return ETK_Struct; + case TTK_Union: return ETK_Union; + case TTK_Enum: return ETK_Enum; + } + llvm_unreachable("Unknown tag type kind."); +} + +TagTypeKind +TypeWithKeyword::getTagTypeKindForKeyword(ElaboratedTypeKeyword Keyword) { + switch (Keyword) { + case ETK_Class: return TTK_Class; + case ETK_Struct: return TTK_Struct; + case ETK_Union: return TTK_Union; + case ETK_Enum: return TTK_Enum; + case ETK_None: // Fall through. + case ETK_Typename: + llvm_unreachable("Elaborated type keyword is not a tag type kind."); + } + llvm_unreachable("Unknown elaborated type keyword."); +} + +bool +TypeWithKeyword::KeywordIsTagTypeKind(ElaboratedTypeKeyword Keyword) { + switch (Keyword) { + case ETK_None: + case ETK_Typename: + return false; + case ETK_Class: + case ETK_Struct: + case ETK_Union: + case ETK_Enum: return true; - - if (const DependentNameType *Dependent = dyn_cast<DependentNameType>(this)) { - switch (Dependent->getKeyword()) { - case ETK_None: - case ETK_Typename: - return false; - - case ETK_Class: - case ETK_Struct: - case ETK_Union: - case ETK_Enum: - return true; - } } - - return false; + llvm_unreachable("Unknown elaborated type keyword."); +} + +const char* +TypeWithKeyword::getKeywordName(ElaboratedTypeKeyword Keyword) { + switch (Keyword) { + default: llvm_unreachable("Unknown elaborated type keyword."); + case ETK_None: return ""; + case ETK_Typename: return "typename"; + case ETK_Class: return "class"; + case ETK_Struct: return "struct"; + case ETK_Union: return "union"; + case ETK_Enum: return "enum"; + } +} + +bool Type::isElaboratedTypeSpecifier() const { + ElaboratedTypeKeyword Keyword; + if (const ElaboratedType *Elab = dyn_cast<ElaboratedType>(this)) + Keyword = Elab->getKeyword(); + else if (const DependentNameType *DepName = dyn_cast<DependentNameType>(this)) + Keyword = DepName->getKeyword(); + else + return false; + + return TypeWithKeyword::KeywordIsTagTypeKind(Keyword); } const char *Type::getTypeClassName() const { @@ -836,10 +908,12 @@ const char *BuiltinType::getName(const LangOptions &LO) const { case UndeducedAuto: return "auto"; case ObjCId: return "id"; case ObjCClass: return "Class"; - case ObjCSel: return "SEL"; + case ObjCSel: return "SEL"; } } +void FunctionType::ANCHOR() {} // Key function for FunctionType. + llvm::StringRef FunctionType::getNameForCallConv(CallingConv CC) { switch (CC) { case CC_Default: llvm_unreachable("no name for default cc"); @@ -848,6 +922,7 @@ llvm::StringRef FunctionType::getNameForCallConv(CallingConv CC) { case CC_C: return "cdecl"; case CC_X86StdCall: return "stdcall"; case CC_X86FastCall: return "fastcall"; + case CC_X86ThisCall: return "thiscall"; } } @@ -881,19 +956,6 @@ void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) { getExtInfo()); } -void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID, - QualType OIT, - ObjCProtocolDecl * const *protocols, - unsigned NumProtocols) { - ID.AddPointer(OIT.getAsOpaquePtr()); - for (unsigned i = 0; i != NumProtocols; i++) - ID.AddPointer(protocols[i]); -} - -void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID) { - Profile(ID, getPointeeType(), qual_begin(), getNumProtocols()); -} - /// LookThroughTypedefs - Return the ultimate type this typedef corresponds to /// potentially looking through *all* consequtive typedefs. This returns the /// sum of the type qualifiers, so if you have: @@ -975,6 +1037,10 @@ static bool isDependent(const TemplateArgument &Arg) { return Arg.getAsTemplate().isDependent(); case TemplateArgument::Declaration: + if (DeclContext *DC = dyn_cast<DeclContext>(Arg.getAsDecl())) + return DC->isDependentContext(); + return Arg.getAsDecl()->getDeclContext()->isDependentContext(); + case TemplateArgument::Integral: // Never dependent return false; @@ -984,7 +1050,13 @@ static bool isDependent(const TemplateArgument &Arg) { Arg.getAsExpr()->isValueDependent()); case TemplateArgument::Pack: - assert(0 && "FIXME: Implement!"); + for (TemplateArgument::pack_iterator P = Arg.pack_begin(), + PEnd = Arg.pack_end(); + P != PEnd; ++P) { + if (isDependent(*P)) + return true; + } + return false; } @@ -1081,36 +1153,53 @@ QualType QualifierCollector::apply(const Type *T) const { return Context->getQualifiedType(T, *this); } -void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID, - const ObjCInterfaceDecl *Decl, - ObjCProtocolDecl * const *protocols, - unsigned NumProtocols) { - ID.AddPointer(Decl); +void ObjCObjectTypeImpl::Profile(llvm::FoldingSetNodeID &ID, + QualType BaseType, + ObjCProtocolDecl * const *Protocols, + unsigned NumProtocols) { + ID.AddPointer(BaseType.getAsOpaquePtr()); for (unsigned i = 0; i != NumProtocols; i++) - ID.AddPointer(protocols[i]); + ID.AddPointer(Protocols[i]); } -void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID) { - Profile(ID, getDecl(), qual_begin(), getNumProtocols()); +void ObjCObjectTypeImpl::Profile(llvm::FoldingSetNodeID &ID) { + Profile(ID, getBaseType(), qual_begin(), getNumProtocols()); } -Linkage Type::getLinkage() const { - // C++ [basic.link]p8: - // Names not covered by these rules have no linkage. +/// \brief Determine the linkage of this type. +Linkage Type::getLinkage() const { if (this != CanonicalType.getTypePtr()) return CanonicalType->getLinkage(); + + if (!LinkageKnown) { + CachedLinkage = getLinkageImpl(); + LinkageKnown = true; + } + + return static_cast<clang::Linkage>(CachedLinkage); +} +Linkage Type::getLinkageImpl() const { + // C++ [basic.link]p8: + // Names not covered by these rules have no linkage. return NoLinkage; } -Linkage BuiltinType::getLinkage() const { +void Type::ClearLinkageCache() { + if (this != CanonicalType.getTypePtr()) + CanonicalType->ClearLinkageCache(); + else + LinkageKnown = false; +} + +Linkage BuiltinType::getLinkageImpl() const { // C++ [basic.link]p8: // A type is said to have linkage if and only if: // - it is a fundamental type (3.9.1); or return ExternalLinkage; } -Linkage TagType::getLinkage() const { +Linkage TagType::getLinkageImpl() const { // C++ [basic.link]p8: // - it is a class or enumeration type that is named (or has a name for // linkage purposes (7.1.3)) and the name has linkage; or @@ -1121,39 +1210,39 @@ Linkage TagType::getLinkage() const { // C++ [basic.link]p8: // - it is a compound type (3.9.2) other than a class or enumeration, // compounded exclusively from types that have linkage; or -Linkage ComplexType::getLinkage() const { +Linkage ComplexType::getLinkageImpl() const { return ElementType->getLinkage(); } -Linkage PointerType::getLinkage() const { +Linkage PointerType::getLinkageImpl() const { return PointeeType->getLinkage(); } -Linkage BlockPointerType::getLinkage() const { +Linkage BlockPointerType::getLinkageImpl() const { return PointeeType->getLinkage(); } -Linkage ReferenceType::getLinkage() const { +Linkage ReferenceType::getLinkageImpl() const { return PointeeType->getLinkage(); } -Linkage MemberPointerType::getLinkage() const { +Linkage MemberPointerType::getLinkageImpl() const { return minLinkage(Class->getLinkage(), PointeeType->getLinkage()); } -Linkage ArrayType::getLinkage() const { +Linkage ArrayType::getLinkageImpl() const { return ElementType->getLinkage(); } -Linkage VectorType::getLinkage() const { +Linkage VectorType::getLinkageImpl() const { return ElementType->getLinkage(); } -Linkage FunctionNoProtoType::getLinkage() const { +Linkage FunctionNoProtoType::getLinkageImpl() const { return getResultType()->getLinkage(); } -Linkage FunctionProtoType::getLinkage() const { +Linkage FunctionProtoType::getLinkageImpl() const { Linkage L = getResultType()->getLinkage(); for (arg_type_iterator A = arg_type_begin(), AEnd = arg_type_end(); A != AEnd; ++A) @@ -1162,10 +1251,10 @@ Linkage FunctionProtoType::getLinkage() const { return L; } -Linkage ObjCInterfaceType::getLinkage() const { +Linkage ObjCObjectType::getLinkageImpl() const { return ExternalLinkage; } -Linkage ObjCObjectPointerType::getLinkage() const { +Linkage ObjCObjectPointerType::getLinkageImpl() const { return ExternalLinkage; } diff --git a/lib/AST/TypeLoc.cpp b/lib/AST/TypeLoc.cpp index fd9fbc1..4893b38 100644 --- a/lib/AST/TypeLoc.cpp +++ b/lib/AST/TypeLoc.cpp @@ -27,13 +27,13 @@ namespace { #define ABSTRACT_TYPELOC(CLASS, PARENT) #define TYPELOC(CLASS, PARENT) \ SourceRange Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \ - return TyLoc.getSourceRange(); \ + return TyLoc.getLocalSourceRange(); \ } #include "clang/AST/TypeLocNodes.def" }; } -SourceRange TypeLoc::getSourceRangeImpl(TypeLoc TL) { +SourceRange TypeLoc::getLocalSourceRangeImpl(TypeLoc TL) { if (TL.isNull()) return SourceRange(); return TypeLocRanger().Visit(TL); } @@ -92,11 +92,58 @@ namespace { /// recursively, as if the entire tree had been written in the /// given location. void TypeLoc::initializeImpl(TypeLoc TL, SourceLocation Loc) { - do { - TypeLocInitializer(Loc).Visit(TL); - } while ((TL = TL.getNextTypeLoc())); + while (true) { + switch (TL.getTypeLocClass()) { +#define ABSTRACT_TYPELOC(CLASS, PARENT) +#define TYPELOC(CLASS, PARENT) \ + case CLASS: { \ + CLASS##TypeLoc TLCasted = cast<CLASS##TypeLoc>(TL); \ + TLCasted.initializeLocal(Loc); \ + TL = TLCasted.getNextTypeLoc(); \ + if (!TL) return; \ + continue; \ + } +#include "clang/AST/TypeLocNodes.def" + } + } +} + +SourceLocation TypeLoc::getBeginLoc() const { + TypeLoc Cur = *this; + while (true) { + switch (Cur.getTypeLocClass()) { + // FIXME: Currently QualifiedTypeLoc does not have a source range + // case Qualified: + case Elaborated: + break; + default: + TypeLoc Next = Cur.getNextTypeLoc(); + if (Next.isNull()) break; + Cur = Next; + continue; + } + break; + } + return Cur.getLocalSourceRange().getBegin(); } +SourceLocation TypeLoc::getEndLoc() const { + TypeLoc Cur = *this; + while (true) { + switch (Cur.getTypeLocClass()) { + default: + break; + case Qualified: + case Elaborated: + Cur = Cur.getNextTypeLoc(); + continue; + } + break; + } + return Cur.getLocalSourceRange().getEnd(); +} + + namespace { struct TSTChecker : public TypeLocVisitor<TSTChecker, bool> { // Overload resolution does the real work for us. @@ -129,7 +176,7 @@ bool TypeSpecTypeLoc::classof(const TypeLoc *TL) { // Reimplemented to account for GNU/C++ extension // typeof unary-expression // where there are no parentheses. -SourceRange TypeOfExprTypeLoc::getSourceRange() const { +SourceRange TypeOfExprTypeLoc::getLocalSourceRange() const { if (getRParenLoc().isValid()) return SourceRange(getTypeofLoc(), getRParenLoc()); else diff --git a/lib/AST/TypePrinter.cpp b/lib/AST/TypePrinter.cpp index 915d7af..35a7e09 100644 --- a/lib/AST/TypePrinter.cpp +++ b/lib/AST/TypePrinter.cpp @@ -295,6 +295,9 @@ void TypePrinter::PrintFunctionProto(const FunctionProtoType *T, case CC_X86FastCall: S += " __attribute__((fastcall))"; break; + case CC_X86ThisCall: + S += " __attribute__((thiscall))"; + break; } if (Info.getNoReturn()) S += " __attribute__((noreturn))"; @@ -497,16 +500,6 @@ void TypePrinter::PrintEnum(const EnumType *T, std::string &S) { PrintTag(T->getDecl(), S); } -void TypePrinter::PrintElaborated(const ElaboratedType *T, std::string &S) { - Print(T->getUnderlyingType(), S); - - // We don't actually make these in C, but the language options - // sometimes lie to us -- for example, if someone calls - // QualType::getAsString(). Just suppress the redundant tag if so. - if (Policy.LangOpts.CPlusPlus) - S = std::string(T->getNameForTagKind(T->getTagKind())) + ' ' + S; -} - void TypePrinter::PrintTemplateTypeParm(const TemplateTypeParmType *T, std::string &S) { if (!S.empty()) // Prefix the basic type, e.g. 'parmname X'. @@ -549,13 +542,17 @@ void TypePrinter::PrintInjectedClassName(const InjectedClassNameType *T, PrintTemplateSpecialization(T->getInjectedTST(), S); } -void TypePrinter::PrintQualifiedName(const QualifiedNameType *T, - std::string &S) { +void TypePrinter::PrintElaborated(const ElaboratedType *T, std::string &S) { std::string MyString; { llvm::raw_string_ostream OS(MyString); - T->getQualifier()->print(OS, Policy); + OS << TypeWithKeyword::getKeywordName(T->getKeyword()); + if (T->getKeyword() != ETK_None) + OS << " "; + NestedNameSpecifier* Qualifier = T->getQualifier(); + if (Qualifier) + Qualifier->print(OS, Policy); } std::string TypeStr; @@ -575,14 +572,9 @@ void TypePrinter::PrintDependentName(const DependentNameType *T, std::string &S) { llvm::raw_string_ostream OS(MyString); - switch (T->getKeyword()) { - case ETK_None: break; - case ETK_Typename: OS << "typename "; break; - case ETK_Class: OS << "class "; break; - case ETK_Struct: OS << "struct "; break; - case ETK_Union: OS << "union "; break; - case ETK_Enum: OS << "enum "; break; - } + OS << TypeWithKeyword::getKeywordName(T->getKeyword()); + if (T->getKeyword() != ETK_None) + OS << " "; T->getQualifier()->print(OS, Policy); @@ -607,25 +599,37 @@ void TypePrinter::PrintObjCInterface(const ObjCInterfaceType *T, std::string &S) { if (!S.empty()) // Prefix the basic type, e.g. 'typedefname X'. S = ' ' + S; - + std::string ObjCQIString = T->getDecl()->getNameAsString(); - if (T->getNumProtocols()) { - ObjCQIString += '<'; - bool isFirst = true; - for (ObjCInterfaceType::qual_iterator I = T->qual_begin(), - E = T->qual_end(); - I != E; ++I) { - if (isFirst) - isFirst = false; - else - ObjCQIString += ','; - ObjCQIString += (*I)->getNameAsString(); - } - ObjCQIString += '>'; - } S = ObjCQIString + S; } +void TypePrinter::PrintObjCObject(const ObjCObjectType *T, + std::string &S) { + if (T->qual_empty()) + return Print(T->getBaseType(), S); + + std::string tmp; + Print(T->getBaseType(), tmp); + tmp += '<'; + bool isFirst = true; + for (ObjCObjectType::qual_iterator + I = T->qual_begin(), E = T->qual_end(); I != E; ++I) { + if (isFirst) + isFirst = false; + else + tmp += ','; + tmp += (*I)->getNameAsString(); + } + tmp += '>'; + + if (!S.empty()) { + tmp += ' '; + tmp += S; + } + std::swap(tmp, S); +} + void TypePrinter::PrintObjCObjectPointer(const ObjCObjectPointerType *T, std::string &S) { std::string ObjCQIString; |