Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 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. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the ASTContext interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/ASTContext.h" |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 15 | #include "clang/AST/CharUnits.h" |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclCXX.h" |
Steve Naroff | 980e508 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclTemplate.h" |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeLoc.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 20 | #include "clang/AST/Expr.h" |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 21 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExternalASTSource.h" |
Argyrios Kyrtzidis | 7b90340 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 23 | #include "clang/AST/ASTMutationListener.h" |
Anders Carlsson | 19cc4ab | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 24 | #include "clang/AST/RecordLayout.h" |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 25 | #include "clang/AST/Mangle.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 26 | #include "clang/Basic/Builtins.h" |
Chris Lattner | a9376d4 | 2009-03-28 03:45:20 +0000 | [diff] [blame] | 27 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 28 | #include "clang/Basic/TargetInfo.h" |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/SmallString.h" |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/StringExtras.h" |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 31 | #include "llvm/Support/MathExtras.h" |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 32 | #include "llvm/Support/raw_ostream.h" |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 33 | #include "CXXABI.h" |
Anders Carlsson | 29445a0 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 34 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 35 | using namespace clang; |
| 36 | |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 37 | unsigned ASTContext::NumImplicitDefaultConstructors; |
| 38 | unsigned ASTContext::NumImplicitDefaultConstructorsDeclared; |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 39 | unsigned ASTContext::NumImplicitCopyConstructors; |
| 40 | unsigned ASTContext::NumImplicitCopyConstructorsDeclared; |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 41 | unsigned ASTContext::NumImplicitCopyAssignmentOperators; |
| 42 | unsigned ASTContext::NumImplicitCopyAssignmentOperatorsDeclared; |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 43 | unsigned ASTContext::NumImplicitDestructors; |
| 44 | unsigned ASTContext::NumImplicitDestructorsDeclared; |
| 45 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 46 | enum FloatingRank { |
| 47 | FloatRank, DoubleRank, LongDoubleRank |
| 48 | }; |
| 49 | |
Douglas Gregor | 3e1274f | 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 | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 55 | ID.AddBoolean(Parm->isParameterPack()); |
Douglas Gregor | 3e1274f | 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 | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 70 | ID.AddBoolean(NTTP->isParameterPack()); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 71 | ID.AddPointer(NTTP->getType().getAsOpaquePtr()); |
Douglas Gregor | 6952f1e | 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 | 3e1274f | 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 | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 90 | TemplateTemplateParmDecl *TTP) const { |
Douglas Gregor | 3e1274f | 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 | 6952f1e | 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 | 3e1274f | 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 | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 155 | TTP->getPosition(), |
| 156 | TTP->isParameterPack(), |
| 157 | 0, |
Douglas Gregor | 3e1274f | 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 | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 175 | CXXABI *ASTContext::createCXXABI(const TargetInfo &T) { |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 176 | if (!LangOpts.CPlusPlus) return 0; |
| 177 | |
Charles Davis | 20cf717 | 2010-08-19 02:18:14 +0000 | [diff] [blame] | 178 | switch (T.getCXXABI()) { |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 179 | case CXXABI_ARM: |
| 180 | return CreateARMCXXABI(*this); |
| 181 | case CXXABI_Itanium: |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 182 | return CreateItaniumCXXABI(*this); |
Charles Davis | 20cf717 | 2010-08-19 02:18:14 +0000 | [diff] [blame] | 183 | case CXXABI_Microsoft: |
| 184 | return CreateMicrosoftCXXABI(*this); |
| 185 | } |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 186 | return 0; |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 189 | ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM, |
Daniel Dunbar | 444be73 | 2009-11-13 05:51:54 +0000 | [diff] [blame] | 190 | const TargetInfo &t, |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 191 | IdentifierTable &idents, SelectorTable &sels, |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 192 | Builtin::Context &builtins, |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 193 | unsigned size_reserve) : |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 194 | TemplateSpecializationTypes(this_()), |
| 195 | DependentTemplateSpecializationTypes(this_()), |
Argyrios Kyrtzidis | 0061138 | 2010-07-04 21:44:19 +0000 | [diff] [blame] | 196 | GlobalNestedNameSpecifier(0), IsInt128Installed(false), |
| 197 | CFConstantStringTypeDecl(0), NSConstantStringTypeDecl(0), |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 198 | ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0), |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 199 | sigjmp_bufDecl(0), BlockDescriptorType(0), BlockDescriptorExtendedType(0), |
Peter Collingbourne | 14b6ba7 | 2011-02-09 21:04:32 +0000 | [diff] [blame] | 200 | cudaConfigureCallDecl(0), |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 201 | NullTypeSourceInfo(QualType()), |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 202 | SourceMgr(SM), LangOpts(LOpts), ABI(createCXXABI(t)), Target(t), |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 203 | Idents(idents), Selectors(sels), |
Ted Kremenek | ac9590e | 2010-05-10 20:40:08 +0000 | [diff] [blame] | 204 | BuiltinInfo(builtins), |
| 205 | DeclarationNames(*this), |
Argyrios Kyrtzidis | 9a44b5f | 2010-10-28 09:29:35 +0000 | [diff] [blame] | 206 | ExternalSource(0), Listener(0), PrintingPolicy(LOpts), |
Ted Kremenek | f057bf7 | 2010-06-18 00:31:04 +0000 | [diff] [blame] | 207 | LastSDM(0, 0), |
| 208 | UniqueBlockByRefTypeID(0), UniqueBlockParmTypeID(0) { |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 209 | ObjCIdRedefinitionType = QualType(); |
| 210 | ObjCClassRedefinitionType = QualType(); |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 211 | ObjCSelRedefinitionType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 212 | if (size_reserve > 0) Types.reserve(size_reserve); |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 213 | TUDecl = TranslationUnitDecl::Create(*this); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 214 | InitBuiltinTypes(); |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 217 | ASTContext::~ASTContext() { |
Ted Kremenek | 3478eb6 | 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 | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 221 | |
Douglas Gregor | 63fe86b | 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 | 0054531 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 225 | |
Douglas Gregor | 7d10b7e | 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 | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 231 | |
Ted Kremenek | dcfcfbe | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 232 | // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed |
Douglas Gregor | 63fe86b | 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 | dcfcfbe | 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 | 6320064 | 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 | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 253 | |
Douglas Gregor | 0054531 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 258 | void |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 259 | ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) { |
| 260 | ExternalSource.reset(Source.take()); |
| 261 | } |
| 262 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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 | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 266 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 267 | unsigned counts[] = { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 268 | #define TYPE(Name, Parent) 0, |
Douglas Gregor | dbe833d | 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 | c2ee10d | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 273 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 274 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 275 | Type *T = Types[i]; |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 276 | counts[(unsigned)T->getTypeClass()]++; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Douglas Gregor | dbe833d | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 288 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 289 | fprintf(stderr, "Total bytes = %d\n", int(TotalBytes)); |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 290 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 291 | // Implicit special member functions. |
Douglas Gregor | 1827403 | 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 | 2258431 | 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 | a376d10 | 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 | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 301 | fprintf(stderr, " %u/%u implicit destructors created\n", |
| 302 | NumImplicitDestructorsDeclared, NumImplicitDestructors); |
| 303 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 304 | if (ExternalSource.get()) { |
| 305 | fprintf(stderr, "\n"); |
| 306 | ExternalSource->PrintStats(); |
| 307 | } |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 308 | |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 309 | BumpAlloc.PrintStats(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 313 | void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 314 | BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 315 | R = CanQualType::CreateUnsafe(QualType(Ty, 0)); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 316 | Types.push_back(Ty); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 319 | void ASTContext::InitBuiltinTypes() { |
| 320 | assert(VoidTy.isNull() && "Context reinitialized?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 321 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 322 | // C99 6.2.5p19. |
| 323 | InitBuiltinType(VoidTy, BuiltinType::Void); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 324 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 325 | // C99 6.2.5p2. |
| 326 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
| 327 | // C99 6.2.5p3. |
Eli Friedman | 15b9176 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 328 | if (LangOpts.CharIsSigned) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 329 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 330 | else |
| 331 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
| 332 | // C99 6.2.5p4. |
| 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 338 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 339 | // C99 6.2.5p6. |
| 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 345 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 346 | // C99 6.2.5p10. |
| 347 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 348 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 349 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 350 | |
Chris Lattner | 2df9ced | 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 | 3f59c97 | 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 | 3a25032 | 2009-02-26 23:43:47 +0000 | [diff] [blame] | 361 | WCharTy = getFromTargetType(Target.getWCharType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 362 | |
Alisdair Meredith | f5c209d | 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 | 898574e | 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 | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 379 | |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 380 | // Placeholder type for functions. |
| 381 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
| 382 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 383 | // Placeholder type for C++0x auto declarations whose real type has |
Anders Carlsson | e89d159 | 2009-06-26 18:41:36 +0000 | [diff] [blame] | 384 | // not yet been deduced. |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 385 | InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 386 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 387 | // C99 6.2.5p11. |
| 388 | FloatComplexTy = getComplexType(FloatTy); |
| 389 | DoubleComplexTy = getComplexType(DoubleTy); |
| 390 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 391 | |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 392 | BuiltinVaListType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 393 | |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 394 | // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope(). |
| 395 | ObjCIdTypedefType = QualType(); |
| 396 | ObjCClassTypedefType = QualType(); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 397 | ObjCSelTypedefType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 398 | |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 399 | // Builtin types for 'id', 'Class', and 'SEL'. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 400 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); |
| 401 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 402 | InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 403 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 404 | ObjCConstantStringType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 405 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 406 | // void * type |
| 407 | VoidPtrTy = getPointerType(VoidTy); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 408 | |
| 409 | // nullptr type (C++0x 2.14.7) |
| 410 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 413 | Diagnostic &ASTContext::getDiagnostics() const { |
| 414 | return SourceMgr.getDiagnostics(); |
| 415 | } |
| 416 | |
Douglas Gregor | 6320064 | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 417 | AttrVec& ASTContext::getDeclAttrs(const Decl *D) { |
| 418 | AttrVec *&Result = DeclAttrs[D]; |
| 419 | if (!Result) { |
| 420 | void *Mem = Allocate(sizeof(AttrVec)); |
| 421 | Result = new (Mem) AttrVec; |
| 422 | } |
| 423 | |
| 424 | return *Result; |
| 425 | } |
| 426 | |
| 427 | /// \brief Erase the attributes corresponding to the given declaration. |
| 428 | void ASTContext::eraseDeclAttrs(const Decl *D) { |
| 429 | llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D); |
| 430 | if (Pos != DeclAttrs.end()) { |
| 431 | Pos->second->~AttrVec(); |
| 432 | DeclAttrs.erase(Pos); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 437 | MemberSpecializationInfo * |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 438 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 439 | assert(Var->isStaticDataMember() && "Not a static data member"); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 440 | llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 441 | = InstantiatedFromStaticDataMember.find(Var); |
| 442 | if (Pos == InstantiatedFromStaticDataMember.end()) |
| 443 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 444 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 445 | return Pos->second; |
| 446 | } |
| 447 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 448 | void |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 449 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, |
Argyrios Kyrtzidis | 9421adc | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 450 | TemplateSpecializationKind TSK, |
| 451 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 452 | assert(Inst->isStaticDataMember() && "Not a static data member"); |
| 453 | assert(Tmpl->isStaticDataMember() && "Not a static data member"); |
| 454 | assert(!InstantiatedFromStaticDataMember[Inst] && |
| 455 | "Already noted what static data member was instantiated from"); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 456 | InstantiatedFromStaticDataMember[Inst] |
Argyrios Kyrtzidis | 9421adc | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 457 | = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 458 | } |
| 459 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 460 | NamedDecl * |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 461 | ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 462 | llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 463 | = InstantiatedFromUsingDecl.find(UUD); |
| 464 | if (Pos == InstantiatedFromUsingDecl.end()) |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 465 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 466 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 467 | return Pos->second; |
| 468 | } |
| 469 | |
| 470 | void |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 471 | ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) { |
| 472 | assert((isa<UsingDecl>(Pattern) || |
| 473 | isa<UnresolvedUsingValueDecl>(Pattern) || |
| 474 | isa<UnresolvedUsingTypenameDecl>(Pattern)) && |
| 475 | "pattern decl is not a using decl"); |
| 476 | assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists"); |
| 477 | InstantiatedFromUsingDecl[Inst] = Pattern; |
| 478 | } |
| 479 | |
| 480 | UsingShadowDecl * |
| 481 | ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) { |
| 482 | llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos |
| 483 | = InstantiatedFromUsingShadowDecl.find(Inst); |
| 484 | if (Pos == InstantiatedFromUsingShadowDecl.end()) |
| 485 | return 0; |
| 486 | |
| 487 | return Pos->second; |
| 488 | } |
| 489 | |
| 490 | void |
| 491 | ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, |
| 492 | UsingShadowDecl *Pattern) { |
| 493 | assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists"); |
| 494 | InstantiatedFromUsingShadowDecl[Inst] = Pattern; |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 497 | FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { |
| 498 | llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos |
| 499 | = InstantiatedFromUnnamedFieldDecl.find(Field); |
| 500 | if (Pos == InstantiatedFromUnnamedFieldDecl.end()) |
| 501 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 502 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 503 | return Pos->second; |
| 504 | } |
| 505 | |
| 506 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, |
| 507 | FieldDecl *Tmpl) { |
| 508 | assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed"); |
| 509 | assert(!Tmpl->getDeclName() && "Template field decl is not unnamed"); |
| 510 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] && |
| 511 | "Already noted what unnamed field was instantiated from"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 512 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 513 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; |
| 514 | } |
| 515 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 516 | ASTContext::overridden_cxx_method_iterator |
| 517 | ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const { |
| 518 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 519 | = OverriddenMethods.find(Method); |
| 520 | if (Pos == OverriddenMethods.end()) |
| 521 | return 0; |
| 522 | |
| 523 | return Pos->second.begin(); |
| 524 | } |
| 525 | |
| 526 | ASTContext::overridden_cxx_method_iterator |
| 527 | ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const { |
| 528 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 529 | = OverriddenMethods.find(Method); |
| 530 | if (Pos == OverriddenMethods.end()) |
| 531 | return 0; |
| 532 | |
| 533 | return Pos->second.end(); |
| 534 | } |
| 535 | |
Argyrios Kyrtzidis | c91e9f4 | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 536 | unsigned |
| 537 | ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const { |
| 538 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 539 | = OverriddenMethods.find(Method); |
| 540 | if (Pos == OverriddenMethods.end()) |
| 541 | return 0; |
| 542 | |
| 543 | return Pos->second.size(); |
| 544 | } |
| 545 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 546 | void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method, |
| 547 | const CXXMethodDecl *Overridden) { |
| 548 | OverriddenMethods[Method].push_back(Overridden); |
| 549 | } |
| 550 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 551 | //===----------------------------------------------------------------------===// |
| 552 | // Type Sizing and Analysis |
| 553 | //===----------------------------------------------------------------------===// |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 554 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 555 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 556 | /// scalar floating point type. |
| 557 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 558 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 559 | assert(BT && "Not a floating point type!"); |
| 560 | switch (BT->getKind()) { |
| 561 | default: assert(0 && "Not a floating point type!"); |
| 562 | case BuiltinType::Float: return Target.getFloatFormat(); |
| 563 | case BuiltinType::Double: return Target.getDoubleFormat(); |
| 564 | case BuiltinType::LongDouble: return Target.getLongDoubleFormat(); |
| 565 | } |
| 566 | } |
| 567 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 568 | /// getDeclAlign - Return a conservative estimate of the alignment of the |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 569 | /// specified decl. Note that bitfields do not have a valid alignment, so |
| 570 | /// this method will assert on them. |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 571 | /// If @p RefAsPointee, references are treated like their underlying type |
| 572 | /// (for alignof), else they're treated like pointers (for CodeGen). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 573 | CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) const { |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 574 | unsigned Align = Target.getCharWidth(); |
| 575 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 576 | bool UseAlignAttrOnly = false; |
| 577 | if (unsigned AlignFromAttr = D->getMaxAlignment()) { |
| 578 | Align = AlignFromAttr; |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 579 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 580 | // __attribute__((aligned)) can increase or decrease alignment |
| 581 | // *except* on a struct or struct member, where it only increases |
| 582 | // alignment unless 'packed' is also specified. |
| 583 | // |
| 584 | // It is an error for [[align]] to decrease alignment, so we can |
| 585 | // ignore that possibility; Sema should diagnose it. |
| 586 | if (isa<FieldDecl>(D)) { |
| 587 | UseAlignAttrOnly = D->hasAttr<PackedAttr>() || |
| 588 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
| 589 | } else { |
| 590 | UseAlignAttrOnly = true; |
| 591 | } |
| 592 | } |
| 593 | |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 594 | // If we're using the align attribute only, just ignore everything |
| 595 | // else about the declaration and its type. |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 596 | if (UseAlignAttrOnly) { |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 597 | // do nothing |
| 598 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 599 | } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 600 | QualType T = VD->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 601 | if (const ReferenceType* RT = T->getAs<ReferenceType>()) { |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 602 | if (RefAsPointee) |
| 603 | T = RT->getPointeeType(); |
| 604 | else |
| 605 | T = getPointerType(RT->getPointeeType()); |
| 606 | } |
| 607 | if (!T->isIncompleteType() && !T->isFunctionType()) { |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 608 | // Adjust alignments of declarations with array type by the |
| 609 | // large-array alignment on the target. |
Rafael Espindola | 6deecb0 | 2010-06-04 23:15:27 +0000 | [diff] [blame] | 610 | unsigned MinWidth = Target.getLargeArrayMinWidth(); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 611 | const ArrayType *arrayType; |
| 612 | if (MinWidth && (arrayType = getAsArrayType(T))) { |
| 613 | if (isa<VariableArrayType>(arrayType)) |
| 614 | Align = std::max(Align, Target.getLargeArrayAlign()); |
| 615 | else if (isa<ConstantArrayType>(arrayType) && |
| 616 | MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType))) |
| 617 | Align = std::max(Align, Target.getLargeArrayAlign()); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 618 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 619 | // Walk through any array types while we're at it. |
| 620 | T = getBaseElementType(arrayType); |
| 621 | } |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 622 | Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); |
| 623 | } |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 624 | |
| 625 | // Fields can be subject to extra alignment constraints, like if |
| 626 | // the field is packed, the struct is packed, or the struct has a |
| 627 | // a max-field-alignment constraint (#pragma pack). So calculate |
| 628 | // the actual alignment of the field within the struct, and then |
| 629 | // (as we're expected to) constrain that by the alignment of the type. |
| 630 | if (const FieldDecl *field = dyn_cast<FieldDecl>(VD)) { |
| 631 | // So calculate the alignment of the field. |
| 632 | const ASTRecordLayout &layout = getASTRecordLayout(field->getParent()); |
| 633 | |
| 634 | // Start with the record's overall alignment. |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 635 | unsigned fieldAlign = toBits(layout.getAlignment()); |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 636 | |
| 637 | // Use the GCD of that and the offset within the record. |
| 638 | uint64_t offset = layout.getFieldOffset(field->getFieldIndex()); |
| 639 | if (offset > 0) { |
| 640 | // Alignment is always a power of 2, so the GCD will be a power of 2, |
| 641 | // which means we get to do this crazy thing instead of Euclid's. |
| 642 | uint64_t lowBitOfOffset = offset & (~offset + 1); |
| 643 | if (lowBitOfOffset < fieldAlign) |
| 644 | fieldAlign = static_cast<unsigned>(lowBitOfOffset); |
| 645 | } |
| 646 | |
| 647 | Align = std::min(Align, fieldAlign); |
Charles Davis | 05f6247 | 2010-02-23 04:52:00 +0000 | [diff] [blame] | 648 | } |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 649 | } |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 650 | |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 651 | return toCharUnitsFromBits(Align); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 652 | } |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 653 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 654 | std::pair<CharUnits, CharUnits> |
Ken Dyck | bee5a79 | 2011-02-20 01:55:18 +0000 | [diff] [blame^] | 655 | ASTContext::getTypeInfoInChars(const Type *T) const { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 656 | std::pair<uint64_t, unsigned> Info = getTypeInfo(T); |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 657 | return std::make_pair(toCharUnitsFromBits(Info.first), |
| 658 | toCharUnitsFromBits(Info.second)); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | std::pair<CharUnits, CharUnits> |
Ken Dyck | bee5a79 | 2011-02-20 01:55:18 +0000 | [diff] [blame^] | 662 | ASTContext::getTypeInfoInChars(QualType T) const { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 663 | return getTypeInfoInChars(T.getTypePtr()); |
| 664 | } |
| 665 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 666 | /// getTypeSize - Return the size of the specified type, in bits. This method |
| 667 | /// does not work on incomplete types. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 668 | /// |
| 669 | /// FIXME: Pointers into different addr spaces could have different sizes and |
| 670 | /// alignment requirements: getPointerInfo should take an AddrSpace, this |
| 671 | /// should take a QualType, &c. |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 672 | std::pair<uint64_t, unsigned> |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 673 | ASTContext::getTypeInfo(const Type *T) const { |
Mike Stump | 5e30100 | 2009-02-27 18:32:39 +0000 | [diff] [blame] | 674 | uint64_t Width=0; |
| 675 | unsigned Align=8; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 676 | switch (T->getTypeClass()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 677 | #define TYPE(Class, Base) |
| 678 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 679 | #define NON_CANONICAL_TYPE(Class, Base) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 680 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 681 | #include "clang/AST/TypeNodes.def" |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 682 | assert(false && "Should not see dependent types"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 683 | break; |
| 684 | |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 685 | case Type::FunctionNoProto: |
| 686 | case Type::FunctionProto: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 687 | // GCC extension: alignof(function) = 32 bits |
| 688 | Width = 0; |
| 689 | Align = 32; |
| 690 | break; |
| 691 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 692 | case Type::IncompleteArray: |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 693 | case Type::VariableArray: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 694 | Width = 0; |
| 695 | Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); |
| 696 | break; |
| 697 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 698 | case Type::ConstantArray: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 699 | const ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 700 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 701 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 702 | Width = EltInfo.first*CAT->getSize().getZExtValue(); |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 703 | Align = EltInfo.second; |
| 704 | break; |
Christopher Lamb | 5c09a02 | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 705 | } |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 706 | case Type::ExtVector: |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 707 | case Type::Vector: { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 708 | const VectorType *VT = cast<VectorType>(T); |
| 709 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType()); |
| 710 | Width = EltInfo.first*VT->getNumElements(); |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 711 | Align = Width; |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 712 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 713 | // This happens for non-power-of-2 length vectors. |
Dan Gohman | 8eefcd3 | 2010-04-21 23:32:43 +0000 | [diff] [blame] | 714 | if (Align & (Align-1)) { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 715 | Align = llvm::NextPowerOf2(Align); |
| 716 | Width = llvm::RoundUpToAlignment(Width, Align); |
| 717 | } |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 718 | break; |
| 719 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 720 | |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 721 | case Type::Builtin: |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 722 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 723 | default: assert(0 && "Unknown builtin type!"); |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 724 | case BuiltinType::Void: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 725 | // GCC extension: alignof(void) = 8 bits. |
| 726 | Width = 0; |
| 727 | Align = 8; |
| 728 | break; |
| 729 | |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 730 | case BuiltinType::Bool: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 731 | Width = Target.getBoolWidth(); |
| 732 | Align = Target.getBoolAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 733 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 734 | case BuiltinType::Char_S: |
| 735 | case BuiltinType::Char_U: |
| 736 | case BuiltinType::UChar: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 737 | case BuiltinType::SChar: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 738 | Width = Target.getCharWidth(); |
| 739 | Align = Target.getCharAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 740 | break; |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 741 | case BuiltinType::WChar_S: |
| 742 | case BuiltinType::WChar_U: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 743 | Width = Target.getWCharWidth(); |
| 744 | Align = Target.getWCharAlign(); |
| 745 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 746 | case BuiltinType::Char16: |
| 747 | Width = Target.getChar16Width(); |
| 748 | Align = Target.getChar16Align(); |
| 749 | break; |
| 750 | case BuiltinType::Char32: |
| 751 | Width = Target.getChar32Width(); |
| 752 | Align = Target.getChar32Align(); |
| 753 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 754 | case BuiltinType::UShort: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 755 | case BuiltinType::Short: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 756 | Width = Target.getShortWidth(); |
| 757 | Align = Target.getShortAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 758 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 759 | case BuiltinType::UInt: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 760 | case BuiltinType::Int: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 761 | Width = Target.getIntWidth(); |
| 762 | Align = Target.getIntAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 763 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 764 | case BuiltinType::ULong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 765 | case BuiltinType::Long: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 766 | Width = Target.getLongWidth(); |
| 767 | Align = Target.getLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 768 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 769 | case BuiltinType::ULongLong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 770 | case BuiltinType::LongLong: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 771 | Width = Target.getLongLongWidth(); |
| 772 | Align = Target.getLongLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 773 | break; |
Chris Lattner | ec16cb9 | 2009-04-30 02:55:13 +0000 | [diff] [blame] | 774 | case BuiltinType::Int128: |
| 775 | case BuiltinType::UInt128: |
| 776 | Width = 128; |
| 777 | Align = 128; // int128_t is 128-bit aligned on all targets. |
| 778 | break; |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 779 | case BuiltinType::Float: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 780 | Width = Target.getFloatWidth(); |
| 781 | Align = Target.getFloatAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 782 | break; |
| 783 | case BuiltinType::Double: |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 784 | Width = Target.getDoubleWidth(); |
| 785 | Align = Target.getDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 786 | break; |
| 787 | case BuiltinType::LongDouble: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 788 | Width = Target.getLongDoubleWidth(); |
| 789 | Align = Target.getLongDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 790 | break; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 791 | case BuiltinType::NullPtr: |
| 792 | Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) |
| 793 | Align = Target.getPointerAlign(0); // == sizeof(void*) |
Sebastian Redl | 1590d9c | 2009-05-27 19:34:06 +0000 | [diff] [blame] | 794 | break; |
Fariborz Jahanian | e04f5fc | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 795 | case BuiltinType::ObjCId: |
| 796 | case BuiltinType::ObjCClass: |
| 797 | case BuiltinType::ObjCSel: |
| 798 | Width = Target.getPointerWidth(0); |
| 799 | Align = Target.getPointerAlign(0); |
| 800 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 801 | } |
Chris Lattner | bfef6d7 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 802 | break; |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 803 | case Type::ObjCObjectPointer: |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 804 | Width = Target.getPointerWidth(0); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 805 | Align = Target.getPointerAlign(0); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 806 | break; |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 807 | case Type::BlockPointer: { |
| 808 | unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace(); |
| 809 | Width = Target.getPointerWidth(AS); |
| 810 | Align = Target.getPointerAlign(AS); |
| 811 | break; |
| 812 | } |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 813 | case Type::LValueReference: |
| 814 | case Type::RValueReference: { |
| 815 | // alignof and sizeof should never enter this code path here, so we go |
| 816 | // the pointer route. |
| 817 | unsigned AS = cast<ReferenceType>(T)->getPointeeType().getAddressSpace(); |
| 818 | Width = Target.getPointerWidth(AS); |
| 819 | Align = Target.getPointerAlign(AS); |
| 820 | break; |
| 821 | } |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 822 | case Type::Pointer: { |
| 823 | unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace(); |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 824 | Width = Target.getPointerWidth(AS); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 825 | Align = Target.getPointerAlign(AS); |
| 826 | break; |
| 827 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 828 | case Type::MemberPointer: { |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 829 | const MemberPointerType *MPT = cast<MemberPointerType>(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 830 | std::pair<uint64_t, unsigned> PtrDiffInfo = |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 831 | getTypeInfo(getPointerDiffType()); |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 832 | Width = PtrDiffInfo.first * ABI->getMemberPointerSize(MPT); |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 833 | Align = PtrDiffInfo.second; |
| 834 | break; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 835 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 836 | case Type::Complex: { |
| 837 | // Complex types have the same alignment as their elements, but twice the |
| 838 | // size. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 839 | std::pair<uint64_t, unsigned> EltInfo = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 840 | getTypeInfo(cast<ComplexType>(T)->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 841 | Width = EltInfo.first*2; |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 842 | Align = EltInfo.second; |
| 843 | break; |
| 844 | } |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 845 | case Type::ObjCObject: |
| 846 | return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr()); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 847 | case Type::ObjCInterface: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 848 | const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 849 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 850 | Width = toBits(Layout.getSize()); |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 851 | Align = toBits(Layout.getAlignment()); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 852 | break; |
| 853 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 854 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 855 | case Type::Enum: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 856 | const TagType *TT = cast<TagType>(T); |
| 857 | |
| 858 | if (TT->getDecl()->isInvalidDecl()) { |
Chris Lattner | 8389eab | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 859 | Width = 1; |
| 860 | Align = 1; |
| 861 | break; |
| 862 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 863 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 864 | if (const EnumType *ET = dyn_cast<EnumType>(TT)) |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 865 | return getTypeInfo(ET->getDecl()->getIntegerType()); |
| 866 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 867 | const RecordType *RT = cast<RecordType>(TT); |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 868 | const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 869 | Width = toBits(Layout.getSize()); |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 870 | Align = toBits(Layout.getAlignment()); |
Chris Lattner | dc0d73e | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 871 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 872 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 873 | |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 874 | case Type::SubstTemplateTypeParm: |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 875 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> |
| 876 | getReplacementType().getTypePtr()); |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 877 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 878 | case Type::Paren: |
| 879 | return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr()); |
| 880 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 881 | case Type::Typedef: { |
| 882 | const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl(); |
Douglas Gregor | df1367a | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 883 | std::pair<uint64_t, unsigned> Info |
| 884 | = getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); |
Chris Lattner | c1de52d | 2011-02-19 22:55:41 +0000 | [diff] [blame] | 885 | // If the typedef has an aligned attribute on it, it overrides any computed |
| 886 | // alignment we have. This violates the GCC documentation (which says that |
| 887 | // attribute(aligned) can only round up) but matches its implementation. |
| 888 | if (unsigned AttrAlign = Typedef->getMaxAlignment()) |
| 889 | Align = AttrAlign; |
| 890 | else |
| 891 | Align = Info.second; |
Douglas Gregor | df1367a | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 892 | Width = Info.first; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 893 | break; |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 894 | } |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 895 | |
| 896 | case Type::TypeOfExpr: |
| 897 | return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType() |
| 898 | .getTypePtr()); |
| 899 | |
| 900 | case Type::TypeOf: |
| 901 | return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr()); |
| 902 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 903 | case Type::Decltype: |
| 904 | return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType() |
| 905 | .getTypePtr()); |
| 906 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 907 | case Type::Elaborated: |
| 908 | return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 909 | |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 910 | case Type::Attributed: |
| 911 | return getTypeInfo( |
| 912 | cast<AttributedType>(T)->getEquivalentType().getTypePtr()); |
| 913 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 914 | case Type::TemplateSpecialization: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 915 | assert(getCanonicalType(T) != T && |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 916 | "Cannot request the size of a dependent type"); |
| 917 | // FIXME: this is likely to be wrong once we support template |
| 918 | // aliases, since a template alias could refer to a typedef that |
| 919 | // has an __aligned__ attribute on it. |
| 920 | return getTypeInfo(getCanonicalType(T)); |
| 921 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 922 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 923 | assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2"); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 924 | return std::make_pair(Width, Align); |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 925 | } |
| 926 | |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 927 | /// toCharUnitsFromBits - Convert a size in bits to a size in characters. |
| 928 | CharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const { |
| 929 | return CharUnits::fromQuantity(BitSize / getCharWidth()); |
| 930 | } |
| 931 | |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 932 | /// toBits - Convert a size in characters to a size in characters. |
| 933 | int64_t ASTContext::toBits(CharUnits CharSize) const { |
| 934 | return CharSize.getQuantity() * getCharWidth(); |
| 935 | } |
| 936 | |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 937 | /// getTypeSizeInChars - Return the size of the specified type, in characters. |
| 938 | /// This method does not work on incomplete types. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 939 | CharUnits ASTContext::getTypeSizeInChars(QualType T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 940 | return toCharUnitsFromBits(getTypeSize(T)); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 941 | } |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 942 | CharUnits ASTContext::getTypeSizeInChars(const Type *T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 943 | return toCharUnitsFromBits(getTypeSize(T)); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 944 | } |
| 945 | |
Ken Dyck | 16e20cc | 2010-01-26 17:25:18 +0000 | [diff] [blame] | 946 | /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 947 | /// characters. This method does not work on incomplete types. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 948 | CharUnits ASTContext::getTypeAlignInChars(QualType T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 949 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 950 | } |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 951 | CharUnits ASTContext::getTypeAlignInChars(const Type *T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 952 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 953 | } |
| 954 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 955 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 956 | /// type for the current target in bits. This can be different than the ABI |
| 957 | /// alignment in cases where it is beneficial for performance to overalign |
| 958 | /// a data type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 959 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) const { |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 960 | unsigned ABIAlign = getTypeAlign(T); |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 961 | |
| 962 | // Double and long long should be naturally aligned if possible. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 963 | if (const ComplexType* CT = T->getAs<ComplexType>()) |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 964 | T = CT->getElementType().getTypePtr(); |
| 965 | if (T->isSpecificBuiltinType(BuiltinType::Double) || |
| 966 | T->isSpecificBuiltinType(BuiltinType::LongLong)) |
| 967 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); |
| 968 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 969 | return ABIAlign; |
| 970 | } |
| 971 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 972 | /// ShallowCollectObjCIvars - |
| 973 | /// Collect all ivars, including those synthesized, in the current class. |
| 974 | /// |
| 975 | void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 976 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) const { |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 977 | // FIXME. This need be removed but there are two many places which |
| 978 | // assume const-ness of ObjCInterfaceDecl |
| 979 | ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI); |
| 980 | for (ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv; |
| 981 | Iv= Iv->getNextIvar()) |
| 982 | Ivars.push_back(Iv); |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 983 | } |
| 984 | |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 985 | /// DeepCollectObjCIvars - |
| 986 | /// This routine first collects all declared, but not synthesized, ivars in |
| 987 | /// super class and then collects all ivars, including those synthesized for |
| 988 | /// current class. This routine is used for implementation of current class |
| 989 | /// when all ivars, declared and synthesized are known. |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 990 | /// |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 991 | void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 992 | bool leafClass, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 993 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) const { |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 994 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
| 995 | DeepCollectObjCIvars(SuperClass, false, Ivars); |
| 996 | if (!leafClass) { |
| 997 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 998 | E = OI->ivar_end(); I != E; ++I) |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 999 | Ivars.push_back(*I); |
| 1000 | } |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1001 | else |
| 1002 | ShallowCollectObjCIvars(OI, Ivars); |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1005 | /// CollectInheritedProtocols - Collect all protocols in current class and |
| 1006 | /// those inherited by it. |
| 1007 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 1008 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1009 | if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1010 | // We can use protocol_iterator here instead of |
| 1011 | // all_referenced_protocol_iterator since we are walking all categories. |
| 1012 | for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(), |
| 1013 | PE = OI->all_referenced_protocol_end(); P != PE; ++P) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1014 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 1015 | Protocols.insert(Proto); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1016 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 1017 | PE = Proto->protocol_end(); P != PE; ++P) { |
| 1018 | Protocols.insert(*P); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1019 | CollectInheritedProtocols(*P, Protocols); |
| 1020 | } |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 1021 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1022 | |
| 1023 | // Categories of this Interface. |
| 1024 | for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList(); |
| 1025 | CDeclChain; CDeclChain = CDeclChain->getNextClassCategory()) |
| 1026 | CollectInheritedProtocols(CDeclChain, Protocols); |
| 1027 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) |
| 1028 | while (SD) { |
| 1029 | CollectInheritedProtocols(SD, Protocols); |
| 1030 | SD = SD->getSuperClass(); |
| 1031 | } |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1032 | } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1033 | for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(), |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1034 | PE = OC->protocol_end(); P != PE; ++P) { |
| 1035 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 1036 | Protocols.insert(Proto); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1037 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 1038 | PE = Proto->protocol_end(); P != PE; ++P) |
| 1039 | CollectInheritedProtocols(*P, Protocols); |
| 1040 | } |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1041 | } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1042 | for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(), |
| 1043 | PE = OP->protocol_end(); P != PE; ++P) { |
| 1044 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 1045 | Protocols.insert(Proto); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1046 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 1047 | PE = Proto->protocol_end(); P != PE; ++P) |
| 1048 | CollectInheritedProtocols(*P, Protocols); |
| 1049 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1050 | } |
| 1051 | } |
| 1052 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1053 | unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const { |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1054 | unsigned count = 0; |
| 1055 | // Count ivars declared in class extension. |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 1056 | for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl; |
| 1057 | CDecl = CDecl->getNextClassExtension()) |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1058 | count += CDecl->ivar_size(); |
| 1059 | |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1060 | // Count ivar defined in this class's implementation. This |
| 1061 | // includes synthesized ivars. |
| 1062 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1063 | count += ImplDecl->ivar_size(); |
| 1064 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 1065 | return count; |
| 1066 | } |
| 1067 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1068 | /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. |
| 1069 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
| 1070 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1071 | I = ObjCImpls.find(D); |
| 1072 | if (I != ObjCImpls.end()) |
| 1073 | return cast<ObjCImplementationDecl>(I->second); |
| 1074 | return 0; |
| 1075 | } |
| 1076 | /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. |
| 1077 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
| 1078 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1079 | I = ObjCImpls.find(D); |
| 1080 | if (I != ObjCImpls.end()) |
| 1081 | return cast<ObjCCategoryImplDecl>(I->second); |
| 1082 | return 0; |
| 1083 | } |
| 1084 | |
| 1085 | /// \brief Set the implementation of ObjCInterfaceDecl. |
| 1086 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
| 1087 | ObjCImplementationDecl *ImplD) { |
| 1088 | assert(IFaceD && ImplD && "Passed null params"); |
| 1089 | ObjCImpls[IFaceD] = ImplD; |
| 1090 | } |
| 1091 | /// \brief Set the implementation of ObjCCategoryDecl. |
| 1092 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
| 1093 | ObjCCategoryImplDecl *ImplD) { |
| 1094 | assert(CatD && ImplD && "Passed null params"); |
| 1095 | ObjCImpls[CatD] = ImplD; |
| 1096 | } |
| 1097 | |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1098 | /// \brief Get the copy initialization expression of VarDecl,or NULL if |
| 1099 | /// none exists. |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1100 | Expr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) { |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1101 | assert(VD && "Passed null params"); |
| 1102 | assert(VD->hasAttr<BlocksAttr>() && |
| 1103 | "getBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1104 | llvm::DenseMap<const VarDecl*, Expr*>::iterator |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1105 | I = BlockVarCopyInits.find(VD); |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1106 | return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0; |
| 1107 | } |
| 1108 | |
| 1109 | /// \brief Set the copy inialization expression of a block var decl. |
| 1110 | void ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) { |
| 1111 | assert(VD && Init && "Passed null params"); |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1112 | assert(VD->hasAttr<BlocksAttr>() && |
| 1113 | "setBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1114 | BlockVarCopyInits[VD] = Init; |
| 1115 | } |
| 1116 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1117 | /// \brief Allocate an uninitialized TypeSourceInfo. |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1118 | /// |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1119 | /// The caller should initialize the memory held by TypeSourceInfo using |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1120 | /// the TypeLoc wrappers. |
| 1121 | /// |
| 1122 | /// \param T the type that will be the basis for type source info. This type |
| 1123 | /// should refer to how the declarator was written in source code, not to |
| 1124 | /// what type semantic analysis resolved the declarator to. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1125 | TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1126 | unsigned DataSize) const { |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1127 | if (!DataSize) |
| 1128 | DataSize = TypeLoc::getFullDataSizeForType(T); |
| 1129 | else |
| 1130 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1131 | "incorrect data size provided to CreateTypeSourceInfo!"); |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1132 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1133 | TypeSourceInfo *TInfo = |
| 1134 | (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8); |
| 1135 | new (TInfo) TypeSourceInfo(T); |
| 1136 | return TInfo; |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1139 | TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1140 | SourceLocation L) const { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1141 | TypeSourceInfo *DI = CreateTypeSourceInfo(T); |
Douglas Gregor | c21c7e9 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 1142 | DI->getTypeLoc().initialize(const_cast<ASTContext &>(*this), L); |
John McCall | a4eb74d | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 1143 | return DI; |
| 1144 | } |
| 1145 | |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1146 | const ASTRecordLayout & |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1147 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const { |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1148 | return getObjCLayout(D, 0); |
| 1149 | } |
| 1150 | |
| 1151 | const ASTRecordLayout & |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1152 | ASTContext::getASTObjCImplementationLayout( |
| 1153 | const ObjCImplementationDecl *D) const { |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1154 | return getObjCLayout(D->getClassInterface(), D); |
| 1155 | } |
| 1156 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1157 | //===----------------------------------------------------------------------===// |
| 1158 | // Type creation/memoization methods |
| 1159 | //===----------------------------------------------------------------------===// |
| 1160 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1161 | QualType |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1162 | ASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const { |
| 1163 | unsigned fastQuals = quals.getFastQualifiers(); |
| 1164 | quals.removeFastQualifiers(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1165 | |
| 1166 | // Check if we've already instantiated this type. |
| 1167 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1168 | ExtQuals::Profile(ID, baseType, quals); |
| 1169 | void *insertPos = 0; |
| 1170 | if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, insertPos)) { |
| 1171 | assert(eq->getQualifiers() == quals); |
| 1172 | return QualType(eq, fastQuals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1175 | // If the base type is not canonical, make the appropriate canonical type. |
| 1176 | QualType canon; |
| 1177 | if (!baseType->isCanonicalUnqualified()) { |
| 1178 | SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split(); |
| 1179 | canonSplit.second.addConsistentQualifiers(quals); |
| 1180 | canon = getExtQualType(canonSplit.first, canonSplit.second); |
| 1181 | |
| 1182 | // Re-find the insert position. |
| 1183 | (void) ExtQualNodes.FindNodeOrInsertPos(ID, insertPos); |
| 1184 | } |
| 1185 | |
| 1186 | ExtQuals *eq = new (*this, TypeAlignment) ExtQuals(baseType, canon, quals); |
| 1187 | ExtQualNodes.InsertNode(eq, insertPos); |
| 1188 | return QualType(eq, fastQuals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1191 | QualType |
| 1192 | ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) const { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1193 | QualType CanT = getCanonicalType(T); |
| 1194 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1195 | return T; |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1196 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1197 | // If we are composing extended qualifiers together, merge together |
| 1198 | // into one ExtQuals node. |
| 1199 | QualifierCollector Quals; |
| 1200 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1201 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1202 | // If this type already has an address space specified, it cannot get |
| 1203 | // another one. |
| 1204 | assert(!Quals.hasAddressSpace() && |
| 1205 | "Type cannot be in multiple addr spaces!"); |
| 1206 | Quals.addAddressSpace(AddressSpace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1207 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1208 | return getExtQualType(TypeNode, Quals); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1211 | QualType ASTContext::getObjCGCQualType(QualType T, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1212 | Qualifiers::GC GCAttr) const { |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1213 | QualType CanT = getCanonicalType(T); |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1214 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1215 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1216 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1217 | if (const PointerType *ptr = T->getAs<PointerType>()) { |
| 1218 | QualType Pointee = ptr->getPointeeType(); |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 1219 | if (Pointee->isAnyPointerType()) { |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1220 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 1221 | return getPointerType(ResultType); |
| 1222 | } |
| 1223 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1224 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1225 | // If we are composing extended qualifiers together, merge together |
| 1226 | // into one ExtQuals node. |
| 1227 | QualifierCollector Quals; |
| 1228 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1229 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1230 | // If this type already has an ObjCGC specified, it cannot get |
| 1231 | // another one. |
| 1232 | assert(!Quals.hasObjCGCAttr() && |
| 1233 | "Type cannot have multiple ObjCGCs!"); |
| 1234 | Quals.addObjCGCAttr(GCAttr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1235 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1236 | return getExtQualType(TypeNode, Quals); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1237 | } |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1238 | |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 1239 | const FunctionType *ASTContext::adjustFunctionType(const FunctionType *T, |
| 1240 | FunctionType::ExtInfo Info) { |
| 1241 | if (T->getExtInfo() == Info) |
| 1242 | return T; |
| 1243 | |
| 1244 | QualType Result; |
| 1245 | if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) { |
| 1246 | Result = getFunctionNoProtoType(FNPT->getResultType(), Info); |
| 1247 | } else { |
| 1248 | const FunctionProtoType *FPT = cast<FunctionProtoType>(T); |
| 1249 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 1250 | EPI.ExtInfo = Info; |
| 1251 | Result = getFunctionType(FPT->getResultType(), FPT->arg_type_begin(), |
| 1252 | FPT->getNumArgs(), EPI); |
| 1253 | } |
| 1254 | |
| 1255 | return cast<FunctionType>(Result.getTypePtr()); |
| 1256 | } |
| 1257 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1258 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 1259 | /// number with the specified element type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1260 | QualType ASTContext::getComplexType(QualType T) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1261 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1262 | // structure. |
| 1263 | llvm::FoldingSetNodeID ID; |
| 1264 | ComplexType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1265 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1266 | void *InsertPos = 0; |
| 1267 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1268 | return QualType(CT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1269 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1270 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1271 | // so fill in the canonical type field. |
| 1272 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1273 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1274 | Canonical = getComplexType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1275 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1276 | // Get the new insert position for the node we care about. |
| 1277 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1278 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1279 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1280 | ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1281 | Types.push_back(New); |
| 1282 | ComplexTypes.InsertNode(New, InsertPos); |
| 1283 | return QualType(New, 0); |
| 1284 | } |
| 1285 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1286 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 1287 | /// the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1288 | QualType ASTContext::getPointerType(QualType T) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1289 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1290 | // structure. |
| 1291 | llvm::FoldingSetNodeID ID; |
| 1292 | PointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1293 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1294 | void *InsertPos = 0; |
| 1295 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1296 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1297 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1298 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1299 | // so fill in the canonical type field. |
| 1300 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1301 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1302 | Canonical = getPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1303 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1304 | // Get the new insert position for the node we care about. |
| 1305 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1306 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1307 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1308 | PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1309 | Types.push_back(New); |
| 1310 | PointerTypes.InsertNode(New, InsertPos); |
| 1311 | return QualType(New, 0); |
| 1312 | } |
| 1313 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1314 | /// getBlockPointerType - Return the uniqued reference to the type for |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1315 | /// a pointer to the specified block. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1316 | QualType ASTContext::getBlockPointerType(QualType T) const { |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1317 | assert(T->isFunctionType() && "block of function types only"); |
| 1318 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1319 | // structure. |
| 1320 | llvm::FoldingSetNodeID ID; |
| 1321 | BlockPointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1322 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1323 | void *InsertPos = 0; |
| 1324 | if (BlockPointerType *PT = |
| 1325 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1326 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1327 | |
| 1328 | // If the block pointee type isn't canonical, this won't be a canonical |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1329 | // type either so fill in the canonical type field. |
| 1330 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1331 | if (!T.isCanonical()) { |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1332 | Canonical = getBlockPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1333 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1334 | // Get the new insert position for the node we care about. |
| 1335 | BlockPointerType *NewIP = |
| 1336 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1337 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1338 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1339 | BlockPointerType *New |
| 1340 | = new (*this, TypeAlignment) BlockPointerType(T, Canonical); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1341 | Types.push_back(New); |
| 1342 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 1343 | return QualType(New, 0); |
| 1344 | } |
| 1345 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1346 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 1347 | /// lvalue reference to the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1348 | QualType |
| 1349 | ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1350 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1351 | // structure. |
| 1352 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1353 | ReferenceType::Profile(ID, T, SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1354 | |
| 1355 | void *InsertPos = 0; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1356 | if (LValueReferenceType *RT = |
| 1357 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1358 | return QualType(RT, 0); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1359 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1360 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1361 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1362 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1363 | // either, so fill in the canonical type field. |
| 1364 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1365 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
| 1366 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1367 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1368 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1369 | // Get the new insert position for the node we care about. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1370 | LValueReferenceType *NewIP = |
| 1371 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1372 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1375 | LValueReferenceType *New |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1376 | = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, |
| 1377 | SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1378 | Types.push_back(New); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1379 | LValueReferenceTypes.InsertNode(New, InsertPos); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1380 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1381 | return QualType(New, 0); |
| 1382 | } |
| 1383 | |
| 1384 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 1385 | /// rvalue reference to the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1386 | QualType ASTContext::getRValueReferenceType(QualType T) const { |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1387 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1388 | // structure. |
| 1389 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1390 | ReferenceType::Profile(ID, T, false); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1391 | |
| 1392 | void *InsertPos = 0; |
| 1393 | if (RValueReferenceType *RT = |
| 1394 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1395 | return QualType(RT, 0); |
| 1396 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1397 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1398 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1399 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1400 | // either, so fill in the canonical type field. |
| 1401 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1402 | if (InnerRef || !T.isCanonical()) { |
| 1403 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1404 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1405 | |
| 1406 | // Get the new insert position for the node we care about. |
| 1407 | RValueReferenceType *NewIP = |
| 1408 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1409 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1412 | RValueReferenceType *New |
| 1413 | = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1414 | Types.push_back(New); |
| 1415 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1416 | return QualType(New, 0); |
| 1417 | } |
| 1418 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1419 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 1420 | /// member pointer to the specified type, in the specified class. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1421 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1422 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1423 | // structure. |
| 1424 | llvm::FoldingSetNodeID ID; |
| 1425 | MemberPointerType::Profile(ID, T, Cls); |
| 1426 | |
| 1427 | void *InsertPos = 0; |
| 1428 | if (MemberPointerType *PT = |
| 1429 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1430 | return QualType(PT, 0); |
| 1431 | |
| 1432 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 1433 | // type either, so fill in the canonical type field. |
| 1434 | QualType Canonical; |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1435 | if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1436 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 1437 | |
| 1438 | // Get the new insert position for the node we care about. |
| 1439 | MemberPointerType *NewIP = |
| 1440 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1441 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1442 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1443 | MemberPointerType *New |
| 1444 | = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1445 | Types.push_back(New); |
| 1446 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 1447 | return QualType(New, 0); |
| 1448 | } |
| 1449 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1450 | /// getConstantArrayType - Return the unique reference to the type for an |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1451 | /// array of the specified element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1452 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1453 | const llvm::APInt &ArySizeIn, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1454 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1455 | unsigned IndexTypeQuals) const { |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1456 | assert((EltTy->isDependentType() || |
| 1457 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && |
Eli Friedman | 587cbdf | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1458 | "Constant array of VLAs is illegal!"); |
| 1459 | |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1460 | // Convert the array size into a canonical width matching the pointer size for |
| 1461 | // the target. |
| 1462 | llvm::APInt ArySize(ArySizeIn); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1463 | ArySize = |
| 1464 | ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1465 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1466 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1467 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, IndexTypeQuals); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1468 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1469 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1470 | if (ConstantArrayType *ATP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1471 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1472 | return QualType(ATP, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1473 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1474 | // If the element type isn't canonical or has qualifiers, this won't |
| 1475 | // be a canonical type either, so fill in the canonical type field. |
| 1476 | QualType Canon; |
| 1477 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 1478 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
| 1479 | Canon = getConstantArrayType(QualType(canonSplit.first, 0), ArySize, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1480 | ASM, IndexTypeQuals); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1481 | Canon = getQualifiedType(Canon, canonSplit.second); |
| 1482 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1483 | // Get the new insert position for the node we care about. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1484 | ConstantArrayType *NewIP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1485 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1486 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1487 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1488 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1489 | ConstantArrayType *New = new(*this,TypeAlignment) |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1490 | ConstantArrayType(EltTy, Canon, ArySize, ASM, IndexTypeQuals); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1491 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1492 | Types.push_back(New); |
| 1493 | return QualType(New, 0); |
| 1494 | } |
| 1495 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1496 | /// getVariableArrayDecayedType - Turns the given type, which may be |
| 1497 | /// variably-modified, into the corresponding type with all the known |
| 1498 | /// sizes replaced with [*]. |
| 1499 | QualType ASTContext::getVariableArrayDecayedType(QualType type) const { |
| 1500 | // Vastly most common case. |
| 1501 | if (!type->isVariablyModifiedType()) return type; |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1502 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1503 | QualType result; |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1504 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1505 | SplitQualType split = type.getSplitDesugaredType(); |
| 1506 | const Type *ty = split.first; |
| 1507 | switch (ty->getTypeClass()) { |
| 1508 | #define TYPE(Class, Base) |
| 1509 | #define ABSTRACT_TYPE(Class, Base) |
| 1510 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 1511 | #include "clang/AST/TypeNodes.def" |
| 1512 | llvm_unreachable("didn't desugar past all non-canonical types?"); |
| 1513 | |
| 1514 | // These types should never be variably-modified. |
| 1515 | case Type::Builtin: |
| 1516 | case Type::Complex: |
| 1517 | case Type::Vector: |
| 1518 | case Type::ExtVector: |
| 1519 | case Type::DependentSizedExtVector: |
| 1520 | case Type::ObjCObject: |
| 1521 | case Type::ObjCInterface: |
| 1522 | case Type::ObjCObjectPointer: |
| 1523 | case Type::Record: |
| 1524 | case Type::Enum: |
| 1525 | case Type::UnresolvedUsing: |
| 1526 | case Type::TypeOfExpr: |
| 1527 | case Type::TypeOf: |
| 1528 | case Type::Decltype: |
| 1529 | case Type::DependentName: |
| 1530 | case Type::InjectedClassName: |
| 1531 | case Type::TemplateSpecialization: |
| 1532 | case Type::DependentTemplateSpecialization: |
| 1533 | case Type::TemplateTypeParm: |
| 1534 | case Type::SubstTemplateTypeParmPack: |
| 1535 | case Type::PackExpansion: |
| 1536 | llvm_unreachable("type should never be variably-modified"); |
| 1537 | |
| 1538 | // These types can be variably-modified but should never need to |
| 1539 | // further decay. |
| 1540 | case Type::FunctionNoProto: |
| 1541 | case Type::FunctionProto: |
| 1542 | case Type::BlockPointer: |
| 1543 | case Type::MemberPointer: |
| 1544 | return type; |
| 1545 | |
| 1546 | // These types can be variably-modified. All these modifications |
| 1547 | // preserve structure except as noted by comments. |
| 1548 | // TODO: if we ever care about optimizing VLAs, there are no-op |
| 1549 | // optimizations available here. |
| 1550 | case Type::Pointer: |
| 1551 | result = getPointerType(getVariableArrayDecayedType( |
| 1552 | cast<PointerType>(ty)->getPointeeType())); |
| 1553 | break; |
| 1554 | |
| 1555 | case Type::LValueReference: { |
| 1556 | const LValueReferenceType *lv = cast<LValueReferenceType>(ty); |
| 1557 | result = getLValueReferenceType( |
| 1558 | getVariableArrayDecayedType(lv->getPointeeType()), |
| 1559 | lv->isSpelledAsLValue()); |
| 1560 | break; |
| 1561 | } |
| 1562 | |
| 1563 | case Type::RValueReference: { |
| 1564 | const RValueReferenceType *lv = cast<RValueReferenceType>(ty); |
| 1565 | result = getRValueReferenceType( |
| 1566 | getVariableArrayDecayedType(lv->getPointeeType())); |
| 1567 | break; |
| 1568 | } |
| 1569 | |
| 1570 | case Type::ConstantArray: { |
| 1571 | const ConstantArrayType *cat = cast<ConstantArrayType>(ty); |
| 1572 | result = getConstantArrayType( |
| 1573 | getVariableArrayDecayedType(cat->getElementType()), |
| 1574 | cat->getSize(), |
| 1575 | cat->getSizeModifier(), |
| 1576 | cat->getIndexTypeCVRQualifiers()); |
| 1577 | break; |
| 1578 | } |
| 1579 | |
| 1580 | case Type::DependentSizedArray: { |
| 1581 | const DependentSizedArrayType *dat = cast<DependentSizedArrayType>(ty); |
| 1582 | result = getDependentSizedArrayType( |
| 1583 | getVariableArrayDecayedType(dat->getElementType()), |
| 1584 | dat->getSizeExpr(), |
| 1585 | dat->getSizeModifier(), |
| 1586 | dat->getIndexTypeCVRQualifiers(), |
| 1587 | dat->getBracketsRange()); |
| 1588 | break; |
| 1589 | } |
| 1590 | |
| 1591 | // Turn incomplete types into [*] types. |
| 1592 | case Type::IncompleteArray: { |
| 1593 | const IncompleteArrayType *iat = cast<IncompleteArrayType>(ty); |
| 1594 | result = getVariableArrayType( |
| 1595 | getVariableArrayDecayedType(iat->getElementType()), |
| 1596 | /*size*/ 0, |
| 1597 | ArrayType::Normal, |
| 1598 | iat->getIndexTypeCVRQualifiers(), |
| 1599 | SourceRange()); |
| 1600 | break; |
| 1601 | } |
| 1602 | |
| 1603 | // Turn VLA types into [*] types. |
| 1604 | case Type::VariableArray: { |
| 1605 | const VariableArrayType *vat = cast<VariableArrayType>(ty); |
| 1606 | result = getVariableArrayType( |
| 1607 | getVariableArrayDecayedType(vat->getElementType()), |
| 1608 | /*size*/ 0, |
| 1609 | ArrayType::Star, |
| 1610 | vat->getIndexTypeCVRQualifiers(), |
| 1611 | vat->getBracketsRange()); |
| 1612 | break; |
| 1613 | } |
| 1614 | } |
| 1615 | |
| 1616 | // Apply the top-level qualifiers from the original. |
| 1617 | return getQualifiedType(result, split.second); |
| 1618 | } |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1619 | |
Steve Naroff | bdbf7b0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 1620 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 1621 | /// variable array of the specified element type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1622 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 1623 | Expr *NumElts, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1624 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1625 | unsigned IndexTypeQuals, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1626 | SourceRange Brackets) const { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1627 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 1628 | // that have an expression provided for their size. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1629 | QualType Canon; |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 1630 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1631 | // Be sure to pull qualifiers off the element type. |
| 1632 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 1633 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
| 1634 | Canon = getVariableArrayType(QualType(canonSplit.first, 0), NumElts, ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1635 | IndexTypeQuals, Brackets); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1636 | Canon = getQualifiedType(Canon, canonSplit.second); |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1639 | VariableArrayType *New = new(*this, TypeAlignment) |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1640 | VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1641 | |
| 1642 | VariableArrayTypes.push_back(New); |
| 1643 | Types.push_back(New); |
| 1644 | return QualType(New, 0); |
| 1645 | } |
| 1646 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1647 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 1648 | /// the type for a dependently-sized array of the specified element |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1649 | /// type. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1650 | QualType ASTContext::getDependentSizedArrayType(QualType elementType, |
| 1651 | Expr *numElements, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1652 | ArrayType::ArraySizeModifier ASM, |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1653 | unsigned elementTypeQuals, |
| 1654 | SourceRange brackets) const { |
| 1655 | assert((!numElements || numElements->isTypeDependent() || |
| 1656 | numElements->isValueDependent()) && |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1657 | "Size must be type- or value-dependent!"); |
| 1658 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1659 | // Dependently-sized array types that do not have a specified number |
| 1660 | // of elements will have their sizes deduced from a dependent |
| 1661 | // initializer. We do no canonicalization here at all, which is okay |
| 1662 | // because they can't be used in most locations. |
| 1663 | if (!numElements) { |
| 1664 | DependentSizedArrayType *newType |
| 1665 | = new (*this, TypeAlignment) |
| 1666 | DependentSizedArrayType(*this, elementType, QualType(), |
| 1667 | numElements, ASM, elementTypeQuals, |
| 1668 | brackets); |
| 1669 | Types.push_back(newType); |
| 1670 | return QualType(newType, 0); |
| 1671 | } |
| 1672 | |
| 1673 | // Otherwise, we actually build a new type every time, but we |
| 1674 | // also build a canonical type. |
| 1675 | |
| 1676 | SplitQualType canonElementType = getCanonicalType(elementType).split(); |
| 1677 | |
| 1678 | void *insertPos = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1679 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1680 | DependentSizedArrayType::Profile(ID, *this, |
| 1681 | QualType(canonElementType.first, 0), |
| 1682 | ASM, elementTypeQuals, numElements); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1683 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1684 | // Look for an existing type with these properties. |
| 1685 | DependentSizedArrayType *canonTy = |
| 1686 | DependentSizedArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1687 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1688 | // If we don't have one, build one. |
| 1689 | if (!canonTy) { |
| 1690 | canonTy = new (*this, TypeAlignment) |
| 1691 | DependentSizedArrayType(*this, QualType(canonElementType.first, 0), |
| 1692 | QualType(), numElements, ASM, elementTypeQuals, |
| 1693 | brackets); |
| 1694 | DependentSizedArrayTypes.InsertNode(canonTy, insertPos); |
| 1695 | Types.push_back(canonTy); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1696 | } |
| 1697 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1698 | // Apply qualifiers from the element type to the array. |
| 1699 | QualType canon = getQualifiedType(QualType(canonTy,0), |
| 1700 | canonElementType.second); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1701 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1702 | // If we didn't need extra canonicalization for the element type, |
| 1703 | // then just use that as our result. |
| 1704 | if (QualType(canonElementType.first, 0) == elementType) |
| 1705 | return canon; |
| 1706 | |
| 1707 | // Otherwise, we need to build a type which follows the spelling |
| 1708 | // of the element type. |
| 1709 | DependentSizedArrayType *sugaredType |
| 1710 | = new (*this, TypeAlignment) |
| 1711 | DependentSizedArrayType(*this, elementType, canon, numElements, |
| 1712 | ASM, elementTypeQuals, brackets); |
| 1713 | Types.push_back(sugaredType); |
| 1714 | return QualType(sugaredType, 0); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1717 | QualType ASTContext::getIncompleteArrayType(QualType elementType, |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1718 | ArrayType::ArraySizeModifier ASM, |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1719 | unsigned elementTypeQuals) const { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1720 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1721 | IncompleteArrayType::Profile(ID, elementType, ASM, elementTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1722 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1723 | void *insertPos = 0; |
| 1724 | if (IncompleteArrayType *iat = |
| 1725 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos)) |
| 1726 | return QualType(iat, 0); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1727 | |
| 1728 | // If the element type isn't canonical, this won't be a canonical type |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1729 | // either, so fill in the canonical type field. We also have to pull |
| 1730 | // qualifiers off the element type. |
| 1731 | QualType canon; |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1732 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1733 | if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) { |
| 1734 | SplitQualType canonSplit = getCanonicalType(elementType).split(); |
| 1735 | canon = getIncompleteArrayType(QualType(canonSplit.first, 0), |
| 1736 | ASM, elementTypeQuals); |
| 1737 | canon = getQualifiedType(canon, canonSplit.second); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1738 | |
| 1739 | // Get the new insert position for the node we care about. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1740 | IncompleteArrayType *existing = |
| 1741 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
| 1742 | assert(!existing && "Shouldn't be in the map!"); (void) existing; |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1743 | } |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1744 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1745 | IncompleteArrayType *newType = new (*this, TypeAlignment) |
| 1746 | IncompleteArrayType(elementType, canon, ASM, elementTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1747 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1748 | IncompleteArrayTypes.InsertNode(newType, insertPos); |
| 1749 | Types.push_back(newType); |
| 1750 | return QualType(newType, 0); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1753 | /// getVectorType - Return the unique reference to a vector type of |
| 1754 | /// the specified element type and size. VectorType must be a built-in type. |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1755 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1756 | VectorType::VectorKind VecKind) const { |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1757 | assert(vecType->isBuiltinType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1758 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1759 | // Check if we've already instantiated a vector of this type. |
| 1760 | llvm::FoldingSetNodeID ID; |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1761 | VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind); |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1762 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1763 | void *InsertPos = 0; |
| 1764 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1765 | return QualType(VTP, 0); |
| 1766 | |
| 1767 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1768 | // so fill in the canonical type field. |
| 1769 | QualType Canonical; |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1770 | if (!vecType.isCanonical()) { |
Bob Wilson | 231da7e | 2010-11-16 00:32:20 +0000 | [diff] [blame] | 1771 | Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1772 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1773 | // Get the new insert position for the node we care about. |
| 1774 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1775 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1776 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1777 | VectorType *New = new (*this, TypeAlignment) |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1778 | VectorType(vecType, NumElts, Canonical, VecKind); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1779 | VectorTypes.InsertNode(New, InsertPos); |
| 1780 | Types.push_back(New); |
| 1781 | return QualType(New, 0); |
| 1782 | } |
| 1783 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1784 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1785 | /// the specified element type and size. VectorType must be a built-in type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1786 | QualType |
| 1787 | ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) const { |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1788 | assert(vecType->isBuiltinType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1789 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1790 | // Check if we've already instantiated a vector of this type. |
| 1791 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1792 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1793 | VectorType::GenericVector); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1794 | void *InsertPos = 0; |
| 1795 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1796 | return QualType(VTP, 0); |
| 1797 | |
| 1798 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1799 | // so fill in the canonical type field. |
| 1800 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1801 | if (!vecType.isCanonical()) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1802 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1803 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1804 | // Get the new insert position for the node we care about. |
| 1805 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1806 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1807 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1808 | ExtVectorType *New = new (*this, TypeAlignment) |
| 1809 | ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1810 | VectorTypes.InsertNode(New, InsertPos); |
| 1811 | Types.push_back(New); |
| 1812 | return QualType(New, 0); |
| 1813 | } |
| 1814 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1815 | QualType |
| 1816 | ASTContext::getDependentSizedExtVectorType(QualType vecType, |
| 1817 | Expr *SizeExpr, |
| 1818 | SourceLocation AttrLoc) const { |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1819 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1820 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1821 | SizeExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1822 | |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1823 | void *InsertPos = 0; |
| 1824 | DependentSizedExtVectorType *Canon |
| 1825 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1826 | DependentSizedExtVectorType *New; |
| 1827 | if (Canon) { |
| 1828 | // We already have a canonical version of this array type; use it as |
| 1829 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1830 | New = new (*this, TypeAlignment) |
| 1831 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), |
| 1832 | SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1833 | } else { |
| 1834 | QualType CanonVecTy = getCanonicalType(vecType); |
| 1835 | if (CanonVecTy == vecType) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1836 | New = new (*this, TypeAlignment) |
| 1837 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, |
| 1838 | AttrLoc); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1839 | |
| 1840 | DependentSizedExtVectorType *CanonCheck |
| 1841 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1842 | assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken"); |
| 1843 | (void)CanonCheck; |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1844 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); |
| 1845 | } else { |
| 1846 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 1847 | SourceLocation()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1848 | New = new (*this, TypeAlignment) |
| 1849 | DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1850 | } |
| 1851 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1852 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1853 | Types.push_back(New); |
| 1854 | return QualType(New, 0); |
| 1855 | } |
| 1856 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1857 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1858 | /// |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1859 | QualType |
| 1860 | ASTContext::getFunctionNoProtoType(QualType ResultTy, |
| 1861 | const FunctionType::ExtInfo &Info) const { |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1862 | const CallingConv CallConv = Info.getCC(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1863 | // Unique functions, to guarantee there is only one function of a particular |
| 1864 | // structure. |
| 1865 | llvm::FoldingSetNodeID ID; |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1866 | FunctionNoProtoType::Profile(ID, ResultTy, Info); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1867 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1868 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1869 | if (FunctionNoProtoType *FT = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1870 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1871 | return QualType(FT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1872 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1873 | QualType Canonical; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1874 | if (!ResultTy.isCanonical() || |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1875 | getCanonicalCallConv(CallConv) != CallConv) { |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1876 | Canonical = |
| 1877 | getFunctionNoProtoType(getCanonicalType(ResultTy), |
| 1878 | Info.withCallingConv(getCanonicalCallConv(CallConv))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1879 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1880 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1881 | FunctionNoProtoType *NewIP = |
| 1882 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1883 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1884 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1885 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1886 | FunctionNoProtoType *New = new (*this, TypeAlignment) |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1887 | FunctionNoProtoType(ResultTy, Canonical, Info); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1888 | Types.push_back(New); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1889 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1890 | return QualType(New, 0); |
| 1891 | } |
| 1892 | |
| 1893 | /// getFunctionType - Return a normal function type with a typed argument |
| 1894 | /// list. isVariadic indicates whether the argument list includes '...'. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1895 | QualType |
| 1896 | ASTContext::getFunctionType(QualType ResultTy, |
| 1897 | const QualType *ArgArray, unsigned NumArgs, |
| 1898 | const FunctionProtoType::ExtProtoInfo &EPI) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1899 | // Unique functions, to guarantee there is only one function of a particular |
| 1900 | // structure. |
| 1901 | llvm::FoldingSetNodeID ID; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1902 | FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, EPI); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1903 | |
| 1904 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1905 | if (FunctionProtoType *FTP = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1906 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1907 | return QualType(FTP, 0); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1908 | |
| 1909 | // Determine whether the type being created is already canonical or not. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1910 | bool isCanonical = !EPI.HasExceptionSpec && ResultTy.isCanonical(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1911 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1912 | if (!ArgArray[i].isCanonicalAsParam()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1913 | isCanonical = false; |
| 1914 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1915 | const CallingConv CallConv = EPI.ExtInfo.getCC(); |
| 1916 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1917 | // If this type isn't canonical, get the canonical version of it. |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1918 | // The exception spec is not part of the canonical type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1919 | QualType Canonical; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1920 | if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1921 | llvm::SmallVector<QualType, 16> CanonicalArgs; |
| 1922 | CanonicalArgs.reserve(NumArgs); |
| 1923 | for (unsigned i = 0; i != NumArgs; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1924 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1925 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1926 | FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI; |
| 1927 | if (CanonicalEPI.HasExceptionSpec) { |
| 1928 | CanonicalEPI.HasExceptionSpec = false; |
| 1929 | CanonicalEPI.HasAnyExceptionSpec = false; |
| 1930 | CanonicalEPI.NumExceptions = 0; |
| 1931 | } |
| 1932 | CanonicalEPI.ExtInfo |
| 1933 | = CanonicalEPI.ExtInfo.withCallingConv(getCanonicalCallConv(CallConv)); |
| 1934 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1935 | Canonical = getFunctionType(getCanonicalType(ResultTy), |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1936 | CanonicalArgs.data(), NumArgs, |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1937 | CanonicalEPI); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1938 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1939 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1940 | FunctionProtoType *NewIP = |
| 1941 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1942 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1943 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1944 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1945 | // FunctionProtoType objects are allocated with extra bytes after them |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1946 | // for two variable size arrays (for parameter and exception types) at the |
| 1947 | // end of them. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1948 | size_t Size = sizeof(FunctionProtoType) + |
| 1949 | NumArgs * sizeof(QualType) + |
| 1950 | EPI.NumExceptions * sizeof(QualType); |
| 1951 | FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment); |
| 1952 | new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, Canonical, EPI); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1953 | Types.push_back(FTP); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1954 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1955 | return QualType(FTP, 0); |
| 1956 | } |
| 1957 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1958 | #ifndef NDEBUG |
| 1959 | static bool NeedsInjectedClassNameType(const RecordDecl *D) { |
| 1960 | if (!isa<CXXRecordDecl>(D)) return false; |
| 1961 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 1962 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) |
| 1963 | return true; |
| 1964 | if (RD->getDescribedClassTemplate() && |
| 1965 | !isa<ClassTemplateSpecializationDecl>(RD)) |
| 1966 | return true; |
| 1967 | return false; |
| 1968 | } |
| 1969 | #endif |
| 1970 | |
| 1971 | /// getInjectedClassNameType - Return the unique reference to the |
| 1972 | /// injected class name type for the specified templated declaration. |
| 1973 | QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1974 | QualType TST) const { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1975 | assert(NeedsInjectedClassNameType(Decl)); |
| 1976 | if (Decl->TypeForDecl) { |
| 1977 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
Argyrios Kyrtzidis | 37ffed3 | 2010-07-02 11:55:32 +0000 | [diff] [blame] | 1978 | } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDeclaration()) { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1979 | assert(PrevDecl->TypeForDecl && "previous declaration has no type"); |
| 1980 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 1981 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
| 1982 | } else { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1983 | Type *newType = |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1984 | new (*this, TypeAlignment) InjectedClassNameType(Decl, TST); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1985 | Decl->TypeForDecl = newType; |
| 1986 | Types.push_back(newType); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1987 | } |
| 1988 | return QualType(Decl->TypeForDecl, 0); |
| 1989 | } |
| 1990 | |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1991 | /// getTypeDeclType - Return the unique reference to the type for the |
| 1992 | /// specified type declaration. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1993 | QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const { |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 1994 | assert(Decl && "Passed null for Decl param"); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1995 | assert(!Decl->TypeForDecl && "TypeForDecl present in slow case"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1996 | |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1997 | if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1998 | return getTypedefType(Typedef); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1999 | |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2000 | assert(!isa<TemplateTypeParmDecl>(Decl) && |
| 2001 | "Template type parameter types are always available."); |
| 2002 | |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2003 | if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2004 | assert(!Record->getPreviousDeclaration() && |
| 2005 | "struct/union has previous declaration"); |
| 2006 | assert(!NeedsInjectedClassNameType(Record)); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2007 | return getRecordType(Record); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2008 | } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2009 | assert(!Enum->getPreviousDeclaration() && |
| 2010 | "enum has previous declaration"); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2011 | return getEnumType(Enum); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2012 | } else if (const UnresolvedUsingTypenameDecl *Using = |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2013 | dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2014 | Type *newType = new (*this, TypeAlignment) UnresolvedUsingType(Using); |
| 2015 | Decl->TypeForDecl = newType; |
| 2016 | Types.push_back(newType); |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 2017 | } else |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2018 | llvm_unreachable("TypeDecl without a type?"); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 2019 | |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 2020 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2021 | } |
| 2022 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2023 | /// getTypedefType - Return the unique reference to the type for the |
| 2024 | /// specified typename decl. |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2025 | QualType |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2026 | ASTContext::getTypedefType(const TypedefDecl *Decl, QualType Canonical) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2027 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2028 | |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2029 | if (Canonical.isNull()) |
| 2030 | Canonical = getCanonicalType(Decl->getUnderlyingType()); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2031 | TypedefType *newType = new(*this, TypeAlignment) |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2032 | TypedefType(Type::Typedef, Decl, Canonical); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2033 | Decl->TypeForDecl = newType; |
| 2034 | Types.push_back(newType); |
| 2035 | return QualType(newType, 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2036 | } |
| 2037 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2038 | QualType ASTContext::getRecordType(const RecordDecl *Decl) const { |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2039 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 2040 | |
| 2041 | if (const RecordDecl *PrevDecl = Decl->getPreviousDeclaration()) |
| 2042 | if (PrevDecl->TypeForDecl) |
| 2043 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 2044 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2045 | RecordType *newType = new (*this, TypeAlignment) RecordType(Decl); |
| 2046 | Decl->TypeForDecl = newType; |
| 2047 | Types.push_back(newType); |
| 2048 | return QualType(newType, 0); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2049 | } |
| 2050 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2051 | QualType ASTContext::getEnumType(const EnumDecl *Decl) const { |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2052 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 2053 | |
| 2054 | if (const EnumDecl *PrevDecl = Decl->getPreviousDeclaration()) |
| 2055 | if (PrevDecl->TypeForDecl) |
| 2056 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 2057 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2058 | EnumType *newType = new (*this, TypeAlignment) EnumType(Decl); |
| 2059 | Decl->TypeForDecl = newType; |
| 2060 | Types.push_back(newType); |
| 2061 | return QualType(newType, 0); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2062 | } |
| 2063 | |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 2064 | QualType ASTContext::getAttributedType(AttributedType::Kind attrKind, |
| 2065 | QualType modifiedType, |
| 2066 | QualType equivalentType) { |
| 2067 | llvm::FoldingSetNodeID id; |
| 2068 | AttributedType::Profile(id, attrKind, modifiedType, equivalentType); |
| 2069 | |
| 2070 | void *insertPos = 0; |
| 2071 | AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos); |
| 2072 | if (type) return QualType(type, 0); |
| 2073 | |
| 2074 | QualType canon = getCanonicalType(equivalentType); |
| 2075 | type = new (*this, TypeAlignment) |
| 2076 | AttributedType(canon, attrKind, modifiedType, equivalentType); |
| 2077 | |
| 2078 | Types.push_back(type); |
| 2079 | AttributedTypes.InsertNode(type, insertPos); |
| 2080 | |
| 2081 | return QualType(type, 0); |
| 2082 | } |
| 2083 | |
| 2084 | |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2085 | /// \brief Retrieve a substitution-result type. |
| 2086 | QualType |
| 2087 | ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2088 | QualType Replacement) const { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2089 | assert(Replacement.isCanonical() |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2090 | && "replacement types must always be canonical"); |
| 2091 | |
| 2092 | llvm::FoldingSetNodeID ID; |
| 2093 | SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); |
| 2094 | void *InsertPos = 0; |
| 2095 | SubstTemplateTypeParmType *SubstParm |
| 2096 | = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2097 | |
| 2098 | if (!SubstParm) { |
| 2099 | SubstParm = new (*this, TypeAlignment) |
| 2100 | SubstTemplateTypeParmType(Parm, Replacement); |
| 2101 | Types.push_back(SubstParm); |
| 2102 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 2103 | } |
| 2104 | |
| 2105 | return QualType(SubstParm, 0); |
| 2106 | } |
| 2107 | |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 2108 | /// \brief Retrieve a |
| 2109 | QualType ASTContext::getSubstTemplateTypeParmPackType( |
| 2110 | const TemplateTypeParmType *Parm, |
| 2111 | const TemplateArgument &ArgPack) { |
| 2112 | #ifndef NDEBUG |
| 2113 | for (TemplateArgument::pack_iterator P = ArgPack.pack_begin(), |
| 2114 | PEnd = ArgPack.pack_end(); |
| 2115 | P != PEnd; ++P) { |
| 2116 | assert(P->getKind() == TemplateArgument::Type &&"Pack contains a non-type"); |
| 2117 | assert(P->getAsType().isCanonical() && "Pack contains non-canonical type"); |
| 2118 | } |
| 2119 | #endif |
| 2120 | |
| 2121 | llvm::FoldingSetNodeID ID; |
| 2122 | SubstTemplateTypeParmPackType::Profile(ID, Parm, ArgPack); |
| 2123 | void *InsertPos = 0; |
| 2124 | if (SubstTemplateTypeParmPackType *SubstParm |
| 2125 | = SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2126 | return QualType(SubstParm, 0); |
| 2127 | |
| 2128 | QualType Canon; |
| 2129 | if (!Parm->isCanonicalUnqualified()) { |
| 2130 | Canon = getCanonicalType(QualType(Parm, 0)); |
| 2131 | Canon = getSubstTemplateTypeParmPackType(cast<TemplateTypeParmType>(Canon), |
| 2132 | ArgPack); |
| 2133 | SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2134 | } |
| 2135 | |
| 2136 | SubstTemplateTypeParmPackType *SubstParm |
| 2137 | = new (*this, TypeAlignment) SubstTemplateTypeParmPackType(Parm, Canon, |
| 2138 | ArgPack); |
| 2139 | Types.push_back(SubstParm); |
| 2140 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 2141 | return QualType(SubstParm, 0); |
| 2142 | } |
| 2143 | |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2144 | /// \brief Retrieve the template type parameter type for a template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2145 | /// parameter or parameter pack with the given depth, index, and (optionally) |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2146 | /// name. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2147 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2148 | bool ParameterPack, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2149 | IdentifierInfo *Name) const { |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2150 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | efed5c8 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 2151 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2152 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2153 | TemplateTypeParmType *TypeParm |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2154 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2155 | |
| 2156 | if (TypeParm) |
| 2157 | return QualType(TypeParm, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2158 | |
Douglas Gregor | efed5c8 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 2159 | if (Name) { |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2160 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | efed5c8 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 2161 | TypeParm = new (*this, TypeAlignment) |
| 2162 | TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2163 | |
| 2164 | TemplateTypeParmType *TypeCheck |
| 2165 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2166 | assert(!TypeCheck && "Template type parameter canonical type broken"); |
| 2167 | (void)TypeCheck; |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2168 | } else |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2169 | TypeParm = new (*this, TypeAlignment) |
| 2170 | TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2171 | |
| 2172 | Types.push_back(TypeParm); |
| 2173 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 2174 | |
| 2175 | return QualType(TypeParm, 0); |
| 2176 | } |
| 2177 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2178 | TypeSourceInfo * |
| 2179 | ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name, |
| 2180 | SourceLocation NameLoc, |
| 2181 | const TemplateArgumentListInfo &Args, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2182 | QualType CanonType) const { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2183 | QualType TST = getTemplateSpecializationType(Name, Args, CanonType); |
| 2184 | |
| 2185 | TypeSourceInfo *DI = CreateTypeSourceInfo(TST); |
| 2186 | TemplateSpecializationTypeLoc TL |
| 2187 | = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc()); |
| 2188 | TL.setTemplateNameLoc(NameLoc); |
| 2189 | TL.setLAngleLoc(Args.getLAngleLoc()); |
| 2190 | TL.setRAngleLoc(Args.getRAngleLoc()); |
| 2191 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 2192 | TL.setArgLocInfo(i, Args[i].getLocInfo()); |
| 2193 | return DI; |
| 2194 | } |
| 2195 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2196 | QualType |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2197 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2198 | const TemplateArgumentListInfo &Args, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2199 | QualType Canon) const { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2200 | unsigned NumArgs = Args.size(); |
| 2201 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2202 | llvm::SmallVector<TemplateArgument, 4> ArgVec; |
| 2203 | ArgVec.reserve(NumArgs); |
| 2204 | for (unsigned i = 0; i != NumArgs; ++i) |
| 2205 | ArgVec.push_back(Args[i].getArgument()); |
| 2206 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2207 | return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 2208 | Canon); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2209 | } |
| 2210 | |
| 2211 | QualType |
| 2212 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2213 | const TemplateArgument *Args, |
| 2214 | unsigned NumArgs, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2215 | QualType Canon) const { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 2216 | if (!Canon.isNull()) |
| 2217 | Canon = getCanonicalType(Canon); |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2218 | else |
| 2219 | Canon = getCanonicalTemplateSpecializationType(Template, Args, NumArgs); |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 2220 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2221 | // Allocate the (non-canonical) template specialization type, but don't |
| 2222 | // try to unique it: these types typically have location information that |
| 2223 | // we don't unique and don't want to lose. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2224 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2225 | sizeof(TemplateArgument) * NumArgs), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2226 | TypeAlignment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2227 | TemplateSpecializationType *Spec |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 2228 | = new (Mem) TemplateSpecializationType(Template, |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2229 | Args, NumArgs, |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 2230 | Canon); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2231 | |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2232 | Types.push_back(Spec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2233 | return QualType(Spec, 0); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2234 | } |
| 2235 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2236 | QualType |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2237 | ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template, |
| 2238 | const TemplateArgument *Args, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2239 | unsigned NumArgs) const { |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2240 | // Build the canonical template specialization type. |
| 2241 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); |
| 2242 | llvm::SmallVector<TemplateArgument, 4> CanonArgs; |
| 2243 | CanonArgs.reserve(NumArgs); |
| 2244 | for (unsigned I = 0; I != NumArgs; ++I) |
| 2245 | CanonArgs.push_back(getCanonicalTemplateArgument(Args[I])); |
| 2246 | |
| 2247 | // Determine whether this canonical template specialization type already |
| 2248 | // exists. |
| 2249 | llvm::FoldingSetNodeID ID; |
| 2250 | TemplateSpecializationType::Profile(ID, CanonTemplate, |
| 2251 | CanonArgs.data(), NumArgs, *this); |
| 2252 | |
| 2253 | void *InsertPos = 0; |
| 2254 | TemplateSpecializationType *Spec |
| 2255 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2256 | |
| 2257 | if (!Spec) { |
| 2258 | // Allocate a new canonical template specialization type. |
| 2259 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
| 2260 | sizeof(TemplateArgument) * NumArgs), |
| 2261 | TypeAlignment); |
| 2262 | Spec = new (Mem) TemplateSpecializationType(CanonTemplate, |
| 2263 | CanonArgs.data(), NumArgs, |
| 2264 | QualType()); |
| 2265 | Types.push_back(Spec); |
| 2266 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
| 2267 | } |
| 2268 | |
| 2269 | assert(Spec->isDependentType() && |
| 2270 | "Non-dependent template-id type must have a canonical type"); |
| 2271 | return QualType(Spec, 0); |
| 2272 | } |
| 2273 | |
| 2274 | QualType |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2275 | ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword, |
| 2276 | NestedNameSpecifier *NNS, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2277 | QualType NamedType) const { |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2278 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2279 | ElaboratedType::Profile(ID, Keyword, NNS, NamedType); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2280 | |
| 2281 | void *InsertPos = 0; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2282 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2283 | if (T) |
| 2284 | return QualType(T, 0); |
| 2285 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2286 | QualType Canon = NamedType; |
| 2287 | if (!Canon.isCanonical()) { |
| 2288 | Canon = getCanonicalType(NamedType); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2289 | ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2290 | assert(!CheckT && "Elaborated canonical type broken"); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2291 | (void)CheckT; |
| 2292 | } |
| 2293 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2294 | T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2295 | Types.push_back(T); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2296 | ElaboratedTypes.InsertNode(T, InsertPos); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2297 | return QualType(T, 0); |
| 2298 | } |
| 2299 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 2300 | QualType |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2301 | ASTContext::getParenType(QualType InnerType) const { |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 2302 | llvm::FoldingSetNodeID ID; |
| 2303 | ParenType::Profile(ID, InnerType); |
| 2304 | |
| 2305 | void *InsertPos = 0; |
| 2306 | ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2307 | if (T) |
| 2308 | return QualType(T, 0); |
| 2309 | |
| 2310 | QualType Canon = InnerType; |
| 2311 | if (!Canon.isCanonical()) { |
| 2312 | Canon = getCanonicalType(InnerType); |
| 2313 | ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2314 | assert(!CheckT && "Paren canonical type broken"); |
| 2315 | (void)CheckT; |
| 2316 | } |
| 2317 | |
| 2318 | T = new (*this) ParenType(InnerType, Canon); |
| 2319 | Types.push_back(T); |
| 2320 | ParenTypes.InsertNode(T, InsertPos); |
| 2321 | return QualType(T, 0); |
| 2322 | } |
| 2323 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2324 | QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, |
| 2325 | NestedNameSpecifier *NNS, |
| 2326 | const IdentifierInfo *Name, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2327 | QualType Canon) const { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2328 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 2329 | |
| 2330 | if (Canon.isNull()) { |
| 2331 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2332 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 2333 | if (Keyword == ETK_None) |
| 2334 | CanonKeyword = ETK_Typename; |
| 2335 | |
| 2336 | if (CanonNNS != NNS || CanonKeyword != Keyword) |
| 2337 | Canon = getDependentNameType(CanonKeyword, CanonNNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2338 | } |
| 2339 | |
| 2340 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2341 | DependentNameType::Profile(ID, Keyword, NNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2342 | |
| 2343 | void *InsertPos = 0; |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2344 | DependentNameType *T |
| 2345 | = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2346 | if (T) |
| 2347 | return QualType(T, 0); |
| 2348 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2349 | T = new (*this) DependentNameType(Keyword, NNS, Name, Canon); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2350 | Types.push_back(T); |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2351 | DependentNameTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2352 | return QualType(T, 0); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2353 | } |
| 2354 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2355 | QualType |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2356 | ASTContext::getDependentTemplateSpecializationType( |
| 2357 | ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2358 | NestedNameSpecifier *NNS, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2359 | const IdentifierInfo *Name, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2360 | const TemplateArgumentListInfo &Args) const { |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2361 | // TODO: avoid this copy |
| 2362 | llvm::SmallVector<TemplateArgument, 16> ArgCopy; |
| 2363 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
| 2364 | ArgCopy.push_back(Args[I].getArgument()); |
| 2365 | return getDependentTemplateSpecializationType(Keyword, NNS, Name, |
| 2366 | ArgCopy.size(), |
| 2367 | ArgCopy.data()); |
| 2368 | } |
| 2369 | |
| 2370 | QualType |
| 2371 | ASTContext::getDependentTemplateSpecializationType( |
| 2372 | ElaboratedTypeKeyword Keyword, |
| 2373 | NestedNameSpecifier *NNS, |
| 2374 | const IdentifierInfo *Name, |
| 2375 | unsigned NumArgs, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2376 | const TemplateArgument *Args) const { |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2377 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 2378 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2379 | llvm::FoldingSetNodeID ID; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2380 | DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS, |
| 2381 | Name, NumArgs, Args); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2382 | |
| 2383 | void *InsertPos = 0; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2384 | DependentTemplateSpecializationType *T |
| 2385 | = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2386 | if (T) |
| 2387 | return QualType(T, 0); |
| 2388 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2389 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2390 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2391 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 2392 | if (Keyword == ETK_None) CanonKeyword = ETK_Typename; |
| 2393 | |
| 2394 | bool AnyNonCanonArgs = false; |
| 2395 | llvm::SmallVector<TemplateArgument, 16> CanonArgs(NumArgs); |
| 2396 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 2397 | CanonArgs[I] = getCanonicalTemplateArgument(Args[I]); |
| 2398 | if (!CanonArgs[I].structurallyEquals(Args[I])) |
| 2399 | AnyNonCanonArgs = true; |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2400 | } |
| 2401 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2402 | QualType Canon; |
| 2403 | if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) { |
| 2404 | Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS, |
| 2405 | Name, NumArgs, |
| 2406 | CanonArgs.data()); |
| 2407 | |
| 2408 | // Find the insert position again. |
| 2409 | DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2410 | } |
| 2411 | |
| 2412 | void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) + |
| 2413 | sizeof(TemplateArgument) * NumArgs), |
| 2414 | TypeAlignment); |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 2415 | T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2416 | Name, NumArgs, Args, Canon); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2417 | Types.push_back(T); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2418 | DependentTemplateSpecializationTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2419 | return QualType(T, 0); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2420 | } |
| 2421 | |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2422 | QualType ASTContext::getPackExpansionType(QualType Pattern, |
| 2423 | llvm::Optional<unsigned> NumExpansions) { |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2424 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2425 | PackExpansionType::Profile(ID, Pattern, NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2426 | |
| 2427 | assert(Pattern->containsUnexpandedParameterPack() && |
| 2428 | "Pack expansions must expand one or more parameter packs"); |
| 2429 | void *InsertPos = 0; |
| 2430 | PackExpansionType *T |
| 2431 | = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2432 | if (T) |
| 2433 | return QualType(T, 0); |
| 2434 | |
| 2435 | QualType Canon; |
| 2436 | if (!Pattern.isCanonical()) { |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2437 | Canon = getPackExpansionType(getCanonicalType(Pattern), NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2438 | |
| 2439 | // Find the insert position again. |
| 2440 | PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2441 | } |
| 2442 | |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2443 | T = new (*this) PackExpansionType(Pattern, Canon, NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2444 | Types.push_back(T); |
| 2445 | PackExpansionTypes.InsertNode(T, InsertPos); |
| 2446 | return QualType(T, 0); |
| 2447 | } |
| 2448 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2449 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 2450 | /// alphabetically. |
| 2451 | static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, |
| 2452 | const ObjCProtocolDecl *RHS) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2453 | return LHS->getDeclName() < RHS->getDeclName(); |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2454 | } |
| 2455 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2456 | static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols, |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2457 | unsigned NumProtocols) { |
| 2458 | if (NumProtocols == 0) return true; |
| 2459 | |
| 2460 | for (unsigned i = 1; i != NumProtocols; ++i) |
| 2461 | if (!CmpProtocolNames(Protocols[i-1], Protocols[i])) |
| 2462 | return false; |
| 2463 | return true; |
| 2464 | } |
| 2465 | |
| 2466 | static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols, |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2467 | unsigned &NumProtocols) { |
| 2468 | ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2469 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2470 | // Sort protocols, keyed by name. |
| 2471 | std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); |
| 2472 | |
| 2473 | // Remove duplicates. |
| 2474 | ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); |
| 2475 | NumProtocols = ProtocolsEnd-Protocols; |
| 2476 | } |
| 2477 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2478 | QualType ASTContext::getObjCObjectType(QualType BaseType, |
| 2479 | ObjCProtocolDecl * const *Protocols, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2480 | unsigned NumProtocols) const { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2481 | // If the base type is an interface and there aren't any protocols |
| 2482 | // to add, then the interface type will do just fine. |
| 2483 | if (!NumProtocols && isa<ObjCInterfaceType>(BaseType)) |
| 2484 | return BaseType; |
| 2485 | |
| 2486 | // Look in the folding set for an existing type. |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2487 | llvm::FoldingSetNodeID ID; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2488 | ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2489 | void *InsertPos = 0; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2490 | if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2491 | return QualType(QT, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2492 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2493 | // Build the canonical type, which has the canonical base type and |
| 2494 | // a sorted-and-uniqued list of protocols. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2495 | QualType Canonical; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2496 | bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols); |
| 2497 | if (!ProtocolsSorted || !BaseType.isCanonical()) { |
| 2498 | if (!ProtocolsSorted) { |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 2499 | llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols, |
| 2500 | Protocols + NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2501 | unsigned UniqueCount = NumProtocols; |
| 2502 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2503 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2504 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 2505 | &Sorted[0], UniqueCount); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2506 | } else { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2507 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 2508 | Protocols, NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2509 | } |
| 2510 | |
| 2511 | // Regenerate InsertPos. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2512 | ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2513 | } |
| 2514 | |
| 2515 | unsigned Size = sizeof(ObjCObjectTypeImpl); |
| 2516 | Size += NumProtocols * sizeof(ObjCProtocolDecl *); |
| 2517 | void *Mem = Allocate(Size, TypeAlignment); |
| 2518 | ObjCObjectTypeImpl *T = |
| 2519 | new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols); |
| 2520 | |
| 2521 | Types.push_back(T); |
| 2522 | ObjCObjectTypes.InsertNode(T, InsertPos); |
| 2523 | return QualType(T, 0); |
| 2524 | } |
| 2525 | |
| 2526 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 2527 | /// the given object type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2528 | QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2529 | llvm::FoldingSetNodeID ID; |
| 2530 | ObjCObjectPointerType::Profile(ID, ObjectT); |
| 2531 | |
| 2532 | void *InsertPos = 0; |
| 2533 | if (ObjCObjectPointerType *QT = |
| 2534 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2535 | return QualType(QT, 0); |
| 2536 | |
| 2537 | // Find the canonical object type. |
| 2538 | QualType Canonical; |
| 2539 | if (!ObjectT.isCanonical()) { |
| 2540 | Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT)); |
| 2541 | |
| 2542 | // Regenerate InsertPos. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2543 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2544 | } |
| 2545 | |
Douglas Gregor | fd6a088 | 2010-02-08 22:59:26 +0000 | [diff] [blame] | 2546 | // No match. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2547 | void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment); |
| 2548 | ObjCObjectPointerType *QType = |
| 2549 | new (Mem) ObjCObjectPointerType(Canonical, ObjectT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2550 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2551 | Types.push_back(QType); |
| 2552 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2553 | return QualType(QType, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2554 | } |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2555 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2556 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 2557 | /// specified ObjC interface decl. The list of protocols is optional. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2558 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) const { |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2559 | if (Decl->TypeForDecl) |
| 2560 | return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2561 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2562 | // FIXME: redeclarations? |
| 2563 | void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment); |
| 2564 | ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl); |
| 2565 | Decl->TypeForDecl = T; |
| 2566 | Types.push_back(T); |
| 2567 | return QualType(T, 0); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2568 | } |
| 2569 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2570 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 2571 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2572 | /// multiple declarations that refer to "typeof(x)" all contain different |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2573 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2574 | /// on canonical type's (which are always unique). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2575 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) const { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2576 | TypeOfExprType *toe; |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2577 | if (tofExpr->isTypeDependent()) { |
| 2578 | llvm::FoldingSetNodeID ID; |
| 2579 | DependentTypeOfExprType::Profile(ID, *this, tofExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2580 | |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2581 | void *InsertPos = 0; |
| 2582 | DependentTypeOfExprType *Canon |
| 2583 | = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2584 | if (Canon) { |
| 2585 | // We already have a "canonical" version of an identical, dependent |
| 2586 | // typeof(expr) type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2587 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2588 | QualType((TypeOfExprType*)Canon, 0)); |
| 2589 | } |
| 2590 | else { |
| 2591 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2592 | Canon |
| 2593 | = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2594 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); |
| 2595 | toe = Canon; |
| 2596 | } |
| 2597 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2598 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2599 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2600 | } |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2601 | Types.push_back(toe); |
| 2602 | return QualType(toe, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2603 | } |
| 2604 | |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2605 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 2606 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 2607 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2608 | /// an issue. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2609 | /// on canonical type's (which are always unique). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2610 | QualType ASTContext::getTypeOfType(QualType tofType) const { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2611 | QualType Canonical = getCanonicalType(tofType); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2612 | TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2613 | Types.push_back(tot); |
| 2614 | return QualType(tot, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2615 | } |
| 2616 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2617 | /// getDecltypeForExpr - Given an expr, will return the decltype for that |
| 2618 | /// expression, according to the rules in C++0x [dcl.type.simple]p4 |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2619 | static QualType getDecltypeForExpr(const Expr *e, const ASTContext &Context) { |
Anders Carlsson | a07c33e | 2009-06-25 15:00:34 +0000 | [diff] [blame] | 2620 | if (e->isTypeDependent()) |
| 2621 | return Context.DependentTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2622 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2623 | // If e is an id expression or a class member access, decltype(e) is defined |
| 2624 | // as the type of the entity named by e. |
| 2625 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) { |
| 2626 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) |
| 2627 | return VD->getType(); |
| 2628 | } |
| 2629 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) { |
| 2630 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) |
| 2631 | return FD->getType(); |
| 2632 | } |
| 2633 | // If e is a function call or an invocation of an overloaded operator, |
| 2634 | // (parentheses around e are ignored), decltype(e) is defined as the |
| 2635 | // return type of that function. |
| 2636 | if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens())) |
| 2637 | return CE->getCallReturnType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2638 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2639 | QualType T = e->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2640 | |
| 2641 | // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2642 | // defined as T&, otherwise decltype(e) is defined as T. |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 2643 | if (e->isLValue()) |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2644 | T = Context.getLValueReferenceType(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2645 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2646 | return T; |
| 2647 | } |
| 2648 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2649 | /// getDecltypeType - Unlike many "get<Type>" functions, we don't unique |
| 2650 | /// DecltypeType AST's. The only motivation to unique these nodes would be |
| 2651 | /// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2652 | /// an issue. This doesn't effect the type checker, since it operates |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2653 | /// on canonical type's (which are always unique). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2654 | QualType ASTContext::getDecltypeType(Expr *e) const { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2655 | DecltypeType *dt; |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2656 | if (e->isTypeDependent()) { |
| 2657 | llvm::FoldingSetNodeID ID; |
| 2658 | DependentDecltypeType::Profile(ID, *this, e); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2659 | |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2660 | void *InsertPos = 0; |
| 2661 | DependentDecltypeType *Canon |
| 2662 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2663 | if (Canon) { |
| 2664 | // We already have a "canonical" version of an equivalent, dependent |
| 2665 | // decltype type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2666 | dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy, |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2667 | QualType((DecltypeType*)Canon, 0)); |
| 2668 | } |
| 2669 | else { |
| 2670 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2671 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2672 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); |
| 2673 | dt = Canon; |
| 2674 | } |
| 2675 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2676 | QualType T = getDecltypeForExpr(e, *this); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2677 | dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T)); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2678 | } |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2679 | Types.push_back(dt); |
| 2680 | return QualType(dt, 0); |
| 2681 | } |
| 2682 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2683 | /// getTagDeclType - Return the unique reference to the type for the |
| 2684 | /// specified TagDecl (struct/union/class/enum) decl. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2685 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) const { |
Ted Kremenek | d778f88 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 2686 | assert (Decl); |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 2687 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 2688 | // away const? mutable? |
| 2689 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2690 | } |
| 2691 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2692 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 2693 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 2694 | /// needs to agree with the definition in <stddef.h>. |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 2695 | CanQualType ASTContext::getSizeType() const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2696 | return getFromTargetType(Target.getSizeType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2697 | } |
| 2698 | |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 2699 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 2700 | /// Used when in C++, as a GCC extension. |
| 2701 | QualType ASTContext::getSignedWCharType() const { |
| 2702 | // FIXME: derive from "Target" ? |
| 2703 | return WCharTy; |
| 2704 | } |
| 2705 | |
| 2706 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 2707 | /// Used when in C++, as a GCC extension. |
| 2708 | QualType ASTContext::getUnsignedWCharType() const { |
| 2709 | // FIXME: derive from "Target" ? |
| 2710 | return UnsignedIntTy; |
| 2711 | } |
| 2712 | |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 2713 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) |
| 2714 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 2715 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2716 | return getFromTargetType(Target.getPtrDiffType(0)); |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 2717 | } |
| 2718 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2719 | //===----------------------------------------------------------------------===// |
| 2720 | // Type Operators |
| 2721 | //===----------------------------------------------------------------------===// |
| 2722 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2723 | CanQualType ASTContext::getCanonicalParamType(QualType T) const { |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2724 | // Push qualifiers into arrays, and then discard any remaining |
| 2725 | // qualifiers. |
| 2726 | T = getCanonicalType(T); |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2727 | T = getVariableArrayDecayedType(T); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2728 | const Type *Ty = T.getTypePtr(); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2729 | QualType Result; |
| 2730 | if (isa<ArrayType>(Ty)) { |
| 2731 | Result = getArrayDecayedType(QualType(Ty,0)); |
| 2732 | } else if (isa<FunctionType>(Ty)) { |
| 2733 | Result = getPointerType(QualType(Ty, 0)); |
| 2734 | } else { |
| 2735 | Result = QualType(Ty, 0); |
| 2736 | } |
| 2737 | |
| 2738 | return CanQualType::CreateUnsafe(Result); |
| 2739 | } |
| 2740 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2741 | |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 2742 | QualType ASTContext::getUnqualifiedArrayType(QualType type, |
| 2743 | Qualifiers &quals) { |
| 2744 | SplitQualType splitType = type.getSplitUnqualifiedType(); |
| 2745 | |
| 2746 | // FIXME: getSplitUnqualifiedType() actually walks all the way to |
| 2747 | // the unqualified desugared type and then drops it on the floor. |
| 2748 | // We then have to strip that sugar back off with |
| 2749 | // getUnqualifiedDesugaredType(), which is silly. |
| 2750 | const ArrayType *AT = |
| 2751 | dyn_cast<ArrayType>(splitType.first->getUnqualifiedDesugaredType()); |
| 2752 | |
| 2753 | // If we don't have an array, just use the results in splitType. |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2754 | if (!AT) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 2755 | quals = splitType.second; |
| 2756 | return QualType(splitType.first, 0); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2757 | } |
| 2758 | |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 2759 | // Otherwise, recurse on the array's element type. |
| 2760 | QualType elementType = AT->getElementType(); |
| 2761 | QualType unqualElementType = getUnqualifiedArrayType(elementType, quals); |
| 2762 | |
| 2763 | // If that didn't change the element type, AT has no qualifiers, so we |
| 2764 | // can just use the results in splitType. |
| 2765 | if (elementType == unqualElementType) { |
| 2766 | assert(quals.empty()); // from the recursive call |
| 2767 | quals = splitType.second; |
| 2768 | return QualType(splitType.first, 0); |
| 2769 | } |
| 2770 | |
| 2771 | // Otherwise, add in the qualifiers from the outermost type, then |
| 2772 | // build the type back up. |
| 2773 | quals.addConsistentQualifiers(splitType.second); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2774 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2775 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 2776 | return getConstantArrayType(unqualElementType, CAT->getSize(), |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2777 | CAT->getSizeModifier(), 0); |
| 2778 | } |
| 2779 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2780 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 2781 | return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2782 | } |
| 2783 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2784 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 2785 | return getVariableArrayType(unqualElementType, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 2786 | VAT->getSizeExpr(), |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2787 | VAT->getSizeModifier(), |
| 2788 | VAT->getIndexTypeCVRQualifiers(), |
| 2789 | VAT->getBracketsRange()); |
| 2790 | } |
| 2791 | |
| 2792 | const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT); |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 2793 | return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(), |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2794 | DSAT->getSizeModifier(), 0, |
| 2795 | SourceRange()); |
| 2796 | } |
| 2797 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2798 | /// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that |
| 2799 | /// may be similar (C++ 4.4), replaces T1 and T2 with the type that |
| 2800 | /// they point to and return true. If T1 and T2 aren't pointer types |
| 2801 | /// or pointer-to-member types, or if they are not similar at this |
| 2802 | /// level, returns false and leaves T1 and T2 unchanged. Top-level |
| 2803 | /// qualifiers on T1 and T2 are ignored. This function will typically |
| 2804 | /// be called in a loop that successively "unwraps" pointer and |
| 2805 | /// pointer-to-member types to compare them at each level. |
| 2806 | bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) { |
| 2807 | const PointerType *T1PtrType = T1->getAs<PointerType>(), |
| 2808 | *T2PtrType = T2->getAs<PointerType>(); |
| 2809 | if (T1PtrType && T2PtrType) { |
| 2810 | T1 = T1PtrType->getPointeeType(); |
| 2811 | T2 = T2PtrType->getPointeeType(); |
| 2812 | return true; |
| 2813 | } |
| 2814 | |
| 2815 | const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(), |
| 2816 | *T2MPType = T2->getAs<MemberPointerType>(); |
| 2817 | if (T1MPType && T2MPType && |
| 2818 | hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0), |
| 2819 | QualType(T2MPType->getClass(), 0))) { |
| 2820 | T1 = T1MPType->getPointeeType(); |
| 2821 | T2 = T2MPType->getPointeeType(); |
| 2822 | return true; |
| 2823 | } |
| 2824 | |
| 2825 | if (getLangOptions().ObjC1) { |
| 2826 | const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(), |
| 2827 | *T2OPType = T2->getAs<ObjCObjectPointerType>(); |
| 2828 | if (T1OPType && T2OPType) { |
| 2829 | T1 = T1OPType->getPointeeType(); |
| 2830 | T2 = T2OPType->getPointeeType(); |
| 2831 | return true; |
| 2832 | } |
| 2833 | } |
| 2834 | |
| 2835 | // FIXME: Block pointers, too? |
| 2836 | |
| 2837 | return false; |
| 2838 | } |
| 2839 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2840 | DeclarationNameInfo |
| 2841 | ASTContext::getNameForTemplate(TemplateName Name, |
| 2842 | SourceLocation NameLoc) const { |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2843 | if (TemplateDecl *TD = Name.getAsTemplateDecl()) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2844 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 2845 | return DeclarationNameInfo(TD->getDeclName(), NameLoc); |
| 2846 | |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2847 | if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2848 | DeclarationName DName; |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2849 | if (DTN->isIdentifier()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2850 | DName = DeclarationNames.getIdentifier(DTN->getIdentifier()); |
| 2851 | return DeclarationNameInfo(DName, NameLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2852 | } else { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2853 | DName = DeclarationNames.getCXXOperatorName(DTN->getOperator()); |
| 2854 | // DNInfo work in progress: FIXME: source locations? |
| 2855 | DeclarationNameLoc DNLoc; |
| 2856 | DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding(); |
| 2857 | DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding(); |
| 2858 | return DeclarationNameInfo(DName, NameLoc, DNLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2859 | } |
| 2860 | } |
| 2861 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 2862 | OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate(); |
| 2863 | assert(Storage); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2864 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 2865 | return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2866 | } |
| 2867 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2868 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const { |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 2869 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 2870 | if (TemplateTemplateParmDecl *TTP |
| 2871 | = dyn_cast<TemplateTemplateParmDecl>(Template)) |
| 2872 | Template = getCanonicalTemplateTemplateParmDecl(TTP); |
| 2873 | |
| 2874 | // The canonical template name is the canonical template declaration. |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 2875 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 2876 | } |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2877 | |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 2878 | if (SubstTemplateTemplateParmPackStorage *SubstPack |
| 2879 | = Name.getAsSubstTemplateTemplateParmPack()) { |
| 2880 | TemplateTemplateParmDecl *CanonParam |
| 2881 | = getCanonicalTemplateTemplateParmDecl(SubstPack->getParameterPack()); |
| 2882 | TemplateArgument CanonArgPack |
| 2883 | = getCanonicalTemplateArgument(SubstPack->getArgumentPack()); |
| 2884 | return getSubstTemplateTemplateParmPack(CanonParam, CanonArgPack); |
| 2885 | } |
| 2886 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 2887 | assert(!Name.getAsOverloadedTemplate()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2888 | |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2889 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 2890 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 2891 | return DTN->CanonicalTemplateName; |
| 2892 | } |
| 2893 | |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 2894 | bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) { |
| 2895 | X = getCanonicalTemplateName(X); |
| 2896 | Y = getCanonicalTemplateName(Y); |
| 2897 | return X.getAsVoidPointer() == Y.getAsVoidPointer(); |
| 2898 | } |
| 2899 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2900 | TemplateArgument |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2901 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const { |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2902 | switch (Arg.getKind()) { |
| 2903 | case TemplateArgument::Null: |
| 2904 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2905 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2906 | case TemplateArgument::Expression: |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2907 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2908 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2909 | case TemplateArgument::Declaration: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2910 | return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2911 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2912 | case TemplateArgument::Template: |
| 2913 | return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate())); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2914 | |
| 2915 | case TemplateArgument::TemplateExpansion: |
| 2916 | return TemplateArgument(getCanonicalTemplateName( |
| 2917 | Arg.getAsTemplateOrTemplatePattern()), |
Douglas Gregor | 2be29f4 | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 2918 | Arg.getNumTemplateExpansions()); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2919 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2920 | case TemplateArgument::Integral: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2921 | return TemplateArgument(*Arg.getAsIntegral(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2922 | getCanonicalType(Arg.getIntegralType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2923 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2924 | case TemplateArgument::Type: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2925 | return TemplateArgument(getCanonicalType(Arg.getAsType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2926 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2927 | case TemplateArgument::Pack: { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 2928 | if (Arg.pack_size() == 0) |
| 2929 | return Arg; |
| 2930 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2931 | TemplateArgument *CanonArgs |
| 2932 | = new (*this) TemplateArgument[Arg.pack_size()]; |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2933 | unsigned Idx = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2934 | for (TemplateArgument::pack_iterator A = Arg.pack_begin(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2935 | AEnd = Arg.pack_end(); |
| 2936 | A != AEnd; (void)++A, ++Idx) |
| 2937 | CanonArgs[Idx] = getCanonicalTemplateArgument(*A); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2938 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2939 | return TemplateArgument(CanonArgs, Arg.pack_size()); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2940 | } |
| 2941 | } |
| 2942 | |
| 2943 | // Silence GCC warning |
| 2944 | assert(false && "Unhandled template argument kind"); |
| 2945 | return TemplateArgument(); |
| 2946 | } |
| 2947 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2948 | NestedNameSpecifier * |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2949 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2950 | if (!NNS) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2951 | return 0; |
| 2952 | |
| 2953 | switch (NNS->getKind()) { |
| 2954 | case NestedNameSpecifier::Identifier: |
| 2955 | // Canonicalize the prefix but keep the identifier the same. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2956 | return NestedNameSpecifier::Create(*this, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2957 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 2958 | NNS->getAsIdentifier()); |
| 2959 | |
| 2960 | case NestedNameSpecifier::Namespace: |
| 2961 | // A namespace is canonical; build a nested-name-specifier with |
| 2962 | // this namespace and no prefix. |
| 2963 | return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace()); |
| 2964 | |
| 2965 | case NestedNameSpecifier::TypeSpec: |
| 2966 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 2967 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 2968 | |
| 2969 | // If we have some kind of dependent-named type (e.g., "typename T::type"), |
| 2970 | // break it apart into its prefix and identifier, then reconsititute those |
| 2971 | // as the canonical nested-name-specifier. This is required to canonicalize |
| 2972 | // a dependent nested-name-specifier involving typedefs of dependent-name |
| 2973 | // types, e.g., |
| 2974 | // typedef typename T::type T1; |
| 2975 | // typedef typename T1::type T2; |
| 2976 | if (const DependentNameType *DNT = T->getAs<DependentNameType>()) { |
| 2977 | NestedNameSpecifier *Prefix |
| 2978 | = getCanonicalNestedNameSpecifier(DNT->getQualifier()); |
| 2979 | return NestedNameSpecifier::Create(*this, Prefix, |
| 2980 | const_cast<IdentifierInfo *>(DNT->getIdentifier())); |
| 2981 | } |
| 2982 | |
Douglas Gregor | 643f843 | 2010-11-04 00:14:23 +0000 | [diff] [blame] | 2983 | // Do the same thing as above, but with dependent-named specializations. |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 2984 | if (const DependentTemplateSpecializationType *DTST |
| 2985 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 2986 | NestedNameSpecifier *Prefix |
| 2987 | = getCanonicalNestedNameSpecifier(DTST->getQualifier()); |
| 2988 | TemplateName Name |
| 2989 | = getDependentTemplateName(Prefix, DTST->getIdentifier()); |
| 2990 | T = getTemplateSpecializationType(Name, |
| 2991 | DTST->getArgs(), DTST->getNumArgs()); |
| 2992 | T = getCanonicalType(T); |
| 2993 | } |
| 2994 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2995 | return NestedNameSpecifier::Create(*this, 0, false, |
| 2996 | const_cast<Type*>(T.getTypePtr())); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2997 | } |
| 2998 | |
| 2999 | case NestedNameSpecifier::Global: |
| 3000 | // The global specifier is canonical and unique. |
| 3001 | return NNS; |
| 3002 | } |
| 3003 | |
| 3004 | // Required to silence a GCC warning |
| 3005 | return 0; |
| 3006 | } |
| 3007 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3008 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3009 | const ArrayType *ASTContext::getAsArrayType(QualType T) const { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3010 | // Handle the non-qualified case efficiently. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3011 | if (!T.hasLocalQualifiers()) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3012 | // Handle the common positive case fast. |
| 3013 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 3014 | return AT; |
| 3015 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3016 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3017 | // Handle the common negative case fast. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3018 | if (!isa<ArrayType>(T.getCanonicalType())) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3019 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3020 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3021 | // Apply any qualifiers from the array type to the element type. This |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3022 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 3023 | // any type qualifiers, the element type is so qualified, not the array type." |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3024 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3025 | // If we get here, we either have type qualifiers on the type, or we have |
| 3026 | // sugar such as a typedef in the way. If we have type qualifiers on the type |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 3027 | // we must propagate them down into the element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3028 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3029 | SplitQualType split = T.getSplitDesugaredType(); |
| 3030 | Qualifiers qs = split.second; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3031 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3032 | // If we have a simple case, just return now. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3033 | const ArrayType *ATy = dyn_cast<ArrayType>(split.first); |
| 3034 | if (ATy == 0 || qs.empty()) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3035 | return ATy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3036 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3037 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 3038 | // qualifiers into the array element type and return a new array type. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3039 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3040 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3041 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 3042 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 3043 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3044 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3045 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 3046 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 3047 | IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3048 | IAT->getIndexTypeCVRQualifiers())); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3049 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3050 | if (const DependentSizedArrayType *DSAT |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3051 | = dyn_cast<DependentSizedArrayType>(ATy)) |
| 3052 | return cast<ArrayType>( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3053 | getDependentSizedArrayType(NewEltTy, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3054 | DSAT->getSizeExpr(), |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3055 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3056 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3057 | DSAT->getBracketsRange())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3058 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3059 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3060 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3061 | VAT->getSizeExpr(), |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3062 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3063 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3064 | VAT->getBracketsRange())); |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 3065 | } |
| 3066 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3067 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 3068 | /// specified array type to a pointer. This operation is non-trivial when |
| 3069 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 3070 | /// this returns a pointer to a properly qualified element of the array. |
| 3071 | /// |
| 3072 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3073 | QualType ASTContext::getArrayDecayedType(QualType Ty) const { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3074 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 3075 | // typedefs in the element type of the array. This also handles propagation |
| 3076 | // of type qualifiers from the array type into the element type if present |
| 3077 | // (C99 6.7.3p8). |
| 3078 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 3079 | assert(PrettyArrayType && "Not an array type!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3080 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3081 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3082 | |
| 3083 | // int x[restrict 4] -> int *restrict |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3084 | return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3085 | } |
| 3086 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3087 | QualType ASTContext::getBaseElementType(const ArrayType *array) const { |
| 3088 | return getBaseElementType(array->getElementType()); |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 3089 | } |
| 3090 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3091 | QualType ASTContext::getBaseElementType(QualType type) const { |
| 3092 | Qualifiers qs; |
| 3093 | while (true) { |
| 3094 | SplitQualType split = type.getSplitDesugaredType(); |
| 3095 | const ArrayType *array = split.first->getAsArrayTypeUnsafe(); |
| 3096 | if (!array) break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3097 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3098 | type = array->getElementType(); |
| 3099 | qs.addConsistentQualifiers(split.second); |
| 3100 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3101 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3102 | return getQualifiedType(type, qs); |
Anders Carlsson | 6183a99 | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 3103 | } |
| 3104 | |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 3105 | /// getConstantArrayElementCount - Returns number of constant array elements. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3106 | uint64_t |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 3107 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
| 3108 | uint64_t ElementCount = 1; |
| 3109 | do { |
| 3110 | ElementCount *= CA->getSize().getZExtValue(); |
| 3111 | CA = dyn_cast<ConstantArrayType>(CA->getElementType()); |
| 3112 | } while (CA); |
| 3113 | return ElementCount; |
| 3114 | } |
| 3115 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3116 | /// getFloatingRank - Return a relative rank for floating point types. |
| 3117 | /// This routine will assert if passed a built-in type that isn't a float. |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3118 | static FloatingRank getFloatingRank(QualType T) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3119 | if (const ComplexType *CT = T->getAs<ComplexType>()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3120 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3121 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3122 | assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"); |
| 3123 | switch (T->getAs<BuiltinType>()->getKind()) { |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3124 | default: assert(0 && "getFloatingRank(): not a floating type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3125 | case BuiltinType::Float: return FloatRank; |
| 3126 | case BuiltinType::Double: return DoubleRank; |
| 3127 | case BuiltinType::LongDouble: return LongDoubleRank; |
| 3128 | } |
| 3129 | } |
| 3130 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3131 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 3132 | /// point or a complex type (based on typeDomain/typeSize). |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 3133 | /// 'typeDomain' is a real floating point or complex type. |
| 3134 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 3135 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 3136 | QualType Domain) const { |
| 3137 | FloatingRank EltRank = getFloatingRank(Size); |
| 3138 | if (Domain->isComplexType()) { |
| 3139 | switch (EltRank) { |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 3140 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 3141 | case FloatRank: return FloatComplexTy; |
| 3142 | case DoubleRank: return DoubleComplexTy; |
| 3143 | case LongDoubleRank: return LongDoubleComplexTy; |
| 3144 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3145 | } |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 3146 | |
| 3147 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 3148 | switch (EltRank) { |
| 3149 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
| 3150 | case FloatRank: return FloatTy; |
| 3151 | case DoubleRank: return DoubleTy; |
| 3152 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 3153 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3154 | } |
| 3155 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3156 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 3157 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 3158 | /// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3159 | /// LHS < RHS, return -1. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3160 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const { |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3161 | FloatingRank LHSR = getFloatingRank(LHS); |
| 3162 | FloatingRank RHSR = getFloatingRank(RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3163 | |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3164 | if (LHSR == RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 3165 | return 0; |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3166 | if (LHSR > RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 3167 | return 1; |
| 3168 | return -1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3169 | } |
| 3170 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3171 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 3172 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 3173 | /// or if it is not canonicalized. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3174 | unsigned ASTContext::getIntegerRank(const Type *T) const { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 3175 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3176 | if (const EnumType* ET = dyn_cast<EnumType>(T)) |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 3177 | T = ET->getDecl()->getPromotionType().getTypePtr(); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3178 | |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 3179 | if (T->isSpecificBuiltinType(BuiltinType::WChar_S) || |
| 3180 | T->isSpecificBuiltinType(BuiltinType::WChar_U)) |
Eli Friedman | a342675 | 2009-07-05 23:44:27 +0000 | [diff] [blame] | 3181 | T = getFromTargetType(Target.getWCharType()).getTypePtr(); |
| 3182 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3183 | if (T->isSpecificBuiltinType(BuiltinType::Char16)) |
| 3184 | T = getFromTargetType(Target.getChar16Type()).getTypePtr(); |
| 3185 | |
| 3186 | if (T->isSpecificBuiltinType(BuiltinType::Char32)) |
| 3187 | T = getFromTargetType(Target.getChar32Type()).getTypePtr(); |
| 3188 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3189 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3190 | default: assert(0 && "getIntegerRank(): not a built-in integer"); |
| 3191 | case BuiltinType::Bool: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3192 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3193 | case BuiltinType::Char_S: |
| 3194 | case BuiltinType::Char_U: |
| 3195 | case BuiltinType::SChar: |
| 3196 | case BuiltinType::UChar: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3197 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3198 | case BuiltinType::Short: |
| 3199 | case BuiltinType::UShort: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3200 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3201 | case BuiltinType::Int: |
| 3202 | case BuiltinType::UInt: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3203 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3204 | case BuiltinType::Long: |
| 3205 | case BuiltinType::ULong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3206 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3207 | case BuiltinType::LongLong: |
| 3208 | case BuiltinType::ULongLong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3209 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 3210 | case BuiltinType::Int128: |
| 3211 | case BuiltinType::UInt128: |
| 3212 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3213 | } |
| 3214 | } |
| 3215 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3216 | /// \brief Whether this is a promotable bitfield reference according |
| 3217 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
| 3218 | /// |
| 3219 | /// \returns the type this bit-field will promote to, or NULL if no |
| 3220 | /// promotion occurs. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3221 | QualType ASTContext::isPromotableBitField(Expr *E) const { |
Douglas Gregor | ceafbde | 2010-05-24 20:13:53 +0000 | [diff] [blame] | 3222 | if (E->isTypeDependent() || E->isValueDependent()) |
| 3223 | return QualType(); |
| 3224 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3225 | FieldDecl *Field = E->getBitField(); |
| 3226 | if (!Field) |
| 3227 | return QualType(); |
| 3228 | |
| 3229 | QualType FT = Field->getType(); |
| 3230 | |
| 3231 | llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this); |
| 3232 | uint64_t BitWidth = BitWidthAP.getZExtValue(); |
| 3233 | uint64_t IntSize = getTypeSize(IntTy); |
| 3234 | // GCC extension compatibility: if the bit-field size is less than or equal |
| 3235 | // to the size of int, it gets promoted no matter what its type is. |
| 3236 | // For instance, unsigned long bf : 4 gets promoted to signed int. |
| 3237 | if (BitWidth < IntSize) |
| 3238 | return IntTy; |
| 3239 | |
| 3240 | if (BitWidth == IntSize) |
| 3241 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
| 3242 | |
| 3243 | // Types bigger than int are not subject to promotions, and therefore act |
| 3244 | // like the base type. |
| 3245 | // FIXME: This doesn't quite match what gcc does, but what gcc does here |
| 3246 | // is ridiculous. |
| 3247 | return QualType(); |
| 3248 | } |
| 3249 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3250 | /// getPromotedIntegerType - Returns the type that Promotable will |
| 3251 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
| 3252 | /// integer type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3253 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) const { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3254 | assert(!Promotable.isNull()); |
| 3255 | assert(Promotable->isPromotableIntegerType()); |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 3256 | if (const EnumType *ET = Promotable->getAs<EnumType>()) |
| 3257 | return ET->getDecl()->getPromotionType(); |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3258 | if (Promotable->isSignedIntegerType()) |
| 3259 | return IntTy; |
| 3260 | uint64_t PromotableSize = getTypeSize(Promotable); |
| 3261 | uint64_t IntSize = getTypeSize(IntTy); |
| 3262 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
| 3263 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
| 3264 | } |
| 3265 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3266 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3267 | /// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3268 | /// LHS < RHS, return -1. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3269 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3270 | const Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 3271 | const Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3272 | if (LHSC == RHSC) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3273 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3274 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 3275 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3276 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3277 | unsigned LHSRank = getIntegerRank(LHSC); |
| 3278 | unsigned RHSRank = getIntegerRank(RHSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3279 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3280 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 3281 | if (LHSRank == RHSRank) return 0; |
| 3282 | return LHSRank > RHSRank ? 1 : -1; |
| 3283 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3284 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3285 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 3286 | if (LHSUnsigned) { |
| 3287 | // If the unsigned [LHS] type is larger, return it. |
| 3288 | if (LHSRank >= RHSRank) |
| 3289 | return 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3290 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3291 | // If the signed type can represent all values of the unsigned type, it |
| 3292 | // wins. Because we are dealing with 2's complement and types that are |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3293 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3294 | return -1; |
| 3295 | } |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3296 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3297 | // If the unsigned [RHS] type is larger, return it. |
| 3298 | if (RHSRank >= LHSRank) |
| 3299 | return -1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3300 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3301 | // If the signed type can represent all values of the unsigned type, it |
| 3302 | // wins. Because we are dealing with 2's complement and types that are |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3303 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3304 | return 1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3305 | } |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3306 | |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3307 | static RecordDecl * |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3308 | CreateRecordDecl(const ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC, |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3309 | SourceLocation L, IdentifierInfo *Id) { |
| 3310 | if (Ctx.getLangOptions().CPlusPlus) |
| 3311 | return CXXRecordDecl::Create(Ctx, TK, DC, L, Id); |
| 3312 | else |
| 3313 | return RecordDecl::Create(Ctx, TK, DC, L, Id); |
| 3314 | } |
| 3315 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3316 | // getCFConstantStringType - Return the type used for constant CFStrings. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3317 | QualType ASTContext::getCFConstantStringType() const { |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3318 | if (!CFConstantStringTypeDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3319 | CFConstantStringTypeDecl = |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3320 | CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3321 | &Idents.get("NSConstantString")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3322 | CFConstantStringTypeDecl->startDefinition(); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3323 | |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 3324 | QualType FieldTypes[4]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3325 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3326 | // const int *isa; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3327 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 3328 | // int flags; |
| 3329 | FieldTypes[1] = IntTy; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3330 | // const char *str; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3331 | FieldTypes[2] = getPointerType(CharTy.withConst()); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3332 | // long length; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3333 | FieldTypes[3] = LongTy; |
| 3334 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3335 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3336 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3337 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3338 | SourceLocation(), 0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3339 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3340 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 3341 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3342 | Field->setAccess(AS_public); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3343 | CFConstantStringTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3344 | } |
| 3345 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3346 | CFConstantStringTypeDecl->completeDefinition(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3347 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3348 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3349 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 8467583 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 3350 | } |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 3351 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3352 | void ASTContext::setCFConstantStringType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3353 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3354 | assert(Rec && "Invalid CFConstantStringType"); |
| 3355 | CFConstantStringTypeDecl = Rec->getDecl(); |
| 3356 | } |
| 3357 | |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 3358 | // getNSConstantStringType - Return the type used for constant NSStrings. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3359 | QualType ASTContext::getNSConstantStringType() const { |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 3360 | if (!NSConstantStringTypeDecl) { |
| 3361 | NSConstantStringTypeDecl = |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3362 | CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 3363 | &Idents.get("__builtin_NSString")); |
| 3364 | NSConstantStringTypeDecl->startDefinition(); |
| 3365 | |
| 3366 | QualType FieldTypes[3]; |
| 3367 | |
| 3368 | // const int *isa; |
| 3369 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
| 3370 | // const char *str; |
| 3371 | FieldTypes[1] = getPointerType(CharTy.withConst()); |
| 3372 | // unsigned int length; |
| 3373 | FieldTypes[2] = UnsignedIntTy; |
| 3374 | |
| 3375 | // Create fields |
| 3376 | for (unsigned i = 0; i < 3; ++i) { |
| 3377 | FieldDecl *Field = FieldDecl::Create(*this, NSConstantStringTypeDecl, |
| 3378 | SourceLocation(), 0, |
| 3379 | FieldTypes[i], /*TInfo=*/0, |
| 3380 | /*BitWidth=*/0, |
| 3381 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3382 | Field->setAccess(AS_public); |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 3383 | NSConstantStringTypeDecl->addDecl(Field); |
| 3384 | } |
| 3385 | |
| 3386 | NSConstantStringTypeDecl->completeDefinition(); |
| 3387 | } |
| 3388 | |
| 3389 | return getTagDeclType(NSConstantStringTypeDecl); |
| 3390 | } |
| 3391 | |
| 3392 | void ASTContext::setNSConstantStringType(QualType T) { |
| 3393 | const RecordType *Rec = T->getAs<RecordType>(); |
| 3394 | assert(Rec && "Invalid NSConstantStringType"); |
| 3395 | NSConstantStringTypeDecl = Rec->getDecl(); |
| 3396 | } |
| 3397 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3398 | QualType ASTContext::getObjCFastEnumerationStateType() const { |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3399 | if (!ObjCFastEnumerationStateTypeDecl) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3400 | ObjCFastEnumerationStateTypeDecl = |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3401 | CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3402 | &Idents.get("__objcFastEnumerationState")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3403 | ObjCFastEnumerationStateTypeDecl->startDefinition(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3404 | |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3405 | QualType FieldTypes[] = { |
| 3406 | UnsignedLongTy, |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3407 | getPointerType(ObjCIdTypedefType), |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3408 | getPointerType(UnsignedLongTy), |
| 3409 | getConstantArrayType(UnsignedLongTy, |
| 3410 | llvm::APInt(32, 5), ArrayType::Normal, 0) |
| 3411 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3412 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3413 | for (size_t i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3414 | FieldDecl *Field = FieldDecl::Create(*this, |
| 3415 | ObjCFastEnumerationStateTypeDecl, |
| 3416 | SourceLocation(), 0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3417 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3418 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 3419 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3420 | Field->setAccess(AS_public); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3421 | ObjCFastEnumerationStateTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3422 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3423 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3424 | ObjCFastEnumerationStateTypeDecl->completeDefinition(); |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3425 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3426 | |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3427 | return getTagDeclType(ObjCFastEnumerationStateTypeDecl); |
| 3428 | } |
| 3429 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3430 | QualType ASTContext::getBlockDescriptorType() const { |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3431 | if (BlockDescriptorType) |
| 3432 | return getTagDeclType(BlockDescriptorType); |
| 3433 | |
| 3434 | RecordDecl *T; |
| 3435 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3436 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3437 | &Idents.get("__block_descriptor")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3438 | T->startDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3439 | |
| 3440 | QualType FieldTypes[] = { |
| 3441 | UnsignedLongTy, |
| 3442 | UnsignedLongTy, |
| 3443 | }; |
| 3444 | |
| 3445 | const char *FieldNames[] = { |
| 3446 | "reserved", |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3447 | "Size" |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3448 | }; |
| 3449 | |
| 3450 | for (size_t i = 0; i < 2; ++i) { |
| 3451 | FieldDecl *Field = FieldDecl::Create(*this, |
| 3452 | T, |
| 3453 | SourceLocation(), |
| 3454 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3455 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3456 | /*BitWidth=*/0, |
| 3457 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3458 | Field->setAccess(AS_public); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3459 | T->addDecl(Field); |
| 3460 | } |
| 3461 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3462 | T->completeDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3463 | |
| 3464 | BlockDescriptorType = T; |
| 3465 | |
| 3466 | return getTagDeclType(BlockDescriptorType); |
| 3467 | } |
| 3468 | |
| 3469 | void ASTContext::setBlockDescriptorType(QualType T) { |
| 3470 | const RecordType *Rec = T->getAs<RecordType>(); |
| 3471 | assert(Rec && "Invalid BlockDescriptorType"); |
| 3472 | BlockDescriptorType = Rec->getDecl(); |
| 3473 | } |
| 3474 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3475 | QualType ASTContext::getBlockDescriptorExtendedType() const { |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3476 | if (BlockDescriptorExtendedType) |
| 3477 | return getTagDeclType(BlockDescriptorExtendedType); |
| 3478 | |
| 3479 | RecordDecl *T; |
| 3480 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3481 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3482 | &Idents.get("__block_descriptor_withcopydispose")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3483 | T->startDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3484 | |
| 3485 | QualType FieldTypes[] = { |
| 3486 | UnsignedLongTy, |
| 3487 | UnsignedLongTy, |
| 3488 | getPointerType(VoidPtrTy), |
| 3489 | getPointerType(VoidPtrTy) |
| 3490 | }; |
| 3491 | |
| 3492 | const char *FieldNames[] = { |
| 3493 | "reserved", |
| 3494 | "Size", |
| 3495 | "CopyFuncPtr", |
| 3496 | "DestroyFuncPtr" |
| 3497 | }; |
| 3498 | |
| 3499 | for (size_t i = 0; i < 4; ++i) { |
| 3500 | FieldDecl *Field = FieldDecl::Create(*this, |
| 3501 | T, |
| 3502 | SourceLocation(), |
| 3503 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3504 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3505 | /*BitWidth=*/0, |
| 3506 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3507 | Field->setAccess(AS_public); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3508 | T->addDecl(Field); |
| 3509 | } |
| 3510 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3511 | T->completeDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3512 | |
| 3513 | BlockDescriptorExtendedType = T; |
| 3514 | |
| 3515 | return getTagDeclType(BlockDescriptorExtendedType); |
| 3516 | } |
| 3517 | |
| 3518 | void ASTContext::setBlockDescriptorExtendedType(QualType T) { |
| 3519 | const RecordType *Rec = T->getAs<RecordType>(); |
| 3520 | assert(Rec && "Invalid BlockDescriptorType"); |
| 3521 | BlockDescriptorExtendedType = Rec->getDecl(); |
| 3522 | } |
| 3523 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3524 | bool ASTContext::BlockRequiresCopying(QualType Ty) const { |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3525 | if (Ty->isBlockPointerType()) |
| 3526 | return true; |
| 3527 | if (isObjCNSObjectType(Ty)) |
| 3528 | return true; |
| 3529 | if (Ty->isObjCObjectPointerType()) |
| 3530 | return true; |
Fariborz Jahanian | e38be61 | 2010-11-17 00:21:28 +0000 | [diff] [blame] | 3531 | if (getLangOptions().CPlusPlus) { |
| 3532 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3533 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 3534 | return RD->hasConstCopyConstructor(*this); |
| 3535 | |
| 3536 | } |
| 3537 | } |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3538 | return false; |
| 3539 | } |
| 3540 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3541 | QualType |
| 3542 | ASTContext::BuildByRefType(llvm::StringRef DeclName, QualType Ty) const { |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3543 | // type = struct __Block_byref_1_X { |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3544 | // void *__isa; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3545 | // struct __Block_byref_1_X *__forwarding; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3546 | // unsigned int __flags; |
| 3547 | // unsigned int __size; |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 3548 | // void *__copy_helper; // as needed |
| 3549 | // void *__destroy_help // as needed |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3550 | // int X; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3551 | // } * |
| 3552 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3553 | bool HasCopyAndDispose = BlockRequiresCopying(Ty); |
| 3554 | |
| 3555 | // FIXME: Move up |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 3556 | llvm::SmallString<36> Name; |
| 3557 | llvm::raw_svector_ostream(Name) << "__Block_byref_" << |
| 3558 | ++UniqueBlockByRefTypeID << '_' << DeclName; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3559 | RecordDecl *T; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3560 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3561 | &Idents.get(Name.str())); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3562 | T->startDefinition(); |
| 3563 | QualType Int32Ty = IntTy; |
| 3564 | assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported"); |
| 3565 | QualType FieldTypes[] = { |
| 3566 | getPointerType(VoidPtrTy), |
| 3567 | getPointerType(getTagDeclType(T)), |
| 3568 | Int32Ty, |
| 3569 | Int32Ty, |
| 3570 | getPointerType(VoidPtrTy), |
| 3571 | getPointerType(VoidPtrTy), |
| 3572 | Ty |
| 3573 | }; |
| 3574 | |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3575 | llvm::StringRef FieldNames[] = { |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3576 | "__isa", |
| 3577 | "__forwarding", |
| 3578 | "__flags", |
| 3579 | "__size", |
| 3580 | "__copy_helper", |
| 3581 | "__destroy_helper", |
| 3582 | DeclName, |
| 3583 | }; |
| 3584 | |
| 3585 | for (size_t i = 0; i < 7; ++i) { |
| 3586 | if (!HasCopyAndDispose && i >=4 && i <= 5) |
| 3587 | continue; |
| 3588 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
| 3589 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3590 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3591 | /*BitWidth=*/0, /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3592 | Field->setAccess(AS_public); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3593 | T->addDecl(Field); |
| 3594 | } |
| 3595 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3596 | T->completeDefinition(); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3597 | |
| 3598 | return getPointerType(getTagDeclType(T)); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3599 | } |
| 3600 | |
| 3601 | |
| 3602 | QualType ASTContext::getBlockParmType( |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3603 | bool BlockHasCopyDispose, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3604 | llvm::SmallVectorImpl<const Expr *> &Layout) const { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 3605 | |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3606 | // FIXME: Move up |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 3607 | llvm::SmallString<36> Name; |
| 3608 | llvm::raw_svector_ostream(Name) << "__block_literal_" |
| 3609 | << ++UniqueBlockParmTypeID; |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3610 | RecordDecl *T; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3611 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3612 | &Idents.get(Name.str())); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3613 | T->startDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3614 | QualType FieldTypes[] = { |
| 3615 | getPointerType(VoidPtrTy), |
| 3616 | IntTy, |
| 3617 | IntTy, |
| 3618 | getPointerType(VoidPtrTy), |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3619 | (BlockHasCopyDispose ? |
| 3620 | getPointerType(getBlockDescriptorExtendedType()) : |
| 3621 | getPointerType(getBlockDescriptorType())) |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3622 | }; |
| 3623 | |
| 3624 | const char *FieldNames[] = { |
| 3625 | "__isa", |
| 3626 | "__flags", |
| 3627 | "__reserved", |
| 3628 | "__FuncPtr", |
| 3629 | "__descriptor" |
| 3630 | }; |
| 3631 | |
| 3632 | for (size_t i = 0; i < 5; ++i) { |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3633 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3634 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3635 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3636 | /*BitWidth=*/0, /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3637 | Field->setAccess(AS_public); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3638 | T->addDecl(Field); |
| 3639 | } |
| 3640 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 3641 | for (unsigned i = 0; i < Layout.size(); ++i) { |
| 3642 | const Expr *E = Layout[i]; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3643 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 3644 | QualType FieldType = E->getType(); |
| 3645 | IdentifierInfo *FieldName = 0; |
| 3646 | if (isa<CXXThisExpr>(E)) { |
| 3647 | FieldName = &Idents.get("this"); |
| 3648 | } else if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E)) { |
| 3649 | const ValueDecl *D = BDRE->getDecl(); |
| 3650 | FieldName = D->getIdentifier(); |
| 3651 | if (BDRE->isByRef()) |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3652 | FieldType = BuildByRefType(D->getName(), FieldType); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 3653 | } else { |
| 3654 | // Padding. |
| 3655 | assert(isa<ConstantArrayType>(FieldType) && |
| 3656 | isa<DeclRefExpr>(E) && |
| 3657 | !cast<DeclRefExpr>(E)->getDecl()->getDeclName() && |
| 3658 | "doesn't match characteristics of padding decl"); |
| 3659 | } |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3660 | |
| 3661 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 3662 | FieldName, FieldType, /*TInfo=*/0, |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3663 | /*BitWidth=*/0, /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3664 | Field->setAccess(AS_public); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3665 | T->addDecl(Field); |
| 3666 | } |
| 3667 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3668 | T->completeDefinition(); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3669 | |
| 3670 | return getPointerType(getTagDeclType(T)); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3671 | } |
| 3672 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3673 | void ASTContext::setObjCFastEnumerationStateType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3674 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3675 | assert(Rec && "Invalid ObjCFAstEnumerationStateType"); |
| 3676 | ObjCFastEnumerationStateTypeDecl = Rec->getDecl(); |
| 3677 | } |
| 3678 | |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3679 | // This returns true if a type has been typedefed to BOOL: |
| 3680 | // typedef <type> BOOL; |
Chris Lattner | 2d99833 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 3681 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3682 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | bb49c3e | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 3683 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 3684 | return II->isStr("BOOL"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3685 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3686 | return false; |
| 3687 | } |
| 3688 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3689 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3690 | /// purpose. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3691 | CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const { |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3692 | CharUnits sz = getTypeSizeInChars(type); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3693 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3694 | // Make all integer and enum types at least as large as an int |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3695 | if (sz.isPositive() && type->isIntegralOrEnumerationType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3696 | sz = std::max(sz, getTypeSizeInChars(IntTy)); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3697 | // Treat arrays as pointers, since that's how they're passed in. |
| 3698 | else if (type->isArrayType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3699 | sz = getTypeSizeInChars(VoidPtrTy); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3700 | return sz; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3701 | } |
| 3702 | |
| 3703 | static inline |
| 3704 | std::string charUnitsToString(const CharUnits &CU) { |
| 3705 | return llvm::itostr(CU.getQuantity()); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3706 | } |
| 3707 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 3708 | /// getObjCEncodingForBlock - Return the encoded type for this block |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3709 | /// declaration. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 3710 | std::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const { |
| 3711 | std::string S; |
| 3712 | |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3713 | const BlockDecl *Decl = Expr->getBlockDecl(); |
| 3714 | QualType BlockTy = |
| 3715 | Expr->getType()->getAs<BlockPointerType>()->getPointeeType(); |
| 3716 | // Encode result type. |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 3717 | getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3718 | // Compute size of all parameters. |
| 3719 | // Start with computing size of a pointer in number of bytes. |
| 3720 | // FIXME: There might(should) be a better way of doing this computation! |
| 3721 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3722 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
| 3723 | CharUnits ParmOffset = PtrSize; |
Fariborz Jahanian | 6f46c26 | 2010-04-08 18:06:22 +0000 | [diff] [blame] | 3724 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3725 | E = Decl->param_end(); PI != E; ++PI) { |
| 3726 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3727 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3728 | assert (sz.isPositive() && "BlockExpr - Incomplete param type"); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3729 | ParmOffset += sz; |
| 3730 | } |
| 3731 | // Size of the argument frame |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3732 | S += charUnitsToString(ParmOffset); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3733 | // Block pointer and offset. |
| 3734 | S += "@?0"; |
| 3735 | ParmOffset = PtrSize; |
| 3736 | |
| 3737 | // Argument types. |
| 3738 | ParmOffset = PtrSize; |
| 3739 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E = |
| 3740 | Decl->param_end(); PI != E; ++PI) { |
| 3741 | ParmVarDecl *PVDecl = *PI; |
| 3742 | QualType PType = PVDecl->getOriginalType(); |
| 3743 | if (const ArrayType *AT = |
| 3744 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3745 | // Use array's original type only if it has known number of |
| 3746 | // elements. |
| 3747 | if (!isa<ConstantArrayType>(AT)) |
| 3748 | PType = PVDecl->getType(); |
| 3749 | } else if (PType->isFunctionType()) |
| 3750 | PType = PVDecl->getType(); |
| 3751 | getObjCEncodingForType(PType, S); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3752 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3753 | ParmOffset += getObjCEncodingTypeSize(PType); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3754 | } |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 3755 | |
| 3756 | return S; |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3757 | } |
| 3758 | |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 3759 | void ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl, |
| 3760 | std::string& S) { |
| 3761 | // Encode result type. |
| 3762 | getObjCEncodingForType(Decl->getResultType(), S); |
| 3763 | CharUnits ParmOffset; |
| 3764 | // Compute size of all parameters. |
| 3765 | for (FunctionDecl::param_const_iterator PI = Decl->param_begin(), |
| 3766 | E = Decl->param_end(); PI != E; ++PI) { |
| 3767 | QualType PType = (*PI)->getType(); |
| 3768 | CharUnits sz = getObjCEncodingTypeSize(PType); |
| 3769 | assert (sz.isPositive() && |
| 3770 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
| 3771 | ParmOffset += sz; |
| 3772 | } |
| 3773 | S += charUnitsToString(ParmOffset); |
| 3774 | ParmOffset = CharUnits::Zero(); |
| 3775 | |
| 3776 | // Argument types. |
| 3777 | for (FunctionDecl::param_const_iterator PI = Decl->param_begin(), |
| 3778 | E = Decl->param_end(); PI != E; ++PI) { |
| 3779 | ParmVarDecl *PVDecl = *PI; |
| 3780 | QualType PType = PVDecl->getOriginalType(); |
| 3781 | if (const ArrayType *AT = |
| 3782 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3783 | // Use array's original type only if it has known number of |
| 3784 | // elements. |
| 3785 | if (!isa<ConstantArrayType>(AT)) |
| 3786 | PType = PVDecl->getType(); |
| 3787 | } else if (PType->isFunctionType()) |
| 3788 | PType = PVDecl->getType(); |
| 3789 | getObjCEncodingForType(PType, S); |
| 3790 | S += charUnitsToString(ParmOffset); |
| 3791 | ParmOffset += getObjCEncodingTypeSize(PType); |
| 3792 | } |
| 3793 | } |
| 3794 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3795 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3796 | /// declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3797 | void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3798 | std::string& S) const { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3799 | // FIXME: This is not very efficient. |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3800 | // Encode type qualifer, 'in', 'inout', etc. for the return type. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3801 | getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3802 | // Encode result type. |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3803 | getObjCEncodingForType(Decl->getResultType(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3804 | // Compute size of all parameters. |
| 3805 | // Start with computing size of a pointer in number of bytes. |
| 3806 | // FIXME: There might(should) be a better way of doing this computation! |
| 3807 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3808 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3809 | // The first two arguments (self and _cmd) are pointers; account for |
| 3810 | // their size. |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3811 | CharUnits ParmOffset = 2 * PtrSize; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3812 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 3813 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3814 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3815 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3816 | assert (sz.isPositive() && |
| 3817 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3818 | ParmOffset += sz; |
| 3819 | } |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3820 | S += charUnitsToString(ParmOffset); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3821 | S += "@0:"; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3822 | S += charUnitsToString(PtrSize); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3823 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3824 | // Argument types. |
| 3825 | ParmOffset = 2 * PtrSize; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3826 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 3827 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3828 | ParmVarDecl *PVDecl = *PI; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3829 | QualType PType = PVDecl->getOriginalType(); |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 3830 | if (const ArrayType *AT = |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 3831 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3832 | // Use array's original type only if it has known number of |
| 3833 | // elements. |
Steve Naroff | bb3fde3 | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 3834 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 3835 | PType = PVDecl->getType(); |
| 3836 | } else if (PType->isFunctionType()) |
| 3837 | PType = PVDecl->getType(); |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3838 | // Process argument qualifiers for user supplied arguments; such as, |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3839 | // 'in', 'inout', etc. |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 3840 | getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3841 | getObjCEncodingForType(PType, S); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3842 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3843 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3844 | } |
| 3845 | } |
| 3846 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3847 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 3848 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3849 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 3850 | /// NULL when getting encodings for protocol properties. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3851 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 3852 | /// attributes readonly and bycopy are encoded as single characters. The |
| 3853 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 3854 | /// encoded as single characters, followed by an identifier. Property types |
| 3855 | /// are also encoded as a parametrized attribute. The characters used to encode |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 3856 | /// these attributes are defined by the following enumeration: |
| 3857 | /// @code |
| 3858 | /// enum PropertyAttributes { |
| 3859 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 3860 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 3861 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 3862 | /// kPropertyDynamic = 'D', // property is dynamic |
| 3863 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 3864 | /// kPropertySetter = 'S', // followed by setter selector name |
| 3865 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
| 3866 | /// kPropertyType = 't' // followed by old-style type encoding. |
| 3867 | /// kPropertyWeak = 'W' // 'weak' property |
| 3868 | /// kPropertyStrong = 'P' // property GC'able |
| 3869 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 3870 | /// }; |
| 3871 | /// @endcode |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3872 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3873 | const Decl *Container, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3874 | std::string& S) const { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3875 | // Collect information from the property implementation decl(s). |
| 3876 | bool Dynamic = false; |
| 3877 | ObjCPropertyImplDecl *SynthesizePID = 0; |
| 3878 | |
| 3879 | // FIXME: Duplicated code due to poor abstraction. |
| 3880 | if (Container) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3881 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3882 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
| 3883 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3884 | i = CID->propimpl_begin(), e = CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3885 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3886 | ObjCPropertyImplDecl *PID = *i; |
| 3887 | if (PID->getPropertyDecl() == PD) { |
| 3888 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 3889 | Dynamic = true; |
| 3890 | } else { |
| 3891 | SynthesizePID = PID; |
| 3892 | } |
| 3893 | } |
| 3894 | } |
| 3895 | } else { |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3896 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3897 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3898 | i = OID->propimpl_begin(), e = OID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3899 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3900 | ObjCPropertyImplDecl *PID = *i; |
| 3901 | if (PID->getPropertyDecl() == PD) { |
| 3902 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 3903 | Dynamic = true; |
| 3904 | } else { |
| 3905 | SynthesizePID = PID; |
| 3906 | } |
| 3907 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3908 | } |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3909 | } |
| 3910 | } |
| 3911 | |
| 3912 | // FIXME: This is not very efficient. |
| 3913 | S = "T"; |
| 3914 | |
| 3915 | // Encode result type. |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3916 | // GCC has some special rules regarding encoding of properties which |
| 3917 | // closely resembles encoding of ivars. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3918 | getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3919 | true /* outermost type */, |
| 3920 | true /* encoding for property */); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3921 | |
| 3922 | if (PD->isReadOnly()) { |
| 3923 | S += ",R"; |
| 3924 | } else { |
| 3925 | switch (PD->getSetterKind()) { |
| 3926 | case ObjCPropertyDecl::Assign: break; |
| 3927 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3928 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3929 | } |
| 3930 | } |
| 3931 | |
| 3932 | // It really isn't clear at all what this means, since properties |
| 3933 | // are "dynamic by default". |
| 3934 | if (Dynamic) |
| 3935 | S += ",D"; |
| 3936 | |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3937 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 3938 | S += ",N"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3939 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3940 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 3941 | S += ",G"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3942 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3943 | } |
| 3944 | |
| 3945 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 3946 | S += ",S"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3947 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3948 | } |
| 3949 | |
| 3950 | if (SynthesizePID) { |
| 3951 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 3952 | S += ",V"; |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 3953 | S += OID->getNameAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3954 | } |
| 3955 | |
| 3956 | // FIXME: OBJCGC: weak & strong |
| 3957 | } |
| 3958 | |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3959 | /// getLegacyIntegralTypeEncoding - |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3960 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
| 3961 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3962 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
| 3963 | /// |
| 3964 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3965 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3966 | if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) { |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3967 | if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3968 | PointeeTy = UnsignedIntTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3969 | else |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3970 | if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3971 | PointeeTy = IntTy; |
| 3972 | } |
| 3973 | } |
| 3974 | } |
| 3975 | |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3976 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3977 | const FieldDecl *Field) const { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3978 | // We follow the behavior of gcc, expanding structures which are |
| 3979 | // directly pointed to, and expanding embedded structures. Note that |
| 3980 | // these rules are sufficient to prevent recursive encoding of the |
| 3981 | // same type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3982 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
Fariborz Jahanian | 5b8c7d9 | 2008-12-22 23:22:27 +0000 | [diff] [blame] | 3983 | true /* outermost type */); |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3984 | } |
| 3985 | |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3986 | static char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) { |
| 3987 | switch (T->getAs<BuiltinType>()->getKind()) { |
| 3988 | default: assert(0 && "Unhandled builtin type kind"); |
| 3989 | case BuiltinType::Void: return 'v'; |
| 3990 | case BuiltinType::Bool: return 'B'; |
| 3991 | case BuiltinType::Char_U: |
| 3992 | case BuiltinType::UChar: return 'C'; |
| 3993 | case BuiltinType::UShort: return 'S'; |
| 3994 | case BuiltinType::UInt: return 'I'; |
| 3995 | case BuiltinType::ULong: |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3996 | return C->getIntWidth(T) == 32 ? 'L' : 'Q'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3997 | case BuiltinType::UInt128: return 'T'; |
| 3998 | case BuiltinType::ULongLong: return 'Q'; |
| 3999 | case BuiltinType::Char_S: |
| 4000 | case BuiltinType::SChar: return 'c'; |
| 4001 | case BuiltinType::Short: return 's'; |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 4002 | case BuiltinType::WChar_S: |
| 4003 | case BuiltinType::WChar_U: |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4004 | case BuiltinType::Int: return 'i'; |
| 4005 | case BuiltinType::Long: |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4006 | return C->getIntWidth(T) == 32 ? 'l' : 'q'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4007 | case BuiltinType::LongLong: return 'q'; |
| 4008 | case BuiltinType::Int128: return 't'; |
| 4009 | case BuiltinType::Float: return 'f'; |
| 4010 | case BuiltinType::Double: return 'd'; |
Daniel Dunbar | 3a0be84 | 2010-10-11 21:13:48 +0000 | [diff] [blame] | 4011 | case BuiltinType::LongDouble: return 'D'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4012 | } |
| 4013 | } |
| 4014 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4015 | static void EncodeBitField(const ASTContext *Ctx, std::string& S, |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4016 | QualType T, const FieldDecl *FD) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4017 | const Expr *E = FD->getBitWidth(); |
| 4018 | assert(E && "bitfield width not there - getObjCEncodingForTypeImpl"); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4019 | S += 'b'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4020 | // The NeXT runtime encodes bit fields as b followed by the number of bits. |
| 4021 | // The GNU runtime requires more information; bitfields are encoded as b, |
| 4022 | // then the offset (in bits) of the first element, then the type of the |
| 4023 | // bitfield, then the size in bits. For example, in this structure: |
| 4024 | // |
| 4025 | // struct |
| 4026 | // { |
| 4027 | // int integer; |
| 4028 | // int flags:2; |
| 4029 | // }; |
| 4030 | // On a 32-bit system, the encoding for flags would be b2 for the NeXT |
| 4031 | // runtime, but b32i2 for the GNU runtime. The reason for this extra |
| 4032 | // information is not especially sensible, but we're stuck with it for |
| 4033 | // compatibility with GCC, although providing it breaks anything that |
| 4034 | // actually uses runtime introspection and wants to work on both runtimes... |
| 4035 | if (!Ctx->getLangOptions().NeXTRuntime) { |
| 4036 | const RecordDecl *RD = FD->getParent(); |
| 4037 | const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD); |
| 4038 | // FIXME: This same linear search is also used in ExprConstant - it might |
| 4039 | // be better if the FieldDecl stored its offset. We'd be increasing the |
| 4040 | // size of the object slightly, but saving some time every time it is used. |
| 4041 | unsigned i = 0; |
| 4042 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 4043 | FieldEnd = RD->field_end(); |
| 4044 | Field != FieldEnd; (void)++Field, ++i) { |
| 4045 | if (*Field == FD) |
| 4046 | break; |
| 4047 | } |
| 4048 | S += llvm::utostr(RL.getFieldOffset(i)); |
David Chisnall | c7ff82c | 2010-12-26 20:12:30 +0000 | [diff] [blame] | 4049 | if (T->isEnumeralType()) |
| 4050 | S += 'i'; |
| 4051 | else |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4052 | S += ObjCEncodingForPrimitiveKind(Ctx, T); |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4053 | } |
| 4054 | unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue(); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4055 | S += llvm::utostr(N); |
| 4056 | } |
| 4057 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 4058 | // FIXME: Use SmallString for accumulating string. |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4059 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 4060 | bool ExpandPointedToStructures, |
| 4061 | bool ExpandStructures, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 4062 | const FieldDecl *FD, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4063 | bool OutermostType, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4064 | bool EncodingProperty) const { |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4065 | if (T->getAs<BuiltinType>()) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4066 | if (FD && FD->isBitField()) |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4067 | return EncodeBitField(this, S, T, FD); |
| 4068 | S += ObjCEncodingForPrimitiveKind(this, T); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4069 | return; |
| 4070 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4071 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4072 | if (const ComplexType *CT = T->getAs<ComplexType>()) { |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 4073 | S += 'j'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4074 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false, |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 4075 | false); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4076 | return; |
| 4077 | } |
Fariborz Jahanian | 60bce3e | 2009-11-23 20:40:50 +0000 | [diff] [blame] | 4078 | |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4079 | // encoding for pointer or r3eference types. |
| 4080 | QualType PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4081 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Fariborz Jahanian | 8d2c0a9 | 2009-11-30 18:43:52 +0000 | [diff] [blame] | 4082 | if (PT->isObjCSelType()) { |
| 4083 | S += ':'; |
| 4084 | return; |
| 4085 | } |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4086 | PointeeTy = PT->getPointeeType(); |
| 4087 | } |
| 4088 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 4089 | PointeeTy = RT->getPointeeType(); |
| 4090 | if (!PointeeTy.isNull()) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4091 | bool isReadOnly = false; |
| 4092 | // For historical/compatibility reasons, the read-only qualifier of the |
| 4093 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 4094 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4095 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 4096 | if (isa<TypedefType>(T.getTypePtr())) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4097 | if (OutermostType && T.isConstQualified()) { |
| 4098 | isReadOnly = true; |
| 4099 | S += 'r'; |
| 4100 | } |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 4101 | } else if (OutermostType) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4102 | QualType P = PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4103 | while (P->getAs<PointerType>()) |
| 4104 | P = P->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4105 | if (P.isConstQualified()) { |
| 4106 | isReadOnly = true; |
| 4107 | S += 'r'; |
| 4108 | } |
| 4109 | } |
| 4110 | if (isReadOnly) { |
| 4111 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 4112 | // combinations need to be rearranged. |
| 4113 | // Rewrite "in const" from "nr" to "rn" |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 4114 | if (llvm::StringRef(S).endswith("nr")) |
| 4115 | S.replace(S.end()-2, S.end(), "rn"); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4116 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4117 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4118 | if (PointeeTy->isCharType()) { |
| 4119 | // char pointer types should be encoded as '*' unless it is a |
| 4120 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 4121 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4122 | S += '*'; |
| 4123 | return; |
| 4124 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4125 | } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) { |
Steve Naroff | 9533a7f | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 4126 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 4127 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 4128 | S += '#'; |
| 4129 | return; |
| 4130 | } |
| 4131 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 4132 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 4133 | S += '@'; |
| 4134 | return; |
| 4135 | } |
| 4136 | // fall through... |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4137 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4138 | S += '^'; |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4139 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 4140 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4141 | getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4142 | NULL); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4143 | return; |
| 4144 | } |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4145 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4146 | if (const ArrayType *AT = |
| 4147 | // Ignore type qualifiers etc. |
| 4148 | dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4149 | if (isa<IncompleteArrayType>(AT)) { |
| 4150 | // Incomplete arrays are encoded as a pointer to the array element. |
| 4151 | S += '^'; |
| 4152 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4153 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4154 | false, ExpandStructures, FD); |
| 4155 | } else { |
| 4156 | S += '['; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4157 | |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4158 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 4159 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 4160 | else { |
| 4161 | //Variable length arrays are encoded as a regular array with 0 elements. |
| 4162 | assert(isa<VariableArrayType>(AT) && "Unknown array type!"); |
| 4163 | S += '0'; |
| 4164 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4165 | |
| 4166 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4167 | false, ExpandStructures, FD); |
| 4168 | S += ']'; |
| 4169 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4170 | return; |
| 4171 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4172 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4173 | if (T->getAs<FunctionType>()) { |
Anders Carlsson | c0a87b7 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 4174 | S += '?'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4175 | return; |
| 4176 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4177 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4178 | if (const RecordType *RTy = T->getAs<RecordType>()) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4179 | RecordDecl *RDecl = RTy->getDecl(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4180 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 4181 | // Anonymous structures print as '?' |
| 4182 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 4183 | S += II->getName(); |
Fariborz Jahanian | 6fb9439 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 4184 | if (ClassTemplateSpecializationDecl *Spec |
| 4185 | = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) { |
| 4186 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 4187 | std::string TemplateArgsStr |
| 4188 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4189 | TemplateArgs.data(), |
| 4190 | TemplateArgs.size(), |
Fariborz Jahanian | 6fb9439 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 4191 | (*this).PrintingPolicy); |
| 4192 | |
| 4193 | S += TemplateArgsStr; |
| 4194 | } |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 4195 | } else { |
| 4196 | S += '?'; |
| 4197 | } |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 4198 | if (ExpandStructures) { |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4199 | S += '='; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4200 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 4201 | FieldEnd = RDecl->field_end(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4202 | Field != FieldEnd; ++Field) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4203 | if (FD) { |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4204 | S += '"'; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4205 | S += Field->getNameAsString(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4206 | S += '"'; |
| 4207 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4208 | |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4209 | // Special case bit-fields. |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4210 | if (Field->isBitField()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4211 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4212 | (*Field)); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4213 | } else { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4214 | QualType qt = Field->getType(); |
| 4215 | getLegacyIntegralTypeEncoding(qt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4216 | getObjCEncodingForTypeImpl(qt, S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4217 | FD); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4218 | } |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4219 | } |
Fariborz Jahanian | 6de88a8 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 4220 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4221 | S += RDecl->isUnion() ? ')' : '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4222 | return; |
| 4223 | } |
Fariborz Jahanian | e6012c7 | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 4224 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4225 | if (T->isEnumeralType()) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4226 | if (FD && FD->isBitField()) |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4227 | EncodeBitField(this, S, T, FD); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4228 | else |
| 4229 | S += 'i'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4230 | return; |
| 4231 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4232 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4233 | if (T->isBlockPointerType()) { |
Steve Naroff | 21a98b1 | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 4234 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4235 | return; |
| 4236 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4237 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4238 | // Ignore protocol qualifiers when mangling at this level. |
| 4239 | if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>()) |
| 4240 | T = OT->getBaseType(); |
| 4241 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4242 | if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4243 | // @encode(class_name) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4244 | ObjCInterfaceDecl *OI = OIT->getDecl(); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4245 | S += '{'; |
| 4246 | const IdentifierInfo *II = OI->getIdentifier(); |
| 4247 | S += II->getName(); |
| 4248 | S += '='; |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4249 | llvm::SmallVector<ObjCIvarDecl*, 32> Ivars; |
| 4250 | DeepCollectObjCIvars(OI, true, Ivars); |
| 4251 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) { |
| 4252 | FieldDecl *Field = cast<FieldDecl>(Ivars[i]); |
| 4253 | if (Field->isBitField()) |
| 4254 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4255 | else |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4256 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4257 | } |
| 4258 | S += '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4259 | return; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4260 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4261 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4262 | if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4263 | if (OPT->isObjCIdType()) { |
| 4264 | S += '@'; |
| 4265 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4266 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4267 | |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4268 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
| 4269 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
| 4270 | // Since this is a binary compatibility issue, need to consult with runtime |
| 4271 | // folks. Fortunately, this is a *very* obsure construct. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4272 | S += '#'; |
| 4273 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4274 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4275 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4276 | if (OPT->isObjCQualifiedIdType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4277 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4278 | ExpandPointedToStructures, |
| 4279 | ExpandStructures, FD); |
| 4280 | if (FD || EncodingProperty) { |
| 4281 | // Note that we do extended encoding of protocol qualifer list |
| 4282 | // Only when doing ivar or property encoding. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4283 | S += '"'; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4284 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 4285 | E = OPT->qual_end(); I != E; ++I) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4286 | S += '<'; |
| 4287 | S += (*I)->getNameAsString(); |
| 4288 | S += '>'; |
| 4289 | } |
| 4290 | S += '"'; |
| 4291 | } |
| 4292 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4293 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4294 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4295 | QualType PointeeTy = OPT->getPointeeType(); |
| 4296 | if (!EncodingProperty && |
| 4297 | isa<TypedefType>(PointeeTy.getTypePtr())) { |
| 4298 | // Another historical/compatibility reason. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4299 | // We encode the underlying type which comes out as |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4300 | // {...}; |
| 4301 | S += '^'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4302 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 4303 | false, ExpandPointedToStructures, |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4304 | NULL); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4305 | return; |
| 4306 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4307 | |
| 4308 | S += '@'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4309 | if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4310 | S += '"'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4311 | S += OPT->getInterfaceDecl()->getIdentifier()->getName(); |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4312 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 4313 | E = OPT->qual_end(); I != E; ++I) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4314 | S += '<'; |
| 4315 | S += (*I)->getNameAsString(); |
| 4316 | S += '>'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4317 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4318 | S += '"'; |
| 4319 | } |
| 4320 | return; |
| 4321 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4322 | |
John McCall | 532ec7b | 2010-05-17 23:56:34 +0000 | [diff] [blame] | 4323 | // gcc just blithely ignores member pointers. |
| 4324 | // TODO: maybe there should be a mangling for these |
| 4325 | if (T->getAs<MemberPointerType>()) |
| 4326 | return; |
Fariborz Jahanian | e6012c7 | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 4327 | |
| 4328 | if (T->isVectorType()) { |
| 4329 | // This matches gcc's encoding, even though technically it is |
| 4330 | // insufficient. |
| 4331 | // FIXME. We should do a better job than gcc. |
| 4332 | return; |
| 4333 | } |
| 4334 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4335 | assert(0 && "@encode for type not implemented!"); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4336 | } |
| 4337 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4338 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 4339 | std::string& S) const { |
| 4340 | if (QT & Decl::OBJC_TQ_In) |
| 4341 | S += 'n'; |
| 4342 | if (QT & Decl::OBJC_TQ_Inout) |
| 4343 | S += 'N'; |
| 4344 | if (QT & Decl::OBJC_TQ_Out) |
| 4345 | S += 'o'; |
| 4346 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 4347 | S += 'O'; |
| 4348 | if (QT & Decl::OBJC_TQ_Byref) |
| 4349 | S += 'R'; |
| 4350 | if (QT & Decl::OBJC_TQ_Oneway) |
| 4351 | S += 'V'; |
| 4352 | } |
| 4353 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4354 | void ASTContext::setBuiltinVaListType(QualType T) { |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 4355 | assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4356 | |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 4357 | BuiltinVaListType = T; |
| 4358 | } |
| 4359 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4360 | void ASTContext::setObjCIdType(QualType T) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 4361 | ObjCIdTypedefType = T; |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 4362 | } |
| 4363 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4364 | void ASTContext::setObjCSelType(QualType T) { |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 4365 | ObjCSelTypedefType = T; |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 4366 | } |
| 4367 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4368 | void ASTContext::setObjCProtoType(QualType QT) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4369 | ObjCProtoType = QT; |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 4370 | } |
| 4371 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4372 | void ASTContext::setObjCClassType(QualType T) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 4373 | ObjCClassTypedefType = T; |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 4374 | } |
| 4375 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4376 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4377 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 4378 | "'NSConstantString' type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4379 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4380 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 4381 | } |
| 4382 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4383 | /// \brief Retrieve the template name that corresponds to a non-empty |
| 4384 | /// lookup. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4385 | TemplateName |
| 4386 | ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin, |
| 4387 | UnresolvedSetIterator End) const { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4388 | unsigned size = End - Begin; |
| 4389 | assert(size > 1 && "set is not overloaded!"); |
| 4390 | |
| 4391 | void *memory = Allocate(sizeof(OverloadedTemplateStorage) + |
| 4392 | size * sizeof(FunctionTemplateDecl*)); |
| 4393 | OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size); |
| 4394 | |
| 4395 | NamedDecl **Storage = OT->getStorage(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4396 | for (UnresolvedSetIterator I = Begin; I != End; ++I) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4397 | NamedDecl *D = *I; |
| 4398 | assert(isa<FunctionTemplateDecl>(D) || |
| 4399 | (isa<UsingShadowDecl>(D) && |
| 4400 | isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))); |
| 4401 | *Storage++ = D; |
| 4402 | } |
| 4403 | |
| 4404 | return TemplateName(OT); |
| 4405 | } |
| 4406 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4407 | /// \brief Retrieve the template name that represents a qualified |
| 4408 | /// template name such as \c std::vector. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4409 | TemplateName |
| 4410 | ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
| 4411 | bool TemplateKeyword, |
| 4412 | TemplateDecl *Template) const { |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4413 | // FIXME: Canonicalization? |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4414 | llvm::FoldingSetNodeID ID; |
| 4415 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 4416 | |
| 4417 | void *InsertPos = 0; |
| 4418 | QualifiedTemplateName *QTN = |
| 4419 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4420 | if (!QTN) { |
| 4421 | QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); |
| 4422 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 4423 | } |
| 4424 | |
| 4425 | return TemplateName(QTN); |
| 4426 | } |
| 4427 | |
| 4428 | /// \brief Retrieve the template name that represents a dependent |
| 4429 | /// template name such as \c MetaFun::template apply. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4430 | TemplateName |
| 4431 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
| 4432 | const IdentifierInfo *Name) const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4433 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 4434 | "Nested name specifier must be dependent"); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4435 | |
| 4436 | llvm::FoldingSetNodeID ID; |
| 4437 | DependentTemplateName::Profile(ID, NNS, Name); |
| 4438 | |
| 4439 | void *InsertPos = 0; |
| 4440 | DependentTemplateName *QTN = |
| 4441 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4442 | |
| 4443 | if (QTN) |
| 4444 | return TemplateName(QTN); |
| 4445 | |
| 4446 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 4447 | if (CanonNNS == NNS) { |
| 4448 | QTN = new (*this,4) DependentTemplateName(NNS, Name); |
| 4449 | } else { |
| 4450 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
| 4451 | QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4452 | DependentTemplateName *CheckQTN = |
| 4453 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4454 | assert(!CheckQTN && "Dependent type name canonicalization broken"); |
| 4455 | (void)CheckQTN; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4456 | } |
| 4457 | |
| 4458 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 4459 | return TemplateName(QTN); |
| 4460 | } |
| 4461 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4462 | /// \brief Retrieve the template name that represents a dependent |
| 4463 | /// template name such as \c MetaFun::template operator+. |
| 4464 | TemplateName |
| 4465 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4466 | OverloadedOperatorKind Operator) const { |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4467 | assert((!NNS || NNS->isDependent()) && |
| 4468 | "Nested name specifier must be dependent"); |
| 4469 | |
| 4470 | llvm::FoldingSetNodeID ID; |
| 4471 | DependentTemplateName::Profile(ID, NNS, Operator); |
| 4472 | |
| 4473 | void *InsertPos = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4474 | DependentTemplateName *QTN |
| 4475 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4476 | |
| 4477 | if (QTN) |
| 4478 | return TemplateName(QTN); |
| 4479 | |
| 4480 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 4481 | if (CanonNNS == NNS) { |
| 4482 | QTN = new (*this,4) DependentTemplateName(NNS, Operator); |
| 4483 | } else { |
| 4484 | TemplateName Canon = getDependentTemplateName(CanonNNS, Operator); |
| 4485 | QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4486 | |
| 4487 | DependentTemplateName *CheckQTN |
| 4488 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4489 | assert(!CheckQTN && "Dependent template name canonicalization broken"); |
| 4490 | (void)CheckQTN; |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4491 | } |
| 4492 | |
| 4493 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 4494 | return TemplateName(QTN); |
| 4495 | } |
| 4496 | |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 4497 | TemplateName |
| 4498 | ASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param, |
| 4499 | const TemplateArgument &ArgPack) const { |
| 4500 | ASTContext &Self = const_cast<ASTContext &>(*this); |
| 4501 | llvm::FoldingSetNodeID ID; |
| 4502 | SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack); |
| 4503 | |
| 4504 | void *InsertPos = 0; |
| 4505 | SubstTemplateTemplateParmPackStorage *Subst |
| 4506 | = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos); |
| 4507 | |
| 4508 | if (!Subst) { |
| 4509 | Subst = new (*this) SubstTemplateTemplateParmPackStorage(Self, Param, |
| 4510 | ArgPack.pack_size(), |
| 4511 | ArgPack.pack_begin()); |
| 4512 | SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos); |
| 4513 | } |
| 4514 | |
| 4515 | return TemplateName(Subst); |
| 4516 | } |
| 4517 | |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4518 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | d934112 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 4519 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 4520 | /// is actually a value of type @c TargetInfo::IntType. |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 4521 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4522 | switch (Type) { |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 4523 | case TargetInfo::NoInt: return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4524 | case TargetInfo::SignedShort: return ShortTy; |
| 4525 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 4526 | case TargetInfo::SignedInt: return IntTy; |
| 4527 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 4528 | case TargetInfo::SignedLong: return LongTy; |
| 4529 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 4530 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 4531 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 4532 | } |
| 4533 | |
| 4534 | assert(false && "Unhandled TargetInfo::IntType value"); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 4535 | return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4536 | } |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 4537 | |
| 4538 | //===----------------------------------------------------------------------===// |
| 4539 | // Type Predicates. |
| 4540 | //===----------------------------------------------------------------------===// |
| 4541 | |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4542 | /// isObjCNSObjectType - Return true if this is an NSObject object using |
| 4543 | /// NSObject attribute on a c-style pointer type. |
| 4544 | /// FIXME - Make it work directly on types. |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 4545 | /// FIXME: Move to Type. |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4546 | /// |
| 4547 | bool ASTContext::isObjCNSObjectType(QualType Ty) const { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 4548 | if (const TypedefType *TDT = dyn_cast<TypedefType>(Ty)) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4549 | if (TypedefDecl *TD = TDT->getDecl()) |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 4550 | if (TD->getAttr<ObjCNSObjectAttr>()) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4551 | return true; |
| 4552 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4553 | return false; |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4554 | } |
| 4555 | |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4556 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 4557 | /// garbage collection attribute. |
| 4558 | /// |
John McCall | ae278a3 | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 4559 | Qualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const { |
| 4560 | if (getLangOptions().getGCMode() == LangOptions::NonGC) |
| 4561 | return Qualifiers::GCNone; |
| 4562 | |
| 4563 | assert(getLangOptions().ObjC1); |
| 4564 | Qualifiers::GC GCAttrs = Ty.getObjCGCAttr(); |
| 4565 | |
| 4566 | // Default behaviour under objective-C's gc is for ObjC pointers |
| 4567 | // (or pointers to them) be treated as though they were declared |
| 4568 | // as __strong. |
| 4569 | if (GCAttrs == Qualifiers::GCNone) { |
| 4570 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
| 4571 | return Qualifiers::Strong; |
| 4572 | else if (Ty->isPointerType()) |
| 4573 | return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); |
| 4574 | } else { |
| 4575 | // It's not valid to set GC attributes on anything that isn't a |
| 4576 | // pointer. |
| 4577 | #ifndef NDEBUG |
| 4578 | QualType CT = Ty->getCanonicalTypeInternal(); |
| 4579 | while (const ArrayType *AT = dyn_cast<ArrayType>(CT)) |
| 4580 | CT = AT->getElementType(); |
| 4581 | assert(CT->isAnyPointerType() || CT->isBlockPointerType()); |
| 4582 | #endif |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4583 | } |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 4584 | return GCAttrs; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4585 | } |
| 4586 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4587 | //===----------------------------------------------------------------------===// |
| 4588 | // Type Compatibility Testing |
| 4589 | //===----------------------------------------------------------------------===// |
Chris Lattner | 770951b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 4590 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4591 | /// areCompatVectorTypes - Return true if the two specified vector types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4592 | /// compatible. |
| 4593 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 4594 | const VectorType *RHS) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 4595 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4596 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4597 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4598 | } |
| 4599 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 4600 | bool ASTContext::areCompatibleVectorTypes(QualType FirstVec, |
| 4601 | QualType SecondVec) { |
| 4602 | assert(FirstVec->isVectorType() && "FirstVec should be a vector type"); |
| 4603 | assert(SecondVec->isVectorType() && "SecondVec should be a vector type"); |
| 4604 | |
| 4605 | if (hasSameUnqualifiedType(FirstVec, SecondVec)) |
| 4606 | return true; |
| 4607 | |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 4608 | // Treat Neon vector types and most AltiVec vector types as if they are the |
| 4609 | // equivalent GCC vector types. |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 4610 | const VectorType *First = FirstVec->getAs<VectorType>(); |
| 4611 | const VectorType *Second = SecondVec->getAs<VectorType>(); |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 4612 | if (First->getNumElements() == Second->getNumElements() && |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 4613 | hasSameType(First->getElementType(), Second->getElementType()) && |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 4614 | First->getVectorKind() != VectorType::AltiVecPixel && |
| 4615 | First->getVectorKind() != VectorType::AltiVecBool && |
| 4616 | Second->getVectorKind() != VectorType::AltiVecPixel && |
| 4617 | Second->getVectorKind() != VectorType::AltiVecBool) |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 4618 | return true; |
| 4619 | |
| 4620 | return false; |
| 4621 | } |
| 4622 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4623 | //===----------------------------------------------------------------------===// |
| 4624 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 4625 | //===----------------------------------------------------------------------===// |
| 4626 | |
| 4627 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 4628 | /// inheritance hierarchy of 'rProto'. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4629 | bool |
| 4630 | ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 4631 | ObjCProtocolDecl *rProto) const { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4632 | if (lProto == rProto) |
| 4633 | return true; |
| 4634 | for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), |
| 4635 | E = rProto->protocol_end(); PI != E; ++PI) |
| 4636 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 4637 | return true; |
| 4638 | return false; |
| 4639 | } |
| 4640 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4641 | /// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...> |
| 4642 | /// return true if lhs's protocols conform to rhs's protocol; false |
| 4643 | /// otherwise. |
| 4644 | bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) { |
| 4645 | if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType()) |
| 4646 | return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false); |
| 4647 | return false; |
| 4648 | } |
| 4649 | |
Fariborz Jahanian | a8f8dac | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 4650 | /// ObjCQualifiedClassTypesAreCompatible - compare Class<p,...> and |
| 4651 | /// Class<p1, ...>. |
| 4652 | bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs, |
| 4653 | QualType rhs) { |
| 4654 | const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>(); |
| 4655 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
| 4656 | assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible"); |
| 4657 | |
| 4658 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4659 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4660 | bool match = false; |
| 4661 | ObjCProtocolDecl *lhsProto = *I; |
| 4662 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 4663 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 4664 | ObjCProtocolDecl *rhsProto = *J; |
| 4665 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) { |
| 4666 | match = true; |
| 4667 | break; |
| 4668 | } |
| 4669 | } |
| 4670 | if (!match) |
| 4671 | return false; |
| 4672 | } |
| 4673 | return true; |
| 4674 | } |
| 4675 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4676 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 4677 | /// ObjCQualifiedIDType. |
| 4678 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 4679 | bool compare) { |
| 4680 | // Allow id<P..> and an 'id' or void* type in all cases. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4681 | if (lhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4682 | lhs->isObjCIdType() || lhs->isObjCClassType()) |
| 4683 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4684 | else if (rhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4685 | rhs->isObjCIdType() || rhs->isObjCClassType()) |
| 4686 | return true; |
| 4687 | |
| 4688 | if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4689 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4690 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4691 | if (!rhsOPT) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4692 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4693 | if (rhsOPT->qual_empty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4694 | // If the RHS is a unqualified interface pointer "NSString*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4695 | // make sure we check the class hierarchy. |
| 4696 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 4697 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4698 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4699 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4700 | // see if static class implements all of id's protocols, directly or |
| 4701 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 4702 | if (!rhsID->ClassImplementsProtocol(*I, true)) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4703 | return false; |
| 4704 | } |
| 4705 | } |
| 4706 | // If there are no qualifiers and no interface, we have an 'id'. |
| 4707 | return true; |
| 4708 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4709 | // Both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4710 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4711 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4712 | ObjCProtocolDecl *lhsProto = *I; |
| 4713 | bool match = false; |
| 4714 | |
| 4715 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4716 | // see if static class implements all of id's protocols, directly or |
| 4717 | // through its super class and categories. |
| 4718 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 4719 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 4720 | ObjCProtocolDecl *rhsProto = *J; |
| 4721 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 4722 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 4723 | match = true; |
| 4724 | break; |
| 4725 | } |
| 4726 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4727 | // If the RHS is a qualified interface pointer "NSString<P>*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4728 | // make sure we check the class hierarchy. |
| 4729 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 4730 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4731 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4732 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4733 | // see if static class implements all of id's protocols, directly or |
| 4734 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 4735 | if (rhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4736 | match = true; |
| 4737 | break; |
| 4738 | } |
| 4739 | } |
| 4740 | } |
| 4741 | if (!match) |
| 4742 | return false; |
| 4743 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4744 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4745 | return true; |
| 4746 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4747 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4748 | const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 4749 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 4750 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4751 | if (const ObjCObjectPointerType *lhsOPT = |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4752 | lhs->getAsObjCInterfacePointerType()) { |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 4753 | // If both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4754 | for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(), |
| 4755 | E = lhsOPT->qual_end(); I != E; ++I) { |
| 4756 | ObjCProtocolDecl *lhsProto = *I; |
| 4757 | bool match = false; |
| 4758 | |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 4759 | // when comparing an id<P> on rhs with a static type on lhs, |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4760 | // see if static class implements all of id's protocols, directly or |
| 4761 | // through its super class and categories. |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 4762 | // First, lhs protocols in the qualifier list must be found, direct |
| 4763 | // or indirect in rhs's qualifier list or it is a mismatch. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4764 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 4765 | E = rhsQID->qual_end(); J != E; ++J) { |
| 4766 | ObjCProtocolDecl *rhsProto = *J; |
| 4767 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 4768 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 4769 | match = true; |
| 4770 | break; |
| 4771 | } |
| 4772 | } |
| 4773 | if (!match) |
| 4774 | return false; |
| 4775 | } |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 4776 | |
| 4777 | // Static class's protocols, or its super class or category protocols |
| 4778 | // must be found, direct or indirect in rhs's qualifier list or it is a mismatch. |
| 4779 | if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { |
| 4780 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
| 4781 | CollectInheritedProtocols(lhsID, LHSInheritedProtocols); |
| 4782 | // This is rather dubious but matches gcc's behavior. If lhs has |
| 4783 | // no type qualifier and its class has no static protocol(s) |
| 4784 | // assume that it is mismatch. |
| 4785 | if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty()) |
| 4786 | return false; |
| 4787 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 4788 | LHSInheritedProtocols.begin(), |
| 4789 | E = LHSInheritedProtocols.end(); I != E; ++I) { |
| 4790 | bool match = false; |
| 4791 | ObjCProtocolDecl *lhsProto = (*I); |
| 4792 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 4793 | E = rhsQID->qual_end(); J != E; ++J) { |
| 4794 | ObjCProtocolDecl *rhsProto = *J; |
| 4795 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 4796 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 4797 | match = true; |
| 4798 | break; |
| 4799 | } |
| 4800 | } |
| 4801 | if (!match) |
| 4802 | return false; |
| 4803 | } |
| 4804 | } |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4805 | return true; |
| 4806 | } |
| 4807 | return false; |
| 4808 | } |
| 4809 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4810 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4811 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 4812 | /// protocol qualifiers on the LHS or RHS. |
| 4813 | /// |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4814 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 4815 | const ObjCObjectPointerType *RHSOPT) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4816 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 4817 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 4818 | |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 4819 | // If either type represents the built-in 'id' or 'Class' types, return true. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4820 | if (LHS->isObjCUnqualifiedIdOrClass() || |
| 4821 | RHS->isObjCUnqualifiedIdOrClass()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4822 | return true; |
| 4823 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4824 | if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4825 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 4826 | QualType(RHSOPT,0), |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4827 | false); |
Fariborz Jahanian | a8f8dac | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 4828 | |
| 4829 | if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass()) |
| 4830 | return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0), |
| 4831 | QualType(RHSOPT,0)); |
| 4832 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4833 | // If we have 2 user-defined types, fall into that path. |
| 4834 | if (LHS->getInterface() && RHS->getInterface()) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4835 | return canAssignObjCInterfaces(LHS, RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4836 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4837 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4838 | } |
| 4839 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4840 | /// canAssignObjCInterfacesInBlockPointer - This routine is specifically written |
| 4841 | /// for providing type-safty for objective-c pointers used to pass/return |
| 4842 | /// arguments in block literals. When passed as arguments, passing 'A*' where |
| 4843 | /// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is |
| 4844 | /// not OK. For the return type, the opposite is not OK. |
| 4845 | bool ASTContext::canAssignObjCInterfacesInBlockPointer( |
| 4846 | const ObjCObjectPointerType *LHSOPT, |
| 4847 | const ObjCObjectPointerType *RHSOPT) { |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 4848 | if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4849 | return true; |
| 4850 | |
| 4851 | if (LHSOPT->isObjCBuiltinType()) { |
| 4852 | return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType(); |
| 4853 | } |
| 4854 | |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 4855 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4856 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 4857 | QualType(RHSOPT,0), |
| 4858 | false); |
| 4859 | |
| 4860 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 4861 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 4862 | if (LHS && RHS) { // We have 2 user-defined types. |
| 4863 | if (LHS != RHS) { |
| 4864 | if (LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
| 4865 | return false; |
| 4866 | if (RHS->getDecl()->isSuperClassOf(LHS->getDecl())) |
| 4867 | return true; |
| 4868 | } |
| 4869 | else |
| 4870 | return true; |
| 4871 | } |
| 4872 | return false; |
| 4873 | } |
| 4874 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4875 | /// getIntersectionOfProtocols - This routine finds the intersection of set |
| 4876 | /// of protocols inherited from two distinct objective-c pointer objects. |
| 4877 | /// It is used to build composite qualifier list of the composite type of |
| 4878 | /// the conditional expression involving two objective-c pointer objects. |
| 4879 | static |
| 4880 | void getIntersectionOfProtocols(ASTContext &Context, |
| 4881 | const ObjCObjectPointerType *LHSOPT, |
| 4882 | const ObjCObjectPointerType *RHSOPT, |
| 4883 | llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) { |
| 4884 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4885 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 4886 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 4887 | assert(LHS->getInterface() && "LHS must have an interface base"); |
| 4888 | assert(RHS->getInterface() && "RHS must have an interface base"); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4889 | |
| 4890 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet; |
| 4891 | unsigned LHSNumProtocols = LHS->getNumProtocols(); |
| 4892 | if (LHSNumProtocols > 0) |
| 4893 | InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end()); |
| 4894 | else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 4895 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4896 | Context.CollectInheritedProtocols(LHS->getInterface(), |
| 4897 | LHSInheritedProtocols); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4898 | InheritedProtocolSet.insert(LHSInheritedProtocols.begin(), |
| 4899 | LHSInheritedProtocols.end()); |
| 4900 | } |
| 4901 | |
| 4902 | unsigned RHSNumProtocols = RHS->getNumProtocols(); |
| 4903 | if (RHSNumProtocols > 0) { |
Dan Gohman | cb421fa | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 4904 | ObjCProtocolDecl **RHSProtocols = |
| 4905 | const_cast<ObjCProtocolDecl **>(RHS->qual_begin()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4906 | for (unsigned i = 0; i < RHSNumProtocols; ++i) |
| 4907 | if (InheritedProtocolSet.count(RHSProtocols[i])) |
| 4908 | IntersectionOfProtocols.push_back(RHSProtocols[i]); |
| 4909 | } |
| 4910 | else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 4911 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4912 | Context.CollectInheritedProtocols(RHS->getInterface(), |
| 4913 | RHSInheritedProtocols); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 4914 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 4915 | RHSInheritedProtocols.begin(), |
| 4916 | E = RHSInheritedProtocols.end(); I != E; ++I) |
| 4917 | if (InheritedProtocolSet.count((*I))) |
| 4918 | IntersectionOfProtocols.push_back((*I)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4919 | } |
| 4920 | } |
| 4921 | |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4922 | /// areCommonBaseCompatible - Returns common base class of the two classes if |
| 4923 | /// one found. Note that this is O'2 algorithm. But it will be called as the |
| 4924 | /// last type comparison in a ?-exp of ObjC pointer types before a |
| 4925 | /// warning is issued. So, its invokation is extremely rare. |
| 4926 | QualType ASTContext::areCommonBaseCompatible( |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4927 | const ObjCObjectPointerType *Lptr, |
| 4928 | const ObjCObjectPointerType *Rptr) { |
| 4929 | const ObjCObjectType *LHS = Lptr->getObjectType(); |
| 4930 | const ObjCObjectType *RHS = Rptr->getObjectType(); |
| 4931 | const ObjCInterfaceDecl* LDecl = LHS->getInterface(); |
| 4932 | const ObjCInterfaceDecl* RDecl = RHS->getInterface(); |
| 4933 | if (!LDecl || !RDecl) |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4934 | return QualType(); |
| 4935 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4936 | while ((LDecl = LDecl->getSuperClass())) { |
| 4937 | LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4938 | if (canAssignObjCInterfaces(LHS, RHS)) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4939 | llvm::SmallVector<ObjCProtocolDecl *, 8> Protocols; |
| 4940 | getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols); |
| 4941 | |
| 4942 | QualType Result = QualType(LHS, 0); |
| 4943 | if (!Protocols.empty()) |
| 4944 | Result = getObjCObjectType(Result, Protocols.data(), Protocols.size()); |
| 4945 | Result = getObjCObjectPointerType(Result); |
| 4946 | return Result; |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4947 | } |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4948 | } |
| 4949 | |
| 4950 | return QualType(); |
| 4951 | } |
| 4952 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4953 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS, |
| 4954 | const ObjCObjectType *RHS) { |
| 4955 | assert(LHS->getInterface() && "LHS is not an interface type"); |
| 4956 | assert(RHS->getInterface() && "RHS is not an interface type"); |
| 4957 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4958 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 4959 | // the LHS. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4960 | if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface())) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4961 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4962 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4963 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 4964 | // protocol qualified at all, then we are good. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4965 | if (LHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4966 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4967 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4968 | // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it |
| 4969 | // isn't a superset. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4970 | if (RHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4971 | return true; // FIXME: should return false! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4972 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4973 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 4974 | LHSPE = LHS->qual_end(); |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4975 | LHSPI != LHSPE; LHSPI++) { |
| 4976 | bool RHSImplementsProtocol = false; |
| 4977 | |
| 4978 | // If the RHS doesn't implement the protocol on the left, the types |
| 4979 | // are incompatible. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4980 | for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(), |
| 4981 | RHSPE = RHS->qual_end(); |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 4982 | RHSPI != RHSPE; RHSPI++) { |
| 4983 | if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) { |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4984 | RHSImplementsProtocol = true; |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 4985 | break; |
| 4986 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4987 | } |
| 4988 | // FIXME: For better diagnostics, consider passing back the protocol name. |
| 4989 | if (!RHSImplementsProtocol) |
| 4990 | return false; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4991 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4992 | // The RHS implements all protocols listed on the LHS. |
| 4993 | return true; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4994 | } |
| 4995 | |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4996 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 4997 | // get the "pointed to" types |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4998 | const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
| 4999 | const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5000 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5001 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 5002 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5003 | |
| 5004 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 5005 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 5006 | } |
| 5007 | |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 5008 | bool ASTContext::canBindObjCObjectType(QualType To, QualType From) { |
| 5009 | return canAssignObjCInterfaces( |
| 5010 | getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(), |
| 5011 | getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>()); |
| 5012 | } |
| 5013 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5014 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5015 | /// both shall have the identically qualified version of a compatible type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5016 | /// C99 6.2.7p1: Two types have compatible types if their types are the |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5017 | /// same. See 6.7.[2,3,5] for additional rules. |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5018 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS, |
| 5019 | bool CompareUnqualified) { |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 5020 | if (getLangOptions().CPlusPlus) |
| 5021 | return hasSameType(LHS, RHS); |
| 5022 | |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5023 | return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5024 | } |
| 5025 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5026 | bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) { |
| 5027 | return !mergeTypes(LHS, RHS, true).isNull(); |
| 5028 | } |
| 5029 | |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 5030 | /// mergeTransparentUnionType - if T is a transparent union type and a member |
| 5031 | /// of T is compatible with SubType, return the merged type, else return |
| 5032 | /// QualType() |
| 5033 | QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType, |
| 5034 | bool OfBlockPointer, |
| 5035 | bool Unqualified) { |
| 5036 | if (const RecordType *UT = T->getAsUnionType()) { |
| 5037 | RecordDecl *UD = UT->getDecl(); |
| 5038 | if (UD->hasAttr<TransparentUnionAttr>()) { |
| 5039 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 5040 | itend = UD->field_end(); it != itend; ++it) { |
Peter Collingbourne | f91d757 | 2010-12-02 21:00:06 +0000 | [diff] [blame] | 5041 | QualType ET = it->getType().getUnqualifiedType(); |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 5042 | QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified); |
| 5043 | if (!MT.isNull()) |
| 5044 | return MT; |
| 5045 | } |
| 5046 | } |
| 5047 | } |
| 5048 | |
| 5049 | return QualType(); |
| 5050 | } |
| 5051 | |
| 5052 | /// mergeFunctionArgumentTypes - merge two types which appear as function |
| 5053 | /// argument types |
| 5054 | QualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs, |
| 5055 | bool OfBlockPointer, |
| 5056 | bool Unqualified) { |
| 5057 | // GNU extension: two types are compatible if they appear as a function |
| 5058 | // argument, one of the types is a transparent union type and the other |
| 5059 | // type is compatible with a union member |
| 5060 | QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer, |
| 5061 | Unqualified); |
| 5062 | if (!lmerge.isNull()) |
| 5063 | return lmerge; |
| 5064 | |
| 5065 | QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer, |
| 5066 | Unqualified); |
| 5067 | if (!rmerge.isNull()) |
| 5068 | return rmerge; |
| 5069 | |
| 5070 | return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified); |
| 5071 | } |
| 5072 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5073 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs, |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5074 | bool OfBlockPointer, |
| 5075 | bool Unqualified) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5076 | const FunctionType *lbase = lhs->getAs<FunctionType>(); |
| 5077 | const FunctionType *rbase = rhs->getAs<FunctionType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5078 | const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 5079 | const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5080 | bool allLTypes = true; |
| 5081 | bool allRTypes = true; |
| 5082 | |
| 5083 | // Check return type |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5084 | QualType retType; |
Fariborz Jahanian | d263fd1 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 5085 | if (OfBlockPointer) { |
| 5086 | QualType RHS = rbase->getResultType(); |
| 5087 | QualType LHS = lbase->getResultType(); |
| 5088 | bool UnqualifiedResult = Unqualified; |
| 5089 | if (!UnqualifiedResult) |
| 5090 | UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers()); |
| 5091 | retType = mergeTypes(RHS, LHS, true, UnqualifiedResult); |
| 5092 | } |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5093 | else |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5094 | retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), false, |
| 5095 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5096 | if (retType.isNull()) return QualType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5097 | |
| 5098 | if (Unqualified) |
| 5099 | retType = retType.getUnqualifiedType(); |
| 5100 | |
| 5101 | CanQualType LRetType = getCanonicalType(lbase->getResultType()); |
| 5102 | CanQualType RRetType = getCanonicalType(rbase->getResultType()); |
| 5103 | if (Unqualified) { |
| 5104 | LRetType = LRetType.getUnqualifiedType(); |
| 5105 | RRetType = RRetType.getUnqualifiedType(); |
| 5106 | } |
| 5107 | |
| 5108 | if (getCanonicalType(retType) != LRetType) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5109 | allLTypes = false; |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5110 | if (getCanonicalType(retType) != RRetType) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5111 | allRTypes = false; |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5112 | |
Daniel Dunbar | 6a15c85 | 2010-04-28 16:20:58 +0000 | [diff] [blame] | 5113 | // FIXME: double check this |
| 5114 | // FIXME: should we error if lbase->getRegParmAttr() != 0 && |
| 5115 | // rbase->getRegParmAttr() != 0 && |
| 5116 | // lbase->getRegParmAttr() != rbase->getRegParmAttr()? |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 5117 | FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo(); |
| 5118 | FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo(); |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5119 | |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 5120 | // Compatible functions must have compatible calling conventions |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5121 | if (!isSameCallConv(lbaseInfo.getCC(), rbaseInfo.getCC())) |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 5122 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5123 | |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5124 | // Regparm is part of the calling convention. |
| 5125 | if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm()) |
| 5126 | return QualType(); |
| 5127 | |
| 5128 | // It's noreturn if either type is. |
| 5129 | // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'. |
| 5130 | bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn(); |
| 5131 | if (NoReturn != lbaseInfo.getNoReturn()) |
| 5132 | allLTypes = false; |
| 5133 | if (NoReturn != rbaseInfo.getNoReturn()) |
| 5134 | allRTypes = false; |
| 5135 | |
| 5136 | FunctionType::ExtInfo einfo(NoReturn, |
| 5137 | lbaseInfo.getRegParm(), |
| 5138 | lbaseInfo.getCC()); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5139 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5140 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 5141 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 5142 | "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5143 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 5144 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 5145 | |
| 5146 | // Compatible functions must have the same number of arguments |
| 5147 | if (lproto_nargs != rproto_nargs) |
| 5148 | return QualType(); |
| 5149 | |
| 5150 | // Variadic and non-variadic functions aren't compatible |
| 5151 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 5152 | return QualType(); |
| 5153 | |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 5154 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 5155 | return QualType(); |
| 5156 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5157 | // Check argument compatibility |
| 5158 | llvm::SmallVector<QualType, 10> types; |
| 5159 | for (unsigned i = 0; i < lproto_nargs; i++) { |
| 5160 | QualType largtype = lproto->getArgType(i).getUnqualifiedType(); |
| 5161 | QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 5162 | QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype, |
| 5163 | OfBlockPointer, |
| 5164 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5165 | if (argtype.isNull()) return QualType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5166 | |
| 5167 | if (Unqualified) |
| 5168 | argtype = argtype.getUnqualifiedType(); |
| 5169 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5170 | types.push_back(argtype); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5171 | if (Unqualified) { |
| 5172 | largtype = largtype.getUnqualifiedType(); |
| 5173 | rargtype = rargtype.getUnqualifiedType(); |
| 5174 | } |
| 5175 | |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5176 | if (getCanonicalType(argtype) != getCanonicalType(largtype)) |
| 5177 | allLTypes = false; |
| 5178 | if (getCanonicalType(argtype) != getCanonicalType(rargtype)) |
| 5179 | allRTypes = false; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5180 | } |
| 5181 | if (allLTypes) return lhs; |
| 5182 | if (allRTypes) return rhs; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5183 | |
| 5184 | FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo(); |
| 5185 | EPI.ExtInfo = einfo; |
| 5186 | return getFunctionType(retType, types.begin(), types.size(), EPI); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5187 | } |
| 5188 | |
| 5189 | if (lproto) allRTypes = false; |
| 5190 | if (rproto) allLTypes = false; |
| 5191 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5192 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5193 | if (proto) { |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 5194 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5195 | if (proto->isVariadic()) return QualType(); |
| 5196 | // Check that the types are compatible with the types that |
| 5197 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 5198 | // The only types actually affected are promotable integer |
| 5199 | // types and floats, which would be passed as a different |
| 5200 | // type depending on whether the prototype is visible. |
| 5201 | unsigned proto_nargs = proto->getNumArgs(); |
| 5202 | for (unsigned i = 0; i < proto_nargs; ++i) { |
| 5203 | QualType argTy = proto->getArgType(i); |
Douglas Gregor | b0f8eac | 2010-02-03 19:27:29 +0000 | [diff] [blame] | 5204 | |
| 5205 | // Look at the promotion type of enum types, since that is the type used |
| 5206 | // to pass enum values. |
| 5207 | if (const EnumType *Enum = argTy->getAs<EnumType>()) |
| 5208 | argTy = Enum->getDecl()->getPromotionType(); |
| 5209 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5210 | if (argTy->isPromotableIntegerType() || |
| 5211 | getCanonicalType(argTy).getUnqualifiedType() == FloatTy) |
| 5212 | return QualType(); |
| 5213 | } |
| 5214 | |
| 5215 | if (allLTypes) return lhs; |
| 5216 | if (allRTypes) return rhs; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5217 | |
| 5218 | FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo(); |
| 5219 | EPI.ExtInfo = einfo; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5220 | return getFunctionType(retType, proto->arg_type_begin(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5221 | proto->getNumArgs(), EPI); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5222 | } |
| 5223 | |
| 5224 | if (allLTypes) return lhs; |
| 5225 | if (allRTypes) return rhs; |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5226 | return getFunctionNoProtoType(retType, einfo); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5227 | } |
| 5228 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5229 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5230 | bool OfBlockPointer, |
| 5231 | bool Unqualified) { |
Bill Wendling | 43d6975 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 5232 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 5233 | // type is adjusted to "T" prior to any further analysis, the expression |
| 5234 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 5235 | // expression is an lvalue unless the reference is an rvalue reference and |
| 5236 | // the expression is a function call (possibly inside parentheses). |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 5237 | assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?"); |
| 5238 | assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?"); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5239 | |
| 5240 | if (Unqualified) { |
| 5241 | LHS = LHS.getUnqualifiedType(); |
| 5242 | RHS = RHS.getUnqualifiedType(); |
| 5243 | } |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 5244 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5245 | QualType LHSCan = getCanonicalType(LHS), |
| 5246 | RHSCan = getCanonicalType(RHS); |
| 5247 | |
| 5248 | // If two types are identical, they are compatible. |
| 5249 | if (LHSCan == RHSCan) |
| 5250 | return LHS; |
| 5251 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5252 | // If the qualifiers are different, the types aren't compatible... mostly. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 5253 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 5254 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5255 | if (LQuals != RQuals) { |
| 5256 | // If any of these qualifiers are different, we have a type |
| 5257 | // mismatch. |
| 5258 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 5259 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 5260 | return QualType(); |
| 5261 | |
| 5262 | // Exactly one GC qualifier difference is allowed: __strong is |
| 5263 | // okay if the other type has no GC qualifier but is an Objective |
| 5264 | // C object pointer (i.e. implicitly strong by default). We fix |
| 5265 | // this by pretending that the unqualified type was actually |
| 5266 | // qualified __strong. |
| 5267 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 5268 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 5269 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 5270 | |
| 5271 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 5272 | return QualType(); |
| 5273 | |
| 5274 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
| 5275 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); |
| 5276 | } |
| 5277 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
| 5278 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); |
| 5279 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5280 | return QualType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5281 | } |
| 5282 | |
| 5283 | // Okay, qualifiers are equal. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5284 | |
Eli Friedman | 852d63b | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 5285 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 5286 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5287 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5288 | // We want to consider the two function types to be the same for these |
| 5289 | // comparisons, just force one to the other. |
| 5290 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 5291 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 4c721d3 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 5292 | |
| 5293 | // Same as above for arrays |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 5294 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 5295 | LHSClass = Type::ConstantArray; |
| 5296 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 5297 | RHSClass = Type::ConstantArray; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5298 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5299 | // ObjCInterfaces are just specialized ObjCObjects. |
| 5300 | if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject; |
| 5301 | if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject; |
| 5302 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 5303 | // Canonicalize ExtVector -> Vector. |
| 5304 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 5305 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5306 | |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 5307 | // If the canonical type classes don't match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5308 | if (LHSClass != RHSClass) { |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5309 | // C99 6.7.2.2p4: Each enumerated type shall be compatible with char, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5310 | // a signed integer type, or an unsigned integer type. |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 5311 | // Compatibility is based on the underlying type, not the promotion |
| 5312 | // type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5313 | if (const EnumType* ETy = LHS->getAs<EnumType>()) { |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5314 | if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType()) |
| 5315 | return RHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 5316 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5317 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5318 | if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType()) |
| 5319 | return LHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 5320 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5321 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5322 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5323 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5324 | |
Steve Naroff | 4a74678 | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 5325 | // The canonical type classes match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5326 | switch (LHSClass) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5327 | #define TYPE(Class, Base) |
| 5328 | #define ABSTRACT_TYPE(Class, Base) |
John McCall | ad5e738 | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 5329 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5330 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 5331 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 5332 | #include "clang/AST/TypeNodes.def" |
| 5333 | assert(false && "Non-canonical and dependent types shouldn't get here"); |
| 5334 | return QualType(); |
| 5335 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 5336 | case Type::LValueReference: |
| 5337 | case Type::RValueReference: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5338 | case Type::MemberPointer: |
| 5339 | assert(false && "C++ should never be in mergeTypes"); |
| 5340 | return QualType(); |
| 5341 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5342 | case Type::ObjCInterface: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5343 | case Type::IncompleteArray: |
| 5344 | case Type::VariableArray: |
| 5345 | case Type::FunctionProto: |
| 5346 | case Type::ExtVector: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5347 | assert(false && "Types are eliminated above"); |
| 5348 | return QualType(); |
| 5349 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5350 | case Type::Pointer: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5351 | { |
| 5352 | // Merge two pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5353 | QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); |
| 5354 | QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5355 | if (Unqualified) { |
| 5356 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 5357 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 5358 | } |
| 5359 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false, |
| 5360 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5361 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 5362 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5363 | return LHS; |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 5364 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5365 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5366 | return getPointerType(ResultType); |
| 5367 | } |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 5368 | case Type::BlockPointer: |
| 5369 | { |
| 5370 | // Merge two block pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5371 | QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); |
| 5372 | QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5373 | if (Unqualified) { |
| 5374 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 5375 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 5376 | } |
| 5377 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer, |
| 5378 | Unqualified); |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 5379 | if (ResultType.isNull()) return QualType(); |
| 5380 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 5381 | return LHS; |
| 5382 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 5383 | return RHS; |
| 5384 | return getBlockPointerType(ResultType); |
| 5385 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5386 | case Type::ConstantArray: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5387 | { |
| 5388 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 5389 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 5390 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 5391 | return QualType(); |
| 5392 | |
| 5393 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 5394 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5395 | if (Unqualified) { |
| 5396 | LHSElem = LHSElem.getUnqualifiedType(); |
| 5397 | RHSElem = RHSElem.getUnqualifiedType(); |
| 5398 | } |
| 5399 | |
| 5400 | QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5401 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5402 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 5403 | return LHS; |
| 5404 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 5405 | return RHS; |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 5406 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 5407 | ArrayType::ArraySizeModifier(), 0); |
| 5408 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 5409 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5410 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 5411 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5412 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 5413 | return LHS; |
| 5414 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 5415 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5416 | if (LVAT) { |
| 5417 | // FIXME: This isn't correct! But tricky to implement because |
| 5418 | // the array's size has to be the size of LHS, but the type |
| 5419 | // has to be different. |
| 5420 | return LHS; |
| 5421 | } |
| 5422 | if (RVAT) { |
| 5423 | // FIXME: This isn't correct! But tricky to implement because |
| 5424 | // the array's size has to be the size of RHS, but the type |
| 5425 | // has to be different. |
| 5426 | return RHS; |
| 5427 | } |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 5428 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 5429 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 5430 | return getIncompleteArrayType(ResultType, |
| 5431 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5432 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5433 | case Type::FunctionNoProto: |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5434 | return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5435 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5436 | case Type::Enum: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5437 | return QualType(); |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5438 | case Type::Builtin: |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 5439 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5440 | return QualType(); |
Daniel Dunbar | 64cfdb7 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 5441 | case Type::Complex: |
| 5442 | // Distinct complex types are incompatible. |
| 5443 | return QualType(); |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 5444 | case Type::Vector: |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 5445 | // FIXME: The merged type should be an ExtVector! |
John McCall | 1c471f3 | 2010-03-12 23:14:13 +0000 | [diff] [blame] | 5446 | if (areCompatVectorTypes(LHSCan->getAs<VectorType>(), |
| 5447 | RHSCan->getAs<VectorType>())) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5448 | return LHS; |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5449 | return QualType(); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5450 | case Type::ObjCObject: { |
| 5451 | // Check if the types are assignment compatible. |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 5452 | // FIXME: This should be type compatibility, e.g. whether |
| 5453 | // "LHS x; RHS x;" at global scope is legal. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5454 | const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>(); |
| 5455 | const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>(); |
| 5456 | if (canAssignObjCInterfaces(LHSIface, RHSIface)) |
Steve Naroff | 5fd659d | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 5457 | return LHS; |
| 5458 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5459 | return QualType(); |
Cedric Venet | 61490e9 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 5460 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5461 | case Type::ObjCObjectPointer: { |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5462 | if (OfBlockPointer) { |
| 5463 | if (canAssignObjCInterfacesInBlockPointer( |
| 5464 | LHS->getAs<ObjCObjectPointerType>(), |
| 5465 | RHS->getAs<ObjCObjectPointerType>())) |
| 5466 | return LHS; |
| 5467 | return QualType(); |
| 5468 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5469 | if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(), |
| 5470 | RHS->getAs<ObjCObjectPointerType>())) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5471 | return LHS; |
| 5472 | |
Steve Naroff | bc76dd0 | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 5473 | return QualType(); |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5474 | } |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5475 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5476 | |
| 5477 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5478 | } |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 5479 | |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 5480 | /// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and |
| 5481 | /// 'RHS' attributes and returns the merged version; including for function |
| 5482 | /// return types. |
| 5483 | QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) { |
| 5484 | QualType LHSCan = getCanonicalType(LHS), |
| 5485 | RHSCan = getCanonicalType(RHS); |
| 5486 | // If two types are identical, they are compatible. |
| 5487 | if (LHSCan == RHSCan) |
| 5488 | return LHS; |
| 5489 | if (RHSCan->isFunctionType()) { |
| 5490 | if (!LHSCan->isFunctionType()) |
| 5491 | return QualType(); |
| 5492 | QualType OldReturnType = |
| 5493 | cast<FunctionType>(RHSCan.getTypePtr())->getResultType(); |
| 5494 | QualType NewReturnType = |
| 5495 | cast<FunctionType>(LHSCan.getTypePtr())->getResultType(); |
| 5496 | QualType ResReturnType = |
| 5497 | mergeObjCGCQualifiers(NewReturnType, OldReturnType); |
| 5498 | if (ResReturnType.isNull()) |
| 5499 | return QualType(); |
| 5500 | if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) { |
| 5501 | // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo(); |
| 5502 | // In either case, use OldReturnType to build the new function type. |
| 5503 | const FunctionType *F = LHS->getAs<FunctionType>(); |
| 5504 | if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5505 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 5506 | EPI.ExtInfo = getFunctionExtInfo(LHS); |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 5507 | QualType ResultType |
| 5508 | = getFunctionType(OldReturnType, FPT->arg_type_begin(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5509 | FPT->getNumArgs(), EPI); |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 5510 | return ResultType; |
| 5511 | } |
| 5512 | } |
| 5513 | return QualType(); |
| 5514 | } |
| 5515 | |
| 5516 | // If the qualifiers are different, the types can still be merged. |
| 5517 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 5518 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
| 5519 | if (LQuals != RQuals) { |
| 5520 | // If any of these qualifiers are different, we have a type mismatch. |
| 5521 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 5522 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 5523 | return QualType(); |
| 5524 | |
| 5525 | // Exactly one GC qualifier difference is allowed: __strong is |
| 5526 | // okay if the other type has no GC qualifier but is an Objective |
| 5527 | // C object pointer (i.e. implicitly strong by default). We fix |
| 5528 | // this by pretending that the unqualified type was actually |
| 5529 | // qualified __strong. |
| 5530 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 5531 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 5532 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 5533 | |
| 5534 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 5535 | return QualType(); |
| 5536 | |
| 5537 | if (GC_L == Qualifiers::Strong) |
| 5538 | return LHS; |
| 5539 | if (GC_R == Qualifiers::Strong) |
| 5540 | return RHS; |
| 5541 | return QualType(); |
| 5542 | } |
| 5543 | |
| 5544 | if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) { |
| 5545 | QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5546 | QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5547 | QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT); |
| 5548 | if (ResQT == LHSBaseQT) |
| 5549 | return LHS; |
| 5550 | if (ResQT == RHSBaseQT) |
| 5551 | return RHS; |
| 5552 | } |
| 5553 | return QualType(); |
| 5554 | } |
| 5555 | |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 5556 | //===----------------------------------------------------------------------===// |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5557 | // Integer Predicates |
| 5558 | //===----------------------------------------------------------------------===// |
Chris Lattner | 88054de | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 5559 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5560 | unsigned ASTContext::getIntWidth(QualType T) const { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 5561 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
Eli Friedman | 29a7f33 | 2009-12-10 22:29:29 +0000 | [diff] [blame] | 5562 | T = ET->getDecl()->getIntegerType(); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 5563 | if (T->isBooleanType()) |
| 5564 | return 1; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 5565 | // For builtin types, just use the standard type sizing method |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5566 | return (unsigned)getTypeSize(T); |
| 5567 | } |
| 5568 | |
| 5569 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) { |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5570 | assert(T->hasSignedIntegerRepresentation() && "Unexpected type"); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5571 | |
| 5572 | // Turn <4 x signed int> -> <4 x unsigned int> |
| 5573 | if (const VectorType *VTy = T->getAs<VectorType>()) |
| 5574 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 5575 | VTy->getNumElements(), VTy->getVectorKind()); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5576 | |
| 5577 | // For enums, we return the unsigned version of the base type. |
| 5578 | if (const EnumType *ETy = T->getAs<EnumType>()) |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5579 | T = ETy->getDecl()->getIntegerType(); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5580 | |
| 5581 | const BuiltinType *BTy = T->getAs<BuiltinType>(); |
| 5582 | assert(BTy && "Unexpected signed integer type"); |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5583 | switch (BTy->getKind()) { |
| 5584 | case BuiltinType::Char_S: |
| 5585 | case BuiltinType::SChar: |
| 5586 | return UnsignedCharTy; |
| 5587 | case BuiltinType::Short: |
| 5588 | return UnsignedShortTy; |
| 5589 | case BuiltinType::Int: |
| 5590 | return UnsignedIntTy; |
| 5591 | case BuiltinType::Long: |
| 5592 | return UnsignedLongTy; |
| 5593 | case BuiltinType::LongLong: |
| 5594 | return UnsignedLongLongTy; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 5595 | case BuiltinType::Int128: |
| 5596 | return UnsignedInt128Ty; |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5597 | default: |
| 5598 | assert(0 && "Unexpected signed integer type"); |
| 5599 | return QualType(); |
| 5600 | } |
| 5601 | } |
| 5602 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 5603 | ExternalASTSource::~ExternalASTSource() { } |
| 5604 | |
| 5605 | void ExternalASTSource::PrintStats() { } |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5606 | |
Argyrios Kyrtzidis | 7b90340 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 5607 | ASTMutationListener::~ASTMutationListener() { } |
| 5608 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5609 | |
| 5610 | //===----------------------------------------------------------------------===// |
| 5611 | // Builtin Type Computation |
| 5612 | //===----------------------------------------------------------------------===// |
| 5613 | |
| 5614 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5615 | /// pointer over the consumed characters. This returns the resultant type. If |
| 5616 | /// AllowTypeModifiers is false then modifier like * are not parsed, just basic |
| 5617 | /// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of |
| 5618 | /// a vector of "i*". |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5619 | /// |
| 5620 | /// RequiresICE is filled in on return to indicate whether the value is required |
| 5621 | /// to be an Integer Constant Expression. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5622 | static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context, |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5623 | ASTContext::GetBuiltinTypeError &Error, |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5624 | bool &RequiresICE, |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5625 | bool AllowTypeModifiers) { |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5626 | // Modifiers. |
| 5627 | int HowLong = 0; |
| 5628 | bool Signed = false, Unsigned = false; |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5629 | RequiresICE = false; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 5630 | |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5631 | // Read the prefixed modifiers first. |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5632 | bool Done = false; |
| 5633 | while (!Done) { |
| 5634 | switch (*Str++) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5635 | default: Done = true; --Str; break; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 5636 | case 'I': |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5637 | RequiresICE = true; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 5638 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5639 | case 'S': |
| 5640 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 5641 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 5642 | Signed = true; |
| 5643 | break; |
| 5644 | case 'U': |
| 5645 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
| 5646 | assert(!Unsigned && "Can't use 'S' modifier multiple times!"); |
| 5647 | Unsigned = true; |
| 5648 | break; |
| 5649 | case 'L': |
| 5650 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 5651 | ++HowLong; |
| 5652 | break; |
| 5653 | } |
| 5654 | } |
| 5655 | |
| 5656 | QualType Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5657 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5658 | // Read the base type. |
| 5659 | switch (*Str++) { |
| 5660 | default: assert(0 && "Unknown builtin type letter!"); |
| 5661 | case 'v': |
| 5662 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 5663 | "Bad modifiers used with 'v'!"); |
| 5664 | Type = Context.VoidTy; |
| 5665 | break; |
| 5666 | case 'f': |
| 5667 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 5668 | "Bad modifiers used with 'f'!"); |
| 5669 | Type = Context.FloatTy; |
| 5670 | break; |
| 5671 | case 'd': |
| 5672 | assert(HowLong < 2 && !Signed && !Unsigned && |
| 5673 | "Bad modifiers used with 'd'!"); |
| 5674 | if (HowLong) |
| 5675 | Type = Context.LongDoubleTy; |
| 5676 | else |
| 5677 | Type = Context.DoubleTy; |
| 5678 | break; |
| 5679 | case 's': |
| 5680 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 5681 | if (Unsigned) |
| 5682 | Type = Context.UnsignedShortTy; |
| 5683 | else |
| 5684 | Type = Context.ShortTy; |
| 5685 | break; |
| 5686 | case 'i': |
| 5687 | if (HowLong == 3) |
| 5688 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 5689 | else if (HowLong == 2) |
| 5690 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 5691 | else if (HowLong == 1) |
| 5692 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 5693 | else |
| 5694 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 5695 | break; |
| 5696 | case 'c': |
| 5697 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 5698 | if (Signed) |
| 5699 | Type = Context.SignedCharTy; |
| 5700 | else if (Unsigned) |
| 5701 | Type = Context.UnsignedCharTy; |
| 5702 | else |
| 5703 | Type = Context.CharTy; |
| 5704 | break; |
| 5705 | case 'b': // boolean |
| 5706 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 5707 | Type = Context.BoolTy; |
| 5708 | break; |
| 5709 | case 'z': // size_t. |
| 5710 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 5711 | Type = Context.getSizeType(); |
| 5712 | break; |
| 5713 | case 'F': |
| 5714 | Type = Context.getCFConstantStringType(); |
| 5715 | break; |
Fariborz Jahanian | ba8bda0 | 2010-11-09 21:38:20 +0000 | [diff] [blame] | 5716 | case 'G': |
| 5717 | Type = Context.getObjCIdType(); |
| 5718 | break; |
| 5719 | case 'H': |
| 5720 | Type = Context.getObjCSelType(); |
| 5721 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5722 | case 'a': |
| 5723 | Type = Context.getBuiltinVaListType(); |
| 5724 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 5725 | break; |
| 5726 | case 'A': |
| 5727 | // This is a "reference" to a va_list; however, what exactly |
| 5728 | // this means depends on how va_list is defined. There are two |
| 5729 | // different kinds of va_list: ones passed by value, and ones |
| 5730 | // passed by reference. An example of a by-value va_list is |
| 5731 | // x86, where va_list is a char*. An example of by-ref va_list |
| 5732 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 5733 | // we want this argument to be a char*&; for x86-64, we want |
| 5734 | // it to be a __va_list_tag*. |
| 5735 | Type = Context.getBuiltinVaListType(); |
| 5736 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5737 | if (Type->isArrayType()) |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5738 | Type = Context.getArrayDecayedType(Type); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5739 | else |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5740 | Type = Context.getLValueReferenceType(Type); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5741 | break; |
| 5742 | case 'V': { |
| 5743 | char *End; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5744 | unsigned NumElements = strtoul(Str, &End, 10); |
| 5745 | assert(End != Str && "Missing vector size"); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5746 | Str = End; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5747 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5748 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, |
| 5749 | RequiresICE, false); |
| 5750 | assert(!RequiresICE && "Can't require vector ICE"); |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5751 | |
| 5752 | // TODO: No way to make AltiVec vectors in builtins yet. |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 5753 | Type = Context.getVectorType(ElementType, NumElements, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 5754 | VectorType::GenericVector); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5755 | break; |
| 5756 | } |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 5757 | case 'X': { |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5758 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
| 5759 | false); |
| 5760 | assert(!RequiresICE && "Can't require complex ICE"); |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 5761 | Type = Context.getComplexType(ElementType); |
| 5762 | break; |
| 5763 | } |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 5764 | case 'P': |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 5765 | Type = Context.getFILEType(); |
| 5766 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 5767 | Error = ASTContext::GE_Missing_stdio; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5768 | return QualType(); |
| 5769 | } |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 5770 | break; |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 5771 | case 'J': |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 5772 | if (Signed) |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 5773 | Type = Context.getsigjmp_bufType(); |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 5774 | else |
| 5775 | Type = Context.getjmp_bufType(); |
| 5776 | |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 5777 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 5778 | Error = ASTContext::GE_Missing_setjmp; |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 5779 | return QualType(); |
| 5780 | } |
| 5781 | break; |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 5782 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5783 | |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5784 | // If there are modifiers and if we're allowed to parse them, go for it. |
| 5785 | Done = !AllowTypeModifiers; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5786 | while (!Done) { |
John McCall | 187ab37 | 2010-03-12 04:21:28 +0000 | [diff] [blame] | 5787 | switch (char c = *Str++) { |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5788 | default: Done = true; --Str; break; |
| 5789 | case '*': |
| 5790 | case '&': { |
| 5791 | // Both pointers and references can have their pointee types |
| 5792 | // qualified with an address space. |
| 5793 | char *End; |
| 5794 | unsigned AddrSpace = strtoul(Str, &End, 10); |
| 5795 | if (End != Str && AddrSpace != 0) { |
| 5796 | Type = Context.getAddrSpaceQualType(Type, AddrSpace); |
| 5797 | Str = End; |
| 5798 | } |
| 5799 | if (c == '*') |
| 5800 | Type = Context.getPointerType(Type); |
| 5801 | else |
| 5802 | Type = Context.getLValueReferenceType(Type); |
| 5803 | break; |
| 5804 | } |
| 5805 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 5806 | case 'C': |
| 5807 | Type = Type.withConst(); |
| 5808 | break; |
| 5809 | case 'D': |
| 5810 | Type = Context.getVolatileType(Type); |
| 5811 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5812 | } |
| 5813 | } |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 5814 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5815 | assert((!RequiresICE || Type->isIntegralOrEnumerationType()) && |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 5816 | "Integer constant 'I' type must be an integer"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5817 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5818 | return Type; |
| 5819 | } |
| 5820 | |
| 5821 | /// GetBuiltinType - Return the type for the specified builtin. |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5822 | QualType ASTContext::GetBuiltinType(unsigned Id, |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5823 | GetBuiltinTypeError &Error, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5824 | unsigned *IntegerConstantArgs) const { |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5825 | const char *TypeStr = BuiltinInfo.GetTypeString(Id); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5826 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5827 | llvm::SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5828 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5829 | bool RequiresICE = false; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5830 | Error = GE_None; |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5831 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error, |
| 5832 | RequiresICE, true); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5833 | if (Error != GE_None) |
| 5834 | return QualType(); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5835 | |
| 5836 | assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE"); |
| 5837 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5838 | while (TypeStr[0] && TypeStr[0] != '.') { |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5839 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5840 | if (Error != GE_None) |
| 5841 | return QualType(); |
| 5842 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5843 | // If this argument is required to be an IntegerConstantExpression and the |
| 5844 | // caller cares, fill in the bitmask we return. |
| 5845 | if (RequiresICE && IntegerConstantArgs) |
| 5846 | *IntegerConstantArgs |= 1 << ArgTypes.size(); |
| 5847 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5848 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 5849 | if (Ty->isArrayType()) |
| 5850 | Ty = getArrayDecayedType(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5851 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5852 | ArgTypes.push_back(Ty); |
| 5853 | } |
| 5854 | |
| 5855 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 5856 | "'.' should only occur at end of builtin type list!"); |
| 5857 | |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 5858 | FunctionType::ExtInfo EI; |
| 5859 | if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true); |
| 5860 | |
| 5861 | bool Variadic = (TypeStr[0] == '.'); |
| 5862 | |
| 5863 | // We really shouldn't be making a no-proto type here, especially in C++. |
| 5864 | if (ArgTypes.empty() && Variadic) |
| 5865 | return getFunctionNoProtoType(ResType, EI); |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 5866 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5867 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 5868 | EPI.ExtInfo = EI; |
| 5869 | EPI.Variadic = Variadic; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5870 | |
| 5871 | return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), EPI); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5872 | } |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5873 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5874 | GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) { |
| 5875 | GVALinkage External = GVA_StrongExternal; |
| 5876 | |
| 5877 | Linkage L = FD->getLinkage(); |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5878 | switch (L) { |
| 5879 | case NoLinkage: |
| 5880 | case InternalLinkage: |
| 5881 | case UniqueExternalLinkage: |
| 5882 | return GVA_Internal; |
| 5883 | |
| 5884 | case ExternalLinkage: |
| 5885 | switch (FD->getTemplateSpecializationKind()) { |
| 5886 | case TSK_Undeclared: |
| 5887 | case TSK_ExplicitSpecialization: |
| 5888 | External = GVA_StrongExternal; |
| 5889 | break; |
| 5890 | |
| 5891 | case TSK_ExplicitInstantiationDefinition: |
| 5892 | return GVA_ExplicitTemplateInstantiation; |
| 5893 | |
| 5894 | case TSK_ExplicitInstantiationDeclaration: |
| 5895 | case TSK_ImplicitInstantiation: |
| 5896 | External = GVA_TemplateInstantiation; |
| 5897 | break; |
| 5898 | } |
| 5899 | } |
| 5900 | |
| 5901 | if (!FD->isInlined()) |
| 5902 | return External; |
| 5903 | |
| 5904 | if (!getLangOptions().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) { |
| 5905 | // GNU or C99 inline semantics. Determine whether this symbol should be |
| 5906 | // externally visible. |
| 5907 | if (FD->isInlineDefinitionExternallyVisible()) |
| 5908 | return External; |
| 5909 | |
| 5910 | // C99 inline semantics, where the symbol is not externally visible. |
| 5911 | return GVA_C99Inline; |
| 5912 | } |
| 5913 | |
| 5914 | // C++0x [temp.explicit]p9: |
| 5915 | // [ Note: The intent is that an inline function that is the subject of |
| 5916 | // an explicit instantiation declaration will still be implicitly |
| 5917 | // instantiated when used so that the body can be considered for |
| 5918 | // inlining, but that no out-of-line copy of the inline function would be |
| 5919 | // generated in the translation unit. -- end note ] |
| 5920 | if (FD->getTemplateSpecializationKind() |
| 5921 | == TSK_ExplicitInstantiationDeclaration) |
| 5922 | return GVA_C99Inline; |
| 5923 | |
| 5924 | return GVA_CXXInline; |
| 5925 | } |
| 5926 | |
| 5927 | GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) { |
| 5928 | // If this is a static data member, compute the kind of template |
| 5929 | // specialization. Otherwise, this variable is not part of a |
| 5930 | // template. |
| 5931 | TemplateSpecializationKind TSK = TSK_Undeclared; |
| 5932 | if (VD->isStaticDataMember()) |
| 5933 | TSK = VD->getTemplateSpecializationKind(); |
| 5934 | |
| 5935 | Linkage L = VD->getLinkage(); |
| 5936 | if (L == ExternalLinkage && getLangOptions().CPlusPlus && |
| 5937 | VD->getType()->getLinkage() == UniqueExternalLinkage) |
| 5938 | L = UniqueExternalLinkage; |
| 5939 | |
| 5940 | switch (L) { |
| 5941 | case NoLinkage: |
| 5942 | case InternalLinkage: |
| 5943 | case UniqueExternalLinkage: |
| 5944 | return GVA_Internal; |
| 5945 | |
| 5946 | case ExternalLinkage: |
| 5947 | switch (TSK) { |
| 5948 | case TSK_Undeclared: |
| 5949 | case TSK_ExplicitSpecialization: |
| 5950 | return GVA_StrongExternal; |
| 5951 | |
| 5952 | case TSK_ExplicitInstantiationDeclaration: |
| 5953 | llvm_unreachable("Variable should not be instantiated"); |
| 5954 | // Fall through to treat this like any other instantiation. |
| 5955 | |
| 5956 | case TSK_ExplicitInstantiationDefinition: |
| 5957 | return GVA_ExplicitTemplateInstantiation; |
| 5958 | |
| 5959 | case TSK_ImplicitInstantiation: |
| 5960 | return GVA_TemplateInstantiation; |
| 5961 | } |
| 5962 | } |
| 5963 | |
| 5964 | return GVA_StrongExternal; |
| 5965 | } |
| 5966 | |
Argyrios Kyrtzidis | 4ac7c0b | 2010-07-29 20:08:05 +0000 | [diff] [blame] | 5967 | bool ASTContext::DeclMustBeEmitted(const Decl *D) { |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5968 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 5969 | if (!VD->isFileVarDecl()) |
| 5970 | return false; |
| 5971 | } else if (!isa<FunctionDecl>(D)) |
| 5972 | return false; |
| 5973 | |
Argyrios Kyrtzidis | ab411c8 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 5974 | // Weak references don't produce any output by themselves. |
| 5975 | if (D->hasAttr<WeakRefAttr>()) |
| 5976 | return false; |
| 5977 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5978 | // Aliases and used decls are required. |
| 5979 | if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>()) |
| 5980 | return true; |
| 5981 | |
| 5982 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 5983 | // Forward declarations aren't required. |
| 5984 | if (!FD->isThisDeclarationADefinition()) |
| 5985 | return false; |
| 5986 | |
| 5987 | // Constructors and destructors are required. |
| 5988 | if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>()) |
| 5989 | return true; |
| 5990 | |
| 5991 | // The key function for a class is required. |
| 5992 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 5993 | const CXXRecordDecl *RD = MD->getParent(); |
| 5994 | if (MD->isOutOfLine() && RD->isDynamicClass()) { |
| 5995 | const CXXMethodDecl *KeyFunc = getKeyFunction(RD); |
| 5996 | if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl()) |
| 5997 | return true; |
| 5998 | } |
| 5999 | } |
| 6000 | |
| 6001 | GVALinkage Linkage = GetGVALinkageForFunction(FD); |
| 6002 | |
| 6003 | // static, static inline, always_inline, and extern inline functions can |
| 6004 | // always be deferred. Normal inline functions can be deferred in C99/C++. |
| 6005 | // Implicit template instantiations can also be deferred in C++. |
| 6006 | if (Linkage == GVA_Internal || Linkage == GVA_C99Inline || |
| 6007 | Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) |
| 6008 | return false; |
| 6009 | return true; |
| 6010 | } |
| 6011 | |
| 6012 | const VarDecl *VD = cast<VarDecl>(D); |
| 6013 | assert(VD->isFileVarDecl() && "Expected file scoped var"); |
| 6014 | |
Argyrios Kyrtzidis | ab411c8 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 6015 | if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly) |
| 6016 | return false; |
| 6017 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6018 | // Structs that have non-trivial constructors or destructors are required. |
| 6019 | |
| 6020 | // FIXME: Handle references. |
| 6021 | if (const RecordType *RT = VD->getType()->getAs<RecordType>()) { |
| 6022 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 6023 | if (RD->hasDefinition() && |
| 6024 | (!RD->hasTrivialConstructor() || !RD->hasTrivialDestructor())) |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6025 | return true; |
| 6026 | } |
| 6027 | } |
| 6028 | |
| 6029 | GVALinkage L = GetGVALinkageForVariable(VD); |
| 6030 | if (L == GVA_Internal || L == GVA_TemplateInstantiation) { |
| 6031 | if (!(VD->getInit() && VD->getInit()->HasSideEffects(*this))) |
| 6032 | return false; |
| 6033 | } |
| 6034 | |
| 6035 | return true; |
| 6036 | } |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 6037 | |
Charles Davis | ee743f9 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 6038 | CallingConv ASTContext::getDefaultMethodCallConv() { |
| 6039 | // Pass through to the C++ ABI object |
| 6040 | return ABI->getDefaultMethodCallConv(); |
| 6041 | } |
| 6042 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6043 | bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const { |
Anders Carlsson | dae0cb5 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 6044 | // Pass through to the C++ ABI object |
| 6045 | return ABI->isNearlyEmpty(RD); |
| 6046 | } |
| 6047 | |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 6048 | MangleContext *ASTContext::createMangleContext() { |
| 6049 | switch (Target.getCXXABI()) { |
| 6050 | case CXXABI_ARM: |
| 6051 | case CXXABI_Itanium: |
| 6052 | return createItaniumMangleContext(*this, getDiagnostics()); |
| 6053 | case CXXABI_Microsoft: |
| 6054 | return createMicrosoftMangleContext(*this, getDiagnostics()); |
| 6055 | } |
| 6056 | assert(0 && "Unsupported ABI"); |
| 6057 | return 0; |
| 6058 | } |
| 6059 | |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 6060 | CXXABI::~CXXABI() {} |