Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 1 | //===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the ASTContext interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/ASTContext.h" |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 15 | #include "clang/AST/CharUnits.h" |
Argyrios Kyrtzidis | faf0876 | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclCXX.h" |
Steve Naroff | 67391b8 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclTemplate.h" |
Argyrios Kyrtzidis | 3f79ad7 | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeLoc.h" |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 20 | #include "clang/AST/Expr.h" |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 21 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExternalASTSource.h" |
Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 23 | #include "clang/AST/ASTMutationListener.h" |
Anders Carlsson | 15b73de | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 24 | #include "clang/AST/RecordLayout.h" |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 25 | #include "clang/AST/Mangle.h" |
Chris Lattner | 15ba949 | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 26 | #include "clang/Basic/Builtins.h" |
Chris Lattner | d286851 | 2009-03-28 03:45:20 +0000 | [diff] [blame] | 27 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 4dc8a6f | 2007-05-20 23:50:58 +0000 | [diff] [blame] | 28 | #include "clang/Basic/TargetInfo.h" |
Benjamin Kramer | 1402ce3 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/SmallString.h" |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/StringExtras.h" |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 31 | #include "llvm/Support/MathExtras.h" |
Benjamin Kramer | 1402ce3 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 32 | #include "llvm/Support/raw_ostream.h" |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 33 | #include "CXXABI.h" |
Anders Carlsson | a4267a6 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 34 | |
Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 35 | using namespace clang; |
| 36 | |
Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 37 | unsigned ASTContext::NumImplicitDefaultConstructors; |
| 38 | unsigned ASTContext::NumImplicitDefaultConstructorsDeclared; |
Douglas Gregor | a6d6950 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 39 | unsigned ASTContext::NumImplicitCopyConstructors; |
| 40 | unsigned ASTContext::NumImplicitCopyConstructorsDeclared; |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 41 | unsigned ASTContext::NumImplicitCopyAssignmentOperators; |
| 42 | unsigned ASTContext::NumImplicitCopyAssignmentOperatorsDeclared; |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 43 | unsigned ASTContext::NumImplicitDestructors; |
| 44 | unsigned ASTContext::NumImplicitDestructorsDeclared; |
| 45 | |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 46 | enum FloatingRank { |
| 47 | FloatRank, DoubleRank, LongDoubleRank |
| 48 | }; |
| 49 | |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 50 | void |
| 51 | ASTContext::CanonicalTemplateTemplateParm::Profile(llvm::FoldingSetNodeID &ID, |
| 52 | TemplateTemplateParmDecl *Parm) { |
| 53 | ID.AddInteger(Parm->getDepth()); |
| 54 | ID.AddInteger(Parm->getPosition()); |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 55 | ID.AddBoolean(Parm->isParameterPack()); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 56 | |
| 57 | TemplateParameterList *Params = Parm->getTemplateParameters(); |
| 58 | ID.AddInteger(Params->size()); |
| 59 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 60 | PEnd = Params->end(); |
| 61 | P != PEnd; ++P) { |
| 62 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
| 63 | ID.AddInteger(0); |
| 64 | ID.AddBoolean(TTP->isParameterPack()); |
| 65 | continue; |
| 66 | } |
| 67 | |
| 68 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 69 | ID.AddInteger(1); |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 70 | ID.AddBoolean(NTTP->isParameterPack()); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 71 | ID.AddPointer(NTTP->getType().getAsOpaquePtr()); |
| 72 | continue; |
| 73 | } |
| 74 | |
| 75 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); |
| 76 | ID.AddInteger(2); |
| 77 | Profile(ID, TTP); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | TemplateTemplateParmDecl * |
| 82 | ASTContext::getCanonicalTemplateTemplateParmDecl( |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 83 | TemplateTemplateParmDecl *TTP) const { |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 84 | // Check if we already have a canonical template template parameter. |
| 85 | llvm::FoldingSetNodeID ID; |
| 86 | CanonicalTemplateTemplateParm::Profile(ID, TTP); |
| 87 | void *InsertPos = 0; |
| 88 | CanonicalTemplateTemplateParm *Canonical |
| 89 | = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
| 90 | if (Canonical) |
| 91 | return Canonical->getParam(); |
| 92 | |
| 93 | // Build a canonical template parameter list. |
| 94 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 95 | llvm::SmallVector<NamedDecl *, 4> CanonParams; |
| 96 | CanonParams.reserve(Params->size()); |
| 97 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 98 | PEnd = Params->end(); |
| 99 | P != PEnd; ++P) { |
| 100 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) |
| 101 | CanonParams.push_back( |
| 102 | TemplateTypeParmDecl::Create(*this, getTranslationUnitDecl(), |
| 103 | SourceLocation(), TTP->getDepth(), |
| 104 | TTP->getIndex(), 0, false, |
| 105 | TTP->isParameterPack())); |
| 106 | else if (NonTypeTemplateParmDecl *NTTP |
| 107 | = dyn_cast<NonTypeTemplateParmDecl>(*P)) |
| 108 | CanonParams.push_back( |
| 109 | NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
| 110 | SourceLocation(), NTTP->getDepth(), |
| 111 | NTTP->getPosition(), 0, |
| 112 | getCanonicalType(NTTP->getType()), |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 113 | NTTP->isParameterPack(), |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 114 | 0)); |
| 115 | else |
| 116 | CanonParams.push_back(getCanonicalTemplateTemplateParmDecl( |
| 117 | cast<TemplateTemplateParmDecl>(*P))); |
| 118 | } |
| 119 | |
| 120 | TemplateTemplateParmDecl *CanonTTP |
| 121 | = TemplateTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
| 122 | SourceLocation(), TTP->getDepth(), |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 123 | TTP->getPosition(), |
| 124 | TTP->isParameterPack(), |
| 125 | 0, |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 126 | TemplateParameterList::Create(*this, SourceLocation(), |
| 127 | SourceLocation(), |
| 128 | CanonParams.data(), |
| 129 | CanonParams.size(), |
| 130 | SourceLocation())); |
| 131 | |
| 132 | // Get the new insert position for the node we care about. |
| 133 | Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
| 134 | assert(Canonical == 0 && "Shouldn't be in the map!"); |
| 135 | (void)Canonical; |
| 136 | |
| 137 | // Create the canonical template template parameter entry. |
| 138 | Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP); |
| 139 | CanonTemplateTemplateParms.InsertNode(Canonical, InsertPos); |
| 140 | return CanonTTP; |
| 141 | } |
| 142 | |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 143 | CXXABI *ASTContext::createCXXABI(const TargetInfo &T) { |
John McCall | 8635341 | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 144 | if (!LangOpts.CPlusPlus) return 0; |
| 145 | |
Charles Davis | 6bcb07a | 2010-08-19 02:18:14 +0000 | [diff] [blame] | 146 | switch (T.getCXXABI()) { |
John McCall | 8635341 | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 147 | case CXXABI_ARM: |
| 148 | return CreateARMCXXABI(*this); |
| 149 | case CXXABI_Itanium: |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 150 | return CreateItaniumCXXABI(*this); |
Charles Davis | 6bcb07a | 2010-08-19 02:18:14 +0000 | [diff] [blame] | 151 | case CXXABI_Microsoft: |
| 152 | return CreateMicrosoftCXXABI(*this); |
| 153 | } |
John McCall | 8635341 | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 154 | return 0; |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 157 | ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM, |
Daniel Dunbar | 1b44419 | 2009-11-13 05:51:54 +0000 | [diff] [blame] | 158 | const TargetInfo &t, |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 159 | IdentifierTable &idents, SelectorTable &sels, |
Chris Lattner | 15ba949 | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 160 | Builtin::Context &builtins, |
Douglas Gregor | 5b11d49 | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 161 | unsigned size_reserve) : |
John McCall | 773cc98 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 162 | TemplateSpecializationTypes(this_()), |
| 163 | DependentTemplateSpecializationTypes(this_()), |
Argyrios Kyrtzidis | e862cbc | 2010-07-04 21:44:19 +0000 | [diff] [blame] | 164 | GlobalNestedNameSpecifier(0), IsInt128Installed(false), |
| 165 | CFConstantStringTypeDecl(0), NSConstantStringTypeDecl(0), |
Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 166 | ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0), |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 167 | sigjmp_bufDecl(0), BlockDescriptorType(0), BlockDescriptorExtendedType(0), |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 168 | NullTypeSourceInfo(QualType()), |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 169 | SourceMgr(SM), LangOpts(LOpts), ABI(createCXXABI(t)), Target(t), |
Douglas Gregor | c6d5edd | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 170 | Idents(idents), Selectors(sels), |
Ted Kremenek | 6aead3a | 2010-05-10 20:40:08 +0000 | [diff] [blame] | 171 | BuiltinInfo(builtins), |
| 172 | DeclarationNames(*this), |
Argyrios Kyrtzidis | 440ea32 | 2010-10-28 09:29:35 +0000 | [diff] [blame] | 173 | ExternalSource(0), Listener(0), PrintingPolicy(LOpts), |
Ted Kremenek | 520f47b | 2010-06-18 00:31:04 +0000 | [diff] [blame] | 174 | LastSDM(0, 0), |
| 175 | UniqueBlockByRefTypeID(0), UniqueBlockParmTypeID(0) { |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 176 | ObjCIdRedefinitionType = QualType(); |
| 177 | ObjCClassRedefinitionType = QualType(); |
Douglas Gregor | 5b11d49 | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 178 | ObjCSelRedefinitionType = QualType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 179 | if (size_reserve > 0) Types.reserve(size_reserve); |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 180 | TUDecl = TranslationUnitDecl::Create(*this); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 181 | InitBuiltinTypes(); |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 184 | ASTContext::~ASTContext() { |
Ted Kremenek | da4e0d3 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 185 | // Release the DenseMaps associated with DeclContext objects. |
| 186 | // FIXME: Is this the ideal solution? |
| 187 | ReleaseDeclContextMaps(); |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 188 | |
Douglas Gregor | 5b11d49 | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 189 | // Call all of the deallocation functions. |
| 190 | for (unsigned I = 0, N = Deallocations.size(); I != N; ++I) |
| 191 | Deallocations[I].first(Deallocations[I].second); |
Douglas Gregor | 1a80933 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 192 | |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 193 | // Release all of the memory associated with overridden C++ methods. |
| 194 | for (llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::iterator |
| 195 | OM = OverriddenMethods.begin(), OMEnd = OverriddenMethods.end(); |
| 196 | OM != OMEnd; ++OM) |
| 197 | OM->second.Destroy(); |
Ted Kremenek | da4e0d3 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 198 | |
Ted Kremenek | 076baeb | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 199 | // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed |
Douglas Gregor | 5b11d49 | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 200 | // because they can contain DenseMaps. |
| 201 | for (llvm::DenseMap<const ObjCContainerDecl*, |
| 202 | const ASTRecordLayout*>::iterator |
| 203 | I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) |
| 204 | // Increment in loop to prevent using deallocated memory. |
| 205 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 206 | R->Destroy(*this); |
| 207 | |
Ted Kremenek | 076baeb | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 208 | for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator |
| 209 | I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) { |
| 210 | // Increment in loop to prevent using deallocated memory. |
| 211 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 212 | R->Destroy(*this); |
| 213 | } |
Douglas Gregor | 561eceb | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 214 | |
| 215 | for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(), |
| 216 | AEnd = DeclAttrs.end(); |
| 217 | A != AEnd; ++A) |
| 218 | A->second->~AttrVec(); |
| 219 | } |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 220 | |
Douglas Gregor | 1a80933 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 221 | void ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) { |
| 222 | Deallocations.push_back(std::make_pair(Callback, Data)); |
| 223 | } |
| 224 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 225 | void |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 226 | ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) { |
| 227 | ExternalSource.reset(Source.take()); |
| 228 | } |
| 229 | |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 230 | void ASTContext::PrintStats() const { |
| 231 | fprintf(stderr, "*** AST Context Stats:\n"); |
| 232 | fprintf(stderr, " %d types total.\n", (int)Types.size()); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 233 | |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 234 | unsigned counts[] = { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 235 | #define TYPE(Name, Parent) 0, |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 236 | #define ABSTRACT_TYPE(Name, Parent) |
| 237 | #include "clang/AST/TypeNodes.def" |
| 238 | 0 // Extra |
| 239 | }; |
Douglas Gregor | b1fe2c9 | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 240 | |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 241 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 242 | Type *T = Types[i]; |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 243 | counts[(unsigned)T->getTypeClass()]++; |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 246 | unsigned Idx = 0; |
| 247 | unsigned TotalBytes = 0; |
| 248 | #define TYPE(Name, Parent) \ |
| 249 | if (counts[Idx]) \ |
| 250 | fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \ |
| 251 | TotalBytes += counts[Idx] * sizeof(Name##Type); \ |
| 252 | ++Idx; |
| 253 | #define ABSTRACT_TYPE(Name, Parent) |
| 254 | #include "clang/AST/TypeNodes.def" |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 255 | |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 256 | fprintf(stderr, "Total bytes = %d\n", int(TotalBytes)); |
Douglas Gregor | 747eb78 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 257 | |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 258 | // Implicit special member functions. |
Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 259 | fprintf(stderr, " %u/%u implicit default constructors created\n", |
| 260 | NumImplicitDefaultConstructorsDeclared, |
| 261 | NumImplicitDefaultConstructors); |
Douglas Gregor | a6d6950 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 262 | fprintf(stderr, " %u/%u implicit copy constructors created\n", |
| 263 | NumImplicitCopyConstructorsDeclared, |
| 264 | NumImplicitCopyConstructors); |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 265 | fprintf(stderr, " %u/%u implicit copy assignment operators created\n", |
| 266 | NumImplicitCopyAssignmentOperatorsDeclared, |
| 267 | NumImplicitCopyAssignmentOperators); |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 268 | fprintf(stderr, " %u/%u implicit destructors created\n", |
| 269 | NumImplicitDestructorsDeclared, NumImplicitDestructors); |
| 270 | |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 271 | if (ExternalSource.get()) { |
| 272 | fprintf(stderr, "\n"); |
| 273 | ExternalSource->PrintStats(); |
| 274 | } |
Douglas Gregor | 747eb78 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 275 | |
Douglas Gregor | 5b11d49 | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 276 | BumpAlloc.PrintStats(); |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 280 | void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) { |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 281 | BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K); |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 282 | R = CanQualType::CreateUnsafe(QualType(Ty, 0)); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 283 | Types.push_back(Ty); |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 286 | void ASTContext::InitBuiltinTypes() { |
| 287 | assert(VoidTy.isNull() && "Context reinitialized?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 288 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 289 | // C99 6.2.5p19. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 290 | InitBuiltinType(VoidTy, BuiltinType::Void); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 291 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 292 | // C99 6.2.5p2. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 293 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 294 | // C99 6.2.5p3. |
Eli Friedman | 9ffd4a9 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 295 | if (LangOpts.CharIsSigned) |
Chris Lattner | b16f455 | 2007-06-03 07:25:34 +0000 | [diff] [blame] | 296 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 297 | else |
| 298 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 299 | // C99 6.2.5p4. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 300 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 301 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 302 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 303 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 304 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 305 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 306 | // C99 6.2.5p6. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 307 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 308 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 309 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 310 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 311 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 312 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 313 | // C99 6.2.5p10. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 314 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 315 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 316 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 317 | |
Chris Lattner | f122cef | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 318 | // GNU extension, 128-bit integers. |
| 319 | InitBuiltinType(Int128Ty, BuiltinType::Int128); |
| 320 | InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128); |
| 321 | |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 322 | if (LangOpts.CPlusPlus) { // C++ 3.9.1p5 |
| 323 | if (!LangOpts.ShortWChar) |
| 324 | InitBuiltinType(WCharTy, BuiltinType::WChar_S); |
| 325 | else // -fshort-wchar makes wchar_t be unsigned. |
| 326 | InitBuiltinType(WCharTy, BuiltinType::WChar_U); |
| 327 | } else // C99 |
Chris Lattner | 007cb02 | 2009-02-26 23:43:47 +0000 | [diff] [blame] | 328 | WCharTy = getFromTargetType(Target.getWCharType()); |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 329 | |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 330 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 331 | InitBuiltinType(Char16Ty, BuiltinType::Char16); |
| 332 | else // C99 |
| 333 | Char16Ty = getFromTargetType(Target.getChar16Type()); |
| 334 | |
| 335 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 336 | InitBuiltinType(Char32Ty, BuiltinType::Char32); |
| 337 | else // C99 |
| 338 | Char32Ty = getFromTargetType(Target.getChar32Type()); |
| 339 | |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 340 | // Placeholder type for type-dependent expressions whose type is |
| 341 | // completely unknown. No code should ever check a type against |
| 342 | // DependentTy and users should never see it; however, it is here to |
| 343 | // help diagnose failures to properly check for type-dependent |
| 344 | // expressions. |
| 345 | InitBuiltinType(DependentTy, BuiltinType::Dependent); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 346 | |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 347 | // Placeholder type for functions. |
| 348 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
| 349 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 350 | // Placeholder type for C++0x auto declarations whose real type has |
Anders Carlsson | 082acde | 2009-06-26 18:41:36 +0000 | [diff] [blame] | 351 | // not yet been deduced. |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 352 | InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 353 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 354 | // C99 6.2.5p11. |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 355 | FloatComplexTy = getComplexType(FloatTy); |
| 356 | DoubleComplexTy = getComplexType(DoubleTy); |
| 357 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 358 | |
Steve Naroff | 66e9f33 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 359 | BuiltinVaListType = QualType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 360 | |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 361 | // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope(). |
| 362 | ObjCIdTypedefType = QualType(); |
| 363 | ObjCClassTypedefType = QualType(); |
Fariborz Jahanian | 252ba5f | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 364 | ObjCSelTypedefType = QualType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 365 | |
Fariborz Jahanian | 252ba5f | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 366 | // Builtin types for 'id', 'Class', and 'SEL'. |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 367 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); |
| 368 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); |
Fariborz Jahanian | 252ba5f | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 369 | InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 370 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 371 | ObjCConstantStringType = QualType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 372 | |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 373 | // void * type |
| 374 | VoidPtrTy = getPointerType(VoidTy); |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 375 | |
| 376 | // nullptr type (C++0x 2.14.7) |
| 377 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 380 | Diagnostic &ASTContext::getDiagnostics() const { |
| 381 | return SourceMgr.getDiagnostics(); |
| 382 | } |
| 383 | |
Douglas Gregor | 561eceb | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 384 | AttrVec& ASTContext::getDeclAttrs(const Decl *D) { |
| 385 | AttrVec *&Result = DeclAttrs[D]; |
| 386 | if (!Result) { |
| 387 | void *Mem = Allocate(sizeof(AttrVec)); |
| 388 | Result = new (Mem) AttrVec; |
| 389 | } |
| 390 | |
| 391 | return *Result; |
| 392 | } |
| 393 | |
| 394 | /// \brief Erase the attributes corresponding to the given declaration. |
| 395 | void ASTContext::eraseDeclAttrs(const Decl *D) { |
| 396 | llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D); |
| 397 | if (Pos != DeclAttrs.end()) { |
| 398 | Pos->second->~AttrVec(); |
| 399 | DeclAttrs.erase(Pos); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 404 | MemberSpecializationInfo * |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 405 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 406 | assert(Var->isStaticDataMember() && "Not a static data member"); |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 407 | llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 408 | = InstantiatedFromStaticDataMember.find(Var); |
| 409 | if (Pos == InstantiatedFromStaticDataMember.end()) |
| 410 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 411 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 412 | return Pos->second; |
| 413 | } |
| 414 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 415 | void |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 416 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, |
Argyrios Kyrtzidis | cdb8b3f | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 417 | TemplateSpecializationKind TSK, |
| 418 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 419 | assert(Inst->isStaticDataMember() && "Not a static data member"); |
| 420 | assert(Tmpl->isStaticDataMember() && "Not a static data member"); |
| 421 | assert(!InstantiatedFromStaticDataMember[Inst] && |
| 422 | "Already noted what static data member was instantiated from"); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 423 | InstantiatedFromStaticDataMember[Inst] |
Argyrios Kyrtzidis | cdb8b3f | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 424 | = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation); |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 425 | } |
| 426 | |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 427 | NamedDecl * |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 428 | ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) { |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 429 | llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 430 | = InstantiatedFromUsingDecl.find(UUD); |
| 431 | if (Pos == InstantiatedFromUsingDecl.end()) |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 432 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 433 | |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 434 | return Pos->second; |
| 435 | } |
| 436 | |
| 437 | void |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 438 | ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) { |
| 439 | assert((isa<UsingDecl>(Pattern) || |
| 440 | isa<UnresolvedUsingValueDecl>(Pattern) || |
| 441 | isa<UnresolvedUsingTypenameDecl>(Pattern)) && |
| 442 | "pattern decl is not a using decl"); |
| 443 | assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists"); |
| 444 | InstantiatedFromUsingDecl[Inst] = Pattern; |
| 445 | } |
| 446 | |
| 447 | UsingShadowDecl * |
| 448 | ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) { |
| 449 | llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos |
| 450 | = InstantiatedFromUsingShadowDecl.find(Inst); |
| 451 | if (Pos == InstantiatedFromUsingShadowDecl.end()) |
| 452 | return 0; |
| 453 | |
| 454 | return Pos->second; |
| 455 | } |
| 456 | |
| 457 | void |
| 458 | ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, |
| 459 | UsingShadowDecl *Pattern) { |
| 460 | assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists"); |
| 461 | InstantiatedFromUsingShadowDecl[Inst] = Pattern; |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 464 | FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { |
| 465 | llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos |
| 466 | = InstantiatedFromUnnamedFieldDecl.find(Field); |
| 467 | if (Pos == InstantiatedFromUnnamedFieldDecl.end()) |
| 468 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 469 | |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 470 | return Pos->second; |
| 471 | } |
| 472 | |
| 473 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, |
| 474 | FieldDecl *Tmpl) { |
| 475 | assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed"); |
| 476 | assert(!Tmpl->getDeclName() && "Template field decl is not unnamed"); |
| 477 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] && |
| 478 | "Already noted what unnamed field was instantiated from"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 479 | |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 480 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; |
| 481 | } |
| 482 | |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 483 | ASTContext::overridden_cxx_method_iterator |
| 484 | ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const { |
| 485 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 486 | = OverriddenMethods.find(Method); |
| 487 | if (Pos == OverriddenMethods.end()) |
| 488 | return 0; |
| 489 | |
| 490 | return Pos->second.begin(); |
| 491 | } |
| 492 | |
| 493 | ASTContext::overridden_cxx_method_iterator |
| 494 | ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const { |
| 495 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 496 | = OverriddenMethods.find(Method); |
| 497 | if (Pos == OverriddenMethods.end()) |
| 498 | return 0; |
| 499 | |
| 500 | return Pos->second.end(); |
| 501 | } |
| 502 | |
Argyrios Kyrtzidis | 6685e8a | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 503 | unsigned |
| 504 | ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const { |
| 505 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 506 | = OverriddenMethods.find(Method); |
| 507 | if (Pos == OverriddenMethods.end()) |
| 508 | return 0; |
| 509 | |
| 510 | return Pos->second.size(); |
| 511 | } |
| 512 | |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 513 | void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method, |
| 514 | const CXXMethodDecl *Overridden) { |
| 515 | OverriddenMethods[Method].push_back(Overridden); |
| 516 | } |
| 517 | |
Chris Lattner | 53cfe80 | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 518 | //===----------------------------------------------------------------------===// |
| 519 | // Type Sizing and Analysis |
| 520 | //===----------------------------------------------------------------------===// |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 521 | |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 522 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 523 | /// scalar floating point type. |
| 524 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 525 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 526 | assert(BT && "Not a floating point type!"); |
| 527 | switch (BT->getKind()) { |
| 528 | default: assert(0 && "Not a floating point type!"); |
| 529 | case BuiltinType::Float: return Target.getFloatFormat(); |
| 530 | case BuiltinType::Double: return Target.getDoubleFormat(); |
| 531 | case BuiltinType::LongDouble: return Target.getLongDoubleFormat(); |
| 532 | } |
| 533 | } |
| 534 | |
Ken Dyck | 160146e | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 535 | /// getDeclAlign - Return a conservative estimate of the alignment of the |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 536 | /// specified decl. Note that bitfields do not have a valid alignment, so |
| 537 | /// this method will assert on them. |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 538 | /// If @p RefAsPointee, references are treated like their underlying type |
| 539 | /// (for alignof), else they're treated like pointers (for CodeGen). |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 540 | CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) const { |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 541 | unsigned Align = Target.getCharWidth(); |
| 542 | |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 543 | bool UseAlignAttrOnly = false; |
| 544 | if (unsigned AlignFromAttr = D->getMaxAlignment()) { |
| 545 | Align = AlignFromAttr; |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 546 | |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 547 | // __attribute__((aligned)) can increase or decrease alignment |
| 548 | // *except* on a struct or struct member, where it only increases |
| 549 | // alignment unless 'packed' is also specified. |
| 550 | // |
| 551 | // It is an error for [[align]] to decrease alignment, so we can |
| 552 | // ignore that possibility; Sema should diagnose it. |
| 553 | if (isa<FieldDecl>(D)) { |
| 554 | UseAlignAttrOnly = D->hasAttr<PackedAttr>() || |
| 555 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
| 556 | } else { |
| 557 | UseAlignAttrOnly = true; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | if (UseAlignAttrOnly) { |
| 562 | // ignore type of value |
| 563 | } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 564 | QualType T = VD->getType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 565 | if (const ReferenceType* RT = T->getAs<ReferenceType>()) { |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 566 | if (RefAsPointee) |
| 567 | T = RT->getPointeeType(); |
| 568 | else |
| 569 | T = getPointerType(RT->getPointeeType()); |
| 570 | } |
| 571 | if (!T->isIncompleteType() && !T->isFunctionType()) { |
Rafael Espindola | e971b9a | 2010-06-04 23:15:27 +0000 | [diff] [blame] | 572 | unsigned MinWidth = Target.getLargeArrayMinWidth(); |
| 573 | unsigned ArrayAlign = Target.getLargeArrayAlign(); |
| 574 | if (isa<VariableArrayType>(T) && MinWidth != 0) |
| 575 | Align = std::max(Align, ArrayAlign); |
| 576 | if (ConstantArrayType *CT = dyn_cast<ConstantArrayType>(T)) { |
| 577 | unsigned Size = getTypeSize(CT); |
| 578 | if (MinWidth != 0 && MinWidth <= Size) |
| 579 | Align = std::max(Align, ArrayAlign); |
| 580 | } |
Anders Carlsson | 9b5038e | 2009-04-10 04:47:03 +0000 | [diff] [blame] | 581 | // Incomplete or function types default to 1. |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 582 | while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T)) |
| 583 | T = cast<ArrayType>(T)->getElementType(); |
| 584 | |
| 585 | Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); |
| 586 | } |
Charles Davis | 3fc5107 | 2010-02-23 04:52:00 +0000 | [diff] [blame] | 587 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) { |
| 588 | // In the case of a field in a packed struct, we want the minimum |
| 589 | // of the alignment of the field and the alignment of the struct. |
| 590 | Align = std::min(Align, |
| 591 | getPreferredTypeAlign(FD->getParent()->getTypeForDecl())); |
| 592 | } |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 593 | } |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 594 | |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 595 | return toCharUnitsFromBits(Align); |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 596 | } |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 597 | |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 598 | std::pair<CharUnits, CharUnits> |
| 599 | ASTContext::getTypeInfoInChars(const Type *T) { |
| 600 | std::pair<uint64_t, unsigned> Info = getTypeInfo(T); |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 601 | return std::make_pair(toCharUnitsFromBits(Info.first), |
| 602 | toCharUnitsFromBits(Info.second)); |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | std::pair<CharUnits, CharUnits> |
| 606 | ASTContext::getTypeInfoInChars(QualType T) { |
| 607 | return getTypeInfoInChars(T.getTypePtr()); |
| 608 | } |
| 609 | |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 610 | /// getTypeSize - Return the size of the specified type, in bits. This method |
| 611 | /// does not work on incomplete types. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 612 | /// |
| 613 | /// FIXME: Pointers into different addr spaces could have different sizes and |
| 614 | /// alignment requirements: getPointerInfo should take an AddrSpace, this |
| 615 | /// should take a QualType, &c. |
Chris Lattner | 4481b42 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 616 | std::pair<uint64_t, unsigned> |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 617 | ASTContext::getTypeInfo(const Type *T) const { |
Mike Stump | 5b9a3d5 | 2009-02-27 18:32:39 +0000 | [diff] [blame] | 618 | uint64_t Width=0; |
| 619 | unsigned Align=8; |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 620 | switch (T->getTypeClass()) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 621 | #define TYPE(Class, Base) |
| 622 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 623 | #define NON_CANONICAL_TYPE(Class, Base) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 624 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 625 | #include "clang/AST/TypeNodes.def" |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 626 | assert(false && "Should not see dependent types"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 627 | break; |
| 628 | |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 629 | case Type::FunctionNoProto: |
| 630 | case Type::FunctionProto: |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 631 | // GCC extension: alignof(function) = 32 bits |
| 632 | Width = 0; |
| 633 | Align = 32; |
| 634 | break; |
| 635 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 636 | case Type::IncompleteArray: |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 637 | case Type::VariableArray: |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 638 | Width = 0; |
| 639 | Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); |
| 640 | break; |
| 641 | |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 642 | case Type::ConstantArray: { |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 643 | const ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 644 | |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 645 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 646 | Width = EltInfo.first*CAT->getSize().getZExtValue(); |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 647 | Align = EltInfo.second; |
| 648 | break; |
Christopher Lamb | c5fafa2 | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 649 | } |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 650 | case Type::ExtVector: |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 651 | case Type::Vector: { |
Chris Lattner | 63d2b36 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 652 | const VectorType *VT = cast<VectorType>(T); |
| 653 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType()); |
| 654 | Width = EltInfo.first*VT->getNumElements(); |
Eli Friedman | 3df5efe | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 655 | Align = Width; |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 656 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 657 | // This happens for non-power-of-2 length vectors. |
Dan Gohman | ef78c8e | 2010-04-21 23:32:43 +0000 | [diff] [blame] | 658 | if (Align & (Align-1)) { |
Chris Lattner | 63d2b36 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 659 | Align = llvm::NextPowerOf2(Align); |
| 660 | Width = llvm::RoundUpToAlignment(Width, Align); |
| 661 | } |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 662 | break; |
| 663 | } |
Chris Lattner | 647fb22 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 664 | |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 665 | case Type::Builtin: |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 666 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 667 | default: assert(0 && "Unknown builtin type!"); |
Chris Lattner | 4481b42 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 668 | case BuiltinType::Void: |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 669 | // GCC extension: alignof(void) = 8 bits. |
| 670 | Width = 0; |
| 671 | Align = 8; |
| 672 | break; |
| 673 | |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 674 | case BuiltinType::Bool: |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 675 | Width = Target.getBoolWidth(); |
| 676 | Align = Target.getBoolAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 677 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 678 | case BuiltinType::Char_S: |
| 679 | case BuiltinType::Char_U: |
| 680 | case BuiltinType::UChar: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 681 | case BuiltinType::SChar: |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 682 | Width = Target.getCharWidth(); |
| 683 | Align = Target.getCharAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 684 | break; |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 685 | case BuiltinType::WChar_S: |
| 686 | case BuiltinType::WChar_U: |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 687 | Width = Target.getWCharWidth(); |
| 688 | Align = Target.getWCharAlign(); |
| 689 | break; |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 690 | case BuiltinType::Char16: |
| 691 | Width = Target.getChar16Width(); |
| 692 | Align = Target.getChar16Align(); |
| 693 | break; |
| 694 | case BuiltinType::Char32: |
| 695 | Width = Target.getChar32Width(); |
| 696 | Align = Target.getChar32Align(); |
| 697 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 698 | case BuiltinType::UShort: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 699 | case BuiltinType::Short: |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 700 | Width = Target.getShortWidth(); |
| 701 | Align = Target.getShortAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 702 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 703 | case BuiltinType::UInt: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 704 | case BuiltinType::Int: |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 705 | Width = Target.getIntWidth(); |
| 706 | Align = Target.getIntAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 707 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 708 | case BuiltinType::ULong: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 709 | case BuiltinType::Long: |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 710 | Width = Target.getLongWidth(); |
| 711 | Align = Target.getLongAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 712 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 713 | case BuiltinType::ULongLong: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 714 | case BuiltinType::LongLong: |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 715 | Width = Target.getLongLongWidth(); |
| 716 | Align = Target.getLongLongAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 717 | break; |
Chris Lattner | 0a415ec | 2009-04-30 02:55:13 +0000 | [diff] [blame] | 718 | case BuiltinType::Int128: |
| 719 | case BuiltinType::UInt128: |
| 720 | Width = 128; |
| 721 | Align = 128; // int128_t is 128-bit aligned on all targets. |
| 722 | break; |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 723 | case BuiltinType::Float: |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 724 | Width = Target.getFloatWidth(); |
| 725 | Align = Target.getFloatAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 726 | break; |
| 727 | case BuiltinType::Double: |
Chris Lattner | 4ba0cef | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 728 | Width = Target.getDoubleWidth(); |
| 729 | Align = Target.getDoubleAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 730 | break; |
| 731 | case BuiltinType::LongDouble: |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 732 | Width = Target.getLongDoubleWidth(); |
| 733 | Align = Target.getLongDoubleAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 734 | break; |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 735 | case BuiltinType::NullPtr: |
| 736 | Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) |
| 737 | Align = Target.getPointerAlign(0); // == sizeof(void*) |
Sebastian Redl | a81b0b7 | 2009-05-27 19:34:06 +0000 | [diff] [blame] | 738 | break; |
Fariborz Jahanian | 9c6a39e | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 739 | case BuiltinType::ObjCId: |
| 740 | case BuiltinType::ObjCClass: |
| 741 | case BuiltinType::ObjCSel: |
| 742 | Width = Target.getPointerWidth(0); |
| 743 | Align = Target.getPointerAlign(0); |
| 744 | break; |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 745 | } |
Chris Lattner | 48f84b8 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 746 | break; |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 747 | case Type::ObjCObjectPointer: |
Chris Lattner | 4ba0cef | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 748 | Width = Target.getPointerWidth(0); |
Chris Lattner | 2dca6ff | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 749 | Align = Target.getPointerAlign(0); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 750 | break; |
Steve Naroff | 921a45c | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 751 | case Type::BlockPointer: { |
| 752 | unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace(); |
| 753 | Width = Target.getPointerWidth(AS); |
| 754 | Align = Target.getPointerAlign(AS); |
| 755 | break; |
| 756 | } |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 757 | case Type::LValueReference: |
| 758 | case Type::RValueReference: { |
| 759 | // alignof and sizeof should never enter this code path here, so we go |
| 760 | // the pointer route. |
| 761 | unsigned AS = cast<ReferenceType>(T)->getPointeeType().getAddressSpace(); |
| 762 | Width = Target.getPointerWidth(AS); |
| 763 | Align = Target.getPointerAlign(AS); |
| 764 | break; |
| 765 | } |
Chris Lattner | 2dca6ff | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 766 | case Type::Pointer: { |
| 767 | unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace(); |
Chris Lattner | 4ba0cef | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 768 | Width = Target.getPointerWidth(AS); |
Chris Lattner | 2dca6ff | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 769 | Align = Target.getPointerAlign(AS); |
| 770 | break; |
| 771 | } |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 772 | case Type::MemberPointer: { |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 773 | const MemberPointerType *MPT = cast<MemberPointerType>(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 774 | std::pair<uint64_t, unsigned> PtrDiffInfo = |
Anders Carlsson | 32440a0 | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 775 | getTypeInfo(getPointerDiffType()); |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 776 | Width = PtrDiffInfo.first * ABI->getMemberPointerSize(MPT); |
Anders Carlsson | 32440a0 | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 777 | Align = PtrDiffInfo.second; |
| 778 | break; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 779 | } |
Chris Lattner | 647fb22 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 780 | case Type::Complex: { |
| 781 | // Complex types have the same alignment as their elements, but twice the |
| 782 | // size. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 783 | std::pair<uint64_t, unsigned> EltInfo = |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 784 | getTypeInfo(cast<ComplexType>(T)->getElementType()); |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 785 | Width = EltInfo.first*2; |
Chris Lattner | 647fb22 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 786 | Align = EltInfo.second; |
| 787 | break; |
| 788 | } |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 789 | case Type::ObjCObject: |
| 790 | return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr()); |
Devang Patel | dbb7263 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 791 | case Type::ObjCInterface: { |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 792 | const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | dbb7263 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 793 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
| 794 | Width = Layout.getSize(); |
| 795 | Align = Layout.getAlignment(); |
| 796 | break; |
| 797 | } |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 798 | case Type::Record: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 799 | case Type::Enum: { |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 800 | const TagType *TT = cast<TagType>(T); |
| 801 | |
| 802 | if (TT->getDecl()->isInvalidDecl()) { |
Chris Lattner | 572100b | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 803 | Width = 1; |
| 804 | Align = 1; |
| 805 | break; |
| 806 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 807 | |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 808 | if (const EnumType *ET = dyn_cast<EnumType>(TT)) |
Chris Lattner | 8b23c25 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 809 | return getTypeInfo(ET->getDecl()->getIntegerType()); |
| 810 | |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 811 | const RecordType *RT = cast<RecordType>(TT); |
Chris Lattner | 8b23c25 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 812 | const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); |
| 813 | Width = Layout.getSize(); |
| 814 | Align = Layout.getAlignment(); |
Chris Lattner | 49a953a | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 815 | break; |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 816 | } |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 817 | |
Chris Lattner | 63d2b36 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 818 | case Type::SubstTemplateTypeParm: |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 819 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> |
| 820 | getReplacementType().getTypePtr()); |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 821 | |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 822 | case Type::Paren: |
| 823 | return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr()); |
| 824 | |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 825 | case Type::Typedef: { |
| 826 | const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl(); |
Douglas Gregor | f5bae22 | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 827 | std::pair<uint64_t, unsigned> Info |
| 828 | = getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); |
| 829 | Align = std::max(Typedef->getMaxAlignment(), Info.second); |
| 830 | Width = Info.first; |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 831 | break; |
Chris Lattner | 8b23c25 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 832 | } |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 833 | |
| 834 | case Type::TypeOfExpr: |
| 835 | return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType() |
| 836 | .getTypePtr()); |
| 837 | |
| 838 | case Type::TypeOf: |
| 839 | return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr()); |
| 840 | |
Anders Carlsson | 81df7b8 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 841 | case Type::Decltype: |
| 842 | return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType() |
| 843 | .getTypePtr()); |
| 844 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 845 | case Type::Elaborated: |
| 846 | return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 847 | |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 848 | case Type::Attributed: |
| 849 | return getTypeInfo( |
| 850 | cast<AttributedType>(T)->getEquivalentType().getTypePtr()); |
| 851 | |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 852 | case Type::TemplateSpecialization: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 853 | assert(getCanonicalType(T) != T && |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 854 | "Cannot request the size of a dependent type"); |
| 855 | // FIXME: this is likely to be wrong once we support template |
| 856 | // aliases, since a template alias could refer to a typedef that |
| 857 | // has an __aligned__ attribute on it. |
| 858 | return getTypeInfo(getCanonicalType(T)); |
| 859 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 860 | |
Chris Lattner | 53cfe80 | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 861 | assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2"); |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 862 | return std::make_pair(Width, Align); |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 863 | } |
| 864 | |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 865 | /// toCharUnitsFromBits - Convert a size in bits to a size in characters. |
| 866 | CharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const { |
| 867 | return CharUnits::fromQuantity(BitSize / getCharWidth()); |
| 868 | } |
| 869 | |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 870 | /// getTypeSizeInChars - Return the size of the specified type, in characters. |
| 871 | /// This method does not work on incomplete types. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 872 | CharUnits ASTContext::getTypeSizeInChars(QualType T) const { |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 873 | return toCharUnitsFromBits(getTypeSize(T)); |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 874 | } |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 875 | CharUnits ASTContext::getTypeSizeInChars(const Type *T) const { |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 876 | return toCharUnitsFromBits(getTypeSize(T)); |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 877 | } |
| 878 | |
Ken Dyck | a6046ab | 2010-01-26 17:25:18 +0000 | [diff] [blame] | 879 | /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in |
Ken Dyck | 24d28d6 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 880 | /// characters. This method does not work on incomplete types. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 881 | CharUnits ASTContext::getTypeAlignInChars(QualType T) const { |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 882 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 24d28d6 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 883 | } |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 884 | CharUnits ASTContext::getTypeAlignInChars(const Type *T) const { |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 885 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 24d28d6 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Chris Lattner | a3402cd | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 888 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 889 | /// type for the current target in bits. This can be different than the ABI |
| 890 | /// alignment in cases where it is beneficial for performance to overalign |
| 891 | /// a data type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 892 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) const { |
Chris Lattner | a3402cd | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 893 | unsigned ABIAlign = getTypeAlign(T); |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 894 | |
| 895 | // Double and long long should be naturally aligned if possible. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 896 | if (const ComplexType* CT = T->getAs<ComplexType>()) |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 897 | T = CT->getElementType().getTypePtr(); |
| 898 | if (T->isSpecificBuiltinType(BuiltinType::Double) || |
| 899 | T->isSpecificBuiltinType(BuiltinType::LongLong)) |
| 900 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); |
| 901 | |
Chris Lattner | a3402cd | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 902 | return ABIAlign; |
| 903 | } |
| 904 | |
Fariborz Jahanian | 7c80959 | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 905 | /// ShallowCollectObjCIvars - |
| 906 | /// Collect all ivars, including those synthesized, in the current class. |
| 907 | /// |
| 908 | void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 909 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) const { |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 910 | // FIXME. This need be removed but there are two many places which |
| 911 | // assume const-ness of ObjCInterfaceDecl |
| 912 | ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI); |
| 913 | for (ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv; |
| 914 | Iv= Iv->getNextIvar()) |
| 915 | Ivars.push_back(Iv); |
Fariborz Jahanian | 7c80959 | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 916 | } |
| 917 | |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 918 | /// DeepCollectObjCIvars - |
| 919 | /// This routine first collects all declared, but not synthesized, ivars in |
| 920 | /// super class and then collects all ivars, including those synthesized for |
| 921 | /// current class. This routine is used for implementation of current class |
| 922 | /// when all ivars, declared and synthesized are known. |
Fariborz Jahanian | 0f44d81 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 923 | /// |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 924 | void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 925 | bool leafClass, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 926 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) const { |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 927 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
| 928 | DeepCollectObjCIvars(SuperClass, false, Ivars); |
| 929 | if (!leafClass) { |
| 930 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 931 | E = OI->ivar_end(); I != E; ++I) |
Fariborz Jahanian | aef6622 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 932 | Ivars.push_back(*I); |
| 933 | } |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 934 | else |
| 935 | ShallowCollectObjCIvars(OI, Ivars); |
Fariborz Jahanian | 0f44d81 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 936 | } |
| 937 | |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 938 | /// CollectInheritedProtocols - Collect all protocols in current class and |
| 939 | /// those inherited by it. |
| 940 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 941 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) { |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 942 | if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
Ted Kremenek | 0ef508d | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 943 | // We can use protocol_iterator here instead of |
| 944 | // all_referenced_protocol_iterator since we are walking all categories. |
| 945 | for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(), |
| 946 | PE = OI->all_referenced_protocol_end(); P != PE; ++P) { |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 947 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 948 | Protocols.insert(Proto); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 949 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
Fariborz Jahanian | 8e3b9db | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 950 | PE = Proto->protocol_end(); P != PE; ++P) { |
| 951 | Protocols.insert(*P); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 952 | CollectInheritedProtocols(*P, Protocols); |
| 953 | } |
Fariborz Jahanian | 8e3b9db | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 954 | } |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 955 | |
| 956 | // Categories of this Interface. |
| 957 | for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList(); |
| 958 | CDeclChain; CDeclChain = CDeclChain->getNextClassCategory()) |
| 959 | CollectInheritedProtocols(CDeclChain, Protocols); |
| 960 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) |
| 961 | while (SD) { |
| 962 | CollectInheritedProtocols(SD, Protocols); |
| 963 | SD = SD->getSuperClass(); |
| 964 | } |
Benjamin Kramer | 0a3fe04 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 965 | } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Ted Kremenek | 0ef508d | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 966 | for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(), |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 967 | PE = OC->protocol_end(); P != PE; ++P) { |
| 968 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 969 | Protocols.insert(Proto); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 970 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 971 | PE = Proto->protocol_end(); P != PE; ++P) |
| 972 | CollectInheritedProtocols(*P, Protocols); |
| 973 | } |
Benjamin Kramer | 0a3fe04 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 974 | } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 975 | for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(), |
| 976 | PE = OP->protocol_end(); P != PE; ++P) { |
| 977 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 978 | Protocols.insert(Proto); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 979 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 980 | PE = Proto->protocol_end(); P != PE; ++P) |
| 981 | CollectInheritedProtocols(*P, Protocols); |
| 982 | } |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 983 | } |
| 984 | } |
| 985 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 986 | unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const { |
Fariborz Jahanian | d2ae2d0 | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 987 | unsigned count = 0; |
| 988 | // Count ivars declared in class extension. |
Fariborz Jahanian | 3bf0ded | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 989 | for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl; |
| 990 | CDecl = CDecl->getNextClassExtension()) |
Benjamin Kramer | 0a3fe04 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 991 | count += CDecl->ivar_size(); |
| 992 | |
Fariborz Jahanian | d2ae2d0 | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 993 | // Count ivar defined in this class's implementation. This |
| 994 | // includes synthesized ivars. |
| 995 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) |
Benjamin Kramer | 0a3fe04 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 996 | count += ImplDecl->ivar_size(); |
| 997 | |
Fariborz Jahanian | 7c80959 | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 998 | return count; |
| 999 | } |
| 1000 | |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1001 | /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. |
| 1002 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
| 1003 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1004 | I = ObjCImpls.find(D); |
| 1005 | if (I != ObjCImpls.end()) |
| 1006 | return cast<ObjCImplementationDecl>(I->second); |
| 1007 | return 0; |
| 1008 | } |
| 1009 | /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. |
| 1010 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
| 1011 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1012 | I = ObjCImpls.find(D); |
| 1013 | if (I != ObjCImpls.end()) |
| 1014 | return cast<ObjCCategoryImplDecl>(I->second); |
| 1015 | return 0; |
| 1016 | } |
| 1017 | |
| 1018 | /// \brief Set the implementation of ObjCInterfaceDecl. |
| 1019 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
| 1020 | ObjCImplementationDecl *ImplD) { |
| 1021 | assert(IFaceD && ImplD && "Passed null params"); |
| 1022 | ObjCImpls[IFaceD] = ImplD; |
| 1023 | } |
| 1024 | /// \brief Set the implementation of ObjCCategoryDecl. |
| 1025 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
| 1026 | ObjCCategoryImplDecl *ImplD) { |
| 1027 | assert(CatD && ImplD && "Passed null params"); |
| 1028 | ObjCImpls[CatD] = ImplD; |
| 1029 | } |
| 1030 | |
Fariborz Jahanian | 7cfe767 | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1031 | /// \brief Get the copy initialization expression of VarDecl,or NULL if |
| 1032 | /// none exists. |
Fariborz Jahanian | 5019809 | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1033 | Expr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) { |
Fariborz Jahanian | 1321cbb | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1034 | assert(VD && "Passed null params"); |
| 1035 | assert(VD->hasAttr<BlocksAttr>() && |
| 1036 | "getBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 5019809 | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1037 | llvm::DenseMap<const VarDecl*, Expr*>::iterator |
Fariborz Jahanian | 1321cbb | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1038 | I = BlockVarCopyInits.find(VD); |
Fariborz Jahanian | 7cfe767 | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1039 | return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0; |
| 1040 | } |
| 1041 | |
| 1042 | /// \brief Set the copy inialization expression of a block var decl. |
| 1043 | void ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) { |
| 1044 | assert(VD && Init && "Passed null params"); |
Fariborz Jahanian | 1321cbb | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1045 | assert(VD->hasAttr<BlocksAttr>() && |
| 1046 | "setBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 7cfe767 | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1047 | BlockVarCopyInits[VD] = Init; |
| 1048 | } |
| 1049 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1050 | /// \brief Allocate an uninitialized TypeSourceInfo. |
Argyrios Kyrtzidis | 3f79ad7 | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1051 | /// |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1052 | /// The caller should initialize the memory held by TypeSourceInfo using |
Argyrios Kyrtzidis | 3f79ad7 | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1053 | /// the TypeLoc wrappers. |
| 1054 | /// |
| 1055 | /// \param T the type that will be the basis for type source info. This type |
| 1056 | /// should refer to how the declarator was written in source code, not to |
| 1057 | /// what type semantic analysis resolved the declarator to. |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1058 | TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1059 | unsigned DataSize) const { |
John McCall | 26fe7e0 | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1060 | if (!DataSize) |
| 1061 | DataSize = TypeLoc::getFullDataSizeForType(T); |
| 1062 | else |
| 1063 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1064 | "incorrect data size provided to CreateTypeSourceInfo!"); |
John McCall | 26fe7e0 | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1065 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1066 | TypeSourceInfo *TInfo = |
| 1067 | (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8); |
| 1068 | new (TInfo) TypeSourceInfo(T); |
| 1069 | return TInfo; |
Argyrios Kyrtzidis | 3f79ad7 | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1072 | TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, |
John McCall | 3665e00 | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 1073 | SourceLocation L) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1074 | TypeSourceInfo *DI = CreateTypeSourceInfo(T); |
John McCall | 3665e00 | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 1075 | DI->getTypeLoc().initialize(L); |
| 1076 | return DI; |
| 1077 | } |
| 1078 | |
Daniel Dunbar | 02f7f5f | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1079 | const ASTRecordLayout & |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1080 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const { |
Daniel Dunbar | 02f7f5f | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1081 | return getObjCLayout(D, 0); |
| 1082 | } |
| 1083 | |
| 1084 | const ASTRecordLayout & |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1085 | ASTContext::getASTObjCImplementationLayout( |
| 1086 | const ObjCImplementationDecl *D) const { |
Daniel Dunbar | 02f7f5f | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1087 | return getObjCLayout(D->getClassInterface(), D); |
| 1088 | } |
| 1089 | |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1090 | //===----------------------------------------------------------------------===// |
| 1091 | // Type creation/memoization methods |
| 1092 | //===----------------------------------------------------------------------===// |
| 1093 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1094 | QualType |
| 1095 | ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) const { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1096 | unsigned Fast = Quals.getFastQualifiers(); |
| 1097 | Quals.removeFastQualifiers(); |
| 1098 | |
| 1099 | // Check if we've already instantiated this type. |
| 1100 | llvm::FoldingSetNodeID ID; |
| 1101 | ExtQuals::Profile(ID, TypeNode, Quals); |
| 1102 | void *InsertPos = 0; |
| 1103 | if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) { |
| 1104 | assert(EQ->getQualifiers() == Quals); |
| 1105 | QualType T = QualType(EQ, Fast); |
| 1106 | return T; |
| 1107 | } |
| 1108 | |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 1109 | ExtQuals *New = new (*this, TypeAlignment) ExtQuals(TypeNode, Quals); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1110 | ExtQualNodes.InsertNode(New, InsertPos); |
| 1111 | QualType T = QualType(New, Fast); |
| 1112 | return T; |
| 1113 | } |
| 1114 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1115 | QualType |
| 1116 | ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) const { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1117 | QualType CanT = getCanonicalType(T); |
| 1118 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | 445fcab | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1119 | return T; |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1120 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1121 | // If we are composing extended qualifiers together, merge together |
| 1122 | // into one ExtQuals node. |
| 1123 | QualifierCollector Quals; |
| 1124 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1125 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1126 | // If this type already has an address space specified, it cannot get |
| 1127 | // another one. |
| 1128 | assert(!Quals.hasAddressSpace() && |
| 1129 | "Type cannot be in multiple addr spaces!"); |
| 1130 | Quals.addAddressSpace(AddressSpace); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1131 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1132 | return getExtQualType(TypeNode, Quals); |
Christopher Lamb | 025b5fb | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1135 | QualType ASTContext::getObjCGCQualType(QualType T, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1136 | Qualifiers::GC GCAttr) const { |
Fariborz Jahanian | e27e934 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1137 | QualType CanT = getCanonicalType(T); |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1138 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | e27e934 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1139 | return T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1140 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1141 | if (const PointerType *ptr = T->getAs<PointerType>()) { |
| 1142 | QualType Pointee = ptr->getPointeeType(); |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 1143 | if (Pointee->isAnyPointerType()) { |
Fariborz Jahanian | b68215c | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1144 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 1145 | return getPointerType(ResultType); |
| 1146 | } |
| 1147 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1148 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1149 | // If we are composing extended qualifiers together, merge together |
| 1150 | // into one ExtQuals node. |
| 1151 | QualifierCollector Quals; |
| 1152 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1153 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1154 | // If this type already has an ObjCGC specified, it cannot get |
| 1155 | // another one. |
| 1156 | assert(!Quals.hasObjCGCAttr() && |
| 1157 | "Type cannot have multiple ObjCGCs!"); |
| 1158 | Quals.addObjCGCAttr(GCAttr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1159 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1160 | return getExtQualType(TypeNode, Quals); |
Fariborz Jahanian | e27e934 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1161 | } |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1162 | |
John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 1163 | const FunctionType *ASTContext::adjustFunctionType(const FunctionType *T, |
| 1164 | FunctionType::ExtInfo Info) { |
| 1165 | if (T->getExtInfo() == Info) |
| 1166 | return T; |
| 1167 | |
| 1168 | QualType Result; |
| 1169 | if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) { |
| 1170 | Result = getFunctionNoProtoType(FNPT->getResultType(), Info); |
| 1171 | } else { |
| 1172 | const FunctionProtoType *FPT = cast<FunctionProtoType>(T); |
| 1173 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 1174 | EPI.ExtInfo = Info; |
| 1175 | Result = getFunctionType(FPT->getResultType(), FPT->arg_type_begin(), |
| 1176 | FPT->getNumArgs(), EPI); |
| 1177 | } |
| 1178 | |
| 1179 | return cast<FunctionType>(Result.getTypePtr()); |
| 1180 | } |
| 1181 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1182 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 1183 | /// number with the specified element type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1184 | QualType ASTContext::getComplexType(QualType T) const { |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1185 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1186 | // structure. |
| 1187 | llvm::FoldingSetNodeID ID; |
| 1188 | ComplexType::Profile(ID, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1189 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1190 | void *InsertPos = 0; |
| 1191 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1192 | return QualType(CT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1193 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1194 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1195 | // so fill in the canonical type field. |
| 1196 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1197 | if (!T.isCanonical()) { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1198 | Canonical = getComplexType(getCanonicalType(T)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1199 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1200 | // Get the new insert position for the node we care about. |
| 1201 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1202 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1203 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1204 | ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical); |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1205 | Types.push_back(New); |
| 1206 | ComplexTypes.InsertNode(New, InsertPos); |
| 1207 | return QualType(New, 0); |
| 1208 | } |
| 1209 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1210 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 1211 | /// the specified type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1212 | QualType ASTContext::getPointerType(QualType T) const { |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 1213 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1214 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 1215 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 1216 | PointerType::Profile(ID, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1217 | |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 1218 | void *InsertPos = 0; |
| 1219 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1220 | return QualType(PT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1221 | |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 1222 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1223 | // so fill in the canonical type field. |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1224 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1225 | if (!T.isCanonical()) { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1226 | Canonical = getPointerType(getCanonicalType(T)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1227 | |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 1228 | // Get the new insert position for the node we care about. |
| 1229 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1230 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 1231 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1232 | PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical); |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 1233 | Types.push_back(New); |
| 1234 | PointerTypes.InsertNode(New, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1235 | return QualType(New, 0); |
Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1238 | /// getBlockPointerType - Return the uniqued reference to the type for |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1239 | /// a pointer to the specified block. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1240 | QualType ASTContext::getBlockPointerType(QualType T) const { |
Steve Naroff | 0ac01283 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1241 | assert(T->isFunctionType() && "block of function types only"); |
| 1242 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1243 | // structure. |
| 1244 | llvm::FoldingSetNodeID ID; |
| 1245 | BlockPointerType::Profile(ID, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1246 | |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1247 | void *InsertPos = 0; |
| 1248 | if (BlockPointerType *PT = |
| 1249 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1250 | return QualType(PT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1251 | |
| 1252 | // If the block pointee type isn't canonical, this won't be a canonical |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1253 | // type either so fill in the canonical type field. |
| 1254 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1255 | if (!T.isCanonical()) { |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1256 | Canonical = getBlockPointerType(getCanonicalType(T)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1257 | |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1258 | // Get the new insert position for the node we care about. |
| 1259 | BlockPointerType *NewIP = |
| 1260 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1261 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1262 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1263 | BlockPointerType *New |
| 1264 | = new (*this, TypeAlignment) BlockPointerType(T, Canonical); |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1265 | Types.push_back(New); |
| 1266 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 1267 | return QualType(New, 0); |
| 1268 | } |
| 1269 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1270 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 1271 | /// lvalue reference to the specified type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1272 | QualType |
| 1273 | ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const { |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1274 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1275 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 1276 | llvm::FoldingSetNodeID ID; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1277 | ReferenceType::Profile(ID, T, SpelledAsLValue); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1278 | |
| 1279 | void *InsertPos = 0; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1280 | if (LValueReferenceType *RT = |
| 1281 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1282 | return QualType(RT, 0); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1283 | |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1284 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1285 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1286 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1287 | // either, so fill in the canonical type field. |
| 1288 | QualType Canonical; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1289 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
| 1290 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1291 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1292 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1293 | // Get the new insert position for the node we care about. |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1294 | LValueReferenceType *NewIP = |
| 1295 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1296 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1299 | LValueReferenceType *New |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1300 | = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, |
| 1301 | SpelledAsLValue); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1302 | Types.push_back(New); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1303 | LValueReferenceTypes.InsertNode(New, InsertPos); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1304 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1305 | return QualType(New, 0); |
| 1306 | } |
| 1307 | |
| 1308 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 1309 | /// rvalue reference to the specified type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1310 | QualType ASTContext::getRValueReferenceType(QualType T) const { |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1311 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1312 | // structure. |
| 1313 | llvm::FoldingSetNodeID ID; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1314 | ReferenceType::Profile(ID, T, false); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1315 | |
| 1316 | void *InsertPos = 0; |
| 1317 | if (RValueReferenceType *RT = |
| 1318 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1319 | return QualType(RT, 0); |
| 1320 | |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1321 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1322 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1323 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1324 | // either, so fill in the canonical type field. |
| 1325 | QualType Canonical; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1326 | if (InnerRef || !T.isCanonical()) { |
| 1327 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1328 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1329 | |
| 1330 | // Get the new insert position for the node we care about. |
| 1331 | RValueReferenceType *NewIP = |
| 1332 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1333 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1334 | } |
| 1335 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1336 | RValueReferenceType *New |
| 1337 | = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1338 | Types.push_back(New); |
| 1339 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1340 | return QualType(New, 0); |
| 1341 | } |
| 1342 | |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1343 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 1344 | /// member pointer to the specified type, in the specified class. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1345 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const { |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1346 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1347 | // structure. |
| 1348 | llvm::FoldingSetNodeID ID; |
| 1349 | MemberPointerType::Profile(ID, T, Cls); |
| 1350 | |
| 1351 | void *InsertPos = 0; |
| 1352 | if (MemberPointerType *PT = |
| 1353 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1354 | return QualType(PT, 0); |
| 1355 | |
| 1356 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 1357 | // type either, so fill in the canonical type field. |
| 1358 | QualType Canonical; |
Douglas Gregor | 615ac67 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1359 | if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) { |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1360 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 1361 | |
| 1362 | // Get the new insert position for the node we care about. |
| 1363 | MemberPointerType *NewIP = |
| 1364 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1365 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1366 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1367 | MemberPointerType *New |
| 1368 | = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1369 | Types.push_back(New); |
| 1370 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 1371 | return QualType(New, 0); |
| 1372 | } |
| 1373 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1374 | /// getConstantArrayType - Return the unique reference to the type for an |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1375 | /// array of the specified element type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1376 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | e2df3f9 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1377 | const llvm::APInt &ArySizeIn, |
Steve Naroff | 90dfdd5 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1378 | ArrayType::ArraySizeModifier ASM, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1379 | unsigned EltTypeQuals) const { |
Sebastian Redl | 2dfdb82 | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1380 | assert((EltTy->isDependentType() || |
| 1381 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && |
Eli Friedman | be7e42b | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1382 | "Constant array of VLAs is illegal!"); |
| 1383 | |
Chris Lattner | e2df3f9 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1384 | // Convert the array size into a canonical width matching the pointer size for |
| 1385 | // the target. |
| 1386 | llvm::APInt ArySize(ArySizeIn); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1387 | ArySize = |
| 1388 | ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1389 | |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 1390 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 780b46f | 2009-02-19 17:31:02 +0000 | [diff] [blame] | 1391 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1392 | |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 1393 | void *InsertPos = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1394 | if (ConstantArrayType *ATP = |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1395 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1396 | return QualType(ATP, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1397 | |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 1398 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1399 | // so fill in the canonical type field. |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1400 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1401 | if (!EltTy.isCanonical()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1402 | Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize, |
Steve Naroff | 90dfdd5 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1403 | ASM, EltTypeQuals); |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 1404 | // Get the new insert position for the node we care about. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1405 | ConstantArrayType *NewIP = |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1406 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1407 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 1408 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1409 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1410 | ConstantArrayType *New = new(*this,TypeAlignment) |
| 1411 | ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals); |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1412 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 1413 | Types.push_back(New); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1414 | return QualType(New, 0); |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 1415 | } |
| 1416 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame^] | 1417 | /// getVariableArrayDecayedType - Turns the given type, which may be |
| 1418 | /// variably-modified, into the corresponding type with all the known |
| 1419 | /// sizes replaced with [*]. |
| 1420 | QualType ASTContext::getVariableArrayDecayedType(QualType type) const { |
| 1421 | // Vastly most common case. |
| 1422 | if (!type->isVariablyModifiedType()) return type; |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1423 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame^] | 1424 | QualType result; |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1425 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame^] | 1426 | SplitQualType split = type.getSplitDesugaredType(); |
| 1427 | const Type *ty = split.first; |
| 1428 | switch (ty->getTypeClass()) { |
| 1429 | #define TYPE(Class, Base) |
| 1430 | #define ABSTRACT_TYPE(Class, Base) |
| 1431 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 1432 | #include "clang/AST/TypeNodes.def" |
| 1433 | llvm_unreachable("didn't desugar past all non-canonical types?"); |
| 1434 | |
| 1435 | // These types should never be variably-modified. |
| 1436 | case Type::Builtin: |
| 1437 | case Type::Complex: |
| 1438 | case Type::Vector: |
| 1439 | case Type::ExtVector: |
| 1440 | case Type::DependentSizedExtVector: |
| 1441 | case Type::ObjCObject: |
| 1442 | case Type::ObjCInterface: |
| 1443 | case Type::ObjCObjectPointer: |
| 1444 | case Type::Record: |
| 1445 | case Type::Enum: |
| 1446 | case Type::UnresolvedUsing: |
| 1447 | case Type::TypeOfExpr: |
| 1448 | case Type::TypeOf: |
| 1449 | case Type::Decltype: |
| 1450 | case Type::DependentName: |
| 1451 | case Type::InjectedClassName: |
| 1452 | case Type::TemplateSpecialization: |
| 1453 | case Type::DependentTemplateSpecialization: |
| 1454 | case Type::TemplateTypeParm: |
| 1455 | case Type::SubstTemplateTypeParmPack: |
| 1456 | case Type::PackExpansion: |
| 1457 | llvm_unreachable("type should never be variably-modified"); |
| 1458 | |
| 1459 | // These types can be variably-modified but should never need to |
| 1460 | // further decay. |
| 1461 | case Type::FunctionNoProto: |
| 1462 | case Type::FunctionProto: |
| 1463 | case Type::BlockPointer: |
| 1464 | case Type::MemberPointer: |
| 1465 | return type; |
| 1466 | |
| 1467 | // These types can be variably-modified. All these modifications |
| 1468 | // preserve structure except as noted by comments. |
| 1469 | // TODO: if we ever care about optimizing VLAs, there are no-op |
| 1470 | // optimizations available here. |
| 1471 | case Type::Pointer: |
| 1472 | result = getPointerType(getVariableArrayDecayedType( |
| 1473 | cast<PointerType>(ty)->getPointeeType())); |
| 1474 | break; |
| 1475 | |
| 1476 | case Type::LValueReference: { |
| 1477 | const LValueReferenceType *lv = cast<LValueReferenceType>(ty); |
| 1478 | result = getLValueReferenceType( |
| 1479 | getVariableArrayDecayedType(lv->getPointeeType()), |
| 1480 | lv->isSpelledAsLValue()); |
| 1481 | break; |
| 1482 | } |
| 1483 | |
| 1484 | case Type::RValueReference: { |
| 1485 | const RValueReferenceType *lv = cast<RValueReferenceType>(ty); |
| 1486 | result = getRValueReferenceType( |
| 1487 | getVariableArrayDecayedType(lv->getPointeeType())); |
| 1488 | break; |
| 1489 | } |
| 1490 | |
| 1491 | case Type::ConstantArray: { |
| 1492 | const ConstantArrayType *cat = cast<ConstantArrayType>(ty); |
| 1493 | result = getConstantArrayType( |
| 1494 | getVariableArrayDecayedType(cat->getElementType()), |
| 1495 | cat->getSize(), |
| 1496 | cat->getSizeModifier(), |
| 1497 | cat->getIndexTypeCVRQualifiers()); |
| 1498 | break; |
| 1499 | } |
| 1500 | |
| 1501 | case Type::DependentSizedArray: { |
| 1502 | const DependentSizedArrayType *dat = cast<DependentSizedArrayType>(ty); |
| 1503 | result = getDependentSizedArrayType( |
| 1504 | getVariableArrayDecayedType(dat->getElementType()), |
| 1505 | dat->getSizeExpr(), |
| 1506 | dat->getSizeModifier(), |
| 1507 | dat->getIndexTypeCVRQualifiers(), |
| 1508 | dat->getBracketsRange()); |
| 1509 | break; |
| 1510 | } |
| 1511 | |
| 1512 | // Turn incomplete types into [*] types. |
| 1513 | case Type::IncompleteArray: { |
| 1514 | const IncompleteArrayType *iat = cast<IncompleteArrayType>(ty); |
| 1515 | result = getVariableArrayType( |
| 1516 | getVariableArrayDecayedType(iat->getElementType()), |
| 1517 | /*size*/ 0, |
| 1518 | ArrayType::Normal, |
| 1519 | iat->getIndexTypeCVRQualifiers(), |
| 1520 | SourceRange()); |
| 1521 | break; |
| 1522 | } |
| 1523 | |
| 1524 | // Turn VLA types into [*] types. |
| 1525 | case Type::VariableArray: { |
| 1526 | const VariableArrayType *vat = cast<VariableArrayType>(ty); |
| 1527 | result = getVariableArrayType( |
| 1528 | getVariableArrayDecayedType(vat->getElementType()), |
| 1529 | /*size*/ 0, |
| 1530 | ArrayType::Star, |
| 1531 | vat->getIndexTypeCVRQualifiers(), |
| 1532 | vat->getBracketsRange()); |
| 1533 | break; |
| 1534 | } |
| 1535 | } |
| 1536 | |
| 1537 | // Apply the top-level qualifiers from the original. |
| 1538 | return getQualifiedType(result, split.second); |
| 1539 | } |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1540 | |
Steve Naroff | cadebd0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 1541 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 1542 | /// variable array of the specified element type. |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1543 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 1544 | Expr *NumElts, |
Steve Naroff | 90dfdd5 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1545 | ArrayType::ArraySizeModifier ASM, |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1546 | unsigned EltTypeQuals, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1547 | SourceRange Brackets) const { |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1548 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 1549 | // that have an expression provided for their size. |
Douglas Gregor | 5e8c8c0 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 1550 | QualType CanonType; |
| 1551 | |
| 1552 | if (!EltTy.isCanonical()) { |
Douglas Gregor | 5e8c8c0 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 1553 | CanonType = getVariableArrayType(getCanonicalType(EltTy), NumElts, ASM, |
| 1554 | EltTypeQuals, Brackets); |
| 1555 | } |
| 1556 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1557 | VariableArrayType *New = new(*this, TypeAlignment) |
Douglas Gregor | 5e8c8c0 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 1558 | VariableArrayType(EltTy, CanonType, NumElts, ASM, EltTypeQuals, Brackets); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1559 | |
| 1560 | VariableArrayTypes.push_back(New); |
| 1561 | Types.push_back(New); |
| 1562 | return QualType(New, 0); |
| 1563 | } |
| 1564 | |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1565 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 1566 | /// the type for a dependently-sized array of the specified element |
Douglas Gregor | f3f9552 | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1567 | /// type. |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1568 | QualType ASTContext::getDependentSizedArrayType(QualType EltTy, |
| 1569 | Expr *NumElts, |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1570 | ArrayType::ArraySizeModifier ASM, |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1571 | unsigned EltTypeQuals, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1572 | SourceRange Brackets) const { |
Douglas Gregor | ad2956c | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1573 | assert((!NumElts || NumElts->isTypeDependent() || |
| 1574 | NumElts->isValueDependent()) && |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1575 | "Size must be type- or value-dependent!"); |
| 1576 | |
Douglas Gregor | f3f9552 | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1577 | void *InsertPos = 0; |
Douglas Gregor | ad2956c | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1578 | DependentSizedArrayType *Canon = 0; |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1579 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | ad2956c | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1580 | |
Douglas Gregor | f86c939 | 2010-10-26 00:51:02 +0000 | [diff] [blame] | 1581 | QualType CanonicalEltTy = getCanonicalType(EltTy); |
Douglas Gregor | ad2956c | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1582 | if (NumElts) { |
| 1583 | // Dependently-sized array types that do not have a specified |
| 1584 | // number of elements will have their sizes deduced from an |
| 1585 | // initializer. |
Douglas Gregor | f86c939 | 2010-10-26 00:51:02 +0000 | [diff] [blame] | 1586 | DependentSizedArrayType::Profile(ID, *this, CanonicalEltTy, ASM, |
Douglas Gregor | ad2956c | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1587 | EltTypeQuals, NumElts); |
| 1588 | |
| 1589 | Canon = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1590 | } |
| 1591 | |
Douglas Gregor | f3f9552 | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1592 | DependentSizedArrayType *New; |
| 1593 | if (Canon) { |
| 1594 | // We already have a canonical version of this array type; use it as |
| 1595 | // the canonical type for a newly-built type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1596 | New = new (*this, TypeAlignment) |
| 1597 | DependentSizedArrayType(*this, EltTy, QualType(Canon, 0), |
| 1598 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | f86c939 | 2010-10-26 00:51:02 +0000 | [diff] [blame] | 1599 | } else if (CanonicalEltTy == EltTy) { |
| 1600 | // This is a canonical type. Record it. |
| 1601 | New = new (*this, TypeAlignment) |
| 1602 | DependentSizedArrayType(*this, EltTy, QualType(), |
| 1603 | NumElts, ASM, EltTypeQuals, Brackets); |
| 1604 | |
| 1605 | if (NumElts) { |
| 1606 | #ifndef NDEBUG |
| 1607 | DependentSizedArrayType *CanonCheck |
| 1608 | = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1609 | assert(!CanonCheck && "Dependent-sized canonical array type broken"); |
| 1610 | (void)CanonCheck; |
| 1611 | #endif |
| 1612 | DependentSizedArrayTypes.InsertNode(New, InsertPos); |
Douglas Gregor | f3f9552 | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1613 | } |
Douglas Gregor | f86c939 | 2010-10-26 00:51:02 +0000 | [diff] [blame] | 1614 | } else { |
| 1615 | QualType Canon = getDependentSizedArrayType(CanonicalEltTy, NumElts, |
| 1616 | ASM, EltTypeQuals, |
| 1617 | SourceRange()); |
| 1618 | New = new (*this, TypeAlignment) |
| 1619 | DependentSizedArrayType(*this, EltTy, Canon, |
| 1620 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | f3f9552 | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1621 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1622 | |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1623 | Types.push_back(New); |
| 1624 | return QualType(New, 0); |
| 1625 | } |
| 1626 | |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1627 | QualType ASTContext::getIncompleteArrayType(QualType EltTy, |
| 1628 | ArrayType::ArraySizeModifier ASM, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1629 | unsigned EltTypeQuals) const { |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1630 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 780b46f | 2009-02-19 17:31:02 +0000 | [diff] [blame] | 1631 | IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1632 | |
| 1633 | void *InsertPos = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1634 | if (IncompleteArrayType *ATP = |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1635 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1636 | return QualType(ATP, 0); |
| 1637 | |
| 1638 | // If the element type isn't canonical, this won't be a canonical type |
| 1639 | // either, so fill in the canonical type field. |
| 1640 | QualType Canonical; |
| 1641 | |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1642 | if (!EltTy.isCanonical()) { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1643 | Canonical = getIncompleteArrayType(getCanonicalType(EltTy), |
Ted Kremenek | 843ebedd | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1644 | ASM, EltTypeQuals); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1645 | |
| 1646 | // Get the new insert position for the node we care about. |
| 1647 | IncompleteArrayType *NewIP = |
| 1648 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1649 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Ted Kremenek | 843ebedd | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1650 | } |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1651 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1652 | IncompleteArrayType *New = new (*this, TypeAlignment) |
| 1653 | IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1654 | |
| 1655 | IncompleteArrayTypes.InsertNode(New, InsertPos); |
| 1656 | Types.push_back(New); |
| 1657 | return QualType(New, 0); |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1660 | /// getVectorType - Return the unique reference to a vector type of |
| 1661 | /// the specified element type and size. VectorType must be a built-in type. |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1662 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1663 | VectorType::VectorKind VecKind) const { |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 1664 | BuiltinType *BaseType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1665 | |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 1666 | BaseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); |
| 1667 | assert(BaseType != 0 && "getVectorType(): Expecting a built-in type"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1668 | |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 1669 | // Check if we've already instantiated a vector of this type. |
| 1670 | llvm::FoldingSetNodeID ID; |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1671 | VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind); |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1672 | |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 1673 | void *InsertPos = 0; |
| 1674 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1675 | return QualType(VTP, 0); |
| 1676 | |
| 1677 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1678 | // so fill in the canonical type field. |
| 1679 | QualType Canonical; |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1680 | if (!vecType.isCanonical()) { |
Bob Wilson | 7795480 | 2010-11-16 00:32:20 +0000 | [diff] [blame] | 1681 | Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1682 | |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 1683 | // Get the new insert position for the node we care about. |
| 1684 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1685 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 1686 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1687 | VectorType *New = new (*this, TypeAlignment) |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1688 | VectorType(vecType, NumElts, Canonical, VecKind); |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 1689 | VectorTypes.InsertNode(New, InsertPos); |
| 1690 | Types.push_back(New); |
| 1691 | return QualType(New, 0); |
| 1692 | } |
| 1693 | |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1694 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1695 | /// the specified element type and size. VectorType must be a built-in type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1696 | QualType |
| 1697 | ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) const { |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1698 | BuiltinType *baseType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1699 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1700 | baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1701 | assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1702 | |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1703 | // Check if we've already instantiated a vector of this type. |
| 1704 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1705 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1706 | VectorType::GenericVector); |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1707 | void *InsertPos = 0; |
| 1708 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1709 | return QualType(VTP, 0); |
| 1710 | |
| 1711 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1712 | // so fill in the canonical type field. |
| 1713 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1714 | if (!vecType.isCanonical()) { |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1715 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1716 | |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1717 | // Get the new insert position for the node we care about. |
| 1718 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1719 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1720 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1721 | ExtVectorType *New = new (*this, TypeAlignment) |
| 1722 | ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1723 | VectorTypes.InsertNode(New, InsertPos); |
| 1724 | Types.push_back(New); |
| 1725 | return QualType(New, 0); |
| 1726 | } |
| 1727 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1728 | QualType |
| 1729 | ASTContext::getDependentSizedExtVectorType(QualType vecType, |
| 1730 | Expr *SizeExpr, |
| 1731 | SourceLocation AttrLoc) const { |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1732 | llvm::FoldingSetNodeID ID; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1733 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1734 | SizeExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1735 | |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1736 | void *InsertPos = 0; |
| 1737 | DependentSizedExtVectorType *Canon |
| 1738 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1739 | DependentSizedExtVectorType *New; |
| 1740 | if (Canon) { |
| 1741 | // We already have a canonical version of this array type; use it as |
| 1742 | // the canonical type for a newly-built type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1743 | New = new (*this, TypeAlignment) |
| 1744 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), |
| 1745 | SizeExpr, AttrLoc); |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1746 | } else { |
| 1747 | QualType CanonVecTy = getCanonicalType(vecType); |
| 1748 | if (CanonVecTy == vecType) { |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1749 | New = new (*this, TypeAlignment) |
| 1750 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, |
| 1751 | AttrLoc); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1752 | |
| 1753 | DependentSizedExtVectorType *CanonCheck |
| 1754 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1755 | assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken"); |
| 1756 | (void)CanonCheck; |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1757 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); |
| 1758 | } else { |
| 1759 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 1760 | SourceLocation()); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1761 | New = new (*this, TypeAlignment) |
| 1762 | DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1763 | } |
| 1764 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1765 | |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1766 | Types.push_back(New); |
| 1767 | return QualType(New, 0); |
| 1768 | } |
| 1769 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1770 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 1771 | /// |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1772 | QualType |
| 1773 | ASTContext::getFunctionNoProtoType(QualType ResultTy, |
| 1774 | const FunctionType::ExtInfo &Info) const { |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1775 | const CallingConv CallConv = Info.getCC(); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 1776 | // Unique functions, to guarantee there is only one function of a particular |
| 1777 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 1778 | llvm::FoldingSetNodeID ID; |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1779 | FunctionNoProtoType::Profile(ID, ResultTy, Info); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1780 | |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 1781 | void *InsertPos = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1782 | if (FunctionNoProtoType *FT = |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1783 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1784 | return QualType(FT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1785 | |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1786 | QualType Canonical; |
Douglas Gregor | 8c94086 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1787 | if (!ResultTy.isCanonical() || |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1788 | getCanonicalCallConv(CallConv) != CallConv) { |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1789 | Canonical = |
| 1790 | getFunctionNoProtoType(getCanonicalType(ResultTy), |
| 1791 | Info.withCallingConv(getCanonicalCallConv(CallConv))); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1792 | |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 1793 | // Get the new insert position for the node we care about. |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1794 | FunctionNoProtoType *NewIP = |
| 1795 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1796 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 1797 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1798 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1799 | FunctionNoProtoType *New = new (*this, TypeAlignment) |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1800 | FunctionNoProtoType(ResultTy, Canonical, Info); |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 1801 | Types.push_back(New); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1802 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1803 | return QualType(New, 0); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 1804 | } |
| 1805 | |
| 1806 | /// getFunctionType - Return a normal function type with a typed argument |
| 1807 | /// list. isVariadic indicates whether the argument list includes '...'. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1808 | QualType |
| 1809 | ASTContext::getFunctionType(QualType ResultTy, |
| 1810 | const QualType *ArgArray, unsigned NumArgs, |
| 1811 | const FunctionProtoType::ExtProtoInfo &EPI) const { |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 1812 | // Unique functions, to guarantee there is only one function of a particular |
| 1813 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 1814 | llvm::FoldingSetNodeID ID; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1815 | FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, EPI); |
Chris Lattner | fd4de79 | 2007-01-27 01:15:32 +0000 | [diff] [blame] | 1816 | |
| 1817 | void *InsertPos = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1818 | if (FunctionProtoType *FTP = |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1819 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1820 | return QualType(FTP, 0); |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1821 | |
| 1822 | // Determine whether the type being created is already canonical or not. |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1823 | bool isCanonical = !EPI.HasExceptionSpec && ResultTy.isCanonical(); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 1824 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1825 | if (!ArgArray[i].isCanonicalAsParam()) |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 1826 | isCanonical = false; |
| 1827 | |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1828 | const CallingConv CallConv = EPI.ExtInfo.getCC(); |
| 1829 | |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 1830 | // If this type isn't canonical, get the canonical version of it. |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1831 | // The exception spec is not part of the canonical type. |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1832 | QualType Canonical; |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1833 | if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) { |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 1834 | llvm::SmallVector<QualType, 16> CanonicalArgs; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 1835 | CanonicalArgs.reserve(NumArgs); |
| 1836 | for (unsigned i = 0; i != NumArgs; ++i) |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1837 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1838 | |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1839 | FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI; |
| 1840 | if (CanonicalEPI.HasExceptionSpec) { |
| 1841 | CanonicalEPI.HasExceptionSpec = false; |
| 1842 | CanonicalEPI.HasAnyExceptionSpec = false; |
| 1843 | CanonicalEPI.NumExceptions = 0; |
| 1844 | } |
| 1845 | CanonicalEPI.ExtInfo |
| 1846 | = CanonicalEPI.ExtInfo.withCallingConv(getCanonicalCallConv(CallConv)); |
| 1847 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1848 | Canonical = getFunctionType(getCanonicalType(ResultTy), |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1849 | CanonicalArgs.data(), NumArgs, |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1850 | CanonicalEPI); |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1851 | |
Chris Lattner | fd4de79 | 2007-01-27 01:15:32 +0000 | [diff] [blame] | 1852 | // Get the new insert position for the node we care about. |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1853 | FunctionProtoType *NewIP = |
| 1854 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1855 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 1856 | } |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1857 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1858 | // FunctionProtoType objects are allocated with extra bytes after them |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1859 | // for two variable size arrays (for parameter and exception types) at the |
| 1860 | // end of them. |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1861 | size_t Size = sizeof(FunctionProtoType) + |
| 1862 | NumArgs * sizeof(QualType) + |
| 1863 | EPI.NumExceptions * sizeof(QualType); |
| 1864 | FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment); |
| 1865 | new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, Canonical, EPI); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 1866 | Types.push_back(FTP); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1867 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1868 | return QualType(FTP, 0); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 1869 | } |
Chris Lattner | ef51c20 | 2006-11-10 07:17:23 +0000 | [diff] [blame] | 1870 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1871 | #ifndef NDEBUG |
| 1872 | static bool NeedsInjectedClassNameType(const RecordDecl *D) { |
| 1873 | if (!isa<CXXRecordDecl>(D)) return false; |
| 1874 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 1875 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) |
| 1876 | return true; |
| 1877 | if (RD->getDescribedClassTemplate() && |
| 1878 | !isa<ClassTemplateSpecializationDecl>(RD)) |
| 1879 | return true; |
| 1880 | return false; |
| 1881 | } |
| 1882 | #endif |
| 1883 | |
| 1884 | /// getInjectedClassNameType - Return the unique reference to the |
| 1885 | /// injected class name type for the specified templated declaration. |
| 1886 | QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1887 | QualType TST) const { |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1888 | assert(NeedsInjectedClassNameType(Decl)); |
| 1889 | if (Decl->TypeForDecl) { |
| 1890 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
Argyrios Kyrtzidis | 2c2167a | 2010-07-02 11:55:32 +0000 | [diff] [blame] | 1891 | } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDeclaration()) { |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1892 | assert(PrevDecl->TypeForDecl && "previous declaration has no type"); |
| 1893 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 1894 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
| 1895 | } else { |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1896 | Decl->TypeForDecl = |
| 1897 | new (*this, TypeAlignment) InjectedClassNameType(Decl, TST); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1898 | Types.push_back(Decl->TypeForDecl); |
| 1899 | } |
| 1900 | return QualType(Decl->TypeForDecl, 0); |
| 1901 | } |
| 1902 | |
Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1903 | /// getTypeDeclType - Return the unique reference to the type for the |
| 1904 | /// specified type declaration. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1905 | QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const { |
Argyrios Kyrtzidis | 89656d2 | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 1906 | assert(Decl && "Passed null for Decl param"); |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1907 | assert(!Decl->TypeForDecl && "TypeForDecl present in slow case"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1908 | |
John McCall | 81e3850 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1909 | if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl)) |
Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1910 | return getTypedefType(Typedef); |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1911 | |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1912 | assert(!isa<TemplateTypeParmDecl>(Decl) && |
| 1913 | "Template type parameter types are always available."); |
| 1914 | |
John McCall | 81e3850 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1915 | if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1916 | assert(!Record->getPreviousDeclaration() && |
| 1917 | "struct/union has previous declaration"); |
| 1918 | assert(!NeedsInjectedClassNameType(Record)); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 1919 | return getRecordType(Record); |
John McCall | 81e3850 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1920 | } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1921 | assert(!Enum->getPreviousDeclaration() && |
| 1922 | "enum has previous declaration"); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 1923 | return getEnumType(Enum); |
John McCall | 81e3850 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1924 | } else if (const UnresolvedUsingTypenameDecl *Using = |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1925 | dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) { |
| 1926 | Decl->TypeForDecl = new (*this, TypeAlignment) UnresolvedUsingType(Using); |
Mike Stump | e9c6ffc | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 1927 | } else |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1928 | llvm_unreachable("TypeDecl without a type?"); |
Argyrios Kyrtzidis | faf0876 | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1929 | |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1930 | Types.push_back(Decl->TypeForDecl); |
Argyrios Kyrtzidis | faf0876 | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1931 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1932 | } |
| 1933 | |
Chris Lattner | 32d920b | 2007-01-26 02:01:53 +0000 | [diff] [blame] | 1934 | /// getTypedefType - Return the unique reference to the type for the |
Chris Lattner | d0342e5 | 2006-11-20 04:02:15 +0000 | [diff] [blame] | 1935 | /// specified typename decl. |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 1936 | QualType |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1937 | ASTContext::getTypedefType(const TypedefDecl *Decl, QualType Canonical) const { |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1938 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1939 | |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 1940 | if (Canonical.isNull()) |
| 1941 | Canonical = getCanonicalType(Decl->getUnderlyingType()); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1942 | Decl->TypeForDecl = new(*this, TypeAlignment) |
| 1943 | TypedefType(Type::Typedef, Decl, Canonical); |
Chris Lattner | cceab1a | 2007-03-26 20:16:44 +0000 | [diff] [blame] | 1944 | Types.push_back(Decl->TypeForDecl); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1945 | return QualType(Decl->TypeForDecl, 0); |
Chris Lattner | d0342e5 | 2006-11-20 04:02:15 +0000 | [diff] [blame] | 1946 | } |
| 1947 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1948 | QualType ASTContext::getRecordType(const RecordDecl *Decl) const { |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 1949 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 1950 | |
| 1951 | if (const RecordDecl *PrevDecl = Decl->getPreviousDeclaration()) |
| 1952 | if (PrevDecl->TypeForDecl) |
| 1953 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 1954 | |
| 1955 | Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Decl); |
| 1956 | Types.push_back(Decl->TypeForDecl); |
| 1957 | return QualType(Decl->TypeForDecl, 0); |
| 1958 | } |
| 1959 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1960 | QualType ASTContext::getEnumType(const EnumDecl *Decl) const { |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 1961 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 1962 | |
| 1963 | if (const EnumDecl *PrevDecl = Decl->getPreviousDeclaration()) |
| 1964 | if (PrevDecl->TypeForDecl) |
| 1965 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 1966 | |
| 1967 | Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Decl); |
| 1968 | Types.push_back(Decl->TypeForDecl); |
| 1969 | return QualType(Decl->TypeForDecl, 0); |
| 1970 | } |
| 1971 | |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 1972 | QualType ASTContext::getAttributedType(AttributedType::Kind attrKind, |
| 1973 | QualType modifiedType, |
| 1974 | QualType equivalentType) { |
| 1975 | llvm::FoldingSetNodeID id; |
| 1976 | AttributedType::Profile(id, attrKind, modifiedType, equivalentType); |
| 1977 | |
| 1978 | void *insertPos = 0; |
| 1979 | AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos); |
| 1980 | if (type) return QualType(type, 0); |
| 1981 | |
| 1982 | QualType canon = getCanonicalType(equivalentType); |
| 1983 | type = new (*this, TypeAlignment) |
| 1984 | AttributedType(canon, attrKind, modifiedType, equivalentType); |
| 1985 | |
| 1986 | Types.push_back(type); |
| 1987 | AttributedTypes.InsertNode(type, insertPos); |
| 1988 | |
| 1989 | return QualType(type, 0); |
| 1990 | } |
| 1991 | |
| 1992 | |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1993 | /// \brief Retrieve a substitution-result type. |
| 1994 | QualType |
| 1995 | ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1996 | QualType Replacement) const { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1997 | assert(Replacement.isCanonical() |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1998 | && "replacement types must always be canonical"); |
| 1999 | |
| 2000 | llvm::FoldingSetNodeID ID; |
| 2001 | SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); |
| 2002 | void *InsertPos = 0; |
| 2003 | SubstTemplateTypeParmType *SubstParm |
| 2004 | = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2005 | |
| 2006 | if (!SubstParm) { |
| 2007 | SubstParm = new (*this, TypeAlignment) |
| 2008 | SubstTemplateTypeParmType(Parm, Replacement); |
| 2009 | Types.push_back(SubstParm); |
| 2010 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 2011 | } |
| 2012 | |
| 2013 | return QualType(SubstParm, 0); |
| 2014 | } |
| 2015 | |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 2016 | /// \brief Retrieve a |
| 2017 | QualType ASTContext::getSubstTemplateTypeParmPackType( |
| 2018 | const TemplateTypeParmType *Parm, |
| 2019 | const TemplateArgument &ArgPack) { |
| 2020 | #ifndef NDEBUG |
| 2021 | for (TemplateArgument::pack_iterator P = ArgPack.pack_begin(), |
| 2022 | PEnd = ArgPack.pack_end(); |
| 2023 | P != PEnd; ++P) { |
| 2024 | assert(P->getKind() == TemplateArgument::Type &&"Pack contains a non-type"); |
| 2025 | assert(P->getAsType().isCanonical() && "Pack contains non-canonical type"); |
| 2026 | } |
| 2027 | #endif |
| 2028 | |
| 2029 | llvm::FoldingSetNodeID ID; |
| 2030 | SubstTemplateTypeParmPackType::Profile(ID, Parm, ArgPack); |
| 2031 | void *InsertPos = 0; |
| 2032 | if (SubstTemplateTypeParmPackType *SubstParm |
| 2033 | = SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2034 | return QualType(SubstParm, 0); |
| 2035 | |
| 2036 | QualType Canon; |
| 2037 | if (!Parm->isCanonicalUnqualified()) { |
| 2038 | Canon = getCanonicalType(QualType(Parm, 0)); |
| 2039 | Canon = getSubstTemplateTypeParmPackType(cast<TemplateTypeParmType>(Canon), |
| 2040 | ArgPack); |
| 2041 | SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2042 | } |
| 2043 | |
| 2044 | SubstTemplateTypeParmPackType *SubstParm |
| 2045 | = new (*this, TypeAlignment) SubstTemplateTypeParmPackType(Parm, Canon, |
| 2046 | ArgPack); |
| 2047 | Types.push_back(SubstParm); |
| 2048 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 2049 | return QualType(SubstParm, 0); |
| 2050 | } |
| 2051 | |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2052 | /// \brief Retrieve the template type parameter type for a template |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2053 | /// parameter or parameter pack with the given depth, index, and (optionally) |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2054 | /// name. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2055 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2056 | bool ParameterPack, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2057 | IdentifierInfo *Name) const { |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2058 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 2ebcae1 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 2059 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name); |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2060 | void *InsertPos = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2061 | TemplateTypeParmType *TypeParm |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2062 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2063 | |
| 2064 | if (TypeParm) |
| 2065 | return QualType(TypeParm, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2066 | |
Douglas Gregor | 2ebcae1 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 2067 | if (Name) { |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2068 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | 2ebcae1 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 2069 | TypeParm = new (*this, TypeAlignment) |
| 2070 | TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2071 | |
| 2072 | TemplateTypeParmType *TypeCheck |
| 2073 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2074 | assert(!TypeCheck && "Template type parameter canonical type broken"); |
| 2075 | (void)TypeCheck; |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2076 | } else |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2077 | TypeParm = new (*this, TypeAlignment) |
| 2078 | TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2079 | |
| 2080 | Types.push_back(TypeParm); |
| 2081 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 2082 | |
| 2083 | return QualType(TypeParm, 0); |
| 2084 | } |
| 2085 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2086 | TypeSourceInfo * |
| 2087 | ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name, |
| 2088 | SourceLocation NameLoc, |
| 2089 | const TemplateArgumentListInfo &Args, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2090 | QualType CanonType) const { |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2091 | QualType TST = getTemplateSpecializationType(Name, Args, CanonType); |
| 2092 | |
| 2093 | TypeSourceInfo *DI = CreateTypeSourceInfo(TST); |
| 2094 | TemplateSpecializationTypeLoc TL |
| 2095 | = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc()); |
| 2096 | TL.setTemplateNameLoc(NameLoc); |
| 2097 | TL.setLAngleLoc(Args.getLAngleLoc()); |
| 2098 | TL.setRAngleLoc(Args.getRAngleLoc()); |
| 2099 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 2100 | TL.setArgLocInfo(i, Args[i].getLocInfo()); |
| 2101 | return DI; |
| 2102 | } |
| 2103 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2104 | QualType |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2105 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2106 | const TemplateArgumentListInfo &Args, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2107 | QualType Canon) const { |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2108 | unsigned NumArgs = Args.size(); |
| 2109 | |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2110 | llvm::SmallVector<TemplateArgument, 4> ArgVec; |
| 2111 | ArgVec.reserve(NumArgs); |
| 2112 | for (unsigned i = 0; i != NumArgs; ++i) |
| 2113 | ArgVec.push_back(Args[i].getArgument()); |
| 2114 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2115 | return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, |
John McCall | 30576cd | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 2116 | Canon); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2117 | } |
| 2118 | |
| 2119 | QualType |
| 2120 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2121 | const TemplateArgument *Args, |
| 2122 | unsigned NumArgs, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2123 | QualType Canon) const { |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 2124 | if (!Canon.isNull()) |
| 2125 | Canon = getCanonicalType(Canon); |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2126 | else |
| 2127 | Canon = getCanonicalTemplateSpecializationType(Template, Args, NumArgs); |
Douglas Gregor | d56a91e | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 2128 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2129 | // Allocate the (non-canonical) template specialization type, but don't |
| 2130 | // try to unique it: these types typically have location information that |
| 2131 | // we don't unique and don't want to lose. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2132 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2133 | sizeof(TemplateArgument) * NumArgs), |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2134 | TypeAlignment); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2135 | TemplateSpecializationType *Spec |
John McCall | 773cc98 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 2136 | = new (Mem) TemplateSpecializationType(Template, |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2137 | Args, NumArgs, |
Douglas Gregor | 0004417 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 2138 | Canon); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2139 | |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2140 | Types.push_back(Spec); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2141 | return QualType(Spec, 0); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2142 | } |
| 2143 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2144 | QualType |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2145 | ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template, |
| 2146 | const TemplateArgument *Args, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2147 | unsigned NumArgs) const { |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2148 | // Build the canonical template specialization type. |
| 2149 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); |
| 2150 | llvm::SmallVector<TemplateArgument, 4> CanonArgs; |
| 2151 | CanonArgs.reserve(NumArgs); |
| 2152 | for (unsigned I = 0; I != NumArgs; ++I) |
| 2153 | CanonArgs.push_back(getCanonicalTemplateArgument(Args[I])); |
| 2154 | |
| 2155 | // Determine whether this canonical template specialization type already |
| 2156 | // exists. |
| 2157 | llvm::FoldingSetNodeID ID; |
| 2158 | TemplateSpecializationType::Profile(ID, CanonTemplate, |
| 2159 | CanonArgs.data(), NumArgs, *this); |
| 2160 | |
| 2161 | void *InsertPos = 0; |
| 2162 | TemplateSpecializationType *Spec |
| 2163 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2164 | |
| 2165 | if (!Spec) { |
| 2166 | // Allocate a new canonical template specialization type. |
| 2167 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
| 2168 | sizeof(TemplateArgument) * NumArgs), |
| 2169 | TypeAlignment); |
| 2170 | Spec = new (Mem) TemplateSpecializationType(CanonTemplate, |
| 2171 | CanonArgs.data(), NumArgs, |
| 2172 | QualType()); |
| 2173 | Types.push_back(Spec); |
| 2174 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
| 2175 | } |
| 2176 | |
| 2177 | assert(Spec->isDependentType() && |
| 2178 | "Non-dependent template-id type must have a canonical type"); |
| 2179 | return QualType(Spec, 0); |
| 2180 | } |
| 2181 | |
| 2182 | QualType |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2183 | ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword, |
| 2184 | NestedNameSpecifier *NNS, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2185 | QualType NamedType) const { |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2186 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2187 | ElaboratedType::Profile(ID, Keyword, NNS, NamedType); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2188 | |
| 2189 | void *InsertPos = 0; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2190 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2191 | if (T) |
| 2192 | return QualType(T, 0); |
| 2193 | |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2194 | QualType Canon = NamedType; |
| 2195 | if (!Canon.isCanonical()) { |
| 2196 | Canon = getCanonicalType(NamedType); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2197 | ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2198 | assert(!CheckT && "Elaborated canonical type broken"); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2199 | (void)CheckT; |
| 2200 | } |
| 2201 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2202 | T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2203 | Types.push_back(T); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2204 | ElaboratedTypes.InsertNode(T, InsertPos); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2205 | return QualType(T, 0); |
| 2206 | } |
| 2207 | |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 2208 | QualType |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2209 | ASTContext::getParenType(QualType InnerType) const { |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 2210 | llvm::FoldingSetNodeID ID; |
| 2211 | ParenType::Profile(ID, InnerType); |
| 2212 | |
| 2213 | void *InsertPos = 0; |
| 2214 | ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2215 | if (T) |
| 2216 | return QualType(T, 0); |
| 2217 | |
| 2218 | QualType Canon = InnerType; |
| 2219 | if (!Canon.isCanonical()) { |
| 2220 | Canon = getCanonicalType(InnerType); |
| 2221 | ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2222 | assert(!CheckT && "Paren canonical type broken"); |
| 2223 | (void)CheckT; |
| 2224 | } |
| 2225 | |
| 2226 | T = new (*this) ParenType(InnerType, Canon); |
| 2227 | Types.push_back(T); |
| 2228 | ParenTypes.InsertNode(T, InsertPos); |
| 2229 | return QualType(T, 0); |
| 2230 | } |
| 2231 | |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2232 | QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, |
| 2233 | NestedNameSpecifier *NNS, |
| 2234 | const IdentifierInfo *Name, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2235 | QualType Canon) const { |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2236 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 2237 | |
| 2238 | if (Canon.isNull()) { |
| 2239 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2240 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 2241 | if (Keyword == ETK_None) |
| 2242 | CanonKeyword = ETK_Typename; |
| 2243 | |
| 2244 | if (CanonNNS != NNS || CanonKeyword != Keyword) |
| 2245 | Canon = getDependentNameType(CanonKeyword, CanonNNS, Name); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2246 | } |
| 2247 | |
| 2248 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2249 | DependentNameType::Profile(ID, Keyword, NNS, Name); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2250 | |
| 2251 | void *InsertPos = 0; |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2252 | DependentNameType *T |
| 2253 | = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2254 | if (T) |
| 2255 | return QualType(T, 0); |
| 2256 | |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2257 | T = new (*this) DependentNameType(Keyword, NNS, Name, Canon); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2258 | Types.push_back(T); |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2259 | DependentNameTypes.InsertNode(T, InsertPos); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2260 | return QualType(T, 0); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2261 | } |
| 2262 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2263 | QualType |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2264 | ASTContext::getDependentTemplateSpecializationType( |
| 2265 | ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2266 | NestedNameSpecifier *NNS, |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2267 | const IdentifierInfo *Name, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2268 | const TemplateArgumentListInfo &Args) const { |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2269 | // TODO: avoid this copy |
| 2270 | llvm::SmallVector<TemplateArgument, 16> ArgCopy; |
| 2271 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
| 2272 | ArgCopy.push_back(Args[I].getArgument()); |
| 2273 | return getDependentTemplateSpecializationType(Keyword, NNS, Name, |
| 2274 | ArgCopy.size(), |
| 2275 | ArgCopy.data()); |
| 2276 | } |
| 2277 | |
| 2278 | QualType |
| 2279 | ASTContext::getDependentTemplateSpecializationType( |
| 2280 | ElaboratedTypeKeyword Keyword, |
| 2281 | NestedNameSpecifier *NNS, |
| 2282 | const IdentifierInfo *Name, |
| 2283 | unsigned NumArgs, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2284 | const TemplateArgument *Args) const { |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2285 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 2286 | |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2287 | llvm::FoldingSetNodeID ID; |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2288 | DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS, |
| 2289 | Name, NumArgs, Args); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2290 | |
| 2291 | void *InsertPos = 0; |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2292 | DependentTemplateSpecializationType *T |
| 2293 | = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2294 | if (T) |
| 2295 | return QualType(T, 0); |
| 2296 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2297 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2298 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2299 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 2300 | if (Keyword == ETK_None) CanonKeyword = ETK_Typename; |
| 2301 | |
| 2302 | bool AnyNonCanonArgs = false; |
| 2303 | llvm::SmallVector<TemplateArgument, 16> CanonArgs(NumArgs); |
| 2304 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 2305 | CanonArgs[I] = getCanonicalTemplateArgument(Args[I]); |
| 2306 | if (!CanonArgs[I].structurallyEquals(Args[I])) |
| 2307 | AnyNonCanonArgs = true; |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2308 | } |
| 2309 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2310 | QualType Canon; |
| 2311 | if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) { |
| 2312 | Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS, |
| 2313 | Name, NumArgs, |
| 2314 | CanonArgs.data()); |
| 2315 | |
| 2316 | // Find the insert position again. |
| 2317 | DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2318 | } |
| 2319 | |
| 2320 | void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) + |
| 2321 | sizeof(TemplateArgument) * NumArgs), |
| 2322 | TypeAlignment); |
John McCall | 773cc98 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 2323 | T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS, |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2324 | Name, NumArgs, Args, Canon); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2325 | Types.push_back(T); |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2326 | DependentTemplateSpecializationTypes.InsertNode(T, InsertPos); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2327 | return QualType(T, 0); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2328 | } |
| 2329 | |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2330 | QualType ASTContext::getPackExpansionType(QualType Pattern, |
| 2331 | llvm::Optional<unsigned> NumExpansions) { |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2332 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2333 | PackExpansionType::Profile(ID, Pattern, NumExpansions); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2334 | |
| 2335 | assert(Pattern->containsUnexpandedParameterPack() && |
| 2336 | "Pack expansions must expand one or more parameter packs"); |
| 2337 | void *InsertPos = 0; |
| 2338 | PackExpansionType *T |
| 2339 | = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2340 | if (T) |
| 2341 | return QualType(T, 0); |
| 2342 | |
| 2343 | QualType Canon; |
| 2344 | if (!Pattern.isCanonical()) { |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2345 | Canon = getPackExpansionType(getCanonicalType(Pattern), NumExpansions); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2346 | |
| 2347 | // Find the insert position again. |
| 2348 | PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2349 | } |
| 2350 | |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2351 | T = new (*this) PackExpansionType(Pattern, Canon, NumExpansions); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2352 | Types.push_back(T); |
| 2353 | PackExpansionTypes.InsertNode(T, InsertPos); |
| 2354 | return QualType(T, 0); |
| 2355 | } |
| 2356 | |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2357 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 2358 | /// alphabetically. |
| 2359 | static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, |
| 2360 | const ObjCProtocolDecl *RHS) { |
Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2361 | return LHS->getDeclName() < RHS->getDeclName(); |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2362 | } |
| 2363 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2364 | static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols, |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2365 | unsigned NumProtocols) { |
| 2366 | if (NumProtocols == 0) return true; |
| 2367 | |
| 2368 | for (unsigned i = 1; i != NumProtocols; ++i) |
| 2369 | if (!CmpProtocolNames(Protocols[i-1], Protocols[i])) |
| 2370 | return false; |
| 2371 | return true; |
| 2372 | } |
| 2373 | |
| 2374 | static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols, |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2375 | unsigned &NumProtocols) { |
| 2376 | ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2377 | |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2378 | // Sort protocols, keyed by name. |
| 2379 | std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); |
| 2380 | |
| 2381 | // Remove duplicates. |
| 2382 | ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); |
| 2383 | NumProtocols = ProtocolsEnd-Protocols; |
| 2384 | } |
| 2385 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2386 | QualType ASTContext::getObjCObjectType(QualType BaseType, |
| 2387 | ObjCProtocolDecl * const *Protocols, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2388 | unsigned NumProtocols) const { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2389 | // If the base type is an interface and there aren't any protocols |
| 2390 | // to add, then the interface type will do just fine. |
| 2391 | if (!NumProtocols && isa<ObjCInterfaceType>(BaseType)) |
| 2392 | return BaseType; |
| 2393 | |
| 2394 | // Look in the folding set for an existing type. |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2395 | llvm::FoldingSetNodeID ID; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2396 | ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols); |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2397 | void *InsertPos = 0; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2398 | if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2399 | return QualType(QT, 0); |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2400 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2401 | // Build the canonical type, which has the canonical base type and |
| 2402 | // a sorted-and-uniqued list of protocols. |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2403 | QualType Canonical; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2404 | bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols); |
| 2405 | if (!ProtocolsSorted || !BaseType.isCanonical()) { |
| 2406 | if (!ProtocolsSorted) { |
Benjamin Kramer | 2e3197e | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 2407 | llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols, |
| 2408 | Protocols + NumProtocols); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2409 | unsigned UniqueCount = NumProtocols; |
| 2410 | |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2411 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2412 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 2413 | &Sorted[0], UniqueCount); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2414 | } else { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2415 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 2416 | Protocols, NumProtocols); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2417 | } |
| 2418 | |
| 2419 | // Regenerate InsertPos. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2420 | ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2421 | } |
| 2422 | |
| 2423 | unsigned Size = sizeof(ObjCObjectTypeImpl); |
| 2424 | Size += NumProtocols * sizeof(ObjCProtocolDecl *); |
| 2425 | void *Mem = Allocate(Size, TypeAlignment); |
| 2426 | ObjCObjectTypeImpl *T = |
| 2427 | new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols); |
| 2428 | |
| 2429 | Types.push_back(T); |
| 2430 | ObjCObjectTypes.InsertNode(T, InsertPos); |
| 2431 | return QualType(T, 0); |
| 2432 | } |
| 2433 | |
| 2434 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 2435 | /// the given object type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2436 | QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2437 | llvm::FoldingSetNodeID ID; |
| 2438 | ObjCObjectPointerType::Profile(ID, ObjectT); |
| 2439 | |
| 2440 | void *InsertPos = 0; |
| 2441 | if (ObjCObjectPointerType *QT = |
| 2442 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2443 | return QualType(QT, 0); |
| 2444 | |
| 2445 | // Find the canonical object type. |
| 2446 | QualType Canonical; |
| 2447 | if (!ObjectT.isCanonical()) { |
| 2448 | Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT)); |
| 2449 | |
| 2450 | // Regenerate InsertPos. |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2451 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2452 | } |
| 2453 | |
Douglas Gregor | f85bee6 | 2010-02-08 22:59:26 +0000 | [diff] [blame] | 2454 | // No match. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2455 | void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment); |
| 2456 | ObjCObjectPointerType *QType = |
| 2457 | new (Mem) ObjCObjectPointerType(Canonical, ObjectT); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2458 | |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2459 | Types.push_back(QType); |
| 2460 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2461 | return QualType(QType, 0); |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2462 | } |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2463 | |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2464 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 2465 | /// specified ObjC interface decl. The list of protocols is optional. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2466 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) const { |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2467 | if (Decl->TypeForDecl) |
| 2468 | return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2469 | |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2470 | // FIXME: redeclarations? |
| 2471 | void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment); |
| 2472 | ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl); |
| 2473 | Decl->TypeForDecl = T; |
| 2474 | Types.push_back(T); |
| 2475 | return QualType(T, 0); |
Fariborz Jahanian | 70e8f10 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2476 | } |
| 2477 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2478 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 2479 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2480 | /// multiple declarations that refer to "typeof(x)" all contain different |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2481 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2482 | /// on canonical type's (which are always unique). |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2483 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) const { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2484 | TypeOfExprType *toe; |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2485 | if (tofExpr->isTypeDependent()) { |
| 2486 | llvm::FoldingSetNodeID ID; |
| 2487 | DependentTypeOfExprType::Profile(ID, *this, tofExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2488 | |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2489 | void *InsertPos = 0; |
| 2490 | DependentTypeOfExprType *Canon |
| 2491 | = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2492 | if (Canon) { |
| 2493 | // We already have a "canonical" version of an identical, dependent |
| 2494 | // typeof(expr) type. Use that as our canonical type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2495 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2496 | QualType((TypeOfExprType*)Canon, 0)); |
| 2497 | } |
| 2498 | else { |
| 2499 | // Build a new, canonical typeof(expr) type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2500 | Canon |
| 2501 | = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2502 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); |
| 2503 | toe = Canon; |
| 2504 | } |
| 2505 | } else { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2506 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2507 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2508 | } |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2509 | Types.push_back(toe); |
| 2510 | return QualType(toe, 0); |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2511 | } |
| 2512 | |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2513 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 2514 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 2515 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2516 | /// an issue. This doesn't effect the type checker, since it operates |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2517 | /// on canonical type's (which are always unique). |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2518 | QualType ASTContext::getTypeOfType(QualType tofType) const { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2519 | QualType Canonical = getCanonicalType(tofType); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2520 | TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2521 | Types.push_back(tot); |
| 2522 | return QualType(tot, 0); |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2523 | } |
| 2524 | |
Anders Carlsson | ad6bd35 | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2525 | /// getDecltypeForExpr - Given an expr, will return the decltype for that |
| 2526 | /// expression, according to the rules in C++0x [dcl.type.simple]p4 |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2527 | static QualType getDecltypeForExpr(const Expr *e, const ASTContext &Context) { |
Anders Carlsson | 7d20957 | 2009-06-25 15:00:34 +0000 | [diff] [blame] | 2528 | if (e->isTypeDependent()) |
| 2529 | return Context.DependentTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2530 | |
Anders Carlsson | ad6bd35 | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2531 | // If e is an id expression or a class member access, decltype(e) is defined |
| 2532 | // as the type of the entity named by e. |
| 2533 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) { |
| 2534 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) |
| 2535 | return VD->getType(); |
| 2536 | } |
| 2537 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) { |
| 2538 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) |
| 2539 | return FD->getType(); |
| 2540 | } |
| 2541 | // If e is a function call or an invocation of an overloaded operator, |
| 2542 | // (parentheses around e are ignored), decltype(e) is defined as the |
| 2543 | // return type of that function. |
| 2544 | if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens())) |
| 2545 | return CE->getCallReturnType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2546 | |
Anders Carlsson | ad6bd35 | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2547 | QualType T = e->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2548 | |
| 2549 | // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is |
Anders Carlsson | ad6bd35 | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2550 | // defined as T&, otherwise decltype(e) is defined as T. |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 2551 | if (e->isLValue()) |
Anders Carlsson | ad6bd35 | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2552 | T = Context.getLValueReferenceType(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2553 | |
Anders Carlsson | ad6bd35 | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2554 | return T; |
| 2555 | } |
| 2556 | |
Anders Carlsson | 81df7b8 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2557 | /// getDecltypeType - Unlike many "get<Type>" functions, we don't unique |
| 2558 | /// DecltypeType AST's. The only motivation to unique these nodes would be |
| 2559 | /// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2560 | /// an issue. This doesn't effect the type checker, since it operates |
Anders Carlsson | 81df7b8 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2561 | /// on canonical type's (which are always unique). |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2562 | QualType ASTContext::getDecltypeType(Expr *e) const { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2563 | DecltypeType *dt; |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2564 | if (e->isTypeDependent()) { |
| 2565 | llvm::FoldingSetNodeID ID; |
| 2566 | DependentDecltypeType::Profile(ID, *this, e); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2567 | |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2568 | void *InsertPos = 0; |
| 2569 | DependentDecltypeType *Canon |
| 2570 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2571 | if (Canon) { |
| 2572 | // We already have a "canonical" version of an equivalent, dependent |
| 2573 | // decltype type. Use that as our canonical type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2574 | dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy, |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2575 | QualType((DecltypeType*)Canon, 0)); |
| 2576 | } |
| 2577 | else { |
| 2578 | // Build a new, canonical typeof(expr) type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2579 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2580 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); |
| 2581 | dt = Canon; |
| 2582 | } |
| 2583 | } else { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2584 | QualType T = getDecltypeForExpr(e, *this); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2585 | dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T)); |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2586 | } |
Anders Carlsson | 81df7b8 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2587 | Types.push_back(dt); |
| 2588 | return QualType(dt, 0); |
| 2589 | } |
| 2590 | |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 2591 | /// getTagDeclType - Return the unique reference to the type for the |
| 2592 | /// specified TagDecl (struct/union/class/enum) decl. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2593 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) const { |
Ted Kremenek | 2b0ce11 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 2594 | assert (Decl); |
Mike Stump | b93185d | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 2595 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 2596 | // away const? mutable? |
| 2597 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 2598 | } |
| 2599 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2600 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 2601 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 2602 | /// needs to agree with the definition in <stddef.h>. |
Anders Carlsson | 22f443f | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 2603 | CanQualType ASTContext::getSizeType() const { |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2604 | return getFromTargetType(Target.getSizeType()); |
Steve Naroff | 92e30f8 | 2007-04-02 22:35:25 +0000 | [diff] [blame] | 2605 | } |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 2606 | |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 2607 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 2608 | /// Used when in C++, as a GCC extension. |
| 2609 | QualType ASTContext::getSignedWCharType() const { |
| 2610 | // FIXME: derive from "Target" ? |
| 2611 | return WCharTy; |
| 2612 | } |
| 2613 | |
| 2614 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 2615 | /// Used when in C++, as a GCC extension. |
| 2616 | QualType ASTContext::getUnsignedWCharType() const { |
| 2617 | // FIXME: derive from "Target" ? |
| 2618 | return UnsignedIntTy; |
| 2619 | } |
| 2620 | |
Chris Lattner | d2b88ab | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 2621 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) |
| 2622 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 2623 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2624 | return getFromTargetType(Target.getPtrDiffType(0)); |
Chris Lattner | d2b88ab | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 2625 | } |
| 2626 | |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2627 | //===----------------------------------------------------------------------===// |
| 2628 | // Type Operators |
| 2629 | //===----------------------------------------------------------------------===// |
| 2630 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2631 | CanQualType ASTContext::getCanonicalParamType(QualType T) const { |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2632 | // Push qualifiers into arrays, and then discard any remaining |
| 2633 | // qualifiers. |
| 2634 | T = getCanonicalType(T); |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2635 | T = getVariableArrayDecayedType(T); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2636 | const Type *Ty = T.getTypePtr(); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2637 | QualType Result; |
| 2638 | if (isa<ArrayType>(Ty)) { |
| 2639 | Result = getArrayDecayedType(QualType(Ty,0)); |
| 2640 | } else if (isa<FunctionType>(Ty)) { |
| 2641 | Result = getPointerType(QualType(Ty, 0)); |
| 2642 | } else { |
| 2643 | Result = QualType(Ty, 0); |
| 2644 | } |
| 2645 | |
| 2646 | return CanQualType::CreateUnsafe(Result); |
| 2647 | } |
| 2648 | |
Chris Lattner | ed0d079 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 2649 | /// getCanonicalType - Return the canonical (structural) type corresponding to |
| 2650 | /// the specified potentially non-canonical type. The non-canonical version |
| 2651 | /// of a type may have many "decorated" versions of types. Decorators can |
| 2652 | /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed |
| 2653 | /// to be free of any of these, allowing two canonical types to be compared |
| 2654 | /// for exact equality with a simple pointer comparison. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2655 | CanQualType ASTContext::getCanonicalType(QualType T) const { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2656 | QualifierCollector Quals; |
| 2657 | const Type *Ptr = Quals.strip(T); |
| 2658 | QualType CanType = Ptr->getCanonicalTypeInternal(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2659 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2660 | // The canonical internal type will be the canonical type *except* |
| 2661 | // that we push type qualifiers down through array types. |
| 2662 | |
| 2663 | // If there are no new qualifiers to push down, stop here. |
| 2664 | if (!Quals.hasQualifiers()) |
Douglas Gregor | 2211d34 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2665 | return CanQualType::CreateUnsafe(CanType); |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2666 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2667 | // If the type qualifiers are on an array type, get the canonical |
| 2668 | // type of the array with the qualifiers applied to the element |
| 2669 | // type. |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2670 | ArrayType *AT = dyn_cast<ArrayType>(CanType); |
| 2671 | if (!AT) |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2672 | return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2673 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2674 | // Get the canonical version of the element with the extra qualifiers on it. |
| 2675 | // This can recursively sink qualifiers through multiple levels of arrays. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2676 | QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals); |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2677 | NewEltTy = getCanonicalType(NewEltTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2678 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2679 | if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
Douglas Gregor | 2211d34 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2680 | return CanQualType::CreateUnsafe( |
| 2681 | getConstantArrayType(NewEltTy, CAT->getSize(), |
| 2682 | CAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2683 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2684 | if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) |
Douglas Gregor | 2211d34 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2685 | return CanQualType::CreateUnsafe( |
| 2686 | getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2687 | IAT->getIndexTypeCVRQualifiers())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2688 | |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2689 | if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT)) |
Douglas Gregor | 2211d34 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2690 | return CanQualType::CreateUnsafe( |
| 2691 | getDependentSizedArrayType(NewEltTy, |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 2692 | DSAT->getSizeExpr(), |
Douglas Gregor | 2211d34 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2693 | DSAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2694 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 326b2fa | 2009-10-30 22:56:57 +0000 | [diff] [blame] | 2695 | DSAT->getBracketsRange())->getCanonicalTypeInternal()); |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2696 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2697 | VariableArrayType *VAT = cast<VariableArrayType>(AT); |
Douglas Gregor | 2211d34 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2698 | return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy, |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 2699 | VAT->getSizeExpr(), |
Douglas Gregor | 2211d34 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2700 | VAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2701 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 2211d34 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2702 | VAT->getBracketsRange())); |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2703 | } |
| 2704 | |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 2705 | QualType ASTContext::getUnqualifiedArrayType(QualType type, |
| 2706 | Qualifiers &quals) { |
| 2707 | SplitQualType splitType = type.getSplitUnqualifiedType(); |
| 2708 | |
| 2709 | // FIXME: getSplitUnqualifiedType() actually walks all the way to |
| 2710 | // the unqualified desugared type and then drops it on the floor. |
| 2711 | // We then have to strip that sugar back off with |
| 2712 | // getUnqualifiedDesugaredType(), which is silly. |
| 2713 | const ArrayType *AT = |
| 2714 | dyn_cast<ArrayType>(splitType.first->getUnqualifiedDesugaredType()); |
| 2715 | |
| 2716 | // If we don't have an array, just use the results in splitType. |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2717 | if (!AT) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 2718 | quals = splitType.second; |
| 2719 | return QualType(splitType.first, 0); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2720 | } |
| 2721 | |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 2722 | // Otherwise, recurse on the array's element type. |
| 2723 | QualType elementType = AT->getElementType(); |
| 2724 | QualType unqualElementType = getUnqualifiedArrayType(elementType, quals); |
| 2725 | |
| 2726 | // If that didn't change the element type, AT has no qualifiers, so we |
| 2727 | // can just use the results in splitType. |
| 2728 | if (elementType == unqualElementType) { |
| 2729 | assert(quals.empty()); // from the recursive call |
| 2730 | quals = splitType.second; |
| 2731 | return QualType(splitType.first, 0); |
| 2732 | } |
| 2733 | |
| 2734 | // Otherwise, add in the qualifiers from the outermost type, then |
| 2735 | // build the type back up. |
| 2736 | quals.addConsistentQualifiers(splitType.second); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2737 | |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2738 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 2739 | return getConstantArrayType(unqualElementType, CAT->getSize(), |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2740 | CAT->getSizeModifier(), 0); |
| 2741 | } |
| 2742 | |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2743 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 2744 | return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2745 | } |
| 2746 | |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2747 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 2748 | return getVariableArrayType(unqualElementType, |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 2749 | VAT->getSizeExpr(), |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2750 | VAT->getSizeModifier(), |
| 2751 | VAT->getIndexTypeCVRQualifiers(), |
| 2752 | VAT->getBracketsRange()); |
| 2753 | } |
| 2754 | |
| 2755 | const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT); |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 2756 | return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(), |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2757 | DSAT->getSizeModifier(), 0, |
| 2758 | SourceRange()); |
| 2759 | } |
| 2760 | |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2761 | /// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that |
| 2762 | /// may be similar (C++ 4.4), replaces T1 and T2 with the type that |
| 2763 | /// they point to and return true. If T1 and T2 aren't pointer types |
| 2764 | /// or pointer-to-member types, or if they are not similar at this |
| 2765 | /// level, returns false and leaves T1 and T2 unchanged. Top-level |
| 2766 | /// qualifiers on T1 and T2 are ignored. This function will typically |
| 2767 | /// be called in a loop that successively "unwraps" pointer and |
| 2768 | /// pointer-to-member types to compare them at each level. |
| 2769 | bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) { |
| 2770 | const PointerType *T1PtrType = T1->getAs<PointerType>(), |
| 2771 | *T2PtrType = T2->getAs<PointerType>(); |
| 2772 | if (T1PtrType && T2PtrType) { |
| 2773 | T1 = T1PtrType->getPointeeType(); |
| 2774 | T2 = T2PtrType->getPointeeType(); |
| 2775 | return true; |
| 2776 | } |
| 2777 | |
| 2778 | const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(), |
| 2779 | *T2MPType = T2->getAs<MemberPointerType>(); |
| 2780 | if (T1MPType && T2MPType && |
| 2781 | hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0), |
| 2782 | QualType(T2MPType->getClass(), 0))) { |
| 2783 | T1 = T1MPType->getPointeeType(); |
| 2784 | T2 = T2MPType->getPointeeType(); |
| 2785 | return true; |
| 2786 | } |
| 2787 | |
| 2788 | if (getLangOptions().ObjC1) { |
| 2789 | const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(), |
| 2790 | *T2OPType = T2->getAs<ObjCObjectPointerType>(); |
| 2791 | if (T1OPType && T2OPType) { |
| 2792 | T1 = T1OPType->getPointeeType(); |
| 2793 | T2 = T2OPType->getPointeeType(); |
| 2794 | return true; |
| 2795 | } |
| 2796 | } |
| 2797 | |
| 2798 | // FIXME: Block pointers, too? |
| 2799 | |
| 2800 | return false; |
| 2801 | } |
| 2802 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2803 | DeclarationNameInfo |
| 2804 | ASTContext::getNameForTemplate(TemplateName Name, |
| 2805 | SourceLocation NameLoc) const { |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2806 | if (TemplateDecl *TD = Name.getAsTemplateDecl()) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2807 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 2808 | return DeclarationNameInfo(TD->getDeclName(), NameLoc); |
| 2809 | |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2810 | if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2811 | DeclarationName DName; |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2812 | if (DTN->isIdentifier()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2813 | DName = DeclarationNames.getIdentifier(DTN->getIdentifier()); |
| 2814 | return DeclarationNameInfo(DName, NameLoc); |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2815 | } else { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2816 | DName = DeclarationNames.getCXXOperatorName(DTN->getOperator()); |
| 2817 | // DNInfo work in progress: FIXME: source locations? |
| 2818 | DeclarationNameLoc DNLoc; |
| 2819 | DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding(); |
| 2820 | DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding(); |
| 2821 | return DeclarationNameInfo(DName, NameLoc, DNLoc); |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2822 | } |
| 2823 | } |
| 2824 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 2825 | OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate(); |
| 2826 | assert(Storage); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2827 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 2828 | return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc); |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2829 | } |
| 2830 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2831 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const { |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 2832 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 2833 | if (TemplateTemplateParmDecl *TTP |
| 2834 | = dyn_cast<TemplateTemplateParmDecl>(Template)) |
| 2835 | Template = getCanonicalTemplateTemplateParmDecl(TTP); |
| 2836 | |
| 2837 | // The canonical template name is the canonical template declaration. |
Argyrios Kyrtzidis | 6b7e376 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 2838 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 2839 | } |
Douglas Gregor | 6bc5058 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2840 | |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 2841 | if (SubstTemplateTemplateParmPackStorage *SubstPack |
| 2842 | = Name.getAsSubstTemplateTemplateParmPack()) { |
| 2843 | TemplateTemplateParmDecl *CanonParam |
| 2844 | = getCanonicalTemplateTemplateParmDecl(SubstPack->getParameterPack()); |
| 2845 | TemplateArgument CanonArgPack |
| 2846 | = getCanonicalTemplateArgument(SubstPack->getArgumentPack()); |
| 2847 | return getSubstTemplateTemplateParmPack(CanonParam, CanonArgPack); |
| 2848 | } |
| 2849 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 2850 | assert(!Name.getAsOverloadedTemplate()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2851 | |
Douglas Gregor | 6bc5058 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2852 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 2853 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 2854 | return DTN->CanonicalTemplateName; |
| 2855 | } |
| 2856 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 2857 | bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) { |
| 2858 | X = getCanonicalTemplateName(X); |
| 2859 | Y = getCanonicalTemplateName(Y); |
| 2860 | return X.getAsVoidPointer() == Y.getAsVoidPointer(); |
| 2861 | } |
| 2862 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2863 | TemplateArgument |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2864 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const { |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2865 | switch (Arg.getKind()) { |
| 2866 | case TemplateArgument::Null: |
| 2867 | return Arg; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2868 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2869 | case TemplateArgument::Expression: |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2870 | return Arg; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2871 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2872 | case TemplateArgument::Declaration: |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2873 | return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2874 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2875 | case TemplateArgument::Template: |
| 2876 | return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate())); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2877 | |
| 2878 | case TemplateArgument::TemplateExpansion: |
| 2879 | return TemplateArgument(getCanonicalTemplateName( |
| 2880 | Arg.getAsTemplateOrTemplatePattern()), |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 2881 | Arg.getNumTemplateExpansions()); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2882 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2883 | case TemplateArgument::Integral: |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2884 | return TemplateArgument(*Arg.getAsIntegral(), |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2885 | getCanonicalType(Arg.getIntegralType())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2886 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2887 | case TemplateArgument::Type: |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2888 | return TemplateArgument(getCanonicalType(Arg.getAsType())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2889 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2890 | case TemplateArgument::Pack: { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 2891 | if (Arg.pack_size() == 0) |
| 2892 | return Arg; |
| 2893 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2894 | TemplateArgument *CanonArgs |
| 2895 | = new (*this) TemplateArgument[Arg.pack_size()]; |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2896 | unsigned Idx = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2897 | for (TemplateArgument::pack_iterator A = Arg.pack_begin(), |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2898 | AEnd = Arg.pack_end(); |
| 2899 | A != AEnd; (void)++A, ++Idx) |
| 2900 | CanonArgs[Idx] = getCanonicalTemplateArgument(*A); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2901 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2902 | return TemplateArgument(CanonArgs, Arg.pack_size()); |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2903 | } |
| 2904 | } |
| 2905 | |
| 2906 | // Silence GCC warning |
| 2907 | assert(false && "Unhandled template argument kind"); |
| 2908 | return TemplateArgument(); |
| 2909 | } |
| 2910 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2911 | NestedNameSpecifier * |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2912 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2913 | if (!NNS) |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2914 | return 0; |
| 2915 | |
| 2916 | switch (NNS->getKind()) { |
| 2917 | case NestedNameSpecifier::Identifier: |
| 2918 | // Canonicalize the prefix but keep the identifier the same. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2919 | return NestedNameSpecifier::Create(*this, |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2920 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 2921 | NNS->getAsIdentifier()); |
| 2922 | |
| 2923 | case NestedNameSpecifier::Namespace: |
| 2924 | // A namespace is canonical; build a nested-name-specifier with |
| 2925 | // this namespace and no prefix. |
| 2926 | return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace()); |
| 2927 | |
| 2928 | case NestedNameSpecifier::TypeSpec: |
| 2929 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 2930 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
Douglas Gregor | 3ade570 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 2931 | |
| 2932 | // If we have some kind of dependent-named type (e.g., "typename T::type"), |
| 2933 | // break it apart into its prefix and identifier, then reconsititute those |
| 2934 | // as the canonical nested-name-specifier. This is required to canonicalize |
| 2935 | // a dependent nested-name-specifier involving typedefs of dependent-name |
| 2936 | // types, e.g., |
| 2937 | // typedef typename T::type T1; |
| 2938 | // typedef typename T1::type T2; |
| 2939 | if (const DependentNameType *DNT = T->getAs<DependentNameType>()) { |
| 2940 | NestedNameSpecifier *Prefix |
| 2941 | = getCanonicalNestedNameSpecifier(DNT->getQualifier()); |
| 2942 | return NestedNameSpecifier::Create(*this, Prefix, |
| 2943 | const_cast<IdentifierInfo *>(DNT->getIdentifier())); |
| 2944 | } |
| 2945 | |
Douglas Gregor | cc10cbf | 2010-11-04 00:14:23 +0000 | [diff] [blame] | 2946 | // Do the same thing as above, but with dependent-named specializations. |
Douglas Gregor | 3ade570 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 2947 | if (const DependentTemplateSpecializationType *DTST |
| 2948 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 2949 | NestedNameSpecifier *Prefix |
| 2950 | = getCanonicalNestedNameSpecifier(DTST->getQualifier()); |
| 2951 | TemplateName Name |
| 2952 | = getDependentTemplateName(Prefix, DTST->getIdentifier()); |
| 2953 | T = getTemplateSpecializationType(Name, |
| 2954 | DTST->getArgs(), DTST->getNumArgs()); |
| 2955 | T = getCanonicalType(T); |
| 2956 | } |
| 2957 | |
| 2958 | return NestedNameSpecifier::Create(*this, 0, false, T.getTypePtr()); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2959 | } |
| 2960 | |
| 2961 | case NestedNameSpecifier::Global: |
| 2962 | // The global specifier is canonical and unique. |
| 2963 | return NNS; |
| 2964 | } |
| 2965 | |
| 2966 | // Required to silence a GCC warning |
| 2967 | return 0; |
| 2968 | } |
| 2969 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2970 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2971 | const ArrayType *ASTContext::getAsArrayType(QualType T) const { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2972 | // Handle the non-qualified case efficiently. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2973 | if (!T.hasLocalQualifiers()) { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2974 | // Handle the common positive case fast. |
| 2975 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 2976 | return AT; |
| 2977 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2978 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2979 | // Handle the common negative case fast. |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2980 | QualType CType = T->getCanonicalTypeInternal(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2981 | if (!isa<ArrayType>(CType)) |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2982 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2983 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2984 | // Apply any qualifiers from the array type to the element type. This |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2985 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 2986 | // any type qualifiers, the element type is so qualified, not the array type." |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2987 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2988 | // If we get here, we either have type qualifiers on the type, or we have |
| 2989 | // sugar such as a typedef in the way. If we have type qualifiers on the type |
Douglas Gregor | 2211d34 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2990 | // we must propagate them down into the element type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2991 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2992 | QualifierCollector Qs; |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 2993 | const Type *Ty = Qs.strip(T.getDesugaredType(*this)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2994 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2995 | // If we have a simple case, just return now. |
| 2996 | const ArrayType *ATy = dyn_cast<ArrayType>(Ty); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2997 | if (ATy == 0 || Qs.empty()) |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2998 | return ATy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2999 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3000 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 3001 | // qualifiers into the array element type and return a new array type. |
| 3002 | // Get the canonical version of the element with the extra qualifiers on it. |
| 3003 | // This can recursively sink qualifiers through multiple levels of arrays. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3004 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3005 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3006 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 3007 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 3008 | CAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3009 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3010 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 3011 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 3012 | IAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3013 | IAT->getIndexTypeCVRQualifiers())); |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3014 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3015 | if (const DependentSizedArrayType *DSAT |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3016 | = dyn_cast<DependentSizedArrayType>(ATy)) |
| 3017 | return cast<ArrayType>( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3018 | getDependentSizedArrayType(NewEltTy, |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3019 | DSAT->getSizeExpr(), |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3020 | DSAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3021 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3022 | DSAT->getBracketsRange())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3023 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3024 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3025 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3026 | VAT->getSizeExpr(), |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3027 | VAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3028 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3029 | VAT->getBracketsRange())); |
Chris Lattner | ed0d079 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 3030 | } |
| 3031 | |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3032 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 3033 | /// specified array type to a pointer. This operation is non-trivial when |
| 3034 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 3035 | /// this returns a pointer to a properly qualified element of the array. |
| 3036 | /// |
| 3037 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3038 | QualType ASTContext::getArrayDecayedType(QualType Ty) const { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3039 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 3040 | // typedefs in the element type of the array. This also handles propagation |
| 3041 | // of type qualifiers from the array type into the element type if present |
| 3042 | // (C99 6.7.3p8). |
| 3043 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 3044 | assert(PrettyArrayType && "Not an array type!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3045 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3046 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3047 | |
| 3048 | // int x[restrict 4] -> int *restrict |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3049 | return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers()); |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3050 | } |
| 3051 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3052 | QualType ASTContext::getBaseElementType(QualType QT) const { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3053 | QualifierCollector Qs; |
Benjamin Kramer | 2e3197e | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 3054 | while (const ArrayType *AT = getAsArrayType(QualType(Qs.strip(QT), 0))) |
| 3055 | QT = AT->getElementType(); |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 3056 | return Qs.apply(*this, QT); |
Douglas Gregor | 79f83ed | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 3057 | } |
| 3058 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3059 | QualType ASTContext::getBaseElementType(const ArrayType *AT) const { |
Anders Carlsson | 4bf8214 | 2009-09-25 01:23:32 +0000 | [diff] [blame] | 3060 | QualType ElemTy = AT->getElementType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3061 | |
Anders Carlsson | 4bf8214 | 2009-09-25 01:23:32 +0000 | [diff] [blame] | 3062 | if (const ArrayType *AT = getAsArrayType(ElemTy)) |
| 3063 | return getBaseElementType(AT); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3064 | |
Anders Carlsson | e0808df | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 3065 | return ElemTy; |
| 3066 | } |
| 3067 | |
Fariborz Jahanian | 6c9e5a2 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 3068 | /// getConstantArrayElementCount - Returns number of constant array elements. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3069 | uint64_t |
Fariborz Jahanian | 6c9e5a2 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 3070 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
| 3071 | uint64_t ElementCount = 1; |
| 3072 | do { |
| 3073 | ElementCount *= CA->getSize().getZExtValue(); |
| 3074 | CA = dyn_cast<ConstantArrayType>(CA->getElementType()); |
| 3075 | } while (CA); |
| 3076 | return ElementCount; |
| 3077 | } |
| 3078 | |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 3079 | /// getFloatingRank - Return a relative rank for floating point types. |
| 3080 | /// This routine will assert if passed a built-in type that isn't a float. |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3081 | static FloatingRank getFloatingRank(QualType T) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3082 | if (const ComplexType *CT = T->getAs<ComplexType>()) |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 3083 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3084 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3085 | assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"); |
| 3086 | switch (T->getAs<BuiltinType>()->getKind()) { |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3087 | default: assert(0 && "getFloatingRank(): not a floating type"); |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 3088 | case BuiltinType::Float: return FloatRank; |
| 3089 | case BuiltinType::Double: return DoubleRank; |
| 3090 | case BuiltinType::LongDouble: return LongDoubleRank; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 3091 | } |
| 3092 | } |
| 3093 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3094 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 3095 | /// point or a complex type (based on typeDomain/typeSize). |
Steve Naroff | fc6ffa2 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 3096 | /// 'typeDomain' is a real floating point or complex type. |
| 3097 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | b9dfb03 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 3098 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 3099 | QualType Domain) const { |
| 3100 | FloatingRank EltRank = getFloatingRank(Size); |
| 3101 | if (Domain->isComplexType()) { |
| 3102 | switch (EltRank) { |
Steve Naroff | fc6ffa2 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 3103 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
Steve Naroff | 9091ef7 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 3104 | case FloatRank: return FloatComplexTy; |
| 3105 | case DoubleRank: return DoubleComplexTy; |
| 3106 | case LongDoubleRank: return LongDoubleComplexTy; |
| 3107 | } |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 3108 | } |
Chris Lattner | b9dfb03 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 3109 | |
| 3110 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 3111 | switch (EltRank) { |
| 3112 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
| 3113 | case FloatRank: return FloatTy; |
| 3114 | case DoubleRank: return DoubleTy; |
| 3115 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | 9091ef7 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 3116 | } |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 3117 | } |
| 3118 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3119 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 3120 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 3121 | /// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3122 | /// LHS < RHS, return -1. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3123 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const { |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3124 | FloatingRank LHSR = getFloatingRank(LHS); |
| 3125 | FloatingRank RHSR = getFloatingRank(RHS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3126 | |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3127 | if (LHSR == RHSR) |
Steve Naroff | 7af82d4 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 3128 | return 0; |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3129 | if (LHSR > RHSR) |
Steve Naroff | 7af82d4 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 3130 | return 1; |
| 3131 | return -1; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 3132 | } |
| 3133 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3134 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 3135 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 3136 | /// or if it is not canonicalized. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3137 | unsigned ASTContext::getIntegerRank(Type *T) const { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 3138 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3139 | if (EnumType* ET = dyn_cast<EnumType>(T)) |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 3140 | T = ET->getDecl()->getPromotionType().getTypePtr(); |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3141 | |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 3142 | if (T->isSpecificBuiltinType(BuiltinType::WChar_S) || |
| 3143 | T->isSpecificBuiltinType(BuiltinType::WChar_U)) |
Eli Friedman | c131d3b | 2009-07-05 23:44:27 +0000 | [diff] [blame] | 3144 | T = getFromTargetType(Target.getWCharType()).getTypePtr(); |
| 3145 | |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3146 | if (T->isSpecificBuiltinType(BuiltinType::Char16)) |
| 3147 | T = getFromTargetType(Target.getChar16Type()).getTypePtr(); |
| 3148 | |
| 3149 | if (T->isSpecificBuiltinType(BuiltinType::Char32)) |
| 3150 | T = getFromTargetType(Target.getChar32Type()).getTypePtr(); |
| 3151 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3152 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3153 | default: assert(0 && "getIntegerRank(): not a built-in integer"); |
| 3154 | case BuiltinType::Bool: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3155 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3156 | case BuiltinType::Char_S: |
| 3157 | case BuiltinType::Char_U: |
| 3158 | case BuiltinType::SChar: |
| 3159 | case BuiltinType::UChar: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3160 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3161 | case BuiltinType::Short: |
| 3162 | case BuiltinType::UShort: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3163 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3164 | case BuiltinType::Int: |
| 3165 | case BuiltinType::UInt: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3166 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3167 | case BuiltinType::Long: |
| 3168 | case BuiltinType::ULong: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3169 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3170 | case BuiltinType::LongLong: |
| 3171 | case BuiltinType::ULongLong: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3172 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | f122cef | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 3173 | case BuiltinType::Int128: |
| 3174 | case BuiltinType::UInt128: |
| 3175 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3176 | } |
| 3177 | } |
| 3178 | |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3179 | /// \brief Whether this is a promotable bitfield reference according |
| 3180 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
| 3181 | /// |
| 3182 | /// \returns the type this bit-field will promote to, or NULL if no |
| 3183 | /// promotion occurs. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3184 | QualType ASTContext::isPromotableBitField(Expr *E) const { |
Douglas Gregor | e05d3cb | 2010-05-24 20:13:53 +0000 | [diff] [blame] | 3185 | if (E->isTypeDependent() || E->isValueDependent()) |
| 3186 | return QualType(); |
| 3187 | |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3188 | FieldDecl *Field = E->getBitField(); |
| 3189 | if (!Field) |
| 3190 | return QualType(); |
| 3191 | |
| 3192 | QualType FT = Field->getType(); |
| 3193 | |
| 3194 | llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this); |
| 3195 | uint64_t BitWidth = BitWidthAP.getZExtValue(); |
| 3196 | uint64_t IntSize = getTypeSize(IntTy); |
| 3197 | // GCC extension compatibility: if the bit-field size is less than or equal |
| 3198 | // to the size of int, it gets promoted no matter what its type is. |
| 3199 | // For instance, unsigned long bf : 4 gets promoted to signed int. |
| 3200 | if (BitWidth < IntSize) |
| 3201 | return IntTy; |
| 3202 | |
| 3203 | if (BitWidth == IntSize) |
| 3204 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
| 3205 | |
| 3206 | // Types bigger than int are not subject to promotions, and therefore act |
| 3207 | // like the base type. |
| 3208 | // FIXME: This doesn't quite match what gcc does, but what gcc does here |
| 3209 | // is ridiculous. |
| 3210 | return QualType(); |
| 3211 | } |
| 3212 | |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3213 | /// getPromotedIntegerType - Returns the type that Promotable will |
| 3214 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
| 3215 | /// integer type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3216 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) const { |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3217 | assert(!Promotable.isNull()); |
| 3218 | assert(Promotable->isPromotableIntegerType()); |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 3219 | if (const EnumType *ET = Promotable->getAs<EnumType>()) |
| 3220 | return ET->getDecl()->getPromotionType(); |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3221 | if (Promotable->isSignedIntegerType()) |
| 3222 | return IntTy; |
| 3223 | uint64_t PromotableSize = getTypeSize(Promotable); |
| 3224 | uint64_t IntSize = getTypeSize(IntTy); |
| 3225 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
| 3226 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
| 3227 | } |
| 3228 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3229 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3230 | /// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3231 | /// LHS < RHS, return -1. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3232 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3233 | Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 3234 | Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3235 | if (LHSC == RHSC) return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3236 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3237 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 3238 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3239 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3240 | unsigned LHSRank = getIntegerRank(LHSC); |
| 3241 | unsigned RHSRank = getIntegerRank(RHSC); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3242 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3243 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 3244 | if (LHSRank == RHSRank) return 0; |
| 3245 | return LHSRank > RHSRank ? 1 : -1; |
| 3246 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3247 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3248 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 3249 | if (LHSUnsigned) { |
| 3250 | // If the unsigned [LHS] type is larger, return it. |
| 3251 | if (LHSRank >= RHSRank) |
| 3252 | return 1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3253 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3254 | // If the signed type can represent all values of the unsigned type, it |
| 3255 | // wins. Because we are dealing with 2's complement and types that are |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3256 | // powers of two larger than each other, this is always safe. |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3257 | return -1; |
| 3258 | } |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3259 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3260 | // If the unsigned [RHS] type is larger, return it. |
| 3261 | if (RHSRank >= LHSRank) |
| 3262 | return -1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3263 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3264 | // If the signed type can represent all values of the unsigned type, it |
| 3265 | // wins. Because we are dealing with 2's complement and types that are |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3266 | // powers of two larger than each other, this is always safe. |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3267 | return 1; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 3268 | } |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3269 | |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3270 | static RecordDecl * |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3271 | CreateRecordDecl(const ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC, |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3272 | SourceLocation L, IdentifierInfo *Id) { |
| 3273 | if (Ctx.getLangOptions().CPlusPlus) |
| 3274 | return CXXRecordDecl::Create(Ctx, TK, DC, L, Id); |
| 3275 | else |
| 3276 | return RecordDecl::Create(Ctx, TK, DC, L, Id); |
| 3277 | } |
| 3278 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3279 | // getCFConstantStringType - Return the type used for constant CFStrings. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3280 | QualType ASTContext::getCFConstantStringType() const { |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3281 | if (!CFConstantStringTypeDecl) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3282 | CFConstantStringTypeDecl = |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3283 | CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3284 | &Idents.get("NSConstantString")); |
John McCall | ae580fe | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3285 | CFConstantStringTypeDecl->startDefinition(); |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3286 | |
Anders Carlsson | 9c1011c | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 3287 | QualType FieldTypes[4]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3288 | |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3289 | // const int *isa; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3290 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
Anders Carlsson | 9c1011c | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 3291 | // int flags; |
| 3292 | FieldTypes[1] = IntTy; |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3293 | // const char *str; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3294 | FieldTypes[2] = getPointerType(CharTy.withConst()); |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3295 | // long length; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3296 | FieldTypes[3] = LongTy; |
| 3297 | |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3298 | // Create fields |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3299 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3300 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3301 | SourceLocation(), 0, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3302 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3303 | /*BitWidth=*/0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 3304 | /*Mutable=*/false); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3305 | Field->setAccess(AS_public); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3306 | CFConstantStringTypeDecl->addDecl(Field); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3307 | } |
| 3308 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3309 | CFConstantStringTypeDecl->completeDefinition(); |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3310 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3311 | |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3312 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 412af03 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 3313 | } |
Anders Carlsson | 87c149b | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 3314 | |
Douglas Gregor | 512b077 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3315 | void ASTContext::setCFConstantStringType(QualType T) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3316 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 512b077 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3317 | assert(Rec && "Invalid CFConstantStringType"); |
| 3318 | CFConstantStringTypeDecl = Rec->getDecl(); |
| 3319 | } |
| 3320 | |
Fariborz Jahanian | e804c28 | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 3321 | // getNSConstantStringType - Return the type used for constant NSStrings. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3322 | QualType ASTContext::getNSConstantStringType() const { |
Fariborz Jahanian | e804c28 | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 3323 | if (!NSConstantStringTypeDecl) { |
| 3324 | NSConstantStringTypeDecl = |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3325 | CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Fariborz Jahanian | e804c28 | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 3326 | &Idents.get("__builtin_NSString")); |
| 3327 | NSConstantStringTypeDecl->startDefinition(); |
| 3328 | |
| 3329 | QualType FieldTypes[3]; |
| 3330 | |
| 3331 | // const int *isa; |
| 3332 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
| 3333 | // const char *str; |
| 3334 | FieldTypes[1] = getPointerType(CharTy.withConst()); |
| 3335 | // unsigned int length; |
| 3336 | FieldTypes[2] = UnsignedIntTy; |
| 3337 | |
| 3338 | // Create fields |
| 3339 | for (unsigned i = 0; i < 3; ++i) { |
| 3340 | FieldDecl *Field = FieldDecl::Create(*this, NSConstantStringTypeDecl, |
| 3341 | SourceLocation(), 0, |
| 3342 | FieldTypes[i], /*TInfo=*/0, |
| 3343 | /*BitWidth=*/0, |
| 3344 | /*Mutable=*/false); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3345 | Field->setAccess(AS_public); |
Fariborz Jahanian | e804c28 | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 3346 | NSConstantStringTypeDecl->addDecl(Field); |
| 3347 | } |
| 3348 | |
| 3349 | NSConstantStringTypeDecl->completeDefinition(); |
| 3350 | } |
| 3351 | |
| 3352 | return getTagDeclType(NSConstantStringTypeDecl); |
| 3353 | } |
| 3354 | |
| 3355 | void ASTContext::setNSConstantStringType(QualType T) { |
| 3356 | const RecordType *Rec = T->getAs<RecordType>(); |
| 3357 | assert(Rec && "Invalid NSConstantStringType"); |
| 3358 | NSConstantStringTypeDecl = Rec->getDecl(); |
| 3359 | } |
| 3360 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3361 | QualType ASTContext::getObjCFastEnumerationStateType() const { |
Anders Carlsson | d89ba7d | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3362 | if (!ObjCFastEnumerationStateTypeDecl) { |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3363 | ObjCFastEnumerationStateTypeDecl = |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3364 | CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3365 | &Idents.get("__objcFastEnumerationState")); |
John McCall | ae580fe | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3366 | ObjCFastEnumerationStateTypeDecl->startDefinition(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3367 | |
Anders Carlsson | d89ba7d | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3368 | QualType FieldTypes[] = { |
| 3369 | UnsignedLongTy, |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3370 | getPointerType(ObjCIdTypedefType), |
Anders Carlsson | d89ba7d | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3371 | getPointerType(UnsignedLongTy), |
| 3372 | getConstantArrayType(UnsignedLongTy, |
| 3373 | llvm::APInt(32, 5), ArrayType::Normal, 0) |
| 3374 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3375 | |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3376 | for (size_t i = 0; i < 4; ++i) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3377 | FieldDecl *Field = FieldDecl::Create(*this, |
| 3378 | ObjCFastEnumerationStateTypeDecl, |
| 3379 | SourceLocation(), 0, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3380 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3381 | /*BitWidth=*/0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 3382 | /*Mutable=*/false); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3383 | Field->setAccess(AS_public); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3384 | ObjCFastEnumerationStateTypeDecl->addDecl(Field); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3385 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3386 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3387 | ObjCFastEnumerationStateTypeDecl->completeDefinition(); |
Anders Carlsson | d89ba7d | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3388 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3389 | |
Anders Carlsson | d89ba7d | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3390 | return getTagDeclType(ObjCFastEnumerationStateTypeDecl); |
| 3391 | } |
| 3392 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3393 | QualType ASTContext::getBlockDescriptorType() const { |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3394 | if (BlockDescriptorType) |
| 3395 | return getTagDeclType(BlockDescriptorType); |
| 3396 | |
| 3397 | RecordDecl *T; |
| 3398 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3399 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3400 | &Idents.get("__block_descriptor")); |
John McCall | ae580fe | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3401 | T->startDefinition(); |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3402 | |
| 3403 | QualType FieldTypes[] = { |
| 3404 | UnsignedLongTy, |
| 3405 | UnsignedLongTy, |
| 3406 | }; |
| 3407 | |
| 3408 | const char *FieldNames[] = { |
| 3409 | "reserved", |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3410 | "Size" |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3411 | }; |
| 3412 | |
| 3413 | for (size_t i = 0; i < 2; ++i) { |
| 3414 | FieldDecl *Field = FieldDecl::Create(*this, |
| 3415 | T, |
| 3416 | SourceLocation(), |
| 3417 | &Idents.get(FieldNames[i]), |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3418 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3419 | /*BitWidth=*/0, |
| 3420 | /*Mutable=*/false); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3421 | Field->setAccess(AS_public); |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3422 | T->addDecl(Field); |
| 3423 | } |
| 3424 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3425 | T->completeDefinition(); |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3426 | |
| 3427 | BlockDescriptorType = T; |
| 3428 | |
| 3429 | return getTagDeclType(BlockDescriptorType); |
| 3430 | } |
| 3431 | |
| 3432 | void ASTContext::setBlockDescriptorType(QualType T) { |
| 3433 | const RecordType *Rec = T->getAs<RecordType>(); |
| 3434 | assert(Rec && "Invalid BlockDescriptorType"); |
| 3435 | BlockDescriptorType = Rec->getDecl(); |
| 3436 | } |
| 3437 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3438 | QualType ASTContext::getBlockDescriptorExtendedType() const { |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3439 | if (BlockDescriptorExtendedType) |
| 3440 | return getTagDeclType(BlockDescriptorExtendedType); |
| 3441 | |
| 3442 | RecordDecl *T; |
| 3443 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3444 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3445 | &Idents.get("__block_descriptor_withcopydispose")); |
John McCall | ae580fe | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3446 | T->startDefinition(); |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3447 | |
| 3448 | QualType FieldTypes[] = { |
| 3449 | UnsignedLongTy, |
| 3450 | UnsignedLongTy, |
| 3451 | getPointerType(VoidPtrTy), |
| 3452 | getPointerType(VoidPtrTy) |
| 3453 | }; |
| 3454 | |
| 3455 | const char *FieldNames[] = { |
| 3456 | "reserved", |
| 3457 | "Size", |
| 3458 | "CopyFuncPtr", |
| 3459 | "DestroyFuncPtr" |
| 3460 | }; |
| 3461 | |
| 3462 | for (size_t i = 0; i < 4; ++i) { |
| 3463 | FieldDecl *Field = FieldDecl::Create(*this, |
| 3464 | T, |
| 3465 | SourceLocation(), |
| 3466 | &Idents.get(FieldNames[i]), |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3467 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3468 | /*BitWidth=*/0, |
| 3469 | /*Mutable=*/false); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3470 | Field->setAccess(AS_public); |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3471 | T->addDecl(Field); |
| 3472 | } |
| 3473 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3474 | T->completeDefinition(); |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3475 | |
| 3476 | BlockDescriptorExtendedType = T; |
| 3477 | |
| 3478 | return getTagDeclType(BlockDescriptorExtendedType); |
| 3479 | } |
| 3480 | |
| 3481 | void ASTContext::setBlockDescriptorExtendedType(QualType T) { |
| 3482 | const RecordType *Rec = T->getAs<RecordType>(); |
| 3483 | assert(Rec && "Invalid BlockDescriptorType"); |
| 3484 | BlockDescriptorExtendedType = Rec->getDecl(); |
| 3485 | } |
| 3486 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3487 | bool ASTContext::BlockRequiresCopying(QualType Ty) const { |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3488 | if (Ty->isBlockPointerType()) |
| 3489 | return true; |
| 3490 | if (isObjCNSObjectType(Ty)) |
| 3491 | return true; |
| 3492 | if (Ty->isObjCObjectPointerType()) |
| 3493 | return true; |
Fariborz Jahanian | a00076c | 2010-11-17 00:21:28 +0000 | [diff] [blame] | 3494 | if (getLangOptions().CPlusPlus) { |
| 3495 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3496 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 3497 | return RD->hasConstCopyConstructor(*this); |
| 3498 | |
| 3499 | } |
| 3500 | } |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3501 | return false; |
| 3502 | } |
| 3503 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3504 | QualType |
| 3505 | ASTContext::BuildByRefType(llvm::StringRef DeclName, QualType Ty) const { |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3506 | // type = struct __Block_byref_1_X { |
Mike Stump | 7fe9cc1 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3507 | // void *__isa; |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3508 | // struct __Block_byref_1_X *__forwarding; |
Mike Stump | 7fe9cc1 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3509 | // unsigned int __flags; |
| 3510 | // unsigned int __size; |
Eli Friedman | 0483192 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 3511 | // void *__copy_helper; // as needed |
| 3512 | // void *__destroy_help // as needed |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3513 | // int X; |
Mike Stump | 7fe9cc1 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3514 | // } * |
| 3515 | |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3516 | bool HasCopyAndDispose = BlockRequiresCopying(Ty); |
| 3517 | |
| 3518 | // FIXME: Move up |
Benjamin Kramer | 1402ce3 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 3519 | llvm::SmallString<36> Name; |
| 3520 | llvm::raw_svector_ostream(Name) << "__Block_byref_" << |
| 3521 | ++UniqueBlockByRefTypeID << '_' << DeclName; |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3522 | RecordDecl *T; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3523 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3524 | &Idents.get(Name.str())); |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3525 | T->startDefinition(); |
| 3526 | QualType Int32Ty = IntTy; |
| 3527 | assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported"); |
| 3528 | QualType FieldTypes[] = { |
| 3529 | getPointerType(VoidPtrTy), |
| 3530 | getPointerType(getTagDeclType(T)), |
| 3531 | Int32Ty, |
| 3532 | Int32Ty, |
| 3533 | getPointerType(VoidPtrTy), |
| 3534 | getPointerType(VoidPtrTy), |
| 3535 | Ty |
| 3536 | }; |
| 3537 | |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3538 | llvm::StringRef FieldNames[] = { |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3539 | "__isa", |
| 3540 | "__forwarding", |
| 3541 | "__flags", |
| 3542 | "__size", |
| 3543 | "__copy_helper", |
| 3544 | "__destroy_helper", |
| 3545 | DeclName, |
| 3546 | }; |
| 3547 | |
| 3548 | for (size_t i = 0; i < 7; ++i) { |
| 3549 | if (!HasCopyAndDispose && i >=4 && i <= 5) |
| 3550 | continue; |
| 3551 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
| 3552 | &Idents.get(FieldNames[i]), |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3553 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3554 | /*BitWidth=*/0, /*Mutable=*/false); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3555 | Field->setAccess(AS_public); |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3556 | T->addDecl(Field); |
| 3557 | } |
| 3558 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3559 | T->completeDefinition(); |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3560 | |
| 3561 | return getPointerType(getTagDeclType(T)); |
Mike Stump | 7fe9cc1 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3562 | } |
| 3563 | |
| 3564 | |
| 3565 | QualType ASTContext::getBlockParmType( |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3566 | bool BlockHasCopyDispose, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3567 | llvm::SmallVectorImpl<const Expr *> &Layout) const { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 3568 | |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3569 | // FIXME: Move up |
Benjamin Kramer | 1402ce3 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 3570 | llvm::SmallString<36> Name; |
| 3571 | llvm::raw_svector_ostream(Name) << "__block_literal_" |
| 3572 | << ++UniqueBlockParmTypeID; |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3573 | RecordDecl *T; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3574 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3575 | &Idents.get(Name.str())); |
John McCall | ae580fe | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3576 | T->startDefinition(); |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3577 | QualType FieldTypes[] = { |
| 3578 | getPointerType(VoidPtrTy), |
| 3579 | IntTy, |
| 3580 | IntTy, |
| 3581 | getPointerType(VoidPtrTy), |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3582 | (BlockHasCopyDispose ? |
| 3583 | getPointerType(getBlockDescriptorExtendedType()) : |
| 3584 | getPointerType(getBlockDescriptorType())) |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3585 | }; |
| 3586 | |
| 3587 | const char *FieldNames[] = { |
| 3588 | "__isa", |
| 3589 | "__flags", |
| 3590 | "__reserved", |
| 3591 | "__FuncPtr", |
| 3592 | "__descriptor" |
| 3593 | }; |
| 3594 | |
| 3595 | for (size_t i = 0; i < 5; ++i) { |
Mike Stump | 7fe9cc1 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3596 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3597 | &Idents.get(FieldNames[i]), |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3598 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 7fe9cc1 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3599 | /*BitWidth=*/0, /*Mutable=*/false); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3600 | Field->setAccess(AS_public); |
Mike Stump | 7fe9cc1 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3601 | T->addDecl(Field); |
| 3602 | } |
| 3603 | |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 3604 | for (unsigned i = 0; i < Layout.size(); ++i) { |
| 3605 | const Expr *E = Layout[i]; |
Mike Stump | 7fe9cc1 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3606 | |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 3607 | QualType FieldType = E->getType(); |
| 3608 | IdentifierInfo *FieldName = 0; |
| 3609 | if (isa<CXXThisExpr>(E)) { |
| 3610 | FieldName = &Idents.get("this"); |
| 3611 | } else if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E)) { |
| 3612 | const ValueDecl *D = BDRE->getDecl(); |
| 3613 | FieldName = D->getIdentifier(); |
| 3614 | if (BDRE->isByRef()) |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3615 | FieldType = BuildByRefType(D->getName(), FieldType); |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 3616 | } else { |
| 3617 | // Padding. |
| 3618 | assert(isa<ConstantArrayType>(FieldType) && |
| 3619 | isa<DeclRefExpr>(E) && |
| 3620 | !cast<DeclRefExpr>(E)->getDecl()->getDeclName() && |
| 3621 | "doesn't match characteristics of padding decl"); |
| 3622 | } |
Mike Stump | 7fe9cc1 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3623 | |
| 3624 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 3625 | FieldName, FieldType, /*TInfo=*/0, |
Mike Stump | 7fe9cc1 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3626 | /*BitWidth=*/0, /*Mutable=*/false); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3627 | Field->setAccess(AS_public); |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3628 | T->addDecl(Field); |
| 3629 | } |
| 3630 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3631 | T->completeDefinition(); |
Mike Stump | 7fe9cc1 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3632 | |
| 3633 | return getPointerType(getTagDeclType(T)); |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3634 | } |
| 3635 | |
Douglas Gregor | 512b077 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3636 | void ASTContext::setObjCFastEnumerationStateType(QualType T) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3637 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 512b077 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3638 | assert(Rec && "Invalid ObjCFAstEnumerationStateType"); |
| 3639 | ObjCFastEnumerationStateTypeDecl = Rec->getDecl(); |
| 3640 | } |
| 3641 | |
Anders Carlsson | 18acd44 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3642 | // This returns true if a type has been typedefed to BOOL: |
| 3643 | // typedef <type> BOOL; |
Chris Lattner | e021899 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 3644 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | 18acd44 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3645 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | 9b1f279 | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 3646 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 3647 | return II->isStr("BOOL"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3648 | |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3649 | return false; |
| 3650 | } |
| 3651 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3652 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3653 | /// purpose. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3654 | CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const { |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3655 | CharUnits sz = getTypeSizeInChars(type); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3656 | |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3657 | // Make all integer and enum types at least as large as an int |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3658 | if (sz.isPositive() && type->isIntegralOrEnumerationType()) |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3659 | sz = std::max(sz, getTypeSizeInChars(IntTy)); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3660 | // Treat arrays as pointers, since that's how they're passed in. |
| 3661 | else if (type->isArrayType()) |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3662 | sz = getTypeSizeInChars(VoidPtrTy); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3663 | return sz; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3664 | } |
| 3665 | |
| 3666 | static inline |
| 3667 | std::string charUnitsToString(const CharUnits &CU) { |
| 3668 | return llvm::itostr(CU.getQuantity()); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3669 | } |
| 3670 | |
Fariborz Jahanian | 590c352 | 2010-04-08 18:06:22 +0000 | [diff] [blame] | 3671 | /// getObjCEncodingForBlockDecl - Return the encoded type for this block |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3672 | /// declaration. |
| 3673 | void ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3674 | std::string& S) const { |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3675 | const BlockDecl *Decl = Expr->getBlockDecl(); |
| 3676 | QualType BlockTy = |
| 3677 | Expr->getType()->getAs<BlockPointerType>()->getPointeeType(); |
| 3678 | // Encode result type. |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 3679 | getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3680 | // Compute size of all parameters. |
| 3681 | // Start with computing size of a pointer in number of bytes. |
| 3682 | // FIXME: There might(should) be a better way of doing this computation! |
| 3683 | SourceLocation Loc; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3684 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
| 3685 | CharUnits ParmOffset = PtrSize; |
Fariborz Jahanian | 590c352 | 2010-04-08 18:06:22 +0000 | [diff] [blame] | 3686 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3687 | E = Decl->param_end(); PI != E; ++PI) { |
| 3688 | QualType PType = (*PI)->getType(); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3689 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3690 | assert (sz.isPositive() && "BlockExpr - Incomplete param type"); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3691 | ParmOffset += sz; |
| 3692 | } |
| 3693 | // Size of the argument frame |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3694 | S += charUnitsToString(ParmOffset); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3695 | // Block pointer and offset. |
| 3696 | S += "@?0"; |
| 3697 | ParmOffset = PtrSize; |
| 3698 | |
| 3699 | // Argument types. |
| 3700 | ParmOffset = PtrSize; |
| 3701 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E = |
| 3702 | Decl->param_end(); PI != E; ++PI) { |
| 3703 | ParmVarDecl *PVDecl = *PI; |
| 3704 | QualType PType = PVDecl->getOriginalType(); |
| 3705 | if (const ArrayType *AT = |
| 3706 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3707 | // Use array's original type only if it has known number of |
| 3708 | // elements. |
| 3709 | if (!isa<ConstantArrayType>(AT)) |
| 3710 | PType = PVDecl->getType(); |
| 3711 | } else if (PType->isFunctionType()) |
| 3712 | PType = PVDecl->getType(); |
| 3713 | getObjCEncodingForType(PType, S); |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3714 | S += charUnitsToString(ParmOffset); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3715 | ParmOffset += getObjCEncodingTypeSize(PType); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3716 | } |
| 3717 | } |
| 3718 | |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 3719 | void ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl, |
| 3720 | std::string& S) { |
| 3721 | // Encode result type. |
| 3722 | getObjCEncodingForType(Decl->getResultType(), S); |
| 3723 | CharUnits ParmOffset; |
| 3724 | // Compute size of all parameters. |
| 3725 | for (FunctionDecl::param_const_iterator PI = Decl->param_begin(), |
| 3726 | E = Decl->param_end(); PI != E; ++PI) { |
| 3727 | QualType PType = (*PI)->getType(); |
| 3728 | CharUnits sz = getObjCEncodingTypeSize(PType); |
| 3729 | assert (sz.isPositive() && |
| 3730 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
| 3731 | ParmOffset += sz; |
| 3732 | } |
| 3733 | S += charUnitsToString(ParmOffset); |
| 3734 | ParmOffset = CharUnits::Zero(); |
| 3735 | |
| 3736 | // Argument types. |
| 3737 | for (FunctionDecl::param_const_iterator PI = Decl->param_begin(), |
| 3738 | E = Decl->param_end(); PI != E; ++PI) { |
| 3739 | ParmVarDecl *PVDecl = *PI; |
| 3740 | QualType PType = PVDecl->getOriginalType(); |
| 3741 | if (const ArrayType *AT = |
| 3742 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3743 | // Use array's original type only if it has known number of |
| 3744 | // elements. |
| 3745 | if (!isa<ConstantArrayType>(AT)) |
| 3746 | PType = PVDecl->getType(); |
| 3747 | } else if (PType->isFunctionType()) |
| 3748 | PType = PVDecl->getType(); |
| 3749 | getObjCEncodingForType(PType, S); |
| 3750 | S += charUnitsToString(ParmOffset); |
| 3751 | ParmOffset += getObjCEncodingTypeSize(PType); |
| 3752 | } |
| 3753 | } |
| 3754 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3755 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3756 | /// declaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3757 | void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3758 | std::string& S) const { |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3759 | // FIXME: This is not very efficient. |
Fariborz Jahanian | ac73ff8 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3760 | // Encode type qualifer, 'in', 'inout', etc. for the return type. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3761 | getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3762 | // Encode result type. |
Daniel Dunbar | fc1066d | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3763 | getObjCEncodingForType(Decl->getResultType(), S); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3764 | // Compute size of all parameters. |
| 3765 | // Start with computing size of a pointer in number of bytes. |
| 3766 | // FIXME: There might(should) be a better way of doing this computation! |
| 3767 | SourceLocation Loc; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3768 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3769 | // The first two arguments (self and _cmd) are pointers; account for |
| 3770 | // their size. |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3771 | CharUnits ParmOffset = 2 * PtrSize; |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3772 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | d9235db | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 3773 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3774 | QualType PType = (*PI)->getType(); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3775 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3776 | assert (sz.isPositive() && |
| 3777 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3778 | ParmOffset += sz; |
| 3779 | } |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3780 | S += charUnitsToString(ParmOffset); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3781 | S += "@0:"; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3782 | S += charUnitsToString(PtrSize); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3783 | |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3784 | // Argument types. |
| 3785 | ParmOffset = 2 * PtrSize; |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3786 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | d9235db | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 3787 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3788 | ParmVarDecl *PVDecl = *PI; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3789 | QualType PType = PVDecl->getOriginalType(); |
Fariborz Jahanian | a0befc0 | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 3790 | if (const ArrayType *AT = |
Steve Naroff | e4e55d2 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 3791 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3792 | // Use array's original type only if it has known number of |
| 3793 | // elements. |
Steve Naroff | 323827e | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 3794 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | e4e55d2 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 3795 | PType = PVDecl->getType(); |
| 3796 | } else if (PType->isFunctionType()) |
| 3797 | PType = PVDecl->getType(); |
Fariborz Jahanian | ac73ff8 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3798 | // Process argument qualifiers for user supplied arguments; such as, |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3799 | // 'in', 'inout', etc. |
Fariborz Jahanian | a0befc0 | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 3800 | getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S); |
Daniel Dunbar | fc1066d | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3801 | getObjCEncodingForType(PType, S); |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3802 | S += charUnitsToString(ParmOffset); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3803 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3804 | } |
| 3805 | } |
| 3806 | |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3807 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 2f85a64 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 3808 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3809 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 3810 | /// NULL when getting encodings for protocol properties. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3811 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 3812 | /// attributes readonly and bycopy are encoded as single characters. The |
| 3813 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 3814 | /// encoded as single characters, followed by an identifier. Property types |
| 3815 | /// are also encoded as a parametrized attribute. The characters used to encode |
Fariborz Jahanian | 2f85a64 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 3816 | /// these attributes are defined by the following enumeration: |
| 3817 | /// @code |
| 3818 | /// enum PropertyAttributes { |
| 3819 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 3820 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 3821 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 3822 | /// kPropertyDynamic = 'D', // property is dynamic |
| 3823 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 3824 | /// kPropertySetter = 'S', // followed by setter selector name |
| 3825 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
| 3826 | /// kPropertyType = 't' // followed by old-style type encoding. |
| 3827 | /// kPropertyWeak = 'W' // 'weak' property |
| 3828 | /// kPropertyStrong = 'P' // property GC'able |
| 3829 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 3830 | /// }; |
| 3831 | /// @endcode |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3832 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3833 | const Decl *Container, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3834 | std::string& S) const { |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3835 | // Collect information from the property implementation decl(s). |
| 3836 | bool Dynamic = false; |
| 3837 | ObjCPropertyImplDecl *SynthesizePID = 0; |
| 3838 | |
| 3839 | // FIXME: Duplicated code due to poor abstraction. |
| 3840 | if (Container) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3841 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3842 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
| 3843 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3844 | i = CID->propimpl_begin(), e = CID->propimpl_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3845 | i != e; ++i) { |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3846 | ObjCPropertyImplDecl *PID = *i; |
| 3847 | if (PID->getPropertyDecl() == PD) { |
| 3848 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 3849 | Dynamic = true; |
| 3850 | } else { |
| 3851 | SynthesizePID = PID; |
| 3852 | } |
| 3853 | } |
| 3854 | } |
| 3855 | } else { |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3856 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3857 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3858 | i = OID->propimpl_begin(), e = OID->propimpl_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3859 | i != e; ++i) { |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3860 | ObjCPropertyImplDecl *PID = *i; |
| 3861 | if (PID->getPropertyDecl() == PD) { |
| 3862 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 3863 | Dynamic = true; |
| 3864 | } else { |
| 3865 | SynthesizePID = PID; |
| 3866 | } |
| 3867 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3868 | } |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3869 | } |
| 3870 | } |
| 3871 | |
| 3872 | // FIXME: This is not very efficient. |
| 3873 | S = "T"; |
| 3874 | |
| 3875 | // Encode result type. |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3876 | // GCC has some special rules regarding encoding of properties which |
| 3877 | // closely resembles encoding of ivars. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3878 | getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0, |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3879 | true /* outermost type */, |
| 3880 | true /* encoding for property */); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3881 | |
| 3882 | if (PD->isReadOnly()) { |
| 3883 | S += ",R"; |
| 3884 | } else { |
| 3885 | switch (PD->getSetterKind()) { |
| 3886 | case ObjCPropertyDecl::Assign: break; |
| 3887 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3888 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3889 | } |
| 3890 | } |
| 3891 | |
| 3892 | // It really isn't clear at all what this means, since properties |
| 3893 | // are "dynamic by default". |
| 3894 | if (Dynamic) |
| 3895 | S += ",D"; |
| 3896 | |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3897 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 3898 | S += ",N"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3899 | |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3900 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 3901 | S += ",G"; |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3902 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3903 | } |
| 3904 | |
| 3905 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 3906 | S += ",S"; |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3907 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3908 | } |
| 3909 | |
| 3910 | if (SynthesizePID) { |
| 3911 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 3912 | S += ",V"; |
Chris Lattner | 1cbaacc | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 3913 | S += OID->getNameAsString(); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3914 | } |
| 3915 | |
| 3916 | // FIXME: OBJCGC: weak & strong |
| 3917 | } |
| 3918 | |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3919 | /// getLegacyIntegralTypeEncoding - |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3920 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
| 3921 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3922 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
| 3923 | /// |
| 3924 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 212005c | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3925 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3926 | if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) { |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3927 | if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3928 | PointeeTy = UnsignedIntTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3929 | else |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3930 | if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3931 | PointeeTy = IntTy; |
| 3932 | } |
| 3933 | } |
| 3934 | } |
| 3935 | |
Fariborz Jahanian | 0a71ad2 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3936 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3937 | const FieldDecl *Field) const { |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3938 | // We follow the behavior of gcc, expanding structures which are |
| 3939 | // directly pointed to, and expanding embedded structures. Note that |
| 3940 | // these rules are sufficient to prevent recursive encoding of the |
| 3941 | // same type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3942 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
Fariborz Jahanian | daef00b | 2008-12-22 23:22:27 +0000 | [diff] [blame] | 3943 | true /* outermost type */); |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3944 | } |
| 3945 | |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3946 | static char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) { |
| 3947 | switch (T->getAs<BuiltinType>()->getKind()) { |
| 3948 | default: assert(0 && "Unhandled builtin type kind"); |
| 3949 | case BuiltinType::Void: return 'v'; |
| 3950 | case BuiltinType::Bool: return 'B'; |
| 3951 | case BuiltinType::Char_U: |
| 3952 | case BuiltinType::UChar: return 'C'; |
| 3953 | case BuiltinType::UShort: return 'S'; |
| 3954 | case BuiltinType::UInt: return 'I'; |
| 3955 | case BuiltinType::ULong: |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3956 | return C->getIntWidth(T) == 32 ? 'L' : 'Q'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3957 | case BuiltinType::UInt128: return 'T'; |
| 3958 | case BuiltinType::ULongLong: return 'Q'; |
| 3959 | case BuiltinType::Char_S: |
| 3960 | case BuiltinType::SChar: return 'c'; |
| 3961 | case BuiltinType::Short: return 's'; |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 3962 | case BuiltinType::WChar_S: |
| 3963 | case BuiltinType::WChar_U: |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3964 | case BuiltinType::Int: return 'i'; |
| 3965 | case BuiltinType::Long: |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3966 | return C->getIntWidth(T) == 32 ? 'l' : 'q'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3967 | case BuiltinType::LongLong: return 'q'; |
| 3968 | case BuiltinType::Int128: return 't'; |
| 3969 | case BuiltinType::Float: return 'f'; |
| 3970 | case BuiltinType::Double: return 'd'; |
Daniel Dunbar | 7cba5a7 | 2010-10-11 21:13:48 +0000 | [diff] [blame] | 3971 | case BuiltinType::LongDouble: return 'D'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3972 | } |
| 3973 | } |
| 3974 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3975 | static void EncodeBitField(const ASTContext *Ctx, std::string& S, |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3976 | QualType T, const FieldDecl *FD) { |
Fariborz Jahanian | 5c76772 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3977 | const Expr *E = FD->getBitWidth(); |
| 3978 | assert(E && "bitfield width not there - getObjCEncodingForTypeImpl"); |
Fariborz Jahanian | 5c76772 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3979 | S += 'b'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3980 | // The NeXT runtime encodes bit fields as b followed by the number of bits. |
| 3981 | // The GNU runtime requires more information; bitfields are encoded as b, |
| 3982 | // then the offset (in bits) of the first element, then the type of the |
| 3983 | // bitfield, then the size in bits. For example, in this structure: |
| 3984 | // |
| 3985 | // struct |
| 3986 | // { |
| 3987 | // int integer; |
| 3988 | // int flags:2; |
| 3989 | // }; |
| 3990 | // On a 32-bit system, the encoding for flags would be b2 for the NeXT |
| 3991 | // runtime, but b32i2 for the GNU runtime. The reason for this extra |
| 3992 | // information is not especially sensible, but we're stuck with it for |
| 3993 | // compatibility with GCC, although providing it breaks anything that |
| 3994 | // actually uses runtime introspection and wants to work on both runtimes... |
| 3995 | if (!Ctx->getLangOptions().NeXTRuntime) { |
| 3996 | const RecordDecl *RD = FD->getParent(); |
| 3997 | const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD); |
| 3998 | // FIXME: This same linear search is also used in ExprConstant - it might |
| 3999 | // be better if the FieldDecl stored its offset. We'd be increasing the |
| 4000 | // size of the object slightly, but saving some time every time it is used. |
| 4001 | unsigned i = 0; |
| 4002 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 4003 | FieldEnd = RD->field_end(); |
| 4004 | Field != FieldEnd; (void)++Field, ++i) { |
| 4005 | if (*Field == FD) |
| 4006 | break; |
| 4007 | } |
| 4008 | S += llvm::utostr(RL.getFieldOffset(i)); |
David Chisnall | 6f0a7d2 | 2010-12-26 20:12:30 +0000 | [diff] [blame] | 4009 | if (T->isEnumeralType()) |
| 4010 | S += 'i'; |
| 4011 | else |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4012 | S += ObjCEncodingForPrimitiveKind(Ctx, T); |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4013 | } |
| 4014 | unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue(); |
Fariborz Jahanian | 5c76772 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4015 | S += llvm::utostr(N); |
| 4016 | } |
| 4017 | |
Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 4018 | // FIXME: Use SmallString for accumulating string. |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4019 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 4020 | bool ExpandPointedToStructures, |
| 4021 | bool ExpandStructures, |
Daniel Dunbar | c040ce4 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 4022 | const FieldDecl *FD, |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4023 | bool OutermostType, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4024 | bool EncodingProperty) const { |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4025 | if (T->getAs<BuiltinType>()) { |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4026 | if (FD && FD->isBitField()) |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4027 | return EncodeBitField(this, S, T, FD); |
| 4028 | S += ObjCEncodingForPrimitiveKind(this, T); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4029 | return; |
| 4030 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4031 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4032 | if (const ComplexType *CT = T->getAs<ComplexType>()) { |
Anders Carlsson | 39b2e13 | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 4033 | S += 'j'; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4034 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false, |
Anders Carlsson | 39b2e13 | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 4035 | false); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4036 | return; |
| 4037 | } |
Fariborz Jahanian | d25c219 | 2009-11-23 20:40:50 +0000 | [diff] [blame] | 4038 | |
Fariborz Jahanian | 9ffd706 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4039 | // encoding for pointer or r3eference types. |
| 4040 | QualType PointeeTy; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4041 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Fariborz Jahanian | 89b660c | 2009-11-30 18:43:52 +0000 | [diff] [blame] | 4042 | if (PT->isObjCSelType()) { |
| 4043 | S += ':'; |
| 4044 | return; |
| 4045 | } |
Fariborz Jahanian | 9ffd706 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4046 | PointeeTy = PT->getPointeeType(); |
| 4047 | } |
| 4048 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 4049 | PointeeTy = RT->getPointeeType(); |
| 4050 | if (!PointeeTy.isNull()) { |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4051 | bool isReadOnly = false; |
| 4052 | // For historical/compatibility reasons, the read-only qualifier of the |
| 4053 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 4054 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4055 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 212005c | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 4056 | if (isa<TypedefType>(T.getTypePtr())) { |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4057 | if (OutermostType && T.isConstQualified()) { |
| 4058 | isReadOnly = true; |
| 4059 | S += 'r'; |
| 4060 | } |
Mike Stump | e9c6ffc | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 4061 | } else if (OutermostType) { |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4062 | QualType P = PointeeTy; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4063 | while (P->getAs<PointerType>()) |
| 4064 | P = P->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4065 | if (P.isConstQualified()) { |
| 4066 | isReadOnly = true; |
| 4067 | S += 'r'; |
| 4068 | } |
| 4069 | } |
| 4070 | if (isReadOnly) { |
| 4071 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 4072 | // combinations need to be rearranged. |
| 4073 | // Rewrite "in const" from "nr" to "rn" |
Benjamin Kramer | 2e3197e | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 4074 | if (llvm::StringRef(S).endswith("nr")) |
| 4075 | S.replace(S.end()-2, S.end(), "rn"); |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4076 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4077 | |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4078 | if (PointeeTy->isCharType()) { |
| 4079 | // char pointer types should be encoded as '*' unless it is a |
| 4080 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | 18acd44 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 4081 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4082 | S += '*'; |
| 4083 | return; |
| 4084 | } |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4085 | } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) { |
Steve Naroff | 3de6b70 | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 4086 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 4087 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 4088 | S += '#'; |
| 4089 | return; |
| 4090 | } |
| 4091 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 4092 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 4093 | S += '@'; |
| 4094 | return; |
| 4095 | } |
| 4096 | // fall through... |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4097 | } |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4098 | S += '^'; |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4099 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 4100 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4101 | getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4102 | NULL); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4103 | return; |
| 4104 | } |
Fariborz Jahanian | 9ffd706 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4105 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4106 | if (const ArrayType *AT = |
| 4107 | // Ignore type qualifiers etc. |
| 4108 | dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) { |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4109 | if (isa<IncompleteArrayType>(AT)) { |
| 4110 | // Incomplete arrays are encoded as a pointer to the array element. |
| 4111 | S += '^'; |
| 4112 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4113 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4114 | false, ExpandStructures, FD); |
| 4115 | } else { |
| 4116 | S += '['; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4117 | |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4118 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 4119 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 4120 | else { |
| 4121 | //Variable length arrays are encoded as a regular array with 0 elements. |
| 4122 | assert(isa<VariableArrayType>(AT) && "Unknown array type!"); |
| 4123 | S += '0'; |
| 4124 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4125 | |
| 4126 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4127 | false, ExpandStructures, FD); |
| 4128 | S += ']'; |
| 4129 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4130 | return; |
| 4131 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4132 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4133 | if (T->getAs<FunctionType>()) { |
Anders Carlsson | df4cc61 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 4134 | S += '?'; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4135 | return; |
| 4136 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4137 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4138 | if (const RecordType *RTy = T->getAs<RecordType>()) { |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4139 | RecordDecl *RDecl = RTy->getDecl(); |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4140 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 40cac77 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 4141 | // Anonymous structures print as '?' |
| 4142 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 4143 | S += II->getName(); |
Fariborz Jahanian | c515820 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 4144 | if (ClassTemplateSpecializationDecl *Spec |
| 4145 | = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) { |
| 4146 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 4147 | std::string TemplateArgsStr |
| 4148 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4149 | TemplateArgs.data(), |
| 4150 | TemplateArgs.size(), |
Fariborz Jahanian | c515820 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 4151 | (*this).PrintingPolicy); |
| 4152 | |
| 4153 | S += TemplateArgsStr; |
| 4154 | } |
Daniel Dunbar | 40cac77 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 4155 | } else { |
| 4156 | S += '?'; |
| 4157 | } |
Daniel Dunbar | fc1066d | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 4158 | if (ExpandStructures) { |
Fariborz Jahanian | 0a71ad2 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4159 | S += '='; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4160 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 4161 | FieldEnd = RDecl->field_end(); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4162 | Field != FieldEnd; ++Field) { |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4163 | if (FD) { |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4164 | S += '"'; |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4165 | S += Field->getNameAsString(); |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4166 | S += '"'; |
| 4167 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4168 | |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4169 | // Special case bit-fields. |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4170 | if (Field->isBitField()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4171 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4172 | (*Field)); |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4173 | } else { |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4174 | QualType qt = Field->getType(); |
| 4175 | getLegacyIntegralTypeEncoding(qt); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4176 | getObjCEncodingForTypeImpl(qt, S, false, true, |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4177 | FD); |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4178 | } |
Fariborz Jahanian | 0a71ad2 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4179 | } |
Fariborz Jahanian | bc92fd7 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 4180 | } |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4181 | S += RDecl->isUnion() ? ')' : '}'; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4182 | return; |
| 4183 | } |
Fariborz Jahanian | e0587be | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 4184 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4185 | if (T->isEnumeralType()) { |
Fariborz Jahanian | 5c76772 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4186 | if (FD && FD->isBitField()) |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4187 | EncodeBitField(this, S, T, FD); |
Fariborz Jahanian | 5c76772 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4188 | else |
| 4189 | S += 'i'; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4190 | return; |
| 4191 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4192 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4193 | if (T->isBlockPointerType()) { |
Steve Naroff | 49140cb | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 4194 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4195 | return; |
| 4196 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4197 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4198 | // Ignore protocol qualifiers when mangling at this level. |
| 4199 | if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>()) |
| 4200 | T = OT->getBaseType(); |
| 4201 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4202 | if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) { |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4203 | // @encode(class_name) |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4204 | ObjCInterfaceDecl *OI = OIT->getDecl(); |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4205 | S += '{'; |
| 4206 | const IdentifierInfo *II = OI->getIdentifier(); |
| 4207 | S += II->getName(); |
| 4208 | S += '='; |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4209 | llvm::SmallVector<ObjCIvarDecl*, 32> Ivars; |
| 4210 | DeepCollectObjCIvars(OI, true, Ivars); |
| 4211 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) { |
| 4212 | FieldDecl *Field = cast<FieldDecl>(Ivars[i]); |
| 4213 | if (Field->isBitField()) |
| 4214 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field); |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4215 | else |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4216 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD); |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4217 | } |
| 4218 | S += '}'; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4219 | return; |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4220 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4221 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4222 | if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4223 | if (OPT->isObjCIdType()) { |
| 4224 | S += '@'; |
| 4225 | return; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4226 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4227 | |
Steve Naroff | f0c8611 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4228 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
| 4229 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
| 4230 | // Since this is a binary compatibility issue, need to consult with runtime |
| 4231 | // folks. Fortunately, this is a *very* obsure construct. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4232 | S += '#'; |
| 4233 | return; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4234 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4235 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4236 | if (OPT->isObjCQualifiedIdType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4237 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4238 | ExpandPointedToStructures, |
| 4239 | ExpandStructures, FD); |
| 4240 | if (FD || EncodingProperty) { |
| 4241 | // Note that we do extended encoding of protocol qualifer list |
| 4242 | // Only when doing ivar or property encoding. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4243 | S += '"'; |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4244 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 4245 | E = OPT->qual_end(); I != E; ++I) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4246 | S += '<'; |
| 4247 | S += (*I)->getNameAsString(); |
| 4248 | S += '>'; |
| 4249 | } |
| 4250 | S += '"'; |
| 4251 | } |
| 4252 | return; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4253 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4254 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4255 | QualType PointeeTy = OPT->getPointeeType(); |
| 4256 | if (!EncodingProperty && |
| 4257 | isa<TypedefType>(PointeeTy.getTypePtr())) { |
| 4258 | // Another historical/compatibility reason. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4259 | // We encode the underlying type which comes out as |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4260 | // {...}; |
| 4261 | S += '^'; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4262 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 4263 | false, ExpandPointedToStructures, |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4264 | NULL); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4265 | return; |
| 4266 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4267 | |
| 4268 | S += '@'; |
Steve Naroff | f0c8611 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4269 | if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) { |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4270 | S += '"'; |
Steve Naroff | f0c8611 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4271 | S += OPT->getInterfaceDecl()->getIdentifier()->getName(); |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4272 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 4273 | E = OPT->qual_end(); I != E; ++I) { |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4274 | S += '<'; |
| 4275 | S += (*I)->getNameAsString(); |
| 4276 | S += '>'; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4277 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4278 | S += '"'; |
| 4279 | } |
| 4280 | return; |
| 4281 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4282 | |
John McCall | a9e6e8d | 2010-05-17 23:56:34 +0000 | [diff] [blame] | 4283 | // gcc just blithely ignores member pointers. |
| 4284 | // TODO: maybe there should be a mangling for these |
| 4285 | if (T->getAs<MemberPointerType>()) |
| 4286 | return; |
Fariborz Jahanian | e0587be | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 4287 | |
| 4288 | if (T->isVectorType()) { |
| 4289 | // This matches gcc's encoding, even though technically it is |
| 4290 | // insufficient. |
| 4291 | // FIXME. We should do a better job than gcc. |
| 4292 | return; |
| 4293 | } |
| 4294 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4295 | assert(0 && "@encode for type not implemented!"); |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4296 | } |
| 4297 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4298 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ac73ff8 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 4299 | std::string& S) const { |
| 4300 | if (QT & Decl::OBJC_TQ_In) |
| 4301 | S += 'n'; |
| 4302 | if (QT & Decl::OBJC_TQ_Inout) |
| 4303 | S += 'N'; |
| 4304 | if (QT & Decl::OBJC_TQ_Out) |
| 4305 | S += 'o'; |
| 4306 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 4307 | S += 'O'; |
| 4308 | if (QT & Decl::OBJC_TQ_Byref) |
| 4309 | S += 'R'; |
| 4310 | if (QT & Decl::OBJC_TQ_Oneway) |
| 4311 | S += 'V'; |
| 4312 | } |
| 4313 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4314 | void ASTContext::setBuiltinVaListType(QualType T) { |
Anders Carlsson | 87c149b | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 4315 | assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4316 | |
Anders Carlsson | 87c149b | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 4317 | BuiltinVaListType = T; |
| 4318 | } |
| 4319 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4320 | void ASTContext::setObjCIdType(QualType T) { |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 4321 | ObjCIdTypedefType = T; |
Steve Naroff | 66e9f33 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 4322 | } |
| 4323 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4324 | void ASTContext::setObjCSelType(QualType T) { |
Fariborz Jahanian | 252ba5f | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 4325 | ObjCSelTypedefType = T; |
Fariborz Jahanian | 4bef462 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 4326 | } |
| 4327 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4328 | void ASTContext::setObjCProtoType(QualType QT) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4329 | ObjCProtoType = QT; |
Fariborz Jahanian | a32aaef | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 4330 | } |
| 4331 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4332 | void ASTContext::setObjCClassType(QualType T) { |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 4333 | ObjCClassTypedefType = T; |
Anders Carlsson | f56a7ae | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 4334 | } |
| 4335 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4336 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4337 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | f73b784 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 4338 | "'NSConstantString' type already set!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4339 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4340 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | f73b784 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 4341 | } |
| 4342 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4343 | /// \brief Retrieve the template name that corresponds to a non-empty |
| 4344 | /// lookup. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4345 | TemplateName |
| 4346 | ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin, |
| 4347 | UnresolvedSetIterator End) const { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4348 | unsigned size = End - Begin; |
| 4349 | assert(size > 1 && "set is not overloaded!"); |
| 4350 | |
| 4351 | void *memory = Allocate(sizeof(OverloadedTemplateStorage) + |
| 4352 | size * sizeof(FunctionTemplateDecl*)); |
| 4353 | OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size); |
| 4354 | |
| 4355 | NamedDecl **Storage = OT->getStorage(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4356 | for (UnresolvedSetIterator I = Begin; I != End; ++I) { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4357 | NamedDecl *D = *I; |
| 4358 | assert(isa<FunctionTemplateDecl>(D) || |
| 4359 | (isa<UsingShadowDecl>(D) && |
| 4360 | isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))); |
| 4361 | *Storage++ = D; |
| 4362 | } |
| 4363 | |
| 4364 | return TemplateName(OT); |
| 4365 | } |
| 4366 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4367 | /// \brief Retrieve the template name that represents a qualified |
| 4368 | /// template name such as \c std::vector. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4369 | TemplateName |
| 4370 | ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
| 4371 | bool TemplateKeyword, |
| 4372 | TemplateDecl *Template) const { |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4373 | // FIXME: Canonicalization? |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4374 | llvm::FoldingSetNodeID ID; |
| 4375 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 4376 | |
| 4377 | void *InsertPos = 0; |
| 4378 | QualifiedTemplateName *QTN = |
| 4379 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4380 | if (!QTN) { |
| 4381 | QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); |
| 4382 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 4383 | } |
| 4384 | |
| 4385 | return TemplateName(QTN); |
| 4386 | } |
| 4387 | |
| 4388 | /// \brief Retrieve the template name that represents a dependent |
| 4389 | /// template name such as \c MetaFun::template apply. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4390 | TemplateName |
| 4391 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
| 4392 | const IdentifierInfo *Name) const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4393 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 308047d | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 4394 | "Nested name specifier must be dependent"); |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4395 | |
| 4396 | llvm::FoldingSetNodeID ID; |
| 4397 | DependentTemplateName::Profile(ID, NNS, Name); |
| 4398 | |
| 4399 | void *InsertPos = 0; |
| 4400 | DependentTemplateName *QTN = |
| 4401 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4402 | |
| 4403 | if (QTN) |
| 4404 | return TemplateName(QTN); |
| 4405 | |
| 4406 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 4407 | if (CanonNNS == NNS) { |
| 4408 | QTN = new (*this,4) DependentTemplateName(NNS, Name); |
| 4409 | } else { |
| 4410 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
| 4411 | QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4412 | DependentTemplateName *CheckQTN = |
| 4413 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4414 | assert(!CheckQTN && "Dependent type name canonicalization broken"); |
| 4415 | (void)CheckQTN; |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4416 | } |
| 4417 | |
| 4418 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 4419 | return TemplateName(QTN); |
| 4420 | } |
| 4421 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4422 | /// \brief Retrieve the template name that represents a dependent |
| 4423 | /// template name such as \c MetaFun::template operator+. |
| 4424 | TemplateName |
| 4425 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4426 | OverloadedOperatorKind Operator) const { |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4427 | assert((!NNS || NNS->isDependent()) && |
| 4428 | "Nested name specifier must be dependent"); |
| 4429 | |
| 4430 | llvm::FoldingSetNodeID ID; |
| 4431 | DependentTemplateName::Profile(ID, NNS, Operator); |
| 4432 | |
| 4433 | void *InsertPos = 0; |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4434 | DependentTemplateName *QTN |
| 4435 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4436 | |
| 4437 | if (QTN) |
| 4438 | return TemplateName(QTN); |
| 4439 | |
| 4440 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 4441 | if (CanonNNS == NNS) { |
| 4442 | QTN = new (*this,4) DependentTemplateName(NNS, Operator); |
| 4443 | } else { |
| 4444 | TemplateName Canon = getDependentTemplateName(CanonNNS, Operator); |
| 4445 | QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4446 | |
| 4447 | DependentTemplateName *CheckQTN |
| 4448 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4449 | assert(!CheckQTN && "Dependent template name canonicalization broken"); |
| 4450 | (void)CheckQTN; |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4451 | } |
| 4452 | |
| 4453 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 4454 | return TemplateName(QTN); |
| 4455 | } |
| 4456 | |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 4457 | TemplateName |
| 4458 | ASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param, |
| 4459 | const TemplateArgument &ArgPack) const { |
| 4460 | ASTContext &Self = const_cast<ASTContext &>(*this); |
| 4461 | llvm::FoldingSetNodeID ID; |
| 4462 | SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack); |
| 4463 | |
| 4464 | void *InsertPos = 0; |
| 4465 | SubstTemplateTemplateParmPackStorage *Subst |
| 4466 | = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos); |
| 4467 | |
| 4468 | if (!Subst) { |
| 4469 | Subst = new (*this) SubstTemplateTemplateParmPackStorage(Self, Param, |
| 4470 | ArgPack.pack_size(), |
| 4471 | ArgPack.pack_begin()); |
| 4472 | SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos); |
| 4473 | } |
| 4474 | |
| 4475 | return TemplateName(Subst); |
| 4476 | } |
| 4477 | |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4478 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | ab13857 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 4479 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 4480 | /// is actually a value of type @c TargetInfo::IntType. |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 4481 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4482 | switch (Type) { |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 4483 | case TargetInfo::NoInt: return CanQualType(); |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4484 | case TargetInfo::SignedShort: return ShortTy; |
| 4485 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 4486 | case TargetInfo::SignedInt: return IntTy; |
| 4487 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 4488 | case TargetInfo::SignedLong: return LongTy; |
| 4489 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 4490 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 4491 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 4492 | } |
| 4493 | |
| 4494 | assert(false && "Unhandled TargetInfo::IntType value"); |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 4495 | return CanQualType(); |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4496 | } |
Ted Kremenek | 77c51b2 | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 4497 | |
| 4498 | //===----------------------------------------------------------------------===// |
| 4499 | // Type Predicates. |
| 4500 | //===----------------------------------------------------------------------===// |
| 4501 | |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4502 | /// isObjCNSObjectType - Return true if this is an NSObject object using |
| 4503 | /// NSObject attribute on a c-style pointer type. |
| 4504 | /// FIXME - Make it work directly on types. |
Steve Naroff | 79d1215 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 4505 | /// FIXME: Move to Type. |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4506 | /// |
| 4507 | bool ASTContext::isObjCNSObjectType(QualType Ty) const { |
| 4508 | if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) { |
| 4509 | if (TypedefDecl *TD = TDT->getDecl()) |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 4510 | if (TD->getAttr<ObjCNSObjectAttr>()) |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4511 | return true; |
| 4512 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4513 | return false; |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4514 | } |
| 4515 | |
Fariborz Jahanian | 9620769 | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4516 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 4517 | /// garbage collection attribute. |
| 4518 | /// |
John McCall | 553d45a | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 4519 | Qualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const { |
| 4520 | if (getLangOptions().getGCMode() == LangOptions::NonGC) |
| 4521 | return Qualifiers::GCNone; |
| 4522 | |
| 4523 | assert(getLangOptions().ObjC1); |
| 4524 | Qualifiers::GC GCAttrs = Ty.getObjCGCAttr(); |
| 4525 | |
| 4526 | // Default behaviour under objective-C's gc is for ObjC pointers |
| 4527 | // (or pointers to them) be treated as though they were declared |
| 4528 | // as __strong. |
| 4529 | if (GCAttrs == Qualifiers::GCNone) { |
| 4530 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
| 4531 | return Qualifiers::Strong; |
| 4532 | else if (Ty->isPointerType()) |
| 4533 | return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); |
| 4534 | } else { |
| 4535 | // It's not valid to set GC attributes on anything that isn't a |
| 4536 | // pointer. |
| 4537 | #ifndef NDEBUG |
| 4538 | QualType CT = Ty->getCanonicalTypeInternal(); |
| 4539 | while (const ArrayType *AT = dyn_cast<ArrayType>(CT)) |
| 4540 | CT = AT->getElementType(); |
| 4541 | assert(CT->isAnyPointerType() || CT->isBlockPointerType()); |
| 4542 | #endif |
Fariborz Jahanian | 9620769 | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4543 | } |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 4544 | return GCAttrs; |
Fariborz Jahanian | 9620769 | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4545 | } |
| 4546 | |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4547 | //===----------------------------------------------------------------------===// |
| 4548 | // Type Compatibility Testing |
| 4549 | //===----------------------------------------------------------------------===// |
Chris Lattner | b338a6b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 4550 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4551 | /// areCompatVectorTypes - Return true if the two specified vector types are |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4552 | /// compatible. |
| 4553 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 4554 | const VectorType *RHS) { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 4555 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4556 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4557 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4558 | } |
| 4559 | |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 4560 | bool ASTContext::areCompatibleVectorTypes(QualType FirstVec, |
| 4561 | QualType SecondVec) { |
| 4562 | assert(FirstVec->isVectorType() && "FirstVec should be a vector type"); |
| 4563 | assert(SecondVec->isVectorType() && "SecondVec should be a vector type"); |
| 4564 | |
| 4565 | if (hasSameUnqualifiedType(FirstVec, SecondVec)) |
| 4566 | return true; |
| 4567 | |
Bob Wilson | e6aeebb | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 4568 | // Treat Neon vector types and most AltiVec vector types as if they are the |
| 4569 | // equivalent GCC vector types. |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 4570 | const VectorType *First = FirstVec->getAs<VectorType>(); |
| 4571 | const VectorType *Second = SecondVec->getAs<VectorType>(); |
Bob Wilson | e6aeebb | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 4572 | if (First->getNumElements() == Second->getNumElements() && |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 4573 | hasSameType(First->getElementType(), Second->getElementType()) && |
Bob Wilson | e6aeebb | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 4574 | First->getVectorKind() != VectorType::AltiVecPixel && |
| 4575 | First->getVectorKind() != VectorType::AltiVecBool && |
| 4576 | Second->getVectorKind() != VectorType::AltiVecPixel && |
| 4577 | Second->getVectorKind() != VectorType::AltiVecBool) |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 4578 | return true; |
| 4579 | |
| 4580 | return false; |
| 4581 | } |
| 4582 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4583 | //===----------------------------------------------------------------------===// |
| 4584 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 4585 | //===----------------------------------------------------------------------===// |
| 4586 | |
| 4587 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 4588 | /// inheritance hierarchy of 'rProto'. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4589 | bool |
| 4590 | ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 4591 | ObjCProtocolDecl *rProto) const { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4592 | if (lProto == rProto) |
| 4593 | return true; |
| 4594 | for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), |
| 4595 | E = rProto->protocol_end(); PI != E; ++PI) |
| 4596 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 4597 | return true; |
| 4598 | return false; |
| 4599 | } |
| 4600 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4601 | /// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...> |
| 4602 | /// return true if lhs's protocols conform to rhs's protocol; false |
| 4603 | /// otherwise. |
| 4604 | bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) { |
| 4605 | if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType()) |
| 4606 | return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false); |
| 4607 | return false; |
| 4608 | } |
| 4609 | |
Fariborz Jahanian | 3c7ebc3 | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 4610 | /// ObjCQualifiedClassTypesAreCompatible - compare Class<p,...> and |
| 4611 | /// Class<p1, ...>. |
| 4612 | bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs, |
| 4613 | QualType rhs) { |
| 4614 | const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>(); |
| 4615 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
| 4616 | assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible"); |
| 4617 | |
| 4618 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4619 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4620 | bool match = false; |
| 4621 | ObjCProtocolDecl *lhsProto = *I; |
| 4622 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 4623 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 4624 | ObjCProtocolDecl *rhsProto = *J; |
| 4625 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) { |
| 4626 | match = true; |
| 4627 | break; |
| 4628 | } |
| 4629 | } |
| 4630 | if (!match) |
| 4631 | return false; |
| 4632 | } |
| 4633 | return true; |
| 4634 | } |
| 4635 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4636 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 4637 | /// ObjCQualifiedIDType. |
| 4638 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 4639 | bool compare) { |
| 4640 | // Allow id<P..> and an 'id' or void* type in all cases. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4641 | if (lhs->isVoidPointerType() || |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4642 | lhs->isObjCIdType() || lhs->isObjCClassType()) |
| 4643 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4644 | else if (rhs->isVoidPointerType() || |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4645 | rhs->isObjCIdType() || rhs->isObjCClassType()) |
| 4646 | return true; |
| 4647 | |
| 4648 | if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4649 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4650 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4651 | if (!rhsOPT) return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4652 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4653 | if (rhsOPT->qual_empty()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4654 | // If the RHS is a unqualified interface pointer "NSString*", |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4655 | // make sure we check the class hierarchy. |
| 4656 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 4657 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4658 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4659 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4660 | // see if static class implements all of id's protocols, directly or |
| 4661 | // through its super class and categories. |
Fariborz Jahanian | 3f8917a | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 4662 | if (!rhsID->ClassImplementsProtocol(*I, true)) |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4663 | return false; |
| 4664 | } |
| 4665 | } |
| 4666 | // If there are no qualifiers and no interface, we have an 'id'. |
| 4667 | return true; |
| 4668 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4669 | // Both the right and left sides have qualifiers. |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4670 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4671 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4672 | ObjCProtocolDecl *lhsProto = *I; |
| 4673 | bool match = false; |
| 4674 | |
| 4675 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4676 | // see if static class implements all of id's protocols, directly or |
| 4677 | // through its super class and categories. |
| 4678 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 4679 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 4680 | ObjCProtocolDecl *rhsProto = *J; |
| 4681 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 4682 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 4683 | match = true; |
| 4684 | break; |
| 4685 | } |
| 4686 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4687 | // If the RHS is a qualified interface pointer "NSString<P>*", |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4688 | // make sure we check the class hierarchy. |
| 4689 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 4690 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4691 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4692 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4693 | // see if static class implements all of id's protocols, directly or |
| 4694 | // through its super class and categories. |
Fariborz Jahanian | 3f8917a | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 4695 | if (rhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4696 | match = true; |
| 4697 | break; |
| 4698 | } |
| 4699 | } |
| 4700 | } |
| 4701 | if (!match) |
| 4702 | return false; |
| 4703 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4704 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4705 | return true; |
| 4706 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4707 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4708 | const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 4709 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 4710 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4711 | if (const ObjCObjectPointerType *lhsOPT = |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4712 | lhs->getAsObjCInterfacePointerType()) { |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 4713 | // If both the right and left sides have qualifiers. |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4714 | for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(), |
| 4715 | E = lhsOPT->qual_end(); I != E; ++I) { |
| 4716 | ObjCProtocolDecl *lhsProto = *I; |
| 4717 | bool match = false; |
| 4718 | |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 4719 | // when comparing an id<P> on rhs with a static type on lhs, |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4720 | // see if static class implements all of id's protocols, directly or |
| 4721 | // through its super class and categories. |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 4722 | // First, lhs protocols in the qualifier list must be found, direct |
| 4723 | // or indirect in rhs's qualifier list or it is a mismatch. |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4724 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 4725 | E = rhsQID->qual_end(); J != E; ++J) { |
| 4726 | ObjCProtocolDecl *rhsProto = *J; |
| 4727 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 4728 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 4729 | match = true; |
| 4730 | break; |
| 4731 | } |
| 4732 | } |
| 4733 | if (!match) |
| 4734 | return false; |
| 4735 | } |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 4736 | |
| 4737 | // Static class's protocols, or its super class or category protocols |
| 4738 | // must be found, direct or indirect in rhs's qualifier list or it is a mismatch. |
| 4739 | if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { |
| 4740 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
| 4741 | CollectInheritedProtocols(lhsID, LHSInheritedProtocols); |
| 4742 | // This is rather dubious but matches gcc's behavior. If lhs has |
| 4743 | // no type qualifier and its class has no static protocol(s) |
| 4744 | // assume that it is mismatch. |
| 4745 | if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty()) |
| 4746 | return false; |
| 4747 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 4748 | LHSInheritedProtocols.begin(), |
| 4749 | E = LHSInheritedProtocols.end(); I != E; ++I) { |
| 4750 | bool match = false; |
| 4751 | ObjCProtocolDecl *lhsProto = (*I); |
| 4752 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 4753 | E = rhsQID->qual_end(); J != E; ++J) { |
| 4754 | ObjCProtocolDecl *rhsProto = *J; |
| 4755 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 4756 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 4757 | match = true; |
| 4758 | break; |
| 4759 | } |
| 4760 | } |
| 4761 | if (!match) |
| 4762 | return false; |
| 4763 | } |
| 4764 | } |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4765 | return true; |
| 4766 | } |
| 4767 | return false; |
| 4768 | } |
| 4769 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4770 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4771 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 4772 | /// protocol qualifiers on the LHS or RHS. |
| 4773 | /// |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4774 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 4775 | const ObjCObjectPointerType *RHSOPT) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4776 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 4777 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 4778 | |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 4779 | // If either type represents the built-in 'id' or 'Class' types, return true. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4780 | if (LHS->isObjCUnqualifiedIdOrClass() || |
| 4781 | RHS->isObjCUnqualifiedIdOrClass()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4782 | return true; |
| 4783 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4784 | if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4785 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 4786 | QualType(RHSOPT,0), |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4787 | false); |
Fariborz Jahanian | 3c7ebc3 | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 4788 | |
| 4789 | if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass()) |
| 4790 | return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0), |
| 4791 | QualType(RHSOPT,0)); |
| 4792 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4793 | // If we have 2 user-defined types, fall into that path. |
| 4794 | if (LHS->getInterface() && RHS->getInterface()) |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4795 | return canAssignObjCInterfaces(LHS, RHS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4796 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4797 | return false; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4798 | } |
| 4799 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4800 | /// canAssignObjCInterfacesInBlockPointer - This routine is specifically written |
| 4801 | /// for providing type-safty for objective-c pointers used to pass/return |
| 4802 | /// arguments in block literals. When passed as arguments, passing 'A*' where |
| 4803 | /// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is |
| 4804 | /// not OK. For the return type, the opposite is not OK. |
| 4805 | bool ASTContext::canAssignObjCInterfacesInBlockPointer( |
| 4806 | const ObjCObjectPointerType *LHSOPT, |
| 4807 | const ObjCObjectPointerType *RHSOPT) { |
Fariborz Jahanian | 440a683 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 4808 | if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType()) |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4809 | return true; |
| 4810 | |
| 4811 | if (LHSOPT->isObjCBuiltinType()) { |
| 4812 | return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType(); |
| 4813 | } |
| 4814 | |
Fariborz Jahanian | 440a683 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 4815 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4816 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 4817 | QualType(RHSOPT,0), |
| 4818 | false); |
| 4819 | |
| 4820 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 4821 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 4822 | if (LHS && RHS) { // We have 2 user-defined types. |
| 4823 | if (LHS != RHS) { |
| 4824 | if (LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
| 4825 | return false; |
| 4826 | if (RHS->getDecl()->isSuperClassOf(LHS->getDecl())) |
| 4827 | return true; |
| 4828 | } |
| 4829 | else |
| 4830 | return true; |
| 4831 | } |
| 4832 | return false; |
| 4833 | } |
| 4834 | |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4835 | /// getIntersectionOfProtocols - This routine finds the intersection of set |
| 4836 | /// of protocols inherited from two distinct objective-c pointer objects. |
| 4837 | /// It is used to build composite qualifier list of the composite type of |
| 4838 | /// the conditional expression involving two objective-c pointer objects. |
| 4839 | static |
| 4840 | void getIntersectionOfProtocols(ASTContext &Context, |
| 4841 | const ObjCObjectPointerType *LHSOPT, |
| 4842 | const ObjCObjectPointerType *RHSOPT, |
| 4843 | llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) { |
| 4844 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4845 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 4846 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 4847 | assert(LHS->getInterface() && "LHS must have an interface base"); |
| 4848 | assert(RHS->getInterface() && "RHS must have an interface base"); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4849 | |
| 4850 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet; |
| 4851 | unsigned LHSNumProtocols = LHS->getNumProtocols(); |
| 4852 | if (LHSNumProtocols > 0) |
| 4853 | InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end()); |
| 4854 | else { |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 4855 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4856 | Context.CollectInheritedProtocols(LHS->getInterface(), |
| 4857 | LHSInheritedProtocols); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4858 | InheritedProtocolSet.insert(LHSInheritedProtocols.begin(), |
| 4859 | LHSInheritedProtocols.end()); |
| 4860 | } |
| 4861 | |
| 4862 | unsigned RHSNumProtocols = RHS->getNumProtocols(); |
| 4863 | if (RHSNumProtocols > 0) { |
Dan Gohman | 145f3f1 | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 4864 | ObjCProtocolDecl **RHSProtocols = |
| 4865 | const_cast<ObjCProtocolDecl **>(RHS->qual_begin()); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4866 | for (unsigned i = 0; i < RHSNumProtocols; ++i) |
| 4867 | if (InheritedProtocolSet.count(RHSProtocols[i])) |
| 4868 | IntersectionOfProtocols.push_back(RHSProtocols[i]); |
| 4869 | } |
| 4870 | else { |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 4871 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4872 | Context.CollectInheritedProtocols(RHS->getInterface(), |
| 4873 | RHSInheritedProtocols); |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 4874 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 4875 | RHSInheritedProtocols.begin(), |
| 4876 | E = RHSInheritedProtocols.end(); I != E; ++I) |
| 4877 | if (InheritedProtocolSet.count((*I))) |
| 4878 | IntersectionOfProtocols.push_back((*I)); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4879 | } |
| 4880 | } |
| 4881 | |
Fariborz Jahanian | ef8b8ce | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4882 | /// areCommonBaseCompatible - Returns common base class of the two classes if |
| 4883 | /// one found. Note that this is O'2 algorithm. But it will be called as the |
| 4884 | /// last type comparison in a ?-exp of ObjC pointer types before a |
| 4885 | /// warning is issued. So, its invokation is extremely rare. |
| 4886 | QualType ASTContext::areCommonBaseCompatible( |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4887 | const ObjCObjectPointerType *Lptr, |
| 4888 | const ObjCObjectPointerType *Rptr) { |
| 4889 | const ObjCObjectType *LHS = Lptr->getObjectType(); |
| 4890 | const ObjCObjectType *RHS = Rptr->getObjectType(); |
| 4891 | const ObjCInterfaceDecl* LDecl = LHS->getInterface(); |
| 4892 | const ObjCInterfaceDecl* RDecl = RHS->getInterface(); |
| 4893 | if (!LDecl || !RDecl) |
Fariborz Jahanian | ef8b8ce | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4894 | return QualType(); |
| 4895 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4896 | while ((LDecl = LDecl->getSuperClass())) { |
| 4897 | LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl)); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4898 | if (canAssignObjCInterfaces(LHS, RHS)) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4899 | llvm::SmallVector<ObjCProtocolDecl *, 8> Protocols; |
| 4900 | getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols); |
| 4901 | |
| 4902 | QualType Result = QualType(LHS, 0); |
| 4903 | if (!Protocols.empty()) |
| 4904 | Result = getObjCObjectType(Result, Protocols.data(), Protocols.size()); |
| 4905 | Result = getObjCObjectPointerType(Result); |
| 4906 | return Result; |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4907 | } |
Fariborz Jahanian | ef8b8ce | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4908 | } |
| 4909 | |
| 4910 | return QualType(); |
| 4911 | } |
| 4912 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4913 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS, |
| 4914 | const ObjCObjectType *RHS) { |
| 4915 | assert(LHS->getInterface() && "LHS is not an interface type"); |
| 4916 | assert(RHS->getInterface() && "RHS is not an interface type"); |
| 4917 | |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4918 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 4919 | // the LHS. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4920 | if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface())) |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4921 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4922 | |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4923 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 4924 | // protocol qualified at all, then we are good. |
Steve Naroff | c277ad1 | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4925 | if (LHS->getNumProtocols() == 0) |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4926 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4927 | |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4928 | // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it |
| 4929 | // isn't a superset. |
Steve Naroff | c277ad1 | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4930 | if (RHS->getNumProtocols() == 0) |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4931 | return true; // FIXME: should return false! |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4932 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4933 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 4934 | LHSPE = LHS->qual_end(); |
Steve Naroff | 114aecb | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4935 | LHSPI != LHSPE; LHSPI++) { |
| 4936 | bool RHSImplementsProtocol = false; |
| 4937 | |
| 4938 | // If the RHS doesn't implement the protocol on the left, the types |
| 4939 | // are incompatible. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4940 | for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(), |
| 4941 | RHSPE = RHS->qual_end(); |
Steve Naroff | fac5bc9 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 4942 | RHSPI != RHSPE; RHSPI++) { |
| 4943 | if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) { |
Steve Naroff | 114aecb | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4944 | RHSImplementsProtocol = true; |
Steve Naroff | fac5bc9 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 4945 | break; |
| 4946 | } |
Steve Naroff | 114aecb | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4947 | } |
| 4948 | // FIXME: For better diagnostics, consider passing back the protocol name. |
| 4949 | if (!RHSImplementsProtocol) |
| 4950 | return false; |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4951 | } |
Steve Naroff | 114aecb | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4952 | // The RHS implements all protocols listed on the LHS. |
| 4953 | return true; |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4954 | } |
| 4955 | |
Steve Naroff | b760515 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4956 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 4957 | // get the "pointed to" types |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4958 | const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
| 4959 | const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4960 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4961 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | b760515 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4962 | return false; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4963 | |
| 4964 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 4965 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | b760515 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4966 | } |
| 4967 | |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4968 | bool ASTContext::canBindObjCObjectType(QualType To, QualType From) { |
| 4969 | return canAssignObjCInterfaces( |
| 4970 | getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(), |
| 4971 | getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>()); |
| 4972 | } |
| 4973 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4974 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4975 | /// both shall have the identically qualified version of a compatible type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4976 | /// C99 6.2.7p1: Two types have compatible types if their types are the |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4977 | /// same. See 6.7.[2,3,5] for additional rules. |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4978 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS, |
| 4979 | bool CompareUnqualified) { |
Douglas Gregor | 21e771e | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 4980 | if (getLangOptions().CPlusPlus) |
| 4981 | return hasSameType(LHS, RHS); |
| 4982 | |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4983 | return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull(); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4984 | } |
| 4985 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4986 | bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) { |
| 4987 | return !mergeTypes(LHS, RHS, true).isNull(); |
| 4988 | } |
| 4989 | |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 4990 | /// mergeTransparentUnionType - if T is a transparent union type and a member |
| 4991 | /// of T is compatible with SubType, return the merged type, else return |
| 4992 | /// QualType() |
| 4993 | QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType, |
| 4994 | bool OfBlockPointer, |
| 4995 | bool Unqualified) { |
| 4996 | if (const RecordType *UT = T->getAsUnionType()) { |
| 4997 | RecordDecl *UD = UT->getDecl(); |
| 4998 | if (UD->hasAttr<TransparentUnionAttr>()) { |
| 4999 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 5000 | itend = UD->field_end(); it != itend; ++it) { |
Peter Collingbourne | 19b961d | 2010-12-02 21:00:06 +0000 | [diff] [blame] | 5001 | QualType ET = it->getType().getUnqualifiedType(); |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 5002 | QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified); |
| 5003 | if (!MT.isNull()) |
| 5004 | return MT; |
| 5005 | } |
| 5006 | } |
| 5007 | } |
| 5008 | |
| 5009 | return QualType(); |
| 5010 | } |
| 5011 | |
| 5012 | /// mergeFunctionArgumentTypes - merge two types which appear as function |
| 5013 | /// argument types |
| 5014 | QualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs, |
| 5015 | bool OfBlockPointer, |
| 5016 | bool Unqualified) { |
| 5017 | // GNU extension: two types are compatible if they appear as a function |
| 5018 | // argument, one of the types is a transparent union type and the other |
| 5019 | // type is compatible with a union member |
| 5020 | QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer, |
| 5021 | Unqualified); |
| 5022 | if (!lmerge.isNull()) |
| 5023 | return lmerge; |
| 5024 | |
| 5025 | QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer, |
| 5026 | Unqualified); |
| 5027 | if (!rmerge.isNull()) |
| 5028 | return rmerge; |
| 5029 | |
| 5030 | return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified); |
| 5031 | } |
| 5032 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5033 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs, |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5034 | bool OfBlockPointer, |
| 5035 | bool Unqualified) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5036 | const FunctionType *lbase = lhs->getAs<FunctionType>(); |
| 5037 | const FunctionType *rbase = rhs->getAs<FunctionType>(); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5038 | const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 5039 | const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5040 | bool allLTypes = true; |
| 5041 | bool allRTypes = true; |
| 5042 | |
| 5043 | // Check return type |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5044 | QualType retType; |
| 5045 | if (OfBlockPointer) |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5046 | retType = mergeTypes(rbase->getResultType(), lbase->getResultType(), true, |
| 5047 | Unqualified); |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5048 | else |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5049 | retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), false, |
| 5050 | Unqualified); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5051 | if (retType.isNull()) return QualType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5052 | |
| 5053 | if (Unqualified) |
| 5054 | retType = retType.getUnqualifiedType(); |
| 5055 | |
| 5056 | CanQualType LRetType = getCanonicalType(lbase->getResultType()); |
| 5057 | CanQualType RRetType = getCanonicalType(rbase->getResultType()); |
| 5058 | if (Unqualified) { |
| 5059 | LRetType = LRetType.getUnqualifiedType(); |
| 5060 | RRetType = RRetType.getUnqualifiedType(); |
| 5061 | } |
| 5062 | |
| 5063 | if (getCanonicalType(retType) != LRetType) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5064 | allLTypes = false; |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5065 | if (getCanonicalType(retType) != RRetType) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5066 | allRTypes = false; |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5067 | |
Daniel Dunbar | edd5bae | 2010-04-28 16:20:58 +0000 | [diff] [blame] | 5068 | // FIXME: double check this |
| 5069 | // FIXME: should we error if lbase->getRegParmAttr() != 0 && |
| 5070 | // rbase->getRegParmAttr() != 0 && |
| 5071 | // lbase->getRegParmAttr() != rbase->getRegParmAttr()? |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 5072 | FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo(); |
| 5073 | FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo(); |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5074 | |
Douglas Gregor | 8c94086 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 5075 | // Compatible functions must have compatible calling conventions |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5076 | if (!isSameCallConv(lbaseInfo.getCC(), rbaseInfo.getCC())) |
Douglas Gregor | 8c94086 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 5077 | return QualType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5078 | |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5079 | // Regparm is part of the calling convention. |
| 5080 | if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm()) |
| 5081 | return QualType(); |
| 5082 | |
| 5083 | // It's noreturn if either type is. |
| 5084 | // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'. |
| 5085 | bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn(); |
| 5086 | if (NoReturn != lbaseInfo.getNoReturn()) |
| 5087 | allLTypes = false; |
| 5088 | if (NoReturn != rbaseInfo.getNoReturn()) |
| 5089 | allRTypes = false; |
| 5090 | |
| 5091 | FunctionType::ExtInfo einfo(NoReturn, |
| 5092 | lbaseInfo.getRegParm(), |
| 5093 | lbaseInfo.getCC()); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5094 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5095 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 5096 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 5097 | "C++ shouldn't be here"); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5098 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 5099 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 5100 | |
| 5101 | // Compatible functions must have the same number of arguments |
| 5102 | if (lproto_nargs != rproto_nargs) |
| 5103 | return QualType(); |
| 5104 | |
| 5105 | // Variadic and non-variadic functions aren't compatible |
| 5106 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 5107 | return QualType(); |
| 5108 | |
Argyrios Kyrtzidis | 22a3735 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 5109 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 5110 | return QualType(); |
| 5111 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5112 | // Check argument compatibility |
| 5113 | llvm::SmallVector<QualType, 10> types; |
| 5114 | for (unsigned i = 0; i < lproto_nargs; i++) { |
| 5115 | QualType largtype = lproto->getArgType(i).getUnqualifiedType(); |
| 5116 | QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 5117 | QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype, |
| 5118 | OfBlockPointer, |
| 5119 | Unqualified); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5120 | if (argtype.isNull()) return QualType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5121 | |
| 5122 | if (Unqualified) |
| 5123 | argtype = argtype.getUnqualifiedType(); |
| 5124 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5125 | types.push_back(argtype); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5126 | if (Unqualified) { |
| 5127 | largtype = largtype.getUnqualifiedType(); |
| 5128 | rargtype = rargtype.getUnqualifiedType(); |
| 5129 | } |
| 5130 | |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5131 | if (getCanonicalType(argtype) != getCanonicalType(largtype)) |
| 5132 | allLTypes = false; |
| 5133 | if (getCanonicalType(argtype) != getCanonicalType(rargtype)) |
| 5134 | allRTypes = false; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5135 | } |
| 5136 | if (allLTypes) return lhs; |
| 5137 | if (allRTypes) return rhs; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5138 | |
| 5139 | FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo(); |
| 5140 | EPI.ExtInfo = einfo; |
| 5141 | return getFunctionType(retType, types.begin(), types.size(), EPI); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5142 | } |
| 5143 | |
| 5144 | if (lproto) allRTypes = false; |
| 5145 | if (rproto) allLTypes = false; |
| 5146 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5147 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5148 | if (proto) { |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 5149 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5150 | if (proto->isVariadic()) return QualType(); |
| 5151 | // Check that the types are compatible with the types that |
| 5152 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 5153 | // The only types actually affected are promotable integer |
| 5154 | // types and floats, which would be passed as a different |
| 5155 | // type depending on whether the prototype is visible. |
| 5156 | unsigned proto_nargs = proto->getNumArgs(); |
| 5157 | for (unsigned i = 0; i < proto_nargs; ++i) { |
| 5158 | QualType argTy = proto->getArgType(i); |
Douglas Gregor | 2973d40 | 2010-02-03 19:27:29 +0000 | [diff] [blame] | 5159 | |
| 5160 | // Look at the promotion type of enum types, since that is the type used |
| 5161 | // to pass enum values. |
| 5162 | if (const EnumType *Enum = argTy->getAs<EnumType>()) |
| 5163 | argTy = Enum->getDecl()->getPromotionType(); |
| 5164 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5165 | if (argTy->isPromotableIntegerType() || |
| 5166 | getCanonicalType(argTy).getUnqualifiedType() == FloatTy) |
| 5167 | return QualType(); |
| 5168 | } |
| 5169 | |
| 5170 | if (allLTypes) return lhs; |
| 5171 | if (allRTypes) return rhs; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5172 | |
| 5173 | FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo(); |
| 5174 | EPI.ExtInfo = einfo; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5175 | return getFunctionType(retType, proto->arg_type_begin(), |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5176 | proto->getNumArgs(), EPI); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5177 | } |
| 5178 | |
| 5179 | if (allLTypes) return lhs; |
| 5180 | if (allRTypes) return rhs; |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5181 | return getFunctionNoProtoType(retType, einfo); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5182 | } |
| 5183 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5184 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5185 | bool OfBlockPointer, |
| 5186 | bool Unqualified) { |
Bill Wendling | db4e349 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 5187 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 5188 | // type is adjusted to "T" prior to any further analysis, the expression |
| 5189 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 5190 | // expression is an lvalue unless the reference is an rvalue reference and |
| 5191 | // the expression is a function call (possibly inside parentheses). |
Douglas Gregor | 21e771e | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 5192 | assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?"); |
| 5193 | assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?"); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5194 | |
| 5195 | if (Unqualified) { |
| 5196 | LHS = LHS.getUnqualifiedType(); |
| 5197 | RHS = RHS.getUnqualifiedType(); |
| 5198 | } |
Douglas Gregor | 21e771e | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 5199 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5200 | QualType LHSCan = getCanonicalType(LHS), |
| 5201 | RHSCan = getCanonicalType(RHS); |
| 5202 | |
| 5203 | // If two types are identical, they are compatible. |
| 5204 | if (LHSCan == RHSCan) |
| 5205 | return LHS; |
| 5206 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5207 | // If the qualifiers are different, the types aren't compatible... mostly. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 5208 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 5209 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5210 | if (LQuals != RQuals) { |
| 5211 | // If any of these qualifiers are different, we have a type |
| 5212 | // mismatch. |
| 5213 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 5214 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 5215 | return QualType(); |
| 5216 | |
| 5217 | // Exactly one GC qualifier difference is allowed: __strong is |
| 5218 | // okay if the other type has no GC qualifier but is an Objective |
| 5219 | // C object pointer (i.e. implicitly strong by default). We fix |
| 5220 | // this by pretending that the unqualified type was actually |
| 5221 | // qualified __strong. |
| 5222 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 5223 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 5224 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 5225 | |
| 5226 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 5227 | return QualType(); |
| 5228 | |
| 5229 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
| 5230 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); |
| 5231 | } |
| 5232 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
| 5233 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); |
| 5234 | } |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5235 | return QualType(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5236 | } |
| 5237 | |
| 5238 | // Okay, qualifiers are equal. |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5239 | |
Eli Friedman | dcca633 | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 5240 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 5241 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5242 | |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5243 | // We want to consider the two function types to be the same for these |
| 5244 | // comparisons, just force one to the other. |
| 5245 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 5246 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 16f9096 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 5247 | |
| 5248 | // Same as above for arrays |
Chris Lattner | 9555466 | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 5249 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 5250 | LHSClass = Type::ConstantArray; |
| 5251 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 5252 | RHSClass = Type::ConstantArray; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5253 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5254 | // ObjCInterfaces are just specialized ObjCObjects. |
| 5255 | if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject; |
| 5256 | if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject; |
| 5257 | |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 5258 | // Canonicalize ExtVector -> Vector. |
| 5259 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 5260 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5261 | |
Chris Lattner | 9555466 | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 5262 | // If the canonical type classes don't match. |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5263 | if (LHSClass != RHSClass) { |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5264 | // C99 6.7.2.2p4: Each enumerated type shall be compatible with char, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5265 | // a signed integer type, or an unsigned integer type. |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 5266 | // Compatibility is based on the underlying type, not the promotion |
| 5267 | // type. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5268 | if (const EnumType* ETy = LHS->getAs<EnumType>()) { |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5269 | if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType()) |
| 5270 | return RHS; |
Eli Friedman | a7bf7ed | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 5271 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5272 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5273 | if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType()) |
| 5274 | return LHS; |
Eli Friedman | a7bf7ed | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 5275 | } |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5276 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5277 | return QualType(); |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5278 | } |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5279 | |
Steve Naroff | c6edcbd | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 5280 | // The canonical type classes match. |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5281 | switch (LHSClass) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5282 | #define TYPE(Class, Base) |
| 5283 | #define ABSTRACT_TYPE(Class, Base) |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 5284 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5285 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 5286 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 5287 | #include "clang/AST/TypeNodes.def" |
| 5288 | assert(false && "Non-canonical and dependent types shouldn't get here"); |
| 5289 | return QualType(); |
| 5290 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 5291 | case Type::LValueReference: |
| 5292 | case Type::RValueReference: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5293 | case Type::MemberPointer: |
| 5294 | assert(false && "C++ should never be in mergeTypes"); |
| 5295 | return QualType(); |
| 5296 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5297 | case Type::ObjCInterface: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5298 | case Type::IncompleteArray: |
| 5299 | case Type::VariableArray: |
| 5300 | case Type::FunctionProto: |
| 5301 | case Type::ExtVector: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5302 | assert(false && "Types are eliminated above"); |
| 5303 | return QualType(); |
| 5304 | |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5305 | case Type::Pointer: |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5306 | { |
| 5307 | // Merge two pointer types, while trying to preserve typedef info |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5308 | QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); |
| 5309 | QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5310 | if (Unqualified) { |
| 5311 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 5312 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 5313 | } |
| 5314 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false, |
| 5315 | Unqualified); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5316 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | 091a9ac | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 5317 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5318 | return LHS; |
Eli Friedman | 091a9ac | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 5319 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5320 | return RHS; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5321 | return getPointerType(ResultType); |
| 5322 | } |
Steve Naroff | 68e167d | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 5323 | case Type::BlockPointer: |
| 5324 | { |
| 5325 | // Merge two block pointer types, while trying to preserve typedef info |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5326 | QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); |
| 5327 | QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5328 | if (Unqualified) { |
| 5329 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 5330 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 5331 | } |
| 5332 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer, |
| 5333 | Unqualified); |
Steve Naroff | 68e167d | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 5334 | if (ResultType.isNull()) return QualType(); |
| 5335 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 5336 | return LHS; |
| 5337 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 5338 | return RHS; |
| 5339 | return getBlockPointerType(ResultType); |
| 5340 | } |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5341 | case Type::ConstantArray: |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5342 | { |
| 5343 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 5344 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 5345 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 5346 | return QualType(); |
| 5347 | |
| 5348 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 5349 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5350 | if (Unqualified) { |
| 5351 | LHSElem = LHSElem.getUnqualifiedType(); |
| 5352 | RHSElem = RHSElem.getUnqualifiedType(); |
| 5353 | } |
| 5354 | |
| 5355 | QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5356 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5357 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 5358 | return LHS; |
| 5359 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 5360 | return RHS; |
Eli Friedman | 3e62c21 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 5361 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 5362 | ArrayType::ArraySizeModifier(), 0); |
| 5363 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 5364 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5365 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 5366 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5367 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 5368 | return LHS; |
| 5369 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 5370 | return RHS; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5371 | if (LVAT) { |
| 5372 | // FIXME: This isn't correct! But tricky to implement because |
| 5373 | // the array's size has to be the size of LHS, but the type |
| 5374 | // has to be different. |
| 5375 | return LHS; |
| 5376 | } |
| 5377 | if (RVAT) { |
| 5378 | // FIXME: This isn't correct! But tricky to implement because |
| 5379 | // the array's size has to be the size of RHS, but the type |
| 5380 | // has to be different. |
| 5381 | return RHS; |
| 5382 | } |
Eli Friedman | 3e62c21 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 5383 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 5384 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 5385 | return getIncompleteArrayType(ResultType, |
| 5386 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5387 | } |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5388 | case Type::FunctionNoProto: |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5389 | return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5390 | case Type::Record: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5391 | case Type::Enum: |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5392 | return QualType(); |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5393 | case Type::Builtin: |
Chris Lattner | 7bbd3d7 | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 5394 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5395 | return QualType(); |
Daniel Dunbar | 804c044 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 5396 | case Type::Complex: |
| 5397 | // Distinct complex types are incompatible. |
| 5398 | return QualType(); |
Chris Lattner | 7bbd3d7 | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 5399 | case Type::Vector: |
Eli Friedman | cad9638 | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 5400 | // FIXME: The merged type should be an ExtVector! |
John McCall | 44c064b | 2010-03-12 23:14:13 +0000 | [diff] [blame] | 5401 | if (areCompatVectorTypes(LHSCan->getAs<VectorType>(), |
| 5402 | RHSCan->getAs<VectorType>())) |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5403 | return LHS; |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5404 | return QualType(); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5405 | case Type::ObjCObject: { |
| 5406 | // Check if the types are assignment compatible. |
Eli Friedman | cad9638 | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 5407 | // FIXME: This should be type compatibility, e.g. whether |
| 5408 | // "LHS x; RHS x;" at global scope is legal. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5409 | const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>(); |
| 5410 | const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>(); |
| 5411 | if (canAssignObjCInterfaces(LHSIface, RHSIface)) |
Steve Naroff | 7a7814c | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 5412 | return LHS; |
| 5413 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5414 | return QualType(); |
Cedric Venet | 4fc88b7 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 5415 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5416 | case Type::ObjCObjectPointer: { |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5417 | if (OfBlockPointer) { |
| 5418 | if (canAssignObjCInterfacesInBlockPointer( |
| 5419 | LHS->getAs<ObjCObjectPointerType>(), |
| 5420 | RHS->getAs<ObjCObjectPointerType>())) |
| 5421 | return LHS; |
| 5422 | return QualType(); |
| 5423 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5424 | if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(), |
| 5425 | RHS->getAs<ObjCObjectPointerType>())) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5426 | return LHS; |
| 5427 | |
Steve Naroff | c68cfcf | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 5428 | return QualType(); |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5429 | } |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5430 | } |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5431 | |
| 5432 | return QualType(); |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5433 | } |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 5434 | |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 5435 | /// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and |
| 5436 | /// 'RHS' attributes and returns the merged version; including for function |
| 5437 | /// return types. |
| 5438 | QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) { |
| 5439 | QualType LHSCan = getCanonicalType(LHS), |
| 5440 | RHSCan = getCanonicalType(RHS); |
| 5441 | // If two types are identical, they are compatible. |
| 5442 | if (LHSCan == RHSCan) |
| 5443 | return LHS; |
| 5444 | if (RHSCan->isFunctionType()) { |
| 5445 | if (!LHSCan->isFunctionType()) |
| 5446 | return QualType(); |
| 5447 | QualType OldReturnType = |
| 5448 | cast<FunctionType>(RHSCan.getTypePtr())->getResultType(); |
| 5449 | QualType NewReturnType = |
| 5450 | cast<FunctionType>(LHSCan.getTypePtr())->getResultType(); |
| 5451 | QualType ResReturnType = |
| 5452 | mergeObjCGCQualifiers(NewReturnType, OldReturnType); |
| 5453 | if (ResReturnType.isNull()) |
| 5454 | return QualType(); |
| 5455 | if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) { |
| 5456 | // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo(); |
| 5457 | // In either case, use OldReturnType to build the new function type. |
| 5458 | const FunctionType *F = LHS->getAs<FunctionType>(); |
| 5459 | if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) { |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5460 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 5461 | EPI.ExtInfo = getFunctionExtInfo(LHS); |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 5462 | QualType ResultType |
| 5463 | = getFunctionType(OldReturnType, FPT->arg_type_begin(), |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5464 | FPT->getNumArgs(), EPI); |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 5465 | return ResultType; |
| 5466 | } |
| 5467 | } |
| 5468 | return QualType(); |
| 5469 | } |
| 5470 | |
| 5471 | // If the qualifiers are different, the types can still be merged. |
| 5472 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 5473 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
| 5474 | if (LQuals != RQuals) { |
| 5475 | // If any of these qualifiers are different, we have a type mismatch. |
| 5476 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 5477 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 5478 | return QualType(); |
| 5479 | |
| 5480 | // Exactly one GC qualifier difference is allowed: __strong is |
| 5481 | // okay if the other type has no GC qualifier but is an Objective |
| 5482 | // C object pointer (i.e. implicitly strong by default). We fix |
| 5483 | // this by pretending that the unqualified type was actually |
| 5484 | // qualified __strong. |
| 5485 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 5486 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 5487 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 5488 | |
| 5489 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 5490 | return QualType(); |
| 5491 | |
| 5492 | if (GC_L == Qualifiers::Strong) |
| 5493 | return LHS; |
| 5494 | if (GC_R == Qualifiers::Strong) |
| 5495 | return RHS; |
| 5496 | return QualType(); |
| 5497 | } |
| 5498 | |
| 5499 | if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) { |
| 5500 | QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5501 | QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5502 | QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT); |
| 5503 | if (ResQT == LHSBaseQT) |
| 5504 | return LHS; |
| 5505 | if (ResQT == RHSBaseQT) |
| 5506 | return RHS; |
| 5507 | } |
| 5508 | return QualType(); |
| 5509 | } |
| 5510 | |
Chris Lattner | 4ba0cef | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 5511 | //===----------------------------------------------------------------------===// |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5512 | // Integer Predicates |
| 5513 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3c91971 | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 5514 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5515 | unsigned ASTContext::getIntWidth(QualType T) const { |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 5516 | if (EnumType *ET = dyn_cast<EnumType>(T)) |
Eli Friedman | ee275c8 | 2009-12-10 22:29:29 +0000 | [diff] [blame] | 5517 | T = ET->getDecl()->getIntegerType(); |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 5518 | if (T->isBooleanType()) |
| 5519 | return 1; |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 5520 | // For builtin types, just use the standard type sizing method |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5521 | return (unsigned)getTypeSize(T); |
| 5522 | } |
| 5523 | |
| 5524 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) { |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5525 | assert(T->hasSignedIntegerRepresentation() && "Unexpected type"); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5526 | |
| 5527 | // Turn <4 x signed int> -> <4 x unsigned int> |
| 5528 | if (const VectorType *VTy = T->getAs<VectorType>()) |
| 5529 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 5530 | VTy->getNumElements(), VTy->getVectorKind()); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5531 | |
| 5532 | // For enums, we return the unsigned version of the base type. |
| 5533 | if (const EnumType *ETy = T->getAs<EnumType>()) |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5534 | T = ETy->getDecl()->getIntegerType(); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5535 | |
| 5536 | const BuiltinType *BTy = T->getAs<BuiltinType>(); |
| 5537 | assert(BTy && "Unexpected signed integer type"); |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5538 | switch (BTy->getKind()) { |
| 5539 | case BuiltinType::Char_S: |
| 5540 | case BuiltinType::SChar: |
| 5541 | return UnsignedCharTy; |
| 5542 | case BuiltinType::Short: |
| 5543 | return UnsignedShortTy; |
| 5544 | case BuiltinType::Int: |
| 5545 | return UnsignedIntTy; |
| 5546 | case BuiltinType::Long: |
| 5547 | return UnsignedLongTy; |
| 5548 | case BuiltinType::LongLong: |
| 5549 | return UnsignedLongLongTy; |
Chris Lattner | f122cef | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 5550 | case BuiltinType::Int128: |
| 5551 | return UnsignedInt128Ty; |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5552 | default: |
| 5553 | assert(0 && "Unexpected signed integer type"); |
| 5554 | return QualType(); |
| 5555 | } |
| 5556 | } |
| 5557 | |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 5558 | ExternalASTSource::~ExternalASTSource() { } |
| 5559 | |
| 5560 | void ExternalASTSource::PrintStats() { } |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5561 | |
Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 5562 | ASTMutationListener::~ASTMutationListener() { } |
| 5563 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5564 | |
| 5565 | //===----------------------------------------------------------------------===// |
| 5566 | // Builtin Type Computation |
| 5567 | //===----------------------------------------------------------------------===// |
| 5568 | |
| 5569 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5570 | /// pointer over the consumed characters. This returns the resultant type. If |
| 5571 | /// AllowTypeModifiers is false then modifier like * are not parsed, just basic |
| 5572 | /// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of |
| 5573 | /// a vector of "i*". |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5574 | /// |
| 5575 | /// RequiresICE is filled in on return to indicate whether the value is required |
| 5576 | /// to be an Integer Constant Expression. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5577 | static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context, |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5578 | ASTContext::GetBuiltinTypeError &Error, |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5579 | bool &RequiresICE, |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5580 | bool AllowTypeModifiers) { |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5581 | // Modifiers. |
| 5582 | int HowLong = 0; |
| 5583 | bool Signed = false, Unsigned = false; |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5584 | RequiresICE = false; |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 5585 | |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5586 | // Read the prefixed modifiers first. |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5587 | bool Done = false; |
| 5588 | while (!Done) { |
| 5589 | switch (*Str++) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5590 | default: Done = true; --Str; break; |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 5591 | case 'I': |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5592 | RequiresICE = true; |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 5593 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5594 | case 'S': |
| 5595 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 5596 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 5597 | Signed = true; |
| 5598 | break; |
| 5599 | case 'U': |
| 5600 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
| 5601 | assert(!Unsigned && "Can't use 'S' modifier multiple times!"); |
| 5602 | Unsigned = true; |
| 5603 | break; |
| 5604 | case 'L': |
| 5605 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 5606 | ++HowLong; |
| 5607 | break; |
| 5608 | } |
| 5609 | } |
| 5610 | |
| 5611 | QualType Type; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5612 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5613 | // Read the base type. |
| 5614 | switch (*Str++) { |
| 5615 | default: assert(0 && "Unknown builtin type letter!"); |
| 5616 | case 'v': |
| 5617 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 5618 | "Bad modifiers used with 'v'!"); |
| 5619 | Type = Context.VoidTy; |
| 5620 | break; |
| 5621 | case 'f': |
| 5622 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 5623 | "Bad modifiers used with 'f'!"); |
| 5624 | Type = Context.FloatTy; |
| 5625 | break; |
| 5626 | case 'd': |
| 5627 | assert(HowLong < 2 && !Signed && !Unsigned && |
| 5628 | "Bad modifiers used with 'd'!"); |
| 5629 | if (HowLong) |
| 5630 | Type = Context.LongDoubleTy; |
| 5631 | else |
| 5632 | Type = Context.DoubleTy; |
| 5633 | break; |
| 5634 | case 's': |
| 5635 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 5636 | if (Unsigned) |
| 5637 | Type = Context.UnsignedShortTy; |
| 5638 | else |
| 5639 | Type = Context.ShortTy; |
| 5640 | break; |
| 5641 | case 'i': |
| 5642 | if (HowLong == 3) |
| 5643 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 5644 | else if (HowLong == 2) |
| 5645 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 5646 | else if (HowLong == 1) |
| 5647 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 5648 | else |
| 5649 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 5650 | break; |
| 5651 | case 'c': |
| 5652 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 5653 | if (Signed) |
| 5654 | Type = Context.SignedCharTy; |
| 5655 | else if (Unsigned) |
| 5656 | Type = Context.UnsignedCharTy; |
| 5657 | else |
| 5658 | Type = Context.CharTy; |
| 5659 | break; |
| 5660 | case 'b': // boolean |
| 5661 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 5662 | Type = Context.BoolTy; |
| 5663 | break; |
| 5664 | case 'z': // size_t. |
| 5665 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 5666 | Type = Context.getSizeType(); |
| 5667 | break; |
| 5668 | case 'F': |
| 5669 | Type = Context.getCFConstantStringType(); |
| 5670 | break; |
Fariborz Jahanian | d11da7e | 2010-11-09 21:38:20 +0000 | [diff] [blame] | 5671 | case 'G': |
| 5672 | Type = Context.getObjCIdType(); |
| 5673 | break; |
| 5674 | case 'H': |
| 5675 | Type = Context.getObjCSelType(); |
| 5676 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5677 | case 'a': |
| 5678 | Type = Context.getBuiltinVaListType(); |
| 5679 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 5680 | break; |
| 5681 | case 'A': |
| 5682 | // This is a "reference" to a va_list; however, what exactly |
| 5683 | // this means depends on how va_list is defined. There are two |
| 5684 | // different kinds of va_list: ones passed by value, and ones |
| 5685 | // passed by reference. An example of a by-value va_list is |
| 5686 | // x86, where va_list is a char*. An example of by-ref va_list |
| 5687 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 5688 | // we want this argument to be a char*&; for x86-64, we want |
| 5689 | // it to be a __va_list_tag*. |
| 5690 | Type = Context.getBuiltinVaListType(); |
| 5691 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5692 | if (Type->isArrayType()) |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5693 | Type = Context.getArrayDecayedType(Type); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5694 | else |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5695 | Type = Context.getLValueReferenceType(Type); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5696 | break; |
| 5697 | case 'V': { |
| 5698 | char *End; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5699 | unsigned NumElements = strtoul(Str, &End, 10); |
| 5700 | assert(End != Str && "Missing vector size"); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5701 | Str = End; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5702 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5703 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, |
| 5704 | RequiresICE, false); |
| 5705 | assert(!RequiresICE && "Can't require vector ICE"); |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5706 | |
| 5707 | // TODO: No way to make AltiVec vectors in builtins yet. |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 5708 | Type = Context.getVectorType(ElementType, NumElements, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 5709 | VectorType::GenericVector); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5710 | break; |
| 5711 | } |
Douglas Gregor | 40ef7c5 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 5712 | case 'X': { |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5713 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
| 5714 | false); |
| 5715 | assert(!RequiresICE && "Can't require complex ICE"); |
Douglas Gregor | 40ef7c5 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 5716 | Type = Context.getComplexType(ElementType); |
| 5717 | break; |
| 5718 | } |
Chris Lattner | a58b3af | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 5719 | case 'P': |
Douglas Gregor | 27821ce | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 5720 | Type = Context.getFILEType(); |
| 5721 | if (Type.isNull()) { |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 5722 | Error = ASTContext::GE_Missing_stdio; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5723 | return QualType(); |
| 5724 | } |
Mike Stump | 2adb4da | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 5725 | break; |
Chris Lattner | a58b3af | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 5726 | case 'J': |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 5727 | if (Signed) |
Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 5728 | Type = Context.getsigjmp_bufType(); |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 5729 | else |
| 5730 | Type = Context.getjmp_bufType(); |
| 5731 | |
Mike Stump | 2adb4da | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 5732 | if (Type.isNull()) { |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 5733 | Error = ASTContext::GE_Missing_setjmp; |
Mike Stump | 2adb4da | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 5734 | return QualType(); |
| 5735 | } |
| 5736 | break; |
Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 5737 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5738 | |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5739 | // If there are modifiers and if we're allowed to parse them, go for it. |
| 5740 | Done = !AllowTypeModifiers; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5741 | while (!Done) { |
John McCall | b8b9466 | 2010-03-12 04:21:28 +0000 | [diff] [blame] | 5742 | switch (char c = *Str++) { |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5743 | default: Done = true; --Str; break; |
| 5744 | case '*': |
| 5745 | case '&': { |
| 5746 | // Both pointers and references can have their pointee types |
| 5747 | // qualified with an address space. |
| 5748 | char *End; |
| 5749 | unsigned AddrSpace = strtoul(Str, &End, 10); |
| 5750 | if (End != Str && AddrSpace != 0) { |
| 5751 | Type = Context.getAddrSpaceQualType(Type, AddrSpace); |
| 5752 | Str = End; |
| 5753 | } |
| 5754 | if (c == '*') |
| 5755 | Type = Context.getPointerType(Type); |
| 5756 | else |
| 5757 | Type = Context.getLValueReferenceType(Type); |
| 5758 | break; |
| 5759 | } |
| 5760 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 5761 | case 'C': |
| 5762 | Type = Type.withConst(); |
| 5763 | break; |
| 5764 | case 'D': |
| 5765 | Type = Context.getVolatileType(Type); |
| 5766 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5767 | } |
| 5768 | } |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 5769 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5770 | assert((!RequiresICE || Type->isIntegralOrEnumerationType()) && |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 5771 | "Integer constant 'I' type must be an integer"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5772 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5773 | return Type; |
| 5774 | } |
| 5775 | |
| 5776 | /// GetBuiltinType - Return the type for the specified builtin. |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5777 | QualType ASTContext::GetBuiltinType(unsigned Id, |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5778 | GetBuiltinTypeError &Error, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5779 | unsigned *IntegerConstantArgs) const { |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5780 | const char *TypeStr = BuiltinInfo.GetTypeString(Id); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5781 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5782 | llvm::SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5783 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5784 | bool RequiresICE = false; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5785 | Error = GE_None; |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5786 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error, |
| 5787 | RequiresICE, true); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5788 | if (Error != GE_None) |
| 5789 | return QualType(); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5790 | |
| 5791 | assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE"); |
| 5792 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5793 | while (TypeStr[0] && TypeStr[0] != '.') { |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5794 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5795 | if (Error != GE_None) |
| 5796 | return QualType(); |
| 5797 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5798 | // If this argument is required to be an IntegerConstantExpression and the |
| 5799 | // caller cares, fill in the bitmask we return. |
| 5800 | if (RequiresICE && IntegerConstantArgs) |
| 5801 | *IntegerConstantArgs |= 1 << ArgTypes.size(); |
| 5802 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5803 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 5804 | if (Ty->isArrayType()) |
| 5805 | Ty = getArrayDecayedType(Ty); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5806 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5807 | ArgTypes.push_back(Ty); |
| 5808 | } |
| 5809 | |
| 5810 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 5811 | "'.' should only occur at end of builtin type list!"); |
| 5812 | |
John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 5813 | FunctionType::ExtInfo EI; |
| 5814 | if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true); |
| 5815 | |
| 5816 | bool Variadic = (TypeStr[0] == '.'); |
| 5817 | |
| 5818 | // We really shouldn't be making a no-proto type here, especially in C++. |
| 5819 | if (ArgTypes.empty() && Variadic) |
| 5820 | return getFunctionNoProtoType(ResType, EI); |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 5821 | |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5822 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 5823 | EPI.ExtInfo = EI; |
| 5824 | EPI.Variadic = Variadic; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5825 | |
| 5826 | return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), EPI); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5827 | } |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5828 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5829 | GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) { |
| 5830 | GVALinkage External = GVA_StrongExternal; |
| 5831 | |
| 5832 | Linkage L = FD->getLinkage(); |
| 5833 | if (L == ExternalLinkage && getLangOptions().CPlusPlus && |
| 5834 | FD->getType()->getLinkage() == UniqueExternalLinkage) |
| 5835 | L = UniqueExternalLinkage; |
| 5836 | |
| 5837 | switch (L) { |
| 5838 | case NoLinkage: |
| 5839 | case InternalLinkage: |
| 5840 | case UniqueExternalLinkage: |
| 5841 | return GVA_Internal; |
| 5842 | |
| 5843 | case ExternalLinkage: |
| 5844 | switch (FD->getTemplateSpecializationKind()) { |
| 5845 | case TSK_Undeclared: |
| 5846 | case TSK_ExplicitSpecialization: |
| 5847 | External = GVA_StrongExternal; |
| 5848 | break; |
| 5849 | |
| 5850 | case TSK_ExplicitInstantiationDefinition: |
| 5851 | return GVA_ExplicitTemplateInstantiation; |
| 5852 | |
| 5853 | case TSK_ExplicitInstantiationDeclaration: |
| 5854 | case TSK_ImplicitInstantiation: |
| 5855 | External = GVA_TemplateInstantiation; |
| 5856 | break; |
| 5857 | } |
| 5858 | } |
| 5859 | |
| 5860 | if (!FD->isInlined()) |
| 5861 | return External; |
| 5862 | |
| 5863 | if (!getLangOptions().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) { |
| 5864 | // GNU or C99 inline semantics. Determine whether this symbol should be |
| 5865 | // externally visible. |
| 5866 | if (FD->isInlineDefinitionExternallyVisible()) |
| 5867 | return External; |
| 5868 | |
| 5869 | // C99 inline semantics, where the symbol is not externally visible. |
| 5870 | return GVA_C99Inline; |
| 5871 | } |
| 5872 | |
| 5873 | // C++0x [temp.explicit]p9: |
| 5874 | // [ Note: The intent is that an inline function that is the subject of |
| 5875 | // an explicit instantiation declaration will still be implicitly |
| 5876 | // instantiated when used so that the body can be considered for |
| 5877 | // inlining, but that no out-of-line copy of the inline function would be |
| 5878 | // generated in the translation unit. -- end note ] |
| 5879 | if (FD->getTemplateSpecializationKind() |
| 5880 | == TSK_ExplicitInstantiationDeclaration) |
| 5881 | return GVA_C99Inline; |
| 5882 | |
| 5883 | return GVA_CXXInline; |
| 5884 | } |
| 5885 | |
| 5886 | GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) { |
| 5887 | // If this is a static data member, compute the kind of template |
| 5888 | // specialization. Otherwise, this variable is not part of a |
| 5889 | // template. |
| 5890 | TemplateSpecializationKind TSK = TSK_Undeclared; |
| 5891 | if (VD->isStaticDataMember()) |
| 5892 | TSK = VD->getTemplateSpecializationKind(); |
| 5893 | |
| 5894 | Linkage L = VD->getLinkage(); |
| 5895 | if (L == ExternalLinkage && getLangOptions().CPlusPlus && |
| 5896 | VD->getType()->getLinkage() == UniqueExternalLinkage) |
| 5897 | L = UniqueExternalLinkage; |
| 5898 | |
| 5899 | switch (L) { |
| 5900 | case NoLinkage: |
| 5901 | case InternalLinkage: |
| 5902 | case UniqueExternalLinkage: |
| 5903 | return GVA_Internal; |
| 5904 | |
| 5905 | case ExternalLinkage: |
| 5906 | switch (TSK) { |
| 5907 | case TSK_Undeclared: |
| 5908 | case TSK_ExplicitSpecialization: |
| 5909 | return GVA_StrongExternal; |
| 5910 | |
| 5911 | case TSK_ExplicitInstantiationDeclaration: |
| 5912 | llvm_unreachable("Variable should not be instantiated"); |
| 5913 | // Fall through to treat this like any other instantiation. |
| 5914 | |
| 5915 | case TSK_ExplicitInstantiationDefinition: |
| 5916 | return GVA_ExplicitTemplateInstantiation; |
| 5917 | |
| 5918 | case TSK_ImplicitInstantiation: |
| 5919 | return GVA_TemplateInstantiation; |
| 5920 | } |
| 5921 | } |
| 5922 | |
| 5923 | return GVA_StrongExternal; |
| 5924 | } |
| 5925 | |
Argyrios Kyrtzidis | c904933 | 2010-07-29 20:08:05 +0000 | [diff] [blame] | 5926 | bool ASTContext::DeclMustBeEmitted(const Decl *D) { |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5927 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 5928 | if (!VD->isFileVarDecl()) |
| 5929 | return false; |
| 5930 | } else if (!isa<FunctionDecl>(D)) |
| 5931 | return false; |
| 5932 | |
Argyrios Kyrtzidis | 6e03a74 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 5933 | // Weak references don't produce any output by themselves. |
| 5934 | if (D->hasAttr<WeakRefAttr>()) |
| 5935 | return false; |
| 5936 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5937 | // Aliases and used decls are required. |
| 5938 | if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>()) |
| 5939 | return true; |
| 5940 | |
| 5941 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 5942 | // Forward declarations aren't required. |
| 5943 | if (!FD->isThisDeclarationADefinition()) |
| 5944 | return false; |
| 5945 | |
| 5946 | // Constructors and destructors are required. |
| 5947 | if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>()) |
| 5948 | return true; |
| 5949 | |
| 5950 | // The key function for a class is required. |
| 5951 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 5952 | const CXXRecordDecl *RD = MD->getParent(); |
| 5953 | if (MD->isOutOfLine() && RD->isDynamicClass()) { |
| 5954 | const CXXMethodDecl *KeyFunc = getKeyFunction(RD); |
| 5955 | if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl()) |
| 5956 | return true; |
| 5957 | } |
| 5958 | } |
| 5959 | |
| 5960 | GVALinkage Linkage = GetGVALinkageForFunction(FD); |
| 5961 | |
| 5962 | // static, static inline, always_inline, and extern inline functions can |
| 5963 | // always be deferred. Normal inline functions can be deferred in C99/C++. |
| 5964 | // Implicit template instantiations can also be deferred in C++. |
| 5965 | if (Linkage == GVA_Internal || Linkage == GVA_C99Inline || |
| 5966 | Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) |
| 5967 | return false; |
| 5968 | return true; |
| 5969 | } |
| 5970 | |
| 5971 | const VarDecl *VD = cast<VarDecl>(D); |
| 5972 | assert(VD->isFileVarDecl() && "Expected file scoped var"); |
| 5973 | |
Argyrios Kyrtzidis | 6e03a74 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 5974 | if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly) |
| 5975 | return false; |
| 5976 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5977 | // Structs that have non-trivial constructors or destructors are required. |
| 5978 | |
| 5979 | // FIXME: Handle references. |
| 5980 | if (const RecordType *RT = VD->getType()->getAs<RecordType>()) { |
| 5981 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5982 | if (RD->hasDefinition() && |
| 5983 | (!RD->hasTrivialConstructor() || !RD->hasTrivialDestructor())) |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5984 | return true; |
| 5985 | } |
| 5986 | } |
| 5987 | |
| 5988 | GVALinkage L = GetGVALinkageForVariable(VD); |
| 5989 | if (L == GVA_Internal || L == GVA_TemplateInstantiation) { |
| 5990 | if (!(VD->getInit() && VD->getInit()->HasSideEffects(*this))) |
| 5991 | return false; |
| 5992 | } |
| 5993 | |
| 5994 | return true; |
| 5995 | } |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 5996 | |
Charles Davis | 99202b3 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 5997 | CallingConv ASTContext::getDefaultMethodCallConv() { |
| 5998 | // Pass through to the C++ ABI object |
| 5999 | return ABI->getDefaultMethodCallConv(); |
| 6000 | } |
| 6001 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6002 | bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const { |
Anders Carlsson | 60a6263 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 6003 | // Pass through to the C++ ABI object |
| 6004 | return ABI->isNearlyEmpty(RD); |
| 6005 | } |
| 6006 | |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 6007 | MangleContext *ASTContext::createMangleContext() { |
| 6008 | switch (Target.getCXXABI()) { |
| 6009 | case CXXABI_ARM: |
| 6010 | case CXXABI_Itanium: |
| 6011 | return createItaniumMangleContext(*this, getDiagnostics()); |
| 6012 | case CXXABI_Microsoft: |
| 6013 | return createMicrosoftMangleContext(*this, getDiagnostics()); |
| 6014 | } |
| 6015 | assert(0 && "Unsupported ABI"); |
| 6016 | return 0; |
| 6017 | } |
| 6018 | |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 6019 | CXXABI::~CXXABI() {} |