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), |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 208 | UniqueBlockByRefTypeID(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 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 383 | // C99 6.2.5p11. |
| 384 | FloatComplexTy = getComplexType(FloatTy); |
| 385 | DoubleComplexTy = getComplexType(DoubleTy); |
| 386 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 387 | |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 388 | BuiltinVaListType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 389 | |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 390 | // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope(). |
| 391 | ObjCIdTypedefType = QualType(); |
| 392 | ObjCClassTypedefType = QualType(); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 393 | ObjCSelTypedefType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 394 | |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 395 | // Builtin types for 'id', 'Class', and 'SEL'. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 396 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); |
| 397 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 398 | InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 399 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 400 | ObjCConstantStringType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 401 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 402 | // void * type |
| 403 | VoidPtrTy = getPointerType(VoidTy); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 404 | |
| 405 | // nullptr type (C++0x 2.14.7) |
| 406 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 409 | Diagnostic &ASTContext::getDiagnostics() const { |
| 410 | return SourceMgr.getDiagnostics(); |
| 411 | } |
| 412 | |
Douglas Gregor | 6320064 | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 413 | AttrVec& ASTContext::getDeclAttrs(const Decl *D) { |
| 414 | AttrVec *&Result = DeclAttrs[D]; |
| 415 | if (!Result) { |
| 416 | void *Mem = Allocate(sizeof(AttrVec)); |
| 417 | Result = new (Mem) AttrVec; |
| 418 | } |
| 419 | |
| 420 | return *Result; |
| 421 | } |
| 422 | |
| 423 | /// \brief Erase the attributes corresponding to the given declaration. |
| 424 | void ASTContext::eraseDeclAttrs(const Decl *D) { |
| 425 | llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D); |
| 426 | if (Pos != DeclAttrs.end()) { |
| 427 | Pos->second->~AttrVec(); |
| 428 | DeclAttrs.erase(Pos); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 433 | MemberSpecializationInfo * |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 434 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 435 | assert(Var->isStaticDataMember() && "Not a static data member"); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 436 | llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 437 | = InstantiatedFromStaticDataMember.find(Var); |
| 438 | if (Pos == InstantiatedFromStaticDataMember.end()) |
| 439 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 440 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 441 | return Pos->second; |
| 442 | } |
| 443 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 444 | void |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 445 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, |
Argyrios Kyrtzidis | 9421adc | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 446 | TemplateSpecializationKind TSK, |
| 447 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 448 | assert(Inst->isStaticDataMember() && "Not a static data member"); |
| 449 | assert(Tmpl->isStaticDataMember() && "Not a static data member"); |
| 450 | assert(!InstantiatedFromStaticDataMember[Inst] && |
| 451 | "Already noted what static data member was instantiated from"); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 452 | InstantiatedFromStaticDataMember[Inst] |
Argyrios Kyrtzidis | 9421adc | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 453 | = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 454 | } |
| 455 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 456 | NamedDecl * |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 457 | ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 458 | llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 459 | = InstantiatedFromUsingDecl.find(UUD); |
| 460 | if (Pos == InstantiatedFromUsingDecl.end()) |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 461 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 462 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 463 | return Pos->second; |
| 464 | } |
| 465 | |
| 466 | void |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 467 | ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) { |
| 468 | assert((isa<UsingDecl>(Pattern) || |
| 469 | isa<UnresolvedUsingValueDecl>(Pattern) || |
| 470 | isa<UnresolvedUsingTypenameDecl>(Pattern)) && |
| 471 | "pattern decl is not a using decl"); |
| 472 | assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists"); |
| 473 | InstantiatedFromUsingDecl[Inst] = Pattern; |
| 474 | } |
| 475 | |
| 476 | UsingShadowDecl * |
| 477 | ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) { |
| 478 | llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos |
| 479 | = InstantiatedFromUsingShadowDecl.find(Inst); |
| 480 | if (Pos == InstantiatedFromUsingShadowDecl.end()) |
| 481 | return 0; |
| 482 | |
| 483 | return Pos->second; |
| 484 | } |
| 485 | |
| 486 | void |
| 487 | ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, |
| 488 | UsingShadowDecl *Pattern) { |
| 489 | assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists"); |
| 490 | InstantiatedFromUsingShadowDecl[Inst] = Pattern; |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 493 | FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { |
| 494 | llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos |
| 495 | = InstantiatedFromUnnamedFieldDecl.find(Field); |
| 496 | if (Pos == InstantiatedFromUnnamedFieldDecl.end()) |
| 497 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 498 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 499 | return Pos->second; |
| 500 | } |
| 501 | |
| 502 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, |
| 503 | FieldDecl *Tmpl) { |
| 504 | assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed"); |
| 505 | assert(!Tmpl->getDeclName() && "Template field decl is not unnamed"); |
| 506 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] && |
| 507 | "Already noted what unnamed field was instantiated from"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 508 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 509 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; |
| 510 | } |
| 511 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 512 | ASTContext::overridden_cxx_method_iterator |
| 513 | ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const { |
| 514 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 515 | = OverriddenMethods.find(Method); |
| 516 | if (Pos == OverriddenMethods.end()) |
| 517 | return 0; |
| 518 | |
| 519 | return Pos->second.begin(); |
| 520 | } |
| 521 | |
| 522 | ASTContext::overridden_cxx_method_iterator |
| 523 | ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const { |
| 524 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 525 | = OverriddenMethods.find(Method); |
| 526 | if (Pos == OverriddenMethods.end()) |
| 527 | return 0; |
| 528 | |
| 529 | return Pos->second.end(); |
| 530 | } |
| 531 | |
Argyrios Kyrtzidis | c91e9f4 | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 532 | unsigned |
| 533 | ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const { |
| 534 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 535 | = OverriddenMethods.find(Method); |
| 536 | if (Pos == OverriddenMethods.end()) |
| 537 | return 0; |
| 538 | |
| 539 | return Pos->second.size(); |
| 540 | } |
| 541 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 542 | void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method, |
| 543 | const CXXMethodDecl *Overridden) { |
| 544 | OverriddenMethods[Method].push_back(Overridden); |
| 545 | } |
| 546 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 547 | //===----------------------------------------------------------------------===// |
| 548 | // Type Sizing and Analysis |
| 549 | //===----------------------------------------------------------------------===// |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 550 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 551 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 552 | /// scalar floating point type. |
| 553 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 554 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 555 | assert(BT && "Not a floating point type!"); |
| 556 | switch (BT->getKind()) { |
| 557 | default: assert(0 && "Not a floating point type!"); |
| 558 | case BuiltinType::Float: return Target.getFloatFormat(); |
| 559 | case BuiltinType::Double: return Target.getDoubleFormat(); |
| 560 | case BuiltinType::LongDouble: return Target.getLongDoubleFormat(); |
| 561 | } |
| 562 | } |
| 563 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 564 | /// getDeclAlign - Return a conservative estimate of the alignment of the |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 565 | /// specified decl. Note that bitfields do not have a valid alignment, so |
| 566 | /// this method will assert on them. |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 567 | /// If @p RefAsPointee, references are treated like their underlying type |
| 568 | /// (for alignof), else they're treated like pointers (for CodeGen). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 569 | CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) const { |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 570 | unsigned Align = Target.getCharWidth(); |
| 571 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 572 | bool UseAlignAttrOnly = false; |
| 573 | if (unsigned AlignFromAttr = D->getMaxAlignment()) { |
| 574 | Align = AlignFromAttr; |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 575 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 576 | // __attribute__((aligned)) can increase or decrease alignment |
| 577 | // *except* on a struct or struct member, where it only increases |
| 578 | // alignment unless 'packed' is also specified. |
| 579 | // |
| 580 | // It is an error for [[align]] to decrease alignment, so we can |
| 581 | // ignore that possibility; Sema should diagnose it. |
| 582 | if (isa<FieldDecl>(D)) { |
| 583 | UseAlignAttrOnly = D->hasAttr<PackedAttr>() || |
| 584 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
| 585 | } else { |
| 586 | UseAlignAttrOnly = true; |
| 587 | } |
| 588 | } |
| 589 | |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 590 | // If we're using the align attribute only, just ignore everything |
| 591 | // else about the declaration and its type. |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 592 | if (UseAlignAttrOnly) { |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 593 | // do nothing |
| 594 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 595 | } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 596 | QualType T = VD->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 597 | if (const ReferenceType* RT = T->getAs<ReferenceType>()) { |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 598 | if (RefAsPointee) |
| 599 | T = RT->getPointeeType(); |
| 600 | else |
| 601 | T = getPointerType(RT->getPointeeType()); |
| 602 | } |
| 603 | if (!T->isIncompleteType() && !T->isFunctionType()) { |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 604 | // Adjust alignments of declarations with array type by the |
| 605 | // large-array alignment on the target. |
Rafael Espindola | 6deecb0 | 2010-06-04 23:15:27 +0000 | [diff] [blame] | 606 | unsigned MinWidth = Target.getLargeArrayMinWidth(); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 607 | const ArrayType *arrayType; |
| 608 | if (MinWidth && (arrayType = getAsArrayType(T))) { |
| 609 | if (isa<VariableArrayType>(arrayType)) |
| 610 | Align = std::max(Align, Target.getLargeArrayAlign()); |
| 611 | else if (isa<ConstantArrayType>(arrayType) && |
| 612 | MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType))) |
| 613 | Align = std::max(Align, Target.getLargeArrayAlign()); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 614 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 615 | // Walk through any array types while we're at it. |
| 616 | T = getBaseElementType(arrayType); |
| 617 | } |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 618 | Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); |
| 619 | } |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 620 | |
| 621 | // Fields can be subject to extra alignment constraints, like if |
| 622 | // the field is packed, the struct is packed, or the struct has a |
| 623 | // a max-field-alignment constraint (#pragma pack). So calculate |
| 624 | // the actual alignment of the field within the struct, and then |
| 625 | // (as we're expected to) constrain that by the alignment of the type. |
| 626 | if (const FieldDecl *field = dyn_cast<FieldDecl>(VD)) { |
| 627 | // So calculate the alignment of the field. |
| 628 | const ASTRecordLayout &layout = getASTRecordLayout(field->getParent()); |
| 629 | |
| 630 | // Start with the record's overall alignment. |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 631 | unsigned fieldAlign = toBits(layout.getAlignment()); |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 632 | |
| 633 | // Use the GCD of that and the offset within the record. |
| 634 | uint64_t offset = layout.getFieldOffset(field->getFieldIndex()); |
| 635 | if (offset > 0) { |
| 636 | // Alignment is always a power of 2, so the GCD will be a power of 2, |
| 637 | // which means we get to do this crazy thing instead of Euclid's. |
| 638 | uint64_t lowBitOfOffset = offset & (~offset + 1); |
| 639 | if (lowBitOfOffset < fieldAlign) |
| 640 | fieldAlign = static_cast<unsigned>(lowBitOfOffset); |
| 641 | } |
| 642 | |
| 643 | Align = std::min(Align, fieldAlign); |
Charles Davis | 05f6247 | 2010-02-23 04:52:00 +0000 | [diff] [blame] | 644 | } |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 645 | } |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 646 | |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 647 | return toCharUnitsFromBits(Align); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 648 | } |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 649 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 650 | std::pair<CharUnits, CharUnits> |
Ken Dyck | bee5a79 | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 651 | ASTContext::getTypeInfoInChars(const Type *T) const { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 652 | std::pair<uint64_t, unsigned> Info = getTypeInfo(T); |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 653 | return std::make_pair(toCharUnitsFromBits(Info.first), |
| 654 | toCharUnitsFromBits(Info.second)); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | std::pair<CharUnits, CharUnits> |
Ken Dyck | bee5a79 | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 658 | ASTContext::getTypeInfoInChars(QualType T) const { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 659 | return getTypeInfoInChars(T.getTypePtr()); |
| 660 | } |
| 661 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 662 | /// getTypeSize - Return the size of the specified type, in bits. This method |
| 663 | /// does not work on incomplete types. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 664 | /// |
| 665 | /// FIXME: Pointers into different addr spaces could have different sizes and |
| 666 | /// alignment requirements: getPointerInfo should take an AddrSpace, this |
| 667 | /// should take a QualType, &c. |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 668 | std::pair<uint64_t, unsigned> |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 669 | ASTContext::getTypeInfo(const Type *T) const { |
Mike Stump | 5e30100 | 2009-02-27 18:32:39 +0000 | [diff] [blame] | 670 | uint64_t Width=0; |
| 671 | unsigned Align=8; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 672 | switch (T->getTypeClass()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 673 | #define TYPE(Class, Base) |
| 674 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 675 | #define NON_CANONICAL_TYPE(Class, Base) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 676 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 677 | #include "clang/AST/TypeNodes.def" |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 678 | assert(false && "Should not see dependent types"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 679 | break; |
| 680 | |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 681 | case Type::FunctionNoProto: |
| 682 | case Type::FunctionProto: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 683 | // GCC extension: alignof(function) = 32 bits |
| 684 | Width = 0; |
| 685 | Align = 32; |
| 686 | break; |
| 687 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 688 | case Type::IncompleteArray: |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 689 | case Type::VariableArray: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 690 | Width = 0; |
| 691 | Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); |
| 692 | break; |
| 693 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 694 | case Type::ConstantArray: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 695 | const ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 696 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 697 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 698 | Width = EltInfo.first*CAT->getSize().getZExtValue(); |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 699 | Align = EltInfo.second; |
| 700 | break; |
Christopher Lamb | 5c09a02 | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 701 | } |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 702 | case Type::ExtVector: |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 703 | case Type::Vector: { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 704 | const VectorType *VT = cast<VectorType>(T); |
| 705 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType()); |
| 706 | Width = EltInfo.first*VT->getNumElements(); |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 707 | Align = Width; |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 708 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 709 | // This happens for non-power-of-2 length vectors. |
Dan Gohman | 8eefcd3 | 2010-04-21 23:32:43 +0000 | [diff] [blame] | 710 | if (Align & (Align-1)) { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 711 | Align = llvm::NextPowerOf2(Align); |
| 712 | Width = llvm::RoundUpToAlignment(Width, Align); |
| 713 | } |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 714 | break; |
| 715 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 716 | |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 717 | case Type::Builtin: |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 718 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 719 | default: assert(0 && "Unknown builtin type!"); |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 720 | case BuiltinType::Void: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 721 | // GCC extension: alignof(void) = 8 bits. |
| 722 | Width = 0; |
| 723 | Align = 8; |
| 724 | break; |
| 725 | |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 726 | case BuiltinType::Bool: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 727 | Width = Target.getBoolWidth(); |
| 728 | Align = Target.getBoolAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 729 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 730 | case BuiltinType::Char_S: |
| 731 | case BuiltinType::Char_U: |
| 732 | case BuiltinType::UChar: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 733 | case BuiltinType::SChar: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 734 | Width = Target.getCharWidth(); |
| 735 | Align = Target.getCharAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 736 | break; |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 737 | case BuiltinType::WChar_S: |
| 738 | case BuiltinType::WChar_U: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 739 | Width = Target.getWCharWidth(); |
| 740 | Align = Target.getWCharAlign(); |
| 741 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 742 | case BuiltinType::Char16: |
| 743 | Width = Target.getChar16Width(); |
| 744 | Align = Target.getChar16Align(); |
| 745 | break; |
| 746 | case BuiltinType::Char32: |
| 747 | Width = Target.getChar32Width(); |
| 748 | Align = Target.getChar32Align(); |
| 749 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 750 | case BuiltinType::UShort: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 751 | case BuiltinType::Short: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 752 | Width = Target.getShortWidth(); |
| 753 | Align = Target.getShortAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 754 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 755 | case BuiltinType::UInt: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 756 | case BuiltinType::Int: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 757 | Width = Target.getIntWidth(); |
| 758 | Align = Target.getIntAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 759 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 760 | case BuiltinType::ULong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 761 | case BuiltinType::Long: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 762 | Width = Target.getLongWidth(); |
| 763 | Align = Target.getLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 764 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 765 | case BuiltinType::ULongLong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 766 | case BuiltinType::LongLong: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 767 | Width = Target.getLongLongWidth(); |
| 768 | Align = Target.getLongLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 769 | break; |
Chris Lattner | ec16cb9 | 2009-04-30 02:55:13 +0000 | [diff] [blame] | 770 | case BuiltinType::Int128: |
| 771 | case BuiltinType::UInt128: |
| 772 | Width = 128; |
| 773 | Align = 128; // int128_t is 128-bit aligned on all targets. |
| 774 | break; |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 775 | case BuiltinType::Float: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 776 | Width = Target.getFloatWidth(); |
| 777 | Align = Target.getFloatAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 778 | break; |
| 779 | case BuiltinType::Double: |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 780 | Width = Target.getDoubleWidth(); |
| 781 | Align = Target.getDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 782 | break; |
| 783 | case BuiltinType::LongDouble: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 784 | Width = Target.getLongDoubleWidth(); |
| 785 | Align = Target.getLongDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 786 | break; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 787 | case BuiltinType::NullPtr: |
| 788 | Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) |
| 789 | Align = Target.getPointerAlign(0); // == sizeof(void*) |
Sebastian Redl | 1590d9c | 2009-05-27 19:34:06 +0000 | [diff] [blame] | 790 | break; |
Fariborz Jahanian | e04f5fc | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 791 | case BuiltinType::ObjCId: |
| 792 | case BuiltinType::ObjCClass: |
| 793 | case BuiltinType::ObjCSel: |
| 794 | Width = Target.getPointerWidth(0); |
| 795 | Align = Target.getPointerAlign(0); |
| 796 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 797 | } |
Chris Lattner | bfef6d7 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 798 | break; |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 799 | case Type::ObjCObjectPointer: |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 800 | Width = Target.getPointerWidth(0); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 801 | Align = Target.getPointerAlign(0); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 802 | break; |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 803 | case Type::BlockPointer: { |
| 804 | unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace(); |
| 805 | Width = Target.getPointerWidth(AS); |
| 806 | Align = Target.getPointerAlign(AS); |
| 807 | break; |
| 808 | } |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 809 | case Type::LValueReference: |
| 810 | case Type::RValueReference: { |
| 811 | // alignof and sizeof should never enter this code path here, so we go |
| 812 | // the pointer route. |
| 813 | unsigned AS = cast<ReferenceType>(T)->getPointeeType().getAddressSpace(); |
| 814 | Width = Target.getPointerWidth(AS); |
| 815 | Align = Target.getPointerAlign(AS); |
| 816 | break; |
| 817 | } |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 818 | case Type::Pointer: { |
| 819 | unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace(); |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 820 | Width = Target.getPointerWidth(AS); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 821 | Align = Target.getPointerAlign(AS); |
| 822 | break; |
| 823 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 824 | case Type::MemberPointer: { |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 825 | const MemberPointerType *MPT = cast<MemberPointerType>(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 826 | std::pair<uint64_t, unsigned> PtrDiffInfo = |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 827 | getTypeInfo(getPointerDiffType()); |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 828 | Width = PtrDiffInfo.first * ABI->getMemberPointerSize(MPT); |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 829 | Align = PtrDiffInfo.second; |
| 830 | break; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 831 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 832 | case Type::Complex: { |
| 833 | // Complex types have the same alignment as their elements, but twice the |
| 834 | // size. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 835 | std::pair<uint64_t, unsigned> EltInfo = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 836 | getTypeInfo(cast<ComplexType>(T)->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 837 | Width = EltInfo.first*2; |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 838 | Align = EltInfo.second; |
| 839 | break; |
| 840 | } |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 841 | case Type::ObjCObject: |
| 842 | return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr()); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 843 | case Type::ObjCInterface: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 844 | const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 845 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 846 | Width = toBits(Layout.getSize()); |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 847 | Align = toBits(Layout.getAlignment()); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 848 | break; |
| 849 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 850 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 851 | case Type::Enum: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 852 | const TagType *TT = cast<TagType>(T); |
| 853 | |
| 854 | if (TT->getDecl()->isInvalidDecl()) { |
Chris Lattner | 8389eab | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 855 | Width = 1; |
| 856 | Align = 1; |
| 857 | break; |
| 858 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 859 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 860 | if (const EnumType *ET = dyn_cast<EnumType>(TT)) |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 861 | return getTypeInfo(ET->getDecl()->getIntegerType()); |
| 862 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 863 | const RecordType *RT = cast<RecordType>(TT); |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 864 | const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 865 | Width = toBits(Layout.getSize()); |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 866 | Align = toBits(Layout.getAlignment()); |
Chris Lattner | dc0d73e | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 867 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 868 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 869 | |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 870 | case Type::SubstTemplateTypeParm: |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 871 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> |
| 872 | getReplacementType().getTypePtr()); |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 873 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 874 | case Type::Auto: { |
| 875 | const AutoType *A = cast<AutoType>(T); |
| 876 | assert(A->isDeduced() && "Cannot request the size of a dependent type"); |
Matt Beaumont-Gay | dc856af | 2011-02-22 20:00:16 +0000 | [diff] [blame] | 877 | return getTypeInfo(A->getDeducedType().getTypePtr()); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 880 | case Type::Paren: |
| 881 | return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr()); |
| 882 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 883 | case Type::Typedef: { |
| 884 | const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl(); |
Douglas Gregor | df1367a | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 885 | std::pair<uint64_t, unsigned> Info |
| 886 | = getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); |
Chris Lattner | c1de52d | 2011-02-19 22:55:41 +0000 | [diff] [blame] | 887 | // If the typedef has an aligned attribute on it, it overrides any computed |
| 888 | // alignment we have. This violates the GCC documentation (which says that |
| 889 | // attribute(aligned) can only round up) but matches its implementation. |
| 890 | if (unsigned AttrAlign = Typedef->getMaxAlignment()) |
| 891 | Align = AttrAlign; |
| 892 | else |
| 893 | Align = Info.second; |
Douglas Gregor | df1367a | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 894 | Width = Info.first; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 895 | break; |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 896 | } |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 897 | |
| 898 | case Type::TypeOfExpr: |
| 899 | return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType() |
| 900 | .getTypePtr()); |
| 901 | |
| 902 | case Type::TypeOf: |
| 903 | return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr()); |
| 904 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 905 | case Type::Decltype: |
| 906 | return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType() |
| 907 | .getTypePtr()); |
| 908 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 909 | case Type::Elaborated: |
| 910 | return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 911 | |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 912 | case Type::Attributed: |
| 913 | return getTypeInfo( |
| 914 | cast<AttributedType>(T)->getEquivalentType().getTypePtr()); |
| 915 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 916 | case Type::TemplateSpecialization: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 917 | assert(getCanonicalType(T) != T && |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 918 | "Cannot request the size of a dependent type"); |
| 919 | // FIXME: this is likely to be wrong once we support template |
| 920 | // aliases, since a template alias could refer to a typedef that |
| 921 | // has an __aligned__ attribute on it. |
| 922 | return getTypeInfo(getCanonicalType(T)); |
| 923 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 924 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 925 | assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2"); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 926 | return std::make_pair(Width, Align); |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 927 | } |
| 928 | |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 929 | /// toCharUnitsFromBits - Convert a size in bits to a size in characters. |
| 930 | CharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const { |
| 931 | return CharUnits::fromQuantity(BitSize / getCharWidth()); |
| 932 | } |
| 933 | |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 934 | /// toBits - Convert a size in characters to a size in characters. |
| 935 | int64_t ASTContext::toBits(CharUnits CharSize) const { |
| 936 | return CharSize.getQuantity() * getCharWidth(); |
| 937 | } |
| 938 | |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 939 | /// getTypeSizeInChars - Return the size of the specified type, in characters. |
| 940 | /// This method does not work on incomplete types. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 941 | CharUnits ASTContext::getTypeSizeInChars(QualType T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 942 | return toCharUnitsFromBits(getTypeSize(T)); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 943 | } |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 944 | CharUnits ASTContext::getTypeSizeInChars(const Type *T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 945 | return toCharUnitsFromBits(getTypeSize(T)); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 946 | } |
| 947 | |
Ken Dyck | 16e20cc | 2010-01-26 17:25:18 +0000 | [diff] [blame] | 948 | /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 949 | /// characters. This method does not work on incomplete types. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 950 | CharUnits ASTContext::getTypeAlignInChars(QualType T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 951 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 952 | } |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 953 | CharUnits ASTContext::getTypeAlignInChars(const Type *T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 954 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 955 | } |
| 956 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 957 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 958 | /// type for the current target in bits. This can be different than the ABI |
| 959 | /// alignment in cases where it is beneficial for performance to overalign |
| 960 | /// a data type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 961 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) const { |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 962 | unsigned ABIAlign = getTypeAlign(T); |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 963 | |
| 964 | // Double and long long should be naturally aligned if possible. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 965 | if (const ComplexType* CT = T->getAs<ComplexType>()) |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 966 | T = CT->getElementType().getTypePtr(); |
| 967 | if (T->isSpecificBuiltinType(BuiltinType::Double) || |
| 968 | T->isSpecificBuiltinType(BuiltinType::LongLong)) |
| 969 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); |
| 970 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 971 | return ABIAlign; |
| 972 | } |
| 973 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 974 | /// ShallowCollectObjCIvars - |
| 975 | /// Collect all ivars, including those synthesized, in the current class. |
| 976 | /// |
| 977 | void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 978 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) const { |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 979 | // FIXME. This need be removed but there are two many places which |
| 980 | // assume const-ness of ObjCInterfaceDecl |
| 981 | ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI); |
| 982 | for (ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv; |
| 983 | Iv= Iv->getNextIvar()) |
| 984 | Ivars.push_back(Iv); |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 985 | } |
| 986 | |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 987 | /// DeepCollectObjCIvars - |
| 988 | /// This routine first collects all declared, but not synthesized, ivars in |
| 989 | /// super class and then collects all ivars, including those synthesized for |
| 990 | /// current class. This routine is used for implementation of current class |
| 991 | /// when all ivars, declared and synthesized are known. |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 992 | /// |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 993 | void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 994 | bool leafClass, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 995 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) const { |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 996 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
| 997 | DeepCollectObjCIvars(SuperClass, false, Ivars); |
| 998 | if (!leafClass) { |
| 999 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 1000 | E = OI->ivar_end(); I != E; ++I) |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 1001 | Ivars.push_back(*I); |
| 1002 | } |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1003 | else |
| 1004 | ShallowCollectObjCIvars(OI, Ivars); |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1007 | /// CollectInheritedProtocols - Collect all protocols in current class and |
| 1008 | /// those inherited by it. |
| 1009 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 1010 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1011 | if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1012 | // We can use protocol_iterator here instead of |
| 1013 | // all_referenced_protocol_iterator since we are walking all categories. |
| 1014 | for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(), |
| 1015 | PE = OI->all_referenced_protocol_end(); P != PE; ++P) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1016 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 1017 | Protocols.insert(Proto); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1018 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 1019 | PE = Proto->protocol_end(); P != PE; ++P) { |
| 1020 | Protocols.insert(*P); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1021 | CollectInheritedProtocols(*P, Protocols); |
| 1022 | } |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 1023 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1024 | |
| 1025 | // Categories of this Interface. |
| 1026 | for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList(); |
| 1027 | CDeclChain; CDeclChain = CDeclChain->getNextClassCategory()) |
| 1028 | CollectInheritedProtocols(CDeclChain, Protocols); |
| 1029 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) |
| 1030 | while (SD) { |
| 1031 | CollectInheritedProtocols(SD, Protocols); |
| 1032 | SD = SD->getSuperClass(); |
| 1033 | } |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1034 | } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1035 | for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(), |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1036 | PE = OC->protocol_end(); P != PE; ++P) { |
| 1037 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 1038 | Protocols.insert(Proto); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1039 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 1040 | PE = Proto->protocol_end(); P != PE; ++P) |
| 1041 | CollectInheritedProtocols(*P, Protocols); |
| 1042 | } |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1043 | } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1044 | for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(), |
| 1045 | PE = OP->protocol_end(); P != PE; ++P) { |
| 1046 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 1047 | Protocols.insert(Proto); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1048 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 1049 | PE = Proto->protocol_end(); P != PE; ++P) |
| 1050 | CollectInheritedProtocols(*P, Protocols); |
| 1051 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1052 | } |
| 1053 | } |
| 1054 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1055 | unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const { |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1056 | unsigned count = 0; |
| 1057 | // Count ivars declared in class extension. |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 1058 | for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl; |
| 1059 | CDecl = CDecl->getNextClassExtension()) |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1060 | count += CDecl->ivar_size(); |
| 1061 | |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1062 | // Count ivar defined in this class's implementation. This |
| 1063 | // includes synthesized ivars. |
| 1064 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1065 | count += ImplDecl->ivar_size(); |
| 1066 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 1067 | return count; |
| 1068 | } |
| 1069 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1070 | /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. |
| 1071 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
| 1072 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1073 | I = ObjCImpls.find(D); |
| 1074 | if (I != ObjCImpls.end()) |
| 1075 | return cast<ObjCImplementationDecl>(I->second); |
| 1076 | return 0; |
| 1077 | } |
| 1078 | /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. |
| 1079 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
| 1080 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1081 | I = ObjCImpls.find(D); |
| 1082 | if (I != ObjCImpls.end()) |
| 1083 | return cast<ObjCCategoryImplDecl>(I->second); |
| 1084 | return 0; |
| 1085 | } |
| 1086 | |
| 1087 | /// \brief Set the implementation of ObjCInterfaceDecl. |
| 1088 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
| 1089 | ObjCImplementationDecl *ImplD) { |
| 1090 | assert(IFaceD && ImplD && "Passed null params"); |
| 1091 | ObjCImpls[IFaceD] = ImplD; |
| 1092 | } |
| 1093 | /// \brief Set the implementation of ObjCCategoryDecl. |
| 1094 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
| 1095 | ObjCCategoryImplDecl *ImplD) { |
| 1096 | assert(CatD && ImplD && "Passed null params"); |
| 1097 | ObjCImpls[CatD] = ImplD; |
| 1098 | } |
| 1099 | |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1100 | /// \brief Get the copy initialization expression of VarDecl,or NULL if |
| 1101 | /// none exists. |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1102 | Expr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) { |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1103 | assert(VD && "Passed null params"); |
| 1104 | assert(VD->hasAttr<BlocksAttr>() && |
| 1105 | "getBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1106 | llvm::DenseMap<const VarDecl*, Expr*>::iterator |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1107 | I = BlockVarCopyInits.find(VD); |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1108 | return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0; |
| 1109 | } |
| 1110 | |
| 1111 | /// \brief Set the copy inialization expression of a block var decl. |
| 1112 | void ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) { |
| 1113 | assert(VD && Init && "Passed null params"); |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1114 | assert(VD->hasAttr<BlocksAttr>() && |
| 1115 | "setBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1116 | BlockVarCopyInits[VD] = Init; |
| 1117 | } |
| 1118 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1119 | /// \brief Allocate an uninitialized TypeSourceInfo. |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1120 | /// |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1121 | /// The caller should initialize the memory held by TypeSourceInfo using |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1122 | /// the TypeLoc wrappers. |
| 1123 | /// |
| 1124 | /// \param T the type that will be the basis for type source info. This type |
| 1125 | /// should refer to how the declarator was written in source code, not to |
| 1126 | /// what type semantic analysis resolved the declarator to. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1127 | TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1128 | unsigned DataSize) const { |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1129 | if (!DataSize) |
| 1130 | DataSize = TypeLoc::getFullDataSizeForType(T); |
| 1131 | else |
| 1132 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1133 | "incorrect data size provided to CreateTypeSourceInfo!"); |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1134 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1135 | TypeSourceInfo *TInfo = |
| 1136 | (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8); |
| 1137 | new (TInfo) TypeSourceInfo(T); |
| 1138 | return TInfo; |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1141 | TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1142 | SourceLocation L) const { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1143 | TypeSourceInfo *DI = CreateTypeSourceInfo(T); |
Douglas Gregor | c21c7e9 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 1144 | DI->getTypeLoc().initialize(const_cast<ASTContext &>(*this), L); |
John McCall | a4eb74d | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 1145 | return DI; |
| 1146 | } |
| 1147 | |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1148 | const ASTRecordLayout & |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1149 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const { |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1150 | return getObjCLayout(D, 0); |
| 1151 | } |
| 1152 | |
| 1153 | const ASTRecordLayout & |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1154 | ASTContext::getASTObjCImplementationLayout( |
| 1155 | const ObjCImplementationDecl *D) const { |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1156 | return getObjCLayout(D->getClassInterface(), D); |
| 1157 | } |
| 1158 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1159 | //===----------------------------------------------------------------------===// |
| 1160 | // Type creation/memoization methods |
| 1161 | //===----------------------------------------------------------------------===// |
| 1162 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1163 | QualType |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1164 | ASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const { |
| 1165 | unsigned fastQuals = quals.getFastQualifiers(); |
| 1166 | quals.removeFastQualifiers(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1167 | |
| 1168 | // Check if we've already instantiated this type. |
| 1169 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1170 | ExtQuals::Profile(ID, baseType, quals); |
| 1171 | void *insertPos = 0; |
| 1172 | if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, insertPos)) { |
| 1173 | assert(eq->getQualifiers() == quals); |
| 1174 | return QualType(eq, fastQuals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1177 | // If the base type is not canonical, make the appropriate canonical type. |
| 1178 | QualType canon; |
| 1179 | if (!baseType->isCanonicalUnqualified()) { |
| 1180 | SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split(); |
| 1181 | canonSplit.second.addConsistentQualifiers(quals); |
| 1182 | canon = getExtQualType(canonSplit.first, canonSplit.second); |
| 1183 | |
| 1184 | // Re-find the insert position. |
| 1185 | (void) ExtQualNodes.FindNodeOrInsertPos(ID, insertPos); |
| 1186 | } |
| 1187 | |
| 1188 | ExtQuals *eq = new (*this, TypeAlignment) ExtQuals(baseType, canon, quals); |
| 1189 | ExtQualNodes.InsertNode(eq, insertPos); |
| 1190 | return QualType(eq, fastQuals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1193 | QualType |
| 1194 | ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) const { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1195 | QualType CanT = getCanonicalType(T); |
| 1196 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1197 | return T; |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1198 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1199 | // If we are composing extended qualifiers together, merge together |
| 1200 | // into one ExtQuals node. |
| 1201 | QualifierCollector Quals; |
| 1202 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1203 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1204 | // If this type already has an address space specified, it cannot get |
| 1205 | // another one. |
| 1206 | assert(!Quals.hasAddressSpace() && |
| 1207 | "Type cannot be in multiple addr spaces!"); |
| 1208 | Quals.addAddressSpace(AddressSpace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1209 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1210 | return getExtQualType(TypeNode, Quals); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1213 | QualType ASTContext::getObjCGCQualType(QualType T, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1214 | Qualifiers::GC GCAttr) const { |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1215 | QualType CanT = getCanonicalType(T); |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1216 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1217 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1218 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1219 | if (const PointerType *ptr = T->getAs<PointerType>()) { |
| 1220 | QualType Pointee = ptr->getPointeeType(); |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 1221 | if (Pointee->isAnyPointerType()) { |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1222 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 1223 | return getPointerType(ResultType); |
| 1224 | } |
| 1225 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1226 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1227 | // If we are composing extended qualifiers together, merge together |
| 1228 | // into one ExtQuals node. |
| 1229 | QualifierCollector Quals; |
| 1230 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1231 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1232 | // If this type already has an ObjCGC specified, it cannot get |
| 1233 | // another one. |
| 1234 | assert(!Quals.hasObjCGCAttr() && |
| 1235 | "Type cannot have multiple ObjCGCs!"); |
| 1236 | Quals.addObjCGCAttr(GCAttr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1237 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1238 | return getExtQualType(TypeNode, Quals); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1239 | } |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1240 | |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 1241 | const FunctionType *ASTContext::adjustFunctionType(const FunctionType *T, |
| 1242 | FunctionType::ExtInfo Info) { |
| 1243 | if (T->getExtInfo() == Info) |
| 1244 | return T; |
| 1245 | |
| 1246 | QualType Result; |
| 1247 | if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) { |
| 1248 | Result = getFunctionNoProtoType(FNPT->getResultType(), Info); |
| 1249 | } else { |
| 1250 | const FunctionProtoType *FPT = cast<FunctionProtoType>(T); |
| 1251 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 1252 | EPI.ExtInfo = Info; |
| 1253 | Result = getFunctionType(FPT->getResultType(), FPT->arg_type_begin(), |
| 1254 | FPT->getNumArgs(), EPI); |
| 1255 | } |
| 1256 | |
| 1257 | return cast<FunctionType>(Result.getTypePtr()); |
| 1258 | } |
| 1259 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1260 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 1261 | /// number with the specified element type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1262 | QualType ASTContext::getComplexType(QualType T) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1263 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1264 | // structure. |
| 1265 | llvm::FoldingSetNodeID ID; |
| 1266 | ComplexType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1267 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1268 | void *InsertPos = 0; |
| 1269 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1270 | return QualType(CT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1271 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1272 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1273 | // so fill in the canonical type field. |
| 1274 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1275 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1276 | Canonical = getComplexType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1277 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1278 | // Get the new insert position for the node we care about. |
| 1279 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1280 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1281 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1282 | ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1283 | Types.push_back(New); |
| 1284 | ComplexTypes.InsertNode(New, InsertPos); |
| 1285 | return QualType(New, 0); |
| 1286 | } |
| 1287 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1288 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 1289 | /// the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1290 | QualType ASTContext::getPointerType(QualType T) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1291 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1292 | // structure. |
| 1293 | llvm::FoldingSetNodeID ID; |
| 1294 | PointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1295 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1296 | void *InsertPos = 0; |
| 1297 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1298 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1299 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1300 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1301 | // so fill in the canonical type field. |
| 1302 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1303 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1304 | Canonical = getPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1305 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1306 | // Get the new insert position for the node we care about. |
| 1307 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1308 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1309 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1310 | PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1311 | Types.push_back(New); |
| 1312 | PointerTypes.InsertNode(New, InsertPos); |
| 1313 | return QualType(New, 0); |
| 1314 | } |
| 1315 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1316 | /// getBlockPointerType - Return the uniqued reference to the type for |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1317 | /// a pointer to the specified block. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1318 | QualType ASTContext::getBlockPointerType(QualType T) const { |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1319 | assert(T->isFunctionType() && "block of function types only"); |
| 1320 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1321 | // structure. |
| 1322 | llvm::FoldingSetNodeID ID; |
| 1323 | BlockPointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1324 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1325 | void *InsertPos = 0; |
| 1326 | if (BlockPointerType *PT = |
| 1327 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1328 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1329 | |
| 1330 | // If the block pointee type isn't canonical, this won't be a canonical |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1331 | // type either so fill in the canonical type field. |
| 1332 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1333 | if (!T.isCanonical()) { |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1334 | Canonical = getBlockPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1335 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1336 | // Get the new insert position for the node we care about. |
| 1337 | BlockPointerType *NewIP = |
| 1338 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1339 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1340 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1341 | BlockPointerType *New |
| 1342 | = new (*this, TypeAlignment) BlockPointerType(T, Canonical); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1343 | Types.push_back(New); |
| 1344 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 1345 | return QualType(New, 0); |
| 1346 | } |
| 1347 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1348 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 1349 | /// lvalue reference to the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1350 | QualType |
| 1351 | ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1352 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1353 | // structure. |
| 1354 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1355 | ReferenceType::Profile(ID, T, SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1356 | |
| 1357 | void *InsertPos = 0; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1358 | if (LValueReferenceType *RT = |
| 1359 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1360 | return QualType(RT, 0); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1361 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1362 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1363 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1364 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1365 | // either, so fill in the canonical type field. |
| 1366 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1367 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
| 1368 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1369 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1370 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1371 | // Get the new insert position for the node we care about. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1372 | LValueReferenceType *NewIP = |
| 1373 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1374 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1377 | LValueReferenceType *New |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1378 | = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, |
| 1379 | SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1380 | Types.push_back(New); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1381 | LValueReferenceTypes.InsertNode(New, InsertPos); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1382 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1383 | return QualType(New, 0); |
| 1384 | } |
| 1385 | |
| 1386 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 1387 | /// rvalue reference to the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1388 | QualType ASTContext::getRValueReferenceType(QualType T) const { |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1389 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1390 | // structure. |
| 1391 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1392 | ReferenceType::Profile(ID, T, false); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1393 | |
| 1394 | void *InsertPos = 0; |
| 1395 | if (RValueReferenceType *RT = |
| 1396 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1397 | return QualType(RT, 0); |
| 1398 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1399 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1400 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1401 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1402 | // either, so fill in the canonical type field. |
| 1403 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1404 | if (InnerRef || !T.isCanonical()) { |
| 1405 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1406 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1407 | |
| 1408 | // Get the new insert position for the node we care about. |
| 1409 | RValueReferenceType *NewIP = |
| 1410 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1411 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1412 | } |
| 1413 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1414 | RValueReferenceType *New |
| 1415 | = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1416 | Types.push_back(New); |
| 1417 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1418 | return QualType(New, 0); |
| 1419 | } |
| 1420 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1421 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 1422 | /// member pointer to the specified type, in the specified class. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1423 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1424 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1425 | // structure. |
| 1426 | llvm::FoldingSetNodeID ID; |
| 1427 | MemberPointerType::Profile(ID, T, Cls); |
| 1428 | |
| 1429 | void *InsertPos = 0; |
| 1430 | if (MemberPointerType *PT = |
| 1431 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1432 | return QualType(PT, 0); |
| 1433 | |
| 1434 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 1435 | // type either, so fill in the canonical type field. |
| 1436 | QualType Canonical; |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1437 | if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1438 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 1439 | |
| 1440 | // Get the new insert position for the node we care about. |
| 1441 | MemberPointerType *NewIP = |
| 1442 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1443 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1444 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1445 | MemberPointerType *New |
| 1446 | = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1447 | Types.push_back(New); |
| 1448 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 1449 | return QualType(New, 0); |
| 1450 | } |
| 1451 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1452 | /// getConstantArrayType - Return the unique reference to the type for an |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1453 | /// array of the specified element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1454 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1455 | const llvm::APInt &ArySizeIn, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1456 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1457 | unsigned IndexTypeQuals) const { |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1458 | assert((EltTy->isDependentType() || |
| 1459 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && |
Eli Friedman | 587cbdf | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1460 | "Constant array of VLAs is illegal!"); |
| 1461 | |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1462 | // Convert the array size into a canonical width matching the pointer size for |
| 1463 | // the target. |
| 1464 | llvm::APInt ArySize(ArySizeIn); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1465 | ArySize = |
| 1466 | ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1467 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1468 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1469 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, IndexTypeQuals); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1470 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1471 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1472 | if (ConstantArrayType *ATP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1473 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1474 | return QualType(ATP, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1475 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1476 | // If the element type isn't canonical or has qualifiers, this won't |
| 1477 | // be a canonical type either, so fill in the canonical type field. |
| 1478 | QualType Canon; |
| 1479 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 1480 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
| 1481 | Canon = getConstantArrayType(QualType(canonSplit.first, 0), ArySize, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1482 | ASM, IndexTypeQuals); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1483 | Canon = getQualifiedType(Canon, canonSplit.second); |
| 1484 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1485 | // Get the new insert position for the node we care about. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1486 | ConstantArrayType *NewIP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1487 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1488 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1489 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1490 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1491 | ConstantArrayType *New = new(*this,TypeAlignment) |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1492 | ConstantArrayType(EltTy, Canon, ArySize, ASM, IndexTypeQuals); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1493 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1494 | Types.push_back(New); |
| 1495 | return QualType(New, 0); |
| 1496 | } |
| 1497 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1498 | /// getVariableArrayDecayedType - Turns the given type, which may be |
| 1499 | /// variably-modified, into the corresponding type with all the known |
| 1500 | /// sizes replaced with [*]. |
| 1501 | QualType ASTContext::getVariableArrayDecayedType(QualType type) const { |
| 1502 | // Vastly most common case. |
| 1503 | if (!type->isVariablyModifiedType()) return type; |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1504 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1505 | QualType result; |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1506 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1507 | SplitQualType split = type.getSplitDesugaredType(); |
| 1508 | const Type *ty = split.first; |
| 1509 | switch (ty->getTypeClass()) { |
| 1510 | #define TYPE(Class, Base) |
| 1511 | #define ABSTRACT_TYPE(Class, Base) |
| 1512 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 1513 | #include "clang/AST/TypeNodes.def" |
| 1514 | llvm_unreachable("didn't desugar past all non-canonical types?"); |
| 1515 | |
| 1516 | // These types should never be variably-modified. |
| 1517 | case Type::Builtin: |
| 1518 | case Type::Complex: |
| 1519 | case Type::Vector: |
| 1520 | case Type::ExtVector: |
| 1521 | case Type::DependentSizedExtVector: |
| 1522 | case Type::ObjCObject: |
| 1523 | case Type::ObjCInterface: |
| 1524 | case Type::ObjCObjectPointer: |
| 1525 | case Type::Record: |
| 1526 | case Type::Enum: |
| 1527 | case Type::UnresolvedUsing: |
| 1528 | case Type::TypeOfExpr: |
| 1529 | case Type::TypeOf: |
| 1530 | case Type::Decltype: |
| 1531 | case Type::DependentName: |
| 1532 | case Type::InjectedClassName: |
| 1533 | case Type::TemplateSpecialization: |
| 1534 | case Type::DependentTemplateSpecialization: |
| 1535 | case Type::TemplateTypeParm: |
| 1536 | case Type::SubstTemplateTypeParmPack: |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1537 | case Type::Auto: |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1538 | case Type::PackExpansion: |
| 1539 | llvm_unreachable("type should never be variably-modified"); |
| 1540 | |
| 1541 | // These types can be variably-modified but should never need to |
| 1542 | // further decay. |
| 1543 | case Type::FunctionNoProto: |
| 1544 | case Type::FunctionProto: |
| 1545 | case Type::BlockPointer: |
| 1546 | case Type::MemberPointer: |
| 1547 | return type; |
| 1548 | |
| 1549 | // These types can be variably-modified. All these modifications |
| 1550 | // preserve structure except as noted by comments. |
| 1551 | // TODO: if we ever care about optimizing VLAs, there are no-op |
| 1552 | // optimizations available here. |
| 1553 | case Type::Pointer: |
| 1554 | result = getPointerType(getVariableArrayDecayedType( |
| 1555 | cast<PointerType>(ty)->getPointeeType())); |
| 1556 | break; |
| 1557 | |
| 1558 | case Type::LValueReference: { |
| 1559 | const LValueReferenceType *lv = cast<LValueReferenceType>(ty); |
| 1560 | result = getLValueReferenceType( |
| 1561 | getVariableArrayDecayedType(lv->getPointeeType()), |
| 1562 | lv->isSpelledAsLValue()); |
| 1563 | break; |
| 1564 | } |
| 1565 | |
| 1566 | case Type::RValueReference: { |
| 1567 | const RValueReferenceType *lv = cast<RValueReferenceType>(ty); |
| 1568 | result = getRValueReferenceType( |
| 1569 | getVariableArrayDecayedType(lv->getPointeeType())); |
| 1570 | break; |
| 1571 | } |
| 1572 | |
| 1573 | case Type::ConstantArray: { |
| 1574 | const ConstantArrayType *cat = cast<ConstantArrayType>(ty); |
| 1575 | result = getConstantArrayType( |
| 1576 | getVariableArrayDecayedType(cat->getElementType()), |
| 1577 | cat->getSize(), |
| 1578 | cat->getSizeModifier(), |
| 1579 | cat->getIndexTypeCVRQualifiers()); |
| 1580 | break; |
| 1581 | } |
| 1582 | |
| 1583 | case Type::DependentSizedArray: { |
| 1584 | const DependentSizedArrayType *dat = cast<DependentSizedArrayType>(ty); |
| 1585 | result = getDependentSizedArrayType( |
| 1586 | getVariableArrayDecayedType(dat->getElementType()), |
| 1587 | dat->getSizeExpr(), |
| 1588 | dat->getSizeModifier(), |
| 1589 | dat->getIndexTypeCVRQualifiers(), |
| 1590 | dat->getBracketsRange()); |
| 1591 | break; |
| 1592 | } |
| 1593 | |
| 1594 | // Turn incomplete types into [*] types. |
| 1595 | case Type::IncompleteArray: { |
| 1596 | const IncompleteArrayType *iat = cast<IncompleteArrayType>(ty); |
| 1597 | result = getVariableArrayType( |
| 1598 | getVariableArrayDecayedType(iat->getElementType()), |
| 1599 | /*size*/ 0, |
| 1600 | ArrayType::Normal, |
| 1601 | iat->getIndexTypeCVRQualifiers(), |
| 1602 | SourceRange()); |
| 1603 | break; |
| 1604 | } |
| 1605 | |
| 1606 | // Turn VLA types into [*] types. |
| 1607 | case Type::VariableArray: { |
| 1608 | const VariableArrayType *vat = cast<VariableArrayType>(ty); |
| 1609 | result = getVariableArrayType( |
| 1610 | getVariableArrayDecayedType(vat->getElementType()), |
| 1611 | /*size*/ 0, |
| 1612 | ArrayType::Star, |
| 1613 | vat->getIndexTypeCVRQualifiers(), |
| 1614 | vat->getBracketsRange()); |
| 1615 | break; |
| 1616 | } |
| 1617 | } |
| 1618 | |
| 1619 | // Apply the top-level qualifiers from the original. |
| 1620 | return getQualifiedType(result, split.second); |
| 1621 | } |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1622 | |
Steve Naroff | bdbf7b0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 1623 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 1624 | /// variable array of the specified element type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1625 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 1626 | Expr *NumElts, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1627 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1628 | unsigned IndexTypeQuals, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1629 | SourceRange Brackets) const { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1630 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 1631 | // that have an expression provided for their size. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1632 | QualType Canon; |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 1633 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1634 | // Be sure to pull qualifiers off the element type. |
| 1635 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 1636 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
| 1637 | Canon = getVariableArrayType(QualType(canonSplit.first, 0), NumElts, ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1638 | IndexTypeQuals, Brackets); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1639 | Canon = getQualifiedType(Canon, canonSplit.second); |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1642 | VariableArrayType *New = new(*this, TypeAlignment) |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1643 | VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1644 | |
| 1645 | VariableArrayTypes.push_back(New); |
| 1646 | Types.push_back(New); |
| 1647 | return QualType(New, 0); |
| 1648 | } |
| 1649 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1650 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 1651 | /// the type for a dependently-sized array of the specified element |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1652 | /// type. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1653 | QualType ASTContext::getDependentSizedArrayType(QualType elementType, |
| 1654 | Expr *numElements, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1655 | ArrayType::ArraySizeModifier ASM, |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1656 | unsigned elementTypeQuals, |
| 1657 | SourceRange brackets) const { |
| 1658 | assert((!numElements || numElements->isTypeDependent() || |
| 1659 | numElements->isValueDependent()) && |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1660 | "Size must be type- or value-dependent!"); |
| 1661 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1662 | // Dependently-sized array types that do not have a specified number |
| 1663 | // of elements will have their sizes deduced from a dependent |
| 1664 | // initializer. We do no canonicalization here at all, which is okay |
| 1665 | // because they can't be used in most locations. |
| 1666 | if (!numElements) { |
| 1667 | DependentSizedArrayType *newType |
| 1668 | = new (*this, TypeAlignment) |
| 1669 | DependentSizedArrayType(*this, elementType, QualType(), |
| 1670 | numElements, ASM, elementTypeQuals, |
| 1671 | brackets); |
| 1672 | Types.push_back(newType); |
| 1673 | return QualType(newType, 0); |
| 1674 | } |
| 1675 | |
| 1676 | // Otherwise, we actually build a new type every time, but we |
| 1677 | // also build a canonical type. |
| 1678 | |
| 1679 | SplitQualType canonElementType = getCanonicalType(elementType).split(); |
| 1680 | |
| 1681 | void *insertPos = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1682 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1683 | DependentSizedArrayType::Profile(ID, *this, |
| 1684 | QualType(canonElementType.first, 0), |
| 1685 | ASM, elementTypeQuals, numElements); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1686 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1687 | // Look for an existing type with these properties. |
| 1688 | DependentSizedArrayType *canonTy = |
| 1689 | DependentSizedArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1690 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1691 | // If we don't have one, build one. |
| 1692 | if (!canonTy) { |
| 1693 | canonTy = new (*this, TypeAlignment) |
| 1694 | DependentSizedArrayType(*this, QualType(canonElementType.first, 0), |
| 1695 | QualType(), numElements, ASM, elementTypeQuals, |
| 1696 | brackets); |
| 1697 | DependentSizedArrayTypes.InsertNode(canonTy, insertPos); |
| 1698 | Types.push_back(canonTy); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1699 | } |
| 1700 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1701 | // Apply qualifiers from the element type to the array. |
| 1702 | QualType canon = getQualifiedType(QualType(canonTy,0), |
| 1703 | canonElementType.second); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1704 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1705 | // If we didn't need extra canonicalization for the element type, |
| 1706 | // then just use that as our result. |
| 1707 | if (QualType(canonElementType.first, 0) == elementType) |
| 1708 | return canon; |
| 1709 | |
| 1710 | // Otherwise, we need to build a type which follows the spelling |
| 1711 | // of the element type. |
| 1712 | DependentSizedArrayType *sugaredType |
| 1713 | = new (*this, TypeAlignment) |
| 1714 | DependentSizedArrayType(*this, elementType, canon, numElements, |
| 1715 | ASM, elementTypeQuals, brackets); |
| 1716 | Types.push_back(sugaredType); |
| 1717 | return QualType(sugaredType, 0); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1720 | QualType ASTContext::getIncompleteArrayType(QualType elementType, |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1721 | ArrayType::ArraySizeModifier ASM, |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1722 | unsigned elementTypeQuals) const { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1723 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1724 | IncompleteArrayType::Profile(ID, elementType, ASM, elementTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1725 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1726 | void *insertPos = 0; |
| 1727 | if (IncompleteArrayType *iat = |
| 1728 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos)) |
| 1729 | return QualType(iat, 0); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1730 | |
| 1731 | // If the element type isn't canonical, this won't be a canonical type |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1732 | // either, so fill in the canonical type field. We also have to pull |
| 1733 | // qualifiers off the element type. |
| 1734 | QualType canon; |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1735 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1736 | if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) { |
| 1737 | SplitQualType canonSplit = getCanonicalType(elementType).split(); |
| 1738 | canon = getIncompleteArrayType(QualType(canonSplit.first, 0), |
| 1739 | ASM, elementTypeQuals); |
| 1740 | canon = getQualifiedType(canon, canonSplit.second); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1741 | |
| 1742 | // Get the new insert position for the node we care about. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1743 | IncompleteArrayType *existing = |
| 1744 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
| 1745 | assert(!existing && "Shouldn't be in the map!"); (void) existing; |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1746 | } |
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 | IncompleteArrayType *newType = new (*this, TypeAlignment) |
| 1749 | IncompleteArrayType(elementType, canon, ASM, elementTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1750 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1751 | IncompleteArrayTypes.InsertNode(newType, insertPos); |
| 1752 | Types.push_back(newType); |
| 1753 | return QualType(newType, 0); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1756 | /// getVectorType - Return the unique reference to a vector type of |
| 1757 | /// the specified element type and size. VectorType must be a built-in type. |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1758 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1759 | VectorType::VectorKind VecKind) const { |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1760 | assert(vecType->isBuiltinType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1761 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1762 | // Check if we've already instantiated a vector of this type. |
| 1763 | llvm::FoldingSetNodeID ID; |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1764 | VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind); |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1765 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1766 | void *InsertPos = 0; |
| 1767 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1768 | return QualType(VTP, 0); |
| 1769 | |
| 1770 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1771 | // so fill in the canonical type field. |
| 1772 | QualType Canonical; |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1773 | if (!vecType.isCanonical()) { |
Bob Wilson | 231da7e | 2010-11-16 00:32:20 +0000 | [diff] [blame] | 1774 | Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1775 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1776 | // Get the new insert position for the node we care about. |
| 1777 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1778 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1779 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1780 | VectorType *New = new (*this, TypeAlignment) |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1781 | VectorType(vecType, NumElts, Canonical, VecKind); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1782 | VectorTypes.InsertNode(New, InsertPos); |
| 1783 | Types.push_back(New); |
| 1784 | return QualType(New, 0); |
| 1785 | } |
| 1786 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1787 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1788 | /// the specified element type and size. VectorType must be a built-in type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1789 | QualType |
| 1790 | ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) const { |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1791 | assert(vecType->isBuiltinType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1792 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1793 | // Check if we've already instantiated a vector of this type. |
| 1794 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1795 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1796 | VectorType::GenericVector); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1797 | void *InsertPos = 0; |
| 1798 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1799 | return QualType(VTP, 0); |
| 1800 | |
| 1801 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1802 | // so fill in the canonical type field. |
| 1803 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1804 | if (!vecType.isCanonical()) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1805 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1806 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1807 | // Get the new insert position for the node we care about. |
| 1808 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1809 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1810 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1811 | ExtVectorType *New = new (*this, TypeAlignment) |
| 1812 | ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1813 | VectorTypes.InsertNode(New, InsertPos); |
| 1814 | Types.push_back(New); |
| 1815 | return QualType(New, 0); |
| 1816 | } |
| 1817 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1818 | QualType |
| 1819 | ASTContext::getDependentSizedExtVectorType(QualType vecType, |
| 1820 | Expr *SizeExpr, |
| 1821 | SourceLocation AttrLoc) const { |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1822 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1823 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1824 | SizeExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1825 | |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1826 | void *InsertPos = 0; |
| 1827 | DependentSizedExtVectorType *Canon |
| 1828 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1829 | DependentSizedExtVectorType *New; |
| 1830 | if (Canon) { |
| 1831 | // We already have a canonical version of this array type; use it as |
| 1832 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1833 | New = new (*this, TypeAlignment) |
| 1834 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), |
| 1835 | SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1836 | } else { |
| 1837 | QualType CanonVecTy = getCanonicalType(vecType); |
| 1838 | if (CanonVecTy == vecType) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1839 | New = new (*this, TypeAlignment) |
| 1840 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, |
| 1841 | AttrLoc); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1842 | |
| 1843 | DependentSizedExtVectorType *CanonCheck |
| 1844 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1845 | assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken"); |
| 1846 | (void)CanonCheck; |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1847 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); |
| 1848 | } else { |
| 1849 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 1850 | SourceLocation()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1851 | New = new (*this, TypeAlignment) |
| 1852 | DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1853 | } |
| 1854 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1855 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1856 | Types.push_back(New); |
| 1857 | return QualType(New, 0); |
| 1858 | } |
| 1859 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1860 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1861 | /// |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1862 | QualType |
| 1863 | ASTContext::getFunctionNoProtoType(QualType ResultTy, |
| 1864 | const FunctionType::ExtInfo &Info) const { |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 1865 | const CallingConv DefaultCC = Info.getCC(); |
| 1866 | const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ? |
| 1867 | CC_X86StdCall : DefaultCC; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1868 | // Unique functions, to guarantee there is only one function of a particular |
| 1869 | // structure. |
| 1870 | llvm::FoldingSetNodeID ID; |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1871 | FunctionNoProtoType::Profile(ID, ResultTy, Info); |
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 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1874 | if (FunctionNoProtoType *FT = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1875 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1876 | return QualType(FT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1877 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1878 | QualType Canonical; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1879 | if (!ResultTy.isCanonical() || |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1880 | getCanonicalCallConv(CallConv) != CallConv) { |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1881 | Canonical = |
| 1882 | getFunctionNoProtoType(getCanonicalType(ResultTy), |
| 1883 | Info.withCallingConv(getCanonicalCallConv(CallConv))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1884 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1885 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1886 | FunctionNoProtoType *NewIP = |
| 1887 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1888 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1889 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1890 | |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 1891 | FunctionProtoType::ExtInfo newInfo = Info.withCallingConv(CallConv); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1892 | FunctionNoProtoType *New = new (*this, TypeAlignment) |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 1893 | FunctionNoProtoType(ResultTy, Canonical, newInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1894 | Types.push_back(New); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1895 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1896 | return QualType(New, 0); |
| 1897 | } |
| 1898 | |
| 1899 | /// getFunctionType - Return a normal function type with a typed argument |
| 1900 | /// list. isVariadic indicates whether the argument list includes '...'. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1901 | QualType |
| 1902 | ASTContext::getFunctionType(QualType ResultTy, |
| 1903 | const QualType *ArgArray, unsigned NumArgs, |
| 1904 | const FunctionProtoType::ExtProtoInfo &EPI) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1905 | // Unique functions, to guarantee there is only one function of a particular |
| 1906 | // structure. |
| 1907 | llvm::FoldingSetNodeID ID; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1908 | FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, EPI); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1909 | |
| 1910 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1911 | if (FunctionProtoType *FTP = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1912 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1913 | return QualType(FTP, 0); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1914 | |
| 1915 | // Determine whether the type being created is already canonical or not. |
Sebastian Redl | 8b5b409 | 2011-03-06 10:52:04 +0000 | [diff] [blame^] | 1916 | bool isCanonical= EPI.ExceptionSpecType == EST_None && ResultTy.isCanonical(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1917 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1918 | if (!ArgArray[i].isCanonicalAsParam()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1919 | isCanonical = false; |
| 1920 | |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 1921 | const CallingConv DefaultCC = EPI.ExtInfo.getCC(); |
| 1922 | const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ? |
| 1923 | CC_X86StdCall : DefaultCC; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1924 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1925 | // If this type isn't canonical, get the canonical version of it. |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1926 | // The exception spec is not part of the canonical type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1927 | QualType Canonical; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1928 | if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1929 | llvm::SmallVector<QualType, 16> CanonicalArgs; |
| 1930 | CanonicalArgs.reserve(NumArgs); |
| 1931 | for (unsigned i = 0; i != NumArgs; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1932 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1933 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1934 | FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI; |
Sebastian Redl | 8b5b409 | 2011-03-06 10:52:04 +0000 | [diff] [blame^] | 1935 | CanonicalEPI.ExceptionSpecType = EST_None; |
| 1936 | CanonicalEPI.NumExceptions = 0; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1937 | CanonicalEPI.ExtInfo |
| 1938 | = CanonicalEPI.ExtInfo.withCallingConv(getCanonicalCallConv(CallConv)); |
| 1939 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1940 | Canonical = getFunctionType(getCanonicalType(ResultTy), |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1941 | CanonicalArgs.data(), NumArgs, |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1942 | CanonicalEPI); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1943 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1944 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1945 | FunctionProtoType *NewIP = |
| 1946 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1947 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1948 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1949 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1950 | // FunctionProtoType objects are allocated with extra bytes after them |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1951 | // for two variable size arrays (for parameter and exception types) at the |
| 1952 | // end of them. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1953 | size_t Size = sizeof(FunctionProtoType) + |
| 1954 | NumArgs * sizeof(QualType) + |
| 1955 | EPI.NumExceptions * sizeof(QualType); |
| 1956 | FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment); |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 1957 | FunctionProtoType::ExtProtoInfo newEPI = EPI; |
| 1958 | newEPI.ExtInfo = EPI.ExtInfo.withCallingConv(CallConv); |
| 1959 | new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, Canonical, newEPI); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1960 | Types.push_back(FTP); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1961 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1962 | return QualType(FTP, 0); |
| 1963 | } |
| 1964 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1965 | #ifndef NDEBUG |
| 1966 | static bool NeedsInjectedClassNameType(const RecordDecl *D) { |
| 1967 | if (!isa<CXXRecordDecl>(D)) return false; |
| 1968 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 1969 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) |
| 1970 | return true; |
| 1971 | if (RD->getDescribedClassTemplate() && |
| 1972 | !isa<ClassTemplateSpecializationDecl>(RD)) |
| 1973 | return true; |
| 1974 | return false; |
| 1975 | } |
| 1976 | #endif |
| 1977 | |
| 1978 | /// getInjectedClassNameType - Return the unique reference to the |
| 1979 | /// injected class name type for the specified templated declaration. |
| 1980 | QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1981 | QualType TST) const { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1982 | assert(NeedsInjectedClassNameType(Decl)); |
| 1983 | if (Decl->TypeForDecl) { |
| 1984 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
Argyrios Kyrtzidis | 37ffed3 | 2010-07-02 11:55:32 +0000 | [diff] [blame] | 1985 | } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDeclaration()) { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1986 | assert(PrevDecl->TypeForDecl && "previous declaration has no type"); |
| 1987 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 1988 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
| 1989 | } else { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1990 | Type *newType = |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1991 | new (*this, TypeAlignment) InjectedClassNameType(Decl, TST); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1992 | Decl->TypeForDecl = newType; |
| 1993 | Types.push_back(newType); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1994 | } |
| 1995 | return QualType(Decl->TypeForDecl, 0); |
| 1996 | } |
| 1997 | |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1998 | /// getTypeDeclType - Return the unique reference to the type for the |
| 1999 | /// specified type declaration. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2000 | QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const { |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 2001 | assert(Decl && "Passed null for Decl param"); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2002 | assert(!Decl->TypeForDecl && "TypeForDecl present in slow case"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2003 | |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2004 | if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2005 | return getTypedefType(Typedef); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2006 | |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2007 | assert(!isa<TemplateTypeParmDecl>(Decl) && |
| 2008 | "Template type parameter types are always available."); |
| 2009 | |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2010 | if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2011 | assert(!Record->getPreviousDeclaration() && |
| 2012 | "struct/union has previous declaration"); |
| 2013 | assert(!NeedsInjectedClassNameType(Record)); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2014 | return getRecordType(Record); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2015 | } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2016 | assert(!Enum->getPreviousDeclaration() && |
| 2017 | "enum has previous declaration"); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2018 | return getEnumType(Enum); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2019 | } else if (const UnresolvedUsingTypenameDecl *Using = |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2020 | dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2021 | Type *newType = new (*this, TypeAlignment) UnresolvedUsingType(Using); |
| 2022 | Decl->TypeForDecl = newType; |
| 2023 | Types.push_back(newType); |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 2024 | } else |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2025 | llvm_unreachable("TypeDecl without a type?"); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 2026 | |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 2027 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2028 | } |
| 2029 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2030 | /// getTypedefType - Return the unique reference to the type for the |
| 2031 | /// specified typename decl. |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2032 | QualType |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2033 | ASTContext::getTypedefType(const TypedefDecl *Decl, QualType Canonical) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2034 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2035 | |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2036 | if (Canonical.isNull()) |
| 2037 | Canonical = getCanonicalType(Decl->getUnderlyingType()); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2038 | TypedefType *newType = new(*this, TypeAlignment) |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2039 | TypedefType(Type::Typedef, Decl, Canonical); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2040 | Decl->TypeForDecl = newType; |
| 2041 | Types.push_back(newType); |
| 2042 | return QualType(newType, 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2043 | } |
| 2044 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2045 | QualType ASTContext::getRecordType(const RecordDecl *Decl) const { |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2046 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 2047 | |
| 2048 | if (const RecordDecl *PrevDecl = Decl->getPreviousDeclaration()) |
| 2049 | if (PrevDecl->TypeForDecl) |
| 2050 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 2051 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2052 | RecordType *newType = new (*this, TypeAlignment) RecordType(Decl); |
| 2053 | Decl->TypeForDecl = newType; |
| 2054 | Types.push_back(newType); |
| 2055 | return QualType(newType, 0); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2056 | } |
| 2057 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2058 | QualType ASTContext::getEnumType(const EnumDecl *Decl) const { |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2059 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 2060 | |
| 2061 | if (const EnumDecl *PrevDecl = Decl->getPreviousDeclaration()) |
| 2062 | if (PrevDecl->TypeForDecl) |
| 2063 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 2064 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2065 | EnumType *newType = new (*this, TypeAlignment) EnumType(Decl); |
| 2066 | Decl->TypeForDecl = newType; |
| 2067 | Types.push_back(newType); |
| 2068 | return QualType(newType, 0); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2069 | } |
| 2070 | |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 2071 | QualType ASTContext::getAttributedType(AttributedType::Kind attrKind, |
| 2072 | QualType modifiedType, |
| 2073 | QualType equivalentType) { |
| 2074 | llvm::FoldingSetNodeID id; |
| 2075 | AttributedType::Profile(id, attrKind, modifiedType, equivalentType); |
| 2076 | |
| 2077 | void *insertPos = 0; |
| 2078 | AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos); |
| 2079 | if (type) return QualType(type, 0); |
| 2080 | |
| 2081 | QualType canon = getCanonicalType(equivalentType); |
| 2082 | type = new (*this, TypeAlignment) |
| 2083 | AttributedType(canon, attrKind, modifiedType, equivalentType); |
| 2084 | |
| 2085 | Types.push_back(type); |
| 2086 | AttributedTypes.InsertNode(type, insertPos); |
| 2087 | |
| 2088 | return QualType(type, 0); |
| 2089 | } |
| 2090 | |
| 2091 | |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2092 | /// \brief Retrieve a substitution-result type. |
| 2093 | QualType |
| 2094 | ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2095 | QualType Replacement) const { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2096 | assert(Replacement.isCanonical() |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2097 | && "replacement types must always be canonical"); |
| 2098 | |
| 2099 | llvm::FoldingSetNodeID ID; |
| 2100 | SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); |
| 2101 | void *InsertPos = 0; |
| 2102 | SubstTemplateTypeParmType *SubstParm |
| 2103 | = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2104 | |
| 2105 | if (!SubstParm) { |
| 2106 | SubstParm = new (*this, TypeAlignment) |
| 2107 | SubstTemplateTypeParmType(Parm, Replacement); |
| 2108 | Types.push_back(SubstParm); |
| 2109 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 2110 | } |
| 2111 | |
| 2112 | return QualType(SubstParm, 0); |
| 2113 | } |
| 2114 | |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 2115 | /// \brief Retrieve a |
| 2116 | QualType ASTContext::getSubstTemplateTypeParmPackType( |
| 2117 | const TemplateTypeParmType *Parm, |
| 2118 | const TemplateArgument &ArgPack) { |
| 2119 | #ifndef NDEBUG |
| 2120 | for (TemplateArgument::pack_iterator P = ArgPack.pack_begin(), |
| 2121 | PEnd = ArgPack.pack_end(); |
| 2122 | P != PEnd; ++P) { |
| 2123 | assert(P->getKind() == TemplateArgument::Type &&"Pack contains a non-type"); |
| 2124 | assert(P->getAsType().isCanonical() && "Pack contains non-canonical type"); |
| 2125 | } |
| 2126 | #endif |
| 2127 | |
| 2128 | llvm::FoldingSetNodeID ID; |
| 2129 | SubstTemplateTypeParmPackType::Profile(ID, Parm, ArgPack); |
| 2130 | void *InsertPos = 0; |
| 2131 | if (SubstTemplateTypeParmPackType *SubstParm |
| 2132 | = SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2133 | return QualType(SubstParm, 0); |
| 2134 | |
| 2135 | QualType Canon; |
| 2136 | if (!Parm->isCanonicalUnqualified()) { |
| 2137 | Canon = getCanonicalType(QualType(Parm, 0)); |
| 2138 | Canon = getSubstTemplateTypeParmPackType(cast<TemplateTypeParmType>(Canon), |
| 2139 | ArgPack); |
| 2140 | SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2141 | } |
| 2142 | |
| 2143 | SubstTemplateTypeParmPackType *SubstParm |
| 2144 | = new (*this, TypeAlignment) SubstTemplateTypeParmPackType(Parm, Canon, |
| 2145 | ArgPack); |
| 2146 | Types.push_back(SubstParm); |
| 2147 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 2148 | return QualType(SubstParm, 0); |
| 2149 | } |
| 2150 | |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2151 | /// \brief Retrieve the template type parameter type for a template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2152 | /// parameter or parameter pack with the given depth, index, and (optionally) |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2153 | /// name. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2154 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2155 | bool ParameterPack, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2156 | IdentifierInfo *Name) const { |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2157 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | efed5c8 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 2158 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2159 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2160 | TemplateTypeParmType *TypeParm |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2161 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2162 | |
| 2163 | if (TypeParm) |
| 2164 | return QualType(TypeParm, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2165 | |
Douglas Gregor | efed5c8 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 2166 | if (Name) { |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2167 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | efed5c8 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 2168 | TypeParm = new (*this, TypeAlignment) |
| 2169 | TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2170 | |
| 2171 | TemplateTypeParmType *TypeCheck |
| 2172 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2173 | assert(!TypeCheck && "Template type parameter canonical type broken"); |
| 2174 | (void)TypeCheck; |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2175 | } else |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2176 | TypeParm = new (*this, TypeAlignment) |
| 2177 | TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2178 | |
| 2179 | Types.push_back(TypeParm); |
| 2180 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 2181 | |
| 2182 | return QualType(TypeParm, 0); |
| 2183 | } |
| 2184 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2185 | TypeSourceInfo * |
| 2186 | ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name, |
| 2187 | SourceLocation NameLoc, |
| 2188 | const TemplateArgumentListInfo &Args, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2189 | QualType CanonType) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2190 | assert(!Name.getAsDependentTemplateName() && |
| 2191 | "No dependent template names here!"); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2192 | QualType TST = getTemplateSpecializationType(Name, Args, CanonType); |
| 2193 | |
| 2194 | TypeSourceInfo *DI = CreateTypeSourceInfo(TST); |
| 2195 | TemplateSpecializationTypeLoc TL |
| 2196 | = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc()); |
| 2197 | TL.setTemplateNameLoc(NameLoc); |
| 2198 | TL.setLAngleLoc(Args.getLAngleLoc()); |
| 2199 | TL.setRAngleLoc(Args.getRAngleLoc()); |
| 2200 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 2201 | TL.setArgLocInfo(i, Args[i].getLocInfo()); |
| 2202 | return DI; |
| 2203 | } |
| 2204 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2205 | QualType |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2206 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2207 | const TemplateArgumentListInfo &Args, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2208 | QualType Canon) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2209 | assert(!Template.getAsDependentTemplateName() && |
| 2210 | "No dependent template names here!"); |
| 2211 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2212 | unsigned NumArgs = Args.size(); |
| 2213 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2214 | llvm::SmallVector<TemplateArgument, 4> ArgVec; |
| 2215 | ArgVec.reserve(NumArgs); |
| 2216 | for (unsigned i = 0; i != NumArgs; ++i) |
| 2217 | ArgVec.push_back(Args[i].getArgument()); |
| 2218 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2219 | return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 2220 | Canon); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2221 | } |
| 2222 | |
| 2223 | QualType |
| 2224 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2225 | const TemplateArgument *Args, |
| 2226 | unsigned NumArgs, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2227 | QualType Canon) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2228 | assert(!Template.getAsDependentTemplateName() && |
| 2229 | "No dependent template names here!"); |
Douglas Gregor | 0f0ea2a | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 2230 | // Look through qualified template names. |
| 2231 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 2232 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2233 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 2234 | if (!Canon.isNull()) |
| 2235 | Canon = getCanonicalType(Canon); |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2236 | else |
| 2237 | Canon = getCanonicalTemplateSpecializationType(Template, Args, NumArgs); |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 2238 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2239 | // Allocate the (non-canonical) template specialization type, but don't |
| 2240 | // try to unique it: these types typically have location information that |
| 2241 | // we don't unique and don't want to lose. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2242 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2243 | sizeof(TemplateArgument) * NumArgs), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2244 | TypeAlignment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2245 | TemplateSpecializationType *Spec |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 2246 | = new (Mem) TemplateSpecializationType(Template, |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2247 | Args, NumArgs, |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 2248 | Canon); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2249 | |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2250 | Types.push_back(Spec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2251 | return QualType(Spec, 0); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2252 | } |
| 2253 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2254 | QualType |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2255 | ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template, |
| 2256 | const TemplateArgument *Args, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2257 | unsigned NumArgs) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2258 | assert(!Template.getAsDependentTemplateName() && |
| 2259 | "No dependent template names here!"); |
Douglas Gregor | 0f0ea2a | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 2260 | // Look through qualified template names. |
| 2261 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 2262 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2263 | |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2264 | // Build the canonical template specialization type. |
| 2265 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); |
| 2266 | llvm::SmallVector<TemplateArgument, 4> CanonArgs; |
| 2267 | CanonArgs.reserve(NumArgs); |
| 2268 | for (unsigned I = 0; I != NumArgs; ++I) |
| 2269 | CanonArgs.push_back(getCanonicalTemplateArgument(Args[I])); |
| 2270 | |
| 2271 | // Determine whether this canonical template specialization type already |
| 2272 | // exists. |
| 2273 | llvm::FoldingSetNodeID ID; |
| 2274 | TemplateSpecializationType::Profile(ID, CanonTemplate, |
| 2275 | CanonArgs.data(), NumArgs, *this); |
| 2276 | |
| 2277 | void *InsertPos = 0; |
| 2278 | TemplateSpecializationType *Spec |
| 2279 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2280 | |
| 2281 | if (!Spec) { |
| 2282 | // Allocate a new canonical template specialization type. |
| 2283 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
| 2284 | sizeof(TemplateArgument) * NumArgs), |
| 2285 | TypeAlignment); |
| 2286 | Spec = new (Mem) TemplateSpecializationType(CanonTemplate, |
| 2287 | CanonArgs.data(), NumArgs, |
| 2288 | QualType()); |
| 2289 | Types.push_back(Spec); |
| 2290 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
| 2291 | } |
| 2292 | |
| 2293 | assert(Spec->isDependentType() && |
| 2294 | "Non-dependent template-id type must have a canonical type"); |
| 2295 | return QualType(Spec, 0); |
| 2296 | } |
| 2297 | |
| 2298 | QualType |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2299 | ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword, |
| 2300 | NestedNameSpecifier *NNS, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2301 | QualType NamedType) const { |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2302 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2303 | ElaboratedType::Profile(ID, Keyword, NNS, NamedType); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2304 | |
| 2305 | void *InsertPos = 0; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2306 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2307 | if (T) |
| 2308 | return QualType(T, 0); |
| 2309 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2310 | QualType Canon = NamedType; |
| 2311 | if (!Canon.isCanonical()) { |
| 2312 | Canon = getCanonicalType(NamedType); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2313 | ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2314 | assert(!CheckT && "Elaborated canonical type broken"); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2315 | (void)CheckT; |
| 2316 | } |
| 2317 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2318 | T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2319 | Types.push_back(T); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2320 | ElaboratedTypes.InsertNode(T, InsertPos); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2321 | return QualType(T, 0); |
| 2322 | } |
| 2323 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 2324 | QualType |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2325 | ASTContext::getParenType(QualType InnerType) const { |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 2326 | llvm::FoldingSetNodeID ID; |
| 2327 | ParenType::Profile(ID, InnerType); |
| 2328 | |
| 2329 | void *InsertPos = 0; |
| 2330 | ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2331 | if (T) |
| 2332 | return QualType(T, 0); |
| 2333 | |
| 2334 | QualType Canon = InnerType; |
| 2335 | if (!Canon.isCanonical()) { |
| 2336 | Canon = getCanonicalType(InnerType); |
| 2337 | ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2338 | assert(!CheckT && "Paren canonical type broken"); |
| 2339 | (void)CheckT; |
| 2340 | } |
| 2341 | |
| 2342 | T = new (*this) ParenType(InnerType, Canon); |
| 2343 | Types.push_back(T); |
| 2344 | ParenTypes.InsertNode(T, InsertPos); |
| 2345 | return QualType(T, 0); |
| 2346 | } |
| 2347 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2348 | QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, |
| 2349 | NestedNameSpecifier *NNS, |
| 2350 | const IdentifierInfo *Name, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2351 | QualType Canon) const { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2352 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 2353 | |
| 2354 | if (Canon.isNull()) { |
| 2355 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2356 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 2357 | if (Keyword == ETK_None) |
| 2358 | CanonKeyword = ETK_Typename; |
| 2359 | |
| 2360 | if (CanonNNS != NNS || CanonKeyword != Keyword) |
| 2361 | Canon = getDependentNameType(CanonKeyword, CanonNNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2362 | } |
| 2363 | |
| 2364 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2365 | DependentNameType::Profile(ID, Keyword, NNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2366 | |
| 2367 | void *InsertPos = 0; |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2368 | DependentNameType *T |
| 2369 | = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2370 | if (T) |
| 2371 | return QualType(T, 0); |
| 2372 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2373 | T = new (*this) DependentNameType(Keyword, NNS, Name, Canon); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2374 | Types.push_back(T); |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2375 | DependentNameTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2376 | return QualType(T, 0); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2377 | } |
| 2378 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2379 | QualType |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2380 | ASTContext::getDependentTemplateSpecializationType( |
| 2381 | ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2382 | NestedNameSpecifier *NNS, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2383 | const IdentifierInfo *Name, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2384 | const TemplateArgumentListInfo &Args) const { |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2385 | // TODO: avoid this copy |
| 2386 | llvm::SmallVector<TemplateArgument, 16> ArgCopy; |
| 2387 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
| 2388 | ArgCopy.push_back(Args[I].getArgument()); |
| 2389 | return getDependentTemplateSpecializationType(Keyword, NNS, Name, |
| 2390 | ArgCopy.size(), |
| 2391 | ArgCopy.data()); |
| 2392 | } |
| 2393 | |
| 2394 | QualType |
| 2395 | ASTContext::getDependentTemplateSpecializationType( |
| 2396 | ElaboratedTypeKeyword Keyword, |
| 2397 | NestedNameSpecifier *NNS, |
| 2398 | const IdentifierInfo *Name, |
| 2399 | unsigned NumArgs, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2400 | const TemplateArgument *Args) const { |
Douglas Gregor | aa2187d | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 2401 | assert((!NNS || NNS->isDependent()) && |
| 2402 | "nested-name-specifier must be dependent"); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2403 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2404 | llvm::FoldingSetNodeID ID; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2405 | DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS, |
| 2406 | Name, NumArgs, Args); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2407 | |
| 2408 | void *InsertPos = 0; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2409 | DependentTemplateSpecializationType *T |
| 2410 | = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2411 | if (T) |
| 2412 | return QualType(T, 0); |
| 2413 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2414 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2415 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2416 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 2417 | if (Keyword == ETK_None) CanonKeyword = ETK_Typename; |
| 2418 | |
| 2419 | bool AnyNonCanonArgs = false; |
| 2420 | llvm::SmallVector<TemplateArgument, 16> CanonArgs(NumArgs); |
| 2421 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 2422 | CanonArgs[I] = getCanonicalTemplateArgument(Args[I]); |
| 2423 | if (!CanonArgs[I].structurallyEquals(Args[I])) |
| 2424 | AnyNonCanonArgs = true; |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2425 | } |
| 2426 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2427 | QualType Canon; |
| 2428 | if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) { |
| 2429 | Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS, |
| 2430 | Name, NumArgs, |
| 2431 | CanonArgs.data()); |
| 2432 | |
| 2433 | // Find the insert position again. |
| 2434 | DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2435 | } |
| 2436 | |
| 2437 | void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) + |
| 2438 | sizeof(TemplateArgument) * NumArgs), |
| 2439 | TypeAlignment); |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 2440 | T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2441 | Name, NumArgs, Args, Canon); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2442 | Types.push_back(T); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2443 | DependentTemplateSpecializationTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2444 | return QualType(T, 0); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2445 | } |
| 2446 | |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2447 | QualType ASTContext::getPackExpansionType(QualType Pattern, |
| 2448 | llvm::Optional<unsigned> NumExpansions) { |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2449 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2450 | PackExpansionType::Profile(ID, Pattern, NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2451 | |
| 2452 | assert(Pattern->containsUnexpandedParameterPack() && |
| 2453 | "Pack expansions must expand one or more parameter packs"); |
| 2454 | void *InsertPos = 0; |
| 2455 | PackExpansionType *T |
| 2456 | = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2457 | if (T) |
| 2458 | return QualType(T, 0); |
| 2459 | |
| 2460 | QualType Canon; |
| 2461 | if (!Pattern.isCanonical()) { |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2462 | Canon = getPackExpansionType(getCanonicalType(Pattern), NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2463 | |
| 2464 | // Find the insert position again. |
| 2465 | PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2466 | } |
| 2467 | |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2468 | T = new (*this) PackExpansionType(Pattern, Canon, NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2469 | Types.push_back(T); |
| 2470 | PackExpansionTypes.InsertNode(T, InsertPos); |
| 2471 | return QualType(T, 0); |
| 2472 | } |
| 2473 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2474 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 2475 | /// alphabetically. |
| 2476 | static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, |
| 2477 | const ObjCProtocolDecl *RHS) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2478 | return LHS->getDeclName() < RHS->getDeclName(); |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2479 | } |
| 2480 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2481 | static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols, |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2482 | unsigned NumProtocols) { |
| 2483 | if (NumProtocols == 0) return true; |
| 2484 | |
| 2485 | for (unsigned i = 1; i != NumProtocols; ++i) |
| 2486 | if (!CmpProtocolNames(Protocols[i-1], Protocols[i])) |
| 2487 | return false; |
| 2488 | return true; |
| 2489 | } |
| 2490 | |
| 2491 | static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols, |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2492 | unsigned &NumProtocols) { |
| 2493 | ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2494 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2495 | // Sort protocols, keyed by name. |
| 2496 | std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); |
| 2497 | |
| 2498 | // Remove duplicates. |
| 2499 | ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); |
| 2500 | NumProtocols = ProtocolsEnd-Protocols; |
| 2501 | } |
| 2502 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2503 | QualType ASTContext::getObjCObjectType(QualType BaseType, |
| 2504 | ObjCProtocolDecl * const *Protocols, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2505 | unsigned NumProtocols) const { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2506 | // If the base type is an interface and there aren't any protocols |
| 2507 | // to add, then the interface type will do just fine. |
| 2508 | if (!NumProtocols && isa<ObjCInterfaceType>(BaseType)) |
| 2509 | return BaseType; |
| 2510 | |
| 2511 | // Look in the folding set for an existing type. |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2512 | llvm::FoldingSetNodeID ID; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2513 | ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2514 | void *InsertPos = 0; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2515 | if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2516 | return QualType(QT, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2517 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2518 | // Build the canonical type, which has the canonical base type and |
| 2519 | // a sorted-and-uniqued list of protocols. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2520 | QualType Canonical; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2521 | bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols); |
| 2522 | if (!ProtocolsSorted || !BaseType.isCanonical()) { |
| 2523 | if (!ProtocolsSorted) { |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 2524 | llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols, |
| 2525 | Protocols + NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2526 | unsigned UniqueCount = NumProtocols; |
| 2527 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2528 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2529 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 2530 | &Sorted[0], UniqueCount); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2531 | } else { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2532 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 2533 | Protocols, NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2534 | } |
| 2535 | |
| 2536 | // Regenerate InsertPos. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2537 | ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2538 | } |
| 2539 | |
| 2540 | unsigned Size = sizeof(ObjCObjectTypeImpl); |
| 2541 | Size += NumProtocols * sizeof(ObjCProtocolDecl *); |
| 2542 | void *Mem = Allocate(Size, TypeAlignment); |
| 2543 | ObjCObjectTypeImpl *T = |
| 2544 | new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols); |
| 2545 | |
| 2546 | Types.push_back(T); |
| 2547 | ObjCObjectTypes.InsertNode(T, InsertPos); |
| 2548 | return QualType(T, 0); |
| 2549 | } |
| 2550 | |
| 2551 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 2552 | /// the given object type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2553 | QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2554 | llvm::FoldingSetNodeID ID; |
| 2555 | ObjCObjectPointerType::Profile(ID, ObjectT); |
| 2556 | |
| 2557 | void *InsertPos = 0; |
| 2558 | if (ObjCObjectPointerType *QT = |
| 2559 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2560 | return QualType(QT, 0); |
| 2561 | |
| 2562 | // Find the canonical object type. |
| 2563 | QualType Canonical; |
| 2564 | if (!ObjectT.isCanonical()) { |
| 2565 | Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT)); |
| 2566 | |
| 2567 | // Regenerate InsertPos. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2568 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2569 | } |
| 2570 | |
Douglas Gregor | fd6a088 | 2010-02-08 22:59:26 +0000 | [diff] [blame] | 2571 | // No match. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2572 | void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment); |
| 2573 | ObjCObjectPointerType *QType = |
| 2574 | new (Mem) ObjCObjectPointerType(Canonical, ObjectT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2575 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2576 | Types.push_back(QType); |
| 2577 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2578 | return QualType(QType, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2579 | } |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2580 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2581 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 2582 | /// specified ObjC interface decl. The list of protocols is optional. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2583 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) const { |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2584 | if (Decl->TypeForDecl) |
| 2585 | return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2586 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2587 | // FIXME: redeclarations? |
| 2588 | void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment); |
| 2589 | ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl); |
| 2590 | Decl->TypeForDecl = T; |
| 2591 | Types.push_back(T); |
| 2592 | return QualType(T, 0); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2593 | } |
| 2594 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2595 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 2596 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2597 | /// multiple declarations that refer to "typeof(x)" all contain different |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2598 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2599 | /// on canonical type's (which are always unique). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2600 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) const { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2601 | TypeOfExprType *toe; |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2602 | if (tofExpr->isTypeDependent()) { |
| 2603 | llvm::FoldingSetNodeID ID; |
| 2604 | DependentTypeOfExprType::Profile(ID, *this, tofExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2605 | |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2606 | void *InsertPos = 0; |
| 2607 | DependentTypeOfExprType *Canon |
| 2608 | = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2609 | if (Canon) { |
| 2610 | // We already have a "canonical" version of an identical, dependent |
| 2611 | // typeof(expr) type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2612 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2613 | QualType((TypeOfExprType*)Canon, 0)); |
| 2614 | } |
| 2615 | else { |
| 2616 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2617 | Canon |
| 2618 | = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2619 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); |
| 2620 | toe = Canon; |
| 2621 | } |
| 2622 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2623 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2624 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2625 | } |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2626 | Types.push_back(toe); |
| 2627 | return QualType(toe, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2628 | } |
| 2629 | |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2630 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 2631 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 2632 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2633 | /// an issue. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2634 | /// on canonical type's (which are always unique). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2635 | QualType ASTContext::getTypeOfType(QualType tofType) const { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2636 | QualType Canonical = getCanonicalType(tofType); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2637 | TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2638 | Types.push_back(tot); |
| 2639 | return QualType(tot, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2640 | } |
| 2641 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2642 | /// getDecltypeForExpr - Given an expr, will return the decltype for that |
| 2643 | /// expression, according to the rules in C++0x [dcl.type.simple]p4 |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2644 | static QualType getDecltypeForExpr(const Expr *e, const ASTContext &Context) { |
Anders Carlsson | a07c33e | 2009-06-25 15:00:34 +0000 | [diff] [blame] | 2645 | if (e->isTypeDependent()) |
| 2646 | return Context.DependentTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2647 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2648 | // If e is an id expression or a class member access, decltype(e) is defined |
| 2649 | // as the type of the entity named by e. |
| 2650 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) { |
| 2651 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) |
| 2652 | return VD->getType(); |
| 2653 | } |
| 2654 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) { |
| 2655 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) |
| 2656 | return FD->getType(); |
| 2657 | } |
| 2658 | // If e is a function call or an invocation of an overloaded operator, |
| 2659 | // (parentheses around e are ignored), decltype(e) is defined as the |
| 2660 | // return type of that function. |
| 2661 | if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens())) |
| 2662 | return CE->getCallReturnType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2663 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2664 | QualType T = e->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2665 | |
| 2666 | // 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] | 2667 | // defined as T&, otherwise decltype(e) is defined as T. |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 2668 | if (e->isLValue()) |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2669 | T = Context.getLValueReferenceType(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2670 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2671 | return T; |
| 2672 | } |
| 2673 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2674 | /// getDecltypeType - Unlike many "get<Type>" functions, we don't unique |
| 2675 | /// DecltypeType AST's. The only motivation to unique these nodes would be |
| 2676 | /// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2677 | /// an issue. This doesn't effect the type checker, since it operates |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2678 | /// on canonical type's (which are always unique). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2679 | QualType ASTContext::getDecltypeType(Expr *e) const { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2680 | DecltypeType *dt; |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2681 | if (e->isTypeDependent()) { |
| 2682 | llvm::FoldingSetNodeID ID; |
| 2683 | DependentDecltypeType::Profile(ID, *this, e); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2684 | |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2685 | void *InsertPos = 0; |
| 2686 | DependentDecltypeType *Canon |
| 2687 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2688 | if (Canon) { |
| 2689 | // We already have a "canonical" version of an equivalent, dependent |
| 2690 | // decltype type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2691 | dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy, |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2692 | QualType((DecltypeType*)Canon, 0)); |
| 2693 | } |
| 2694 | else { |
| 2695 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2696 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2697 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); |
| 2698 | dt = Canon; |
| 2699 | } |
| 2700 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2701 | QualType T = getDecltypeForExpr(e, *this); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2702 | dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T)); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2703 | } |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2704 | Types.push_back(dt); |
| 2705 | return QualType(dt, 0); |
| 2706 | } |
| 2707 | |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 2708 | /// getAutoType - We only unique auto types after they've been deduced. |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2709 | QualType ASTContext::getAutoType(QualType DeducedType) const { |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 2710 | void *InsertPos = 0; |
| 2711 | if (!DeducedType.isNull()) { |
| 2712 | // Look in the folding set for an existing type. |
| 2713 | llvm::FoldingSetNodeID ID; |
| 2714 | AutoType::Profile(ID, DeducedType); |
| 2715 | if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2716 | return QualType(AT, 0); |
| 2717 | } |
| 2718 | |
| 2719 | AutoType *AT = new (*this, TypeAlignment) AutoType(DeducedType); |
| 2720 | Types.push_back(AT); |
| 2721 | if (InsertPos) |
| 2722 | AutoTypes.InsertNode(AT, InsertPos); |
| 2723 | return QualType(AT, 0); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2724 | } |
| 2725 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2726 | /// getTagDeclType - Return the unique reference to the type for the |
| 2727 | /// specified TagDecl (struct/union/class/enum) decl. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2728 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) const { |
Ted Kremenek | d778f88 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 2729 | assert (Decl); |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 2730 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 2731 | // away const? mutable? |
| 2732 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2733 | } |
| 2734 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2735 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 2736 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 2737 | /// needs to agree with the definition in <stddef.h>. |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 2738 | CanQualType ASTContext::getSizeType() const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2739 | return getFromTargetType(Target.getSizeType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2740 | } |
| 2741 | |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 2742 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 2743 | /// Used when in C++, as a GCC extension. |
| 2744 | QualType ASTContext::getSignedWCharType() const { |
| 2745 | // FIXME: derive from "Target" ? |
| 2746 | return WCharTy; |
| 2747 | } |
| 2748 | |
| 2749 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 2750 | /// Used when in C++, as a GCC extension. |
| 2751 | QualType ASTContext::getUnsignedWCharType() const { |
| 2752 | // FIXME: derive from "Target" ? |
| 2753 | return UnsignedIntTy; |
| 2754 | } |
| 2755 | |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 2756 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) |
| 2757 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 2758 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2759 | return getFromTargetType(Target.getPtrDiffType(0)); |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 2760 | } |
| 2761 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2762 | //===----------------------------------------------------------------------===// |
| 2763 | // Type Operators |
| 2764 | //===----------------------------------------------------------------------===// |
| 2765 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2766 | CanQualType ASTContext::getCanonicalParamType(QualType T) const { |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2767 | // Push qualifiers into arrays, and then discard any remaining |
| 2768 | // qualifiers. |
| 2769 | T = getCanonicalType(T); |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2770 | T = getVariableArrayDecayedType(T); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2771 | const Type *Ty = T.getTypePtr(); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2772 | QualType Result; |
| 2773 | if (isa<ArrayType>(Ty)) { |
| 2774 | Result = getArrayDecayedType(QualType(Ty,0)); |
| 2775 | } else if (isa<FunctionType>(Ty)) { |
| 2776 | Result = getPointerType(QualType(Ty, 0)); |
| 2777 | } else { |
| 2778 | Result = QualType(Ty, 0); |
| 2779 | } |
| 2780 | |
| 2781 | return CanQualType::CreateUnsafe(Result); |
| 2782 | } |
| 2783 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2784 | |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 2785 | QualType ASTContext::getUnqualifiedArrayType(QualType type, |
| 2786 | Qualifiers &quals) { |
| 2787 | SplitQualType splitType = type.getSplitUnqualifiedType(); |
| 2788 | |
| 2789 | // FIXME: getSplitUnqualifiedType() actually walks all the way to |
| 2790 | // the unqualified desugared type and then drops it on the floor. |
| 2791 | // We then have to strip that sugar back off with |
| 2792 | // getUnqualifiedDesugaredType(), which is silly. |
| 2793 | const ArrayType *AT = |
| 2794 | dyn_cast<ArrayType>(splitType.first->getUnqualifiedDesugaredType()); |
| 2795 | |
| 2796 | // 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] | 2797 | if (!AT) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 2798 | quals = splitType.second; |
| 2799 | return QualType(splitType.first, 0); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2800 | } |
| 2801 | |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 2802 | // Otherwise, recurse on the array's element type. |
| 2803 | QualType elementType = AT->getElementType(); |
| 2804 | QualType unqualElementType = getUnqualifiedArrayType(elementType, quals); |
| 2805 | |
| 2806 | // If that didn't change the element type, AT has no qualifiers, so we |
| 2807 | // can just use the results in splitType. |
| 2808 | if (elementType == unqualElementType) { |
| 2809 | assert(quals.empty()); // from the recursive call |
| 2810 | quals = splitType.second; |
| 2811 | return QualType(splitType.first, 0); |
| 2812 | } |
| 2813 | |
| 2814 | // Otherwise, add in the qualifiers from the outermost type, then |
| 2815 | // build the type back up. |
| 2816 | quals.addConsistentQualifiers(splitType.second); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2817 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2818 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 2819 | return getConstantArrayType(unqualElementType, CAT->getSize(), |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2820 | CAT->getSizeModifier(), 0); |
| 2821 | } |
| 2822 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2823 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 2824 | return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2825 | } |
| 2826 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2827 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 2828 | return getVariableArrayType(unqualElementType, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 2829 | VAT->getSizeExpr(), |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2830 | VAT->getSizeModifier(), |
| 2831 | VAT->getIndexTypeCVRQualifiers(), |
| 2832 | VAT->getBracketsRange()); |
| 2833 | } |
| 2834 | |
| 2835 | const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT); |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 2836 | return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(), |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2837 | DSAT->getSizeModifier(), 0, |
| 2838 | SourceRange()); |
| 2839 | } |
| 2840 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2841 | /// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that |
| 2842 | /// may be similar (C++ 4.4), replaces T1 and T2 with the type that |
| 2843 | /// they point to and return true. If T1 and T2 aren't pointer types |
| 2844 | /// or pointer-to-member types, or if they are not similar at this |
| 2845 | /// level, returns false and leaves T1 and T2 unchanged. Top-level |
| 2846 | /// qualifiers on T1 and T2 are ignored. This function will typically |
| 2847 | /// be called in a loop that successively "unwraps" pointer and |
| 2848 | /// pointer-to-member types to compare them at each level. |
| 2849 | bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) { |
| 2850 | const PointerType *T1PtrType = T1->getAs<PointerType>(), |
| 2851 | *T2PtrType = T2->getAs<PointerType>(); |
| 2852 | if (T1PtrType && T2PtrType) { |
| 2853 | T1 = T1PtrType->getPointeeType(); |
| 2854 | T2 = T2PtrType->getPointeeType(); |
| 2855 | return true; |
| 2856 | } |
| 2857 | |
| 2858 | const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(), |
| 2859 | *T2MPType = T2->getAs<MemberPointerType>(); |
| 2860 | if (T1MPType && T2MPType && |
| 2861 | hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0), |
| 2862 | QualType(T2MPType->getClass(), 0))) { |
| 2863 | T1 = T1MPType->getPointeeType(); |
| 2864 | T2 = T2MPType->getPointeeType(); |
| 2865 | return true; |
| 2866 | } |
| 2867 | |
| 2868 | if (getLangOptions().ObjC1) { |
| 2869 | const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(), |
| 2870 | *T2OPType = T2->getAs<ObjCObjectPointerType>(); |
| 2871 | if (T1OPType && T2OPType) { |
| 2872 | T1 = T1OPType->getPointeeType(); |
| 2873 | T2 = T2OPType->getPointeeType(); |
| 2874 | return true; |
| 2875 | } |
| 2876 | } |
| 2877 | |
| 2878 | // FIXME: Block pointers, too? |
| 2879 | |
| 2880 | return false; |
| 2881 | } |
| 2882 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2883 | DeclarationNameInfo |
| 2884 | ASTContext::getNameForTemplate(TemplateName Name, |
| 2885 | SourceLocation NameLoc) const { |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2886 | if (TemplateDecl *TD = Name.getAsTemplateDecl()) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2887 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 2888 | return DeclarationNameInfo(TD->getDeclName(), NameLoc); |
| 2889 | |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2890 | if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2891 | DeclarationName DName; |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2892 | if (DTN->isIdentifier()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2893 | DName = DeclarationNames.getIdentifier(DTN->getIdentifier()); |
| 2894 | return DeclarationNameInfo(DName, NameLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2895 | } else { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2896 | DName = DeclarationNames.getCXXOperatorName(DTN->getOperator()); |
| 2897 | // DNInfo work in progress: FIXME: source locations? |
| 2898 | DeclarationNameLoc DNLoc; |
| 2899 | DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding(); |
| 2900 | DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding(); |
| 2901 | return DeclarationNameInfo(DName, NameLoc, DNLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2902 | } |
| 2903 | } |
| 2904 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 2905 | OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate(); |
| 2906 | assert(Storage); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2907 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 2908 | return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2909 | } |
| 2910 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2911 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const { |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 2912 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 2913 | if (TemplateTemplateParmDecl *TTP |
| 2914 | = dyn_cast<TemplateTemplateParmDecl>(Template)) |
| 2915 | Template = getCanonicalTemplateTemplateParmDecl(TTP); |
| 2916 | |
| 2917 | // The canonical template name is the canonical template declaration. |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 2918 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 2919 | } |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2920 | |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 2921 | if (SubstTemplateTemplateParmPackStorage *SubstPack |
| 2922 | = Name.getAsSubstTemplateTemplateParmPack()) { |
| 2923 | TemplateTemplateParmDecl *CanonParam |
| 2924 | = getCanonicalTemplateTemplateParmDecl(SubstPack->getParameterPack()); |
| 2925 | TemplateArgument CanonArgPack |
| 2926 | = getCanonicalTemplateArgument(SubstPack->getArgumentPack()); |
| 2927 | return getSubstTemplateTemplateParmPack(CanonParam, CanonArgPack); |
| 2928 | } |
| 2929 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 2930 | assert(!Name.getAsOverloadedTemplate()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2931 | |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2932 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 2933 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 2934 | return DTN->CanonicalTemplateName; |
| 2935 | } |
| 2936 | |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 2937 | bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) { |
| 2938 | X = getCanonicalTemplateName(X); |
| 2939 | Y = getCanonicalTemplateName(Y); |
| 2940 | return X.getAsVoidPointer() == Y.getAsVoidPointer(); |
| 2941 | } |
| 2942 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2943 | TemplateArgument |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2944 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const { |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2945 | switch (Arg.getKind()) { |
| 2946 | case TemplateArgument::Null: |
| 2947 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2948 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2949 | case TemplateArgument::Expression: |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2950 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2951 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2952 | case TemplateArgument::Declaration: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2953 | return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2954 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2955 | case TemplateArgument::Template: |
| 2956 | return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate())); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2957 | |
| 2958 | case TemplateArgument::TemplateExpansion: |
| 2959 | return TemplateArgument(getCanonicalTemplateName( |
| 2960 | Arg.getAsTemplateOrTemplatePattern()), |
Douglas Gregor | 2be29f4 | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 2961 | Arg.getNumTemplateExpansions()); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2962 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2963 | case TemplateArgument::Integral: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2964 | return TemplateArgument(*Arg.getAsIntegral(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2965 | getCanonicalType(Arg.getIntegralType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2966 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2967 | case TemplateArgument::Type: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2968 | return TemplateArgument(getCanonicalType(Arg.getAsType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2969 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2970 | case TemplateArgument::Pack: { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 2971 | if (Arg.pack_size() == 0) |
| 2972 | return Arg; |
| 2973 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2974 | TemplateArgument *CanonArgs |
| 2975 | = new (*this) TemplateArgument[Arg.pack_size()]; |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2976 | unsigned Idx = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2977 | for (TemplateArgument::pack_iterator A = Arg.pack_begin(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2978 | AEnd = Arg.pack_end(); |
| 2979 | A != AEnd; (void)++A, ++Idx) |
| 2980 | CanonArgs[Idx] = getCanonicalTemplateArgument(*A); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2981 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2982 | return TemplateArgument(CanonArgs, Arg.pack_size()); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2983 | } |
| 2984 | } |
| 2985 | |
| 2986 | // Silence GCC warning |
| 2987 | assert(false && "Unhandled template argument kind"); |
| 2988 | return TemplateArgument(); |
| 2989 | } |
| 2990 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2991 | NestedNameSpecifier * |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2992 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2993 | if (!NNS) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2994 | return 0; |
| 2995 | |
| 2996 | switch (NNS->getKind()) { |
| 2997 | case NestedNameSpecifier::Identifier: |
| 2998 | // Canonicalize the prefix but keep the identifier the same. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2999 | return NestedNameSpecifier::Create(*this, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3000 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 3001 | NNS->getAsIdentifier()); |
| 3002 | |
| 3003 | case NestedNameSpecifier::Namespace: |
| 3004 | // A namespace is canonical; build a nested-name-specifier with |
| 3005 | // this namespace and no prefix. |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 3006 | return NestedNameSpecifier::Create(*this, 0, |
| 3007 | NNS->getAsNamespace()->getOriginalNamespace()); |
| 3008 | |
| 3009 | case NestedNameSpecifier::NamespaceAlias: |
| 3010 | // A namespace is canonical; build a nested-name-specifier with |
| 3011 | // this namespace and no prefix. |
| 3012 | return NestedNameSpecifier::Create(*this, 0, |
| 3013 | NNS->getAsNamespaceAlias()->getNamespace() |
| 3014 | ->getOriginalNamespace()); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3015 | |
| 3016 | case NestedNameSpecifier::TypeSpec: |
| 3017 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 3018 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3019 | |
| 3020 | // If we have some kind of dependent-named type (e.g., "typename T::type"), |
| 3021 | // break it apart into its prefix and identifier, then reconsititute those |
| 3022 | // as the canonical nested-name-specifier. This is required to canonicalize |
| 3023 | // a dependent nested-name-specifier involving typedefs of dependent-name |
| 3024 | // types, e.g., |
| 3025 | // typedef typename T::type T1; |
| 3026 | // typedef typename T1::type T2; |
| 3027 | if (const DependentNameType *DNT = T->getAs<DependentNameType>()) { |
| 3028 | NestedNameSpecifier *Prefix |
| 3029 | = getCanonicalNestedNameSpecifier(DNT->getQualifier()); |
| 3030 | return NestedNameSpecifier::Create(*this, Prefix, |
| 3031 | const_cast<IdentifierInfo *>(DNT->getIdentifier())); |
| 3032 | } |
| 3033 | |
Douglas Gregor | 643f843 | 2010-11-04 00:14:23 +0000 | [diff] [blame] | 3034 | // Do the same thing as above, but with dependent-named specializations. |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3035 | if (const DependentTemplateSpecializationType *DTST |
| 3036 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 3037 | NestedNameSpecifier *Prefix |
| 3038 | = getCanonicalNestedNameSpecifier(DTST->getQualifier()); |
Douglas Gregor | aa2187d | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 3039 | |
| 3040 | T = getDependentTemplateSpecializationType(DTST->getKeyword(), |
| 3041 | Prefix, DTST->getIdentifier(), |
| 3042 | DTST->getNumArgs(), |
| 3043 | DTST->getArgs()); |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3044 | T = getCanonicalType(T); |
| 3045 | } |
| 3046 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3047 | return NestedNameSpecifier::Create(*this, 0, false, |
| 3048 | const_cast<Type*>(T.getTypePtr())); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3049 | } |
| 3050 | |
| 3051 | case NestedNameSpecifier::Global: |
| 3052 | // The global specifier is canonical and unique. |
| 3053 | return NNS; |
| 3054 | } |
| 3055 | |
| 3056 | // Required to silence a GCC warning |
| 3057 | return 0; |
| 3058 | } |
| 3059 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3060 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3061 | const ArrayType *ASTContext::getAsArrayType(QualType T) const { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3062 | // Handle the non-qualified case efficiently. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3063 | if (!T.hasLocalQualifiers()) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3064 | // Handle the common positive case fast. |
| 3065 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 3066 | return AT; |
| 3067 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3068 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3069 | // Handle the common negative case fast. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3070 | if (!isa<ArrayType>(T.getCanonicalType())) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3071 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3072 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3073 | // Apply any qualifiers from the array type to the element type. This |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3074 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 3075 | // 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] | 3076 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3077 | // If we get here, we either have type qualifiers on the type, or we have |
| 3078 | // 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] | 3079 | // we must propagate them down into the element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3080 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3081 | SplitQualType split = T.getSplitDesugaredType(); |
| 3082 | Qualifiers qs = split.second; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3083 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3084 | // If we have a simple case, just return now. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3085 | const ArrayType *ATy = dyn_cast<ArrayType>(split.first); |
| 3086 | if (ATy == 0 || qs.empty()) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3087 | return ATy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3088 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3089 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 3090 | // qualifiers into the array element type and return a new array type. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3091 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3092 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3093 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 3094 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 3095 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3096 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3097 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 3098 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 3099 | IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3100 | IAT->getIndexTypeCVRQualifiers())); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3101 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3102 | if (const DependentSizedArrayType *DSAT |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3103 | = dyn_cast<DependentSizedArrayType>(ATy)) |
| 3104 | return cast<ArrayType>( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3105 | getDependentSizedArrayType(NewEltTy, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3106 | DSAT->getSizeExpr(), |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3107 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3108 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3109 | DSAT->getBracketsRange())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3110 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3111 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3112 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3113 | VAT->getSizeExpr(), |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3114 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3115 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3116 | VAT->getBracketsRange())); |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 3117 | } |
| 3118 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3119 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 3120 | /// specified array type to a pointer. This operation is non-trivial when |
| 3121 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 3122 | /// this returns a pointer to a properly qualified element of the array. |
| 3123 | /// |
| 3124 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3125 | QualType ASTContext::getArrayDecayedType(QualType Ty) const { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3126 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 3127 | // typedefs in the element type of the array. This also handles propagation |
| 3128 | // of type qualifiers from the array type into the element type if present |
| 3129 | // (C99 6.7.3p8). |
| 3130 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 3131 | assert(PrettyArrayType && "Not an array type!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3132 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3133 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3134 | |
| 3135 | // int x[restrict 4] -> int *restrict |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3136 | return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3137 | } |
| 3138 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3139 | QualType ASTContext::getBaseElementType(const ArrayType *array) const { |
| 3140 | return getBaseElementType(array->getElementType()); |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 3141 | } |
| 3142 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3143 | QualType ASTContext::getBaseElementType(QualType type) const { |
| 3144 | Qualifiers qs; |
| 3145 | while (true) { |
| 3146 | SplitQualType split = type.getSplitDesugaredType(); |
| 3147 | const ArrayType *array = split.first->getAsArrayTypeUnsafe(); |
| 3148 | if (!array) break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3149 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3150 | type = array->getElementType(); |
| 3151 | qs.addConsistentQualifiers(split.second); |
| 3152 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3153 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3154 | return getQualifiedType(type, qs); |
Anders Carlsson | 6183a99 | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 3155 | } |
| 3156 | |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 3157 | /// getConstantArrayElementCount - Returns number of constant array elements. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3158 | uint64_t |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 3159 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
| 3160 | uint64_t ElementCount = 1; |
| 3161 | do { |
| 3162 | ElementCount *= CA->getSize().getZExtValue(); |
| 3163 | CA = dyn_cast<ConstantArrayType>(CA->getElementType()); |
| 3164 | } while (CA); |
| 3165 | return ElementCount; |
| 3166 | } |
| 3167 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3168 | /// getFloatingRank - Return a relative rank for floating point types. |
| 3169 | /// 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] | 3170 | static FloatingRank getFloatingRank(QualType T) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3171 | if (const ComplexType *CT = T->getAs<ComplexType>()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3172 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3173 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3174 | assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"); |
| 3175 | switch (T->getAs<BuiltinType>()->getKind()) { |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3176 | default: assert(0 && "getFloatingRank(): not a floating type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3177 | case BuiltinType::Float: return FloatRank; |
| 3178 | case BuiltinType::Double: return DoubleRank; |
| 3179 | case BuiltinType::LongDouble: return LongDoubleRank; |
| 3180 | } |
| 3181 | } |
| 3182 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3183 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 3184 | /// point or a complex type (based on typeDomain/typeSize). |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 3185 | /// 'typeDomain' is a real floating point or complex type. |
| 3186 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 3187 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 3188 | QualType Domain) const { |
| 3189 | FloatingRank EltRank = getFloatingRank(Size); |
| 3190 | if (Domain->isComplexType()) { |
| 3191 | switch (EltRank) { |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 3192 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 3193 | case FloatRank: return FloatComplexTy; |
| 3194 | case DoubleRank: return DoubleComplexTy; |
| 3195 | case LongDoubleRank: return LongDoubleComplexTy; |
| 3196 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3197 | } |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 3198 | |
| 3199 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 3200 | switch (EltRank) { |
| 3201 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
| 3202 | case FloatRank: return FloatTy; |
| 3203 | case DoubleRank: return DoubleTy; |
| 3204 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 3205 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3206 | } |
| 3207 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3208 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 3209 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 3210 | /// '_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] | 3211 | /// LHS < RHS, return -1. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3212 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const { |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3213 | FloatingRank LHSR = getFloatingRank(LHS); |
| 3214 | FloatingRank RHSR = getFloatingRank(RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3215 | |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3216 | if (LHSR == RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 3217 | return 0; |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3218 | if (LHSR > RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 3219 | return 1; |
| 3220 | return -1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3221 | } |
| 3222 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3223 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 3224 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 3225 | /// or if it is not canonicalized. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3226 | unsigned ASTContext::getIntegerRank(const Type *T) const { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 3227 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3228 | if (const EnumType* ET = dyn_cast<EnumType>(T)) |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 3229 | T = ET->getDecl()->getPromotionType().getTypePtr(); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3230 | |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 3231 | if (T->isSpecificBuiltinType(BuiltinType::WChar_S) || |
| 3232 | T->isSpecificBuiltinType(BuiltinType::WChar_U)) |
Eli Friedman | a342675 | 2009-07-05 23:44:27 +0000 | [diff] [blame] | 3233 | T = getFromTargetType(Target.getWCharType()).getTypePtr(); |
| 3234 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3235 | if (T->isSpecificBuiltinType(BuiltinType::Char16)) |
| 3236 | T = getFromTargetType(Target.getChar16Type()).getTypePtr(); |
| 3237 | |
| 3238 | if (T->isSpecificBuiltinType(BuiltinType::Char32)) |
| 3239 | T = getFromTargetType(Target.getChar32Type()).getTypePtr(); |
| 3240 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3241 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3242 | default: assert(0 && "getIntegerRank(): not a built-in integer"); |
| 3243 | case BuiltinType::Bool: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3244 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3245 | case BuiltinType::Char_S: |
| 3246 | case BuiltinType::Char_U: |
| 3247 | case BuiltinType::SChar: |
| 3248 | case BuiltinType::UChar: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3249 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3250 | case BuiltinType::Short: |
| 3251 | case BuiltinType::UShort: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3252 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3253 | case BuiltinType::Int: |
| 3254 | case BuiltinType::UInt: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3255 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3256 | case BuiltinType::Long: |
| 3257 | case BuiltinType::ULong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3258 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3259 | case BuiltinType::LongLong: |
| 3260 | case BuiltinType::ULongLong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3261 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 3262 | case BuiltinType::Int128: |
| 3263 | case BuiltinType::UInt128: |
| 3264 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3265 | } |
| 3266 | } |
| 3267 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3268 | /// \brief Whether this is a promotable bitfield reference according |
| 3269 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
| 3270 | /// |
| 3271 | /// \returns the type this bit-field will promote to, or NULL if no |
| 3272 | /// promotion occurs. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3273 | QualType ASTContext::isPromotableBitField(Expr *E) const { |
Douglas Gregor | ceafbde | 2010-05-24 20:13:53 +0000 | [diff] [blame] | 3274 | if (E->isTypeDependent() || E->isValueDependent()) |
| 3275 | return QualType(); |
| 3276 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3277 | FieldDecl *Field = E->getBitField(); |
| 3278 | if (!Field) |
| 3279 | return QualType(); |
| 3280 | |
| 3281 | QualType FT = Field->getType(); |
| 3282 | |
| 3283 | llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this); |
| 3284 | uint64_t BitWidth = BitWidthAP.getZExtValue(); |
| 3285 | uint64_t IntSize = getTypeSize(IntTy); |
| 3286 | // GCC extension compatibility: if the bit-field size is less than or equal |
| 3287 | // to the size of int, it gets promoted no matter what its type is. |
| 3288 | // For instance, unsigned long bf : 4 gets promoted to signed int. |
| 3289 | if (BitWidth < IntSize) |
| 3290 | return IntTy; |
| 3291 | |
| 3292 | if (BitWidth == IntSize) |
| 3293 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
| 3294 | |
| 3295 | // Types bigger than int are not subject to promotions, and therefore act |
| 3296 | // like the base type. |
| 3297 | // FIXME: This doesn't quite match what gcc does, but what gcc does here |
| 3298 | // is ridiculous. |
| 3299 | return QualType(); |
| 3300 | } |
| 3301 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3302 | /// getPromotedIntegerType - Returns the type that Promotable will |
| 3303 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
| 3304 | /// integer type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3305 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) const { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3306 | assert(!Promotable.isNull()); |
| 3307 | assert(Promotable->isPromotableIntegerType()); |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 3308 | if (const EnumType *ET = Promotable->getAs<EnumType>()) |
| 3309 | return ET->getDecl()->getPromotionType(); |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3310 | if (Promotable->isSignedIntegerType()) |
| 3311 | return IntTy; |
| 3312 | uint64_t PromotableSize = getTypeSize(Promotable); |
| 3313 | uint64_t IntSize = getTypeSize(IntTy); |
| 3314 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
| 3315 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
| 3316 | } |
| 3317 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3318 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3319 | /// 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] | 3320 | /// LHS < RHS, return -1. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3321 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3322 | const Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 3323 | const Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3324 | if (LHSC == RHSC) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3325 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3326 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 3327 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3328 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3329 | unsigned LHSRank = getIntegerRank(LHSC); |
| 3330 | unsigned RHSRank = getIntegerRank(RHSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3331 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3332 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 3333 | if (LHSRank == RHSRank) return 0; |
| 3334 | return LHSRank > RHSRank ? 1 : -1; |
| 3335 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3336 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3337 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 3338 | if (LHSUnsigned) { |
| 3339 | // If the unsigned [LHS] type is larger, return it. |
| 3340 | if (LHSRank >= RHSRank) |
| 3341 | return 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3342 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3343 | // If the signed type can represent all values of the unsigned type, it |
| 3344 | // wins. Because we are dealing with 2's complement and types that are |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3345 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3346 | return -1; |
| 3347 | } |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3348 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3349 | // If the unsigned [RHS] type is larger, return it. |
| 3350 | if (RHSRank >= LHSRank) |
| 3351 | return -1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3352 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3353 | // If the signed type can represent all values of the unsigned type, it |
| 3354 | // 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] | 3355 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3356 | return 1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3357 | } |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3358 | |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3359 | static RecordDecl * |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3360 | CreateRecordDecl(const ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC, |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3361 | SourceLocation L, IdentifierInfo *Id) { |
| 3362 | if (Ctx.getLangOptions().CPlusPlus) |
| 3363 | return CXXRecordDecl::Create(Ctx, TK, DC, L, Id); |
| 3364 | else |
| 3365 | return RecordDecl::Create(Ctx, TK, DC, L, Id); |
| 3366 | } |
| 3367 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3368 | // getCFConstantStringType - Return the type used for constant CFStrings. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3369 | QualType ASTContext::getCFConstantStringType() const { |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3370 | if (!CFConstantStringTypeDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3371 | CFConstantStringTypeDecl = |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3372 | CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3373 | &Idents.get("NSConstantString")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3374 | CFConstantStringTypeDecl->startDefinition(); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3375 | |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 3376 | QualType FieldTypes[4]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3377 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3378 | // const int *isa; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3379 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 3380 | // int flags; |
| 3381 | FieldTypes[1] = IntTy; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3382 | // const char *str; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3383 | FieldTypes[2] = getPointerType(CharTy.withConst()); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3384 | // long length; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3385 | FieldTypes[3] = LongTy; |
| 3386 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3387 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3388 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3389 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3390 | SourceLocation(), 0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3391 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3392 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 3393 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3394 | Field->setAccess(AS_public); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3395 | CFConstantStringTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3396 | } |
| 3397 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3398 | CFConstantStringTypeDecl->completeDefinition(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3399 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3400 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3401 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 8467583 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 3402 | } |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 3403 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3404 | void ASTContext::setCFConstantStringType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3405 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3406 | assert(Rec && "Invalid CFConstantStringType"); |
| 3407 | CFConstantStringTypeDecl = Rec->getDecl(); |
| 3408 | } |
| 3409 | |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 3410 | // getNSConstantStringType - Return the type used for constant NSStrings. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3411 | QualType ASTContext::getNSConstantStringType() const { |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 3412 | if (!NSConstantStringTypeDecl) { |
| 3413 | NSConstantStringTypeDecl = |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3414 | CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 3415 | &Idents.get("__builtin_NSString")); |
| 3416 | NSConstantStringTypeDecl->startDefinition(); |
| 3417 | |
| 3418 | QualType FieldTypes[3]; |
| 3419 | |
| 3420 | // const int *isa; |
| 3421 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
| 3422 | // const char *str; |
| 3423 | FieldTypes[1] = getPointerType(CharTy.withConst()); |
| 3424 | // unsigned int length; |
| 3425 | FieldTypes[2] = UnsignedIntTy; |
| 3426 | |
| 3427 | // Create fields |
| 3428 | for (unsigned i = 0; i < 3; ++i) { |
| 3429 | FieldDecl *Field = FieldDecl::Create(*this, NSConstantStringTypeDecl, |
| 3430 | SourceLocation(), 0, |
| 3431 | FieldTypes[i], /*TInfo=*/0, |
| 3432 | /*BitWidth=*/0, |
| 3433 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3434 | Field->setAccess(AS_public); |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 3435 | NSConstantStringTypeDecl->addDecl(Field); |
| 3436 | } |
| 3437 | |
| 3438 | NSConstantStringTypeDecl->completeDefinition(); |
| 3439 | } |
| 3440 | |
| 3441 | return getTagDeclType(NSConstantStringTypeDecl); |
| 3442 | } |
| 3443 | |
| 3444 | void ASTContext::setNSConstantStringType(QualType T) { |
| 3445 | const RecordType *Rec = T->getAs<RecordType>(); |
| 3446 | assert(Rec && "Invalid NSConstantStringType"); |
| 3447 | NSConstantStringTypeDecl = Rec->getDecl(); |
| 3448 | } |
| 3449 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3450 | QualType ASTContext::getObjCFastEnumerationStateType() const { |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3451 | if (!ObjCFastEnumerationStateTypeDecl) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3452 | ObjCFastEnumerationStateTypeDecl = |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3453 | CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3454 | &Idents.get("__objcFastEnumerationState")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3455 | ObjCFastEnumerationStateTypeDecl->startDefinition(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3456 | |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3457 | QualType FieldTypes[] = { |
| 3458 | UnsignedLongTy, |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3459 | getPointerType(ObjCIdTypedefType), |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3460 | getPointerType(UnsignedLongTy), |
| 3461 | getConstantArrayType(UnsignedLongTy, |
| 3462 | llvm::APInt(32, 5), ArrayType::Normal, 0) |
| 3463 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3464 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3465 | for (size_t i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3466 | FieldDecl *Field = FieldDecl::Create(*this, |
| 3467 | ObjCFastEnumerationStateTypeDecl, |
| 3468 | SourceLocation(), 0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3469 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3470 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 3471 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3472 | Field->setAccess(AS_public); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3473 | ObjCFastEnumerationStateTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3474 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3475 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3476 | ObjCFastEnumerationStateTypeDecl->completeDefinition(); |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3477 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3478 | |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3479 | return getTagDeclType(ObjCFastEnumerationStateTypeDecl); |
| 3480 | } |
| 3481 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3482 | QualType ASTContext::getBlockDescriptorType() const { |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3483 | if (BlockDescriptorType) |
| 3484 | return getTagDeclType(BlockDescriptorType); |
| 3485 | |
| 3486 | RecordDecl *T; |
| 3487 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3488 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3489 | &Idents.get("__block_descriptor")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3490 | T->startDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3491 | |
| 3492 | QualType FieldTypes[] = { |
| 3493 | UnsignedLongTy, |
| 3494 | UnsignedLongTy, |
| 3495 | }; |
| 3496 | |
| 3497 | const char *FieldNames[] = { |
| 3498 | "reserved", |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3499 | "Size" |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3500 | }; |
| 3501 | |
| 3502 | for (size_t i = 0; i < 2; ++i) { |
| 3503 | FieldDecl *Field = FieldDecl::Create(*this, |
| 3504 | T, |
| 3505 | SourceLocation(), |
| 3506 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3507 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3508 | /*BitWidth=*/0, |
| 3509 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3510 | Field->setAccess(AS_public); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3511 | T->addDecl(Field); |
| 3512 | } |
| 3513 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3514 | T->completeDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3515 | |
| 3516 | BlockDescriptorType = T; |
| 3517 | |
| 3518 | return getTagDeclType(BlockDescriptorType); |
| 3519 | } |
| 3520 | |
| 3521 | void ASTContext::setBlockDescriptorType(QualType T) { |
| 3522 | const RecordType *Rec = T->getAs<RecordType>(); |
| 3523 | assert(Rec && "Invalid BlockDescriptorType"); |
| 3524 | BlockDescriptorType = Rec->getDecl(); |
| 3525 | } |
| 3526 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3527 | QualType ASTContext::getBlockDescriptorExtendedType() const { |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3528 | if (BlockDescriptorExtendedType) |
| 3529 | return getTagDeclType(BlockDescriptorExtendedType); |
| 3530 | |
| 3531 | RecordDecl *T; |
| 3532 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3533 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3534 | &Idents.get("__block_descriptor_withcopydispose")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3535 | T->startDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3536 | |
| 3537 | QualType FieldTypes[] = { |
| 3538 | UnsignedLongTy, |
| 3539 | UnsignedLongTy, |
| 3540 | getPointerType(VoidPtrTy), |
| 3541 | getPointerType(VoidPtrTy) |
| 3542 | }; |
| 3543 | |
| 3544 | const char *FieldNames[] = { |
| 3545 | "reserved", |
| 3546 | "Size", |
| 3547 | "CopyFuncPtr", |
| 3548 | "DestroyFuncPtr" |
| 3549 | }; |
| 3550 | |
| 3551 | for (size_t i = 0; i < 4; ++i) { |
| 3552 | FieldDecl *Field = FieldDecl::Create(*this, |
| 3553 | T, |
| 3554 | SourceLocation(), |
| 3555 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3556 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3557 | /*BitWidth=*/0, |
| 3558 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3559 | Field->setAccess(AS_public); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3560 | T->addDecl(Field); |
| 3561 | } |
| 3562 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3563 | T->completeDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3564 | |
| 3565 | BlockDescriptorExtendedType = T; |
| 3566 | |
| 3567 | return getTagDeclType(BlockDescriptorExtendedType); |
| 3568 | } |
| 3569 | |
| 3570 | void ASTContext::setBlockDescriptorExtendedType(QualType T) { |
| 3571 | const RecordType *Rec = T->getAs<RecordType>(); |
| 3572 | assert(Rec && "Invalid BlockDescriptorType"); |
| 3573 | BlockDescriptorExtendedType = Rec->getDecl(); |
| 3574 | } |
| 3575 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3576 | bool ASTContext::BlockRequiresCopying(QualType Ty) const { |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3577 | if (Ty->isBlockPointerType()) |
| 3578 | return true; |
| 3579 | if (isObjCNSObjectType(Ty)) |
| 3580 | return true; |
| 3581 | if (Ty->isObjCObjectPointerType()) |
| 3582 | return true; |
Fariborz Jahanian | e38be61 | 2010-11-17 00:21:28 +0000 | [diff] [blame] | 3583 | if (getLangOptions().CPlusPlus) { |
| 3584 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3585 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 3586 | return RD->hasConstCopyConstructor(*this); |
| 3587 | |
| 3588 | } |
| 3589 | } |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3590 | return false; |
| 3591 | } |
| 3592 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3593 | QualType |
| 3594 | ASTContext::BuildByRefType(llvm::StringRef DeclName, QualType Ty) const { |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3595 | // type = struct __Block_byref_1_X { |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3596 | // void *__isa; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3597 | // struct __Block_byref_1_X *__forwarding; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3598 | // unsigned int __flags; |
| 3599 | // unsigned int __size; |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 3600 | // void *__copy_helper; // as needed |
| 3601 | // void *__destroy_help // as needed |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3602 | // int X; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3603 | // } * |
| 3604 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3605 | bool HasCopyAndDispose = BlockRequiresCopying(Ty); |
| 3606 | |
| 3607 | // FIXME: Move up |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 3608 | llvm::SmallString<36> Name; |
| 3609 | llvm::raw_svector_ostream(Name) << "__Block_byref_" << |
| 3610 | ++UniqueBlockByRefTypeID << '_' << DeclName; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3611 | RecordDecl *T; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3612 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3613 | &Idents.get(Name.str())); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3614 | T->startDefinition(); |
| 3615 | QualType Int32Ty = IntTy; |
| 3616 | assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported"); |
| 3617 | QualType FieldTypes[] = { |
| 3618 | getPointerType(VoidPtrTy), |
| 3619 | getPointerType(getTagDeclType(T)), |
| 3620 | Int32Ty, |
| 3621 | Int32Ty, |
| 3622 | getPointerType(VoidPtrTy), |
| 3623 | getPointerType(VoidPtrTy), |
| 3624 | Ty |
| 3625 | }; |
| 3626 | |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3627 | llvm::StringRef FieldNames[] = { |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3628 | "__isa", |
| 3629 | "__forwarding", |
| 3630 | "__flags", |
| 3631 | "__size", |
| 3632 | "__copy_helper", |
| 3633 | "__destroy_helper", |
| 3634 | DeclName, |
| 3635 | }; |
| 3636 | |
| 3637 | for (size_t i = 0; i < 7; ++i) { |
| 3638 | if (!HasCopyAndDispose && i >=4 && i <= 5) |
| 3639 | continue; |
| 3640 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
| 3641 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3642 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3643 | /*BitWidth=*/0, /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3644 | Field->setAccess(AS_public); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3645 | T->addDecl(Field); |
| 3646 | } |
| 3647 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3648 | T->completeDefinition(); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3649 | |
| 3650 | return getPointerType(getTagDeclType(T)); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3651 | } |
| 3652 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3653 | void ASTContext::setObjCFastEnumerationStateType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3654 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3655 | assert(Rec && "Invalid ObjCFAstEnumerationStateType"); |
| 3656 | ObjCFastEnumerationStateTypeDecl = Rec->getDecl(); |
| 3657 | } |
| 3658 | |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3659 | // This returns true if a type has been typedefed to BOOL: |
| 3660 | // typedef <type> BOOL; |
Chris Lattner | 2d99833 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 3661 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3662 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | bb49c3e | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 3663 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 3664 | return II->isStr("BOOL"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3665 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3666 | return false; |
| 3667 | } |
| 3668 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3669 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3670 | /// purpose. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3671 | CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const { |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3672 | CharUnits sz = getTypeSizeInChars(type); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3673 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3674 | // 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] | 3675 | if (sz.isPositive() && type->isIntegralOrEnumerationType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3676 | sz = std::max(sz, getTypeSizeInChars(IntTy)); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3677 | // Treat arrays as pointers, since that's how they're passed in. |
| 3678 | else if (type->isArrayType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3679 | sz = getTypeSizeInChars(VoidPtrTy); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3680 | return sz; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3681 | } |
| 3682 | |
| 3683 | static inline |
| 3684 | std::string charUnitsToString(const CharUnits &CU) { |
| 3685 | return llvm::itostr(CU.getQuantity()); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3686 | } |
| 3687 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 3688 | /// getObjCEncodingForBlock - Return the encoded type for this block |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3689 | /// declaration. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 3690 | std::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const { |
| 3691 | std::string S; |
| 3692 | |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3693 | const BlockDecl *Decl = Expr->getBlockDecl(); |
| 3694 | QualType BlockTy = |
| 3695 | Expr->getType()->getAs<BlockPointerType>()->getPointeeType(); |
| 3696 | // Encode result type. |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 3697 | getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3698 | // Compute size of all parameters. |
| 3699 | // Start with computing size of a pointer in number of bytes. |
| 3700 | // FIXME: There might(should) be a better way of doing this computation! |
| 3701 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3702 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
| 3703 | CharUnits ParmOffset = PtrSize; |
Fariborz Jahanian | 6f46c26 | 2010-04-08 18:06:22 +0000 | [diff] [blame] | 3704 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3705 | E = Decl->param_end(); PI != E; ++PI) { |
| 3706 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3707 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3708 | assert (sz.isPositive() && "BlockExpr - Incomplete param type"); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3709 | ParmOffset += sz; |
| 3710 | } |
| 3711 | // Size of the argument frame |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3712 | S += charUnitsToString(ParmOffset); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3713 | // Block pointer and offset. |
| 3714 | S += "@?0"; |
| 3715 | ParmOffset = PtrSize; |
| 3716 | |
| 3717 | // Argument types. |
| 3718 | ParmOffset = PtrSize; |
| 3719 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E = |
| 3720 | Decl->param_end(); PI != E; ++PI) { |
| 3721 | ParmVarDecl *PVDecl = *PI; |
| 3722 | QualType PType = PVDecl->getOriginalType(); |
| 3723 | if (const ArrayType *AT = |
| 3724 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3725 | // Use array's original type only if it has known number of |
| 3726 | // elements. |
| 3727 | if (!isa<ConstantArrayType>(AT)) |
| 3728 | PType = PVDecl->getType(); |
| 3729 | } else if (PType->isFunctionType()) |
| 3730 | PType = PVDecl->getType(); |
| 3731 | getObjCEncodingForType(PType, S); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3732 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3733 | ParmOffset += getObjCEncodingTypeSize(PType); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3734 | } |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 3735 | |
| 3736 | return S; |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3737 | } |
| 3738 | |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 3739 | void ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl, |
| 3740 | std::string& S) { |
| 3741 | // Encode result type. |
| 3742 | getObjCEncodingForType(Decl->getResultType(), S); |
| 3743 | CharUnits ParmOffset; |
| 3744 | // Compute size of all parameters. |
| 3745 | for (FunctionDecl::param_const_iterator PI = Decl->param_begin(), |
| 3746 | E = Decl->param_end(); PI != E; ++PI) { |
| 3747 | QualType PType = (*PI)->getType(); |
| 3748 | CharUnits sz = getObjCEncodingTypeSize(PType); |
| 3749 | assert (sz.isPositive() && |
| 3750 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
| 3751 | ParmOffset += sz; |
| 3752 | } |
| 3753 | S += charUnitsToString(ParmOffset); |
| 3754 | ParmOffset = CharUnits::Zero(); |
| 3755 | |
| 3756 | // Argument types. |
| 3757 | for (FunctionDecl::param_const_iterator PI = Decl->param_begin(), |
| 3758 | E = Decl->param_end(); PI != E; ++PI) { |
| 3759 | ParmVarDecl *PVDecl = *PI; |
| 3760 | QualType PType = PVDecl->getOriginalType(); |
| 3761 | if (const ArrayType *AT = |
| 3762 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3763 | // Use array's original type only if it has known number of |
| 3764 | // elements. |
| 3765 | if (!isa<ConstantArrayType>(AT)) |
| 3766 | PType = PVDecl->getType(); |
| 3767 | } else if (PType->isFunctionType()) |
| 3768 | PType = PVDecl->getType(); |
| 3769 | getObjCEncodingForType(PType, S); |
| 3770 | S += charUnitsToString(ParmOffset); |
| 3771 | ParmOffset += getObjCEncodingTypeSize(PType); |
| 3772 | } |
| 3773 | } |
| 3774 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3775 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3776 | /// declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3777 | void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3778 | std::string& S) const { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3779 | // FIXME: This is not very efficient. |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3780 | // Encode type qualifer, 'in', 'inout', etc. for the return type. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3781 | getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3782 | // Encode result type. |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3783 | getObjCEncodingForType(Decl->getResultType(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3784 | // Compute size of all parameters. |
| 3785 | // Start with computing size of a pointer in number of bytes. |
| 3786 | // FIXME: There might(should) be a better way of doing this computation! |
| 3787 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3788 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3789 | // The first two arguments (self and _cmd) are pointers; account for |
| 3790 | // their size. |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3791 | CharUnits ParmOffset = 2 * PtrSize; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3792 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 3793 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3794 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3795 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3796 | assert (sz.isPositive() && |
| 3797 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3798 | ParmOffset += sz; |
| 3799 | } |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3800 | S += charUnitsToString(ParmOffset); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3801 | S += "@0:"; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3802 | S += charUnitsToString(PtrSize); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3803 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3804 | // Argument types. |
| 3805 | ParmOffset = 2 * PtrSize; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3806 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 3807 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3808 | ParmVarDecl *PVDecl = *PI; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3809 | QualType PType = PVDecl->getOriginalType(); |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 3810 | if (const ArrayType *AT = |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 3811 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3812 | // Use array's original type only if it has known number of |
| 3813 | // elements. |
Steve Naroff | bb3fde3 | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 3814 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 3815 | PType = PVDecl->getType(); |
| 3816 | } else if (PType->isFunctionType()) |
| 3817 | PType = PVDecl->getType(); |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3818 | // Process argument qualifiers for user supplied arguments; such as, |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3819 | // 'in', 'inout', etc. |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 3820 | getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3821 | getObjCEncodingForType(PType, S); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3822 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3823 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3824 | } |
| 3825 | } |
| 3826 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3827 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 3828 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3829 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 3830 | /// NULL when getting encodings for protocol properties. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3831 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 3832 | /// attributes readonly and bycopy are encoded as single characters. The |
| 3833 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 3834 | /// encoded as single characters, followed by an identifier. Property types |
| 3835 | /// are also encoded as a parametrized attribute. The characters used to encode |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 3836 | /// these attributes are defined by the following enumeration: |
| 3837 | /// @code |
| 3838 | /// enum PropertyAttributes { |
| 3839 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 3840 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 3841 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 3842 | /// kPropertyDynamic = 'D', // property is dynamic |
| 3843 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 3844 | /// kPropertySetter = 'S', // followed by setter selector name |
| 3845 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
| 3846 | /// kPropertyType = 't' // followed by old-style type encoding. |
| 3847 | /// kPropertyWeak = 'W' // 'weak' property |
| 3848 | /// kPropertyStrong = 'P' // property GC'able |
| 3849 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 3850 | /// }; |
| 3851 | /// @endcode |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3852 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3853 | const Decl *Container, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3854 | std::string& S) const { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3855 | // Collect information from the property implementation decl(s). |
| 3856 | bool Dynamic = false; |
| 3857 | ObjCPropertyImplDecl *SynthesizePID = 0; |
| 3858 | |
| 3859 | // FIXME: Duplicated code due to poor abstraction. |
| 3860 | if (Container) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3861 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3862 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
| 3863 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3864 | i = CID->propimpl_begin(), e = CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3865 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3866 | ObjCPropertyImplDecl *PID = *i; |
| 3867 | if (PID->getPropertyDecl() == PD) { |
| 3868 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 3869 | Dynamic = true; |
| 3870 | } else { |
| 3871 | SynthesizePID = PID; |
| 3872 | } |
| 3873 | } |
| 3874 | } |
| 3875 | } else { |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3876 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3877 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3878 | i = OID->propimpl_begin(), e = OID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3879 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3880 | ObjCPropertyImplDecl *PID = *i; |
| 3881 | if (PID->getPropertyDecl() == PD) { |
| 3882 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 3883 | Dynamic = true; |
| 3884 | } else { |
| 3885 | SynthesizePID = PID; |
| 3886 | } |
| 3887 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3888 | } |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3889 | } |
| 3890 | } |
| 3891 | |
| 3892 | // FIXME: This is not very efficient. |
| 3893 | S = "T"; |
| 3894 | |
| 3895 | // Encode result type. |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3896 | // GCC has some special rules regarding encoding of properties which |
| 3897 | // closely resembles encoding of ivars. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3898 | getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3899 | true /* outermost type */, |
| 3900 | true /* encoding for property */); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3901 | |
| 3902 | if (PD->isReadOnly()) { |
| 3903 | S += ",R"; |
| 3904 | } else { |
| 3905 | switch (PD->getSetterKind()) { |
| 3906 | case ObjCPropertyDecl::Assign: break; |
| 3907 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3908 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3909 | } |
| 3910 | } |
| 3911 | |
| 3912 | // It really isn't clear at all what this means, since properties |
| 3913 | // are "dynamic by default". |
| 3914 | if (Dynamic) |
| 3915 | S += ",D"; |
| 3916 | |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3917 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 3918 | S += ",N"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3919 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3920 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 3921 | S += ",G"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3922 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3923 | } |
| 3924 | |
| 3925 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 3926 | S += ",S"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3927 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3928 | } |
| 3929 | |
| 3930 | if (SynthesizePID) { |
| 3931 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 3932 | S += ",V"; |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 3933 | S += OID->getNameAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3934 | } |
| 3935 | |
| 3936 | // FIXME: OBJCGC: weak & strong |
| 3937 | } |
| 3938 | |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3939 | /// getLegacyIntegralTypeEncoding - |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3940 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
| 3941 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3942 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
| 3943 | /// |
| 3944 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3945 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3946 | if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) { |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3947 | if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3948 | PointeeTy = UnsignedIntTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3949 | else |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3950 | if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3951 | PointeeTy = IntTy; |
| 3952 | } |
| 3953 | } |
| 3954 | } |
| 3955 | |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3956 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3957 | const FieldDecl *Field) const { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3958 | // We follow the behavior of gcc, expanding structures which are |
| 3959 | // directly pointed to, and expanding embedded structures. Note that |
| 3960 | // these rules are sufficient to prevent recursive encoding of the |
| 3961 | // same type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3962 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
Fariborz Jahanian | 5b8c7d9 | 2008-12-22 23:22:27 +0000 | [diff] [blame] | 3963 | true /* outermost type */); |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3964 | } |
| 3965 | |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3966 | static char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) { |
| 3967 | switch (T->getAs<BuiltinType>()->getKind()) { |
| 3968 | default: assert(0 && "Unhandled builtin type kind"); |
| 3969 | case BuiltinType::Void: return 'v'; |
| 3970 | case BuiltinType::Bool: return 'B'; |
| 3971 | case BuiltinType::Char_U: |
| 3972 | case BuiltinType::UChar: return 'C'; |
| 3973 | case BuiltinType::UShort: return 'S'; |
| 3974 | case BuiltinType::UInt: return 'I'; |
| 3975 | case BuiltinType::ULong: |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3976 | return C->getIntWidth(T) == 32 ? 'L' : 'Q'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3977 | case BuiltinType::UInt128: return 'T'; |
| 3978 | case BuiltinType::ULongLong: return 'Q'; |
| 3979 | case BuiltinType::Char_S: |
| 3980 | case BuiltinType::SChar: return 'c'; |
| 3981 | case BuiltinType::Short: return 's'; |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 3982 | case BuiltinType::WChar_S: |
| 3983 | case BuiltinType::WChar_U: |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3984 | case BuiltinType::Int: return 'i'; |
| 3985 | case BuiltinType::Long: |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3986 | return C->getIntWidth(T) == 32 ? 'l' : 'q'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3987 | case BuiltinType::LongLong: return 'q'; |
| 3988 | case BuiltinType::Int128: return 't'; |
| 3989 | case BuiltinType::Float: return 'f'; |
| 3990 | case BuiltinType::Double: return 'd'; |
Daniel Dunbar | 3a0be84 | 2010-10-11 21:13:48 +0000 | [diff] [blame] | 3991 | case BuiltinType::LongDouble: return 'D'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3992 | } |
| 3993 | } |
| 3994 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3995 | static void EncodeBitField(const ASTContext *Ctx, std::string& S, |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3996 | QualType T, const FieldDecl *FD) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3997 | const Expr *E = FD->getBitWidth(); |
| 3998 | assert(E && "bitfield width not there - getObjCEncodingForTypeImpl"); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3999 | S += 'b'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4000 | // The NeXT runtime encodes bit fields as b followed by the number of bits. |
| 4001 | // The GNU runtime requires more information; bitfields are encoded as b, |
| 4002 | // then the offset (in bits) of the first element, then the type of the |
| 4003 | // bitfield, then the size in bits. For example, in this structure: |
| 4004 | // |
| 4005 | // struct |
| 4006 | // { |
| 4007 | // int integer; |
| 4008 | // int flags:2; |
| 4009 | // }; |
| 4010 | // On a 32-bit system, the encoding for flags would be b2 for the NeXT |
| 4011 | // runtime, but b32i2 for the GNU runtime. The reason for this extra |
| 4012 | // information is not especially sensible, but we're stuck with it for |
| 4013 | // compatibility with GCC, although providing it breaks anything that |
| 4014 | // actually uses runtime introspection and wants to work on both runtimes... |
| 4015 | if (!Ctx->getLangOptions().NeXTRuntime) { |
| 4016 | const RecordDecl *RD = FD->getParent(); |
| 4017 | const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD); |
| 4018 | // FIXME: This same linear search is also used in ExprConstant - it might |
| 4019 | // be better if the FieldDecl stored its offset. We'd be increasing the |
| 4020 | // size of the object slightly, but saving some time every time it is used. |
| 4021 | unsigned i = 0; |
| 4022 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 4023 | FieldEnd = RD->field_end(); |
| 4024 | Field != FieldEnd; (void)++Field, ++i) { |
| 4025 | if (*Field == FD) |
| 4026 | break; |
| 4027 | } |
| 4028 | S += llvm::utostr(RL.getFieldOffset(i)); |
David Chisnall | c7ff82c | 2010-12-26 20:12:30 +0000 | [diff] [blame] | 4029 | if (T->isEnumeralType()) |
| 4030 | S += 'i'; |
| 4031 | else |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4032 | S += ObjCEncodingForPrimitiveKind(Ctx, T); |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4033 | } |
| 4034 | unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue(); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4035 | S += llvm::utostr(N); |
| 4036 | } |
| 4037 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 4038 | // FIXME: Use SmallString for accumulating string. |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4039 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 4040 | bool ExpandPointedToStructures, |
| 4041 | bool ExpandStructures, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 4042 | const FieldDecl *FD, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4043 | bool OutermostType, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4044 | bool EncodingProperty) const { |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4045 | if (T->getAs<BuiltinType>()) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4046 | if (FD && FD->isBitField()) |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4047 | return EncodeBitField(this, S, T, FD); |
| 4048 | S += ObjCEncodingForPrimitiveKind(this, T); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4049 | return; |
| 4050 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4051 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4052 | if (const ComplexType *CT = T->getAs<ComplexType>()) { |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 4053 | S += 'j'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4054 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false, |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 4055 | false); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4056 | return; |
| 4057 | } |
Fariborz Jahanian | 60bce3e | 2009-11-23 20:40:50 +0000 | [diff] [blame] | 4058 | |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4059 | // encoding for pointer or r3eference types. |
| 4060 | QualType PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4061 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Fariborz Jahanian | 8d2c0a9 | 2009-11-30 18:43:52 +0000 | [diff] [blame] | 4062 | if (PT->isObjCSelType()) { |
| 4063 | S += ':'; |
| 4064 | return; |
| 4065 | } |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4066 | PointeeTy = PT->getPointeeType(); |
| 4067 | } |
| 4068 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 4069 | PointeeTy = RT->getPointeeType(); |
| 4070 | if (!PointeeTy.isNull()) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4071 | bool isReadOnly = false; |
| 4072 | // For historical/compatibility reasons, the read-only qualifier of the |
| 4073 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 4074 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4075 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 4076 | if (isa<TypedefType>(T.getTypePtr())) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4077 | if (OutermostType && T.isConstQualified()) { |
| 4078 | isReadOnly = true; |
| 4079 | S += 'r'; |
| 4080 | } |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 4081 | } else if (OutermostType) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4082 | QualType P = PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4083 | while (P->getAs<PointerType>()) |
| 4084 | P = P->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4085 | if (P.isConstQualified()) { |
| 4086 | isReadOnly = true; |
| 4087 | S += 'r'; |
| 4088 | } |
| 4089 | } |
| 4090 | if (isReadOnly) { |
| 4091 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 4092 | // combinations need to be rearranged. |
| 4093 | // Rewrite "in const" from "nr" to "rn" |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 4094 | if (llvm::StringRef(S).endswith("nr")) |
| 4095 | S.replace(S.end()-2, S.end(), "rn"); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4096 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4097 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4098 | if (PointeeTy->isCharType()) { |
| 4099 | // char pointer types should be encoded as '*' unless it is a |
| 4100 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 4101 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4102 | S += '*'; |
| 4103 | return; |
| 4104 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4105 | } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) { |
Steve Naroff | 9533a7f | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 4106 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 4107 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 4108 | S += '#'; |
| 4109 | return; |
| 4110 | } |
| 4111 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 4112 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 4113 | S += '@'; |
| 4114 | return; |
| 4115 | } |
| 4116 | // fall through... |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4117 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4118 | S += '^'; |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4119 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 4120 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4121 | getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4122 | NULL); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4123 | return; |
| 4124 | } |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4125 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4126 | if (const ArrayType *AT = |
| 4127 | // Ignore type qualifiers etc. |
| 4128 | dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4129 | if (isa<IncompleteArrayType>(AT)) { |
| 4130 | // Incomplete arrays are encoded as a pointer to the array element. |
| 4131 | S += '^'; |
| 4132 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4133 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4134 | false, ExpandStructures, FD); |
| 4135 | } else { |
| 4136 | S += '['; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4137 | |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4138 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 4139 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 4140 | else { |
| 4141 | //Variable length arrays are encoded as a regular array with 0 elements. |
| 4142 | assert(isa<VariableArrayType>(AT) && "Unknown array type!"); |
| 4143 | S += '0'; |
| 4144 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4145 | |
| 4146 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4147 | false, ExpandStructures, FD); |
| 4148 | S += ']'; |
| 4149 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4150 | return; |
| 4151 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4152 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4153 | if (T->getAs<FunctionType>()) { |
Anders Carlsson | c0a87b7 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 4154 | S += '?'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4155 | return; |
| 4156 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4157 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4158 | if (const RecordType *RTy = T->getAs<RecordType>()) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4159 | RecordDecl *RDecl = RTy->getDecl(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4160 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 4161 | // Anonymous structures print as '?' |
| 4162 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 4163 | S += II->getName(); |
Fariborz Jahanian | 6fb9439 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 4164 | if (ClassTemplateSpecializationDecl *Spec |
| 4165 | = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) { |
| 4166 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 4167 | std::string TemplateArgsStr |
| 4168 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4169 | TemplateArgs.data(), |
| 4170 | TemplateArgs.size(), |
Fariborz Jahanian | 6fb9439 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 4171 | (*this).PrintingPolicy); |
| 4172 | |
| 4173 | S += TemplateArgsStr; |
| 4174 | } |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 4175 | } else { |
| 4176 | S += '?'; |
| 4177 | } |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 4178 | if (ExpandStructures) { |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4179 | S += '='; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4180 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 4181 | FieldEnd = RDecl->field_end(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4182 | Field != FieldEnd; ++Field) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4183 | if (FD) { |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4184 | S += '"'; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4185 | S += Field->getNameAsString(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4186 | S += '"'; |
| 4187 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4188 | |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4189 | // Special case bit-fields. |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4190 | if (Field->isBitField()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4191 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4192 | (*Field)); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4193 | } else { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4194 | QualType qt = Field->getType(); |
| 4195 | getLegacyIntegralTypeEncoding(qt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4196 | getObjCEncodingForTypeImpl(qt, S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4197 | FD); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4198 | } |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4199 | } |
Fariborz Jahanian | 6de88a8 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 4200 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4201 | S += RDecl->isUnion() ? ')' : '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4202 | return; |
| 4203 | } |
Fariborz Jahanian | e6012c7 | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 4204 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4205 | if (T->isEnumeralType()) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4206 | if (FD && FD->isBitField()) |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4207 | EncodeBitField(this, S, T, FD); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4208 | else |
| 4209 | S += 'i'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4210 | return; |
| 4211 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4212 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4213 | if (T->isBlockPointerType()) { |
Steve Naroff | 21a98b1 | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 4214 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4215 | return; |
| 4216 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4217 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4218 | // Ignore protocol qualifiers when mangling at this level. |
| 4219 | if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>()) |
| 4220 | T = OT->getBaseType(); |
| 4221 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4222 | if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4223 | // @encode(class_name) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4224 | ObjCInterfaceDecl *OI = OIT->getDecl(); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4225 | S += '{'; |
| 4226 | const IdentifierInfo *II = OI->getIdentifier(); |
| 4227 | S += II->getName(); |
| 4228 | S += '='; |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4229 | llvm::SmallVector<ObjCIvarDecl*, 32> Ivars; |
| 4230 | DeepCollectObjCIvars(OI, true, Ivars); |
| 4231 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) { |
| 4232 | FieldDecl *Field = cast<FieldDecl>(Ivars[i]); |
| 4233 | if (Field->isBitField()) |
| 4234 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4235 | else |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4236 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4237 | } |
| 4238 | S += '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4239 | return; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4240 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4241 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4242 | if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4243 | if (OPT->isObjCIdType()) { |
| 4244 | S += '@'; |
| 4245 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4246 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4247 | |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4248 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
| 4249 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
| 4250 | // Since this is a binary compatibility issue, need to consult with runtime |
| 4251 | // folks. Fortunately, this is a *very* obsure construct. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4252 | S += '#'; |
| 4253 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4254 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4255 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4256 | if (OPT->isObjCQualifiedIdType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4257 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4258 | ExpandPointedToStructures, |
| 4259 | ExpandStructures, FD); |
| 4260 | if (FD || EncodingProperty) { |
| 4261 | // Note that we do extended encoding of protocol qualifer list |
| 4262 | // Only when doing ivar or property encoding. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4263 | S += '"'; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4264 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 4265 | E = OPT->qual_end(); I != E; ++I) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4266 | S += '<'; |
| 4267 | S += (*I)->getNameAsString(); |
| 4268 | S += '>'; |
| 4269 | } |
| 4270 | S += '"'; |
| 4271 | } |
| 4272 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4273 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4274 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4275 | QualType PointeeTy = OPT->getPointeeType(); |
| 4276 | if (!EncodingProperty && |
| 4277 | isa<TypedefType>(PointeeTy.getTypePtr())) { |
| 4278 | // Another historical/compatibility reason. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4279 | // We encode the underlying type which comes out as |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4280 | // {...}; |
| 4281 | S += '^'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4282 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 4283 | false, ExpandPointedToStructures, |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4284 | NULL); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4285 | return; |
| 4286 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4287 | |
| 4288 | S += '@'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4289 | if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4290 | S += '"'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4291 | S += OPT->getInterfaceDecl()->getIdentifier()->getName(); |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4292 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 4293 | E = OPT->qual_end(); I != E; ++I) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4294 | S += '<'; |
| 4295 | S += (*I)->getNameAsString(); |
| 4296 | S += '>'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4297 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4298 | S += '"'; |
| 4299 | } |
| 4300 | return; |
| 4301 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4302 | |
John McCall | 532ec7b | 2010-05-17 23:56:34 +0000 | [diff] [blame] | 4303 | // gcc just blithely ignores member pointers. |
| 4304 | // TODO: maybe there should be a mangling for these |
| 4305 | if (T->getAs<MemberPointerType>()) |
| 4306 | return; |
Fariborz Jahanian | e6012c7 | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 4307 | |
| 4308 | if (T->isVectorType()) { |
| 4309 | // This matches gcc's encoding, even though technically it is |
| 4310 | // insufficient. |
| 4311 | // FIXME. We should do a better job than gcc. |
| 4312 | return; |
| 4313 | } |
| 4314 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4315 | assert(0 && "@encode for type not implemented!"); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4316 | } |
| 4317 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4318 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 4319 | std::string& S) const { |
| 4320 | if (QT & Decl::OBJC_TQ_In) |
| 4321 | S += 'n'; |
| 4322 | if (QT & Decl::OBJC_TQ_Inout) |
| 4323 | S += 'N'; |
| 4324 | if (QT & Decl::OBJC_TQ_Out) |
| 4325 | S += 'o'; |
| 4326 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 4327 | S += 'O'; |
| 4328 | if (QT & Decl::OBJC_TQ_Byref) |
| 4329 | S += 'R'; |
| 4330 | if (QT & Decl::OBJC_TQ_Oneway) |
| 4331 | S += 'V'; |
| 4332 | } |
| 4333 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4334 | void ASTContext::setBuiltinVaListType(QualType T) { |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 4335 | assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4336 | |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 4337 | BuiltinVaListType = T; |
| 4338 | } |
| 4339 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4340 | void ASTContext::setObjCIdType(QualType T) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 4341 | ObjCIdTypedefType = T; |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 4342 | } |
| 4343 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4344 | void ASTContext::setObjCSelType(QualType T) { |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 4345 | ObjCSelTypedefType = T; |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 4346 | } |
| 4347 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4348 | void ASTContext::setObjCProtoType(QualType QT) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4349 | ObjCProtoType = QT; |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 4350 | } |
| 4351 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4352 | void ASTContext::setObjCClassType(QualType T) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 4353 | ObjCClassTypedefType = T; |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 4354 | } |
| 4355 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4356 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4357 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 4358 | "'NSConstantString' type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4359 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4360 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 4361 | } |
| 4362 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4363 | /// \brief Retrieve the template name that corresponds to a non-empty |
| 4364 | /// lookup. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4365 | TemplateName |
| 4366 | ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin, |
| 4367 | UnresolvedSetIterator End) const { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4368 | unsigned size = End - Begin; |
| 4369 | assert(size > 1 && "set is not overloaded!"); |
| 4370 | |
| 4371 | void *memory = Allocate(sizeof(OverloadedTemplateStorage) + |
| 4372 | size * sizeof(FunctionTemplateDecl*)); |
| 4373 | OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size); |
| 4374 | |
| 4375 | NamedDecl **Storage = OT->getStorage(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4376 | for (UnresolvedSetIterator I = Begin; I != End; ++I) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4377 | NamedDecl *D = *I; |
| 4378 | assert(isa<FunctionTemplateDecl>(D) || |
| 4379 | (isa<UsingShadowDecl>(D) && |
| 4380 | isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))); |
| 4381 | *Storage++ = D; |
| 4382 | } |
| 4383 | |
| 4384 | return TemplateName(OT); |
| 4385 | } |
| 4386 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4387 | /// \brief Retrieve the template name that represents a qualified |
| 4388 | /// template name such as \c std::vector. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4389 | TemplateName |
| 4390 | ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
| 4391 | bool TemplateKeyword, |
| 4392 | TemplateDecl *Template) const { |
Douglas Gregor | 0f0ea2a | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 4393 | assert(NNS && "Missing nested-name-specifier in qualified template name"); |
| 4394 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4395 | // FIXME: Canonicalization? |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4396 | llvm::FoldingSetNodeID ID; |
| 4397 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 4398 | |
| 4399 | void *InsertPos = 0; |
| 4400 | QualifiedTemplateName *QTN = |
| 4401 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4402 | if (!QTN) { |
| 4403 | QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); |
| 4404 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 4405 | } |
| 4406 | |
| 4407 | return TemplateName(QTN); |
| 4408 | } |
| 4409 | |
| 4410 | /// \brief Retrieve the template name that represents a dependent |
| 4411 | /// template name such as \c MetaFun::template apply. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4412 | TemplateName |
| 4413 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
| 4414 | const IdentifierInfo *Name) const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4415 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 4416 | "Nested name specifier must be dependent"); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4417 | |
| 4418 | llvm::FoldingSetNodeID ID; |
| 4419 | DependentTemplateName::Profile(ID, NNS, Name); |
| 4420 | |
| 4421 | void *InsertPos = 0; |
| 4422 | DependentTemplateName *QTN = |
| 4423 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4424 | |
| 4425 | if (QTN) |
| 4426 | return TemplateName(QTN); |
| 4427 | |
| 4428 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 4429 | if (CanonNNS == NNS) { |
| 4430 | QTN = new (*this,4) DependentTemplateName(NNS, Name); |
| 4431 | } else { |
| 4432 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
| 4433 | QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4434 | DependentTemplateName *CheckQTN = |
| 4435 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4436 | assert(!CheckQTN && "Dependent type name canonicalization broken"); |
| 4437 | (void)CheckQTN; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4438 | } |
| 4439 | |
| 4440 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 4441 | return TemplateName(QTN); |
| 4442 | } |
| 4443 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4444 | /// \brief Retrieve the template name that represents a dependent |
| 4445 | /// template name such as \c MetaFun::template operator+. |
| 4446 | TemplateName |
| 4447 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4448 | OverloadedOperatorKind Operator) const { |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4449 | assert((!NNS || NNS->isDependent()) && |
| 4450 | "Nested name specifier must be dependent"); |
| 4451 | |
| 4452 | llvm::FoldingSetNodeID ID; |
| 4453 | DependentTemplateName::Profile(ID, NNS, Operator); |
| 4454 | |
| 4455 | void *InsertPos = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4456 | DependentTemplateName *QTN |
| 4457 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4458 | |
| 4459 | if (QTN) |
| 4460 | return TemplateName(QTN); |
| 4461 | |
| 4462 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 4463 | if (CanonNNS == NNS) { |
| 4464 | QTN = new (*this,4) DependentTemplateName(NNS, Operator); |
| 4465 | } else { |
| 4466 | TemplateName Canon = getDependentTemplateName(CanonNNS, Operator); |
| 4467 | QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4468 | |
| 4469 | DependentTemplateName *CheckQTN |
| 4470 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4471 | assert(!CheckQTN && "Dependent template name canonicalization broken"); |
| 4472 | (void)CheckQTN; |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4473 | } |
| 4474 | |
| 4475 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 4476 | return TemplateName(QTN); |
| 4477 | } |
| 4478 | |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 4479 | TemplateName |
| 4480 | ASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param, |
| 4481 | const TemplateArgument &ArgPack) const { |
| 4482 | ASTContext &Self = const_cast<ASTContext &>(*this); |
| 4483 | llvm::FoldingSetNodeID ID; |
| 4484 | SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack); |
| 4485 | |
| 4486 | void *InsertPos = 0; |
| 4487 | SubstTemplateTemplateParmPackStorage *Subst |
| 4488 | = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos); |
| 4489 | |
| 4490 | if (!Subst) { |
| 4491 | Subst = new (*this) SubstTemplateTemplateParmPackStorage(Self, Param, |
| 4492 | ArgPack.pack_size(), |
| 4493 | ArgPack.pack_begin()); |
| 4494 | SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos); |
| 4495 | } |
| 4496 | |
| 4497 | return TemplateName(Subst); |
| 4498 | } |
| 4499 | |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4500 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | d934112 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 4501 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 4502 | /// is actually a value of type @c TargetInfo::IntType. |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 4503 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4504 | switch (Type) { |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 4505 | case TargetInfo::NoInt: return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4506 | case TargetInfo::SignedShort: return ShortTy; |
| 4507 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 4508 | case TargetInfo::SignedInt: return IntTy; |
| 4509 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 4510 | case TargetInfo::SignedLong: return LongTy; |
| 4511 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 4512 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 4513 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 4514 | } |
| 4515 | |
| 4516 | assert(false && "Unhandled TargetInfo::IntType value"); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 4517 | return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4518 | } |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 4519 | |
| 4520 | //===----------------------------------------------------------------------===// |
| 4521 | // Type Predicates. |
| 4522 | //===----------------------------------------------------------------------===// |
| 4523 | |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4524 | /// isObjCNSObjectType - Return true if this is an NSObject object using |
| 4525 | /// NSObject attribute on a c-style pointer type. |
| 4526 | /// FIXME - Make it work directly on types. |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 4527 | /// FIXME: Move to Type. |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4528 | /// |
| 4529 | bool ASTContext::isObjCNSObjectType(QualType Ty) const { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 4530 | if (const TypedefType *TDT = dyn_cast<TypedefType>(Ty)) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4531 | if (TypedefDecl *TD = TDT->getDecl()) |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 4532 | if (TD->getAttr<ObjCNSObjectAttr>()) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4533 | return true; |
| 4534 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4535 | return false; |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4536 | } |
| 4537 | |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4538 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 4539 | /// garbage collection attribute. |
| 4540 | /// |
John McCall | ae278a3 | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 4541 | Qualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const { |
| 4542 | if (getLangOptions().getGCMode() == LangOptions::NonGC) |
| 4543 | return Qualifiers::GCNone; |
| 4544 | |
| 4545 | assert(getLangOptions().ObjC1); |
| 4546 | Qualifiers::GC GCAttrs = Ty.getObjCGCAttr(); |
| 4547 | |
| 4548 | // Default behaviour under objective-C's gc is for ObjC pointers |
| 4549 | // (or pointers to them) be treated as though they were declared |
| 4550 | // as __strong. |
| 4551 | if (GCAttrs == Qualifiers::GCNone) { |
| 4552 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
| 4553 | return Qualifiers::Strong; |
| 4554 | else if (Ty->isPointerType()) |
| 4555 | return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); |
| 4556 | } else { |
| 4557 | // It's not valid to set GC attributes on anything that isn't a |
| 4558 | // pointer. |
| 4559 | #ifndef NDEBUG |
| 4560 | QualType CT = Ty->getCanonicalTypeInternal(); |
| 4561 | while (const ArrayType *AT = dyn_cast<ArrayType>(CT)) |
| 4562 | CT = AT->getElementType(); |
| 4563 | assert(CT->isAnyPointerType() || CT->isBlockPointerType()); |
| 4564 | #endif |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4565 | } |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 4566 | return GCAttrs; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4567 | } |
| 4568 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4569 | //===----------------------------------------------------------------------===// |
| 4570 | // Type Compatibility Testing |
| 4571 | //===----------------------------------------------------------------------===// |
Chris Lattner | 770951b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 4572 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4573 | /// areCompatVectorTypes - Return true if the two specified vector types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4574 | /// compatible. |
| 4575 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 4576 | const VectorType *RHS) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 4577 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4578 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4579 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4580 | } |
| 4581 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 4582 | bool ASTContext::areCompatibleVectorTypes(QualType FirstVec, |
| 4583 | QualType SecondVec) { |
| 4584 | assert(FirstVec->isVectorType() && "FirstVec should be a vector type"); |
| 4585 | assert(SecondVec->isVectorType() && "SecondVec should be a vector type"); |
| 4586 | |
| 4587 | if (hasSameUnqualifiedType(FirstVec, SecondVec)) |
| 4588 | return true; |
| 4589 | |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 4590 | // Treat Neon vector types and most AltiVec vector types as if they are the |
| 4591 | // equivalent GCC vector types. |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 4592 | const VectorType *First = FirstVec->getAs<VectorType>(); |
| 4593 | const VectorType *Second = SecondVec->getAs<VectorType>(); |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 4594 | if (First->getNumElements() == Second->getNumElements() && |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 4595 | hasSameType(First->getElementType(), Second->getElementType()) && |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 4596 | First->getVectorKind() != VectorType::AltiVecPixel && |
| 4597 | First->getVectorKind() != VectorType::AltiVecBool && |
| 4598 | Second->getVectorKind() != VectorType::AltiVecPixel && |
| 4599 | Second->getVectorKind() != VectorType::AltiVecBool) |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 4600 | return true; |
| 4601 | |
| 4602 | return false; |
| 4603 | } |
| 4604 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4605 | //===----------------------------------------------------------------------===// |
| 4606 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 4607 | //===----------------------------------------------------------------------===// |
| 4608 | |
| 4609 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 4610 | /// inheritance hierarchy of 'rProto'. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4611 | bool |
| 4612 | ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 4613 | ObjCProtocolDecl *rProto) const { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4614 | if (lProto == rProto) |
| 4615 | return true; |
| 4616 | for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), |
| 4617 | E = rProto->protocol_end(); PI != E; ++PI) |
| 4618 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 4619 | return true; |
| 4620 | return false; |
| 4621 | } |
| 4622 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4623 | /// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...> |
| 4624 | /// return true if lhs's protocols conform to rhs's protocol; false |
| 4625 | /// otherwise. |
| 4626 | bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) { |
| 4627 | if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType()) |
| 4628 | return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false); |
| 4629 | return false; |
| 4630 | } |
| 4631 | |
Fariborz Jahanian | a8f8dac | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 4632 | /// ObjCQualifiedClassTypesAreCompatible - compare Class<p,...> and |
| 4633 | /// Class<p1, ...>. |
| 4634 | bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs, |
| 4635 | QualType rhs) { |
| 4636 | const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>(); |
| 4637 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
| 4638 | assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible"); |
| 4639 | |
| 4640 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4641 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4642 | bool match = false; |
| 4643 | ObjCProtocolDecl *lhsProto = *I; |
| 4644 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 4645 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 4646 | ObjCProtocolDecl *rhsProto = *J; |
| 4647 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) { |
| 4648 | match = true; |
| 4649 | break; |
| 4650 | } |
| 4651 | } |
| 4652 | if (!match) |
| 4653 | return false; |
| 4654 | } |
| 4655 | return true; |
| 4656 | } |
| 4657 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4658 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 4659 | /// ObjCQualifiedIDType. |
| 4660 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 4661 | bool compare) { |
| 4662 | // Allow id<P..> and an 'id' or void* type in all cases. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4663 | if (lhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4664 | lhs->isObjCIdType() || lhs->isObjCClassType()) |
| 4665 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4666 | else if (rhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4667 | rhs->isObjCIdType() || rhs->isObjCClassType()) |
| 4668 | return true; |
| 4669 | |
| 4670 | if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4671 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4672 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4673 | if (!rhsOPT) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4674 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4675 | if (rhsOPT->qual_empty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4676 | // If the RHS is a unqualified interface pointer "NSString*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4677 | // make sure we check the class hierarchy. |
| 4678 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 4679 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4680 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4681 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4682 | // see if static class implements all of id's protocols, directly or |
| 4683 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 4684 | if (!rhsID->ClassImplementsProtocol(*I, true)) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4685 | return false; |
| 4686 | } |
| 4687 | } |
| 4688 | // If there are no qualifiers and no interface, we have an 'id'. |
| 4689 | return true; |
| 4690 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4691 | // Both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4692 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4693 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4694 | ObjCProtocolDecl *lhsProto = *I; |
| 4695 | bool match = false; |
| 4696 | |
| 4697 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4698 | // see if static class implements all of id's protocols, directly or |
| 4699 | // through its super class and categories. |
| 4700 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 4701 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 4702 | ObjCProtocolDecl *rhsProto = *J; |
| 4703 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 4704 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 4705 | match = true; |
| 4706 | break; |
| 4707 | } |
| 4708 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4709 | // If the RHS is a qualified interface pointer "NSString<P>*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4710 | // make sure we check the class hierarchy. |
| 4711 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 4712 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4713 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4714 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4715 | // see if static class implements all of id's protocols, directly or |
| 4716 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 4717 | if (rhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4718 | match = true; |
| 4719 | break; |
| 4720 | } |
| 4721 | } |
| 4722 | } |
| 4723 | if (!match) |
| 4724 | return false; |
| 4725 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4726 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4727 | return true; |
| 4728 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4729 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4730 | const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 4731 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 4732 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4733 | if (const ObjCObjectPointerType *lhsOPT = |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4734 | lhs->getAsObjCInterfacePointerType()) { |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 4735 | // If both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4736 | for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(), |
| 4737 | E = lhsOPT->qual_end(); I != E; ++I) { |
| 4738 | ObjCProtocolDecl *lhsProto = *I; |
| 4739 | bool match = false; |
| 4740 | |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 4741 | // 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] | 4742 | // see if static class implements all of id's protocols, directly or |
| 4743 | // through its super class and categories. |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 4744 | // First, lhs protocols in the qualifier list must be found, direct |
| 4745 | // or indirect in rhs's qualifier list or it is a mismatch. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4746 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 4747 | E = rhsQID->qual_end(); J != E; ++J) { |
| 4748 | ObjCProtocolDecl *rhsProto = *J; |
| 4749 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 4750 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 4751 | match = true; |
| 4752 | break; |
| 4753 | } |
| 4754 | } |
| 4755 | if (!match) |
| 4756 | return false; |
| 4757 | } |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 4758 | |
| 4759 | // Static class's protocols, or its super class or category protocols |
| 4760 | // must be found, direct or indirect in rhs's qualifier list or it is a mismatch. |
| 4761 | if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { |
| 4762 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
| 4763 | CollectInheritedProtocols(lhsID, LHSInheritedProtocols); |
| 4764 | // This is rather dubious but matches gcc's behavior. If lhs has |
| 4765 | // no type qualifier and its class has no static protocol(s) |
| 4766 | // assume that it is mismatch. |
| 4767 | if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty()) |
| 4768 | return false; |
| 4769 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 4770 | LHSInheritedProtocols.begin(), |
| 4771 | E = LHSInheritedProtocols.end(); I != E; ++I) { |
| 4772 | bool match = false; |
| 4773 | ObjCProtocolDecl *lhsProto = (*I); |
| 4774 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 4775 | E = rhsQID->qual_end(); J != E; ++J) { |
| 4776 | ObjCProtocolDecl *rhsProto = *J; |
| 4777 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 4778 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 4779 | match = true; |
| 4780 | break; |
| 4781 | } |
| 4782 | } |
| 4783 | if (!match) |
| 4784 | return false; |
| 4785 | } |
| 4786 | } |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4787 | return true; |
| 4788 | } |
| 4789 | return false; |
| 4790 | } |
| 4791 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4792 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4793 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 4794 | /// protocol qualifiers on the LHS or RHS. |
| 4795 | /// |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4796 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 4797 | const ObjCObjectPointerType *RHSOPT) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4798 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 4799 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 4800 | |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 4801 | // 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] | 4802 | if (LHS->isObjCUnqualifiedIdOrClass() || |
| 4803 | RHS->isObjCUnqualifiedIdOrClass()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4804 | return true; |
| 4805 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4806 | if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4807 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 4808 | QualType(RHSOPT,0), |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4809 | false); |
Fariborz Jahanian | a8f8dac | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 4810 | |
| 4811 | if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass()) |
| 4812 | return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0), |
| 4813 | QualType(RHSOPT,0)); |
| 4814 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4815 | // If we have 2 user-defined types, fall into that path. |
| 4816 | if (LHS->getInterface() && RHS->getInterface()) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4817 | return canAssignObjCInterfaces(LHS, RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4818 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4819 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4820 | } |
| 4821 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4822 | /// canAssignObjCInterfacesInBlockPointer - This routine is specifically written |
| 4823 | /// for providing type-safty for objective-c pointers used to pass/return |
| 4824 | /// arguments in block literals. When passed as arguments, passing 'A*' where |
| 4825 | /// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is |
| 4826 | /// not OK. For the return type, the opposite is not OK. |
| 4827 | bool ASTContext::canAssignObjCInterfacesInBlockPointer( |
| 4828 | const ObjCObjectPointerType *LHSOPT, |
| 4829 | const ObjCObjectPointerType *RHSOPT) { |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 4830 | if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4831 | return true; |
| 4832 | |
| 4833 | if (LHSOPT->isObjCBuiltinType()) { |
| 4834 | return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType(); |
| 4835 | } |
| 4836 | |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 4837 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4838 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 4839 | QualType(RHSOPT,0), |
| 4840 | false); |
| 4841 | |
| 4842 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 4843 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 4844 | if (LHS && RHS) { // We have 2 user-defined types. |
| 4845 | if (LHS != RHS) { |
| 4846 | if (LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
| 4847 | return false; |
| 4848 | if (RHS->getDecl()->isSuperClassOf(LHS->getDecl())) |
| 4849 | return true; |
| 4850 | } |
| 4851 | else |
| 4852 | return true; |
| 4853 | } |
| 4854 | return false; |
| 4855 | } |
| 4856 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4857 | /// getIntersectionOfProtocols - This routine finds the intersection of set |
| 4858 | /// of protocols inherited from two distinct objective-c pointer objects. |
| 4859 | /// It is used to build composite qualifier list of the composite type of |
| 4860 | /// the conditional expression involving two objective-c pointer objects. |
| 4861 | static |
| 4862 | void getIntersectionOfProtocols(ASTContext &Context, |
| 4863 | const ObjCObjectPointerType *LHSOPT, |
| 4864 | const ObjCObjectPointerType *RHSOPT, |
| 4865 | llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) { |
| 4866 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4867 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 4868 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 4869 | assert(LHS->getInterface() && "LHS must have an interface base"); |
| 4870 | assert(RHS->getInterface() && "RHS must have an interface base"); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4871 | |
| 4872 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet; |
| 4873 | unsigned LHSNumProtocols = LHS->getNumProtocols(); |
| 4874 | if (LHSNumProtocols > 0) |
| 4875 | InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end()); |
| 4876 | else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 4877 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4878 | Context.CollectInheritedProtocols(LHS->getInterface(), |
| 4879 | LHSInheritedProtocols); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4880 | InheritedProtocolSet.insert(LHSInheritedProtocols.begin(), |
| 4881 | LHSInheritedProtocols.end()); |
| 4882 | } |
| 4883 | |
| 4884 | unsigned RHSNumProtocols = RHS->getNumProtocols(); |
| 4885 | if (RHSNumProtocols > 0) { |
Dan Gohman | cb421fa | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 4886 | ObjCProtocolDecl **RHSProtocols = |
| 4887 | const_cast<ObjCProtocolDecl **>(RHS->qual_begin()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4888 | for (unsigned i = 0; i < RHSNumProtocols; ++i) |
| 4889 | if (InheritedProtocolSet.count(RHSProtocols[i])) |
| 4890 | IntersectionOfProtocols.push_back(RHSProtocols[i]); |
| 4891 | } |
| 4892 | else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 4893 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4894 | Context.CollectInheritedProtocols(RHS->getInterface(), |
| 4895 | RHSInheritedProtocols); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 4896 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 4897 | RHSInheritedProtocols.begin(), |
| 4898 | E = RHSInheritedProtocols.end(); I != E; ++I) |
| 4899 | if (InheritedProtocolSet.count((*I))) |
| 4900 | IntersectionOfProtocols.push_back((*I)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4901 | } |
| 4902 | } |
| 4903 | |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4904 | /// areCommonBaseCompatible - Returns common base class of the two classes if |
| 4905 | /// one found. Note that this is O'2 algorithm. But it will be called as the |
| 4906 | /// last type comparison in a ?-exp of ObjC pointer types before a |
| 4907 | /// warning is issued. So, its invokation is extremely rare. |
| 4908 | QualType ASTContext::areCommonBaseCompatible( |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4909 | const ObjCObjectPointerType *Lptr, |
| 4910 | const ObjCObjectPointerType *Rptr) { |
| 4911 | const ObjCObjectType *LHS = Lptr->getObjectType(); |
| 4912 | const ObjCObjectType *RHS = Rptr->getObjectType(); |
| 4913 | const ObjCInterfaceDecl* LDecl = LHS->getInterface(); |
| 4914 | const ObjCInterfaceDecl* RDecl = RHS->getInterface(); |
| 4915 | if (!LDecl || !RDecl) |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4916 | return QualType(); |
| 4917 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4918 | while ((LDecl = LDecl->getSuperClass())) { |
| 4919 | LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4920 | if (canAssignObjCInterfaces(LHS, RHS)) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4921 | llvm::SmallVector<ObjCProtocolDecl *, 8> Protocols; |
| 4922 | getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols); |
| 4923 | |
| 4924 | QualType Result = QualType(LHS, 0); |
| 4925 | if (!Protocols.empty()) |
| 4926 | Result = getObjCObjectType(Result, Protocols.data(), Protocols.size()); |
| 4927 | Result = getObjCObjectPointerType(Result); |
| 4928 | return Result; |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4929 | } |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4930 | } |
| 4931 | |
| 4932 | return QualType(); |
| 4933 | } |
| 4934 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4935 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS, |
| 4936 | const ObjCObjectType *RHS) { |
| 4937 | assert(LHS->getInterface() && "LHS is not an interface type"); |
| 4938 | assert(RHS->getInterface() && "RHS is not an interface type"); |
| 4939 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4940 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 4941 | // the LHS. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4942 | if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface())) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4943 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4944 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4945 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 4946 | // protocol qualified at all, then we are good. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4947 | if (LHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4948 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4949 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4950 | // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it |
| 4951 | // isn't a superset. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4952 | if (RHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4953 | return true; // FIXME: should return false! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4954 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4955 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 4956 | LHSPE = LHS->qual_end(); |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4957 | LHSPI != LHSPE; LHSPI++) { |
| 4958 | bool RHSImplementsProtocol = false; |
| 4959 | |
| 4960 | // If the RHS doesn't implement the protocol on the left, the types |
| 4961 | // are incompatible. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4962 | for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(), |
| 4963 | RHSPE = RHS->qual_end(); |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 4964 | RHSPI != RHSPE; RHSPI++) { |
| 4965 | if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) { |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4966 | RHSImplementsProtocol = true; |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 4967 | break; |
| 4968 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4969 | } |
| 4970 | // FIXME: For better diagnostics, consider passing back the protocol name. |
| 4971 | if (!RHSImplementsProtocol) |
| 4972 | return false; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4973 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4974 | // The RHS implements all protocols listed on the LHS. |
| 4975 | return true; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4976 | } |
| 4977 | |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4978 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 4979 | // get the "pointed to" types |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4980 | const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
| 4981 | const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4982 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4983 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4984 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4985 | |
| 4986 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 4987 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4988 | } |
| 4989 | |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4990 | bool ASTContext::canBindObjCObjectType(QualType To, QualType From) { |
| 4991 | return canAssignObjCInterfaces( |
| 4992 | getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(), |
| 4993 | getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>()); |
| 4994 | } |
| 4995 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4996 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4997 | /// both shall have the identically qualified version of a compatible type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4998 | /// 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] | 4999 | /// same. See 6.7.[2,3,5] for additional rules. |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5000 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS, |
| 5001 | bool CompareUnqualified) { |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 5002 | if (getLangOptions().CPlusPlus) |
| 5003 | return hasSameType(LHS, RHS); |
| 5004 | |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5005 | return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5006 | } |
| 5007 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5008 | bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) { |
| 5009 | return !mergeTypes(LHS, RHS, true).isNull(); |
| 5010 | } |
| 5011 | |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 5012 | /// mergeTransparentUnionType - if T is a transparent union type and a member |
| 5013 | /// of T is compatible with SubType, return the merged type, else return |
| 5014 | /// QualType() |
| 5015 | QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType, |
| 5016 | bool OfBlockPointer, |
| 5017 | bool Unqualified) { |
| 5018 | if (const RecordType *UT = T->getAsUnionType()) { |
| 5019 | RecordDecl *UD = UT->getDecl(); |
| 5020 | if (UD->hasAttr<TransparentUnionAttr>()) { |
| 5021 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 5022 | itend = UD->field_end(); it != itend; ++it) { |
Peter Collingbourne | f91d757 | 2010-12-02 21:00:06 +0000 | [diff] [blame] | 5023 | QualType ET = it->getType().getUnqualifiedType(); |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 5024 | QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified); |
| 5025 | if (!MT.isNull()) |
| 5026 | return MT; |
| 5027 | } |
| 5028 | } |
| 5029 | } |
| 5030 | |
| 5031 | return QualType(); |
| 5032 | } |
| 5033 | |
| 5034 | /// mergeFunctionArgumentTypes - merge two types which appear as function |
| 5035 | /// argument types |
| 5036 | QualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs, |
| 5037 | bool OfBlockPointer, |
| 5038 | bool Unqualified) { |
| 5039 | // GNU extension: two types are compatible if they appear as a function |
| 5040 | // argument, one of the types is a transparent union type and the other |
| 5041 | // type is compatible with a union member |
| 5042 | QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer, |
| 5043 | Unqualified); |
| 5044 | if (!lmerge.isNull()) |
| 5045 | return lmerge; |
| 5046 | |
| 5047 | QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer, |
| 5048 | Unqualified); |
| 5049 | if (!rmerge.isNull()) |
| 5050 | return rmerge; |
| 5051 | |
| 5052 | return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified); |
| 5053 | } |
| 5054 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5055 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs, |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5056 | bool OfBlockPointer, |
| 5057 | bool Unqualified) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5058 | const FunctionType *lbase = lhs->getAs<FunctionType>(); |
| 5059 | const FunctionType *rbase = rhs->getAs<FunctionType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5060 | const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 5061 | const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5062 | bool allLTypes = true; |
| 5063 | bool allRTypes = true; |
| 5064 | |
| 5065 | // Check return type |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5066 | QualType retType; |
Fariborz Jahanian | d263fd1 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 5067 | if (OfBlockPointer) { |
| 5068 | QualType RHS = rbase->getResultType(); |
| 5069 | QualType LHS = lbase->getResultType(); |
| 5070 | bool UnqualifiedResult = Unqualified; |
| 5071 | if (!UnqualifiedResult) |
| 5072 | UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers()); |
| 5073 | retType = mergeTypes(RHS, LHS, true, UnqualifiedResult); |
| 5074 | } |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5075 | else |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5076 | retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), false, |
| 5077 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5078 | if (retType.isNull()) return QualType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5079 | |
| 5080 | if (Unqualified) |
| 5081 | retType = retType.getUnqualifiedType(); |
| 5082 | |
| 5083 | CanQualType LRetType = getCanonicalType(lbase->getResultType()); |
| 5084 | CanQualType RRetType = getCanonicalType(rbase->getResultType()); |
| 5085 | if (Unqualified) { |
| 5086 | LRetType = LRetType.getUnqualifiedType(); |
| 5087 | RRetType = RRetType.getUnqualifiedType(); |
| 5088 | } |
| 5089 | |
| 5090 | if (getCanonicalType(retType) != LRetType) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5091 | allLTypes = false; |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5092 | if (getCanonicalType(retType) != RRetType) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5093 | allRTypes = false; |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5094 | |
Daniel Dunbar | 6a15c85 | 2010-04-28 16:20:58 +0000 | [diff] [blame] | 5095 | // FIXME: double check this |
| 5096 | // FIXME: should we error if lbase->getRegParmAttr() != 0 && |
| 5097 | // rbase->getRegParmAttr() != 0 && |
| 5098 | // lbase->getRegParmAttr() != rbase->getRegParmAttr()? |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 5099 | FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo(); |
| 5100 | FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo(); |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5101 | |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 5102 | // Compatible functions must have compatible calling conventions |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5103 | if (!isSameCallConv(lbaseInfo.getCC(), rbaseInfo.getCC())) |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 5104 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5105 | |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5106 | // Regparm is part of the calling convention. |
| 5107 | if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm()) |
| 5108 | return QualType(); |
| 5109 | |
| 5110 | // It's noreturn if either type is. |
| 5111 | // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'. |
| 5112 | bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn(); |
| 5113 | if (NoReturn != lbaseInfo.getNoReturn()) |
| 5114 | allLTypes = false; |
| 5115 | if (NoReturn != rbaseInfo.getNoReturn()) |
| 5116 | allRTypes = false; |
| 5117 | |
| 5118 | FunctionType::ExtInfo einfo(NoReturn, |
| 5119 | lbaseInfo.getRegParm(), |
| 5120 | lbaseInfo.getCC()); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5121 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5122 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 5123 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 5124 | "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5125 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 5126 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 5127 | |
| 5128 | // Compatible functions must have the same number of arguments |
| 5129 | if (lproto_nargs != rproto_nargs) |
| 5130 | return QualType(); |
| 5131 | |
| 5132 | // Variadic and non-variadic functions aren't compatible |
| 5133 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 5134 | return QualType(); |
| 5135 | |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 5136 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 5137 | return QualType(); |
| 5138 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5139 | // Check argument compatibility |
| 5140 | llvm::SmallVector<QualType, 10> types; |
| 5141 | for (unsigned i = 0; i < lproto_nargs; i++) { |
| 5142 | QualType largtype = lproto->getArgType(i).getUnqualifiedType(); |
| 5143 | QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 5144 | QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype, |
| 5145 | OfBlockPointer, |
| 5146 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5147 | if (argtype.isNull()) return QualType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5148 | |
| 5149 | if (Unqualified) |
| 5150 | argtype = argtype.getUnqualifiedType(); |
| 5151 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5152 | types.push_back(argtype); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5153 | if (Unqualified) { |
| 5154 | largtype = largtype.getUnqualifiedType(); |
| 5155 | rargtype = rargtype.getUnqualifiedType(); |
| 5156 | } |
| 5157 | |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5158 | if (getCanonicalType(argtype) != getCanonicalType(largtype)) |
| 5159 | allLTypes = false; |
| 5160 | if (getCanonicalType(argtype) != getCanonicalType(rargtype)) |
| 5161 | allRTypes = false; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5162 | } |
| 5163 | if (allLTypes) return lhs; |
| 5164 | if (allRTypes) return rhs; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5165 | |
| 5166 | FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo(); |
| 5167 | EPI.ExtInfo = einfo; |
| 5168 | return getFunctionType(retType, types.begin(), types.size(), EPI); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5169 | } |
| 5170 | |
| 5171 | if (lproto) allRTypes = false; |
| 5172 | if (rproto) allLTypes = false; |
| 5173 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5174 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5175 | if (proto) { |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 5176 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5177 | if (proto->isVariadic()) return QualType(); |
| 5178 | // Check that the types are compatible with the types that |
| 5179 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 5180 | // The only types actually affected are promotable integer |
| 5181 | // types and floats, which would be passed as a different |
| 5182 | // type depending on whether the prototype is visible. |
| 5183 | unsigned proto_nargs = proto->getNumArgs(); |
| 5184 | for (unsigned i = 0; i < proto_nargs; ++i) { |
| 5185 | QualType argTy = proto->getArgType(i); |
Douglas Gregor | b0f8eac | 2010-02-03 19:27:29 +0000 | [diff] [blame] | 5186 | |
| 5187 | // Look at the promotion type of enum types, since that is the type used |
| 5188 | // to pass enum values. |
| 5189 | if (const EnumType *Enum = argTy->getAs<EnumType>()) |
| 5190 | argTy = Enum->getDecl()->getPromotionType(); |
| 5191 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5192 | if (argTy->isPromotableIntegerType() || |
| 5193 | getCanonicalType(argTy).getUnqualifiedType() == FloatTy) |
| 5194 | return QualType(); |
| 5195 | } |
| 5196 | |
| 5197 | if (allLTypes) return lhs; |
| 5198 | if (allRTypes) return rhs; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5199 | |
| 5200 | FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo(); |
| 5201 | EPI.ExtInfo = einfo; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5202 | return getFunctionType(retType, proto->arg_type_begin(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5203 | proto->getNumArgs(), EPI); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5204 | } |
| 5205 | |
| 5206 | if (allLTypes) return lhs; |
| 5207 | if (allRTypes) return rhs; |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5208 | return getFunctionNoProtoType(retType, einfo); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5209 | } |
| 5210 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5211 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5212 | bool OfBlockPointer, |
| 5213 | bool Unqualified) { |
Bill Wendling | 43d6975 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 5214 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 5215 | // type is adjusted to "T" prior to any further analysis, the expression |
| 5216 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 5217 | // expression is an lvalue unless the reference is an rvalue reference and |
| 5218 | // the expression is a function call (possibly inside parentheses). |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 5219 | assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?"); |
| 5220 | assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?"); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5221 | |
| 5222 | if (Unqualified) { |
| 5223 | LHS = LHS.getUnqualifiedType(); |
| 5224 | RHS = RHS.getUnqualifiedType(); |
| 5225 | } |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 5226 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5227 | QualType LHSCan = getCanonicalType(LHS), |
| 5228 | RHSCan = getCanonicalType(RHS); |
| 5229 | |
| 5230 | // If two types are identical, they are compatible. |
| 5231 | if (LHSCan == RHSCan) |
| 5232 | return LHS; |
| 5233 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5234 | // If the qualifiers are different, the types aren't compatible... mostly. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 5235 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 5236 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5237 | if (LQuals != RQuals) { |
| 5238 | // If any of these qualifiers are different, we have a type |
| 5239 | // mismatch. |
| 5240 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 5241 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 5242 | return QualType(); |
| 5243 | |
| 5244 | // Exactly one GC qualifier difference is allowed: __strong is |
| 5245 | // okay if the other type has no GC qualifier but is an Objective |
| 5246 | // C object pointer (i.e. implicitly strong by default). We fix |
| 5247 | // this by pretending that the unqualified type was actually |
| 5248 | // qualified __strong. |
| 5249 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 5250 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 5251 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 5252 | |
| 5253 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 5254 | return QualType(); |
| 5255 | |
| 5256 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
| 5257 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); |
| 5258 | } |
| 5259 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
| 5260 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); |
| 5261 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5262 | return QualType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5263 | } |
| 5264 | |
| 5265 | // Okay, qualifiers are equal. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5266 | |
Eli Friedman | 852d63b | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 5267 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 5268 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5269 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5270 | // We want to consider the two function types to be the same for these |
| 5271 | // comparisons, just force one to the other. |
| 5272 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 5273 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 4c721d3 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 5274 | |
| 5275 | // Same as above for arrays |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 5276 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 5277 | LHSClass = Type::ConstantArray; |
| 5278 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 5279 | RHSClass = Type::ConstantArray; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5280 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5281 | // ObjCInterfaces are just specialized ObjCObjects. |
| 5282 | if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject; |
| 5283 | if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject; |
| 5284 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 5285 | // Canonicalize ExtVector -> Vector. |
| 5286 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 5287 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5288 | |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 5289 | // If the canonical type classes don't match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5290 | if (LHSClass != RHSClass) { |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5291 | // 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] | 5292 | // a signed integer type, or an unsigned integer type. |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 5293 | // Compatibility is based on the underlying type, not the promotion |
| 5294 | // type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5295 | if (const EnumType* ETy = LHS->getAs<EnumType>()) { |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5296 | if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType()) |
| 5297 | return RHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 5298 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5299 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5300 | if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType()) |
| 5301 | return LHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 5302 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5303 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5304 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5305 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5306 | |
Steve Naroff | 4a74678 | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 5307 | // The canonical type classes match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5308 | switch (LHSClass) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5309 | #define TYPE(Class, Base) |
| 5310 | #define ABSTRACT_TYPE(Class, Base) |
John McCall | ad5e738 | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 5311 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5312 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 5313 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 5314 | #include "clang/AST/TypeNodes.def" |
| 5315 | assert(false && "Non-canonical and dependent types shouldn't get here"); |
| 5316 | return QualType(); |
| 5317 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 5318 | case Type::LValueReference: |
| 5319 | case Type::RValueReference: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5320 | case Type::MemberPointer: |
| 5321 | assert(false && "C++ should never be in mergeTypes"); |
| 5322 | return QualType(); |
| 5323 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5324 | case Type::ObjCInterface: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5325 | case Type::IncompleteArray: |
| 5326 | case Type::VariableArray: |
| 5327 | case Type::FunctionProto: |
| 5328 | case Type::ExtVector: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5329 | assert(false && "Types are eliminated above"); |
| 5330 | return QualType(); |
| 5331 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5332 | case Type::Pointer: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5333 | { |
| 5334 | // Merge two pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5335 | QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); |
| 5336 | QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5337 | if (Unqualified) { |
| 5338 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 5339 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 5340 | } |
| 5341 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false, |
| 5342 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5343 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 5344 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5345 | return LHS; |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 5346 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5347 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5348 | return getPointerType(ResultType); |
| 5349 | } |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 5350 | case Type::BlockPointer: |
| 5351 | { |
| 5352 | // Merge two block 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<BlockPointerType>()->getPointeeType(); |
| 5354 | QualType RHSPointee = RHS->getAs<BlockPointerType>()->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, OfBlockPointer, |
| 5360 | Unqualified); |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 5361 | if (ResultType.isNull()) return QualType(); |
| 5362 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 5363 | return LHS; |
| 5364 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 5365 | return RHS; |
| 5366 | return getBlockPointerType(ResultType); |
| 5367 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5368 | case Type::ConstantArray: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5369 | { |
| 5370 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 5371 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 5372 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 5373 | return QualType(); |
| 5374 | |
| 5375 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 5376 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5377 | if (Unqualified) { |
| 5378 | LHSElem = LHSElem.getUnqualifiedType(); |
| 5379 | RHSElem = RHSElem.getUnqualifiedType(); |
| 5380 | } |
| 5381 | |
| 5382 | QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5383 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5384 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 5385 | return LHS; |
| 5386 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 5387 | return RHS; |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 5388 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 5389 | ArrayType::ArraySizeModifier(), 0); |
| 5390 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 5391 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5392 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 5393 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5394 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 5395 | return LHS; |
| 5396 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 5397 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5398 | if (LVAT) { |
| 5399 | // FIXME: This isn't correct! But tricky to implement because |
| 5400 | // the array's size has to be the size of LHS, but the type |
| 5401 | // has to be different. |
| 5402 | return LHS; |
| 5403 | } |
| 5404 | if (RVAT) { |
| 5405 | // FIXME: This isn't correct! But tricky to implement because |
| 5406 | // the array's size has to be the size of RHS, but the type |
| 5407 | // has to be different. |
| 5408 | return RHS; |
| 5409 | } |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 5410 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 5411 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 5412 | return getIncompleteArrayType(ResultType, |
| 5413 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5414 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5415 | case Type::FunctionNoProto: |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5416 | return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5417 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5418 | case Type::Enum: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5419 | return QualType(); |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5420 | case Type::Builtin: |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 5421 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5422 | return QualType(); |
Daniel Dunbar | 64cfdb7 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 5423 | case Type::Complex: |
| 5424 | // Distinct complex types are incompatible. |
| 5425 | return QualType(); |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 5426 | case Type::Vector: |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 5427 | // FIXME: The merged type should be an ExtVector! |
John McCall | 1c471f3 | 2010-03-12 23:14:13 +0000 | [diff] [blame] | 5428 | if (areCompatVectorTypes(LHSCan->getAs<VectorType>(), |
| 5429 | RHSCan->getAs<VectorType>())) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5430 | return LHS; |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5431 | return QualType(); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5432 | case Type::ObjCObject: { |
| 5433 | // Check if the types are assignment compatible. |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 5434 | // FIXME: This should be type compatibility, e.g. whether |
| 5435 | // "LHS x; RHS x;" at global scope is legal. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5436 | const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>(); |
| 5437 | const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>(); |
| 5438 | if (canAssignObjCInterfaces(LHSIface, RHSIface)) |
Steve Naroff | 5fd659d | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 5439 | return LHS; |
| 5440 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5441 | return QualType(); |
Cedric Venet | 61490e9 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 5442 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5443 | case Type::ObjCObjectPointer: { |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5444 | if (OfBlockPointer) { |
| 5445 | if (canAssignObjCInterfacesInBlockPointer( |
| 5446 | LHS->getAs<ObjCObjectPointerType>(), |
| 5447 | RHS->getAs<ObjCObjectPointerType>())) |
| 5448 | return LHS; |
| 5449 | return QualType(); |
| 5450 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5451 | if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(), |
| 5452 | RHS->getAs<ObjCObjectPointerType>())) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5453 | return LHS; |
| 5454 | |
Steve Naroff | bc76dd0 | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 5455 | return QualType(); |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5456 | } |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5457 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5458 | |
| 5459 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5460 | } |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 5461 | |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 5462 | /// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and |
| 5463 | /// 'RHS' attributes and returns the merged version; including for function |
| 5464 | /// return types. |
| 5465 | QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) { |
| 5466 | QualType LHSCan = getCanonicalType(LHS), |
| 5467 | RHSCan = getCanonicalType(RHS); |
| 5468 | // If two types are identical, they are compatible. |
| 5469 | if (LHSCan == RHSCan) |
| 5470 | return LHS; |
| 5471 | if (RHSCan->isFunctionType()) { |
| 5472 | if (!LHSCan->isFunctionType()) |
| 5473 | return QualType(); |
| 5474 | QualType OldReturnType = |
| 5475 | cast<FunctionType>(RHSCan.getTypePtr())->getResultType(); |
| 5476 | QualType NewReturnType = |
| 5477 | cast<FunctionType>(LHSCan.getTypePtr())->getResultType(); |
| 5478 | QualType ResReturnType = |
| 5479 | mergeObjCGCQualifiers(NewReturnType, OldReturnType); |
| 5480 | if (ResReturnType.isNull()) |
| 5481 | return QualType(); |
| 5482 | if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) { |
| 5483 | // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo(); |
| 5484 | // In either case, use OldReturnType to build the new function type. |
| 5485 | const FunctionType *F = LHS->getAs<FunctionType>(); |
| 5486 | if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5487 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 5488 | EPI.ExtInfo = getFunctionExtInfo(LHS); |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 5489 | QualType ResultType |
| 5490 | = getFunctionType(OldReturnType, FPT->arg_type_begin(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5491 | FPT->getNumArgs(), EPI); |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 5492 | return ResultType; |
| 5493 | } |
| 5494 | } |
| 5495 | return QualType(); |
| 5496 | } |
| 5497 | |
| 5498 | // If the qualifiers are different, the types can still be merged. |
| 5499 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 5500 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
| 5501 | if (LQuals != RQuals) { |
| 5502 | // If any of these qualifiers are different, we have a type mismatch. |
| 5503 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 5504 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 5505 | return QualType(); |
| 5506 | |
| 5507 | // Exactly one GC qualifier difference is allowed: __strong is |
| 5508 | // okay if the other type has no GC qualifier but is an Objective |
| 5509 | // C object pointer (i.e. implicitly strong by default). We fix |
| 5510 | // this by pretending that the unqualified type was actually |
| 5511 | // qualified __strong. |
| 5512 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 5513 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 5514 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 5515 | |
| 5516 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 5517 | return QualType(); |
| 5518 | |
| 5519 | if (GC_L == Qualifiers::Strong) |
| 5520 | return LHS; |
| 5521 | if (GC_R == Qualifiers::Strong) |
| 5522 | return RHS; |
| 5523 | return QualType(); |
| 5524 | } |
| 5525 | |
| 5526 | if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) { |
| 5527 | QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5528 | QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5529 | QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT); |
| 5530 | if (ResQT == LHSBaseQT) |
| 5531 | return LHS; |
| 5532 | if (ResQT == RHSBaseQT) |
| 5533 | return RHS; |
| 5534 | } |
| 5535 | return QualType(); |
| 5536 | } |
| 5537 | |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 5538 | //===----------------------------------------------------------------------===// |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5539 | // Integer Predicates |
| 5540 | //===----------------------------------------------------------------------===// |
Chris Lattner | 88054de | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 5541 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5542 | unsigned ASTContext::getIntWidth(QualType T) const { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 5543 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
Eli Friedman | 29a7f33 | 2009-12-10 22:29:29 +0000 | [diff] [blame] | 5544 | T = ET->getDecl()->getIntegerType(); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 5545 | if (T->isBooleanType()) |
| 5546 | return 1; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 5547 | // For builtin types, just use the standard type sizing method |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5548 | return (unsigned)getTypeSize(T); |
| 5549 | } |
| 5550 | |
| 5551 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) { |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5552 | assert(T->hasSignedIntegerRepresentation() && "Unexpected type"); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5553 | |
| 5554 | // Turn <4 x signed int> -> <4 x unsigned int> |
| 5555 | if (const VectorType *VTy = T->getAs<VectorType>()) |
| 5556 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 5557 | VTy->getNumElements(), VTy->getVectorKind()); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5558 | |
| 5559 | // For enums, we return the unsigned version of the base type. |
| 5560 | if (const EnumType *ETy = T->getAs<EnumType>()) |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5561 | T = ETy->getDecl()->getIntegerType(); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5562 | |
| 5563 | const BuiltinType *BTy = T->getAs<BuiltinType>(); |
| 5564 | assert(BTy && "Unexpected signed integer type"); |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5565 | switch (BTy->getKind()) { |
| 5566 | case BuiltinType::Char_S: |
| 5567 | case BuiltinType::SChar: |
| 5568 | return UnsignedCharTy; |
| 5569 | case BuiltinType::Short: |
| 5570 | return UnsignedShortTy; |
| 5571 | case BuiltinType::Int: |
| 5572 | return UnsignedIntTy; |
| 5573 | case BuiltinType::Long: |
| 5574 | return UnsignedLongTy; |
| 5575 | case BuiltinType::LongLong: |
| 5576 | return UnsignedLongLongTy; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 5577 | case BuiltinType::Int128: |
| 5578 | return UnsignedInt128Ty; |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5579 | default: |
| 5580 | assert(0 && "Unexpected signed integer type"); |
| 5581 | return QualType(); |
| 5582 | } |
| 5583 | } |
| 5584 | |
Argyrios Kyrtzidis | 7b90340 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 5585 | ASTMutationListener::~ASTMutationListener() { } |
| 5586 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5587 | |
| 5588 | //===----------------------------------------------------------------------===// |
| 5589 | // Builtin Type Computation |
| 5590 | //===----------------------------------------------------------------------===// |
| 5591 | |
| 5592 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5593 | /// pointer over the consumed characters. This returns the resultant type. If |
| 5594 | /// AllowTypeModifiers is false then modifier like * are not parsed, just basic |
| 5595 | /// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of |
| 5596 | /// a vector of "i*". |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5597 | /// |
| 5598 | /// RequiresICE is filled in on return to indicate whether the value is required |
| 5599 | /// to be an Integer Constant Expression. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5600 | static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context, |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5601 | ASTContext::GetBuiltinTypeError &Error, |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5602 | bool &RequiresICE, |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5603 | bool AllowTypeModifiers) { |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5604 | // Modifiers. |
| 5605 | int HowLong = 0; |
| 5606 | bool Signed = false, Unsigned = false; |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5607 | RequiresICE = false; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 5608 | |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5609 | // Read the prefixed modifiers first. |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5610 | bool Done = false; |
| 5611 | while (!Done) { |
| 5612 | switch (*Str++) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5613 | default: Done = true; --Str; break; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 5614 | case 'I': |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5615 | RequiresICE = true; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 5616 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5617 | case 'S': |
| 5618 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 5619 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 5620 | Signed = true; |
| 5621 | break; |
| 5622 | case 'U': |
| 5623 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
| 5624 | assert(!Unsigned && "Can't use 'S' modifier multiple times!"); |
| 5625 | Unsigned = true; |
| 5626 | break; |
| 5627 | case 'L': |
| 5628 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 5629 | ++HowLong; |
| 5630 | break; |
| 5631 | } |
| 5632 | } |
| 5633 | |
| 5634 | QualType Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5635 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5636 | // Read the base type. |
| 5637 | switch (*Str++) { |
| 5638 | default: assert(0 && "Unknown builtin type letter!"); |
| 5639 | case 'v': |
| 5640 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 5641 | "Bad modifiers used with 'v'!"); |
| 5642 | Type = Context.VoidTy; |
| 5643 | break; |
| 5644 | case 'f': |
| 5645 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 5646 | "Bad modifiers used with 'f'!"); |
| 5647 | Type = Context.FloatTy; |
| 5648 | break; |
| 5649 | case 'd': |
| 5650 | assert(HowLong < 2 && !Signed && !Unsigned && |
| 5651 | "Bad modifiers used with 'd'!"); |
| 5652 | if (HowLong) |
| 5653 | Type = Context.LongDoubleTy; |
| 5654 | else |
| 5655 | Type = Context.DoubleTy; |
| 5656 | break; |
| 5657 | case 's': |
| 5658 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 5659 | if (Unsigned) |
| 5660 | Type = Context.UnsignedShortTy; |
| 5661 | else |
| 5662 | Type = Context.ShortTy; |
| 5663 | break; |
| 5664 | case 'i': |
| 5665 | if (HowLong == 3) |
| 5666 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 5667 | else if (HowLong == 2) |
| 5668 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 5669 | else if (HowLong == 1) |
| 5670 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 5671 | else |
| 5672 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 5673 | break; |
| 5674 | case 'c': |
| 5675 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 5676 | if (Signed) |
| 5677 | Type = Context.SignedCharTy; |
| 5678 | else if (Unsigned) |
| 5679 | Type = Context.UnsignedCharTy; |
| 5680 | else |
| 5681 | Type = Context.CharTy; |
| 5682 | break; |
| 5683 | case 'b': // boolean |
| 5684 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 5685 | Type = Context.BoolTy; |
| 5686 | break; |
| 5687 | case 'z': // size_t. |
| 5688 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 5689 | Type = Context.getSizeType(); |
| 5690 | break; |
| 5691 | case 'F': |
| 5692 | Type = Context.getCFConstantStringType(); |
| 5693 | break; |
Fariborz Jahanian | ba8bda0 | 2010-11-09 21:38:20 +0000 | [diff] [blame] | 5694 | case 'G': |
| 5695 | Type = Context.getObjCIdType(); |
| 5696 | break; |
| 5697 | case 'H': |
| 5698 | Type = Context.getObjCSelType(); |
| 5699 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5700 | case 'a': |
| 5701 | Type = Context.getBuiltinVaListType(); |
| 5702 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 5703 | break; |
| 5704 | case 'A': |
| 5705 | // This is a "reference" to a va_list; however, what exactly |
| 5706 | // this means depends on how va_list is defined. There are two |
| 5707 | // different kinds of va_list: ones passed by value, and ones |
| 5708 | // passed by reference. An example of a by-value va_list is |
| 5709 | // x86, where va_list is a char*. An example of by-ref va_list |
| 5710 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 5711 | // we want this argument to be a char*&; for x86-64, we want |
| 5712 | // it to be a __va_list_tag*. |
| 5713 | Type = Context.getBuiltinVaListType(); |
| 5714 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5715 | if (Type->isArrayType()) |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5716 | Type = Context.getArrayDecayedType(Type); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5717 | else |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5718 | Type = Context.getLValueReferenceType(Type); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5719 | break; |
| 5720 | case 'V': { |
| 5721 | char *End; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5722 | unsigned NumElements = strtoul(Str, &End, 10); |
| 5723 | assert(End != Str && "Missing vector size"); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5724 | Str = End; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5725 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5726 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, |
| 5727 | RequiresICE, false); |
| 5728 | assert(!RequiresICE && "Can't require vector ICE"); |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5729 | |
| 5730 | // TODO: No way to make AltiVec vectors in builtins yet. |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 5731 | Type = Context.getVectorType(ElementType, NumElements, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 5732 | VectorType::GenericVector); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5733 | break; |
| 5734 | } |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 5735 | case 'X': { |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5736 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
| 5737 | false); |
| 5738 | assert(!RequiresICE && "Can't require complex ICE"); |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 5739 | Type = Context.getComplexType(ElementType); |
| 5740 | break; |
| 5741 | } |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 5742 | case 'P': |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 5743 | Type = Context.getFILEType(); |
| 5744 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 5745 | Error = ASTContext::GE_Missing_stdio; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5746 | return QualType(); |
| 5747 | } |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 5748 | break; |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 5749 | case 'J': |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 5750 | if (Signed) |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 5751 | Type = Context.getsigjmp_bufType(); |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 5752 | else |
| 5753 | Type = Context.getjmp_bufType(); |
| 5754 | |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 5755 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 5756 | Error = ASTContext::GE_Missing_setjmp; |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 5757 | return QualType(); |
| 5758 | } |
| 5759 | break; |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 5760 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5761 | |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5762 | // If there are modifiers and if we're allowed to parse them, go for it. |
| 5763 | Done = !AllowTypeModifiers; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5764 | while (!Done) { |
John McCall | 187ab37 | 2010-03-12 04:21:28 +0000 | [diff] [blame] | 5765 | switch (char c = *Str++) { |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5766 | default: Done = true; --Str; break; |
| 5767 | case '*': |
| 5768 | case '&': { |
| 5769 | // Both pointers and references can have their pointee types |
| 5770 | // qualified with an address space. |
| 5771 | char *End; |
| 5772 | unsigned AddrSpace = strtoul(Str, &End, 10); |
| 5773 | if (End != Str && AddrSpace != 0) { |
| 5774 | Type = Context.getAddrSpaceQualType(Type, AddrSpace); |
| 5775 | Str = End; |
| 5776 | } |
| 5777 | if (c == '*') |
| 5778 | Type = Context.getPointerType(Type); |
| 5779 | else |
| 5780 | Type = Context.getLValueReferenceType(Type); |
| 5781 | break; |
| 5782 | } |
| 5783 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 5784 | case 'C': |
| 5785 | Type = Type.withConst(); |
| 5786 | break; |
| 5787 | case 'D': |
| 5788 | Type = Context.getVolatileType(Type); |
| 5789 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5790 | } |
| 5791 | } |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 5792 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5793 | assert((!RequiresICE || Type->isIntegralOrEnumerationType()) && |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 5794 | "Integer constant 'I' type must be an integer"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5795 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5796 | return Type; |
| 5797 | } |
| 5798 | |
| 5799 | /// GetBuiltinType - Return the type for the specified builtin. |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5800 | QualType ASTContext::GetBuiltinType(unsigned Id, |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5801 | GetBuiltinTypeError &Error, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5802 | unsigned *IntegerConstantArgs) const { |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5803 | const char *TypeStr = BuiltinInfo.GetTypeString(Id); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5804 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5805 | llvm::SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5806 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5807 | bool RequiresICE = false; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5808 | Error = GE_None; |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5809 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error, |
| 5810 | RequiresICE, true); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5811 | if (Error != GE_None) |
| 5812 | return QualType(); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5813 | |
| 5814 | assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE"); |
| 5815 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5816 | while (TypeStr[0] && TypeStr[0] != '.') { |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5817 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5818 | if (Error != GE_None) |
| 5819 | return QualType(); |
| 5820 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5821 | // If this argument is required to be an IntegerConstantExpression and the |
| 5822 | // caller cares, fill in the bitmask we return. |
| 5823 | if (RequiresICE && IntegerConstantArgs) |
| 5824 | *IntegerConstantArgs |= 1 << ArgTypes.size(); |
| 5825 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5826 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 5827 | if (Ty->isArrayType()) |
| 5828 | Ty = getArrayDecayedType(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5829 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5830 | ArgTypes.push_back(Ty); |
| 5831 | } |
| 5832 | |
| 5833 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 5834 | "'.' should only occur at end of builtin type list!"); |
| 5835 | |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 5836 | FunctionType::ExtInfo EI; |
| 5837 | if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true); |
| 5838 | |
| 5839 | bool Variadic = (TypeStr[0] == '.'); |
| 5840 | |
| 5841 | // We really shouldn't be making a no-proto type here, especially in C++. |
| 5842 | if (ArgTypes.empty() && Variadic) |
| 5843 | return getFunctionNoProtoType(ResType, EI); |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 5844 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5845 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 5846 | EPI.ExtInfo = EI; |
| 5847 | EPI.Variadic = Variadic; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5848 | |
| 5849 | return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), EPI); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5850 | } |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5851 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5852 | GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) { |
| 5853 | GVALinkage External = GVA_StrongExternal; |
| 5854 | |
| 5855 | Linkage L = FD->getLinkage(); |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5856 | switch (L) { |
| 5857 | case NoLinkage: |
| 5858 | case InternalLinkage: |
| 5859 | case UniqueExternalLinkage: |
| 5860 | return GVA_Internal; |
| 5861 | |
| 5862 | case ExternalLinkage: |
| 5863 | switch (FD->getTemplateSpecializationKind()) { |
| 5864 | case TSK_Undeclared: |
| 5865 | case TSK_ExplicitSpecialization: |
| 5866 | External = GVA_StrongExternal; |
| 5867 | break; |
| 5868 | |
| 5869 | case TSK_ExplicitInstantiationDefinition: |
| 5870 | return GVA_ExplicitTemplateInstantiation; |
| 5871 | |
| 5872 | case TSK_ExplicitInstantiationDeclaration: |
| 5873 | case TSK_ImplicitInstantiation: |
| 5874 | External = GVA_TemplateInstantiation; |
| 5875 | break; |
| 5876 | } |
| 5877 | } |
| 5878 | |
| 5879 | if (!FD->isInlined()) |
| 5880 | return External; |
| 5881 | |
| 5882 | if (!getLangOptions().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) { |
| 5883 | // GNU or C99 inline semantics. Determine whether this symbol should be |
| 5884 | // externally visible. |
| 5885 | if (FD->isInlineDefinitionExternallyVisible()) |
| 5886 | return External; |
| 5887 | |
| 5888 | // C99 inline semantics, where the symbol is not externally visible. |
| 5889 | return GVA_C99Inline; |
| 5890 | } |
| 5891 | |
| 5892 | // C++0x [temp.explicit]p9: |
| 5893 | // [ Note: The intent is that an inline function that is the subject of |
| 5894 | // an explicit instantiation declaration will still be implicitly |
| 5895 | // instantiated when used so that the body can be considered for |
| 5896 | // inlining, but that no out-of-line copy of the inline function would be |
| 5897 | // generated in the translation unit. -- end note ] |
| 5898 | if (FD->getTemplateSpecializationKind() |
| 5899 | == TSK_ExplicitInstantiationDeclaration) |
| 5900 | return GVA_C99Inline; |
| 5901 | |
| 5902 | return GVA_CXXInline; |
| 5903 | } |
| 5904 | |
| 5905 | GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) { |
| 5906 | // If this is a static data member, compute the kind of template |
| 5907 | // specialization. Otherwise, this variable is not part of a |
| 5908 | // template. |
| 5909 | TemplateSpecializationKind TSK = TSK_Undeclared; |
| 5910 | if (VD->isStaticDataMember()) |
| 5911 | TSK = VD->getTemplateSpecializationKind(); |
| 5912 | |
| 5913 | Linkage L = VD->getLinkage(); |
| 5914 | if (L == ExternalLinkage && getLangOptions().CPlusPlus && |
| 5915 | VD->getType()->getLinkage() == UniqueExternalLinkage) |
| 5916 | L = UniqueExternalLinkage; |
| 5917 | |
| 5918 | switch (L) { |
| 5919 | case NoLinkage: |
| 5920 | case InternalLinkage: |
| 5921 | case UniqueExternalLinkage: |
| 5922 | return GVA_Internal; |
| 5923 | |
| 5924 | case ExternalLinkage: |
| 5925 | switch (TSK) { |
| 5926 | case TSK_Undeclared: |
| 5927 | case TSK_ExplicitSpecialization: |
| 5928 | return GVA_StrongExternal; |
| 5929 | |
| 5930 | case TSK_ExplicitInstantiationDeclaration: |
| 5931 | llvm_unreachable("Variable should not be instantiated"); |
| 5932 | // Fall through to treat this like any other instantiation. |
| 5933 | |
| 5934 | case TSK_ExplicitInstantiationDefinition: |
| 5935 | return GVA_ExplicitTemplateInstantiation; |
| 5936 | |
| 5937 | case TSK_ImplicitInstantiation: |
| 5938 | return GVA_TemplateInstantiation; |
| 5939 | } |
| 5940 | } |
| 5941 | |
| 5942 | return GVA_StrongExternal; |
| 5943 | } |
| 5944 | |
Argyrios Kyrtzidis | 4ac7c0b | 2010-07-29 20:08:05 +0000 | [diff] [blame] | 5945 | bool ASTContext::DeclMustBeEmitted(const Decl *D) { |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5946 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 5947 | if (!VD->isFileVarDecl()) |
| 5948 | return false; |
| 5949 | } else if (!isa<FunctionDecl>(D)) |
| 5950 | return false; |
| 5951 | |
Argyrios Kyrtzidis | ab411c8 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 5952 | // Weak references don't produce any output by themselves. |
| 5953 | if (D->hasAttr<WeakRefAttr>()) |
| 5954 | return false; |
| 5955 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5956 | // Aliases and used decls are required. |
| 5957 | if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>()) |
| 5958 | return true; |
| 5959 | |
| 5960 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 5961 | // Forward declarations aren't required. |
| 5962 | if (!FD->isThisDeclarationADefinition()) |
| 5963 | return false; |
| 5964 | |
| 5965 | // Constructors and destructors are required. |
| 5966 | if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>()) |
| 5967 | return true; |
| 5968 | |
| 5969 | // The key function for a class is required. |
| 5970 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 5971 | const CXXRecordDecl *RD = MD->getParent(); |
| 5972 | if (MD->isOutOfLine() && RD->isDynamicClass()) { |
| 5973 | const CXXMethodDecl *KeyFunc = getKeyFunction(RD); |
| 5974 | if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl()) |
| 5975 | return true; |
| 5976 | } |
| 5977 | } |
| 5978 | |
| 5979 | GVALinkage Linkage = GetGVALinkageForFunction(FD); |
| 5980 | |
| 5981 | // static, static inline, always_inline, and extern inline functions can |
| 5982 | // always be deferred. Normal inline functions can be deferred in C99/C++. |
| 5983 | // Implicit template instantiations can also be deferred in C++. |
| 5984 | if (Linkage == GVA_Internal || Linkage == GVA_C99Inline || |
| 5985 | Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) |
| 5986 | return false; |
| 5987 | return true; |
| 5988 | } |
| 5989 | |
| 5990 | const VarDecl *VD = cast<VarDecl>(D); |
| 5991 | assert(VD->isFileVarDecl() && "Expected file scoped var"); |
| 5992 | |
Argyrios Kyrtzidis | ab411c8 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 5993 | if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly) |
| 5994 | return false; |
| 5995 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5996 | // Structs that have non-trivial constructors or destructors are required. |
| 5997 | |
| 5998 | // FIXME: Handle references. |
| 5999 | if (const RecordType *RT = VD->getType()->getAs<RecordType>()) { |
| 6000 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 6001 | if (RD->hasDefinition() && |
| 6002 | (!RD->hasTrivialConstructor() || !RD->hasTrivialDestructor())) |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6003 | return true; |
| 6004 | } |
| 6005 | } |
| 6006 | |
| 6007 | GVALinkage L = GetGVALinkageForVariable(VD); |
| 6008 | if (L == GVA_Internal || L == GVA_TemplateInstantiation) { |
| 6009 | if (!(VD->getInit() && VD->getInit()->HasSideEffects(*this))) |
| 6010 | return false; |
| 6011 | } |
| 6012 | |
| 6013 | return true; |
| 6014 | } |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 6015 | |
Charles Davis | ee743f9 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 6016 | CallingConv ASTContext::getDefaultMethodCallConv() { |
| 6017 | // Pass through to the C++ ABI object |
| 6018 | return ABI->getDefaultMethodCallConv(); |
| 6019 | } |
| 6020 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6021 | bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const { |
Anders Carlsson | dae0cb5 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 6022 | // Pass through to the C++ ABI object |
| 6023 | return ABI->isNearlyEmpty(RD); |
| 6024 | } |
| 6025 | |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 6026 | MangleContext *ASTContext::createMangleContext() { |
| 6027 | switch (Target.getCXXABI()) { |
| 6028 | case CXXABI_ARM: |
| 6029 | case CXXABI_Itanium: |
| 6030 | return createItaniumMangleContext(*this, getDiagnostics()); |
| 6031 | case CXXABI_Microsoft: |
| 6032 | return createMicrosoftMangleContext(*this, getDiagnostics()); |
| 6033 | } |
| 6034 | assert(0 && "Unsupported ABI"); |
| 6035 | return 0; |
| 6036 | } |
| 6037 | |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 6038 | CXXABI::~CXXABI() {} |