Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 1 | //===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the ASTContext interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/ASTContext.h" |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 15 | #include "clang/AST/CharUnits.h" |
Argyrios Kyrtzidis | faf0876 | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclCXX.h" |
Steve Naroff | 67391b8 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclTemplate.h" |
Argyrios Kyrtzidis | 3f79ad7 | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeLoc.h" |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 20 | #include "clang/AST/Expr.h" |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 21 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExternalASTSource.h" |
Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 23 | #include "clang/AST/ASTMutationListener.h" |
Anders Carlsson | 15b73de | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 24 | #include "clang/AST/RecordLayout.h" |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 25 | #include "clang/AST/Mangle.h" |
Chris Lattner | 15ba949 | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 26 | #include "clang/Basic/Builtins.h" |
Chris Lattner | d286851 | 2009-03-28 03:45:20 +0000 | [diff] [blame] | 27 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 4dc8a6f | 2007-05-20 23:50:58 +0000 | [diff] [blame] | 28 | #include "clang/Basic/TargetInfo.h" |
Benjamin Kramer | 1402ce3 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/SmallString.h" |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/StringExtras.h" |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 31 | #include "llvm/Support/MathExtras.h" |
Benjamin Kramer | 1402ce3 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 32 | #include "llvm/Support/raw_ostream.h" |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 33 | #include "CXXABI.h" |
Anders Carlsson | a4267a6 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 34 | |
Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 35 | using namespace clang; |
| 36 | |
Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 37 | unsigned ASTContext::NumImplicitDefaultConstructors; |
| 38 | unsigned ASTContext::NumImplicitDefaultConstructorsDeclared; |
Douglas Gregor | a6d6950 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 39 | unsigned ASTContext::NumImplicitCopyConstructors; |
| 40 | unsigned ASTContext::NumImplicitCopyConstructorsDeclared; |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 41 | unsigned ASTContext::NumImplicitCopyAssignmentOperators; |
| 42 | unsigned ASTContext::NumImplicitCopyAssignmentOperatorsDeclared; |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 43 | unsigned ASTContext::NumImplicitDestructors; |
| 44 | unsigned ASTContext::NumImplicitDestructorsDeclared; |
| 45 | |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 46 | enum FloatingRank { |
| 47 | FloatRank, DoubleRank, LongDoubleRank |
| 48 | }; |
| 49 | |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 50 | void |
| 51 | ASTContext::CanonicalTemplateTemplateParm::Profile(llvm::FoldingSetNodeID &ID, |
| 52 | TemplateTemplateParmDecl *Parm) { |
| 53 | ID.AddInteger(Parm->getDepth()); |
| 54 | ID.AddInteger(Parm->getPosition()); |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 55 | ID.AddBoolean(Parm->isParameterPack()); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 56 | |
| 57 | TemplateParameterList *Params = Parm->getTemplateParameters(); |
| 58 | ID.AddInteger(Params->size()); |
| 59 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 60 | PEnd = Params->end(); |
| 61 | P != PEnd; ++P) { |
| 62 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
| 63 | ID.AddInteger(0); |
| 64 | ID.AddBoolean(TTP->isParameterPack()); |
| 65 | continue; |
| 66 | } |
| 67 | |
| 68 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 69 | ID.AddInteger(1); |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 70 | ID.AddBoolean(NTTP->isParameterPack()); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 71 | ID.AddPointer(NTTP->getType().getAsOpaquePtr()); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 72 | if (NTTP->isExpandedParameterPack()) { |
| 73 | ID.AddBoolean(true); |
| 74 | ID.AddInteger(NTTP->getNumExpansionTypes()); |
| 75 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) |
| 76 | ID.AddPointer(NTTP->getExpansionType(I).getAsOpaquePtr()); |
| 77 | } else |
| 78 | ID.AddBoolean(false); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 79 | continue; |
| 80 | } |
| 81 | |
| 82 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); |
| 83 | ID.AddInteger(2); |
| 84 | Profile(ID, TTP); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | TemplateTemplateParmDecl * |
| 89 | ASTContext::getCanonicalTemplateTemplateParmDecl( |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 90 | TemplateTemplateParmDecl *TTP) const { |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 91 | // Check if we already have a canonical template template parameter. |
| 92 | llvm::FoldingSetNodeID ID; |
| 93 | CanonicalTemplateTemplateParm::Profile(ID, TTP); |
| 94 | void *InsertPos = 0; |
| 95 | CanonicalTemplateTemplateParm *Canonical |
| 96 | = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
| 97 | if (Canonical) |
| 98 | return Canonical->getParam(); |
| 99 | |
| 100 | // Build a canonical template parameter list. |
| 101 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 102 | llvm::SmallVector<NamedDecl *, 4> CanonParams; |
| 103 | CanonParams.reserve(Params->size()); |
| 104 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 105 | PEnd = Params->end(); |
| 106 | P != PEnd; ++P) { |
| 107 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) |
| 108 | CanonParams.push_back( |
| 109 | TemplateTypeParmDecl::Create(*this, getTranslationUnitDecl(), |
| 110 | SourceLocation(), TTP->getDepth(), |
| 111 | TTP->getIndex(), 0, false, |
| 112 | TTP->isParameterPack())); |
| 113 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 114 | = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 115 | QualType T = getCanonicalType(NTTP->getType()); |
| 116 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T); |
| 117 | NonTypeTemplateParmDecl *Param; |
| 118 | if (NTTP->isExpandedParameterPack()) { |
| 119 | llvm::SmallVector<QualType, 2> ExpandedTypes; |
| 120 | llvm::SmallVector<TypeSourceInfo *, 2> ExpandedTInfos; |
| 121 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) { |
| 122 | ExpandedTypes.push_back(getCanonicalType(NTTP->getExpansionType(I))); |
| 123 | ExpandedTInfos.push_back( |
| 124 | getTrivialTypeSourceInfo(ExpandedTypes.back())); |
| 125 | } |
| 126 | |
| 127 | Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
| 128 | SourceLocation(), |
| 129 | NTTP->getDepth(), |
| 130 | NTTP->getPosition(), 0, |
| 131 | T, |
| 132 | TInfo, |
| 133 | ExpandedTypes.data(), |
| 134 | ExpandedTypes.size(), |
| 135 | ExpandedTInfos.data()); |
| 136 | } else { |
| 137 | Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
| 138 | SourceLocation(), |
| 139 | NTTP->getDepth(), |
| 140 | NTTP->getPosition(), 0, |
| 141 | T, |
| 142 | NTTP->isParameterPack(), |
| 143 | TInfo); |
| 144 | } |
| 145 | CanonParams.push_back(Param); |
| 146 | |
| 147 | } else |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 148 | CanonParams.push_back(getCanonicalTemplateTemplateParmDecl( |
| 149 | cast<TemplateTemplateParmDecl>(*P))); |
| 150 | } |
| 151 | |
| 152 | TemplateTemplateParmDecl *CanonTTP |
| 153 | = TemplateTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
| 154 | SourceLocation(), TTP->getDepth(), |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 155 | TTP->getPosition(), |
| 156 | TTP->isParameterPack(), |
| 157 | 0, |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 158 | TemplateParameterList::Create(*this, SourceLocation(), |
| 159 | SourceLocation(), |
| 160 | CanonParams.data(), |
| 161 | CanonParams.size(), |
| 162 | SourceLocation())); |
| 163 | |
| 164 | // Get the new insert position for the node we care about. |
| 165 | Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
| 166 | assert(Canonical == 0 && "Shouldn't be in the map!"); |
| 167 | (void)Canonical; |
| 168 | |
| 169 | // Create the canonical template template parameter entry. |
| 170 | Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP); |
| 171 | CanonTemplateTemplateParms.InsertNode(Canonical, InsertPos); |
| 172 | return CanonTTP; |
| 173 | } |
| 174 | |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 175 | CXXABI *ASTContext::createCXXABI(const TargetInfo &T) { |
John McCall | 8635341 | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 176 | if (!LangOpts.CPlusPlus) return 0; |
| 177 | |
Charles Davis | 6bcb07a | 2010-08-19 02:18:14 +0000 | [diff] [blame] | 178 | switch (T.getCXXABI()) { |
John McCall | 8635341 | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 179 | case CXXABI_ARM: |
| 180 | return CreateARMCXXABI(*this); |
| 181 | case CXXABI_Itanium: |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 182 | return CreateItaniumCXXABI(*this); |
Charles Davis | 6bcb07a | 2010-08-19 02:18:14 +0000 | [diff] [blame] | 183 | case CXXABI_Microsoft: |
| 184 | return CreateMicrosoftCXXABI(*this); |
| 185 | } |
John McCall | 8635341 | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 186 | return 0; |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 189 | ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM, |
Daniel Dunbar | 1b44419 | 2009-11-13 05:51:54 +0000 | [diff] [blame] | 190 | const TargetInfo &t, |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 191 | IdentifierTable &idents, SelectorTable &sels, |
Chris Lattner | 15ba949 | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 192 | Builtin::Context &builtins, |
Douglas Gregor | 5b11d49 | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 193 | unsigned size_reserve) : |
John McCall | 773cc98 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 194 | TemplateSpecializationTypes(this_()), |
| 195 | DependentTemplateSpecializationTypes(this_()), |
Argyrios Kyrtzidis | e862cbc | 2010-07-04 21:44:19 +0000 | [diff] [blame] | 196 | GlobalNestedNameSpecifier(0), IsInt128Installed(false), |
| 197 | CFConstantStringTypeDecl(0), NSConstantStringTypeDecl(0), |
Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 198 | ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0), |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 199 | sigjmp_bufDecl(0), BlockDescriptorType(0), BlockDescriptorExtendedType(0), |
Peter Collingbourne | 9e2c81f | 2011-02-09 21:04:32 +0000 | [diff] [blame] | 200 | cudaConfigureCallDecl(0), |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 201 | NullTypeSourceInfo(QualType()), |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 202 | SourceMgr(SM), LangOpts(LOpts), ABI(createCXXABI(t)), Target(t), |
Douglas Gregor | c6d5edd | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 203 | Idents(idents), Selectors(sels), |
Ted Kremenek | 6aead3a | 2010-05-10 20:40:08 +0000 | [diff] [blame] | 204 | BuiltinInfo(builtins), |
| 205 | DeclarationNames(*this), |
Argyrios Kyrtzidis | 440ea32 | 2010-10-28 09:29:35 +0000 | [diff] [blame] | 206 | ExternalSource(0), Listener(0), PrintingPolicy(LOpts), |
Ted Kremenek | 520f47b | 2010-06-18 00:31:04 +0000 | [diff] [blame] | 207 | LastSDM(0, 0), |
John McCall | 147d021 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 208 | UniqueBlockByRefTypeID(0) { |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 209 | ObjCIdRedefinitionType = QualType(); |
| 210 | ObjCClassRedefinitionType = QualType(); |
Douglas Gregor | 5b11d49 | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 211 | ObjCSelRedefinitionType = QualType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 212 | if (size_reserve > 0) Types.reserve(size_reserve); |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 213 | TUDecl = TranslationUnitDecl::Create(*this); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 214 | InitBuiltinTypes(); |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 217 | ASTContext::~ASTContext() { |
Ted Kremenek | da4e0d3 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 218 | // Release the DenseMaps associated with DeclContext objects. |
| 219 | // FIXME: Is this the ideal solution? |
| 220 | ReleaseDeclContextMaps(); |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 221 | |
Douglas Gregor | 5b11d49 | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 222 | // Call all of the deallocation functions. |
| 223 | for (unsigned I = 0, N = Deallocations.size(); I != N; ++I) |
| 224 | Deallocations[I].first(Deallocations[I].second); |
Douglas Gregor | 1a80933 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 225 | |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 226 | // Release all of the memory associated with overridden C++ methods. |
| 227 | for (llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::iterator |
| 228 | OM = OverriddenMethods.begin(), OMEnd = OverriddenMethods.end(); |
| 229 | OM != OMEnd; ++OM) |
| 230 | OM->second.Destroy(); |
Ted Kremenek | da4e0d3 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 231 | |
Ted Kremenek | 076baeb | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 232 | // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed |
Douglas Gregor | 5b11d49 | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 233 | // because they can contain DenseMaps. |
| 234 | for (llvm::DenseMap<const ObjCContainerDecl*, |
| 235 | const ASTRecordLayout*>::iterator |
| 236 | I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) |
| 237 | // Increment in loop to prevent using deallocated memory. |
| 238 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 239 | R->Destroy(*this); |
| 240 | |
Ted Kremenek | 076baeb | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 241 | for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator |
| 242 | I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) { |
| 243 | // Increment in loop to prevent using deallocated memory. |
| 244 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 245 | R->Destroy(*this); |
| 246 | } |
Douglas Gregor | 561eceb | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 247 | |
| 248 | for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(), |
| 249 | AEnd = DeclAttrs.end(); |
| 250 | A != AEnd; ++A) |
| 251 | A->second->~AttrVec(); |
| 252 | } |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 253 | |
Douglas Gregor | 1a80933 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 254 | void ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) { |
| 255 | Deallocations.push_back(std::make_pair(Callback, Data)); |
| 256 | } |
| 257 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 258 | void |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 259 | ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) { |
| 260 | ExternalSource.reset(Source.take()); |
| 261 | } |
| 262 | |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 263 | void ASTContext::PrintStats() const { |
| 264 | fprintf(stderr, "*** AST Context Stats:\n"); |
| 265 | fprintf(stderr, " %d types total.\n", (int)Types.size()); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 266 | |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 267 | unsigned counts[] = { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 268 | #define TYPE(Name, Parent) 0, |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 269 | #define ABSTRACT_TYPE(Name, Parent) |
| 270 | #include "clang/AST/TypeNodes.def" |
| 271 | 0 // Extra |
| 272 | }; |
Douglas Gregor | b1fe2c9 | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 273 | |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 274 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 275 | Type *T = Types[i]; |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 276 | counts[(unsigned)T->getTypeClass()]++; |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 279 | unsigned Idx = 0; |
| 280 | unsigned TotalBytes = 0; |
| 281 | #define TYPE(Name, Parent) \ |
| 282 | if (counts[Idx]) \ |
| 283 | fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \ |
| 284 | TotalBytes += counts[Idx] * sizeof(Name##Type); \ |
| 285 | ++Idx; |
| 286 | #define ABSTRACT_TYPE(Name, Parent) |
| 287 | #include "clang/AST/TypeNodes.def" |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 288 | |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 289 | fprintf(stderr, "Total bytes = %d\n", int(TotalBytes)); |
Douglas Gregor | 747eb78 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 290 | |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 291 | // Implicit special member functions. |
Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 292 | fprintf(stderr, " %u/%u implicit default constructors created\n", |
| 293 | NumImplicitDefaultConstructorsDeclared, |
| 294 | NumImplicitDefaultConstructors); |
Douglas Gregor | a6d6950 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 295 | fprintf(stderr, " %u/%u implicit copy constructors created\n", |
| 296 | NumImplicitCopyConstructorsDeclared, |
| 297 | NumImplicitCopyConstructors); |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 298 | fprintf(stderr, " %u/%u implicit copy assignment operators created\n", |
| 299 | NumImplicitCopyAssignmentOperatorsDeclared, |
| 300 | NumImplicitCopyAssignmentOperators); |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 301 | fprintf(stderr, " %u/%u implicit destructors created\n", |
| 302 | NumImplicitDestructorsDeclared, NumImplicitDestructors); |
| 303 | |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 304 | if (ExternalSource.get()) { |
| 305 | fprintf(stderr, "\n"); |
| 306 | ExternalSource->PrintStats(); |
| 307 | } |
Douglas Gregor | 747eb78 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 308 | |
Douglas Gregor | 5b11d49 | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 309 | BumpAlloc.PrintStats(); |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 313 | void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) { |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 314 | BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K); |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 315 | R = CanQualType::CreateUnsafe(QualType(Ty, 0)); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 316 | Types.push_back(Ty); |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 319 | void ASTContext::InitBuiltinTypes() { |
| 320 | assert(VoidTy.isNull() && "Context reinitialized?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 321 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 322 | // C99 6.2.5p19. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 323 | InitBuiltinType(VoidTy, BuiltinType::Void); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 324 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 325 | // C99 6.2.5p2. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 326 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 327 | // C99 6.2.5p3. |
Eli Friedman | 9ffd4a9 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 328 | if (LangOpts.CharIsSigned) |
Chris Lattner | b16f455 | 2007-06-03 07:25:34 +0000 | [diff] [blame] | 329 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 330 | else |
| 331 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 332 | // C99 6.2.5p4. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 333 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 334 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 335 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 336 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 337 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 338 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 339 | // C99 6.2.5p6. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 340 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 341 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 342 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 343 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 344 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 345 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 346 | // C99 6.2.5p10. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 347 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 348 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 349 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 350 | |
Chris Lattner | f122cef | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 351 | // GNU extension, 128-bit integers. |
| 352 | InitBuiltinType(Int128Ty, BuiltinType::Int128); |
| 353 | InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128); |
| 354 | |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 355 | if (LangOpts.CPlusPlus) { // C++ 3.9.1p5 |
| 356 | if (!LangOpts.ShortWChar) |
| 357 | InitBuiltinType(WCharTy, BuiltinType::WChar_S); |
| 358 | else // -fshort-wchar makes wchar_t be unsigned. |
| 359 | InitBuiltinType(WCharTy, BuiltinType::WChar_U); |
| 360 | } else // C99 |
Chris Lattner | 007cb02 | 2009-02-26 23:43:47 +0000 | [diff] [blame] | 361 | WCharTy = getFromTargetType(Target.getWCharType()); |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 362 | |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 363 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 364 | InitBuiltinType(Char16Ty, BuiltinType::Char16); |
| 365 | else // C99 |
| 366 | Char16Ty = getFromTargetType(Target.getChar16Type()); |
| 367 | |
| 368 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 369 | InitBuiltinType(Char32Ty, BuiltinType::Char32); |
| 370 | else // C99 |
| 371 | Char32Ty = getFromTargetType(Target.getChar32Type()); |
| 372 | |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 373 | // Placeholder type for type-dependent expressions whose type is |
| 374 | // completely unknown. No code should ever check a type against |
| 375 | // DependentTy and users should never see it; however, it is here to |
| 376 | // help diagnose failures to properly check for type-dependent |
| 377 | // expressions. |
| 378 | InitBuiltinType(DependentTy, BuiltinType::Dependent); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 379 | |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 380 | // Placeholder type for functions. |
| 381 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
| 382 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 383 | // C99 6.2.5p11. |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 384 | FloatComplexTy = getComplexType(FloatTy); |
| 385 | DoubleComplexTy = getComplexType(DoubleTy); |
| 386 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 387 | |
Steve Naroff | 66e9f33 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 388 | BuiltinVaListType = QualType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 389 | |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 390 | // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope(). |
| 391 | ObjCIdTypedefType = QualType(); |
| 392 | ObjCClassTypedefType = QualType(); |
Fariborz Jahanian | 252ba5f | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 393 | ObjCSelTypedefType = QualType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 394 | |
Fariborz Jahanian | 252ba5f | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 395 | // Builtin types for 'id', 'Class', and 'SEL'. |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 396 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); |
| 397 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); |
Fariborz Jahanian | 252ba5f | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 398 | InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 399 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 400 | ObjCConstantStringType = QualType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 401 | |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 402 | // void * type |
| 403 | VoidPtrTy = getPointerType(VoidTy); |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 404 | |
| 405 | // nullptr type (C++0x 2.14.7) |
| 406 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 409 | Diagnostic &ASTContext::getDiagnostics() const { |
| 410 | return SourceMgr.getDiagnostics(); |
| 411 | } |
| 412 | |
Douglas Gregor | 561eceb | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 413 | AttrVec& ASTContext::getDeclAttrs(const Decl *D) { |
| 414 | AttrVec *&Result = DeclAttrs[D]; |
| 415 | if (!Result) { |
| 416 | void *Mem = Allocate(sizeof(AttrVec)); |
| 417 | Result = new (Mem) AttrVec; |
| 418 | } |
| 419 | |
| 420 | return *Result; |
| 421 | } |
| 422 | |
| 423 | /// \brief Erase the attributes corresponding to the given declaration. |
| 424 | void ASTContext::eraseDeclAttrs(const Decl *D) { |
| 425 | llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D); |
| 426 | if (Pos != DeclAttrs.end()) { |
| 427 | Pos->second->~AttrVec(); |
| 428 | DeclAttrs.erase(Pos); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 433 | MemberSpecializationInfo * |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 434 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 435 | assert(Var->isStaticDataMember() && "Not a static data member"); |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 436 | llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 437 | = InstantiatedFromStaticDataMember.find(Var); |
| 438 | if (Pos == InstantiatedFromStaticDataMember.end()) |
| 439 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 440 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 441 | return Pos->second; |
| 442 | } |
| 443 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 444 | void |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 445 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, |
Argyrios Kyrtzidis | cdb8b3f | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 446 | TemplateSpecializationKind TSK, |
| 447 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 448 | assert(Inst->isStaticDataMember() && "Not a static data member"); |
| 449 | assert(Tmpl->isStaticDataMember() && "Not a static data member"); |
| 450 | assert(!InstantiatedFromStaticDataMember[Inst] && |
| 451 | "Already noted what static data member was instantiated from"); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 452 | InstantiatedFromStaticDataMember[Inst] |
Argyrios Kyrtzidis | cdb8b3f | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 453 | = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation); |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 454 | } |
| 455 | |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 456 | NamedDecl * |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 457 | ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) { |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 458 | llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 459 | = InstantiatedFromUsingDecl.find(UUD); |
| 460 | if (Pos == InstantiatedFromUsingDecl.end()) |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 461 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 462 | |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 463 | return Pos->second; |
| 464 | } |
| 465 | |
| 466 | void |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 467 | ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) { |
| 468 | assert((isa<UsingDecl>(Pattern) || |
| 469 | isa<UnresolvedUsingValueDecl>(Pattern) || |
| 470 | isa<UnresolvedUsingTypenameDecl>(Pattern)) && |
| 471 | "pattern decl is not a using decl"); |
| 472 | assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists"); |
| 473 | InstantiatedFromUsingDecl[Inst] = Pattern; |
| 474 | } |
| 475 | |
| 476 | UsingShadowDecl * |
| 477 | ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) { |
| 478 | llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos |
| 479 | = InstantiatedFromUsingShadowDecl.find(Inst); |
| 480 | if (Pos == InstantiatedFromUsingShadowDecl.end()) |
| 481 | return 0; |
| 482 | |
| 483 | return Pos->second; |
| 484 | } |
| 485 | |
| 486 | void |
| 487 | ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, |
| 488 | UsingShadowDecl *Pattern) { |
| 489 | assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists"); |
| 490 | InstantiatedFromUsingShadowDecl[Inst] = Pattern; |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 493 | FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { |
| 494 | llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos |
| 495 | = InstantiatedFromUnnamedFieldDecl.find(Field); |
| 496 | if (Pos == InstantiatedFromUnnamedFieldDecl.end()) |
| 497 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 498 | |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 499 | return Pos->second; |
| 500 | } |
| 501 | |
| 502 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, |
| 503 | FieldDecl *Tmpl) { |
| 504 | assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed"); |
| 505 | assert(!Tmpl->getDeclName() && "Template field decl is not unnamed"); |
| 506 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] && |
| 507 | "Already noted what unnamed field was instantiated from"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 508 | |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 509 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; |
| 510 | } |
| 511 | |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 512 | ASTContext::overridden_cxx_method_iterator |
| 513 | ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const { |
| 514 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 515 | = OverriddenMethods.find(Method); |
| 516 | if (Pos == OverriddenMethods.end()) |
| 517 | return 0; |
| 518 | |
| 519 | return Pos->second.begin(); |
| 520 | } |
| 521 | |
| 522 | ASTContext::overridden_cxx_method_iterator |
| 523 | ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const { |
| 524 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 525 | = OverriddenMethods.find(Method); |
| 526 | if (Pos == OverriddenMethods.end()) |
| 527 | return 0; |
| 528 | |
| 529 | return Pos->second.end(); |
| 530 | } |
| 531 | |
Argyrios Kyrtzidis | 6685e8a | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 532 | unsigned |
| 533 | ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const { |
| 534 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 535 | = OverriddenMethods.find(Method); |
| 536 | if (Pos == OverriddenMethods.end()) |
| 537 | return 0; |
| 538 | |
| 539 | return Pos->second.size(); |
| 540 | } |
| 541 | |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 542 | void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method, |
| 543 | const CXXMethodDecl *Overridden) { |
| 544 | OverriddenMethods[Method].push_back(Overridden); |
| 545 | } |
| 546 | |
Chris Lattner | 53cfe80 | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 547 | //===----------------------------------------------------------------------===// |
| 548 | // Type Sizing and Analysis |
| 549 | //===----------------------------------------------------------------------===// |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 550 | |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 551 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 552 | /// scalar floating point type. |
| 553 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 554 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 555 | assert(BT && "Not a floating point type!"); |
| 556 | switch (BT->getKind()) { |
| 557 | default: assert(0 && "Not a floating point type!"); |
| 558 | case BuiltinType::Float: return Target.getFloatFormat(); |
| 559 | case BuiltinType::Double: return Target.getDoubleFormat(); |
| 560 | case BuiltinType::LongDouble: return Target.getLongDoubleFormat(); |
| 561 | } |
| 562 | } |
| 563 | |
Ken Dyck | 160146e | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 564 | /// getDeclAlign - Return a conservative estimate of the alignment of the |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 565 | /// specified decl. Note that bitfields do not have a valid alignment, so |
| 566 | /// this method will assert on them. |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 567 | /// If @p RefAsPointee, references are treated like their underlying type |
| 568 | /// (for alignof), else they're treated like pointers (for CodeGen). |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 569 | CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) const { |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 570 | unsigned Align = Target.getCharWidth(); |
| 571 | |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 572 | bool UseAlignAttrOnly = false; |
| 573 | if (unsigned AlignFromAttr = D->getMaxAlignment()) { |
| 574 | Align = AlignFromAttr; |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 575 | |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 576 | // __attribute__((aligned)) can increase or decrease alignment |
| 577 | // *except* on a struct or struct member, where it only increases |
| 578 | // alignment unless 'packed' is also specified. |
| 579 | // |
| 580 | // It is an error for [[align]] to decrease alignment, so we can |
| 581 | // ignore that possibility; Sema should diagnose it. |
| 582 | if (isa<FieldDecl>(D)) { |
| 583 | UseAlignAttrOnly = D->hasAttr<PackedAttr>() || |
| 584 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
| 585 | } else { |
| 586 | UseAlignAttrOnly = true; |
| 587 | } |
| 588 | } |
| 589 | |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 590 | // If we're using the align attribute only, just ignore everything |
| 591 | // else about the declaration and its type. |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 592 | if (UseAlignAttrOnly) { |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 593 | // do nothing |
| 594 | |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 595 | } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 596 | QualType T = VD->getType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 597 | if (const ReferenceType* RT = T->getAs<ReferenceType>()) { |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 598 | if (RefAsPointee) |
| 599 | T = RT->getPointeeType(); |
| 600 | else |
| 601 | T = getPointerType(RT->getPointeeType()); |
| 602 | } |
| 603 | if (!T->isIncompleteType() && !T->isFunctionType()) { |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 604 | // Adjust alignments of declarations with array type by the |
| 605 | // large-array alignment on the target. |
Rafael Espindola | e971b9a | 2010-06-04 23:15:27 +0000 | [diff] [blame] | 606 | unsigned MinWidth = Target.getLargeArrayMinWidth(); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 607 | const ArrayType *arrayType; |
| 608 | if (MinWidth && (arrayType = getAsArrayType(T))) { |
| 609 | if (isa<VariableArrayType>(arrayType)) |
| 610 | Align = std::max(Align, Target.getLargeArrayAlign()); |
| 611 | else if (isa<ConstantArrayType>(arrayType) && |
| 612 | MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType))) |
| 613 | Align = std::max(Align, Target.getLargeArrayAlign()); |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 614 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 615 | // Walk through any array types while we're at it. |
| 616 | T = getBaseElementType(arrayType); |
| 617 | } |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 618 | Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); |
| 619 | } |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 620 | |
| 621 | // Fields can be subject to extra alignment constraints, like if |
| 622 | // the field is packed, the struct is packed, or the struct has a |
| 623 | // a max-field-alignment constraint (#pragma pack). So calculate |
| 624 | // the actual alignment of the field within the struct, and then |
| 625 | // (as we're expected to) constrain that by the alignment of the type. |
| 626 | if (const FieldDecl *field = dyn_cast<FieldDecl>(VD)) { |
| 627 | // So calculate the alignment of the field. |
| 628 | const ASTRecordLayout &layout = getASTRecordLayout(field->getParent()); |
| 629 | |
| 630 | // Start with the record's overall alignment. |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 631 | unsigned fieldAlign = toBits(layout.getAlignment()); |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 632 | |
| 633 | // Use the GCD of that and the offset within the record. |
| 634 | uint64_t offset = layout.getFieldOffset(field->getFieldIndex()); |
| 635 | if (offset > 0) { |
| 636 | // Alignment is always a power of 2, so the GCD will be a power of 2, |
| 637 | // which means we get to do this crazy thing instead of Euclid's. |
| 638 | uint64_t lowBitOfOffset = offset & (~offset + 1); |
| 639 | if (lowBitOfOffset < fieldAlign) |
| 640 | fieldAlign = static_cast<unsigned>(lowBitOfOffset); |
| 641 | } |
| 642 | |
| 643 | Align = std::min(Align, fieldAlign); |
Charles Davis | 3fc5107 | 2010-02-23 04:52:00 +0000 | [diff] [blame] | 644 | } |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 645 | } |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 646 | |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 647 | return toCharUnitsFromBits(Align); |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 648 | } |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 649 | |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 650 | std::pair<CharUnits, CharUnits> |
Ken Dyck | d24099d | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 651 | ASTContext::getTypeInfoInChars(const Type *T) const { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 652 | std::pair<uint64_t, unsigned> Info = getTypeInfo(T); |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 653 | return std::make_pair(toCharUnitsFromBits(Info.first), |
| 654 | toCharUnitsFromBits(Info.second)); |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | std::pair<CharUnits, CharUnits> |
Ken Dyck | d24099d | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 658 | ASTContext::getTypeInfoInChars(QualType T) const { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 659 | return getTypeInfoInChars(T.getTypePtr()); |
| 660 | } |
| 661 | |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 662 | /// getTypeSize - Return the size of the specified type, in bits. This method |
| 663 | /// does not work on incomplete types. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 664 | /// |
| 665 | /// FIXME: Pointers into different addr spaces could have different sizes and |
| 666 | /// alignment requirements: getPointerInfo should take an AddrSpace, this |
| 667 | /// should take a QualType, &c. |
Chris Lattner | 4481b42 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 668 | std::pair<uint64_t, unsigned> |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 669 | ASTContext::getTypeInfo(const Type *T) const { |
Mike Stump | 5b9a3d5 | 2009-02-27 18:32:39 +0000 | [diff] [blame] | 670 | uint64_t Width=0; |
| 671 | unsigned Align=8; |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 672 | switch (T->getTypeClass()) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 673 | #define TYPE(Class, Base) |
| 674 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 675 | #define NON_CANONICAL_TYPE(Class, Base) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 676 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 677 | #include "clang/AST/TypeNodes.def" |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 678 | assert(false && "Should not see dependent types"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 679 | break; |
| 680 | |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 681 | case Type::FunctionNoProto: |
| 682 | case Type::FunctionProto: |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 683 | // GCC extension: alignof(function) = 32 bits |
| 684 | Width = 0; |
| 685 | Align = 32; |
| 686 | break; |
| 687 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 688 | case Type::IncompleteArray: |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 689 | case Type::VariableArray: |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 690 | Width = 0; |
| 691 | Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); |
| 692 | break; |
| 693 | |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 694 | case Type::ConstantArray: { |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 695 | const ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 696 | |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 697 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 698 | Width = EltInfo.first*CAT->getSize().getZExtValue(); |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 699 | Align = EltInfo.second; |
| 700 | break; |
Christopher Lamb | c5fafa2 | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 701 | } |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 702 | case Type::ExtVector: |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 703 | case Type::Vector: { |
Chris Lattner | 63d2b36 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 704 | const VectorType *VT = cast<VectorType>(T); |
| 705 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType()); |
| 706 | Width = EltInfo.first*VT->getNumElements(); |
Eli Friedman | 3df5efe | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 707 | Align = Width; |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 708 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 709 | // This happens for non-power-of-2 length vectors. |
Dan Gohman | ef78c8e | 2010-04-21 23:32:43 +0000 | [diff] [blame] | 710 | if (Align & (Align-1)) { |
Chris Lattner | 63d2b36 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 711 | Align = llvm::NextPowerOf2(Align); |
| 712 | Width = llvm::RoundUpToAlignment(Width, Align); |
| 713 | } |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 714 | break; |
| 715 | } |
Chris Lattner | 647fb22 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 716 | |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 717 | case Type::Builtin: |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 718 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 719 | default: assert(0 && "Unknown builtin type!"); |
Chris Lattner | 4481b42 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 720 | case BuiltinType::Void: |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 721 | // GCC extension: alignof(void) = 8 bits. |
| 722 | Width = 0; |
| 723 | Align = 8; |
| 724 | break; |
| 725 | |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 726 | case BuiltinType::Bool: |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 727 | Width = Target.getBoolWidth(); |
| 728 | Align = Target.getBoolAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 729 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 730 | case BuiltinType::Char_S: |
| 731 | case BuiltinType::Char_U: |
| 732 | case BuiltinType::UChar: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 733 | case BuiltinType::SChar: |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 734 | Width = Target.getCharWidth(); |
| 735 | Align = Target.getCharAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 736 | break; |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 737 | case BuiltinType::WChar_S: |
| 738 | case BuiltinType::WChar_U: |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 739 | Width = Target.getWCharWidth(); |
| 740 | Align = Target.getWCharAlign(); |
| 741 | break; |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 742 | case BuiltinType::Char16: |
| 743 | Width = Target.getChar16Width(); |
| 744 | Align = Target.getChar16Align(); |
| 745 | break; |
| 746 | case BuiltinType::Char32: |
| 747 | Width = Target.getChar32Width(); |
| 748 | Align = Target.getChar32Align(); |
| 749 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 750 | case BuiltinType::UShort: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 751 | case BuiltinType::Short: |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 752 | Width = Target.getShortWidth(); |
| 753 | Align = Target.getShortAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 754 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 755 | case BuiltinType::UInt: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 756 | case BuiltinType::Int: |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 757 | Width = Target.getIntWidth(); |
| 758 | Align = Target.getIntAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 759 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 760 | case BuiltinType::ULong: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 761 | case BuiltinType::Long: |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 762 | Width = Target.getLongWidth(); |
| 763 | Align = Target.getLongAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 764 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 765 | case BuiltinType::ULongLong: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 766 | case BuiltinType::LongLong: |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 767 | Width = Target.getLongLongWidth(); |
| 768 | Align = Target.getLongLongAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 769 | break; |
Chris Lattner | 0a415ec | 2009-04-30 02:55:13 +0000 | [diff] [blame] | 770 | case BuiltinType::Int128: |
| 771 | case BuiltinType::UInt128: |
| 772 | Width = 128; |
| 773 | Align = 128; // int128_t is 128-bit aligned on all targets. |
| 774 | break; |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 775 | case BuiltinType::Float: |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 776 | Width = Target.getFloatWidth(); |
| 777 | Align = Target.getFloatAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 778 | break; |
| 779 | case BuiltinType::Double: |
Chris Lattner | 4ba0cef | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 780 | Width = Target.getDoubleWidth(); |
| 781 | Align = Target.getDoubleAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 782 | break; |
| 783 | case BuiltinType::LongDouble: |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 784 | Width = Target.getLongDoubleWidth(); |
| 785 | Align = Target.getLongDoubleAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 786 | break; |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 787 | case BuiltinType::NullPtr: |
| 788 | Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) |
| 789 | Align = Target.getPointerAlign(0); // == sizeof(void*) |
Sebastian Redl | a81b0b7 | 2009-05-27 19:34:06 +0000 | [diff] [blame] | 790 | break; |
Fariborz Jahanian | 9c6a39e | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 791 | case BuiltinType::ObjCId: |
| 792 | case BuiltinType::ObjCClass: |
| 793 | case BuiltinType::ObjCSel: |
| 794 | Width = Target.getPointerWidth(0); |
| 795 | Align = Target.getPointerAlign(0); |
| 796 | break; |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 797 | } |
Chris Lattner | 48f84b8 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 798 | break; |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 799 | case Type::ObjCObjectPointer: |
Chris Lattner | 4ba0cef | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 800 | Width = Target.getPointerWidth(0); |
Chris Lattner | 2dca6ff | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 801 | Align = Target.getPointerAlign(0); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 802 | break; |
Steve Naroff | 921a45c | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 803 | case Type::BlockPointer: { |
| 804 | unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace(); |
| 805 | Width = Target.getPointerWidth(AS); |
| 806 | Align = Target.getPointerAlign(AS); |
| 807 | break; |
| 808 | } |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 809 | case Type::LValueReference: |
| 810 | case Type::RValueReference: { |
| 811 | // alignof and sizeof should never enter this code path here, so we go |
| 812 | // the pointer route. |
| 813 | unsigned AS = cast<ReferenceType>(T)->getPointeeType().getAddressSpace(); |
| 814 | Width = Target.getPointerWidth(AS); |
| 815 | Align = Target.getPointerAlign(AS); |
| 816 | break; |
| 817 | } |
Chris Lattner | 2dca6ff | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 818 | case Type::Pointer: { |
| 819 | unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace(); |
Chris Lattner | 4ba0cef | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 820 | Width = Target.getPointerWidth(AS); |
Chris Lattner | 2dca6ff | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 821 | Align = Target.getPointerAlign(AS); |
| 822 | break; |
| 823 | } |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 824 | case Type::MemberPointer: { |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 825 | const MemberPointerType *MPT = cast<MemberPointerType>(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 826 | std::pair<uint64_t, unsigned> PtrDiffInfo = |
Anders Carlsson | 32440a0 | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 827 | getTypeInfo(getPointerDiffType()); |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 828 | Width = PtrDiffInfo.first * ABI->getMemberPointerSize(MPT); |
Anders Carlsson | 32440a0 | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 829 | Align = PtrDiffInfo.second; |
| 830 | break; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 831 | } |
Chris Lattner | 647fb22 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 832 | case Type::Complex: { |
| 833 | // Complex types have the same alignment as their elements, but twice the |
| 834 | // size. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 835 | std::pair<uint64_t, unsigned> EltInfo = |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 836 | getTypeInfo(cast<ComplexType>(T)->getElementType()); |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 837 | Width = EltInfo.first*2; |
Chris Lattner | 647fb22 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 838 | Align = EltInfo.second; |
| 839 | break; |
| 840 | } |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 841 | case Type::ObjCObject: |
| 842 | return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr()); |
Devang Patel | dbb7263 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 843 | case Type::ObjCInterface: { |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 844 | const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | dbb7263 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 845 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 846 | Width = toBits(Layout.getSize()); |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 847 | Align = toBits(Layout.getAlignment()); |
Devang Patel | dbb7263 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 848 | break; |
| 849 | } |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 850 | case Type::Record: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 851 | case Type::Enum: { |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 852 | const TagType *TT = cast<TagType>(T); |
| 853 | |
| 854 | if (TT->getDecl()->isInvalidDecl()) { |
Chris Lattner | 572100b | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 855 | Width = 1; |
| 856 | Align = 1; |
| 857 | break; |
| 858 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 859 | |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 860 | if (const EnumType *ET = dyn_cast<EnumType>(TT)) |
Chris Lattner | 8b23c25 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 861 | return getTypeInfo(ET->getDecl()->getIntegerType()); |
| 862 | |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 863 | const RecordType *RT = cast<RecordType>(TT); |
Chris Lattner | 8b23c25 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 864 | const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 865 | Width = toBits(Layout.getSize()); |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 866 | Align = toBits(Layout.getAlignment()); |
Chris Lattner | 49a953a | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 867 | break; |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 868 | } |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 869 | |
Chris Lattner | 63d2b36 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 870 | case Type::SubstTemplateTypeParm: |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 871 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> |
| 872 | getReplacementType().getTypePtr()); |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 873 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 874 | case Type::Auto: { |
| 875 | const AutoType *A = cast<AutoType>(T); |
| 876 | assert(A->isDeduced() && "Cannot request the size of a dependent type"); |
Matt Beaumont-Gay | 7a24210e | 2011-02-22 20:00:16 +0000 | [diff] [blame] | 877 | return getTypeInfo(A->getDeducedType().getTypePtr()); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 880 | case Type::Paren: |
| 881 | return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr()); |
| 882 | |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 883 | case Type::Typedef: { |
| 884 | const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl(); |
Douglas Gregor | f5bae22 | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 885 | std::pair<uint64_t, unsigned> Info |
| 886 | = getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); |
Chris Lattner | 29eb47b | 2011-02-19 22:55:41 +0000 | [diff] [blame] | 887 | // If the typedef has an aligned attribute on it, it overrides any computed |
| 888 | // alignment we have. This violates the GCC documentation (which says that |
| 889 | // attribute(aligned) can only round up) but matches its implementation. |
| 890 | if (unsigned AttrAlign = Typedef->getMaxAlignment()) |
| 891 | Align = AttrAlign; |
| 892 | else |
| 893 | Align = Info.second; |
Douglas Gregor | f5bae22 | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 894 | Width = Info.first; |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 895 | break; |
Chris Lattner | 8b23c25 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 896 | } |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 897 | |
| 898 | case Type::TypeOfExpr: |
| 899 | return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType() |
| 900 | .getTypePtr()); |
| 901 | |
| 902 | case Type::TypeOf: |
| 903 | return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr()); |
| 904 | |
Anders Carlsson | 81df7b8 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 905 | case Type::Decltype: |
| 906 | return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType() |
| 907 | .getTypePtr()); |
| 908 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 909 | case Type::Elaborated: |
| 910 | return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 911 | |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 912 | case Type::Attributed: |
| 913 | return getTypeInfo( |
| 914 | cast<AttributedType>(T)->getEquivalentType().getTypePtr()); |
| 915 | |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 916 | case Type::TemplateSpecialization: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 917 | assert(getCanonicalType(T) != T && |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 918 | "Cannot request the size of a dependent type"); |
| 919 | // FIXME: this is likely to be wrong once we support template |
| 920 | // aliases, since a template alias could refer to a typedef that |
| 921 | // has an __aligned__ attribute on it. |
| 922 | return getTypeInfo(getCanonicalType(T)); |
| 923 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 924 | |
Chris Lattner | 53cfe80 | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 925 | assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2"); |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 926 | return std::make_pair(Width, Align); |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 927 | } |
| 928 | |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 929 | /// toCharUnitsFromBits - Convert a size in bits to a size in characters. |
| 930 | CharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const { |
| 931 | return CharUnits::fromQuantity(BitSize / getCharWidth()); |
| 932 | } |
| 933 | |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 934 | /// toBits - Convert a size in characters to a size in characters. |
| 935 | int64_t ASTContext::toBits(CharUnits CharSize) const { |
| 936 | return CharSize.getQuantity() * getCharWidth(); |
| 937 | } |
| 938 | |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 939 | /// getTypeSizeInChars - Return the size of the specified type, in characters. |
| 940 | /// This method does not work on incomplete types. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 941 | CharUnits ASTContext::getTypeSizeInChars(QualType T) const { |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 942 | return toCharUnitsFromBits(getTypeSize(T)); |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 943 | } |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 944 | CharUnits ASTContext::getTypeSizeInChars(const Type *T) const { |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 945 | return toCharUnitsFromBits(getTypeSize(T)); |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 946 | } |
| 947 | |
Ken Dyck | a6046ab | 2010-01-26 17:25:18 +0000 | [diff] [blame] | 948 | /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in |
Ken Dyck | 24d28d6 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 949 | /// characters. This method does not work on incomplete types. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 950 | CharUnits ASTContext::getTypeAlignInChars(QualType T) const { |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 951 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 24d28d6 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 952 | } |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 953 | CharUnits ASTContext::getTypeAlignInChars(const Type *T) const { |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 954 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 24d28d6 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 955 | } |
| 956 | |
Chris Lattner | a3402cd | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 957 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 958 | /// type for the current target in bits. This can be different than the ABI |
| 959 | /// alignment in cases where it is beneficial for performance to overalign |
| 960 | /// a data type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 961 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) const { |
Chris Lattner | a3402cd | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 962 | unsigned ABIAlign = getTypeAlign(T); |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 963 | |
| 964 | // Double and long long should be naturally aligned if possible. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 965 | if (const ComplexType* CT = T->getAs<ComplexType>()) |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 966 | T = CT->getElementType().getTypePtr(); |
| 967 | if (T->isSpecificBuiltinType(BuiltinType::Double) || |
| 968 | T->isSpecificBuiltinType(BuiltinType::LongLong)) |
| 969 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); |
| 970 | |
Chris Lattner | a3402cd | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 971 | return ABIAlign; |
| 972 | } |
| 973 | |
Fariborz Jahanian | 7c80959 | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 974 | /// ShallowCollectObjCIvars - |
| 975 | /// Collect all ivars, including those synthesized, in the current class. |
| 976 | /// |
| 977 | void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 978 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) const { |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 979 | // FIXME. This need be removed but there are two many places which |
| 980 | // assume const-ness of ObjCInterfaceDecl |
| 981 | ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI); |
| 982 | for (ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv; |
| 983 | Iv= Iv->getNextIvar()) |
| 984 | Ivars.push_back(Iv); |
Fariborz Jahanian | 7c80959 | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 985 | } |
| 986 | |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 987 | /// DeepCollectObjCIvars - |
| 988 | /// This routine first collects all declared, but not synthesized, ivars in |
| 989 | /// super class and then collects all ivars, including those synthesized for |
| 990 | /// current class. This routine is used for implementation of current class |
| 991 | /// when all ivars, declared and synthesized are known. |
Fariborz Jahanian | 0f44d81 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 992 | /// |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 993 | void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 994 | bool leafClass, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 995 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) const { |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 996 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
| 997 | DeepCollectObjCIvars(SuperClass, false, Ivars); |
| 998 | if (!leafClass) { |
| 999 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 1000 | E = OI->ivar_end(); I != E; ++I) |
Fariborz Jahanian | aef6622 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 1001 | Ivars.push_back(*I); |
| 1002 | } |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1003 | else |
| 1004 | ShallowCollectObjCIvars(OI, Ivars); |
Fariborz Jahanian | 0f44d81 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1007 | /// CollectInheritedProtocols - Collect all protocols in current class and |
| 1008 | /// those inherited by it. |
| 1009 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 1010 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) { |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1011 | if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
Ted Kremenek | 0ef508d | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1012 | // We can use protocol_iterator here instead of |
| 1013 | // all_referenced_protocol_iterator since we are walking all categories. |
| 1014 | for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(), |
| 1015 | PE = OI->all_referenced_protocol_end(); P != PE; ++P) { |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1016 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 1017 | Protocols.insert(Proto); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1018 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
Fariborz Jahanian | 8e3b9db | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 1019 | PE = Proto->protocol_end(); P != PE; ++P) { |
| 1020 | Protocols.insert(*P); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1021 | CollectInheritedProtocols(*P, Protocols); |
| 1022 | } |
Fariborz Jahanian | 8e3b9db | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 1023 | } |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1024 | |
| 1025 | // Categories of this Interface. |
| 1026 | for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList(); |
| 1027 | CDeclChain; CDeclChain = CDeclChain->getNextClassCategory()) |
| 1028 | CollectInheritedProtocols(CDeclChain, Protocols); |
| 1029 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) |
| 1030 | while (SD) { |
| 1031 | CollectInheritedProtocols(SD, Protocols); |
| 1032 | SD = SD->getSuperClass(); |
| 1033 | } |
Benjamin Kramer | 0a3fe04 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1034 | } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Ted Kremenek | 0ef508d | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1035 | for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(), |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1036 | PE = OC->protocol_end(); P != PE; ++P) { |
| 1037 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 1038 | Protocols.insert(Proto); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1039 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 1040 | PE = Proto->protocol_end(); P != PE; ++P) |
| 1041 | CollectInheritedProtocols(*P, Protocols); |
| 1042 | } |
Benjamin Kramer | 0a3fe04 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1043 | } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1044 | for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(), |
| 1045 | PE = OP->protocol_end(); P != PE; ++P) { |
| 1046 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 1047 | Protocols.insert(Proto); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1048 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 1049 | PE = Proto->protocol_end(); P != PE; ++P) |
| 1050 | CollectInheritedProtocols(*P, Protocols); |
| 1051 | } |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1052 | } |
| 1053 | } |
| 1054 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1055 | unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const { |
Fariborz Jahanian | d2ae2d0 | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1056 | unsigned count = 0; |
| 1057 | // Count ivars declared in class extension. |
Fariborz Jahanian | 3bf0ded | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 1058 | for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl; |
| 1059 | CDecl = CDecl->getNextClassExtension()) |
Benjamin Kramer | 0a3fe04 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1060 | count += CDecl->ivar_size(); |
| 1061 | |
Fariborz Jahanian | d2ae2d0 | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1062 | // Count ivar defined in this class's implementation. This |
| 1063 | // includes synthesized ivars. |
| 1064 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) |
Benjamin Kramer | 0a3fe04 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1065 | count += ImplDecl->ivar_size(); |
| 1066 | |
Fariborz Jahanian | 7c80959 | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 1067 | return count; |
| 1068 | } |
| 1069 | |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1070 | /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. |
| 1071 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
| 1072 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1073 | I = ObjCImpls.find(D); |
| 1074 | if (I != ObjCImpls.end()) |
| 1075 | return cast<ObjCImplementationDecl>(I->second); |
| 1076 | return 0; |
| 1077 | } |
| 1078 | /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. |
| 1079 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
| 1080 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1081 | I = ObjCImpls.find(D); |
| 1082 | if (I != ObjCImpls.end()) |
| 1083 | return cast<ObjCCategoryImplDecl>(I->second); |
| 1084 | return 0; |
| 1085 | } |
| 1086 | |
| 1087 | /// \brief Set the implementation of ObjCInterfaceDecl. |
| 1088 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
| 1089 | ObjCImplementationDecl *ImplD) { |
| 1090 | assert(IFaceD && ImplD && "Passed null params"); |
| 1091 | ObjCImpls[IFaceD] = ImplD; |
| 1092 | } |
| 1093 | /// \brief Set the implementation of ObjCCategoryDecl. |
| 1094 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
| 1095 | ObjCCategoryImplDecl *ImplD) { |
| 1096 | assert(CatD && ImplD && "Passed null params"); |
| 1097 | ObjCImpls[CatD] = ImplD; |
| 1098 | } |
| 1099 | |
Fariborz Jahanian | 7cfe767 | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1100 | /// \brief Get the copy initialization expression of VarDecl,or NULL if |
| 1101 | /// none exists. |
Fariborz Jahanian | 5019809 | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1102 | Expr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) { |
Fariborz Jahanian | 1321cbb | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1103 | assert(VD && "Passed null params"); |
| 1104 | assert(VD->hasAttr<BlocksAttr>() && |
| 1105 | "getBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 5019809 | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1106 | llvm::DenseMap<const VarDecl*, Expr*>::iterator |
Fariborz Jahanian | 1321cbb | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1107 | I = BlockVarCopyInits.find(VD); |
Fariborz Jahanian | 7cfe767 | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1108 | return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0; |
| 1109 | } |
| 1110 | |
| 1111 | /// \brief Set the copy inialization expression of a block var decl. |
| 1112 | void ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) { |
| 1113 | assert(VD && Init && "Passed null params"); |
Fariborz Jahanian | 1321cbb | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1114 | assert(VD->hasAttr<BlocksAttr>() && |
| 1115 | "setBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 7cfe767 | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1116 | BlockVarCopyInits[VD] = Init; |
| 1117 | } |
| 1118 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1119 | /// \brief Allocate an uninitialized TypeSourceInfo. |
Argyrios Kyrtzidis | 3f79ad7 | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1120 | /// |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1121 | /// The caller should initialize the memory held by TypeSourceInfo using |
Argyrios Kyrtzidis | 3f79ad7 | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1122 | /// the TypeLoc wrappers. |
| 1123 | /// |
| 1124 | /// \param T the type that will be the basis for type source info. This type |
| 1125 | /// should refer to how the declarator was written in source code, not to |
| 1126 | /// what type semantic analysis resolved the declarator to. |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1127 | TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1128 | unsigned DataSize) const { |
John McCall | 26fe7e0 | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1129 | if (!DataSize) |
| 1130 | DataSize = TypeLoc::getFullDataSizeForType(T); |
| 1131 | else |
| 1132 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1133 | "incorrect data size provided to CreateTypeSourceInfo!"); |
John McCall | 26fe7e0 | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1134 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1135 | TypeSourceInfo *TInfo = |
| 1136 | (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8); |
| 1137 | new (TInfo) TypeSourceInfo(T); |
| 1138 | return TInfo; |
Argyrios Kyrtzidis | 3f79ad7 | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1141 | TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1142 | SourceLocation L) const { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1143 | TypeSourceInfo *DI = CreateTypeSourceInfo(T); |
Douglas Gregor | 2d525f0 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 1144 | DI->getTypeLoc().initialize(const_cast<ASTContext &>(*this), L); |
John McCall | 3665e00 | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 1145 | return DI; |
| 1146 | } |
| 1147 | |
Daniel Dunbar | 02f7f5f | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1148 | const ASTRecordLayout & |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1149 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const { |
Daniel Dunbar | 02f7f5f | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1150 | return getObjCLayout(D, 0); |
| 1151 | } |
| 1152 | |
| 1153 | const ASTRecordLayout & |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1154 | ASTContext::getASTObjCImplementationLayout( |
| 1155 | const ObjCImplementationDecl *D) const { |
Daniel Dunbar | 02f7f5f | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1156 | return getObjCLayout(D->getClassInterface(), D); |
| 1157 | } |
| 1158 | |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1159 | //===----------------------------------------------------------------------===// |
| 1160 | // Type creation/memoization methods |
| 1161 | //===----------------------------------------------------------------------===// |
| 1162 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1163 | QualType |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1164 | ASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const { |
| 1165 | unsigned fastQuals = quals.getFastQualifiers(); |
| 1166 | quals.removeFastQualifiers(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1167 | |
| 1168 | // Check if we've already instantiated this type. |
| 1169 | llvm::FoldingSetNodeID ID; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1170 | ExtQuals::Profile(ID, baseType, quals); |
| 1171 | void *insertPos = 0; |
| 1172 | if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, insertPos)) { |
| 1173 | assert(eq->getQualifiers() == quals); |
| 1174 | return QualType(eq, fastQuals); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1177 | // If the base type is not canonical, make the appropriate canonical type. |
| 1178 | QualType canon; |
| 1179 | if (!baseType->isCanonicalUnqualified()) { |
| 1180 | SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split(); |
| 1181 | canonSplit.second.addConsistentQualifiers(quals); |
| 1182 | canon = getExtQualType(canonSplit.first, canonSplit.second); |
| 1183 | |
| 1184 | // Re-find the insert position. |
| 1185 | (void) ExtQualNodes.FindNodeOrInsertPos(ID, insertPos); |
| 1186 | } |
| 1187 | |
| 1188 | ExtQuals *eq = new (*this, TypeAlignment) ExtQuals(baseType, canon, quals); |
| 1189 | ExtQualNodes.InsertNode(eq, insertPos); |
| 1190 | return QualType(eq, fastQuals); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1193 | QualType |
| 1194 | ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) const { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1195 | QualType CanT = getCanonicalType(T); |
| 1196 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | 445fcab | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1197 | return T; |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1198 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1199 | // If we are composing extended qualifiers together, merge together |
| 1200 | // into one ExtQuals node. |
| 1201 | QualifierCollector Quals; |
| 1202 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1203 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1204 | // If this type already has an address space specified, it cannot get |
| 1205 | // another one. |
| 1206 | assert(!Quals.hasAddressSpace() && |
| 1207 | "Type cannot be in multiple addr spaces!"); |
| 1208 | Quals.addAddressSpace(AddressSpace); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1209 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1210 | return getExtQualType(TypeNode, Quals); |
Christopher Lamb | 025b5fb | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1213 | QualType ASTContext::getObjCGCQualType(QualType T, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1214 | Qualifiers::GC GCAttr) const { |
Fariborz Jahanian | e27e934 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1215 | QualType CanT = getCanonicalType(T); |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1216 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | e27e934 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1217 | return T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1218 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1219 | if (const PointerType *ptr = T->getAs<PointerType>()) { |
| 1220 | QualType Pointee = ptr->getPointeeType(); |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 1221 | if (Pointee->isAnyPointerType()) { |
Fariborz Jahanian | b68215c | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1222 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 1223 | return getPointerType(ResultType); |
| 1224 | } |
| 1225 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1226 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1227 | // If we are composing extended qualifiers together, merge together |
| 1228 | // into one ExtQuals node. |
| 1229 | QualifierCollector Quals; |
| 1230 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1231 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1232 | // If this type already has an ObjCGC specified, it cannot get |
| 1233 | // another one. |
| 1234 | assert(!Quals.hasObjCGCAttr() && |
| 1235 | "Type cannot have multiple ObjCGCs!"); |
| 1236 | Quals.addObjCGCAttr(GCAttr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1237 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1238 | return getExtQualType(TypeNode, Quals); |
Fariborz Jahanian | e27e934 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1239 | } |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1240 | |
John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 1241 | const FunctionType *ASTContext::adjustFunctionType(const FunctionType *T, |
| 1242 | FunctionType::ExtInfo Info) { |
| 1243 | if (T->getExtInfo() == Info) |
| 1244 | return T; |
| 1245 | |
| 1246 | QualType Result; |
| 1247 | if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) { |
| 1248 | Result = getFunctionNoProtoType(FNPT->getResultType(), Info); |
| 1249 | } else { |
| 1250 | const FunctionProtoType *FPT = cast<FunctionProtoType>(T); |
| 1251 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 1252 | EPI.ExtInfo = Info; |
| 1253 | Result = getFunctionType(FPT->getResultType(), FPT->arg_type_begin(), |
| 1254 | FPT->getNumArgs(), EPI); |
| 1255 | } |
| 1256 | |
| 1257 | return cast<FunctionType>(Result.getTypePtr()); |
| 1258 | } |
| 1259 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1260 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 1261 | /// number with the specified element type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1262 | QualType ASTContext::getComplexType(QualType T) const { |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1263 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1264 | // structure. |
| 1265 | llvm::FoldingSetNodeID ID; |
| 1266 | ComplexType::Profile(ID, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1267 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1268 | void *InsertPos = 0; |
| 1269 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1270 | return QualType(CT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1271 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1272 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1273 | // so fill in the canonical type field. |
| 1274 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1275 | if (!T.isCanonical()) { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1276 | Canonical = getComplexType(getCanonicalType(T)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1277 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1278 | // Get the new insert position for the node we care about. |
| 1279 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1280 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1281 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1282 | ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical); |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1283 | Types.push_back(New); |
| 1284 | ComplexTypes.InsertNode(New, InsertPos); |
| 1285 | return QualType(New, 0); |
| 1286 | } |
| 1287 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1288 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 1289 | /// the specified type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1290 | QualType ASTContext::getPointerType(QualType T) const { |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 1291 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1292 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 1293 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 1294 | PointerType::Profile(ID, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1295 | |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 1296 | void *InsertPos = 0; |
| 1297 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1298 | return QualType(PT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1299 | |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 1300 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1301 | // so fill in the canonical type field. |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1302 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1303 | if (!T.isCanonical()) { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1304 | Canonical = getPointerType(getCanonicalType(T)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1305 | |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 1306 | // Get the new insert position for the node we care about. |
| 1307 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1308 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 1309 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1310 | PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical); |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 1311 | Types.push_back(New); |
| 1312 | PointerTypes.InsertNode(New, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1313 | return QualType(New, 0); |
Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1316 | /// getBlockPointerType - Return the uniqued reference to the type for |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1317 | /// a pointer to the specified block. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1318 | QualType ASTContext::getBlockPointerType(QualType T) const { |
Steve Naroff | 0ac01283 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1319 | assert(T->isFunctionType() && "block of function types only"); |
| 1320 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1321 | // structure. |
| 1322 | llvm::FoldingSetNodeID ID; |
| 1323 | BlockPointerType::Profile(ID, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1324 | |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1325 | void *InsertPos = 0; |
| 1326 | if (BlockPointerType *PT = |
| 1327 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1328 | return QualType(PT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1329 | |
| 1330 | // If the block pointee type isn't canonical, this won't be a canonical |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1331 | // type either so fill in the canonical type field. |
| 1332 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1333 | if (!T.isCanonical()) { |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1334 | Canonical = getBlockPointerType(getCanonicalType(T)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1335 | |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1336 | // Get the new insert position for the node we care about. |
| 1337 | BlockPointerType *NewIP = |
| 1338 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1339 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1340 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1341 | BlockPointerType *New |
| 1342 | = new (*this, TypeAlignment) BlockPointerType(T, Canonical); |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1343 | Types.push_back(New); |
| 1344 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 1345 | return QualType(New, 0); |
| 1346 | } |
| 1347 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1348 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 1349 | /// lvalue reference to the specified type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1350 | QualType |
| 1351 | ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const { |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1352 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1353 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 1354 | llvm::FoldingSetNodeID ID; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1355 | ReferenceType::Profile(ID, T, SpelledAsLValue); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1356 | |
| 1357 | void *InsertPos = 0; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1358 | if (LValueReferenceType *RT = |
| 1359 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1360 | return QualType(RT, 0); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1361 | |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1362 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1363 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1364 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1365 | // either, so fill in the canonical type field. |
| 1366 | QualType Canonical; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1367 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
| 1368 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1369 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1370 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1371 | // Get the new insert position for the node we care about. |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1372 | LValueReferenceType *NewIP = |
| 1373 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1374 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1377 | LValueReferenceType *New |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1378 | = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, |
| 1379 | SpelledAsLValue); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1380 | Types.push_back(New); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1381 | LValueReferenceTypes.InsertNode(New, InsertPos); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1382 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1383 | return QualType(New, 0); |
| 1384 | } |
| 1385 | |
| 1386 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 1387 | /// rvalue reference to the specified type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1388 | QualType ASTContext::getRValueReferenceType(QualType T) const { |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1389 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1390 | // structure. |
| 1391 | llvm::FoldingSetNodeID ID; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1392 | ReferenceType::Profile(ID, T, false); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1393 | |
| 1394 | void *InsertPos = 0; |
| 1395 | if (RValueReferenceType *RT = |
| 1396 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1397 | return QualType(RT, 0); |
| 1398 | |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1399 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1400 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1401 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1402 | // either, so fill in the canonical type field. |
| 1403 | QualType Canonical; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1404 | if (InnerRef || !T.isCanonical()) { |
| 1405 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1406 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1407 | |
| 1408 | // Get the new insert position for the node we care about. |
| 1409 | RValueReferenceType *NewIP = |
| 1410 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1411 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1412 | } |
| 1413 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1414 | RValueReferenceType *New |
| 1415 | = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1416 | Types.push_back(New); |
| 1417 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1418 | return QualType(New, 0); |
| 1419 | } |
| 1420 | |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1421 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 1422 | /// member pointer to the specified type, in the specified class. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1423 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const { |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1424 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1425 | // structure. |
| 1426 | llvm::FoldingSetNodeID ID; |
| 1427 | MemberPointerType::Profile(ID, T, Cls); |
| 1428 | |
| 1429 | void *InsertPos = 0; |
| 1430 | if (MemberPointerType *PT = |
| 1431 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1432 | return QualType(PT, 0); |
| 1433 | |
| 1434 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 1435 | // type either, so fill in the canonical type field. |
| 1436 | QualType Canonical; |
Douglas Gregor | 615ac67 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1437 | if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) { |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1438 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 1439 | |
| 1440 | // Get the new insert position for the node we care about. |
| 1441 | MemberPointerType *NewIP = |
| 1442 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1443 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1444 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1445 | MemberPointerType *New |
| 1446 | = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1447 | Types.push_back(New); |
| 1448 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 1449 | return QualType(New, 0); |
| 1450 | } |
| 1451 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1452 | /// getConstantArrayType - Return the unique reference to the type for an |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1453 | /// array of the specified element type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1454 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | e2df3f9 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1455 | const llvm::APInt &ArySizeIn, |
Steve Naroff | 90dfdd5 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1456 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1457 | unsigned IndexTypeQuals) const { |
Sebastian Redl | 2dfdb82 | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1458 | assert((EltTy->isDependentType() || |
| 1459 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && |
Eli Friedman | be7e42b | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1460 | "Constant array of VLAs is illegal!"); |
| 1461 | |
Chris Lattner | e2df3f9 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1462 | // Convert the array size into a canonical width matching the pointer size for |
| 1463 | // the target. |
| 1464 | llvm::APInt ArySize(ArySizeIn); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1465 | ArySize = |
| 1466 | ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1467 | |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 1468 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1469 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, IndexTypeQuals); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1470 | |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 1471 | void *InsertPos = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1472 | if (ConstantArrayType *ATP = |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1473 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1474 | return QualType(ATP, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1475 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1476 | // If the element type isn't canonical or has qualifiers, this won't |
| 1477 | // be a canonical type either, so fill in the canonical type field. |
| 1478 | QualType Canon; |
| 1479 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 1480 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
| 1481 | Canon = getConstantArrayType(QualType(canonSplit.first, 0), ArySize, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1482 | ASM, IndexTypeQuals); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1483 | Canon = getQualifiedType(Canon, canonSplit.second); |
| 1484 | |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 1485 | // Get the new insert position for the node we care about. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1486 | ConstantArrayType *NewIP = |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1487 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1488 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 1489 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1490 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1491 | ConstantArrayType *New = new(*this,TypeAlignment) |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1492 | ConstantArrayType(EltTy, Canon, ArySize, ASM, IndexTypeQuals); |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1493 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 1494 | Types.push_back(New); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1495 | return QualType(New, 0); |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 1496 | } |
| 1497 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1498 | /// getVariableArrayDecayedType - Turns the given type, which may be |
| 1499 | /// variably-modified, into the corresponding type with all the known |
| 1500 | /// sizes replaced with [*]. |
| 1501 | QualType ASTContext::getVariableArrayDecayedType(QualType type) const { |
| 1502 | // Vastly most common case. |
| 1503 | if (!type->isVariablyModifiedType()) return type; |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1504 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1505 | QualType result; |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1506 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1507 | SplitQualType split = type.getSplitDesugaredType(); |
| 1508 | const Type *ty = split.first; |
| 1509 | switch (ty->getTypeClass()) { |
| 1510 | #define TYPE(Class, Base) |
| 1511 | #define ABSTRACT_TYPE(Class, Base) |
| 1512 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 1513 | #include "clang/AST/TypeNodes.def" |
| 1514 | llvm_unreachable("didn't desugar past all non-canonical types?"); |
| 1515 | |
| 1516 | // These types should never be variably-modified. |
| 1517 | case Type::Builtin: |
| 1518 | case Type::Complex: |
| 1519 | case Type::Vector: |
| 1520 | case Type::ExtVector: |
| 1521 | case Type::DependentSizedExtVector: |
| 1522 | case Type::ObjCObject: |
| 1523 | case Type::ObjCInterface: |
| 1524 | case Type::ObjCObjectPointer: |
| 1525 | case Type::Record: |
| 1526 | case Type::Enum: |
| 1527 | case Type::UnresolvedUsing: |
| 1528 | case Type::TypeOfExpr: |
| 1529 | case Type::TypeOf: |
| 1530 | case Type::Decltype: |
| 1531 | case Type::DependentName: |
| 1532 | case Type::InjectedClassName: |
| 1533 | case Type::TemplateSpecialization: |
| 1534 | case Type::DependentTemplateSpecialization: |
| 1535 | case Type::TemplateTypeParm: |
| 1536 | case Type::SubstTemplateTypeParmPack: |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1537 | case Type::Auto: |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1538 | case Type::PackExpansion: |
| 1539 | llvm_unreachable("type should never be variably-modified"); |
| 1540 | |
| 1541 | // These types can be variably-modified but should never need to |
| 1542 | // further decay. |
| 1543 | case Type::FunctionNoProto: |
| 1544 | case Type::FunctionProto: |
| 1545 | case Type::BlockPointer: |
| 1546 | case Type::MemberPointer: |
| 1547 | return type; |
| 1548 | |
| 1549 | // These types can be variably-modified. All these modifications |
| 1550 | // preserve structure except as noted by comments. |
| 1551 | // TODO: if we ever care about optimizing VLAs, there are no-op |
| 1552 | // optimizations available here. |
| 1553 | case Type::Pointer: |
| 1554 | result = getPointerType(getVariableArrayDecayedType( |
| 1555 | cast<PointerType>(ty)->getPointeeType())); |
| 1556 | break; |
| 1557 | |
| 1558 | case Type::LValueReference: { |
| 1559 | const LValueReferenceType *lv = cast<LValueReferenceType>(ty); |
| 1560 | result = getLValueReferenceType( |
| 1561 | getVariableArrayDecayedType(lv->getPointeeType()), |
| 1562 | lv->isSpelledAsLValue()); |
| 1563 | break; |
| 1564 | } |
| 1565 | |
| 1566 | case Type::RValueReference: { |
| 1567 | const RValueReferenceType *lv = cast<RValueReferenceType>(ty); |
| 1568 | result = getRValueReferenceType( |
| 1569 | getVariableArrayDecayedType(lv->getPointeeType())); |
| 1570 | break; |
| 1571 | } |
| 1572 | |
| 1573 | case Type::ConstantArray: { |
| 1574 | const ConstantArrayType *cat = cast<ConstantArrayType>(ty); |
| 1575 | result = getConstantArrayType( |
| 1576 | getVariableArrayDecayedType(cat->getElementType()), |
| 1577 | cat->getSize(), |
| 1578 | cat->getSizeModifier(), |
| 1579 | cat->getIndexTypeCVRQualifiers()); |
| 1580 | break; |
| 1581 | } |
| 1582 | |
| 1583 | case Type::DependentSizedArray: { |
| 1584 | const DependentSizedArrayType *dat = cast<DependentSizedArrayType>(ty); |
| 1585 | result = getDependentSizedArrayType( |
| 1586 | getVariableArrayDecayedType(dat->getElementType()), |
| 1587 | dat->getSizeExpr(), |
| 1588 | dat->getSizeModifier(), |
| 1589 | dat->getIndexTypeCVRQualifiers(), |
| 1590 | dat->getBracketsRange()); |
| 1591 | break; |
| 1592 | } |
| 1593 | |
| 1594 | // Turn incomplete types into [*] types. |
| 1595 | case Type::IncompleteArray: { |
| 1596 | const IncompleteArrayType *iat = cast<IncompleteArrayType>(ty); |
| 1597 | result = getVariableArrayType( |
| 1598 | getVariableArrayDecayedType(iat->getElementType()), |
| 1599 | /*size*/ 0, |
| 1600 | ArrayType::Normal, |
| 1601 | iat->getIndexTypeCVRQualifiers(), |
| 1602 | SourceRange()); |
| 1603 | break; |
| 1604 | } |
| 1605 | |
| 1606 | // Turn VLA types into [*] types. |
| 1607 | case Type::VariableArray: { |
| 1608 | const VariableArrayType *vat = cast<VariableArrayType>(ty); |
| 1609 | result = getVariableArrayType( |
| 1610 | getVariableArrayDecayedType(vat->getElementType()), |
| 1611 | /*size*/ 0, |
| 1612 | ArrayType::Star, |
| 1613 | vat->getIndexTypeCVRQualifiers(), |
| 1614 | vat->getBracketsRange()); |
| 1615 | break; |
| 1616 | } |
| 1617 | } |
| 1618 | |
| 1619 | // Apply the top-level qualifiers from the original. |
| 1620 | return getQualifiedType(result, split.second); |
| 1621 | } |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1622 | |
Steve Naroff | cadebd0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 1623 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 1624 | /// variable array of the specified element type. |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1625 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 1626 | Expr *NumElts, |
Steve Naroff | 90dfdd5 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1627 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1628 | unsigned IndexTypeQuals, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1629 | SourceRange Brackets) const { |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1630 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 1631 | // that have an expression provided for their size. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1632 | QualType Canon; |
Douglas Gregor | 5e8c8c0 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 1633 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1634 | // Be sure to pull qualifiers off the element type. |
| 1635 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 1636 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
| 1637 | Canon = getVariableArrayType(QualType(canonSplit.first, 0), NumElts, ASM, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1638 | IndexTypeQuals, Brackets); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1639 | Canon = getQualifiedType(Canon, canonSplit.second); |
Douglas Gregor | 5e8c8c0 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1642 | VariableArrayType *New = new(*this, TypeAlignment) |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1643 | VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1644 | |
| 1645 | VariableArrayTypes.push_back(New); |
| 1646 | Types.push_back(New); |
| 1647 | return QualType(New, 0); |
| 1648 | } |
| 1649 | |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1650 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 1651 | /// the type for a dependently-sized array of the specified element |
Douglas Gregor | f3f9552 | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1652 | /// type. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1653 | QualType ASTContext::getDependentSizedArrayType(QualType elementType, |
| 1654 | Expr *numElements, |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1655 | ArrayType::ArraySizeModifier ASM, |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1656 | unsigned elementTypeQuals, |
| 1657 | SourceRange brackets) const { |
| 1658 | assert((!numElements || numElements->isTypeDependent() || |
| 1659 | numElements->isValueDependent()) && |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1660 | "Size must be type- or value-dependent!"); |
| 1661 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1662 | // Dependently-sized array types that do not have a specified number |
| 1663 | // of elements will have their sizes deduced from a dependent |
| 1664 | // initializer. We do no canonicalization here at all, which is okay |
| 1665 | // because they can't be used in most locations. |
| 1666 | if (!numElements) { |
| 1667 | DependentSizedArrayType *newType |
| 1668 | = new (*this, TypeAlignment) |
| 1669 | DependentSizedArrayType(*this, elementType, QualType(), |
| 1670 | numElements, ASM, elementTypeQuals, |
| 1671 | brackets); |
| 1672 | Types.push_back(newType); |
| 1673 | return QualType(newType, 0); |
| 1674 | } |
| 1675 | |
| 1676 | // Otherwise, we actually build a new type every time, but we |
| 1677 | // also build a canonical type. |
| 1678 | |
| 1679 | SplitQualType canonElementType = getCanonicalType(elementType).split(); |
| 1680 | |
| 1681 | void *insertPos = 0; |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1682 | llvm::FoldingSetNodeID ID; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1683 | DependentSizedArrayType::Profile(ID, *this, |
| 1684 | QualType(canonElementType.first, 0), |
| 1685 | ASM, elementTypeQuals, numElements); |
Douglas Gregor | ad2956c | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1686 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1687 | // Look for an existing type with these properties. |
| 1688 | DependentSizedArrayType *canonTy = |
| 1689 | DependentSizedArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
Douglas Gregor | ad2956c | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1690 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1691 | // If we don't have one, build one. |
| 1692 | if (!canonTy) { |
| 1693 | canonTy = new (*this, TypeAlignment) |
| 1694 | DependentSizedArrayType(*this, QualType(canonElementType.first, 0), |
| 1695 | QualType(), numElements, ASM, elementTypeQuals, |
| 1696 | brackets); |
| 1697 | DependentSizedArrayTypes.InsertNode(canonTy, insertPos); |
| 1698 | Types.push_back(canonTy); |
Douglas Gregor | ad2956c | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1699 | } |
| 1700 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1701 | // Apply qualifiers from the element type to the array. |
| 1702 | QualType canon = getQualifiedType(QualType(canonTy,0), |
| 1703 | canonElementType.second); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1704 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1705 | // If we didn't need extra canonicalization for the element type, |
| 1706 | // then just use that as our result. |
| 1707 | if (QualType(canonElementType.first, 0) == elementType) |
| 1708 | return canon; |
| 1709 | |
| 1710 | // Otherwise, we need to build a type which follows the spelling |
| 1711 | // of the element type. |
| 1712 | DependentSizedArrayType *sugaredType |
| 1713 | = new (*this, TypeAlignment) |
| 1714 | DependentSizedArrayType(*this, elementType, canon, numElements, |
| 1715 | ASM, elementTypeQuals, brackets); |
| 1716 | Types.push_back(sugaredType); |
| 1717 | return QualType(sugaredType, 0); |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1720 | QualType ASTContext::getIncompleteArrayType(QualType elementType, |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1721 | ArrayType::ArraySizeModifier ASM, |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1722 | unsigned elementTypeQuals) const { |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1723 | llvm::FoldingSetNodeID ID; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1724 | IncompleteArrayType::Profile(ID, elementType, ASM, elementTypeQuals); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1725 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1726 | void *insertPos = 0; |
| 1727 | if (IncompleteArrayType *iat = |
| 1728 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos)) |
| 1729 | return QualType(iat, 0); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1730 | |
| 1731 | // If the element type isn't canonical, this won't be a canonical type |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1732 | // either, so fill in the canonical type field. We also have to pull |
| 1733 | // qualifiers off the element type. |
| 1734 | QualType canon; |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1735 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1736 | if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) { |
| 1737 | SplitQualType canonSplit = getCanonicalType(elementType).split(); |
| 1738 | canon = getIncompleteArrayType(QualType(canonSplit.first, 0), |
| 1739 | ASM, elementTypeQuals); |
| 1740 | canon = getQualifiedType(canon, canonSplit.second); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1741 | |
| 1742 | // Get the new insert position for the node we care about. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1743 | IncompleteArrayType *existing = |
| 1744 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
| 1745 | assert(!existing && "Shouldn't be in the map!"); (void) existing; |
Ted Kremenek | 843ebedd | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1746 | } |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1747 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1748 | IncompleteArrayType *newType = new (*this, TypeAlignment) |
| 1749 | IncompleteArrayType(elementType, canon, ASM, elementTypeQuals); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1750 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1751 | IncompleteArrayTypes.InsertNode(newType, insertPos); |
| 1752 | Types.push_back(newType); |
| 1753 | return QualType(newType, 0); |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1756 | /// getVectorType - Return the unique reference to a vector type of |
| 1757 | /// the specified element type and size. VectorType must be a built-in type. |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1758 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1759 | VectorType::VectorKind VecKind) const { |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1760 | assert(vecType->isBuiltinType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1761 | |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 1762 | // Check if we've already instantiated a vector of this type. |
| 1763 | llvm::FoldingSetNodeID ID; |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1764 | VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind); |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1765 | |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 1766 | void *InsertPos = 0; |
| 1767 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1768 | return QualType(VTP, 0); |
| 1769 | |
| 1770 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1771 | // so fill in the canonical type field. |
| 1772 | QualType Canonical; |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1773 | if (!vecType.isCanonical()) { |
Bob Wilson | 7795480 | 2010-11-16 00:32:20 +0000 | [diff] [blame] | 1774 | Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1775 | |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 1776 | // Get the new insert position for the node we care about. |
| 1777 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1778 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 1779 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1780 | VectorType *New = new (*this, TypeAlignment) |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1781 | VectorType(vecType, NumElts, Canonical, VecKind); |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 1782 | VectorTypes.InsertNode(New, InsertPos); |
| 1783 | Types.push_back(New); |
| 1784 | return QualType(New, 0); |
| 1785 | } |
| 1786 | |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1787 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1788 | /// the specified element type and size. VectorType must be a built-in type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1789 | QualType |
| 1790 | ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) const { |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1791 | assert(vecType->isBuiltinType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1792 | |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1793 | // Check if we've already instantiated a vector of this type. |
| 1794 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1795 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1796 | VectorType::GenericVector); |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1797 | void *InsertPos = 0; |
| 1798 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1799 | return QualType(VTP, 0); |
| 1800 | |
| 1801 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1802 | // so fill in the canonical type field. |
| 1803 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1804 | if (!vecType.isCanonical()) { |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1805 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1806 | |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1807 | // Get the new insert position for the node we care about. |
| 1808 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1809 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1810 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1811 | ExtVectorType *New = new (*this, TypeAlignment) |
| 1812 | ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1813 | VectorTypes.InsertNode(New, InsertPos); |
| 1814 | Types.push_back(New); |
| 1815 | return QualType(New, 0); |
| 1816 | } |
| 1817 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1818 | QualType |
| 1819 | ASTContext::getDependentSizedExtVectorType(QualType vecType, |
| 1820 | Expr *SizeExpr, |
| 1821 | SourceLocation AttrLoc) const { |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1822 | llvm::FoldingSetNodeID ID; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1823 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1824 | SizeExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1825 | |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1826 | void *InsertPos = 0; |
| 1827 | DependentSizedExtVectorType *Canon |
| 1828 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1829 | DependentSizedExtVectorType *New; |
| 1830 | if (Canon) { |
| 1831 | // We already have a canonical version of this array type; use it as |
| 1832 | // the canonical type for a newly-built type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1833 | New = new (*this, TypeAlignment) |
| 1834 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), |
| 1835 | SizeExpr, AttrLoc); |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1836 | } else { |
| 1837 | QualType CanonVecTy = getCanonicalType(vecType); |
| 1838 | if (CanonVecTy == vecType) { |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1839 | New = new (*this, TypeAlignment) |
| 1840 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, |
| 1841 | AttrLoc); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1842 | |
| 1843 | DependentSizedExtVectorType *CanonCheck |
| 1844 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1845 | assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken"); |
| 1846 | (void)CanonCheck; |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1847 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); |
| 1848 | } else { |
| 1849 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 1850 | SourceLocation()); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1851 | New = new (*this, TypeAlignment) |
| 1852 | DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1853 | } |
| 1854 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1855 | |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1856 | Types.push_back(New); |
| 1857 | return QualType(New, 0); |
| 1858 | } |
| 1859 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1860 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 1861 | /// |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1862 | QualType |
| 1863 | ASTContext::getFunctionNoProtoType(QualType ResultTy, |
| 1864 | const FunctionType::ExtInfo &Info) const { |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1865 | const CallingConv CallConv = Info.getCC(); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 1866 | // Unique functions, to guarantee there is only one function of a particular |
| 1867 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 1868 | llvm::FoldingSetNodeID ID; |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1869 | FunctionNoProtoType::Profile(ID, ResultTy, Info); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1870 | |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 1871 | void *InsertPos = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1872 | if (FunctionNoProtoType *FT = |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1873 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1874 | return QualType(FT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1875 | |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1876 | QualType Canonical; |
Douglas Gregor | 8c94086 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1877 | if (!ResultTy.isCanonical() || |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1878 | getCanonicalCallConv(CallConv) != CallConv) { |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1879 | Canonical = |
| 1880 | getFunctionNoProtoType(getCanonicalType(ResultTy), |
| 1881 | Info.withCallingConv(getCanonicalCallConv(CallConv))); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1882 | |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 1883 | // Get the new insert position for the node we care about. |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1884 | FunctionNoProtoType *NewIP = |
| 1885 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1886 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 1887 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1888 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1889 | FunctionNoProtoType *New = new (*this, TypeAlignment) |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1890 | FunctionNoProtoType(ResultTy, Canonical, Info); |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 1891 | Types.push_back(New); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1892 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1893 | return QualType(New, 0); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 1894 | } |
| 1895 | |
| 1896 | /// getFunctionType - Return a normal function type with a typed argument |
| 1897 | /// list. isVariadic indicates whether the argument list includes '...'. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1898 | QualType |
| 1899 | ASTContext::getFunctionType(QualType ResultTy, |
| 1900 | const QualType *ArgArray, unsigned NumArgs, |
| 1901 | const FunctionProtoType::ExtProtoInfo &EPI) const { |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 1902 | // Unique functions, to guarantee there is only one function of a particular |
| 1903 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 1904 | llvm::FoldingSetNodeID ID; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1905 | FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, EPI); |
Chris Lattner | fd4de79 | 2007-01-27 01:15:32 +0000 | [diff] [blame] | 1906 | |
| 1907 | void *InsertPos = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1908 | if (FunctionProtoType *FTP = |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1909 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1910 | return QualType(FTP, 0); |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1911 | |
| 1912 | // Determine whether the type being created is already canonical or not. |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1913 | bool isCanonical = !EPI.HasExceptionSpec && ResultTy.isCanonical(); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 1914 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1915 | if (!ArgArray[i].isCanonicalAsParam()) |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 1916 | isCanonical = false; |
| 1917 | |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1918 | const CallingConv CallConv = EPI.ExtInfo.getCC(); |
| 1919 | |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 1920 | // If this type isn't canonical, get the canonical version of it. |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1921 | // The exception spec is not part of the canonical type. |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1922 | QualType Canonical; |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1923 | if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) { |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 1924 | llvm::SmallVector<QualType, 16> CanonicalArgs; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 1925 | CanonicalArgs.reserve(NumArgs); |
| 1926 | for (unsigned i = 0; i != NumArgs; ++i) |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1927 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1928 | |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1929 | FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI; |
| 1930 | if (CanonicalEPI.HasExceptionSpec) { |
| 1931 | CanonicalEPI.HasExceptionSpec = false; |
| 1932 | CanonicalEPI.HasAnyExceptionSpec = false; |
| 1933 | CanonicalEPI.NumExceptions = 0; |
| 1934 | } |
| 1935 | CanonicalEPI.ExtInfo |
| 1936 | = CanonicalEPI.ExtInfo.withCallingConv(getCanonicalCallConv(CallConv)); |
| 1937 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1938 | Canonical = getFunctionType(getCanonicalType(ResultTy), |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1939 | CanonicalArgs.data(), NumArgs, |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1940 | CanonicalEPI); |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1941 | |
Chris Lattner | fd4de79 | 2007-01-27 01:15:32 +0000 | [diff] [blame] | 1942 | // Get the new insert position for the node we care about. |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1943 | FunctionProtoType *NewIP = |
| 1944 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1945 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 1946 | } |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1947 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1948 | // FunctionProtoType objects are allocated with extra bytes after them |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1949 | // for two variable size arrays (for parameter and exception types) at the |
| 1950 | // end of them. |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1951 | size_t Size = sizeof(FunctionProtoType) + |
| 1952 | NumArgs * sizeof(QualType) + |
| 1953 | EPI.NumExceptions * sizeof(QualType); |
| 1954 | FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment); |
| 1955 | new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, Canonical, EPI); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 1956 | Types.push_back(FTP); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1957 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1958 | return QualType(FTP, 0); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 1959 | } |
Chris Lattner | ef51c20 | 2006-11-10 07:17:23 +0000 | [diff] [blame] | 1960 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1961 | #ifndef NDEBUG |
| 1962 | static bool NeedsInjectedClassNameType(const RecordDecl *D) { |
| 1963 | if (!isa<CXXRecordDecl>(D)) return false; |
| 1964 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 1965 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) |
| 1966 | return true; |
| 1967 | if (RD->getDescribedClassTemplate() && |
| 1968 | !isa<ClassTemplateSpecializationDecl>(RD)) |
| 1969 | return true; |
| 1970 | return false; |
| 1971 | } |
| 1972 | #endif |
| 1973 | |
| 1974 | /// getInjectedClassNameType - Return the unique reference to the |
| 1975 | /// injected class name type for the specified templated declaration. |
| 1976 | QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1977 | QualType TST) const { |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1978 | assert(NeedsInjectedClassNameType(Decl)); |
| 1979 | if (Decl->TypeForDecl) { |
| 1980 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
Argyrios Kyrtzidis | 2c2167a | 2010-07-02 11:55:32 +0000 | [diff] [blame] | 1981 | } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDeclaration()) { |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1982 | assert(PrevDecl->TypeForDecl && "previous declaration has no type"); |
| 1983 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 1984 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
| 1985 | } else { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1986 | Type *newType = |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1987 | new (*this, TypeAlignment) InjectedClassNameType(Decl, TST); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1988 | Decl->TypeForDecl = newType; |
| 1989 | Types.push_back(newType); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1990 | } |
| 1991 | return QualType(Decl->TypeForDecl, 0); |
| 1992 | } |
| 1993 | |
Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1994 | /// getTypeDeclType - Return the unique reference to the type for the |
| 1995 | /// specified type declaration. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1996 | QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const { |
Argyrios Kyrtzidis | 89656d2 | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 1997 | assert(Decl && "Passed null for Decl param"); |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1998 | assert(!Decl->TypeForDecl && "TypeForDecl present in slow case"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1999 | |
John McCall | 81e3850 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2000 | if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl)) |
Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2001 | return getTypedefType(Typedef); |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2002 | |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2003 | assert(!isa<TemplateTypeParmDecl>(Decl) && |
| 2004 | "Template type parameter types are always available."); |
| 2005 | |
John McCall | 81e3850 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2006 | if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2007 | assert(!Record->getPreviousDeclaration() && |
| 2008 | "struct/union has previous declaration"); |
| 2009 | assert(!NeedsInjectedClassNameType(Record)); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2010 | return getRecordType(Record); |
John McCall | 81e3850 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2011 | } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2012 | assert(!Enum->getPreviousDeclaration() && |
| 2013 | "enum has previous declaration"); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2014 | return getEnumType(Enum); |
John McCall | 81e3850 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2015 | } else if (const UnresolvedUsingTypenameDecl *Using = |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2016 | dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2017 | Type *newType = new (*this, TypeAlignment) UnresolvedUsingType(Using); |
| 2018 | Decl->TypeForDecl = newType; |
| 2019 | Types.push_back(newType); |
Mike Stump | e9c6ffc | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 2020 | } else |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2021 | llvm_unreachable("TypeDecl without a type?"); |
Argyrios Kyrtzidis | faf0876 | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 2022 | |
Argyrios Kyrtzidis | faf0876 | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 2023 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2024 | } |
| 2025 | |
Chris Lattner | 32d920b | 2007-01-26 02:01:53 +0000 | [diff] [blame] | 2026 | /// getTypedefType - Return the unique reference to the type for the |
Chris Lattner | d0342e5 | 2006-11-20 04:02:15 +0000 | [diff] [blame] | 2027 | /// specified typename decl. |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2028 | QualType |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2029 | ASTContext::getTypedefType(const TypedefDecl *Decl, QualType Canonical) const { |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2030 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2031 | |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2032 | if (Canonical.isNull()) |
| 2033 | Canonical = getCanonicalType(Decl->getUnderlyingType()); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2034 | TypedefType *newType = new(*this, TypeAlignment) |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2035 | TypedefType(Type::Typedef, Decl, Canonical); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2036 | Decl->TypeForDecl = newType; |
| 2037 | Types.push_back(newType); |
| 2038 | return QualType(newType, 0); |
Chris Lattner | d0342e5 | 2006-11-20 04:02:15 +0000 | [diff] [blame] | 2039 | } |
| 2040 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2041 | QualType ASTContext::getRecordType(const RecordDecl *Decl) const { |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2042 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 2043 | |
| 2044 | if (const RecordDecl *PrevDecl = Decl->getPreviousDeclaration()) |
| 2045 | if (PrevDecl->TypeForDecl) |
| 2046 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 2047 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2048 | RecordType *newType = new (*this, TypeAlignment) RecordType(Decl); |
| 2049 | Decl->TypeForDecl = newType; |
| 2050 | Types.push_back(newType); |
| 2051 | return QualType(newType, 0); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2052 | } |
| 2053 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2054 | QualType ASTContext::getEnumType(const EnumDecl *Decl) const { |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2055 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 2056 | |
| 2057 | if (const EnumDecl *PrevDecl = Decl->getPreviousDeclaration()) |
| 2058 | if (PrevDecl->TypeForDecl) |
| 2059 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 2060 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2061 | EnumType *newType = new (*this, TypeAlignment) EnumType(Decl); |
| 2062 | Decl->TypeForDecl = newType; |
| 2063 | Types.push_back(newType); |
| 2064 | return QualType(newType, 0); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 2067 | QualType ASTContext::getAttributedType(AttributedType::Kind attrKind, |
| 2068 | QualType modifiedType, |
| 2069 | QualType equivalentType) { |
| 2070 | llvm::FoldingSetNodeID id; |
| 2071 | AttributedType::Profile(id, attrKind, modifiedType, equivalentType); |
| 2072 | |
| 2073 | void *insertPos = 0; |
| 2074 | AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos); |
| 2075 | if (type) return QualType(type, 0); |
| 2076 | |
| 2077 | QualType canon = getCanonicalType(equivalentType); |
| 2078 | type = new (*this, TypeAlignment) |
| 2079 | AttributedType(canon, attrKind, modifiedType, equivalentType); |
| 2080 | |
| 2081 | Types.push_back(type); |
| 2082 | AttributedTypes.InsertNode(type, insertPos); |
| 2083 | |
| 2084 | return QualType(type, 0); |
| 2085 | } |
| 2086 | |
| 2087 | |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2088 | /// \brief Retrieve a substitution-result type. |
| 2089 | QualType |
| 2090 | ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2091 | QualType Replacement) const { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2092 | assert(Replacement.isCanonical() |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2093 | && "replacement types must always be canonical"); |
| 2094 | |
| 2095 | llvm::FoldingSetNodeID ID; |
| 2096 | SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); |
| 2097 | void *InsertPos = 0; |
| 2098 | SubstTemplateTypeParmType *SubstParm |
| 2099 | = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2100 | |
| 2101 | if (!SubstParm) { |
| 2102 | SubstParm = new (*this, TypeAlignment) |
| 2103 | SubstTemplateTypeParmType(Parm, Replacement); |
| 2104 | Types.push_back(SubstParm); |
| 2105 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 2106 | } |
| 2107 | |
| 2108 | return QualType(SubstParm, 0); |
| 2109 | } |
| 2110 | |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 2111 | /// \brief Retrieve a |
| 2112 | QualType ASTContext::getSubstTemplateTypeParmPackType( |
| 2113 | const TemplateTypeParmType *Parm, |
| 2114 | const TemplateArgument &ArgPack) { |
| 2115 | #ifndef NDEBUG |
| 2116 | for (TemplateArgument::pack_iterator P = ArgPack.pack_begin(), |
| 2117 | PEnd = ArgPack.pack_end(); |
| 2118 | P != PEnd; ++P) { |
| 2119 | assert(P->getKind() == TemplateArgument::Type &&"Pack contains a non-type"); |
| 2120 | assert(P->getAsType().isCanonical() && "Pack contains non-canonical type"); |
| 2121 | } |
| 2122 | #endif |
| 2123 | |
| 2124 | llvm::FoldingSetNodeID ID; |
| 2125 | SubstTemplateTypeParmPackType::Profile(ID, Parm, ArgPack); |
| 2126 | void *InsertPos = 0; |
| 2127 | if (SubstTemplateTypeParmPackType *SubstParm |
| 2128 | = SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2129 | return QualType(SubstParm, 0); |
| 2130 | |
| 2131 | QualType Canon; |
| 2132 | if (!Parm->isCanonicalUnqualified()) { |
| 2133 | Canon = getCanonicalType(QualType(Parm, 0)); |
| 2134 | Canon = getSubstTemplateTypeParmPackType(cast<TemplateTypeParmType>(Canon), |
| 2135 | ArgPack); |
| 2136 | SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2137 | } |
| 2138 | |
| 2139 | SubstTemplateTypeParmPackType *SubstParm |
| 2140 | = new (*this, TypeAlignment) SubstTemplateTypeParmPackType(Parm, Canon, |
| 2141 | ArgPack); |
| 2142 | Types.push_back(SubstParm); |
| 2143 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 2144 | return QualType(SubstParm, 0); |
| 2145 | } |
| 2146 | |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2147 | /// \brief Retrieve the template type parameter type for a template |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2148 | /// parameter or parameter pack with the given depth, index, and (optionally) |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2149 | /// name. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2150 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2151 | bool ParameterPack, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2152 | IdentifierInfo *Name) const { |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2153 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 2ebcae1 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 2154 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name); |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2155 | void *InsertPos = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2156 | TemplateTypeParmType *TypeParm |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2157 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2158 | |
| 2159 | if (TypeParm) |
| 2160 | return QualType(TypeParm, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2161 | |
Douglas Gregor | 2ebcae1 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 2162 | if (Name) { |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2163 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | 2ebcae1 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 2164 | TypeParm = new (*this, TypeAlignment) |
| 2165 | TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2166 | |
| 2167 | TemplateTypeParmType *TypeCheck |
| 2168 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2169 | assert(!TypeCheck && "Template type parameter canonical type broken"); |
| 2170 | (void)TypeCheck; |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2171 | } else |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2172 | TypeParm = new (*this, TypeAlignment) |
| 2173 | TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2174 | |
| 2175 | Types.push_back(TypeParm); |
| 2176 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 2177 | |
| 2178 | return QualType(TypeParm, 0); |
| 2179 | } |
| 2180 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2181 | TypeSourceInfo * |
| 2182 | ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name, |
| 2183 | SourceLocation NameLoc, |
| 2184 | const TemplateArgumentListInfo &Args, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2185 | QualType CanonType) const { |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame^] | 2186 | assert(!Name.getAsDependentTemplateName() && |
| 2187 | "No dependent template names here!"); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2188 | QualType TST = getTemplateSpecializationType(Name, Args, CanonType); |
| 2189 | |
| 2190 | TypeSourceInfo *DI = CreateTypeSourceInfo(TST); |
| 2191 | TemplateSpecializationTypeLoc TL |
| 2192 | = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc()); |
| 2193 | TL.setTemplateNameLoc(NameLoc); |
| 2194 | TL.setLAngleLoc(Args.getLAngleLoc()); |
| 2195 | TL.setRAngleLoc(Args.getRAngleLoc()); |
| 2196 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 2197 | TL.setArgLocInfo(i, Args[i].getLocInfo()); |
| 2198 | return DI; |
| 2199 | } |
| 2200 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2201 | QualType |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2202 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2203 | const TemplateArgumentListInfo &Args, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2204 | QualType Canon) const { |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame^] | 2205 | assert(!Template.getAsDependentTemplateName() && |
| 2206 | "No dependent template names here!"); |
| 2207 | |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2208 | unsigned NumArgs = Args.size(); |
| 2209 | |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2210 | llvm::SmallVector<TemplateArgument, 4> ArgVec; |
| 2211 | ArgVec.reserve(NumArgs); |
| 2212 | for (unsigned i = 0; i != NumArgs; ++i) |
| 2213 | ArgVec.push_back(Args[i].getArgument()); |
| 2214 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2215 | return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, |
John McCall | 30576cd | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 2216 | Canon); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2217 | } |
| 2218 | |
| 2219 | QualType |
| 2220 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2221 | const TemplateArgument *Args, |
| 2222 | unsigned NumArgs, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2223 | QualType Canon) const { |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame^] | 2224 | assert(!Template.getAsDependentTemplateName() && |
| 2225 | "No dependent template names here!"); |
| 2226 | |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 2227 | if (!Canon.isNull()) |
| 2228 | Canon = getCanonicalType(Canon); |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2229 | else |
| 2230 | Canon = getCanonicalTemplateSpecializationType(Template, Args, NumArgs); |
Douglas Gregor | d56a91e | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 2231 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2232 | // Allocate the (non-canonical) template specialization type, but don't |
| 2233 | // try to unique it: these types typically have location information that |
| 2234 | // we don't unique and don't want to lose. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2235 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2236 | sizeof(TemplateArgument) * NumArgs), |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2237 | TypeAlignment); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2238 | TemplateSpecializationType *Spec |
John McCall | 773cc98 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 2239 | = new (Mem) TemplateSpecializationType(Template, |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2240 | Args, NumArgs, |
Douglas Gregor | 0004417 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 2241 | Canon); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2242 | |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2243 | Types.push_back(Spec); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2244 | return QualType(Spec, 0); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2245 | } |
| 2246 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2247 | QualType |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2248 | ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template, |
| 2249 | const TemplateArgument *Args, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2250 | unsigned NumArgs) const { |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame^] | 2251 | assert(!Template.getAsDependentTemplateName() && |
| 2252 | "No dependent template names here!"); |
| 2253 | |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2254 | // Build the canonical template specialization type. |
| 2255 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); |
| 2256 | llvm::SmallVector<TemplateArgument, 4> CanonArgs; |
| 2257 | CanonArgs.reserve(NumArgs); |
| 2258 | for (unsigned I = 0; I != NumArgs; ++I) |
| 2259 | CanonArgs.push_back(getCanonicalTemplateArgument(Args[I])); |
| 2260 | |
| 2261 | // Determine whether this canonical template specialization type already |
| 2262 | // exists. |
| 2263 | llvm::FoldingSetNodeID ID; |
| 2264 | TemplateSpecializationType::Profile(ID, CanonTemplate, |
| 2265 | CanonArgs.data(), NumArgs, *this); |
| 2266 | |
| 2267 | void *InsertPos = 0; |
| 2268 | TemplateSpecializationType *Spec |
| 2269 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2270 | |
| 2271 | if (!Spec) { |
| 2272 | // Allocate a new canonical template specialization type. |
| 2273 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
| 2274 | sizeof(TemplateArgument) * NumArgs), |
| 2275 | TypeAlignment); |
| 2276 | Spec = new (Mem) TemplateSpecializationType(CanonTemplate, |
| 2277 | CanonArgs.data(), NumArgs, |
| 2278 | QualType()); |
| 2279 | Types.push_back(Spec); |
| 2280 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
| 2281 | } |
| 2282 | |
| 2283 | assert(Spec->isDependentType() && |
| 2284 | "Non-dependent template-id type must have a canonical type"); |
| 2285 | return QualType(Spec, 0); |
| 2286 | } |
| 2287 | |
| 2288 | QualType |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2289 | ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword, |
| 2290 | NestedNameSpecifier *NNS, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2291 | QualType NamedType) const { |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2292 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2293 | ElaboratedType::Profile(ID, Keyword, NNS, NamedType); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2294 | |
| 2295 | void *InsertPos = 0; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2296 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2297 | if (T) |
| 2298 | return QualType(T, 0); |
| 2299 | |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2300 | QualType Canon = NamedType; |
| 2301 | if (!Canon.isCanonical()) { |
| 2302 | Canon = getCanonicalType(NamedType); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2303 | ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2304 | assert(!CheckT && "Elaborated canonical type broken"); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2305 | (void)CheckT; |
| 2306 | } |
| 2307 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2308 | T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2309 | Types.push_back(T); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2310 | ElaboratedTypes.InsertNode(T, InsertPos); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2311 | return QualType(T, 0); |
| 2312 | } |
| 2313 | |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 2314 | QualType |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2315 | ASTContext::getParenType(QualType InnerType) const { |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 2316 | llvm::FoldingSetNodeID ID; |
| 2317 | ParenType::Profile(ID, InnerType); |
| 2318 | |
| 2319 | void *InsertPos = 0; |
| 2320 | ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2321 | if (T) |
| 2322 | return QualType(T, 0); |
| 2323 | |
| 2324 | QualType Canon = InnerType; |
| 2325 | if (!Canon.isCanonical()) { |
| 2326 | Canon = getCanonicalType(InnerType); |
| 2327 | ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2328 | assert(!CheckT && "Paren canonical type broken"); |
| 2329 | (void)CheckT; |
| 2330 | } |
| 2331 | |
| 2332 | T = new (*this) ParenType(InnerType, Canon); |
| 2333 | Types.push_back(T); |
| 2334 | ParenTypes.InsertNode(T, InsertPos); |
| 2335 | return QualType(T, 0); |
| 2336 | } |
| 2337 | |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2338 | QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, |
| 2339 | NestedNameSpecifier *NNS, |
| 2340 | const IdentifierInfo *Name, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2341 | QualType Canon) const { |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2342 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 2343 | |
| 2344 | if (Canon.isNull()) { |
| 2345 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2346 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 2347 | if (Keyword == ETK_None) |
| 2348 | CanonKeyword = ETK_Typename; |
| 2349 | |
| 2350 | if (CanonNNS != NNS || CanonKeyword != Keyword) |
| 2351 | Canon = getDependentNameType(CanonKeyword, CanonNNS, Name); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2352 | } |
| 2353 | |
| 2354 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2355 | DependentNameType::Profile(ID, Keyword, NNS, Name); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2356 | |
| 2357 | void *InsertPos = 0; |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2358 | DependentNameType *T |
| 2359 | = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2360 | if (T) |
| 2361 | return QualType(T, 0); |
| 2362 | |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2363 | T = new (*this) DependentNameType(Keyword, NNS, Name, Canon); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2364 | Types.push_back(T); |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2365 | DependentNameTypes.InsertNode(T, InsertPos); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2366 | return QualType(T, 0); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2367 | } |
| 2368 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2369 | QualType |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2370 | ASTContext::getDependentTemplateSpecializationType( |
| 2371 | ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2372 | NestedNameSpecifier *NNS, |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2373 | const IdentifierInfo *Name, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2374 | const TemplateArgumentListInfo &Args) const { |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2375 | // TODO: avoid this copy |
| 2376 | llvm::SmallVector<TemplateArgument, 16> ArgCopy; |
| 2377 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
| 2378 | ArgCopy.push_back(Args[I].getArgument()); |
| 2379 | return getDependentTemplateSpecializationType(Keyword, NNS, Name, |
| 2380 | ArgCopy.size(), |
| 2381 | ArgCopy.data()); |
| 2382 | } |
| 2383 | |
| 2384 | QualType |
| 2385 | ASTContext::getDependentTemplateSpecializationType( |
| 2386 | ElaboratedTypeKeyword Keyword, |
| 2387 | NestedNameSpecifier *NNS, |
| 2388 | const IdentifierInfo *Name, |
| 2389 | unsigned NumArgs, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2390 | const TemplateArgument *Args) const { |
Douglas Gregor | 6e06801 | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 2391 | assert((!NNS || NNS->isDependent()) && |
| 2392 | "nested-name-specifier must be dependent"); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2393 | |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2394 | llvm::FoldingSetNodeID ID; |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2395 | DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS, |
| 2396 | Name, NumArgs, Args); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2397 | |
| 2398 | void *InsertPos = 0; |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2399 | DependentTemplateSpecializationType *T |
| 2400 | = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2401 | if (T) |
| 2402 | return QualType(T, 0); |
| 2403 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2404 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2405 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2406 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 2407 | if (Keyword == ETK_None) CanonKeyword = ETK_Typename; |
| 2408 | |
| 2409 | bool AnyNonCanonArgs = false; |
| 2410 | llvm::SmallVector<TemplateArgument, 16> CanonArgs(NumArgs); |
| 2411 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 2412 | CanonArgs[I] = getCanonicalTemplateArgument(Args[I]); |
| 2413 | if (!CanonArgs[I].structurallyEquals(Args[I])) |
| 2414 | AnyNonCanonArgs = true; |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2415 | } |
| 2416 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2417 | QualType Canon; |
| 2418 | if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) { |
| 2419 | Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS, |
| 2420 | Name, NumArgs, |
| 2421 | CanonArgs.data()); |
| 2422 | |
| 2423 | // Find the insert position again. |
| 2424 | DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2425 | } |
| 2426 | |
| 2427 | void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) + |
| 2428 | sizeof(TemplateArgument) * NumArgs), |
| 2429 | TypeAlignment); |
John McCall | 773cc98 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 2430 | T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS, |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2431 | Name, NumArgs, Args, Canon); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2432 | Types.push_back(T); |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2433 | DependentTemplateSpecializationTypes.InsertNode(T, InsertPos); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2434 | return QualType(T, 0); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2435 | } |
| 2436 | |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2437 | QualType ASTContext::getPackExpansionType(QualType Pattern, |
| 2438 | llvm::Optional<unsigned> NumExpansions) { |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2439 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2440 | PackExpansionType::Profile(ID, Pattern, NumExpansions); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2441 | |
| 2442 | assert(Pattern->containsUnexpandedParameterPack() && |
| 2443 | "Pack expansions must expand one or more parameter packs"); |
| 2444 | void *InsertPos = 0; |
| 2445 | PackExpansionType *T |
| 2446 | = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2447 | if (T) |
| 2448 | return QualType(T, 0); |
| 2449 | |
| 2450 | QualType Canon; |
| 2451 | if (!Pattern.isCanonical()) { |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2452 | Canon = getPackExpansionType(getCanonicalType(Pattern), NumExpansions); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2453 | |
| 2454 | // Find the insert position again. |
| 2455 | PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2456 | } |
| 2457 | |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2458 | T = new (*this) PackExpansionType(Pattern, Canon, NumExpansions); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2459 | Types.push_back(T); |
| 2460 | PackExpansionTypes.InsertNode(T, InsertPos); |
| 2461 | return QualType(T, 0); |
| 2462 | } |
| 2463 | |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2464 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 2465 | /// alphabetically. |
| 2466 | static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, |
| 2467 | const ObjCProtocolDecl *RHS) { |
Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2468 | return LHS->getDeclName() < RHS->getDeclName(); |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2469 | } |
| 2470 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2471 | static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols, |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2472 | unsigned NumProtocols) { |
| 2473 | if (NumProtocols == 0) return true; |
| 2474 | |
| 2475 | for (unsigned i = 1; i != NumProtocols; ++i) |
| 2476 | if (!CmpProtocolNames(Protocols[i-1], Protocols[i])) |
| 2477 | return false; |
| 2478 | return true; |
| 2479 | } |
| 2480 | |
| 2481 | static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols, |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2482 | unsigned &NumProtocols) { |
| 2483 | ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2484 | |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2485 | // Sort protocols, keyed by name. |
| 2486 | std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); |
| 2487 | |
| 2488 | // Remove duplicates. |
| 2489 | ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); |
| 2490 | NumProtocols = ProtocolsEnd-Protocols; |
| 2491 | } |
| 2492 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2493 | QualType ASTContext::getObjCObjectType(QualType BaseType, |
| 2494 | ObjCProtocolDecl * const *Protocols, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2495 | unsigned NumProtocols) const { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2496 | // If the base type is an interface and there aren't any protocols |
| 2497 | // to add, then the interface type will do just fine. |
| 2498 | if (!NumProtocols && isa<ObjCInterfaceType>(BaseType)) |
| 2499 | return BaseType; |
| 2500 | |
| 2501 | // Look in the folding set for an existing type. |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2502 | llvm::FoldingSetNodeID ID; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2503 | ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols); |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2504 | void *InsertPos = 0; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2505 | if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2506 | return QualType(QT, 0); |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2507 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2508 | // Build the canonical type, which has the canonical base type and |
| 2509 | // a sorted-and-uniqued list of protocols. |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2510 | QualType Canonical; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2511 | bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols); |
| 2512 | if (!ProtocolsSorted || !BaseType.isCanonical()) { |
| 2513 | if (!ProtocolsSorted) { |
Benjamin Kramer | 2e3197e | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 2514 | llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols, |
| 2515 | Protocols + NumProtocols); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2516 | unsigned UniqueCount = NumProtocols; |
| 2517 | |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2518 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2519 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 2520 | &Sorted[0], UniqueCount); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2521 | } else { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2522 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 2523 | Protocols, NumProtocols); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2524 | } |
| 2525 | |
| 2526 | // Regenerate InsertPos. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2527 | ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2528 | } |
| 2529 | |
| 2530 | unsigned Size = sizeof(ObjCObjectTypeImpl); |
| 2531 | Size += NumProtocols * sizeof(ObjCProtocolDecl *); |
| 2532 | void *Mem = Allocate(Size, TypeAlignment); |
| 2533 | ObjCObjectTypeImpl *T = |
| 2534 | new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols); |
| 2535 | |
| 2536 | Types.push_back(T); |
| 2537 | ObjCObjectTypes.InsertNode(T, InsertPos); |
| 2538 | return QualType(T, 0); |
| 2539 | } |
| 2540 | |
| 2541 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 2542 | /// the given object type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2543 | QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2544 | llvm::FoldingSetNodeID ID; |
| 2545 | ObjCObjectPointerType::Profile(ID, ObjectT); |
| 2546 | |
| 2547 | void *InsertPos = 0; |
| 2548 | if (ObjCObjectPointerType *QT = |
| 2549 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2550 | return QualType(QT, 0); |
| 2551 | |
| 2552 | // Find the canonical object type. |
| 2553 | QualType Canonical; |
| 2554 | if (!ObjectT.isCanonical()) { |
| 2555 | Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT)); |
| 2556 | |
| 2557 | // Regenerate InsertPos. |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2558 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2559 | } |
| 2560 | |
Douglas Gregor | f85bee6 | 2010-02-08 22:59:26 +0000 | [diff] [blame] | 2561 | // No match. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2562 | void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment); |
| 2563 | ObjCObjectPointerType *QType = |
| 2564 | new (Mem) ObjCObjectPointerType(Canonical, ObjectT); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2565 | |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2566 | Types.push_back(QType); |
| 2567 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2568 | return QualType(QType, 0); |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2569 | } |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2570 | |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2571 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 2572 | /// specified ObjC interface decl. The list of protocols is optional. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2573 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) const { |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2574 | if (Decl->TypeForDecl) |
| 2575 | return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2576 | |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2577 | // FIXME: redeclarations? |
| 2578 | void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment); |
| 2579 | ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl); |
| 2580 | Decl->TypeForDecl = T; |
| 2581 | Types.push_back(T); |
| 2582 | return QualType(T, 0); |
Fariborz Jahanian | 70e8f10 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2583 | } |
| 2584 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2585 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 2586 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2587 | /// multiple declarations that refer to "typeof(x)" all contain different |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2588 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2589 | /// on canonical type's (which are always unique). |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2590 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) const { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2591 | TypeOfExprType *toe; |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2592 | if (tofExpr->isTypeDependent()) { |
| 2593 | llvm::FoldingSetNodeID ID; |
| 2594 | DependentTypeOfExprType::Profile(ID, *this, tofExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2595 | |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2596 | void *InsertPos = 0; |
| 2597 | DependentTypeOfExprType *Canon |
| 2598 | = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2599 | if (Canon) { |
| 2600 | // We already have a "canonical" version of an identical, dependent |
| 2601 | // typeof(expr) type. Use that as our canonical type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2602 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2603 | QualType((TypeOfExprType*)Canon, 0)); |
| 2604 | } |
| 2605 | else { |
| 2606 | // Build a new, canonical typeof(expr) type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2607 | Canon |
| 2608 | = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2609 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); |
| 2610 | toe = Canon; |
| 2611 | } |
| 2612 | } else { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2613 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2614 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2615 | } |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2616 | Types.push_back(toe); |
| 2617 | return QualType(toe, 0); |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2618 | } |
| 2619 | |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2620 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 2621 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 2622 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2623 | /// an issue. This doesn't effect the type checker, since it operates |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2624 | /// on canonical type's (which are always unique). |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2625 | QualType ASTContext::getTypeOfType(QualType tofType) const { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2626 | QualType Canonical = getCanonicalType(tofType); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2627 | TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2628 | Types.push_back(tot); |
| 2629 | return QualType(tot, 0); |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2630 | } |
| 2631 | |
Anders Carlsson | ad6bd35 | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2632 | /// getDecltypeForExpr - Given an expr, will return the decltype for that |
| 2633 | /// expression, according to the rules in C++0x [dcl.type.simple]p4 |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2634 | static QualType getDecltypeForExpr(const Expr *e, const ASTContext &Context) { |
Anders Carlsson | 7d20957 | 2009-06-25 15:00:34 +0000 | [diff] [blame] | 2635 | if (e->isTypeDependent()) |
| 2636 | return Context.DependentTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2637 | |
Anders Carlsson | ad6bd35 | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2638 | // If e is an id expression or a class member access, decltype(e) is defined |
| 2639 | // as the type of the entity named by e. |
| 2640 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) { |
| 2641 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) |
| 2642 | return VD->getType(); |
| 2643 | } |
| 2644 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) { |
| 2645 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) |
| 2646 | return FD->getType(); |
| 2647 | } |
| 2648 | // If e is a function call or an invocation of an overloaded operator, |
| 2649 | // (parentheses around e are ignored), decltype(e) is defined as the |
| 2650 | // return type of that function. |
| 2651 | if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens())) |
| 2652 | return CE->getCallReturnType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2653 | |
Anders Carlsson | ad6bd35 | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2654 | QualType T = e->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2655 | |
| 2656 | // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is |
Anders Carlsson | ad6bd35 | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2657 | // defined as T&, otherwise decltype(e) is defined as T. |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 2658 | if (e->isLValue()) |
Anders Carlsson | ad6bd35 | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2659 | T = Context.getLValueReferenceType(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2660 | |
Anders Carlsson | ad6bd35 | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2661 | return T; |
| 2662 | } |
| 2663 | |
Anders Carlsson | 81df7b8 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2664 | /// getDecltypeType - Unlike many "get<Type>" functions, we don't unique |
| 2665 | /// DecltypeType AST's. The only motivation to unique these nodes would be |
| 2666 | /// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2667 | /// an issue. This doesn't effect the type checker, since it operates |
Anders Carlsson | 81df7b8 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2668 | /// on canonical type's (which are always unique). |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2669 | QualType ASTContext::getDecltypeType(Expr *e) const { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2670 | DecltypeType *dt; |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2671 | if (e->isTypeDependent()) { |
| 2672 | llvm::FoldingSetNodeID ID; |
| 2673 | DependentDecltypeType::Profile(ID, *this, e); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2674 | |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2675 | void *InsertPos = 0; |
| 2676 | DependentDecltypeType *Canon |
| 2677 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2678 | if (Canon) { |
| 2679 | // We already have a "canonical" version of an equivalent, dependent |
| 2680 | // decltype type. Use that as our canonical type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2681 | dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy, |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2682 | QualType((DecltypeType*)Canon, 0)); |
| 2683 | } |
| 2684 | else { |
| 2685 | // Build a new, canonical typeof(expr) type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2686 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2687 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); |
| 2688 | dt = Canon; |
| 2689 | } |
| 2690 | } else { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2691 | QualType T = getDecltypeForExpr(e, *this); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2692 | dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T)); |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2693 | } |
Anders Carlsson | 81df7b8 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2694 | Types.push_back(dt); |
| 2695 | return QualType(dt, 0); |
| 2696 | } |
| 2697 | |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 2698 | /// getAutoType - We only unique auto types after they've been deduced. |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2699 | QualType ASTContext::getAutoType(QualType DeducedType) const { |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 2700 | void *InsertPos = 0; |
| 2701 | if (!DeducedType.isNull()) { |
| 2702 | // Look in the folding set for an existing type. |
| 2703 | llvm::FoldingSetNodeID ID; |
| 2704 | AutoType::Profile(ID, DeducedType); |
| 2705 | if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2706 | return QualType(AT, 0); |
| 2707 | } |
| 2708 | |
| 2709 | AutoType *AT = new (*this, TypeAlignment) AutoType(DeducedType); |
| 2710 | Types.push_back(AT); |
| 2711 | if (InsertPos) |
| 2712 | AutoTypes.InsertNode(AT, InsertPos); |
| 2713 | return QualType(AT, 0); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2714 | } |
| 2715 | |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 2716 | /// getTagDeclType - Return the unique reference to the type for the |
| 2717 | /// specified TagDecl (struct/union/class/enum) decl. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2718 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) const { |
Ted Kremenek | 2b0ce11 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 2719 | assert (Decl); |
Mike Stump | b93185d | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 2720 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 2721 | // away const? mutable? |
| 2722 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 2723 | } |
| 2724 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2725 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 2726 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 2727 | /// needs to agree with the definition in <stddef.h>. |
Anders Carlsson | 22f443f | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 2728 | CanQualType ASTContext::getSizeType() const { |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2729 | return getFromTargetType(Target.getSizeType()); |
Steve Naroff | 92e30f8 | 2007-04-02 22:35:25 +0000 | [diff] [blame] | 2730 | } |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 2731 | |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 2732 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 2733 | /// Used when in C++, as a GCC extension. |
| 2734 | QualType ASTContext::getSignedWCharType() const { |
| 2735 | // FIXME: derive from "Target" ? |
| 2736 | return WCharTy; |
| 2737 | } |
| 2738 | |
| 2739 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 2740 | /// Used when in C++, as a GCC extension. |
| 2741 | QualType ASTContext::getUnsignedWCharType() const { |
| 2742 | // FIXME: derive from "Target" ? |
| 2743 | return UnsignedIntTy; |
| 2744 | } |
| 2745 | |
Chris Lattner | d2b88ab | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 2746 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) |
| 2747 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 2748 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2749 | return getFromTargetType(Target.getPtrDiffType(0)); |
Chris Lattner | d2b88ab | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 2750 | } |
| 2751 | |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2752 | //===----------------------------------------------------------------------===// |
| 2753 | // Type Operators |
| 2754 | //===----------------------------------------------------------------------===// |
| 2755 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2756 | CanQualType ASTContext::getCanonicalParamType(QualType T) const { |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2757 | // Push qualifiers into arrays, and then discard any remaining |
| 2758 | // qualifiers. |
| 2759 | T = getCanonicalType(T); |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2760 | T = getVariableArrayDecayedType(T); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2761 | const Type *Ty = T.getTypePtr(); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2762 | QualType Result; |
| 2763 | if (isa<ArrayType>(Ty)) { |
| 2764 | Result = getArrayDecayedType(QualType(Ty,0)); |
| 2765 | } else if (isa<FunctionType>(Ty)) { |
| 2766 | Result = getPointerType(QualType(Ty, 0)); |
| 2767 | } else { |
| 2768 | Result = QualType(Ty, 0); |
| 2769 | } |
| 2770 | |
| 2771 | return CanQualType::CreateUnsafe(Result); |
| 2772 | } |
| 2773 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2774 | |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 2775 | QualType ASTContext::getUnqualifiedArrayType(QualType type, |
| 2776 | Qualifiers &quals) { |
| 2777 | SplitQualType splitType = type.getSplitUnqualifiedType(); |
| 2778 | |
| 2779 | // FIXME: getSplitUnqualifiedType() actually walks all the way to |
| 2780 | // the unqualified desugared type and then drops it on the floor. |
| 2781 | // We then have to strip that sugar back off with |
| 2782 | // getUnqualifiedDesugaredType(), which is silly. |
| 2783 | const ArrayType *AT = |
| 2784 | dyn_cast<ArrayType>(splitType.first->getUnqualifiedDesugaredType()); |
| 2785 | |
| 2786 | // If we don't have an array, just use the results in splitType. |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2787 | if (!AT) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 2788 | quals = splitType.second; |
| 2789 | return QualType(splitType.first, 0); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2790 | } |
| 2791 | |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 2792 | // Otherwise, recurse on the array's element type. |
| 2793 | QualType elementType = AT->getElementType(); |
| 2794 | QualType unqualElementType = getUnqualifiedArrayType(elementType, quals); |
| 2795 | |
| 2796 | // If that didn't change the element type, AT has no qualifiers, so we |
| 2797 | // can just use the results in splitType. |
| 2798 | if (elementType == unqualElementType) { |
| 2799 | assert(quals.empty()); // from the recursive call |
| 2800 | quals = splitType.second; |
| 2801 | return QualType(splitType.first, 0); |
| 2802 | } |
| 2803 | |
| 2804 | // Otherwise, add in the qualifiers from the outermost type, then |
| 2805 | // build the type back up. |
| 2806 | quals.addConsistentQualifiers(splitType.second); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2807 | |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2808 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 2809 | return getConstantArrayType(unqualElementType, CAT->getSize(), |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2810 | CAT->getSizeModifier(), 0); |
| 2811 | } |
| 2812 | |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2813 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 2814 | return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2815 | } |
| 2816 | |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2817 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 2818 | return getVariableArrayType(unqualElementType, |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 2819 | VAT->getSizeExpr(), |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2820 | VAT->getSizeModifier(), |
| 2821 | VAT->getIndexTypeCVRQualifiers(), |
| 2822 | VAT->getBracketsRange()); |
| 2823 | } |
| 2824 | |
| 2825 | const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT); |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 2826 | return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(), |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2827 | DSAT->getSizeModifier(), 0, |
| 2828 | SourceRange()); |
| 2829 | } |
| 2830 | |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2831 | /// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that |
| 2832 | /// may be similar (C++ 4.4), replaces T1 and T2 with the type that |
| 2833 | /// they point to and return true. If T1 and T2 aren't pointer types |
| 2834 | /// or pointer-to-member types, or if they are not similar at this |
| 2835 | /// level, returns false and leaves T1 and T2 unchanged. Top-level |
| 2836 | /// qualifiers on T1 and T2 are ignored. This function will typically |
| 2837 | /// be called in a loop that successively "unwraps" pointer and |
| 2838 | /// pointer-to-member types to compare them at each level. |
| 2839 | bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) { |
| 2840 | const PointerType *T1PtrType = T1->getAs<PointerType>(), |
| 2841 | *T2PtrType = T2->getAs<PointerType>(); |
| 2842 | if (T1PtrType && T2PtrType) { |
| 2843 | T1 = T1PtrType->getPointeeType(); |
| 2844 | T2 = T2PtrType->getPointeeType(); |
| 2845 | return true; |
| 2846 | } |
| 2847 | |
| 2848 | const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(), |
| 2849 | *T2MPType = T2->getAs<MemberPointerType>(); |
| 2850 | if (T1MPType && T2MPType && |
| 2851 | hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0), |
| 2852 | QualType(T2MPType->getClass(), 0))) { |
| 2853 | T1 = T1MPType->getPointeeType(); |
| 2854 | T2 = T2MPType->getPointeeType(); |
| 2855 | return true; |
| 2856 | } |
| 2857 | |
| 2858 | if (getLangOptions().ObjC1) { |
| 2859 | const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(), |
| 2860 | *T2OPType = T2->getAs<ObjCObjectPointerType>(); |
| 2861 | if (T1OPType && T2OPType) { |
| 2862 | T1 = T1OPType->getPointeeType(); |
| 2863 | T2 = T2OPType->getPointeeType(); |
| 2864 | return true; |
| 2865 | } |
| 2866 | } |
| 2867 | |
| 2868 | // FIXME: Block pointers, too? |
| 2869 | |
| 2870 | return false; |
| 2871 | } |
| 2872 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2873 | DeclarationNameInfo |
| 2874 | ASTContext::getNameForTemplate(TemplateName Name, |
| 2875 | SourceLocation NameLoc) const { |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2876 | if (TemplateDecl *TD = Name.getAsTemplateDecl()) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2877 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 2878 | return DeclarationNameInfo(TD->getDeclName(), NameLoc); |
| 2879 | |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2880 | if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2881 | DeclarationName DName; |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2882 | if (DTN->isIdentifier()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2883 | DName = DeclarationNames.getIdentifier(DTN->getIdentifier()); |
| 2884 | return DeclarationNameInfo(DName, NameLoc); |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2885 | } else { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2886 | DName = DeclarationNames.getCXXOperatorName(DTN->getOperator()); |
| 2887 | // DNInfo work in progress: FIXME: source locations? |
| 2888 | DeclarationNameLoc DNLoc; |
| 2889 | DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding(); |
| 2890 | DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding(); |
| 2891 | return DeclarationNameInfo(DName, NameLoc, DNLoc); |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2892 | } |
| 2893 | } |
| 2894 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 2895 | OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate(); |
| 2896 | assert(Storage); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2897 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 2898 | return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc); |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2899 | } |
| 2900 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2901 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const { |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 2902 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 2903 | if (TemplateTemplateParmDecl *TTP |
| 2904 | = dyn_cast<TemplateTemplateParmDecl>(Template)) |
| 2905 | Template = getCanonicalTemplateTemplateParmDecl(TTP); |
| 2906 | |
| 2907 | // The canonical template name is the canonical template declaration. |
Argyrios Kyrtzidis | 6b7e376 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 2908 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 2909 | } |
Douglas Gregor | 6bc5058 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2910 | |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 2911 | if (SubstTemplateTemplateParmPackStorage *SubstPack |
| 2912 | = Name.getAsSubstTemplateTemplateParmPack()) { |
| 2913 | TemplateTemplateParmDecl *CanonParam |
| 2914 | = getCanonicalTemplateTemplateParmDecl(SubstPack->getParameterPack()); |
| 2915 | TemplateArgument CanonArgPack |
| 2916 | = getCanonicalTemplateArgument(SubstPack->getArgumentPack()); |
| 2917 | return getSubstTemplateTemplateParmPack(CanonParam, CanonArgPack); |
| 2918 | } |
| 2919 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 2920 | assert(!Name.getAsOverloadedTemplate()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2921 | |
Douglas Gregor | 6bc5058 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2922 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 2923 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 2924 | return DTN->CanonicalTemplateName; |
| 2925 | } |
| 2926 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 2927 | bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) { |
| 2928 | X = getCanonicalTemplateName(X); |
| 2929 | Y = getCanonicalTemplateName(Y); |
| 2930 | return X.getAsVoidPointer() == Y.getAsVoidPointer(); |
| 2931 | } |
| 2932 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2933 | TemplateArgument |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2934 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const { |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2935 | switch (Arg.getKind()) { |
| 2936 | case TemplateArgument::Null: |
| 2937 | return Arg; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2938 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2939 | case TemplateArgument::Expression: |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2940 | return Arg; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2941 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2942 | case TemplateArgument::Declaration: |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2943 | return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2944 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2945 | case TemplateArgument::Template: |
| 2946 | return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate())); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2947 | |
| 2948 | case TemplateArgument::TemplateExpansion: |
| 2949 | return TemplateArgument(getCanonicalTemplateName( |
| 2950 | Arg.getAsTemplateOrTemplatePattern()), |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 2951 | Arg.getNumTemplateExpansions()); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2952 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2953 | case TemplateArgument::Integral: |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2954 | return TemplateArgument(*Arg.getAsIntegral(), |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2955 | getCanonicalType(Arg.getIntegralType())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2956 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2957 | case TemplateArgument::Type: |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2958 | return TemplateArgument(getCanonicalType(Arg.getAsType())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2959 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2960 | case TemplateArgument::Pack: { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 2961 | if (Arg.pack_size() == 0) |
| 2962 | return Arg; |
| 2963 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2964 | TemplateArgument *CanonArgs |
| 2965 | = new (*this) TemplateArgument[Arg.pack_size()]; |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2966 | unsigned Idx = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2967 | for (TemplateArgument::pack_iterator A = Arg.pack_begin(), |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2968 | AEnd = Arg.pack_end(); |
| 2969 | A != AEnd; (void)++A, ++Idx) |
| 2970 | CanonArgs[Idx] = getCanonicalTemplateArgument(*A); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2971 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2972 | return TemplateArgument(CanonArgs, Arg.pack_size()); |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2973 | } |
| 2974 | } |
| 2975 | |
| 2976 | // Silence GCC warning |
| 2977 | assert(false && "Unhandled template argument kind"); |
| 2978 | return TemplateArgument(); |
| 2979 | } |
| 2980 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2981 | NestedNameSpecifier * |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2982 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2983 | if (!NNS) |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2984 | return 0; |
| 2985 | |
| 2986 | switch (NNS->getKind()) { |
| 2987 | case NestedNameSpecifier::Identifier: |
| 2988 | // Canonicalize the prefix but keep the identifier the same. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2989 | return NestedNameSpecifier::Create(*this, |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2990 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 2991 | NNS->getAsIdentifier()); |
| 2992 | |
| 2993 | case NestedNameSpecifier::Namespace: |
| 2994 | // A namespace is canonical; build a nested-name-specifier with |
| 2995 | // this namespace and no prefix. |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 2996 | return NestedNameSpecifier::Create(*this, 0, |
| 2997 | NNS->getAsNamespace()->getOriginalNamespace()); |
| 2998 | |
| 2999 | case NestedNameSpecifier::NamespaceAlias: |
| 3000 | // A namespace is canonical; build a nested-name-specifier with |
| 3001 | // this namespace and no prefix. |
| 3002 | return NestedNameSpecifier::Create(*this, 0, |
| 3003 | NNS->getAsNamespaceAlias()->getNamespace() |
| 3004 | ->getOriginalNamespace()); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3005 | |
| 3006 | case NestedNameSpecifier::TypeSpec: |
| 3007 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 3008 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
Douglas Gregor | 3ade570 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3009 | |
| 3010 | // If we have some kind of dependent-named type (e.g., "typename T::type"), |
| 3011 | // break it apart into its prefix and identifier, then reconsititute those |
| 3012 | // as the canonical nested-name-specifier. This is required to canonicalize |
| 3013 | // a dependent nested-name-specifier involving typedefs of dependent-name |
| 3014 | // types, e.g., |
| 3015 | // typedef typename T::type T1; |
| 3016 | // typedef typename T1::type T2; |
| 3017 | if (const DependentNameType *DNT = T->getAs<DependentNameType>()) { |
| 3018 | NestedNameSpecifier *Prefix |
| 3019 | = getCanonicalNestedNameSpecifier(DNT->getQualifier()); |
| 3020 | return NestedNameSpecifier::Create(*this, Prefix, |
| 3021 | const_cast<IdentifierInfo *>(DNT->getIdentifier())); |
| 3022 | } |
| 3023 | |
Douglas Gregor | cc10cbf | 2010-11-04 00:14:23 +0000 | [diff] [blame] | 3024 | // Do the same thing as above, but with dependent-named specializations. |
Douglas Gregor | 3ade570 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3025 | if (const DependentTemplateSpecializationType *DTST |
| 3026 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 3027 | NestedNameSpecifier *Prefix |
| 3028 | = getCanonicalNestedNameSpecifier(DTST->getQualifier()); |
Douglas Gregor | 6e06801 | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 3029 | |
| 3030 | T = getDependentTemplateSpecializationType(DTST->getKeyword(), |
| 3031 | Prefix, DTST->getIdentifier(), |
| 3032 | DTST->getNumArgs(), |
| 3033 | DTST->getArgs()); |
Douglas Gregor | 3ade570 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3034 | T = getCanonicalType(T); |
| 3035 | } |
| 3036 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3037 | return NestedNameSpecifier::Create(*this, 0, false, |
| 3038 | const_cast<Type*>(T.getTypePtr())); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3039 | } |
| 3040 | |
| 3041 | case NestedNameSpecifier::Global: |
| 3042 | // The global specifier is canonical and unique. |
| 3043 | return NNS; |
| 3044 | } |
| 3045 | |
| 3046 | // Required to silence a GCC warning |
| 3047 | return 0; |
| 3048 | } |
| 3049 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3050 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3051 | const ArrayType *ASTContext::getAsArrayType(QualType T) const { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3052 | // Handle the non-qualified case efficiently. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3053 | if (!T.hasLocalQualifiers()) { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3054 | // Handle the common positive case fast. |
| 3055 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 3056 | return AT; |
| 3057 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3058 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3059 | // Handle the common negative case fast. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3060 | if (!isa<ArrayType>(T.getCanonicalType())) |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3061 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3062 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3063 | // Apply any qualifiers from the array type to the element type. This |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3064 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 3065 | // any type qualifiers, the element type is so qualified, not the array type." |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3066 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3067 | // If we get here, we either have type qualifiers on the type, or we have |
| 3068 | // sugar such as a typedef in the way. If we have type qualifiers on the type |
Douglas Gregor | 2211d34 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 3069 | // we must propagate them down into the element type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3070 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3071 | SplitQualType split = T.getSplitDesugaredType(); |
| 3072 | Qualifiers qs = split.second; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3073 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3074 | // If we have a simple case, just return now. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3075 | const ArrayType *ATy = dyn_cast<ArrayType>(split.first); |
| 3076 | if (ATy == 0 || qs.empty()) |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3077 | return ATy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3078 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3079 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 3080 | // qualifiers into the array element type and return a new array type. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3081 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3082 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3083 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 3084 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 3085 | CAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3086 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3087 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 3088 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 3089 | IAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3090 | IAT->getIndexTypeCVRQualifiers())); |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3091 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3092 | if (const DependentSizedArrayType *DSAT |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3093 | = dyn_cast<DependentSizedArrayType>(ATy)) |
| 3094 | return cast<ArrayType>( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3095 | getDependentSizedArrayType(NewEltTy, |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3096 | DSAT->getSizeExpr(), |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3097 | DSAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3098 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3099 | DSAT->getBracketsRange())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3100 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3101 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3102 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3103 | VAT->getSizeExpr(), |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3104 | VAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3105 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3106 | VAT->getBracketsRange())); |
Chris Lattner | ed0d079 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 3107 | } |
| 3108 | |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3109 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 3110 | /// specified array type to a pointer. This operation is non-trivial when |
| 3111 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 3112 | /// this returns a pointer to a properly qualified element of the array. |
| 3113 | /// |
| 3114 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3115 | QualType ASTContext::getArrayDecayedType(QualType Ty) const { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3116 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 3117 | // typedefs in the element type of the array. This also handles propagation |
| 3118 | // of type qualifiers from the array type into the element type if present |
| 3119 | // (C99 6.7.3p8). |
| 3120 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 3121 | assert(PrettyArrayType && "Not an array type!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3122 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3123 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3124 | |
| 3125 | // int x[restrict 4] -> int *restrict |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3126 | return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers()); |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3127 | } |
| 3128 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3129 | QualType ASTContext::getBaseElementType(const ArrayType *array) const { |
| 3130 | return getBaseElementType(array->getElementType()); |
Douglas Gregor | 79f83ed | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 3131 | } |
| 3132 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3133 | QualType ASTContext::getBaseElementType(QualType type) const { |
| 3134 | Qualifiers qs; |
| 3135 | while (true) { |
| 3136 | SplitQualType split = type.getSplitDesugaredType(); |
| 3137 | const ArrayType *array = split.first->getAsArrayTypeUnsafe(); |
| 3138 | if (!array) break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3139 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3140 | type = array->getElementType(); |
| 3141 | qs.addConsistentQualifiers(split.second); |
| 3142 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3143 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3144 | return getQualifiedType(type, qs); |
Anders Carlsson | e0808df | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 3145 | } |
| 3146 | |
Fariborz Jahanian | 6c9e5a2 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 3147 | /// getConstantArrayElementCount - Returns number of constant array elements. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3148 | uint64_t |
Fariborz Jahanian | 6c9e5a2 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 3149 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
| 3150 | uint64_t ElementCount = 1; |
| 3151 | do { |
| 3152 | ElementCount *= CA->getSize().getZExtValue(); |
| 3153 | CA = dyn_cast<ConstantArrayType>(CA->getElementType()); |
| 3154 | } while (CA); |
| 3155 | return ElementCount; |
| 3156 | } |
| 3157 | |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 3158 | /// getFloatingRank - Return a relative rank for floating point types. |
| 3159 | /// This routine will assert if passed a built-in type that isn't a float. |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3160 | static FloatingRank getFloatingRank(QualType T) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3161 | if (const ComplexType *CT = T->getAs<ComplexType>()) |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 3162 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3163 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3164 | assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"); |
| 3165 | switch (T->getAs<BuiltinType>()->getKind()) { |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3166 | default: assert(0 && "getFloatingRank(): not a floating type"); |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 3167 | case BuiltinType::Float: return FloatRank; |
| 3168 | case BuiltinType::Double: return DoubleRank; |
| 3169 | case BuiltinType::LongDouble: return LongDoubleRank; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 3170 | } |
| 3171 | } |
| 3172 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3173 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 3174 | /// point or a complex type (based on typeDomain/typeSize). |
Steve Naroff | fc6ffa2 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 3175 | /// 'typeDomain' is a real floating point or complex type. |
| 3176 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | b9dfb03 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 3177 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 3178 | QualType Domain) const { |
| 3179 | FloatingRank EltRank = getFloatingRank(Size); |
| 3180 | if (Domain->isComplexType()) { |
| 3181 | switch (EltRank) { |
Steve Naroff | fc6ffa2 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 3182 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
Steve Naroff | 9091ef7 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 3183 | case FloatRank: return FloatComplexTy; |
| 3184 | case DoubleRank: return DoubleComplexTy; |
| 3185 | case LongDoubleRank: return LongDoubleComplexTy; |
| 3186 | } |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 3187 | } |
Chris Lattner | b9dfb03 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 3188 | |
| 3189 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 3190 | switch (EltRank) { |
| 3191 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
| 3192 | case FloatRank: return FloatTy; |
| 3193 | case DoubleRank: return DoubleTy; |
| 3194 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | 9091ef7 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 3195 | } |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 3196 | } |
| 3197 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3198 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 3199 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 3200 | /// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3201 | /// LHS < RHS, return -1. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3202 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const { |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3203 | FloatingRank LHSR = getFloatingRank(LHS); |
| 3204 | FloatingRank RHSR = getFloatingRank(RHS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3205 | |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3206 | if (LHSR == RHSR) |
Steve Naroff | 7af82d4 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 3207 | return 0; |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3208 | if (LHSR > RHSR) |
Steve Naroff | 7af82d4 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 3209 | return 1; |
| 3210 | return -1; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 3211 | } |
| 3212 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3213 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 3214 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 3215 | /// or if it is not canonicalized. |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3216 | unsigned ASTContext::getIntegerRank(const Type *T) const { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 3217 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3218 | if (const EnumType* ET = dyn_cast<EnumType>(T)) |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 3219 | T = ET->getDecl()->getPromotionType().getTypePtr(); |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3220 | |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 3221 | if (T->isSpecificBuiltinType(BuiltinType::WChar_S) || |
| 3222 | T->isSpecificBuiltinType(BuiltinType::WChar_U)) |
Eli Friedman | c131d3b | 2009-07-05 23:44:27 +0000 | [diff] [blame] | 3223 | T = getFromTargetType(Target.getWCharType()).getTypePtr(); |
| 3224 | |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3225 | if (T->isSpecificBuiltinType(BuiltinType::Char16)) |
| 3226 | T = getFromTargetType(Target.getChar16Type()).getTypePtr(); |
| 3227 | |
| 3228 | if (T->isSpecificBuiltinType(BuiltinType::Char32)) |
| 3229 | T = getFromTargetType(Target.getChar32Type()).getTypePtr(); |
| 3230 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3231 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3232 | default: assert(0 && "getIntegerRank(): not a built-in integer"); |
| 3233 | case BuiltinType::Bool: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3234 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3235 | case BuiltinType::Char_S: |
| 3236 | case BuiltinType::Char_U: |
| 3237 | case BuiltinType::SChar: |
| 3238 | case BuiltinType::UChar: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3239 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3240 | case BuiltinType::Short: |
| 3241 | case BuiltinType::UShort: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3242 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3243 | case BuiltinType::Int: |
| 3244 | case BuiltinType::UInt: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3245 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3246 | case BuiltinType::Long: |
| 3247 | case BuiltinType::ULong: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3248 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3249 | case BuiltinType::LongLong: |
| 3250 | case BuiltinType::ULongLong: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3251 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | f122cef | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 3252 | case BuiltinType::Int128: |
| 3253 | case BuiltinType::UInt128: |
| 3254 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3255 | } |
| 3256 | } |
| 3257 | |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3258 | /// \brief Whether this is a promotable bitfield reference according |
| 3259 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
| 3260 | /// |
| 3261 | /// \returns the type this bit-field will promote to, or NULL if no |
| 3262 | /// promotion occurs. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3263 | QualType ASTContext::isPromotableBitField(Expr *E) const { |
Douglas Gregor | e05d3cb | 2010-05-24 20:13:53 +0000 | [diff] [blame] | 3264 | if (E->isTypeDependent() || E->isValueDependent()) |
| 3265 | return QualType(); |
| 3266 | |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3267 | FieldDecl *Field = E->getBitField(); |
| 3268 | if (!Field) |
| 3269 | return QualType(); |
| 3270 | |
| 3271 | QualType FT = Field->getType(); |
| 3272 | |
| 3273 | llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this); |
| 3274 | uint64_t BitWidth = BitWidthAP.getZExtValue(); |
| 3275 | uint64_t IntSize = getTypeSize(IntTy); |
| 3276 | // GCC extension compatibility: if the bit-field size is less than or equal |
| 3277 | // to the size of int, it gets promoted no matter what its type is. |
| 3278 | // For instance, unsigned long bf : 4 gets promoted to signed int. |
| 3279 | if (BitWidth < IntSize) |
| 3280 | return IntTy; |
| 3281 | |
| 3282 | if (BitWidth == IntSize) |
| 3283 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
| 3284 | |
| 3285 | // Types bigger than int are not subject to promotions, and therefore act |
| 3286 | // like the base type. |
| 3287 | // FIXME: This doesn't quite match what gcc does, but what gcc does here |
| 3288 | // is ridiculous. |
| 3289 | return QualType(); |
| 3290 | } |
| 3291 | |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3292 | /// getPromotedIntegerType - Returns the type that Promotable will |
| 3293 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
| 3294 | /// integer type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3295 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) const { |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3296 | assert(!Promotable.isNull()); |
| 3297 | assert(Promotable->isPromotableIntegerType()); |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 3298 | if (const EnumType *ET = Promotable->getAs<EnumType>()) |
| 3299 | return ET->getDecl()->getPromotionType(); |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3300 | if (Promotable->isSignedIntegerType()) |
| 3301 | return IntTy; |
| 3302 | uint64_t PromotableSize = getTypeSize(Promotable); |
| 3303 | uint64_t IntSize = getTypeSize(IntTy); |
| 3304 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
| 3305 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
| 3306 | } |
| 3307 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3308 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3309 | /// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3310 | /// LHS < RHS, return -1. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3311 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3312 | const Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 3313 | const Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3314 | if (LHSC == RHSC) return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3315 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3316 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 3317 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3318 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3319 | unsigned LHSRank = getIntegerRank(LHSC); |
| 3320 | unsigned RHSRank = getIntegerRank(RHSC); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3321 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3322 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 3323 | if (LHSRank == RHSRank) return 0; |
| 3324 | return LHSRank > RHSRank ? 1 : -1; |
| 3325 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3326 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3327 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 3328 | if (LHSUnsigned) { |
| 3329 | // If the unsigned [LHS] type is larger, return it. |
| 3330 | if (LHSRank >= RHSRank) |
| 3331 | return 1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3332 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3333 | // If the signed type can represent all values of the unsigned type, it |
| 3334 | // wins. Because we are dealing with 2's complement and types that are |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3335 | // powers of two larger than each other, this is always safe. |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3336 | return -1; |
| 3337 | } |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3338 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3339 | // If the unsigned [RHS] type is larger, return it. |
| 3340 | if (RHSRank >= LHSRank) |
| 3341 | return -1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3342 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3343 | // If the signed type can represent all values of the unsigned type, it |
| 3344 | // wins. Because we are dealing with 2's complement and types that are |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3345 | // powers of two larger than each other, this is always safe. |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3346 | return 1; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 3347 | } |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3348 | |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3349 | static RecordDecl * |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3350 | CreateRecordDecl(const ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC, |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3351 | SourceLocation L, IdentifierInfo *Id) { |
| 3352 | if (Ctx.getLangOptions().CPlusPlus) |
| 3353 | return CXXRecordDecl::Create(Ctx, TK, DC, L, Id); |
| 3354 | else |
| 3355 | return RecordDecl::Create(Ctx, TK, DC, L, Id); |
| 3356 | } |
| 3357 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3358 | // getCFConstantStringType - Return the type used for constant CFStrings. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3359 | QualType ASTContext::getCFConstantStringType() const { |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3360 | if (!CFConstantStringTypeDecl) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3361 | CFConstantStringTypeDecl = |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3362 | CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3363 | &Idents.get("NSConstantString")); |
John McCall | ae580fe | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3364 | CFConstantStringTypeDecl->startDefinition(); |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3365 | |
Anders Carlsson | 9c1011c | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 3366 | QualType FieldTypes[4]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3367 | |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3368 | // const int *isa; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3369 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
Anders Carlsson | 9c1011c | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 3370 | // int flags; |
| 3371 | FieldTypes[1] = IntTy; |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3372 | // const char *str; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3373 | FieldTypes[2] = getPointerType(CharTy.withConst()); |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3374 | // long length; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3375 | FieldTypes[3] = LongTy; |
| 3376 | |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3377 | // Create fields |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3378 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3379 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3380 | SourceLocation(), 0, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3381 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3382 | /*BitWidth=*/0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 3383 | /*Mutable=*/false); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3384 | Field->setAccess(AS_public); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3385 | CFConstantStringTypeDecl->addDecl(Field); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3386 | } |
| 3387 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3388 | CFConstantStringTypeDecl->completeDefinition(); |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3389 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3390 | |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3391 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 412af03 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 3392 | } |
Anders Carlsson | 87c149b | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 3393 | |
Douglas Gregor | 512b077 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3394 | void ASTContext::setCFConstantStringType(QualType T) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3395 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 512b077 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3396 | assert(Rec && "Invalid CFConstantStringType"); |
| 3397 | CFConstantStringTypeDecl = Rec->getDecl(); |
| 3398 | } |
| 3399 | |
Fariborz Jahanian | e804c28 | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 3400 | // getNSConstantStringType - Return the type used for constant NSStrings. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3401 | QualType ASTContext::getNSConstantStringType() const { |
Fariborz Jahanian | e804c28 | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 3402 | if (!NSConstantStringTypeDecl) { |
| 3403 | NSConstantStringTypeDecl = |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3404 | CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Fariborz Jahanian | e804c28 | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 3405 | &Idents.get("__builtin_NSString")); |
| 3406 | NSConstantStringTypeDecl->startDefinition(); |
| 3407 | |
| 3408 | QualType FieldTypes[3]; |
| 3409 | |
| 3410 | // const int *isa; |
| 3411 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
| 3412 | // const char *str; |
| 3413 | FieldTypes[1] = getPointerType(CharTy.withConst()); |
| 3414 | // unsigned int length; |
| 3415 | FieldTypes[2] = UnsignedIntTy; |
| 3416 | |
| 3417 | // Create fields |
| 3418 | for (unsigned i = 0; i < 3; ++i) { |
| 3419 | FieldDecl *Field = FieldDecl::Create(*this, NSConstantStringTypeDecl, |
| 3420 | SourceLocation(), 0, |
| 3421 | FieldTypes[i], /*TInfo=*/0, |
| 3422 | /*BitWidth=*/0, |
| 3423 | /*Mutable=*/false); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3424 | Field->setAccess(AS_public); |
Fariborz Jahanian | e804c28 | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 3425 | NSConstantStringTypeDecl->addDecl(Field); |
| 3426 | } |
| 3427 | |
| 3428 | NSConstantStringTypeDecl->completeDefinition(); |
| 3429 | } |
| 3430 | |
| 3431 | return getTagDeclType(NSConstantStringTypeDecl); |
| 3432 | } |
| 3433 | |
| 3434 | void ASTContext::setNSConstantStringType(QualType T) { |
| 3435 | const RecordType *Rec = T->getAs<RecordType>(); |
| 3436 | assert(Rec && "Invalid NSConstantStringType"); |
| 3437 | NSConstantStringTypeDecl = Rec->getDecl(); |
| 3438 | } |
| 3439 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3440 | QualType ASTContext::getObjCFastEnumerationStateType() const { |
Anders Carlsson | d89ba7d | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3441 | if (!ObjCFastEnumerationStateTypeDecl) { |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3442 | ObjCFastEnumerationStateTypeDecl = |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3443 | CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3444 | &Idents.get("__objcFastEnumerationState")); |
John McCall | ae580fe | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3445 | ObjCFastEnumerationStateTypeDecl->startDefinition(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3446 | |
Anders Carlsson | d89ba7d | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3447 | QualType FieldTypes[] = { |
| 3448 | UnsignedLongTy, |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3449 | getPointerType(ObjCIdTypedefType), |
Anders Carlsson | d89ba7d | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3450 | getPointerType(UnsignedLongTy), |
| 3451 | getConstantArrayType(UnsignedLongTy, |
| 3452 | llvm::APInt(32, 5), ArrayType::Normal, 0) |
| 3453 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3454 | |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3455 | for (size_t i = 0; i < 4; ++i) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3456 | FieldDecl *Field = FieldDecl::Create(*this, |
| 3457 | ObjCFastEnumerationStateTypeDecl, |
| 3458 | SourceLocation(), 0, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3459 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3460 | /*BitWidth=*/0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 3461 | /*Mutable=*/false); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3462 | Field->setAccess(AS_public); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3463 | ObjCFastEnumerationStateTypeDecl->addDecl(Field); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3464 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3465 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3466 | ObjCFastEnumerationStateTypeDecl->completeDefinition(); |
Anders Carlsson | d89ba7d | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3467 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3468 | |
Anders Carlsson | d89ba7d | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3469 | return getTagDeclType(ObjCFastEnumerationStateTypeDecl); |
| 3470 | } |
| 3471 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3472 | QualType ASTContext::getBlockDescriptorType() const { |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3473 | if (BlockDescriptorType) |
| 3474 | return getTagDeclType(BlockDescriptorType); |
| 3475 | |
| 3476 | RecordDecl *T; |
| 3477 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3478 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3479 | &Idents.get("__block_descriptor")); |
John McCall | ae580fe | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3480 | T->startDefinition(); |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3481 | |
| 3482 | QualType FieldTypes[] = { |
| 3483 | UnsignedLongTy, |
| 3484 | UnsignedLongTy, |
| 3485 | }; |
| 3486 | |
| 3487 | const char *FieldNames[] = { |
| 3488 | "reserved", |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3489 | "Size" |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3490 | }; |
| 3491 | |
| 3492 | for (size_t i = 0; i < 2; ++i) { |
| 3493 | FieldDecl *Field = FieldDecl::Create(*this, |
| 3494 | T, |
| 3495 | SourceLocation(), |
| 3496 | &Idents.get(FieldNames[i]), |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3497 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3498 | /*BitWidth=*/0, |
| 3499 | /*Mutable=*/false); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3500 | Field->setAccess(AS_public); |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3501 | T->addDecl(Field); |
| 3502 | } |
| 3503 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3504 | T->completeDefinition(); |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3505 | |
| 3506 | BlockDescriptorType = T; |
| 3507 | |
| 3508 | return getTagDeclType(BlockDescriptorType); |
| 3509 | } |
| 3510 | |
| 3511 | void ASTContext::setBlockDescriptorType(QualType T) { |
| 3512 | const RecordType *Rec = T->getAs<RecordType>(); |
| 3513 | assert(Rec && "Invalid BlockDescriptorType"); |
| 3514 | BlockDescriptorType = Rec->getDecl(); |
| 3515 | } |
| 3516 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3517 | QualType ASTContext::getBlockDescriptorExtendedType() const { |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3518 | if (BlockDescriptorExtendedType) |
| 3519 | return getTagDeclType(BlockDescriptorExtendedType); |
| 3520 | |
| 3521 | RecordDecl *T; |
| 3522 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3523 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3524 | &Idents.get("__block_descriptor_withcopydispose")); |
John McCall | ae580fe | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3525 | T->startDefinition(); |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3526 | |
| 3527 | QualType FieldTypes[] = { |
| 3528 | UnsignedLongTy, |
| 3529 | UnsignedLongTy, |
| 3530 | getPointerType(VoidPtrTy), |
| 3531 | getPointerType(VoidPtrTy) |
| 3532 | }; |
| 3533 | |
| 3534 | const char *FieldNames[] = { |
| 3535 | "reserved", |
| 3536 | "Size", |
| 3537 | "CopyFuncPtr", |
| 3538 | "DestroyFuncPtr" |
| 3539 | }; |
| 3540 | |
| 3541 | for (size_t i = 0; i < 4; ++i) { |
| 3542 | FieldDecl *Field = FieldDecl::Create(*this, |
| 3543 | T, |
| 3544 | SourceLocation(), |
| 3545 | &Idents.get(FieldNames[i]), |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3546 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3547 | /*BitWidth=*/0, |
| 3548 | /*Mutable=*/false); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3549 | Field->setAccess(AS_public); |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3550 | T->addDecl(Field); |
| 3551 | } |
| 3552 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3553 | T->completeDefinition(); |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3554 | |
| 3555 | BlockDescriptorExtendedType = T; |
| 3556 | |
| 3557 | return getTagDeclType(BlockDescriptorExtendedType); |
| 3558 | } |
| 3559 | |
| 3560 | void ASTContext::setBlockDescriptorExtendedType(QualType T) { |
| 3561 | const RecordType *Rec = T->getAs<RecordType>(); |
| 3562 | assert(Rec && "Invalid BlockDescriptorType"); |
| 3563 | BlockDescriptorExtendedType = Rec->getDecl(); |
| 3564 | } |
| 3565 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3566 | bool ASTContext::BlockRequiresCopying(QualType Ty) const { |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3567 | if (Ty->isBlockPointerType()) |
| 3568 | return true; |
| 3569 | if (isObjCNSObjectType(Ty)) |
| 3570 | return true; |
| 3571 | if (Ty->isObjCObjectPointerType()) |
| 3572 | return true; |
Fariborz Jahanian | a00076c | 2010-11-17 00:21:28 +0000 | [diff] [blame] | 3573 | if (getLangOptions().CPlusPlus) { |
| 3574 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3575 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 3576 | return RD->hasConstCopyConstructor(*this); |
| 3577 | |
| 3578 | } |
| 3579 | } |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3580 | return false; |
| 3581 | } |
| 3582 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3583 | QualType |
| 3584 | ASTContext::BuildByRefType(llvm::StringRef DeclName, QualType Ty) const { |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3585 | // type = struct __Block_byref_1_X { |
Mike Stump | 7fe9cc1 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3586 | // void *__isa; |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3587 | // struct __Block_byref_1_X *__forwarding; |
Mike Stump | 7fe9cc1 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3588 | // unsigned int __flags; |
| 3589 | // unsigned int __size; |
Eli Friedman | 0483192 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 3590 | // void *__copy_helper; // as needed |
| 3591 | // void *__destroy_help // as needed |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3592 | // int X; |
Mike Stump | 7fe9cc1 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3593 | // } * |
| 3594 | |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3595 | bool HasCopyAndDispose = BlockRequiresCopying(Ty); |
| 3596 | |
| 3597 | // FIXME: Move up |
Benjamin Kramer | 1402ce3 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 3598 | llvm::SmallString<36> Name; |
| 3599 | llvm::raw_svector_ostream(Name) << "__Block_byref_" << |
| 3600 | ++UniqueBlockByRefTypeID << '_' << DeclName; |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3601 | RecordDecl *T; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3602 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3603 | &Idents.get(Name.str())); |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3604 | T->startDefinition(); |
| 3605 | QualType Int32Ty = IntTy; |
| 3606 | assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported"); |
| 3607 | QualType FieldTypes[] = { |
| 3608 | getPointerType(VoidPtrTy), |
| 3609 | getPointerType(getTagDeclType(T)), |
| 3610 | Int32Ty, |
| 3611 | Int32Ty, |
| 3612 | getPointerType(VoidPtrTy), |
| 3613 | getPointerType(VoidPtrTy), |
| 3614 | Ty |
| 3615 | }; |
| 3616 | |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3617 | llvm::StringRef FieldNames[] = { |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3618 | "__isa", |
| 3619 | "__forwarding", |
| 3620 | "__flags", |
| 3621 | "__size", |
| 3622 | "__copy_helper", |
| 3623 | "__destroy_helper", |
| 3624 | DeclName, |
| 3625 | }; |
| 3626 | |
| 3627 | for (size_t i = 0; i < 7; ++i) { |
| 3628 | if (!HasCopyAndDispose && i >=4 && i <= 5) |
| 3629 | continue; |
| 3630 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
| 3631 | &Idents.get(FieldNames[i]), |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3632 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3633 | /*BitWidth=*/0, /*Mutable=*/false); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3634 | Field->setAccess(AS_public); |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3635 | T->addDecl(Field); |
| 3636 | } |
| 3637 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3638 | T->completeDefinition(); |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3639 | |
| 3640 | return getPointerType(getTagDeclType(T)); |
Mike Stump | 7fe9cc1 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3641 | } |
| 3642 | |
Douglas Gregor | 512b077 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3643 | void ASTContext::setObjCFastEnumerationStateType(QualType T) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3644 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 512b077 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3645 | assert(Rec && "Invalid ObjCFAstEnumerationStateType"); |
| 3646 | ObjCFastEnumerationStateTypeDecl = Rec->getDecl(); |
| 3647 | } |
| 3648 | |
Anders Carlsson | 18acd44 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3649 | // This returns true if a type has been typedefed to BOOL: |
| 3650 | // typedef <type> BOOL; |
Chris Lattner | e021899 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 3651 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | 18acd44 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3652 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | 9b1f279 | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 3653 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 3654 | return II->isStr("BOOL"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3655 | |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3656 | return false; |
| 3657 | } |
| 3658 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3659 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3660 | /// purpose. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3661 | CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const { |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3662 | CharUnits sz = getTypeSizeInChars(type); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3663 | |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3664 | // Make all integer and enum types at least as large as an int |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3665 | if (sz.isPositive() && type->isIntegralOrEnumerationType()) |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3666 | sz = std::max(sz, getTypeSizeInChars(IntTy)); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3667 | // Treat arrays as pointers, since that's how they're passed in. |
| 3668 | else if (type->isArrayType()) |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3669 | sz = getTypeSizeInChars(VoidPtrTy); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3670 | return sz; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3671 | } |
| 3672 | |
| 3673 | static inline |
| 3674 | std::string charUnitsToString(const CharUnits &CU) { |
| 3675 | return llvm::itostr(CU.getQuantity()); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3676 | } |
| 3677 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 3678 | /// getObjCEncodingForBlock - Return the encoded type for this block |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3679 | /// declaration. |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 3680 | std::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const { |
| 3681 | std::string S; |
| 3682 | |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3683 | const BlockDecl *Decl = Expr->getBlockDecl(); |
| 3684 | QualType BlockTy = |
| 3685 | Expr->getType()->getAs<BlockPointerType>()->getPointeeType(); |
| 3686 | // Encode result type. |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 3687 | getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3688 | // Compute size of all parameters. |
| 3689 | // Start with computing size of a pointer in number of bytes. |
| 3690 | // FIXME: There might(should) be a better way of doing this computation! |
| 3691 | SourceLocation Loc; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3692 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
| 3693 | CharUnits ParmOffset = PtrSize; |
Fariborz Jahanian | 590c352 | 2010-04-08 18:06:22 +0000 | [diff] [blame] | 3694 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3695 | E = Decl->param_end(); PI != E; ++PI) { |
| 3696 | QualType PType = (*PI)->getType(); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3697 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3698 | assert (sz.isPositive() && "BlockExpr - Incomplete param type"); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3699 | ParmOffset += sz; |
| 3700 | } |
| 3701 | // Size of the argument frame |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3702 | S += charUnitsToString(ParmOffset); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3703 | // Block pointer and offset. |
| 3704 | S += "@?0"; |
| 3705 | ParmOffset = PtrSize; |
| 3706 | |
| 3707 | // Argument types. |
| 3708 | ParmOffset = PtrSize; |
| 3709 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E = |
| 3710 | Decl->param_end(); PI != E; ++PI) { |
| 3711 | ParmVarDecl *PVDecl = *PI; |
| 3712 | QualType PType = PVDecl->getOriginalType(); |
| 3713 | if (const ArrayType *AT = |
| 3714 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3715 | // Use array's original type only if it has known number of |
| 3716 | // elements. |
| 3717 | if (!isa<ConstantArrayType>(AT)) |
| 3718 | PType = PVDecl->getType(); |
| 3719 | } else if (PType->isFunctionType()) |
| 3720 | PType = PVDecl->getType(); |
| 3721 | getObjCEncodingForType(PType, S); |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3722 | S += charUnitsToString(ParmOffset); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3723 | ParmOffset += getObjCEncodingTypeSize(PType); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3724 | } |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 3725 | |
| 3726 | return S; |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3727 | } |
| 3728 | |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 3729 | void ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl, |
| 3730 | std::string& S) { |
| 3731 | // Encode result type. |
| 3732 | getObjCEncodingForType(Decl->getResultType(), S); |
| 3733 | CharUnits ParmOffset; |
| 3734 | // Compute size of all parameters. |
| 3735 | for (FunctionDecl::param_const_iterator PI = Decl->param_begin(), |
| 3736 | E = Decl->param_end(); PI != E; ++PI) { |
| 3737 | QualType PType = (*PI)->getType(); |
| 3738 | CharUnits sz = getObjCEncodingTypeSize(PType); |
| 3739 | assert (sz.isPositive() && |
| 3740 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
| 3741 | ParmOffset += sz; |
| 3742 | } |
| 3743 | S += charUnitsToString(ParmOffset); |
| 3744 | ParmOffset = CharUnits::Zero(); |
| 3745 | |
| 3746 | // Argument types. |
| 3747 | for (FunctionDecl::param_const_iterator PI = Decl->param_begin(), |
| 3748 | E = Decl->param_end(); PI != E; ++PI) { |
| 3749 | ParmVarDecl *PVDecl = *PI; |
| 3750 | QualType PType = PVDecl->getOriginalType(); |
| 3751 | if (const ArrayType *AT = |
| 3752 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3753 | // Use array's original type only if it has known number of |
| 3754 | // elements. |
| 3755 | if (!isa<ConstantArrayType>(AT)) |
| 3756 | PType = PVDecl->getType(); |
| 3757 | } else if (PType->isFunctionType()) |
| 3758 | PType = PVDecl->getType(); |
| 3759 | getObjCEncodingForType(PType, S); |
| 3760 | S += charUnitsToString(ParmOffset); |
| 3761 | ParmOffset += getObjCEncodingTypeSize(PType); |
| 3762 | } |
| 3763 | } |
| 3764 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3765 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3766 | /// declaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3767 | void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3768 | std::string& S) const { |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3769 | // FIXME: This is not very efficient. |
Fariborz Jahanian | ac73ff8 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3770 | // Encode type qualifer, 'in', 'inout', etc. for the return type. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3771 | getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3772 | // Encode result type. |
Daniel Dunbar | fc1066d | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3773 | getObjCEncodingForType(Decl->getResultType(), S); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3774 | // Compute size of all parameters. |
| 3775 | // Start with computing size of a pointer in number of bytes. |
| 3776 | // FIXME: There might(should) be a better way of doing this computation! |
| 3777 | SourceLocation Loc; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3778 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3779 | // The first two arguments (self and _cmd) are pointers; account for |
| 3780 | // their size. |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3781 | CharUnits ParmOffset = 2 * PtrSize; |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3782 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | d9235db | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 3783 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3784 | QualType PType = (*PI)->getType(); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3785 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3786 | assert (sz.isPositive() && |
| 3787 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3788 | ParmOffset += sz; |
| 3789 | } |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3790 | S += charUnitsToString(ParmOffset); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3791 | S += "@0:"; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3792 | S += charUnitsToString(PtrSize); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3793 | |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3794 | // Argument types. |
| 3795 | ParmOffset = 2 * PtrSize; |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3796 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | d9235db | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 3797 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3798 | ParmVarDecl *PVDecl = *PI; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3799 | QualType PType = PVDecl->getOriginalType(); |
Fariborz Jahanian | a0befc0 | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 3800 | if (const ArrayType *AT = |
Steve Naroff | e4e55d2 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 3801 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3802 | // Use array's original type only if it has known number of |
| 3803 | // elements. |
Steve Naroff | 323827e | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 3804 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | e4e55d2 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 3805 | PType = PVDecl->getType(); |
| 3806 | } else if (PType->isFunctionType()) |
| 3807 | PType = PVDecl->getType(); |
Fariborz Jahanian | ac73ff8 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3808 | // Process argument qualifiers for user supplied arguments; such as, |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3809 | // 'in', 'inout', etc. |
Fariborz Jahanian | a0befc0 | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 3810 | getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S); |
Daniel Dunbar | fc1066d | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3811 | getObjCEncodingForType(PType, S); |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3812 | S += charUnitsToString(ParmOffset); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3813 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3814 | } |
| 3815 | } |
| 3816 | |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3817 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 2f85a64 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 3818 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3819 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 3820 | /// NULL when getting encodings for protocol properties. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3821 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 3822 | /// attributes readonly and bycopy are encoded as single characters. The |
| 3823 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 3824 | /// encoded as single characters, followed by an identifier. Property types |
| 3825 | /// are also encoded as a parametrized attribute. The characters used to encode |
Fariborz Jahanian | 2f85a64 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 3826 | /// these attributes are defined by the following enumeration: |
| 3827 | /// @code |
| 3828 | /// enum PropertyAttributes { |
| 3829 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 3830 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 3831 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 3832 | /// kPropertyDynamic = 'D', // property is dynamic |
| 3833 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 3834 | /// kPropertySetter = 'S', // followed by setter selector name |
| 3835 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
| 3836 | /// kPropertyType = 't' // followed by old-style type encoding. |
| 3837 | /// kPropertyWeak = 'W' // 'weak' property |
| 3838 | /// kPropertyStrong = 'P' // property GC'able |
| 3839 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 3840 | /// }; |
| 3841 | /// @endcode |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3842 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3843 | const Decl *Container, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3844 | std::string& S) const { |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3845 | // Collect information from the property implementation decl(s). |
| 3846 | bool Dynamic = false; |
| 3847 | ObjCPropertyImplDecl *SynthesizePID = 0; |
| 3848 | |
| 3849 | // FIXME: Duplicated code due to poor abstraction. |
| 3850 | if (Container) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3851 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3852 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
| 3853 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3854 | i = CID->propimpl_begin(), e = CID->propimpl_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3855 | i != e; ++i) { |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3856 | ObjCPropertyImplDecl *PID = *i; |
| 3857 | if (PID->getPropertyDecl() == PD) { |
| 3858 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 3859 | Dynamic = true; |
| 3860 | } else { |
| 3861 | SynthesizePID = PID; |
| 3862 | } |
| 3863 | } |
| 3864 | } |
| 3865 | } else { |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3866 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3867 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3868 | i = OID->propimpl_begin(), e = OID->propimpl_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3869 | i != e; ++i) { |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3870 | ObjCPropertyImplDecl *PID = *i; |
| 3871 | if (PID->getPropertyDecl() == PD) { |
| 3872 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 3873 | Dynamic = true; |
| 3874 | } else { |
| 3875 | SynthesizePID = PID; |
| 3876 | } |
| 3877 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3878 | } |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3879 | } |
| 3880 | } |
| 3881 | |
| 3882 | // FIXME: This is not very efficient. |
| 3883 | S = "T"; |
| 3884 | |
| 3885 | // Encode result type. |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3886 | // GCC has some special rules regarding encoding of properties which |
| 3887 | // closely resembles encoding of ivars. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3888 | getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0, |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3889 | true /* outermost type */, |
| 3890 | true /* encoding for property */); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3891 | |
| 3892 | if (PD->isReadOnly()) { |
| 3893 | S += ",R"; |
| 3894 | } else { |
| 3895 | switch (PD->getSetterKind()) { |
| 3896 | case ObjCPropertyDecl::Assign: break; |
| 3897 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3898 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3899 | } |
| 3900 | } |
| 3901 | |
| 3902 | // It really isn't clear at all what this means, since properties |
| 3903 | // are "dynamic by default". |
| 3904 | if (Dynamic) |
| 3905 | S += ",D"; |
| 3906 | |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3907 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 3908 | S += ",N"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3909 | |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3910 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 3911 | S += ",G"; |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3912 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3913 | } |
| 3914 | |
| 3915 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 3916 | S += ",S"; |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3917 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3918 | } |
| 3919 | |
| 3920 | if (SynthesizePID) { |
| 3921 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 3922 | S += ",V"; |
Chris Lattner | 1cbaacc | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 3923 | S += OID->getNameAsString(); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3924 | } |
| 3925 | |
| 3926 | // FIXME: OBJCGC: weak & strong |
| 3927 | } |
| 3928 | |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3929 | /// getLegacyIntegralTypeEncoding - |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3930 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
| 3931 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3932 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
| 3933 | /// |
| 3934 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 212005c | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3935 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3936 | if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) { |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3937 | if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3938 | PointeeTy = UnsignedIntTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3939 | else |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3940 | if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3941 | PointeeTy = IntTy; |
| 3942 | } |
| 3943 | } |
| 3944 | } |
| 3945 | |
Fariborz Jahanian | 0a71ad2 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3946 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3947 | const FieldDecl *Field) const { |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3948 | // We follow the behavior of gcc, expanding structures which are |
| 3949 | // directly pointed to, and expanding embedded structures. Note that |
| 3950 | // these rules are sufficient to prevent recursive encoding of the |
| 3951 | // same type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3952 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
Fariborz Jahanian | daef00b | 2008-12-22 23:22:27 +0000 | [diff] [blame] | 3953 | true /* outermost type */); |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3954 | } |
| 3955 | |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3956 | static char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) { |
| 3957 | switch (T->getAs<BuiltinType>()->getKind()) { |
| 3958 | default: assert(0 && "Unhandled builtin type kind"); |
| 3959 | case BuiltinType::Void: return 'v'; |
| 3960 | case BuiltinType::Bool: return 'B'; |
| 3961 | case BuiltinType::Char_U: |
| 3962 | case BuiltinType::UChar: return 'C'; |
| 3963 | case BuiltinType::UShort: return 'S'; |
| 3964 | case BuiltinType::UInt: return 'I'; |
| 3965 | case BuiltinType::ULong: |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3966 | return C->getIntWidth(T) == 32 ? 'L' : 'Q'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3967 | case BuiltinType::UInt128: return 'T'; |
| 3968 | case BuiltinType::ULongLong: return 'Q'; |
| 3969 | case BuiltinType::Char_S: |
| 3970 | case BuiltinType::SChar: return 'c'; |
| 3971 | case BuiltinType::Short: return 's'; |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 3972 | case BuiltinType::WChar_S: |
| 3973 | case BuiltinType::WChar_U: |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3974 | case BuiltinType::Int: return 'i'; |
| 3975 | case BuiltinType::Long: |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3976 | return C->getIntWidth(T) == 32 ? 'l' : 'q'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3977 | case BuiltinType::LongLong: return 'q'; |
| 3978 | case BuiltinType::Int128: return 't'; |
| 3979 | case BuiltinType::Float: return 'f'; |
| 3980 | case BuiltinType::Double: return 'd'; |
Daniel Dunbar | 7cba5a7 | 2010-10-11 21:13:48 +0000 | [diff] [blame] | 3981 | case BuiltinType::LongDouble: return 'D'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3982 | } |
| 3983 | } |
| 3984 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3985 | static void EncodeBitField(const ASTContext *Ctx, std::string& S, |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3986 | QualType T, const FieldDecl *FD) { |
Fariborz Jahanian | 5c76772 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3987 | const Expr *E = FD->getBitWidth(); |
| 3988 | assert(E && "bitfield width not there - getObjCEncodingForTypeImpl"); |
Fariborz Jahanian | 5c76772 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3989 | S += 'b'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3990 | // The NeXT runtime encodes bit fields as b followed by the number of bits. |
| 3991 | // The GNU runtime requires more information; bitfields are encoded as b, |
| 3992 | // then the offset (in bits) of the first element, then the type of the |
| 3993 | // bitfield, then the size in bits. For example, in this structure: |
| 3994 | // |
| 3995 | // struct |
| 3996 | // { |
| 3997 | // int integer; |
| 3998 | // int flags:2; |
| 3999 | // }; |
| 4000 | // On a 32-bit system, the encoding for flags would be b2 for the NeXT |
| 4001 | // runtime, but b32i2 for the GNU runtime. The reason for this extra |
| 4002 | // information is not especially sensible, but we're stuck with it for |
| 4003 | // compatibility with GCC, although providing it breaks anything that |
| 4004 | // actually uses runtime introspection and wants to work on both runtimes... |
| 4005 | if (!Ctx->getLangOptions().NeXTRuntime) { |
| 4006 | const RecordDecl *RD = FD->getParent(); |
| 4007 | const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD); |
| 4008 | // FIXME: This same linear search is also used in ExprConstant - it might |
| 4009 | // be better if the FieldDecl stored its offset. We'd be increasing the |
| 4010 | // size of the object slightly, but saving some time every time it is used. |
| 4011 | unsigned i = 0; |
| 4012 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 4013 | FieldEnd = RD->field_end(); |
| 4014 | Field != FieldEnd; (void)++Field, ++i) { |
| 4015 | if (*Field == FD) |
| 4016 | break; |
| 4017 | } |
| 4018 | S += llvm::utostr(RL.getFieldOffset(i)); |
David Chisnall | 6f0a7d2 | 2010-12-26 20:12:30 +0000 | [diff] [blame] | 4019 | if (T->isEnumeralType()) |
| 4020 | S += 'i'; |
| 4021 | else |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4022 | S += ObjCEncodingForPrimitiveKind(Ctx, T); |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4023 | } |
| 4024 | unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue(); |
Fariborz Jahanian | 5c76772 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4025 | S += llvm::utostr(N); |
| 4026 | } |
| 4027 | |
Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 4028 | // FIXME: Use SmallString for accumulating string. |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4029 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 4030 | bool ExpandPointedToStructures, |
| 4031 | bool ExpandStructures, |
Daniel Dunbar | c040ce4 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 4032 | const FieldDecl *FD, |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4033 | bool OutermostType, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4034 | bool EncodingProperty) const { |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4035 | if (T->getAs<BuiltinType>()) { |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4036 | if (FD && FD->isBitField()) |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4037 | return EncodeBitField(this, S, T, FD); |
| 4038 | S += ObjCEncodingForPrimitiveKind(this, T); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4039 | return; |
| 4040 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4041 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4042 | if (const ComplexType *CT = T->getAs<ComplexType>()) { |
Anders Carlsson | 39b2e13 | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 4043 | S += 'j'; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4044 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false, |
Anders Carlsson | 39b2e13 | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 4045 | false); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4046 | return; |
| 4047 | } |
Fariborz Jahanian | d25c219 | 2009-11-23 20:40:50 +0000 | [diff] [blame] | 4048 | |
Fariborz Jahanian | 9ffd706 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4049 | // encoding for pointer or r3eference types. |
| 4050 | QualType PointeeTy; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4051 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Fariborz Jahanian | 89b660c | 2009-11-30 18:43:52 +0000 | [diff] [blame] | 4052 | if (PT->isObjCSelType()) { |
| 4053 | S += ':'; |
| 4054 | return; |
| 4055 | } |
Fariborz Jahanian | 9ffd706 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4056 | PointeeTy = PT->getPointeeType(); |
| 4057 | } |
| 4058 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 4059 | PointeeTy = RT->getPointeeType(); |
| 4060 | if (!PointeeTy.isNull()) { |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4061 | bool isReadOnly = false; |
| 4062 | // For historical/compatibility reasons, the read-only qualifier of the |
| 4063 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 4064 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4065 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 212005c | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 4066 | if (isa<TypedefType>(T.getTypePtr())) { |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4067 | if (OutermostType && T.isConstQualified()) { |
| 4068 | isReadOnly = true; |
| 4069 | S += 'r'; |
| 4070 | } |
Mike Stump | e9c6ffc | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 4071 | } else if (OutermostType) { |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4072 | QualType P = PointeeTy; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4073 | while (P->getAs<PointerType>()) |
| 4074 | P = P->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4075 | if (P.isConstQualified()) { |
| 4076 | isReadOnly = true; |
| 4077 | S += 'r'; |
| 4078 | } |
| 4079 | } |
| 4080 | if (isReadOnly) { |
| 4081 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 4082 | // combinations need to be rearranged. |
| 4083 | // Rewrite "in const" from "nr" to "rn" |
Benjamin Kramer | 2e3197e | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 4084 | if (llvm::StringRef(S).endswith("nr")) |
| 4085 | S.replace(S.end()-2, S.end(), "rn"); |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4086 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4087 | |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4088 | if (PointeeTy->isCharType()) { |
| 4089 | // char pointer types should be encoded as '*' unless it is a |
| 4090 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | 18acd44 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 4091 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4092 | S += '*'; |
| 4093 | return; |
| 4094 | } |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4095 | } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) { |
Steve Naroff | 3de6b70 | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 4096 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 4097 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 4098 | S += '#'; |
| 4099 | return; |
| 4100 | } |
| 4101 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 4102 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 4103 | S += '@'; |
| 4104 | return; |
| 4105 | } |
| 4106 | // fall through... |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4107 | } |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4108 | S += '^'; |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4109 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 4110 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4111 | getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4112 | NULL); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4113 | return; |
| 4114 | } |
Fariborz Jahanian | 9ffd706 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4115 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4116 | if (const ArrayType *AT = |
| 4117 | // Ignore type qualifiers etc. |
| 4118 | dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) { |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4119 | if (isa<IncompleteArrayType>(AT)) { |
| 4120 | // Incomplete arrays are encoded as a pointer to the array element. |
| 4121 | S += '^'; |
| 4122 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4123 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4124 | false, ExpandStructures, FD); |
| 4125 | } else { |
| 4126 | S += '['; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4127 | |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4128 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 4129 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 4130 | else { |
| 4131 | //Variable length arrays are encoded as a regular array with 0 elements. |
| 4132 | assert(isa<VariableArrayType>(AT) && "Unknown array type!"); |
| 4133 | S += '0'; |
| 4134 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4135 | |
| 4136 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4137 | false, ExpandStructures, FD); |
| 4138 | S += ']'; |
| 4139 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4140 | return; |
| 4141 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4142 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4143 | if (T->getAs<FunctionType>()) { |
Anders Carlsson | df4cc61 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 4144 | S += '?'; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4145 | return; |
| 4146 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4147 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4148 | if (const RecordType *RTy = T->getAs<RecordType>()) { |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4149 | RecordDecl *RDecl = RTy->getDecl(); |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4150 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 40cac77 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 4151 | // Anonymous structures print as '?' |
| 4152 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 4153 | S += II->getName(); |
Fariborz Jahanian | c515820 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 4154 | if (ClassTemplateSpecializationDecl *Spec |
| 4155 | = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) { |
| 4156 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 4157 | std::string TemplateArgsStr |
| 4158 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4159 | TemplateArgs.data(), |
| 4160 | TemplateArgs.size(), |
Fariborz Jahanian | c515820 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 4161 | (*this).PrintingPolicy); |
| 4162 | |
| 4163 | S += TemplateArgsStr; |
| 4164 | } |
Daniel Dunbar | 40cac77 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 4165 | } else { |
| 4166 | S += '?'; |
| 4167 | } |
Daniel Dunbar | fc1066d | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 4168 | if (ExpandStructures) { |
Fariborz Jahanian | 0a71ad2 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4169 | S += '='; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4170 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 4171 | FieldEnd = RDecl->field_end(); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4172 | Field != FieldEnd; ++Field) { |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4173 | if (FD) { |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4174 | S += '"'; |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4175 | S += Field->getNameAsString(); |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4176 | S += '"'; |
| 4177 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4178 | |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4179 | // Special case bit-fields. |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4180 | if (Field->isBitField()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4181 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4182 | (*Field)); |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4183 | } else { |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4184 | QualType qt = Field->getType(); |
| 4185 | getLegacyIntegralTypeEncoding(qt); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4186 | getObjCEncodingForTypeImpl(qt, S, false, true, |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4187 | FD); |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4188 | } |
Fariborz Jahanian | 0a71ad2 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4189 | } |
Fariborz Jahanian | bc92fd7 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 4190 | } |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4191 | S += RDecl->isUnion() ? ')' : '}'; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4192 | return; |
| 4193 | } |
Fariborz Jahanian | e0587be | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 4194 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4195 | if (T->isEnumeralType()) { |
Fariborz Jahanian | 5c76772 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4196 | if (FD && FD->isBitField()) |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4197 | EncodeBitField(this, S, T, FD); |
Fariborz Jahanian | 5c76772 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4198 | else |
| 4199 | S += 'i'; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4200 | return; |
| 4201 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4202 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4203 | if (T->isBlockPointerType()) { |
Steve Naroff | 49140cb | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 4204 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4205 | return; |
| 4206 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4207 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4208 | // Ignore protocol qualifiers when mangling at this level. |
| 4209 | if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>()) |
| 4210 | T = OT->getBaseType(); |
| 4211 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4212 | if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) { |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4213 | // @encode(class_name) |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4214 | ObjCInterfaceDecl *OI = OIT->getDecl(); |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4215 | S += '{'; |
| 4216 | const IdentifierInfo *II = OI->getIdentifier(); |
| 4217 | S += II->getName(); |
| 4218 | S += '='; |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4219 | llvm::SmallVector<ObjCIvarDecl*, 32> Ivars; |
| 4220 | DeepCollectObjCIvars(OI, true, Ivars); |
| 4221 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) { |
| 4222 | FieldDecl *Field = cast<FieldDecl>(Ivars[i]); |
| 4223 | if (Field->isBitField()) |
| 4224 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field); |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4225 | else |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4226 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD); |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4227 | } |
| 4228 | S += '}'; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4229 | return; |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4230 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4231 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4232 | if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4233 | if (OPT->isObjCIdType()) { |
| 4234 | S += '@'; |
| 4235 | return; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4236 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4237 | |
Steve Naroff | f0c8611 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4238 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
| 4239 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
| 4240 | // Since this is a binary compatibility issue, need to consult with runtime |
| 4241 | // folks. Fortunately, this is a *very* obsure construct. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4242 | S += '#'; |
| 4243 | return; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4244 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4245 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4246 | if (OPT->isObjCQualifiedIdType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4247 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4248 | ExpandPointedToStructures, |
| 4249 | ExpandStructures, FD); |
| 4250 | if (FD || EncodingProperty) { |
| 4251 | // Note that we do extended encoding of protocol qualifer list |
| 4252 | // Only when doing ivar or property encoding. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4253 | S += '"'; |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4254 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 4255 | E = OPT->qual_end(); I != E; ++I) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4256 | S += '<'; |
| 4257 | S += (*I)->getNameAsString(); |
| 4258 | S += '>'; |
| 4259 | } |
| 4260 | S += '"'; |
| 4261 | } |
| 4262 | return; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4263 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4264 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4265 | QualType PointeeTy = OPT->getPointeeType(); |
| 4266 | if (!EncodingProperty && |
| 4267 | isa<TypedefType>(PointeeTy.getTypePtr())) { |
| 4268 | // Another historical/compatibility reason. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4269 | // We encode the underlying type which comes out as |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4270 | // {...}; |
| 4271 | S += '^'; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4272 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 4273 | false, ExpandPointedToStructures, |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4274 | NULL); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4275 | return; |
| 4276 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4277 | |
| 4278 | S += '@'; |
Steve Naroff | f0c8611 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4279 | if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) { |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4280 | S += '"'; |
Steve Naroff | f0c8611 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4281 | S += OPT->getInterfaceDecl()->getIdentifier()->getName(); |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4282 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 4283 | E = OPT->qual_end(); I != E; ++I) { |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4284 | S += '<'; |
| 4285 | S += (*I)->getNameAsString(); |
| 4286 | S += '>'; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4287 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4288 | S += '"'; |
| 4289 | } |
| 4290 | return; |
| 4291 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4292 | |
John McCall | a9e6e8d | 2010-05-17 23:56:34 +0000 | [diff] [blame] | 4293 | // gcc just blithely ignores member pointers. |
| 4294 | // TODO: maybe there should be a mangling for these |
| 4295 | if (T->getAs<MemberPointerType>()) |
| 4296 | return; |
Fariborz Jahanian | e0587be | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 4297 | |
| 4298 | if (T->isVectorType()) { |
| 4299 | // This matches gcc's encoding, even though technically it is |
| 4300 | // insufficient. |
| 4301 | // FIXME. We should do a better job than gcc. |
| 4302 | return; |
| 4303 | } |
| 4304 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4305 | assert(0 && "@encode for type not implemented!"); |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4306 | } |
| 4307 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4308 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ac73ff8 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 4309 | std::string& S) const { |
| 4310 | if (QT & Decl::OBJC_TQ_In) |
| 4311 | S += 'n'; |
| 4312 | if (QT & Decl::OBJC_TQ_Inout) |
| 4313 | S += 'N'; |
| 4314 | if (QT & Decl::OBJC_TQ_Out) |
| 4315 | S += 'o'; |
| 4316 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 4317 | S += 'O'; |
| 4318 | if (QT & Decl::OBJC_TQ_Byref) |
| 4319 | S += 'R'; |
| 4320 | if (QT & Decl::OBJC_TQ_Oneway) |
| 4321 | S += 'V'; |
| 4322 | } |
| 4323 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4324 | void ASTContext::setBuiltinVaListType(QualType T) { |
Anders Carlsson | 87c149b | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 4325 | assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4326 | |
Anders Carlsson | 87c149b | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 4327 | BuiltinVaListType = T; |
| 4328 | } |
| 4329 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4330 | void ASTContext::setObjCIdType(QualType T) { |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 4331 | ObjCIdTypedefType = T; |
Steve Naroff | 66e9f33 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 4332 | } |
| 4333 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4334 | void ASTContext::setObjCSelType(QualType T) { |
Fariborz Jahanian | 252ba5f | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 4335 | ObjCSelTypedefType = T; |
Fariborz Jahanian | 4bef462 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 4336 | } |
| 4337 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4338 | void ASTContext::setObjCProtoType(QualType QT) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4339 | ObjCProtoType = QT; |
Fariborz Jahanian | a32aaef | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 4340 | } |
| 4341 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4342 | void ASTContext::setObjCClassType(QualType T) { |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 4343 | ObjCClassTypedefType = T; |
Anders Carlsson | f56a7ae | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 4344 | } |
| 4345 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4346 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4347 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | f73b784 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 4348 | "'NSConstantString' type already set!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4349 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4350 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | f73b784 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 4351 | } |
| 4352 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4353 | /// \brief Retrieve the template name that corresponds to a non-empty |
| 4354 | /// lookup. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4355 | TemplateName |
| 4356 | ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin, |
| 4357 | UnresolvedSetIterator End) const { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4358 | unsigned size = End - Begin; |
| 4359 | assert(size > 1 && "set is not overloaded!"); |
| 4360 | |
| 4361 | void *memory = Allocate(sizeof(OverloadedTemplateStorage) + |
| 4362 | size * sizeof(FunctionTemplateDecl*)); |
| 4363 | OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size); |
| 4364 | |
| 4365 | NamedDecl **Storage = OT->getStorage(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4366 | for (UnresolvedSetIterator I = Begin; I != End; ++I) { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4367 | NamedDecl *D = *I; |
| 4368 | assert(isa<FunctionTemplateDecl>(D) || |
| 4369 | (isa<UsingShadowDecl>(D) && |
| 4370 | isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))); |
| 4371 | *Storage++ = D; |
| 4372 | } |
| 4373 | |
| 4374 | return TemplateName(OT); |
| 4375 | } |
| 4376 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4377 | /// \brief Retrieve the template name that represents a qualified |
| 4378 | /// template name such as \c std::vector. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4379 | TemplateName |
| 4380 | ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
| 4381 | bool TemplateKeyword, |
| 4382 | TemplateDecl *Template) const { |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4383 | // FIXME: Canonicalization? |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4384 | llvm::FoldingSetNodeID ID; |
| 4385 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 4386 | |
| 4387 | void *InsertPos = 0; |
| 4388 | QualifiedTemplateName *QTN = |
| 4389 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4390 | if (!QTN) { |
| 4391 | QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); |
| 4392 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 4393 | } |
| 4394 | |
| 4395 | return TemplateName(QTN); |
| 4396 | } |
| 4397 | |
| 4398 | /// \brief Retrieve the template name that represents a dependent |
| 4399 | /// template name such as \c MetaFun::template apply. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4400 | TemplateName |
| 4401 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
| 4402 | const IdentifierInfo *Name) const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4403 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 308047d | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 4404 | "Nested name specifier must be dependent"); |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4405 | |
| 4406 | llvm::FoldingSetNodeID ID; |
| 4407 | DependentTemplateName::Profile(ID, NNS, Name); |
| 4408 | |
| 4409 | void *InsertPos = 0; |
| 4410 | DependentTemplateName *QTN = |
| 4411 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4412 | |
| 4413 | if (QTN) |
| 4414 | return TemplateName(QTN); |
| 4415 | |
| 4416 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 4417 | if (CanonNNS == NNS) { |
| 4418 | QTN = new (*this,4) DependentTemplateName(NNS, Name); |
| 4419 | } else { |
| 4420 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
| 4421 | QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4422 | DependentTemplateName *CheckQTN = |
| 4423 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4424 | assert(!CheckQTN && "Dependent type name canonicalization broken"); |
| 4425 | (void)CheckQTN; |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4426 | } |
| 4427 | |
| 4428 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 4429 | return TemplateName(QTN); |
| 4430 | } |
| 4431 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4432 | /// \brief Retrieve the template name that represents a dependent |
| 4433 | /// template name such as \c MetaFun::template operator+. |
| 4434 | TemplateName |
| 4435 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4436 | OverloadedOperatorKind Operator) const { |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4437 | assert((!NNS || NNS->isDependent()) && |
| 4438 | "Nested name specifier must be dependent"); |
| 4439 | |
| 4440 | llvm::FoldingSetNodeID ID; |
| 4441 | DependentTemplateName::Profile(ID, NNS, Operator); |
| 4442 | |
| 4443 | void *InsertPos = 0; |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4444 | DependentTemplateName *QTN |
| 4445 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4446 | |
| 4447 | if (QTN) |
| 4448 | return TemplateName(QTN); |
| 4449 | |
| 4450 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 4451 | if (CanonNNS == NNS) { |
| 4452 | QTN = new (*this,4) DependentTemplateName(NNS, Operator); |
| 4453 | } else { |
| 4454 | TemplateName Canon = getDependentTemplateName(CanonNNS, Operator); |
| 4455 | QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4456 | |
| 4457 | DependentTemplateName *CheckQTN |
| 4458 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4459 | assert(!CheckQTN && "Dependent template name canonicalization broken"); |
| 4460 | (void)CheckQTN; |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4461 | } |
| 4462 | |
| 4463 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 4464 | return TemplateName(QTN); |
| 4465 | } |
| 4466 | |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 4467 | TemplateName |
| 4468 | ASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param, |
| 4469 | const TemplateArgument &ArgPack) const { |
| 4470 | ASTContext &Self = const_cast<ASTContext &>(*this); |
| 4471 | llvm::FoldingSetNodeID ID; |
| 4472 | SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack); |
| 4473 | |
| 4474 | void *InsertPos = 0; |
| 4475 | SubstTemplateTemplateParmPackStorage *Subst |
| 4476 | = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos); |
| 4477 | |
| 4478 | if (!Subst) { |
| 4479 | Subst = new (*this) SubstTemplateTemplateParmPackStorage(Self, Param, |
| 4480 | ArgPack.pack_size(), |
| 4481 | ArgPack.pack_begin()); |
| 4482 | SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos); |
| 4483 | } |
| 4484 | |
| 4485 | return TemplateName(Subst); |
| 4486 | } |
| 4487 | |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4488 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | ab13857 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 4489 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 4490 | /// is actually a value of type @c TargetInfo::IntType. |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 4491 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4492 | switch (Type) { |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 4493 | case TargetInfo::NoInt: return CanQualType(); |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4494 | case TargetInfo::SignedShort: return ShortTy; |
| 4495 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 4496 | case TargetInfo::SignedInt: return IntTy; |
| 4497 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 4498 | case TargetInfo::SignedLong: return LongTy; |
| 4499 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 4500 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 4501 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 4502 | } |
| 4503 | |
| 4504 | assert(false && "Unhandled TargetInfo::IntType value"); |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 4505 | return CanQualType(); |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4506 | } |
Ted Kremenek | 77c51b2 | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 4507 | |
| 4508 | //===----------------------------------------------------------------------===// |
| 4509 | // Type Predicates. |
| 4510 | //===----------------------------------------------------------------------===// |
| 4511 | |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4512 | /// isObjCNSObjectType - Return true if this is an NSObject object using |
| 4513 | /// NSObject attribute on a c-style pointer type. |
| 4514 | /// FIXME - Make it work directly on types. |
Steve Naroff | 79d1215 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 4515 | /// FIXME: Move to Type. |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4516 | /// |
| 4517 | bool ASTContext::isObjCNSObjectType(QualType Ty) const { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 4518 | if (const TypedefType *TDT = dyn_cast<TypedefType>(Ty)) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4519 | if (TypedefDecl *TD = TDT->getDecl()) |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 4520 | if (TD->getAttr<ObjCNSObjectAttr>()) |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4521 | return true; |
| 4522 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4523 | return false; |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4524 | } |
| 4525 | |
Fariborz Jahanian | 9620769 | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4526 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 4527 | /// garbage collection attribute. |
| 4528 | /// |
John McCall | 553d45a | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 4529 | Qualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const { |
| 4530 | if (getLangOptions().getGCMode() == LangOptions::NonGC) |
| 4531 | return Qualifiers::GCNone; |
| 4532 | |
| 4533 | assert(getLangOptions().ObjC1); |
| 4534 | Qualifiers::GC GCAttrs = Ty.getObjCGCAttr(); |
| 4535 | |
| 4536 | // Default behaviour under objective-C's gc is for ObjC pointers |
| 4537 | // (or pointers to them) be treated as though they were declared |
| 4538 | // as __strong. |
| 4539 | if (GCAttrs == Qualifiers::GCNone) { |
| 4540 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
| 4541 | return Qualifiers::Strong; |
| 4542 | else if (Ty->isPointerType()) |
| 4543 | return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); |
| 4544 | } else { |
| 4545 | // It's not valid to set GC attributes on anything that isn't a |
| 4546 | // pointer. |
| 4547 | #ifndef NDEBUG |
| 4548 | QualType CT = Ty->getCanonicalTypeInternal(); |
| 4549 | while (const ArrayType *AT = dyn_cast<ArrayType>(CT)) |
| 4550 | CT = AT->getElementType(); |
| 4551 | assert(CT->isAnyPointerType() || CT->isBlockPointerType()); |
| 4552 | #endif |
Fariborz Jahanian | 9620769 | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4553 | } |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 4554 | return GCAttrs; |
Fariborz Jahanian | 9620769 | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4555 | } |
| 4556 | |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4557 | //===----------------------------------------------------------------------===// |
| 4558 | // Type Compatibility Testing |
| 4559 | //===----------------------------------------------------------------------===// |
Chris Lattner | b338a6b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 4560 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4561 | /// areCompatVectorTypes - Return true if the two specified vector types are |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4562 | /// compatible. |
| 4563 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 4564 | const VectorType *RHS) { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 4565 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4566 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4567 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4568 | } |
| 4569 | |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 4570 | bool ASTContext::areCompatibleVectorTypes(QualType FirstVec, |
| 4571 | QualType SecondVec) { |
| 4572 | assert(FirstVec->isVectorType() && "FirstVec should be a vector type"); |
| 4573 | assert(SecondVec->isVectorType() && "SecondVec should be a vector type"); |
| 4574 | |
| 4575 | if (hasSameUnqualifiedType(FirstVec, SecondVec)) |
| 4576 | return true; |
| 4577 | |
Bob Wilson | e6aeebb | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 4578 | // Treat Neon vector types and most AltiVec vector types as if they are the |
| 4579 | // equivalent GCC vector types. |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 4580 | const VectorType *First = FirstVec->getAs<VectorType>(); |
| 4581 | const VectorType *Second = SecondVec->getAs<VectorType>(); |
Bob Wilson | e6aeebb | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 4582 | if (First->getNumElements() == Second->getNumElements() && |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 4583 | hasSameType(First->getElementType(), Second->getElementType()) && |
Bob Wilson | e6aeebb | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 4584 | First->getVectorKind() != VectorType::AltiVecPixel && |
| 4585 | First->getVectorKind() != VectorType::AltiVecBool && |
| 4586 | Second->getVectorKind() != VectorType::AltiVecPixel && |
| 4587 | Second->getVectorKind() != VectorType::AltiVecBool) |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 4588 | return true; |
| 4589 | |
| 4590 | return false; |
| 4591 | } |
| 4592 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4593 | //===----------------------------------------------------------------------===// |
| 4594 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 4595 | //===----------------------------------------------------------------------===// |
| 4596 | |
| 4597 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 4598 | /// inheritance hierarchy of 'rProto'. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4599 | bool |
| 4600 | ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 4601 | ObjCProtocolDecl *rProto) const { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4602 | if (lProto == rProto) |
| 4603 | return true; |
| 4604 | for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), |
| 4605 | E = rProto->protocol_end(); PI != E; ++PI) |
| 4606 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 4607 | return true; |
| 4608 | return false; |
| 4609 | } |
| 4610 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4611 | /// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...> |
| 4612 | /// return true if lhs's protocols conform to rhs's protocol; false |
| 4613 | /// otherwise. |
| 4614 | bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) { |
| 4615 | if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType()) |
| 4616 | return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false); |
| 4617 | return false; |
| 4618 | } |
| 4619 | |
Fariborz Jahanian | 3c7ebc3 | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 4620 | /// ObjCQualifiedClassTypesAreCompatible - compare Class<p,...> and |
| 4621 | /// Class<p1, ...>. |
| 4622 | bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs, |
| 4623 | QualType rhs) { |
| 4624 | const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>(); |
| 4625 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
| 4626 | assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible"); |
| 4627 | |
| 4628 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4629 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4630 | bool match = false; |
| 4631 | ObjCProtocolDecl *lhsProto = *I; |
| 4632 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 4633 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 4634 | ObjCProtocolDecl *rhsProto = *J; |
| 4635 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) { |
| 4636 | match = true; |
| 4637 | break; |
| 4638 | } |
| 4639 | } |
| 4640 | if (!match) |
| 4641 | return false; |
| 4642 | } |
| 4643 | return true; |
| 4644 | } |
| 4645 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4646 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 4647 | /// ObjCQualifiedIDType. |
| 4648 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 4649 | bool compare) { |
| 4650 | // Allow id<P..> and an 'id' or void* type in all cases. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4651 | if (lhs->isVoidPointerType() || |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4652 | lhs->isObjCIdType() || lhs->isObjCClassType()) |
| 4653 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4654 | else if (rhs->isVoidPointerType() || |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4655 | rhs->isObjCIdType() || rhs->isObjCClassType()) |
| 4656 | return true; |
| 4657 | |
| 4658 | if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4659 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4660 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4661 | if (!rhsOPT) return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4662 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4663 | if (rhsOPT->qual_empty()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4664 | // If the RHS is a unqualified interface pointer "NSString*", |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4665 | // make sure we check the class hierarchy. |
| 4666 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 4667 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4668 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4669 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4670 | // see if static class implements all of id's protocols, directly or |
| 4671 | // through its super class and categories. |
Fariborz Jahanian | 3f8917a | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 4672 | if (!rhsID->ClassImplementsProtocol(*I, true)) |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4673 | return false; |
| 4674 | } |
| 4675 | } |
| 4676 | // If there are no qualifiers and no interface, we have an 'id'. |
| 4677 | return true; |
| 4678 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4679 | // Both the right and left sides have qualifiers. |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4680 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4681 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4682 | ObjCProtocolDecl *lhsProto = *I; |
| 4683 | bool match = false; |
| 4684 | |
| 4685 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4686 | // see if static class implements all of id's protocols, directly or |
| 4687 | // through its super class and categories. |
| 4688 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 4689 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 4690 | ObjCProtocolDecl *rhsProto = *J; |
| 4691 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 4692 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 4693 | match = true; |
| 4694 | break; |
| 4695 | } |
| 4696 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4697 | // If the RHS is a qualified interface pointer "NSString<P>*", |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4698 | // make sure we check the class hierarchy. |
| 4699 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 4700 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4701 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4702 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4703 | // see if static class implements all of id's protocols, directly or |
| 4704 | // through its super class and categories. |
Fariborz Jahanian | 3f8917a | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 4705 | if (rhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4706 | match = true; |
| 4707 | break; |
| 4708 | } |
| 4709 | } |
| 4710 | } |
| 4711 | if (!match) |
| 4712 | return false; |
| 4713 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4714 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4715 | return true; |
| 4716 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4717 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4718 | const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 4719 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 4720 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4721 | if (const ObjCObjectPointerType *lhsOPT = |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4722 | lhs->getAsObjCInterfacePointerType()) { |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 4723 | // If both the right and left sides have qualifiers. |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4724 | for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(), |
| 4725 | E = lhsOPT->qual_end(); I != E; ++I) { |
| 4726 | ObjCProtocolDecl *lhsProto = *I; |
| 4727 | bool match = false; |
| 4728 | |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 4729 | // when comparing an id<P> on rhs with a static type on lhs, |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4730 | // see if static class implements all of id's protocols, directly or |
| 4731 | // through its super class and categories. |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 4732 | // First, lhs protocols in the qualifier list must be found, direct |
| 4733 | // or indirect in rhs's qualifier list or it is a mismatch. |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4734 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 4735 | E = rhsQID->qual_end(); J != E; ++J) { |
| 4736 | ObjCProtocolDecl *rhsProto = *J; |
| 4737 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 4738 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 4739 | match = true; |
| 4740 | break; |
| 4741 | } |
| 4742 | } |
| 4743 | if (!match) |
| 4744 | return false; |
| 4745 | } |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 4746 | |
| 4747 | // Static class's protocols, or its super class or category protocols |
| 4748 | // must be found, direct or indirect in rhs's qualifier list or it is a mismatch. |
| 4749 | if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { |
| 4750 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
| 4751 | CollectInheritedProtocols(lhsID, LHSInheritedProtocols); |
| 4752 | // This is rather dubious but matches gcc's behavior. If lhs has |
| 4753 | // no type qualifier and its class has no static protocol(s) |
| 4754 | // assume that it is mismatch. |
| 4755 | if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty()) |
| 4756 | return false; |
| 4757 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 4758 | LHSInheritedProtocols.begin(), |
| 4759 | E = LHSInheritedProtocols.end(); I != E; ++I) { |
| 4760 | bool match = false; |
| 4761 | ObjCProtocolDecl *lhsProto = (*I); |
| 4762 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 4763 | E = rhsQID->qual_end(); J != E; ++J) { |
| 4764 | ObjCProtocolDecl *rhsProto = *J; |
| 4765 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 4766 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 4767 | match = true; |
| 4768 | break; |
| 4769 | } |
| 4770 | } |
| 4771 | if (!match) |
| 4772 | return false; |
| 4773 | } |
| 4774 | } |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4775 | return true; |
| 4776 | } |
| 4777 | return false; |
| 4778 | } |
| 4779 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4780 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4781 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 4782 | /// protocol qualifiers on the LHS or RHS. |
| 4783 | /// |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4784 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 4785 | const ObjCObjectPointerType *RHSOPT) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4786 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 4787 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 4788 | |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 4789 | // If either type represents the built-in 'id' or 'Class' types, return true. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4790 | if (LHS->isObjCUnqualifiedIdOrClass() || |
| 4791 | RHS->isObjCUnqualifiedIdOrClass()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4792 | return true; |
| 4793 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4794 | if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4795 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 4796 | QualType(RHSOPT,0), |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4797 | false); |
Fariborz Jahanian | 3c7ebc3 | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 4798 | |
| 4799 | if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass()) |
| 4800 | return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0), |
| 4801 | QualType(RHSOPT,0)); |
| 4802 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4803 | // If we have 2 user-defined types, fall into that path. |
| 4804 | if (LHS->getInterface() && RHS->getInterface()) |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4805 | return canAssignObjCInterfaces(LHS, RHS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4806 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4807 | return false; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4808 | } |
| 4809 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4810 | /// canAssignObjCInterfacesInBlockPointer - This routine is specifically written |
| 4811 | /// for providing type-safty for objective-c pointers used to pass/return |
| 4812 | /// arguments in block literals. When passed as arguments, passing 'A*' where |
| 4813 | /// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is |
| 4814 | /// not OK. For the return type, the opposite is not OK. |
| 4815 | bool ASTContext::canAssignObjCInterfacesInBlockPointer( |
| 4816 | const ObjCObjectPointerType *LHSOPT, |
| 4817 | const ObjCObjectPointerType *RHSOPT) { |
Fariborz Jahanian | 440a683 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 4818 | if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType()) |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4819 | return true; |
| 4820 | |
| 4821 | if (LHSOPT->isObjCBuiltinType()) { |
| 4822 | return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType(); |
| 4823 | } |
| 4824 | |
Fariborz Jahanian | 440a683 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 4825 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4826 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 4827 | QualType(RHSOPT,0), |
| 4828 | false); |
| 4829 | |
| 4830 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 4831 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 4832 | if (LHS && RHS) { // We have 2 user-defined types. |
| 4833 | if (LHS != RHS) { |
| 4834 | if (LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
| 4835 | return false; |
| 4836 | if (RHS->getDecl()->isSuperClassOf(LHS->getDecl())) |
| 4837 | return true; |
| 4838 | } |
| 4839 | else |
| 4840 | return true; |
| 4841 | } |
| 4842 | return false; |
| 4843 | } |
| 4844 | |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4845 | /// getIntersectionOfProtocols - This routine finds the intersection of set |
| 4846 | /// of protocols inherited from two distinct objective-c pointer objects. |
| 4847 | /// It is used to build composite qualifier list of the composite type of |
| 4848 | /// the conditional expression involving two objective-c pointer objects. |
| 4849 | static |
| 4850 | void getIntersectionOfProtocols(ASTContext &Context, |
| 4851 | const ObjCObjectPointerType *LHSOPT, |
| 4852 | const ObjCObjectPointerType *RHSOPT, |
| 4853 | llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) { |
| 4854 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4855 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 4856 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 4857 | assert(LHS->getInterface() && "LHS must have an interface base"); |
| 4858 | assert(RHS->getInterface() && "RHS must have an interface base"); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4859 | |
| 4860 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet; |
| 4861 | unsigned LHSNumProtocols = LHS->getNumProtocols(); |
| 4862 | if (LHSNumProtocols > 0) |
| 4863 | InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end()); |
| 4864 | else { |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 4865 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4866 | Context.CollectInheritedProtocols(LHS->getInterface(), |
| 4867 | LHSInheritedProtocols); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4868 | InheritedProtocolSet.insert(LHSInheritedProtocols.begin(), |
| 4869 | LHSInheritedProtocols.end()); |
| 4870 | } |
| 4871 | |
| 4872 | unsigned RHSNumProtocols = RHS->getNumProtocols(); |
| 4873 | if (RHSNumProtocols > 0) { |
Dan Gohman | 145f3f1 | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 4874 | ObjCProtocolDecl **RHSProtocols = |
| 4875 | const_cast<ObjCProtocolDecl **>(RHS->qual_begin()); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4876 | for (unsigned i = 0; i < RHSNumProtocols; ++i) |
| 4877 | if (InheritedProtocolSet.count(RHSProtocols[i])) |
| 4878 | IntersectionOfProtocols.push_back(RHSProtocols[i]); |
| 4879 | } |
| 4880 | else { |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 4881 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4882 | Context.CollectInheritedProtocols(RHS->getInterface(), |
| 4883 | RHSInheritedProtocols); |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 4884 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 4885 | RHSInheritedProtocols.begin(), |
| 4886 | E = RHSInheritedProtocols.end(); I != E; ++I) |
| 4887 | if (InheritedProtocolSet.count((*I))) |
| 4888 | IntersectionOfProtocols.push_back((*I)); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4889 | } |
| 4890 | } |
| 4891 | |
Fariborz Jahanian | ef8b8ce | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4892 | /// areCommonBaseCompatible - Returns common base class of the two classes if |
| 4893 | /// one found. Note that this is O'2 algorithm. But it will be called as the |
| 4894 | /// last type comparison in a ?-exp of ObjC pointer types before a |
| 4895 | /// warning is issued. So, its invokation is extremely rare. |
| 4896 | QualType ASTContext::areCommonBaseCompatible( |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4897 | const ObjCObjectPointerType *Lptr, |
| 4898 | const ObjCObjectPointerType *Rptr) { |
| 4899 | const ObjCObjectType *LHS = Lptr->getObjectType(); |
| 4900 | const ObjCObjectType *RHS = Rptr->getObjectType(); |
| 4901 | const ObjCInterfaceDecl* LDecl = LHS->getInterface(); |
| 4902 | const ObjCInterfaceDecl* RDecl = RHS->getInterface(); |
| 4903 | if (!LDecl || !RDecl) |
Fariborz Jahanian | ef8b8ce | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4904 | return QualType(); |
| 4905 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4906 | while ((LDecl = LDecl->getSuperClass())) { |
| 4907 | LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl)); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4908 | if (canAssignObjCInterfaces(LHS, RHS)) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4909 | llvm::SmallVector<ObjCProtocolDecl *, 8> Protocols; |
| 4910 | getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols); |
| 4911 | |
| 4912 | QualType Result = QualType(LHS, 0); |
| 4913 | if (!Protocols.empty()) |
| 4914 | Result = getObjCObjectType(Result, Protocols.data(), Protocols.size()); |
| 4915 | Result = getObjCObjectPointerType(Result); |
| 4916 | return Result; |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4917 | } |
Fariborz Jahanian | ef8b8ce | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4918 | } |
| 4919 | |
| 4920 | return QualType(); |
| 4921 | } |
| 4922 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4923 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS, |
| 4924 | const ObjCObjectType *RHS) { |
| 4925 | assert(LHS->getInterface() && "LHS is not an interface type"); |
| 4926 | assert(RHS->getInterface() && "RHS is not an interface type"); |
| 4927 | |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4928 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 4929 | // the LHS. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4930 | if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface())) |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4931 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4932 | |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4933 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 4934 | // protocol qualified at all, then we are good. |
Steve Naroff | c277ad1 | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4935 | if (LHS->getNumProtocols() == 0) |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4936 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4937 | |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4938 | // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it |
| 4939 | // isn't a superset. |
Steve Naroff | c277ad1 | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4940 | if (RHS->getNumProtocols() == 0) |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4941 | return true; // FIXME: should return false! |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4942 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4943 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 4944 | LHSPE = LHS->qual_end(); |
Steve Naroff | 114aecb | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4945 | LHSPI != LHSPE; LHSPI++) { |
| 4946 | bool RHSImplementsProtocol = false; |
| 4947 | |
| 4948 | // If the RHS doesn't implement the protocol on the left, the types |
| 4949 | // are incompatible. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4950 | for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(), |
| 4951 | RHSPE = RHS->qual_end(); |
Steve Naroff | fac5bc9 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 4952 | RHSPI != RHSPE; RHSPI++) { |
| 4953 | if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) { |
Steve Naroff | 114aecb | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4954 | RHSImplementsProtocol = true; |
Steve Naroff | fac5bc9 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 4955 | break; |
| 4956 | } |
Steve Naroff | 114aecb | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4957 | } |
| 4958 | // FIXME: For better diagnostics, consider passing back the protocol name. |
| 4959 | if (!RHSImplementsProtocol) |
| 4960 | return false; |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4961 | } |
Steve Naroff | 114aecb | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4962 | // The RHS implements all protocols listed on the LHS. |
| 4963 | return true; |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4964 | } |
| 4965 | |
Steve Naroff | b760515 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4966 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 4967 | // get the "pointed to" types |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4968 | const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
| 4969 | const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4970 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4971 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | b760515 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4972 | return false; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4973 | |
| 4974 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 4975 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | b760515 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4976 | } |
| 4977 | |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4978 | bool ASTContext::canBindObjCObjectType(QualType To, QualType From) { |
| 4979 | return canAssignObjCInterfaces( |
| 4980 | getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(), |
| 4981 | getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>()); |
| 4982 | } |
| 4983 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4984 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4985 | /// both shall have the identically qualified version of a compatible type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4986 | /// C99 6.2.7p1: Two types have compatible types if their types are the |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4987 | /// same. See 6.7.[2,3,5] for additional rules. |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4988 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS, |
| 4989 | bool CompareUnqualified) { |
Douglas Gregor | 21e771e | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 4990 | if (getLangOptions().CPlusPlus) |
| 4991 | return hasSameType(LHS, RHS); |
| 4992 | |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4993 | return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull(); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4994 | } |
| 4995 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4996 | bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) { |
| 4997 | return !mergeTypes(LHS, RHS, true).isNull(); |
| 4998 | } |
| 4999 | |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 5000 | /// mergeTransparentUnionType - if T is a transparent union type and a member |
| 5001 | /// of T is compatible with SubType, return the merged type, else return |
| 5002 | /// QualType() |
| 5003 | QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType, |
| 5004 | bool OfBlockPointer, |
| 5005 | bool Unqualified) { |
| 5006 | if (const RecordType *UT = T->getAsUnionType()) { |
| 5007 | RecordDecl *UD = UT->getDecl(); |
| 5008 | if (UD->hasAttr<TransparentUnionAttr>()) { |
| 5009 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 5010 | itend = UD->field_end(); it != itend; ++it) { |
Peter Collingbourne | 19b961d | 2010-12-02 21:00:06 +0000 | [diff] [blame] | 5011 | QualType ET = it->getType().getUnqualifiedType(); |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 5012 | QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified); |
| 5013 | if (!MT.isNull()) |
| 5014 | return MT; |
| 5015 | } |
| 5016 | } |
| 5017 | } |
| 5018 | |
| 5019 | return QualType(); |
| 5020 | } |
| 5021 | |
| 5022 | /// mergeFunctionArgumentTypes - merge two types which appear as function |
| 5023 | /// argument types |
| 5024 | QualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs, |
| 5025 | bool OfBlockPointer, |
| 5026 | bool Unqualified) { |
| 5027 | // GNU extension: two types are compatible if they appear as a function |
| 5028 | // argument, one of the types is a transparent union type and the other |
| 5029 | // type is compatible with a union member |
| 5030 | QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer, |
| 5031 | Unqualified); |
| 5032 | if (!lmerge.isNull()) |
| 5033 | return lmerge; |
| 5034 | |
| 5035 | QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer, |
| 5036 | Unqualified); |
| 5037 | if (!rmerge.isNull()) |
| 5038 | return rmerge; |
| 5039 | |
| 5040 | return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified); |
| 5041 | } |
| 5042 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5043 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs, |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5044 | bool OfBlockPointer, |
| 5045 | bool Unqualified) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5046 | const FunctionType *lbase = lhs->getAs<FunctionType>(); |
| 5047 | const FunctionType *rbase = rhs->getAs<FunctionType>(); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5048 | const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 5049 | const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5050 | bool allLTypes = true; |
| 5051 | bool allRTypes = true; |
| 5052 | |
| 5053 | // Check return type |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5054 | QualType retType; |
Fariborz Jahanian | 1782597 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 5055 | if (OfBlockPointer) { |
| 5056 | QualType RHS = rbase->getResultType(); |
| 5057 | QualType LHS = lbase->getResultType(); |
| 5058 | bool UnqualifiedResult = Unqualified; |
| 5059 | if (!UnqualifiedResult) |
| 5060 | UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers()); |
| 5061 | retType = mergeTypes(RHS, LHS, true, UnqualifiedResult); |
| 5062 | } |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5063 | else |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5064 | retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), false, |
| 5065 | Unqualified); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5066 | if (retType.isNull()) return QualType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5067 | |
| 5068 | if (Unqualified) |
| 5069 | retType = retType.getUnqualifiedType(); |
| 5070 | |
| 5071 | CanQualType LRetType = getCanonicalType(lbase->getResultType()); |
| 5072 | CanQualType RRetType = getCanonicalType(rbase->getResultType()); |
| 5073 | if (Unqualified) { |
| 5074 | LRetType = LRetType.getUnqualifiedType(); |
| 5075 | RRetType = RRetType.getUnqualifiedType(); |
| 5076 | } |
| 5077 | |
| 5078 | if (getCanonicalType(retType) != LRetType) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5079 | allLTypes = false; |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5080 | if (getCanonicalType(retType) != RRetType) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5081 | allRTypes = false; |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5082 | |
Daniel Dunbar | edd5bae | 2010-04-28 16:20:58 +0000 | [diff] [blame] | 5083 | // FIXME: double check this |
| 5084 | // FIXME: should we error if lbase->getRegParmAttr() != 0 && |
| 5085 | // rbase->getRegParmAttr() != 0 && |
| 5086 | // lbase->getRegParmAttr() != rbase->getRegParmAttr()? |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 5087 | FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo(); |
| 5088 | FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo(); |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5089 | |
Douglas Gregor | 8c94086 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 5090 | // Compatible functions must have compatible calling conventions |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5091 | if (!isSameCallConv(lbaseInfo.getCC(), rbaseInfo.getCC())) |
Douglas Gregor | 8c94086 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 5092 | return QualType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5093 | |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5094 | // Regparm is part of the calling convention. |
| 5095 | if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm()) |
| 5096 | return QualType(); |
| 5097 | |
| 5098 | // It's noreturn if either type is. |
| 5099 | // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'. |
| 5100 | bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn(); |
| 5101 | if (NoReturn != lbaseInfo.getNoReturn()) |
| 5102 | allLTypes = false; |
| 5103 | if (NoReturn != rbaseInfo.getNoReturn()) |
| 5104 | allRTypes = false; |
| 5105 | |
| 5106 | FunctionType::ExtInfo einfo(NoReturn, |
| 5107 | lbaseInfo.getRegParm(), |
| 5108 | lbaseInfo.getCC()); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5109 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5110 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 5111 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 5112 | "C++ shouldn't be here"); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5113 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 5114 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 5115 | |
| 5116 | // Compatible functions must have the same number of arguments |
| 5117 | if (lproto_nargs != rproto_nargs) |
| 5118 | return QualType(); |
| 5119 | |
| 5120 | // Variadic and non-variadic functions aren't compatible |
| 5121 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 5122 | return QualType(); |
| 5123 | |
Argyrios Kyrtzidis | 22a3735 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 5124 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 5125 | return QualType(); |
| 5126 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5127 | // Check argument compatibility |
| 5128 | llvm::SmallVector<QualType, 10> types; |
| 5129 | for (unsigned i = 0; i < lproto_nargs; i++) { |
| 5130 | QualType largtype = lproto->getArgType(i).getUnqualifiedType(); |
| 5131 | QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 5132 | QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype, |
| 5133 | OfBlockPointer, |
| 5134 | Unqualified); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5135 | if (argtype.isNull()) return QualType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5136 | |
| 5137 | if (Unqualified) |
| 5138 | argtype = argtype.getUnqualifiedType(); |
| 5139 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5140 | types.push_back(argtype); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5141 | if (Unqualified) { |
| 5142 | largtype = largtype.getUnqualifiedType(); |
| 5143 | rargtype = rargtype.getUnqualifiedType(); |
| 5144 | } |
| 5145 | |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5146 | if (getCanonicalType(argtype) != getCanonicalType(largtype)) |
| 5147 | allLTypes = false; |
| 5148 | if (getCanonicalType(argtype) != getCanonicalType(rargtype)) |
| 5149 | allRTypes = false; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5150 | } |
| 5151 | if (allLTypes) return lhs; |
| 5152 | if (allRTypes) return rhs; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5153 | |
| 5154 | FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo(); |
| 5155 | EPI.ExtInfo = einfo; |
| 5156 | return getFunctionType(retType, types.begin(), types.size(), EPI); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5157 | } |
| 5158 | |
| 5159 | if (lproto) allRTypes = false; |
| 5160 | if (rproto) allLTypes = false; |
| 5161 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5162 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5163 | if (proto) { |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 5164 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5165 | if (proto->isVariadic()) return QualType(); |
| 5166 | // Check that the types are compatible with the types that |
| 5167 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 5168 | // The only types actually affected are promotable integer |
| 5169 | // types and floats, which would be passed as a different |
| 5170 | // type depending on whether the prototype is visible. |
| 5171 | unsigned proto_nargs = proto->getNumArgs(); |
| 5172 | for (unsigned i = 0; i < proto_nargs; ++i) { |
| 5173 | QualType argTy = proto->getArgType(i); |
Douglas Gregor | 2973d40 | 2010-02-03 19:27:29 +0000 | [diff] [blame] | 5174 | |
| 5175 | // Look at the promotion type of enum types, since that is the type used |
| 5176 | // to pass enum values. |
| 5177 | if (const EnumType *Enum = argTy->getAs<EnumType>()) |
| 5178 | argTy = Enum->getDecl()->getPromotionType(); |
| 5179 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5180 | if (argTy->isPromotableIntegerType() || |
| 5181 | getCanonicalType(argTy).getUnqualifiedType() == FloatTy) |
| 5182 | return QualType(); |
| 5183 | } |
| 5184 | |
| 5185 | if (allLTypes) return lhs; |
| 5186 | if (allRTypes) return rhs; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5187 | |
| 5188 | FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo(); |
| 5189 | EPI.ExtInfo = einfo; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5190 | return getFunctionType(retType, proto->arg_type_begin(), |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5191 | proto->getNumArgs(), EPI); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5192 | } |
| 5193 | |
| 5194 | if (allLTypes) return lhs; |
| 5195 | if (allRTypes) return rhs; |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5196 | return getFunctionNoProtoType(retType, einfo); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5197 | } |
| 5198 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5199 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5200 | bool OfBlockPointer, |
| 5201 | bool Unqualified) { |
Bill Wendling | db4e349 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 5202 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 5203 | // type is adjusted to "T" prior to any further analysis, the expression |
| 5204 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 5205 | // expression is an lvalue unless the reference is an rvalue reference and |
| 5206 | // the expression is a function call (possibly inside parentheses). |
Douglas Gregor | 21e771e | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 5207 | assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?"); |
| 5208 | assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?"); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5209 | |
| 5210 | if (Unqualified) { |
| 5211 | LHS = LHS.getUnqualifiedType(); |
| 5212 | RHS = RHS.getUnqualifiedType(); |
| 5213 | } |
Douglas Gregor | 21e771e | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 5214 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5215 | QualType LHSCan = getCanonicalType(LHS), |
| 5216 | RHSCan = getCanonicalType(RHS); |
| 5217 | |
| 5218 | // If two types are identical, they are compatible. |
| 5219 | if (LHSCan == RHSCan) |
| 5220 | return LHS; |
| 5221 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5222 | // If the qualifiers are different, the types aren't compatible... mostly. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 5223 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 5224 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5225 | if (LQuals != RQuals) { |
| 5226 | // If any of these qualifiers are different, we have a type |
| 5227 | // mismatch. |
| 5228 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 5229 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 5230 | return QualType(); |
| 5231 | |
| 5232 | // Exactly one GC qualifier difference is allowed: __strong is |
| 5233 | // okay if the other type has no GC qualifier but is an Objective |
| 5234 | // C object pointer (i.e. implicitly strong by default). We fix |
| 5235 | // this by pretending that the unqualified type was actually |
| 5236 | // qualified __strong. |
| 5237 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 5238 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 5239 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 5240 | |
| 5241 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 5242 | return QualType(); |
| 5243 | |
| 5244 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
| 5245 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); |
| 5246 | } |
| 5247 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
| 5248 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); |
| 5249 | } |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5250 | return QualType(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5251 | } |
| 5252 | |
| 5253 | // Okay, qualifiers are equal. |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5254 | |
Eli Friedman | dcca633 | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 5255 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 5256 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5257 | |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5258 | // We want to consider the two function types to be the same for these |
| 5259 | // comparisons, just force one to the other. |
| 5260 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 5261 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 16f9096 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 5262 | |
| 5263 | // Same as above for arrays |
Chris Lattner | 9555466 | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 5264 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 5265 | LHSClass = Type::ConstantArray; |
| 5266 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 5267 | RHSClass = Type::ConstantArray; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5268 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5269 | // ObjCInterfaces are just specialized ObjCObjects. |
| 5270 | if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject; |
| 5271 | if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject; |
| 5272 | |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 5273 | // Canonicalize ExtVector -> Vector. |
| 5274 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 5275 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5276 | |
Chris Lattner | 9555466 | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 5277 | // If the canonical type classes don't match. |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5278 | if (LHSClass != RHSClass) { |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5279 | // C99 6.7.2.2p4: Each enumerated type shall be compatible with char, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5280 | // a signed integer type, or an unsigned integer type. |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 5281 | // Compatibility is based on the underlying type, not the promotion |
| 5282 | // type. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5283 | if (const EnumType* ETy = LHS->getAs<EnumType>()) { |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5284 | if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType()) |
| 5285 | return RHS; |
Eli Friedman | a7bf7ed | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 5286 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5287 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5288 | if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType()) |
| 5289 | return LHS; |
Eli Friedman | a7bf7ed | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 5290 | } |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5291 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5292 | return QualType(); |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5293 | } |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5294 | |
Steve Naroff | c6edcbd | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 5295 | // The canonical type classes match. |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5296 | switch (LHSClass) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5297 | #define TYPE(Class, Base) |
| 5298 | #define ABSTRACT_TYPE(Class, Base) |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 5299 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5300 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 5301 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 5302 | #include "clang/AST/TypeNodes.def" |
| 5303 | assert(false && "Non-canonical and dependent types shouldn't get here"); |
| 5304 | return QualType(); |
| 5305 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 5306 | case Type::LValueReference: |
| 5307 | case Type::RValueReference: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5308 | case Type::MemberPointer: |
| 5309 | assert(false && "C++ should never be in mergeTypes"); |
| 5310 | return QualType(); |
| 5311 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5312 | case Type::ObjCInterface: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5313 | case Type::IncompleteArray: |
| 5314 | case Type::VariableArray: |
| 5315 | case Type::FunctionProto: |
| 5316 | case Type::ExtVector: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5317 | assert(false && "Types are eliminated above"); |
| 5318 | return QualType(); |
| 5319 | |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5320 | case Type::Pointer: |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5321 | { |
| 5322 | // Merge two pointer types, while trying to preserve typedef info |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5323 | QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); |
| 5324 | QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5325 | if (Unqualified) { |
| 5326 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 5327 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 5328 | } |
| 5329 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false, |
| 5330 | Unqualified); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5331 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | 091a9ac | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 5332 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5333 | return LHS; |
Eli Friedman | 091a9ac | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 5334 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5335 | return RHS; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5336 | return getPointerType(ResultType); |
| 5337 | } |
Steve Naroff | 68e167d | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 5338 | case Type::BlockPointer: |
| 5339 | { |
| 5340 | // Merge two block pointer types, while trying to preserve typedef info |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5341 | QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); |
| 5342 | QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5343 | if (Unqualified) { |
| 5344 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 5345 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 5346 | } |
| 5347 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer, |
| 5348 | Unqualified); |
Steve Naroff | 68e167d | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 5349 | if (ResultType.isNull()) return QualType(); |
| 5350 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 5351 | return LHS; |
| 5352 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 5353 | return RHS; |
| 5354 | return getBlockPointerType(ResultType); |
| 5355 | } |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5356 | case Type::ConstantArray: |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5357 | { |
| 5358 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 5359 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 5360 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 5361 | return QualType(); |
| 5362 | |
| 5363 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 5364 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5365 | if (Unqualified) { |
| 5366 | LHSElem = LHSElem.getUnqualifiedType(); |
| 5367 | RHSElem = RHSElem.getUnqualifiedType(); |
| 5368 | } |
| 5369 | |
| 5370 | QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5371 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5372 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 5373 | return LHS; |
| 5374 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 5375 | return RHS; |
Eli Friedman | 3e62c21 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 5376 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 5377 | ArrayType::ArraySizeModifier(), 0); |
| 5378 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 5379 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5380 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 5381 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5382 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 5383 | return LHS; |
| 5384 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 5385 | return RHS; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5386 | if (LVAT) { |
| 5387 | // FIXME: This isn't correct! But tricky to implement because |
| 5388 | // the array's size has to be the size of LHS, but the type |
| 5389 | // has to be different. |
| 5390 | return LHS; |
| 5391 | } |
| 5392 | if (RVAT) { |
| 5393 | // FIXME: This isn't correct! But tricky to implement because |
| 5394 | // the array's size has to be the size of RHS, but the type |
| 5395 | // has to be different. |
| 5396 | return RHS; |
| 5397 | } |
Eli Friedman | 3e62c21 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 5398 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 5399 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 5400 | return getIncompleteArrayType(ResultType, |
| 5401 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5402 | } |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5403 | case Type::FunctionNoProto: |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5404 | return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5405 | case Type::Record: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5406 | case Type::Enum: |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5407 | return QualType(); |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5408 | case Type::Builtin: |
Chris Lattner | 7bbd3d7 | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 5409 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5410 | return QualType(); |
Daniel Dunbar | 804c044 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 5411 | case Type::Complex: |
| 5412 | // Distinct complex types are incompatible. |
| 5413 | return QualType(); |
Chris Lattner | 7bbd3d7 | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 5414 | case Type::Vector: |
Eli Friedman | cad9638 | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 5415 | // FIXME: The merged type should be an ExtVector! |
John McCall | 44c064b | 2010-03-12 23:14:13 +0000 | [diff] [blame] | 5416 | if (areCompatVectorTypes(LHSCan->getAs<VectorType>(), |
| 5417 | RHSCan->getAs<VectorType>())) |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5418 | return LHS; |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5419 | return QualType(); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5420 | case Type::ObjCObject: { |
| 5421 | // Check if the types are assignment compatible. |
Eli Friedman | cad9638 | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 5422 | // FIXME: This should be type compatibility, e.g. whether |
| 5423 | // "LHS x; RHS x;" at global scope is legal. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5424 | const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>(); |
| 5425 | const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>(); |
| 5426 | if (canAssignObjCInterfaces(LHSIface, RHSIface)) |
Steve Naroff | 7a7814c | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 5427 | return LHS; |
| 5428 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5429 | return QualType(); |
Cedric Venet | 4fc88b7 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 5430 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5431 | case Type::ObjCObjectPointer: { |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5432 | if (OfBlockPointer) { |
| 5433 | if (canAssignObjCInterfacesInBlockPointer( |
| 5434 | LHS->getAs<ObjCObjectPointerType>(), |
| 5435 | RHS->getAs<ObjCObjectPointerType>())) |
| 5436 | return LHS; |
| 5437 | return QualType(); |
| 5438 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5439 | if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(), |
| 5440 | RHS->getAs<ObjCObjectPointerType>())) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5441 | return LHS; |
| 5442 | |
Steve Naroff | c68cfcf | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 5443 | return QualType(); |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5444 | } |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5445 | } |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5446 | |
| 5447 | return QualType(); |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5448 | } |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 5449 | |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 5450 | /// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and |
| 5451 | /// 'RHS' attributes and returns the merged version; including for function |
| 5452 | /// return types. |
| 5453 | QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) { |
| 5454 | QualType LHSCan = getCanonicalType(LHS), |
| 5455 | RHSCan = getCanonicalType(RHS); |
| 5456 | // If two types are identical, they are compatible. |
| 5457 | if (LHSCan == RHSCan) |
| 5458 | return LHS; |
| 5459 | if (RHSCan->isFunctionType()) { |
| 5460 | if (!LHSCan->isFunctionType()) |
| 5461 | return QualType(); |
| 5462 | QualType OldReturnType = |
| 5463 | cast<FunctionType>(RHSCan.getTypePtr())->getResultType(); |
| 5464 | QualType NewReturnType = |
| 5465 | cast<FunctionType>(LHSCan.getTypePtr())->getResultType(); |
| 5466 | QualType ResReturnType = |
| 5467 | mergeObjCGCQualifiers(NewReturnType, OldReturnType); |
| 5468 | if (ResReturnType.isNull()) |
| 5469 | return QualType(); |
| 5470 | if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) { |
| 5471 | // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo(); |
| 5472 | // In either case, use OldReturnType to build the new function type. |
| 5473 | const FunctionType *F = LHS->getAs<FunctionType>(); |
| 5474 | if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) { |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5475 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 5476 | EPI.ExtInfo = getFunctionExtInfo(LHS); |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 5477 | QualType ResultType |
| 5478 | = getFunctionType(OldReturnType, FPT->arg_type_begin(), |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5479 | FPT->getNumArgs(), EPI); |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 5480 | return ResultType; |
| 5481 | } |
| 5482 | } |
| 5483 | return QualType(); |
| 5484 | } |
| 5485 | |
| 5486 | // If the qualifiers are different, the types can still be merged. |
| 5487 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 5488 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
| 5489 | if (LQuals != RQuals) { |
| 5490 | // If any of these qualifiers are different, we have a type mismatch. |
| 5491 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 5492 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 5493 | return QualType(); |
| 5494 | |
| 5495 | // Exactly one GC qualifier difference is allowed: __strong is |
| 5496 | // okay if the other type has no GC qualifier but is an Objective |
| 5497 | // C object pointer (i.e. implicitly strong by default). We fix |
| 5498 | // this by pretending that the unqualified type was actually |
| 5499 | // qualified __strong. |
| 5500 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 5501 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 5502 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 5503 | |
| 5504 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 5505 | return QualType(); |
| 5506 | |
| 5507 | if (GC_L == Qualifiers::Strong) |
| 5508 | return LHS; |
| 5509 | if (GC_R == Qualifiers::Strong) |
| 5510 | return RHS; |
| 5511 | return QualType(); |
| 5512 | } |
| 5513 | |
| 5514 | if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) { |
| 5515 | QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5516 | QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5517 | QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT); |
| 5518 | if (ResQT == LHSBaseQT) |
| 5519 | return LHS; |
| 5520 | if (ResQT == RHSBaseQT) |
| 5521 | return RHS; |
| 5522 | } |
| 5523 | return QualType(); |
| 5524 | } |
| 5525 | |
Chris Lattner | 4ba0cef | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 5526 | //===----------------------------------------------------------------------===// |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5527 | // Integer Predicates |
| 5528 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3c91971 | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 5529 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5530 | unsigned ASTContext::getIntWidth(QualType T) const { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 5531 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
Eli Friedman | ee275c8 | 2009-12-10 22:29:29 +0000 | [diff] [blame] | 5532 | T = ET->getDecl()->getIntegerType(); |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 5533 | if (T->isBooleanType()) |
| 5534 | return 1; |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 5535 | // For builtin types, just use the standard type sizing method |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5536 | return (unsigned)getTypeSize(T); |
| 5537 | } |
| 5538 | |
| 5539 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) { |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5540 | assert(T->hasSignedIntegerRepresentation() && "Unexpected type"); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5541 | |
| 5542 | // Turn <4 x signed int> -> <4 x unsigned int> |
| 5543 | if (const VectorType *VTy = T->getAs<VectorType>()) |
| 5544 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 5545 | VTy->getNumElements(), VTy->getVectorKind()); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5546 | |
| 5547 | // For enums, we return the unsigned version of the base type. |
| 5548 | if (const EnumType *ETy = T->getAs<EnumType>()) |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5549 | T = ETy->getDecl()->getIntegerType(); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5550 | |
| 5551 | const BuiltinType *BTy = T->getAs<BuiltinType>(); |
| 5552 | assert(BTy && "Unexpected signed integer type"); |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5553 | switch (BTy->getKind()) { |
| 5554 | case BuiltinType::Char_S: |
| 5555 | case BuiltinType::SChar: |
| 5556 | return UnsignedCharTy; |
| 5557 | case BuiltinType::Short: |
| 5558 | return UnsignedShortTy; |
| 5559 | case BuiltinType::Int: |
| 5560 | return UnsignedIntTy; |
| 5561 | case BuiltinType::Long: |
| 5562 | return UnsignedLongTy; |
| 5563 | case BuiltinType::LongLong: |
| 5564 | return UnsignedLongLongTy; |
Chris Lattner | f122cef | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 5565 | case BuiltinType::Int128: |
| 5566 | return UnsignedInt128Ty; |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5567 | default: |
| 5568 | assert(0 && "Unexpected signed integer type"); |
| 5569 | return QualType(); |
| 5570 | } |
| 5571 | } |
| 5572 | |
Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 5573 | ASTMutationListener::~ASTMutationListener() { } |
| 5574 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5575 | |
| 5576 | //===----------------------------------------------------------------------===// |
| 5577 | // Builtin Type Computation |
| 5578 | //===----------------------------------------------------------------------===// |
| 5579 | |
| 5580 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5581 | /// pointer over the consumed characters. This returns the resultant type. If |
| 5582 | /// AllowTypeModifiers is false then modifier like * are not parsed, just basic |
| 5583 | /// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of |
| 5584 | /// a vector of "i*". |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5585 | /// |
| 5586 | /// RequiresICE is filled in on return to indicate whether the value is required |
| 5587 | /// to be an Integer Constant Expression. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5588 | static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context, |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5589 | ASTContext::GetBuiltinTypeError &Error, |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5590 | bool &RequiresICE, |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5591 | bool AllowTypeModifiers) { |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5592 | // Modifiers. |
| 5593 | int HowLong = 0; |
| 5594 | bool Signed = false, Unsigned = false; |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5595 | RequiresICE = false; |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 5596 | |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5597 | // Read the prefixed modifiers first. |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5598 | bool Done = false; |
| 5599 | while (!Done) { |
| 5600 | switch (*Str++) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5601 | default: Done = true; --Str; break; |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 5602 | case 'I': |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5603 | RequiresICE = true; |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 5604 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5605 | case 'S': |
| 5606 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 5607 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 5608 | Signed = true; |
| 5609 | break; |
| 5610 | case 'U': |
| 5611 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
| 5612 | assert(!Unsigned && "Can't use 'S' modifier multiple times!"); |
| 5613 | Unsigned = true; |
| 5614 | break; |
| 5615 | case 'L': |
| 5616 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 5617 | ++HowLong; |
| 5618 | break; |
| 5619 | } |
| 5620 | } |
| 5621 | |
| 5622 | QualType Type; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5623 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5624 | // Read the base type. |
| 5625 | switch (*Str++) { |
| 5626 | default: assert(0 && "Unknown builtin type letter!"); |
| 5627 | case 'v': |
| 5628 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 5629 | "Bad modifiers used with 'v'!"); |
| 5630 | Type = Context.VoidTy; |
| 5631 | break; |
| 5632 | case 'f': |
| 5633 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 5634 | "Bad modifiers used with 'f'!"); |
| 5635 | Type = Context.FloatTy; |
| 5636 | break; |
| 5637 | case 'd': |
| 5638 | assert(HowLong < 2 && !Signed && !Unsigned && |
| 5639 | "Bad modifiers used with 'd'!"); |
| 5640 | if (HowLong) |
| 5641 | Type = Context.LongDoubleTy; |
| 5642 | else |
| 5643 | Type = Context.DoubleTy; |
| 5644 | break; |
| 5645 | case 's': |
| 5646 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 5647 | if (Unsigned) |
| 5648 | Type = Context.UnsignedShortTy; |
| 5649 | else |
| 5650 | Type = Context.ShortTy; |
| 5651 | break; |
| 5652 | case 'i': |
| 5653 | if (HowLong == 3) |
| 5654 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 5655 | else if (HowLong == 2) |
| 5656 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 5657 | else if (HowLong == 1) |
| 5658 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 5659 | else |
| 5660 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 5661 | break; |
| 5662 | case 'c': |
| 5663 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 5664 | if (Signed) |
| 5665 | Type = Context.SignedCharTy; |
| 5666 | else if (Unsigned) |
| 5667 | Type = Context.UnsignedCharTy; |
| 5668 | else |
| 5669 | Type = Context.CharTy; |
| 5670 | break; |
| 5671 | case 'b': // boolean |
| 5672 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 5673 | Type = Context.BoolTy; |
| 5674 | break; |
| 5675 | case 'z': // size_t. |
| 5676 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 5677 | Type = Context.getSizeType(); |
| 5678 | break; |
| 5679 | case 'F': |
| 5680 | Type = Context.getCFConstantStringType(); |
| 5681 | break; |
Fariborz Jahanian | d11da7e | 2010-11-09 21:38:20 +0000 | [diff] [blame] | 5682 | case 'G': |
| 5683 | Type = Context.getObjCIdType(); |
| 5684 | break; |
| 5685 | case 'H': |
| 5686 | Type = Context.getObjCSelType(); |
| 5687 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5688 | case 'a': |
| 5689 | Type = Context.getBuiltinVaListType(); |
| 5690 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 5691 | break; |
| 5692 | case 'A': |
| 5693 | // This is a "reference" to a va_list; however, what exactly |
| 5694 | // this means depends on how va_list is defined. There are two |
| 5695 | // different kinds of va_list: ones passed by value, and ones |
| 5696 | // passed by reference. An example of a by-value va_list is |
| 5697 | // x86, where va_list is a char*. An example of by-ref va_list |
| 5698 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 5699 | // we want this argument to be a char*&; for x86-64, we want |
| 5700 | // it to be a __va_list_tag*. |
| 5701 | Type = Context.getBuiltinVaListType(); |
| 5702 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5703 | if (Type->isArrayType()) |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5704 | Type = Context.getArrayDecayedType(Type); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5705 | else |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5706 | Type = Context.getLValueReferenceType(Type); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5707 | break; |
| 5708 | case 'V': { |
| 5709 | char *End; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5710 | unsigned NumElements = strtoul(Str, &End, 10); |
| 5711 | assert(End != Str && "Missing vector size"); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5712 | Str = End; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5713 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5714 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, |
| 5715 | RequiresICE, false); |
| 5716 | assert(!RequiresICE && "Can't require vector ICE"); |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5717 | |
| 5718 | // TODO: No way to make AltiVec vectors in builtins yet. |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 5719 | Type = Context.getVectorType(ElementType, NumElements, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 5720 | VectorType::GenericVector); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5721 | break; |
| 5722 | } |
Douglas Gregor | 40ef7c5 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 5723 | case 'X': { |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5724 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
| 5725 | false); |
| 5726 | assert(!RequiresICE && "Can't require complex ICE"); |
Douglas Gregor | 40ef7c5 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 5727 | Type = Context.getComplexType(ElementType); |
| 5728 | break; |
| 5729 | } |
Chris Lattner | a58b3af | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 5730 | case 'P': |
Douglas Gregor | 27821ce | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 5731 | Type = Context.getFILEType(); |
| 5732 | if (Type.isNull()) { |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 5733 | Error = ASTContext::GE_Missing_stdio; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5734 | return QualType(); |
| 5735 | } |
Mike Stump | 2adb4da | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 5736 | break; |
Chris Lattner | a58b3af | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 5737 | case 'J': |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 5738 | if (Signed) |
Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 5739 | Type = Context.getsigjmp_bufType(); |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 5740 | else |
| 5741 | Type = Context.getjmp_bufType(); |
| 5742 | |
Mike Stump | 2adb4da | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 5743 | if (Type.isNull()) { |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 5744 | Error = ASTContext::GE_Missing_setjmp; |
Mike Stump | 2adb4da | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 5745 | return QualType(); |
| 5746 | } |
| 5747 | break; |
Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 5748 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5749 | |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5750 | // If there are modifiers and if we're allowed to parse them, go for it. |
| 5751 | Done = !AllowTypeModifiers; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5752 | while (!Done) { |
John McCall | b8b9466 | 2010-03-12 04:21:28 +0000 | [diff] [blame] | 5753 | switch (char c = *Str++) { |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5754 | default: Done = true; --Str; break; |
| 5755 | case '*': |
| 5756 | case '&': { |
| 5757 | // Both pointers and references can have their pointee types |
| 5758 | // qualified with an address space. |
| 5759 | char *End; |
| 5760 | unsigned AddrSpace = strtoul(Str, &End, 10); |
| 5761 | if (End != Str && AddrSpace != 0) { |
| 5762 | Type = Context.getAddrSpaceQualType(Type, AddrSpace); |
| 5763 | Str = End; |
| 5764 | } |
| 5765 | if (c == '*') |
| 5766 | Type = Context.getPointerType(Type); |
| 5767 | else |
| 5768 | Type = Context.getLValueReferenceType(Type); |
| 5769 | break; |
| 5770 | } |
| 5771 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 5772 | case 'C': |
| 5773 | Type = Type.withConst(); |
| 5774 | break; |
| 5775 | case 'D': |
| 5776 | Type = Context.getVolatileType(Type); |
| 5777 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5778 | } |
| 5779 | } |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 5780 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5781 | assert((!RequiresICE || Type->isIntegralOrEnumerationType()) && |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 5782 | "Integer constant 'I' type must be an integer"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5783 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5784 | return Type; |
| 5785 | } |
| 5786 | |
| 5787 | /// GetBuiltinType - Return the type for the specified builtin. |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5788 | QualType ASTContext::GetBuiltinType(unsigned Id, |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5789 | GetBuiltinTypeError &Error, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5790 | unsigned *IntegerConstantArgs) const { |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5791 | const char *TypeStr = BuiltinInfo.GetTypeString(Id); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5792 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5793 | llvm::SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5794 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5795 | bool RequiresICE = false; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5796 | Error = GE_None; |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5797 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error, |
| 5798 | RequiresICE, true); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5799 | if (Error != GE_None) |
| 5800 | return QualType(); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5801 | |
| 5802 | assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE"); |
| 5803 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5804 | while (TypeStr[0] && TypeStr[0] != '.') { |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5805 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5806 | if (Error != GE_None) |
| 5807 | return QualType(); |
| 5808 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5809 | // If this argument is required to be an IntegerConstantExpression and the |
| 5810 | // caller cares, fill in the bitmask we return. |
| 5811 | if (RequiresICE && IntegerConstantArgs) |
| 5812 | *IntegerConstantArgs |= 1 << ArgTypes.size(); |
| 5813 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5814 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 5815 | if (Ty->isArrayType()) |
| 5816 | Ty = getArrayDecayedType(Ty); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5817 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5818 | ArgTypes.push_back(Ty); |
| 5819 | } |
| 5820 | |
| 5821 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 5822 | "'.' should only occur at end of builtin type list!"); |
| 5823 | |
John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 5824 | FunctionType::ExtInfo EI; |
| 5825 | if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true); |
| 5826 | |
| 5827 | bool Variadic = (TypeStr[0] == '.'); |
| 5828 | |
| 5829 | // We really shouldn't be making a no-proto type here, especially in C++. |
| 5830 | if (ArgTypes.empty() && Variadic) |
| 5831 | return getFunctionNoProtoType(ResType, EI); |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 5832 | |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5833 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 5834 | EPI.ExtInfo = EI; |
| 5835 | EPI.Variadic = Variadic; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5836 | |
| 5837 | return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), EPI); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5838 | } |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5839 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5840 | GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) { |
| 5841 | GVALinkage External = GVA_StrongExternal; |
| 5842 | |
| 5843 | Linkage L = FD->getLinkage(); |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5844 | switch (L) { |
| 5845 | case NoLinkage: |
| 5846 | case InternalLinkage: |
| 5847 | case UniqueExternalLinkage: |
| 5848 | return GVA_Internal; |
| 5849 | |
| 5850 | case ExternalLinkage: |
| 5851 | switch (FD->getTemplateSpecializationKind()) { |
| 5852 | case TSK_Undeclared: |
| 5853 | case TSK_ExplicitSpecialization: |
| 5854 | External = GVA_StrongExternal; |
| 5855 | break; |
| 5856 | |
| 5857 | case TSK_ExplicitInstantiationDefinition: |
| 5858 | return GVA_ExplicitTemplateInstantiation; |
| 5859 | |
| 5860 | case TSK_ExplicitInstantiationDeclaration: |
| 5861 | case TSK_ImplicitInstantiation: |
| 5862 | External = GVA_TemplateInstantiation; |
| 5863 | break; |
| 5864 | } |
| 5865 | } |
| 5866 | |
| 5867 | if (!FD->isInlined()) |
| 5868 | return External; |
| 5869 | |
| 5870 | if (!getLangOptions().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) { |
| 5871 | // GNU or C99 inline semantics. Determine whether this symbol should be |
| 5872 | // externally visible. |
| 5873 | if (FD->isInlineDefinitionExternallyVisible()) |
| 5874 | return External; |
| 5875 | |
| 5876 | // C99 inline semantics, where the symbol is not externally visible. |
| 5877 | return GVA_C99Inline; |
| 5878 | } |
| 5879 | |
| 5880 | // C++0x [temp.explicit]p9: |
| 5881 | // [ Note: The intent is that an inline function that is the subject of |
| 5882 | // an explicit instantiation declaration will still be implicitly |
| 5883 | // instantiated when used so that the body can be considered for |
| 5884 | // inlining, but that no out-of-line copy of the inline function would be |
| 5885 | // generated in the translation unit. -- end note ] |
| 5886 | if (FD->getTemplateSpecializationKind() |
| 5887 | == TSK_ExplicitInstantiationDeclaration) |
| 5888 | return GVA_C99Inline; |
| 5889 | |
| 5890 | return GVA_CXXInline; |
| 5891 | } |
| 5892 | |
| 5893 | GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) { |
| 5894 | // If this is a static data member, compute the kind of template |
| 5895 | // specialization. Otherwise, this variable is not part of a |
| 5896 | // template. |
| 5897 | TemplateSpecializationKind TSK = TSK_Undeclared; |
| 5898 | if (VD->isStaticDataMember()) |
| 5899 | TSK = VD->getTemplateSpecializationKind(); |
| 5900 | |
| 5901 | Linkage L = VD->getLinkage(); |
| 5902 | if (L == ExternalLinkage && getLangOptions().CPlusPlus && |
| 5903 | VD->getType()->getLinkage() == UniqueExternalLinkage) |
| 5904 | L = UniqueExternalLinkage; |
| 5905 | |
| 5906 | switch (L) { |
| 5907 | case NoLinkage: |
| 5908 | case InternalLinkage: |
| 5909 | case UniqueExternalLinkage: |
| 5910 | return GVA_Internal; |
| 5911 | |
| 5912 | case ExternalLinkage: |
| 5913 | switch (TSK) { |
| 5914 | case TSK_Undeclared: |
| 5915 | case TSK_ExplicitSpecialization: |
| 5916 | return GVA_StrongExternal; |
| 5917 | |
| 5918 | case TSK_ExplicitInstantiationDeclaration: |
| 5919 | llvm_unreachable("Variable should not be instantiated"); |
| 5920 | // Fall through to treat this like any other instantiation. |
| 5921 | |
| 5922 | case TSK_ExplicitInstantiationDefinition: |
| 5923 | return GVA_ExplicitTemplateInstantiation; |
| 5924 | |
| 5925 | case TSK_ImplicitInstantiation: |
| 5926 | return GVA_TemplateInstantiation; |
| 5927 | } |
| 5928 | } |
| 5929 | |
| 5930 | return GVA_StrongExternal; |
| 5931 | } |
| 5932 | |
Argyrios Kyrtzidis | c904933 | 2010-07-29 20:08:05 +0000 | [diff] [blame] | 5933 | bool ASTContext::DeclMustBeEmitted(const Decl *D) { |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5934 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 5935 | if (!VD->isFileVarDecl()) |
| 5936 | return false; |
| 5937 | } else if (!isa<FunctionDecl>(D)) |
| 5938 | return false; |
| 5939 | |
Argyrios Kyrtzidis | 6e03a74 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 5940 | // Weak references don't produce any output by themselves. |
| 5941 | if (D->hasAttr<WeakRefAttr>()) |
| 5942 | return false; |
| 5943 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5944 | // Aliases and used decls are required. |
| 5945 | if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>()) |
| 5946 | return true; |
| 5947 | |
| 5948 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 5949 | // Forward declarations aren't required. |
| 5950 | if (!FD->isThisDeclarationADefinition()) |
| 5951 | return false; |
| 5952 | |
| 5953 | // Constructors and destructors are required. |
| 5954 | if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>()) |
| 5955 | return true; |
| 5956 | |
| 5957 | // The key function for a class is required. |
| 5958 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 5959 | const CXXRecordDecl *RD = MD->getParent(); |
| 5960 | if (MD->isOutOfLine() && RD->isDynamicClass()) { |
| 5961 | const CXXMethodDecl *KeyFunc = getKeyFunction(RD); |
| 5962 | if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl()) |
| 5963 | return true; |
| 5964 | } |
| 5965 | } |
| 5966 | |
| 5967 | GVALinkage Linkage = GetGVALinkageForFunction(FD); |
| 5968 | |
| 5969 | // static, static inline, always_inline, and extern inline functions can |
| 5970 | // always be deferred. Normal inline functions can be deferred in C99/C++. |
| 5971 | // Implicit template instantiations can also be deferred in C++. |
| 5972 | if (Linkage == GVA_Internal || Linkage == GVA_C99Inline || |
| 5973 | Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) |
| 5974 | return false; |
| 5975 | return true; |
| 5976 | } |
| 5977 | |
| 5978 | const VarDecl *VD = cast<VarDecl>(D); |
| 5979 | assert(VD->isFileVarDecl() && "Expected file scoped var"); |
| 5980 | |
Argyrios Kyrtzidis | 6e03a74 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 5981 | if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly) |
| 5982 | return false; |
| 5983 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5984 | // Structs that have non-trivial constructors or destructors are required. |
| 5985 | |
| 5986 | // FIXME: Handle references. |
| 5987 | if (const RecordType *RT = VD->getType()->getAs<RecordType>()) { |
| 5988 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5989 | if (RD->hasDefinition() && |
| 5990 | (!RD->hasTrivialConstructor() || !RD->hasTrivialDestructor())) |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5991 | return true; |
| 5992 | } |
| 5993 | } |
| 5994 | |
| 5995 | GVALinkage L = GetGVALinkageForVariable(VD); |
| 5996 | if (L == GVA_Internal || L == GVA_TemplateInstantiation) { |
| 5997 | if (!(VD->getInit() && VD->getInit()->HasSideEffects(*this))) |
| 5998 | return false; |
| 5999 | } |
| 6000 | |
| 6001 | return true; |
| 6002 | } |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 6003 | |
Charles Davis | 99202b3 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 6004 | CallingConv ASTContext::getDefaultMethodCallConv() { |
| 6005 | // Pass through to the C++ ABI object |
| 6006 | return ABI->getDefaultMethodCallConv(); |
| 6007 | } |
| 6008 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6009 | bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const { |
Anders Carlsson | 60a6263 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 6010 | // Pass through to the C++ ABI object |
| 6011 | return ABI->isNearlyEmpty(RD); |
| 6012 | } |
| 6013 | |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 6014 | MangleContext *ASTContext::createMangleContext() { |
| 6015 | switch (Target.getCXXABI()) { |
| 6016 | case CXXABI_ARM: |
| 6017 | case CXXABI_Itanium: |
| 6018 | return createItaniumMangleContext(*this, getDiagnostics()); |
| 6019 | case CXXABI_Microsoft: |
| 6020 | return createMicrosoftMangleContext(*this, getDiagnostics()); |
| 6021 | } |
| 6022 | assert(0 && "Unsupported ABI"); |
| 6023 | return 0; |
| 6024 | } |
| 6025 | |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 6026 | CXXABI::~CXXABI() {} |