Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the ASTContext interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/ASTContext.h" |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 15 | #include "clang/AST/CharUnits.h" |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclCXX.h" |
Steve Naroff | 980e508 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclTemplate.h" |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeLoc.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 20 | #include "clang/AST/Expr.h" |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 21 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExternalASTSource.h" |
Argyrios Kyrtzidis | 7b90340 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 23 | #include "clang/AST/ASTMutationListener.h" |
Anders Carlsson | 19cc4ab | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 24 | #include "clang/AST/RecordLayout.h" |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 25 | #include "clang/AST/Mangle.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 26 | #include "clang/Basic/Builtins.h" |
Chris Lattner | a9376d4 | 2009-03-28 03:45:20 +0000 | [diff] [blame] | 27 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 28 | #include "clang/Basic/TargetInfo.h" |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/SmallString.h" |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/StringExtras.h" |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 31 | #include "llvm/Support/MathExtras.h" |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 32 | #include "llvm/Support/raw_ostream.h" |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 33 | #include "CXXABI.h" |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 34 | #include <map> |
Anders Carlsson | 29445a0 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 35 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 36 | using namespace clang; |
| 37 | |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 38 | unsigned ASTContext::NumImplicitDefaultConstructors; |
| 39 | unsigned ASTContext::NumImplicitDefaultConstructorsDeclared; |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 40 | unsigned ASTContext::NumImplicitCopyConstructors; |
| 41 | unsigned ASTContext::NumImplicitCopyConstructorsDeclared; |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 42 | unsigned ASTContext::NumImplicitMoveConstructors; |
| 43 | unsigned ASTContext::NumImplicitMoveConstructorsDeclared; |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 44 | unsigned ASTContext::NumImplicitCopyAssignmentOperators; |
| 45 | unsigned ASTContext::NumImplicitCopyAssignmentOperatorsDeclared; |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 46 | unsigned ASTContext::NumImplicitMoveAssignmentOperators; |
| 47 | unsigned ASTContext::NumImplicitMoveAssignmentOperatorsDeclared; |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 48 | unsigned ASTContext::NumImplicitDestructors; |
| 49 | unsigned ASTContext::NumImplicitDestructorsDeclared; |
| 50 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 51 | enum FloatingRank { |
| 52 | FloatRank, DoubleRank, LongDoubleRank |
| 53 | }; |
| 54 | |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 55 | void |
| 56 | ASTContext::CanonicalTemplateTemplateParm::Profile(llvm::FoldingSetNodeID &ID, |
| 57 | TemplateTemplateParmDecl *Parm) { |
| 58 | ID.AddInteger(Parm->getDepth()); |
| 59 | ID.AddInteger(Parm->getPosition()); |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 60 | ID.AddBoolean(Parm->isParameterPack()); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 61 | |
| 62 | TemplateParameterList *Params = Parm->getTemplateParameters(); |
| 63 | ID.AddInteger(Params->size()); |
| 64 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 65 | PEnd = Params->end(); |
| 66 | P != PEnd; ++P) { |
| 67 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
| 68 | ID.AddInteger(0); |
| 69 | ID.AddBoolean(TTP->isParameterPack()); |
| 70 | continue; |
| 71 | } |
| 72 | |
| 73 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 74 | ID.AddInteger(1); |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 75 | ID.AddBoolean(NTTP->isParameterPack()); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 76 | ID.AddPointer(NTTP->getType().getAsOpaquePtr()); |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 77 | if (NTTP->isExpandedParameterPack()) { |
| 78 | ID.AddBoolean(true); |
| 79 | ID.AddInteger(NTTP->getNumExpansionTypes()); |
| 80 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) |
| 81 | ID.AddPointer(NTTP->getExpansionType(I).getAsOpaquePtr()); |
| 82 | } else |
| 83 | ID.AddBoolean(false); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 84 | continue; |
| 85 | } |
| 86 | |
| 87 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); |
| 88 | ID.AddInteger(2); |
| 89 | Profile(ID, TTP); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | TemplateTemplateParmDecl * |
| 94 | ASTContext::getCanonicalTemplateTemplateParmDecl( |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 95 | TemplateTemplateParmDecl *TTP) const { |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 96 | // Check if we already have a canonical template template parameter. |
| 97 | llvm::FoldingSetNodeID ID; |
| 98 | CanonicalTemplateTemplateParm::Profile(ID, TTP); |
| 99 | void *InsertPos = 0; |
| 100 | CanonicalTemplateTemplateParm *Canonical |
| 101 | = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
| 102 | if (Canonical) |
| 103 | return Canonical->getParam(); |
| 104 | |
| 105 | // Build a canonical template parameter list. |
| 106 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 107 | llvm::SmallVector<NamedDecl *, 4> CanonParams; |
| 108 | CanonParams.reserve(Params->size()); |
| 109 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 110 | PEnd = Params->end(); |
| 111 | P != PEnd; ++P) { |
| 112 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) |
| 113 | CanonParams.push_back( |
| 114 | TemplateTypeParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 115 | SourceLocation(), |
| 116 | SourceLocation(), |
| 117 | TTP->getDepth(), |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 118 | TTP->getIndex(), 0, false, |
| 119 | TTP->isParameterPack())); |
| 120 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 121 | = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 122 | QualType T = getCanonicalType(NTTP->getType()); |
| 123 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T); |
| 124 | NonTypeTemplateParmDecl *Param; |
| 125 | if (NTTP->isExpandedParameterPack()) { |
| 126 | llvm::SmallVector<QualType, 2> ExpandedTypes; |
| 127 | llvm::SmallVector<TypeSourceInfo *, 2> ExpandedTInfos; |
| 128 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) { |
| 129 | ExpandedTypes.push_back(getCanonicalType(NTTP->getExpansionType(I))); |
| 130 | ExpandedTInfos.push_back( |
| 131 | getTrivialTypeSourceInfo(ExpandedTypes.back())); |
| 132 | } |
| 133 | |
| 134 | Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 135 | SourceLocation(), |
| 136 | SourceLocation(), |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 137 | NTTP->getDepth(), |
| 138 | NTTP->getPosition(), 0, |
| 139 | T, |
| 140 | TInfo, |
| 141 | ExpandedTypes.data(), |
| 142 | ExpandedTypes.size(), |
| 143 | ExpandedTInfos.data()); |
| 144 | } else { |
| 145 | Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 146 | SourceLocation(), |
| 147 | SourceLocation(), |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 148 | NTTP->getDepth(), |
| 149 | NTTP->getPosition(), 0, |
| 150 | T, |
| 151 | NTTP->isParameterPack(), |
| 152 | TInfo); |
| 153 | } |
| 154 | CanonParams.push_back(Param); |
| 155 | |
| 156 | } else |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 157 | CanonParams.push_back(getCanonicalTemplateTemplateParmDecl( |
| 158 | cast<TemplateTemplateParmDecl>(*P))); |
| 159 | } |
| 160 | |
| 161 | TemplateTemplateParmDecl *CanonTTP |
| 162 | = TemplateTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
| 163 | SourceLocation(), TTP->getDepth(), |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 164 | TTP->getPosition(), |
| 165 | TTP->isParameterPack(), |
| 166 | 0, |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 167 | TemplateParameterList::Create(*this, SourceLocation(), |
| 168 | SourceLocation(), |
| 169 | CanonParams.data(), |
| 170 | CanonParams.size(), |
| 171 | SourceLocation())); |
| 172 | |
| 173 | // Get the new insert position for the node we care about. |
| 174 | Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
| 175 | assert(Canonical == 0 && "Shouldn't be in the map!"); |
| 176 | (void)Canonical; |
| 177 | |
| 178 | // Create the canonical template template parameter entry. |
| 179 | Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP); |
| 180 | CanonTemplateTemplateParms.InsertNode(Canonical, InsertPos); |
| 181 | return CanonTTP; |
| 182 | } |
| 183 | |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 184 | CXXABI *ASTContext::createCXXABI(const TargetInfo &T) { |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 185 | if (!LangOpts.CPlusPlus) return 0; |
| 186 | |
Charles Davis | 20cf717 | 2010-08-19 02:18:14 +0000 | [diff] [blame] | 187 | switch (T.getCXXABI()) { |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 188 | case CXXABI_ARM: |
| 189 | return CreateARMCXXABI(*this); |
| 190 | case CXXABI_Itanium: |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 191 | return CreateItaniumCXXABI(*this); |
Charles Davis | 20cf717 | 2010-08-19 02:18:14 +0000 | [diff] [blame] | 192 | case CXXABI_Microsoft: |
| 193 | return CreateMicrosoftCXXABI(*this); |
| 194 | } |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 195 | return 0; |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 198 | static const LangAS::Map &getAddressSpaceMap(const TargetInfo &T, |
| 199 | const LangOptions &LOpts) { |
| 200 | if (LOpts.FakeAddressSpaceMap) { |
| 201 | // The fake address space map must have a distinct entry for each |
| 202 | // language-specific address space. |
| 203 | static const unsigned FakeAddrSpaceMap[] = { |
| 204 | 1, // opencl_global |
| 205 | 2, // opencl_local |
| 206 | 3 // opencl_constant |
| 207 | }; |
| 208 | return FakeAddrSpaceMap; |
| 209 | } else { |
| 210 | return T.getAddressSpaceMap(); |
| 211 | } |
| 212 | } |
| 213 | |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 214 | ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM, |
Daniel Dunbar | 444be73 | 2009-11-13 05:51:54 +0000 | [diff] [blame] | 215 | const TargetInfo &t, |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 216 | IdentifierTable &idents, SelectorTable &sels, |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 217 | Builtin::Context &builtins, |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 218 | unsigned size_reserve) : |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 219 | FunctionProtoTypes(this_()), |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 220 | TemplateSpecializationTypes(this_()), |
| 221 | DependentTemplateSpecializationTypes(this_()), |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 222 | SubstTemplateTemplateParmPacks(this_()), |
Argyrios Kyrtzidis | 0061138 | 2010-07-04 21:44:19 +0000 | [diff] [blame] | 223 | GlobalNestedNameSpecifier(0), IsInt128Installed(false), |
| 224 | CFConstantStringTypeDecl(0), NSConstantStringTypeDecl(0), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 225 | ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), |
| 226 | jmp_bufDecl(0), sigjmp_bufDecl(0), BlockDescriptorType(0), |
| 227 | BlockDescriptorExtendedType(0), cudaConfigureCallDecl(0), |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 228 | NullTypeSourceInfo(QualType()), |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 229 | SourceMgr(SM), LangOpts(LOpts), ABI(createCXXABI(t)), |
| 230 | AddrSpaceMap(getAddressSpaceMap(t, LOpts)), Target(t), |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 231 | Idents(idents), Selectors(sels), |
Ted Kremenek | ac9590e | 2010-05-10 20:40:08 +0000 | [diff] [blame] | 232 | BuiltinInfo(builtins), |
| 233 | DeclarationNames(*this), |
Argyrios Kyrtzidis | 9a44b5f | 2010-10-28 09:29:35 +0000 | [diff] [blame] | 234 | ExternalSource(0), Listener(0), PrintingPolicy(LOpts), |
Ted Kremenek | f057bf7 | 2010-06-18 00:31:04 +0000 | [diff] [blame] | 235 | LastSDM(0, 0), |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 236 | UniqueBlockByRefTypeID(0) { |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 237 | ObjCIdRedefinitionType = QualType(); |
| 238 | ObjCClassRedefinitionType = QualType(); |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 239 | ObjCSelRedefinitionType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 240 | if (size_reserve > 0) Types.reserve(size_reserve); |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 241 | TUDecl = TranslationUnitDecl::Create(*this); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 242 | InitBuiltinTypes(); |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 245 | ASTContext::~ASTContext() { |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 246 | // Release the DenseMaps associated with DeclContext objects. |
| 247 | // FIXME: Is this the ideal solution? |
| 248 | ReleaseDeclContextMaps(); |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 249 | |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 250 | // Call all of the deallocation functions. |
| 251 | for (unsigned I = 0, N = Deallocations.size(); I != N; ++I) |
| 252 | Deallocations[I].first(Deallocations[I].second); |
Douglas Gregor | 0054531 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 253 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 254 | // Release all of the memory associated with overridden C++ methods. |
| 255 | for (llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::iterator |
| 256 | OM = OverriddenMethods.begin(), OMEnd = OverriddenMethods.end(); |
| 257 | OM != OMEnd; ++OM) |
| 258 | OM->second.Destroy(); |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 259 | |
Ted Kremenek | dcfcfbe | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 260 | // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 261 | // because they can contain DenseMaps. |
| 262 | for (llvm::DenseMap<const ObjCContainerDecl*, |
| 263 | const ASTRecordLayout*>::iterator |
| 264 | I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) |
| 265 | // Increment in loop to prevent using deallocated memory. |
| 266 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 267 | R->Destroy(*this); |
| 268 | |
Ted Kremenek | dcfcfbe | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 269 | for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator |
| 270 | I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) { |
| 271 | // Increment in loop to prevent using deallocated memory. |
| 272 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 273 | R->Destroy(*this); |
| 274 | } |
Douglas Gregor | 6320064 | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 275 | |
| 276 | for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(), |
| 277 | AEnd = DeclAttrs.end(); |
| 278 | A != AEnd; ++A) |
| 279 | A->second->~AttrVec(); |
| 280 | } |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 281 | |
Douglas Gregor | 0054531 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 282 | void ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) { |
| 283 | Deallocations.push_back(std::make_pair(Callback, Data)); |
| 284 | } |
| 285 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 286 | void |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 287 | ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) { |
| 288 | ExternalSource.reset(Source.take()); |
| 289 | } |
| 290 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 291 | void ASTContext::PrintStats() const { |
| 292 | fprintf(stderr, "*** AST Context Stats:\n"); |
| 293 | fprintf(stderr, " %d types total.\n", (int)Types.size()); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 294 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 295 | unsigned counts[] = { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 296 | #define TYPE(Name, Parent) 0, |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 297 | #define ABSTRACT_TYPE(Name, Parent) |
| 298 | #include "clang/AST/TypeNodes.def" |
| 299 | 0 // Extra |
| 300 | }; |
Douglas Gregor | c2ee10d | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 301 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 302 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 303 | Type *T = Types[i]; |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 304 | counts[(unsigned)T->getTypeClass()]++; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 307 | unsigned Idx = 0; |
| 308 | unsigned TotalBytes = 0; |
| 309 | #define TYPE(Name, Parent) \ |
| 310 | if (counts[Idx]) \ |
| 311 | fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \ |
| 312 | TotalBytes += counts[Idx] * sizeof(Name##Type); \ |
| 313 | ++Idx; |
| 314 | #define ABSTRACT_TYPE(Name, Parent) |
| 315 | #include "clang/AST/TypeNodes.def" |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 316 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 317 | fprintf(stderr, "Total bytes = %d\n", int(TotalBytes)); |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 318 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 319 | // Implicit special member functions. |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 320 | fprintf(stderr, " %u/%u implicit default constructors created\n", |
| 321 | NumImplicitDefaultConstructorsDeclared, |
| 322 | NumImplicitDefaultConstructors); |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 323 | fprintf(stderr, " %u/%u implicit copy constructors created\n", |
| 324 | NumImplicitCopyConstructorsDeclared, |
| 325 | NumImplicitCopyConstructors); |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 326 | if (getLangOptions().CPlusPlus) |
| 327 | fprintf(stderr, " %u/%u implicit move constructors created\n", |
| 328 | NumImplicitMoveConstructorsDeclared, |
| 329 | NumImplicitMoveConstructors); |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 330 | fprintf(stderr, " %u/%u implicit copy assignment operators created\n", |
| 331 | NumImplicitCopyAssignmentOperatorsDeclared, |
| 332 | NumImplicitCopyAssignmentOperators); |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 333 | if (getLangOptions().CPlusPlus) |
| 334 | fprintf(stderr, " %u/%u implicit move assignment operators created\n", |
| 335 | NumImplicitMoveAssignmentOperatorsDeclared, |
| 336 | NumImplicitMoveAssignmentOperators); |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 337 | fprintf(stderr, " %u/%u implicit destructors created\n", |
| 338 | NumImplicitDestructorsDeclared, NumImplicitDestructors); |
| 339 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 340 | if (ExternalSource.get()) { |
| 341 | fprintf(stderr, "\n"); |
| 342 | ExternalSource->PrintStats(); |
| 343 | } |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 344 | |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 345 | BumpAlloc.PrintStats(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 349 | void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 350 | BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 351 | R = CanQualType::CreateUnsafe(QualType(Ty, 0)); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 352 | Types.push_back(Ty); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 355 | void ASTContext::InitBuiltinTypes() { |
| 356 | assert(VoidTy.isNull() && "Context reinitialized?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 357 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 358 | // C99 6.2.5p19. |
| 359 | InitBuiltinType(VoidTy, BuiltinType::Void); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 360 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 361 | // C99 6.2.5p2. |
| 362 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
| 363 | // C99 6.2.5p3. |
Eli Friedman | 15b9176 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 364 | if (LangOpts.CharIsSigned) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 365 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 366 | else |
| 367 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
| 368 | // C99 6.2.5p4. |
| 369 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 370 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 371 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 372 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 373 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 374 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 375 | // C99 6.2.5p6. |
| 376 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 377 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 378 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 379 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 380 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 381 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 382 | // C99 6.2.5p10. |
| 383 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 384 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 385 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 386 | |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 387 | // GNU extension, 128-bit integers. |
| 388 | InitBuiltinType(Int128Ty, BuiltinType::Int128); |
| 389 | InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128); |
| 390 | |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 391 | if (LangOpts.CPlusPlus) { // C++ 3.9.1p5 |
Eli Friedman | d3d77cd | 2011-04-30 19:24:24 +0000 | [diff] [blame] | 392 | if (TargetInfo::isTypeSigned(Target.getWCharType())) |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 393 | InitBuiltinType(WCharTy, BuiltinType::WChar_S); |
| 394 | else // -fshort-wchar makes wchar_t be unsigned. |
| 395 | InitBuiltinType(WCharTy, BuiltinType::WChar_U); |
| 396 | } else // C99 |
Chris Lattner | 3a25032 | 2009-02-26 23:43:47 +0000 | [diff] [blame] | 397 | WCharTy = getFromTargetType(Target.getWCharType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 398 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 399 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 400 | InitBuiltinType(Char16Ty, BuiltinType::Char16); |
| 401 | else // C99 |
| 402 | Char16Ty = getFromTargetType(Target.getChar16Type()); |
| 403 | |
| 404 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 405 | InitBuiltinType(Char32Ty, BuiltinType::Char32); |
| 406 | else // C99 |
| 407 | Char32Ty = getFromTargetType(Target.getChar32Type()); |
| 408 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 409 | // Placeholder type for type-dependent expressions whose type is |
| 410 | // completely unknown. No code should ever check a type against |
| 411 | // DependentTy and users should never see it; however, it is here to |
| 412 | // help diagnose failures to properly check for type-dependent |
| 413 | // expressions. |
| 414 | InitBuiltinType(DependentTy, BuiltinType::Dependent); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 415 | |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 416 | // Placeholder type for functions. |
| 417 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
| 418 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 419 | // Placeholder type for bound members. |
| 420 | InitBuiltinType(BoundMemberTy, BuiltinType::BoundMember); |
| 421 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 422 | // "any" type; useful for debugger-like clients. |
| 423 | InitBuiltinType(UnknownAnyTy, BuiltinType::UnknownAny); |
| 424 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 425 | // C99 6.2.5p11. |
| 426 | FloatComplexTy = getComplexType(FloatTy); |
| 427 | DoubleComplexTy = getComplexType(DoubleTy); |
| 428 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 429 | |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 430 | BuiltinVaListType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 431 | |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 432 | // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope(). |
| 433 | ObjCIdTypedefType = QualType(); |
| 434 | ObjCClassTypedefType = QualType(); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 435 | ObjCSelTypedefType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 436 | |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 437 | // Builtin types for 'id', 'Class', and 'SEL'. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 438 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); |
| 439 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 440 | InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 441 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 442 | ObjCConstantStringType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 443 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 444 | // void * type |
| 445 | VoidPtrTy = getPointerType(VoidTy); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 446 | |
| 447 | // nullptr type (C++0x 2.14.7) |
| 448 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 451 | Diagnostic &ASTContext::getDiagnostics() const { |
| 452 | return SourceMgr.getDiagnostics(); |
| 453 | } |
| 454 | |
Douglas Gregor | 6320064 | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 455 | AttrVec& ASTContext::getDeclAttrs(const Decl *D) { |
| 456 | AttrVec *&Result = DeclAttrs[D]; |
| 457 | if (!Result) { |
| 458 | void *Mem = Allocate(sizeof(AttrVec)); |
| 459 | Result = new (Mem) AttrVec; |
| 460 | } |
| 461 | |
| 462 | return *Result; |
| 463 | } |
| 464 | |
| 465 | /// \brief Erase the attributes corresponding to the given declaration. |
| 466 | void ASTContext::eraseDeclAttrs(const Decl *D) { |
| 467 | llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D); |
| 468 | if (Pos != DeclAttrs.end()) { |
| 469 | Pos->second->~AttrVec(); |
| 470 | DeclAttrs.erase(Pos); |
| 471 | } |
| 472 | } |
| 473 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 474 | MemberSpecializationInfo * |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 475 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 476 | assert(Var->isStaticDataMember() && "Not a static data member"); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 477 | llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 478 | = InstantiatedFromStaticDataMember.find(Var); |
| 479 | if (Pos == InstantiatedFromStaticDataMember.end()) |
| 480 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 481 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 482 | return Pos->second; |
| 483 | } |
| 484 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 485 | void |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 486 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, |
Argyrios Kyrtzidis | 9421adc | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 487 | TemplateSpecializationKind TSK, |
| 488 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 489 | assert(Inst->isStaticDataMember() && "Not a static data member"); |
| 490 | assert(Tmpl->isStaticDataMember() && "Not a static data member"); |
| 491 | assert(!InstantiatedFromStaticDataMember[Inst] && |
| 492 | "Already noted what static data member was instantiated from"); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 493 | InstantiatedFromStaticDataMember[Inst] |
Argyrios Kyrtzidis | 9421adc | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 494 | = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 495 | } |
| 496 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 497 | NamedDecl * |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 498 | ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 499 | llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 500 | = InstantiatedFromUsingDecl.find(UUD); |
| 501 | if (Pos == InstantiatedFromUsingDecl.end()) |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 502 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 503 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 504 | return Pos->second; |
| 505 | } |
| 506 | |
| 507 | void |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 508 | ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) { |
| 509 | assert((isa<UsingDecl>(Pattern) || |
| 510 | isa<UnresolvedUsingValueDecl>(Pattern) || |
| 511 | isa<UnresolvedUsingTypenameDecl>(Pattern)) && |
| 512 | "pattern decl is not a using decl"); |
| 513 | assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists"); |
| 514 | InstantiatedFromUsingDecl[Inst] = Pattern; |
| 515 | } |
| 516 | |
| 517 | UsingShadowDecl * |
| 518 | ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) { |
| 519 | llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos |
| 520 | = InstantiatedFromUsingShadowDecl.find(Inst); |
| 521 | if (Pos == InstantiatedFromUsingShadowDecl.end()) |
| 522 | return 0; |
| 523 | |
| 524 | return Pos->second; |
| 525 | } |
| 526 | |
| 527 | void |
| 528 | ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, |
| 529 | UsingShadowDecl *Pattern) { |
| 530 | assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists"); |
| 531 | InstantiatedFromUsingShadowDecl[Inst] = Pattern; |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 534 | FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { |
| 535 | llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos |
| 536 | = InstantiatedFromUnnamedFieldDecl.find(Field); |
| 537 | if (Pos == InstantiatedFromUnnamedFieldDecl.end()) |
| 538 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 539 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 540 | return Pos->second; |
| 541 | } |
| 542 | |
| 543 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, |
| 544 | FieldDecl *Tmpl) { |
| 545 | assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed"); |
| 546 | assert(!Tmpl->getDeclName() && "Template field decl is not unnamed"); |
| 547 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] && |
| 548 | "Already noted what unnamed field was instantiated from"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 549 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 550 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; |
| 551 | } |
| 552 | |
Fariborz Jahanian | 14d56ef | 2011-04-27 17:14:21 +0000 | [diff] [blame] | 553 | bool ASTContext::ZeroBitfieldFollowsNonBitfield(const FieldDecl *FD, |
| 554 | const FieldDecl *LastFD) const { |
| 555 | return (FD->isBitField() && LastFD && !LastFD->isBitField() && |
| 556 | FD->getBitWidth()-> EvaluateAsInt(*this).getZExtValue() == 0); |
| 557 | |
| 558 | } |
| 559 | |
Fariborz Jahanian | 340fa24 | 2011-05-02 17:20:56 +0000 | [diff] [blame] | 560 | bool ASTContext::ZeroBitfieldFollowsBitfield(const FieldDecl *FD, |
| 561 | const FieldDecl *LastFD) const { |
| 562 | return (FD->isBitField() && LastFD && LastFD->isBitField() && |
Fariborz Jahanian | cc0f9f1 | 2011-05-03 22:07:14 +0000 | [diff] [blame] | 563 | FD->getBitWidth()-> EvaluateAsInt(*this).getZExtValue() == 0 && |
| 564 | LastFD->getBitWidth()-> EvaluateAsInt(*this).getZExtValue() != 0); |
Fariborz Jahanian | 340fa24 | 2011-05-02 17:20:56 +0000 | [diff] [blame] | 565 | |
| 566 | } |
| 567 | |
Fariborz Jahanian | 9b3acaa | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 568 | bool ASTContext::BitfieldFollowsBitfield(const FieldDecl *FD, |
| 569 | const FieldDecl *LastFD) const { |
| 570 | return (FD->isBitField() && LastFD && LastFD->isBitField() && |
| 571 | FD->getBitWidth()-> EvaluateAsInt(*this).getZExtValue() && |
| 572 | LastFD->getBitWidth()-> EvaluateAsInt(*this).getZExtValue()); |
| 573 | } |
| 574 | |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 575 | bool ASTContext::NoneBitfieldFollowsBitfield(const FieldDecl *FD, |
| 576 | const FieldDecl *LastFD) const { |
| 577 | return (!FD->isBitField() && LastFD && LastFD->isBitField() && |
| 578 | LastFD->getBitWidth()-> EvaluateAsInt(*this).getZExtValue()); |
| 579 | } |
| 580 | |
| 581 | bool ASTContext::BitfieldFollowsNoneBitfield(const FieldDecl *FD, |
| 582 | const FieldDecl *LastFD) const { |
| 583 | return (FD->isBitField() && LastFD && !LastFD->isBitField() && |
| 584 | FD->getBitWidth()-> EvaluateAsInt(*this).getZExtValue()); |
| 585 | } |
| 586 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 587 | ASTContext::overridden_cxx_method_iterator |
| 588 | ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const { |
| 589 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 590 | = OverriddenMethods.find(Method); |
| 591 | if (Pos == OverriddenMethods.end()) |
| 592 | return 0; |
| 593 | |
| 594 | return Pos->second.begin(); |
| 595 | } |
| 596 | |
| 597 | ASTContext::overridden_cxx_method_iterator |
| 598 | ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const { |
| 599 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 600 | = OverriddenMethods.find(Method); |
| 601 | if (Pos == OverriddenMethods.end()) |
| 602 | return 0; |
| 603 | |
| 604 | return Pos->second.end(); |
| 605 | } |
| 606 | |
Argyrios Kyrtzidis | c91e9f4 | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 607 | unsigned |
| 608 | ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const { |
| 609 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 610 | = OverriddenMethods.find(Method); |
| 611 | if (Pos == OverriddenMethods.end()) |
| 612 | return 0; |
| 613 | |
| 614 | return Pos->second.size(); |
| 615 | } |
| 616 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 617 | void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method, |
| 618 | const CXXMethodDecl *Overridden) { |
| 619 | OverriddenMethods[Method].push_back(Overridden); |
| 620 | } |
| 621 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 622 | //===----------------------------------------------------------------------===// |
| 623 | // Type Sizing and Analysis |
| 624 | //===----------------------------------------------------------------------===// |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 625 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 626 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 627 | /// scalar floating point type. |
| 628 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 629 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 630 | assert(BT && "Not a floating point type!"); |
| 631 | switch (BT->getKind()) { |
| 632 | default: assert(0 && "Not a floating point type!"); |
| 633 | case BuiltinType::Float: return Target.getFloatFormat(); |
| 634 | case BuiltinType::Double: return Target.getDoubleFormat(); |
| 635 | case BuiltinType::LongDouble: return Target.getLongDoubleFormat(); |
| 636 | } |
| 637 | } |
| 638 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 639 | /// getDeclAlign - Return a conservative estimate of the alignment of the |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 640 | /// specified decl. Note that bitfields do not have a valid alignment, so |
| 641 | /// this method will assert on them. |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 642 | /// If @p RefAsPointee, references are treated like their underlying type |
| 643 | /// (for alignof), else they're treated like pointers (for CodeGen). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 644 | CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) const { |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 645 | unsigned Align = Target.getCharWidth(); |
| 646 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 647 | bool UseAlignAttrOnly = false; |
| 648 | if (unsigned AlignFromAttr = D->getMaxAlignment()) { |
| 649 | Align = AlignFromAttr; |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 650 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 651 | // __attribute__((aligned)) can increase or decrease alignment |
| 652 | // *except* on a struct or struct member, where it only increases |
| 653 | // alignment unless 'packed' is also specified. |
| 654 | // |
| 655 | // It is an error for [[align]] to decrease alignment, so we can |
| 656 | // ignore that possibility; Sema should diagnose it. |
| 657 | if (isa<FieldDecl>(D)) { |
| 658 | UseAlignAttrOnly = D->hasAttr<PackedAttr>() || |
| 659 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
| 660 | } else { |
| 661 | UseAlignAttrOnly = true; |
| 662 | } |
| 663 | } |
Fariborz Jahanian | 78a7d7d | 2011-05-05 21:19:14 +0000 | [diff] [blame] | 664 | else if (isa<FieldDecl>(D)) |
| 665 | UseAlignAttrOnly = |
| 666 | D->hasAttr<PackedAttr>() || |
| 667 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 668 | |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 669 | // If we're using the align attribute only, just ignore everything |
| 670 | // else about the declaration and its type. |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 671 | if (UseAlignAttrOnly) { |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 672 | // do nothing |
| 673 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 674 | } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 675 | QualType T = VD->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 676 | if (const ReferenceType* RT = T->getAs<ReferenceType>()) { |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 677 | if (RefAsPointee) |
| 678 | T = RT->getPointeeType(); |
| 679 | else |
| 680 | T = getPointerType(RT->getPointeeType()); |
| 681 | } |
| 682 | if (!T->isIncompleteType() && !T->isFunctionType()) { |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 683 | // Adjust alignments of declarations with array type by the |
| 684 | // large-array alignment on the target. |
Rafael Espindola | 6deecb0 | 2010-06-04 23:15:27 +0000 | [diff] [blame] | 685 | unsigned MinWidth = Target.getLargeArrayMinWidth(); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 686 | const ArrayType *arrayType; |
| 687 | if (MinWidth && (arrayType = getAsArrayType(T))) { |
| 688 | if (isa<VariableArrayType>(arrayType)) |
| 689 | Align = std::max(Align, Target.getLargeArrayAlign()); |
| 690 | else if (isa<ConstantArrayType>(arrayType) && |
| 691 | MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType))) |
| 692 | Align = std::max(Align, Target.getLargeArrayAlign()); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 693 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 694 | // Walk through any array types while we're at it. |
| 695 | T = getBaseElementType(arrayType); |
| 696 | } |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 697 | Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); |
| 698 | } |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 699 | |
| 700 | // Fields can be subject to extra alignment constraints, like if |
| 701 | // the field is packed, the struct is packed, or the struct has a |
| 702 | // a max-field-alignment constraint (#pragma pack). So calculate |
| 703 | // the actual alignment of the field within the struct, and then |
| 704 | // (as we're expected to) constrain that by the alignment of the type. |
| 705 | if (const FieldDecl *field = dyn_cast<FieldDecl>(VD)) { |
| 706 | // So calculate the alignment of the field. |
| 707 | const ASTRecordLayout &layout = getASTRecordLayout(field->getParent()); |
| 708 | |
| 709 | // Start with the record's overall alignment. |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 710 | unsigned fieldAlign = toBits(layout.getAlignment()); |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 711 | |
| 712 | // Use the GCD of that and the offset within the record. |
| 713 | uint64_t offset = layout.getFieldOffset(field->getFieldIndex()); |
| 714 | if (offset > 0) { |
| 715 | // Alignment is always a power of 2, so the GCD will be a power of 2, |
| 716 | // which means we get to do this crazy thing instead of Euclid's. |
| 717 | uint64_t lowBitOfOffset = offset & (~offset + 1); |
| 718 | if (lowBitOfOffset < fieldAlign) |
| 719 | fieldAlign = static_cast<unsigned>(lowBitOfOffset); |
| 720 | } |
| 721 | |
| 722 | Align = std::min(Align, fieldAlign); |
Charles Davis | 05f6247 | 2010-02-23 04:52:00 +0000 | [diff] [blame] | 723 | } |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 724 | } |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 725 | |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 726 | return toCharUnitsFromBits(Align); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 727 | } |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 728 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 729 | std::pair<CharUnits, CharUnits> |
Ken Dyck | bee5a79 | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 730 | ASTContext::getTypeInfoInChars(const Type *T) const { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 731 | std::pair<uint64_t, unsigned> Info = getTypeInfo(T); |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 732 | return std::make_pair(toCharUnitsFromBits(Info.first), |
| 733 | toCharUnitsFromBits(Info.second)); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | std::pair<CharUnits, CharUnits> |
Ken Dyck | bee5a79 | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 737 | ASTContext::getTypeInfoInChars(QualType T) const { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 738 | return getTypeInfoInChars(T.getTypePtr()); |
| 739 | } |
| 740 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 741 | /// getTypeSize - Return the size of the specified type, in bits. This method |
| 742 | /// does not work on incomplete types. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 743 | /// |
| 744 | /// FIXME: Pointers into different addr spaces could have different sizes and |
| 745 | /// alignment requirements: getPointerInfo should take an AddrSpace, this |
| 746 | /// should take a QualType, &c. |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 747 | std::pair<uint64_t, unsigned> |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 748 | ASTContext::getTypeInfo(const Type *T) const { |
Mike Stump | 5e30100 | 2009-02-27 18:32:39 +0000 | [diff] [blame] | 749 | uint64_t Width=0; |
| 750 | unsigned Align=8; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 751 | switch (T->getTypeClass()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 752 | #define TYPE(Class, Base) |
| 753 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 754 | #define NON_CANONICAL_TYPE(Class, Base) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 755 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 756 | #include "clang/AST/TypeNodes.def" |
John McCall | d3d49bb | 2011-06-28 16:49:23 +0000 | [diff] [blame] | 757 | llvm_unreachable("Should not see dependent types"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 758 | break; |
| 759 | |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 760 | case Type::FunctionNoProto: |
| 761 | case Type::FunctionProto: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 762 | // GCC extension: alignof(function) = 32 bits |
| 763 | Width = 0; |
| 764 | Align = 32; |
| 765 | break; |
| 766 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 767 | case Type::IncompleteArray: |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 768 | case Type::VariableArray: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 769 | Width = 0; |
| 770 | Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); |
| 771 | break; |
| 772 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 773 | case Type::ConstantArray: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 774 | const ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 775 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 776 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 777 | Width = EltInfo.first*CAT->getSize().getZExtValue(); |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 778 | Align = EltInfo.second; |
Argyrios Kyrtzidis | cd88b41 | 2011-04-26 21:05:39 +0000 | [diff] [blame] | 779 | Width = llvm::RoundUpToAlignment(Width, Align); |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 780 | break; |
Christopher Lamb | 5c09a02 | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 781 | } |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 782 | case Type::ExtVector: |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 783 | case Type::Vector: { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 784 | const VectorType *VT = cast<VectorType>(T); |
| 785 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType()); |
| 786 | Width = EltInfo.first*VT->getNumElements(); |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 787 | Align = Width; |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 788 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 789 | // This happens for non-power-of-2 length vectors. |
Dan Gohman | 8eefcd3 | 2010-04-21 23:32:43 +0000 | [diff] [blame] | 790 | if (Align & (Align-1)) { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 791 | Align = llvm::NextPowerOf2(Align); |
| 792 | Width = llvm::RoundUpToAlignment(Width, Align); |
| 793 | } |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 794 | break; |
| 795 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 796 | |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 797 | case Type::Builtin: |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 798 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 799 | default: assert(0 && "Unknown builtin type!"); |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 800 | case BuiltinType::Void: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 801 | // GCC extension: alignof(void) = 8 bits. |
| 802 | Width = 0; |
| 803 | Align = 8; |
| 804 | break; |
| 805 | |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 806 | case BuiltinType::Bool: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 807 | Width = Target.getBoolWidth(); |
| 808 | Align = Target.getBoolAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 809 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 810 | case BuiltinType::Char_S: |
| 811 | case BuiltinType::Char_U: |
| 812 | case BuiltinType::UChar: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 813 | case BuiltinType::SChar: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 814 | Width = Target.getCharWidth(); |
| 815 | Align = Target.getCharAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 816 | break; |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 817 | case BuiltinType::WChar_S: |
| 818 | case BuiltinType::WChar_U: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 819 | Width = Target.getWCharWidth(); |
| 820 | Align = Target.getWCharAlign(); |
| 821 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 822 | case BuiltinType::Char16: |
| 823 | Width = Target.getChar16Width(); |
| 824 | Align = Target.getChar16Align(); |
| 825 | break; |
| 826 | case BuiltinType::Char32: |
| 827 | Width = Target.getChar32Width(); |
| 828 | Align = Target.getChar32Align(); |
| 829 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 830 | case BuiltinType::UShort: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 831 | case BuiltinType::Short: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 832 | Width = Target.getShortWidth(); |
| 833 | Align = Target.getShortAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 834 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 835 | case BuiltinType::UInt: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 836 | case BuiltinType::Int: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 837 | Width = Target.getIntWidth(); |
| 838 | Align = Target.getIntAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 839 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 840 | case BuiltinType::ULong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 841 | case BuiltinType::Long: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 842 | Width = Target.getLongWidth(); |
| 843 | Align = Target.getLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 844 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 845 | case BuiltinType::ULongLong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 846 | case BuiltinType::LongLong: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 847 | Width = Target.getLongLongWidth(); |
| 848 | Align = Target.getLongLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 849 | break; |
Chris Lattner | ec16cb9 | 2009-04-30 02:55:13 +0000 | [diff] [blame] | 850 | case BuiltinType::Int128: |
| 851 | case BuiltinType::UInt128: |
| 852 | Width = 128; |
| 853 | Align = 128; // int128_t is 128-bit aligned on all targets. |
| 854 | break; |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 855 | case BuiltinType::Float: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 856 | Width = Target.getFloatWidth(); |
| 857 | Align = Target.getFloatAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 858 | break; |
| 859 | case BuiltinType::Double: |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 860 | Width = Target.getDoubleWidth(); |
| 861 | Align = Target.getDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 862 | break; |
| 863 | case BuiltinType::LongDouble: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 864 | Width = Target.getLongDoubleWidth(); |
| 865 | Align = Target.getLongDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 866 | break; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 867 | case BuiltinType::NullPtr: |
| 868 | Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) |
| 869 | Align = Target.getPointerAlign(0); // == sizeof(void*) |
Sebastian Redl | 1590d9c | 2009-05-27 19:34:06 +0000 | [diff] [blame] | 870 | break; |
Fariborz Jahanian | e04f5fc | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 871 | case BuiltinType::ObjCId: |
| 872 | case BuiltinType::ObjCClass: |
| 873 | case BuiltinType::ObjCSel: |
| 874 | Width = Target.getPointerWidth(0); |
| 875 | Align = Target.getPointerAlign(0); |
| 876 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 877 | } |
Chris Lattner | bfef6d7 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 878 | break; |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 879 | case Type::ObjCObjectPointer: |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 880 | Width = Target.getPointerWidth(0); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 881 | Align = Target.getPointerAlign(0); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 882 | break; |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 883 | case Type::BlockPointer: { |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 884 | unsigned AS = getTargetAddressSpace( |
| 885 | cast<BlockPointerType>(T)->getPointeeType()); |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 886 | Width = Target.getPointerWidth(AS); |
| 887 | Align = Target.getPointerAlign(AS); |
| 888 | break; |
| 889 | } |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 890 | case Type::LValueReference: |
| 891 | case Type::RValueReference: { |
| 892 | // alignof and sizeof should never enter this code path here, so we go |
| 893 | // the pointer route. |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 894 | unsigned AS = getTargetAddressSpace( |
| 895 | cast<ReferenceType>(T)->getPointeeType()); |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 896 | Width = Target.getPointerWidth(AS); |
| 897 | Align = Target.getPointerAlign(AS); |
| 898 | break; |
| 899 | } |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 900 | case Type::Pointer: { |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 901 | unsigned AS = getTargetAddressSpace(cast<PointerType>(T)->getPointeeType()); |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 902 | Width = Target.getPointerWidth(AS); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 903 | Align = Target.getPointerAlign(AS); |
| 904 | break; |
| 905 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 906 | case Type::MemberPointer: { |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 907 | const MemberPointerType *MPT = cast<MemberPointerType>(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 908 | std::pair<uint64_t, unsigned> PtrDiffInfo = |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 909 | getTypeInfo(getPointerDiffType()); |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 910 | Width = PtrDiffInfo.first * ABI->getMemberPointerSize(MPT); |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 911 | Align = PtrDiffInfo.second; |
| 912 | break; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 913 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 914 | case Type::Complex: { |
| 915 | // Complex types have the same alignment as their elements, but twice the |
| 916 | // size. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 917 | std::pair<uint64_t, unsigned> EltInfo = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 918 | getTypeInfo(cast<ComplexType>(T)->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 919 | Width = EltInfo.first*2; |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 920 | Align = EltInfo.second; |
| 921 | break; |
| 922 | } |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 923 | case Type::ObjCObject: |
| 924 | return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr()); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 925 | case Type::ObjCInterface: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 926 | const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 927 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 928 | Width = toBits(Layout.getSize()); |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 929 | Align = toBits(Layout.getAlignment()); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 930 | break; |
| 931 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 932 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 933 | case Type::Enum: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 934 | const TagType *TT = cast<TagType>(T); |
| 935 | |
| 936 | if (TT->getDecl()->isInvalidDecl()) { |
Douglas Gregor | 22ce41d | 2011-04-20 17:29:44 +0000 | [diff] [blame] | 937 | Width = 8; |
| 938 | Align = 8; |
Chris Lattner | 8389eab | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 939 | break; |
| 940 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 941 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 942 | if (const EnumType *ET = dyn_cast<EnumType>(TT)) |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 943 | return getTypeInfo(ET->getDecl()->getIntegerType()); |
| 944 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 945 | const RecordType *RT = cast<RecordType>(TT); |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 946 | const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 947 | Width = toBits(Layout.getSize()); |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 948 | Align = toBits(Layout.getAlignment()); |
Chris Lattner | dc0d73e | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 949 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 950 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 951 | |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 952 | case Type::SubstTemplateTypeParm: |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 953 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> |
| 954 | getReplacementType().getTypePtr()); |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 955 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 956 | case Type::Auto: { |
| 957 | const AutoType *A = cast<AutoType>(T); |
| 958 | assert(A->isDeduced() && "Cannot request the size of a dependent type"); |
Matt Beaumont-Gay | dc856af | 2011-02-22 20:00:16 +0000 | [diff] [blame] | 959 | return getTypeInfo(A->getDeducedType().getTypePtr()); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 960 | } |
| 961 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 962 | case Type::Paren: |
| 963 | return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr()); |
| 964 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 965 | case Type::Typedef: { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 966 | const TypedefNameDecl *Typedef = cast<TypedefType>(T)->getDecl(); |
Douglas Gregor | df1367a | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 967 | std::pair<uint64_t, unsigned> Info |
| 968 | = getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); |
Chris Lattner | c1de52d | 2011-02-19 22:55:41 +0000 | [diff] [blame] | 969 | // If the typedef has an aligned attribute on it, it overrides any computed |
| 970 | // alignment we have. This violates the GCC documentation (which says that |
| 971 | // attribute(aligned) can only round up) but matches its implementation. |
| 972 | if (unsigned AttrAlign = Typedef->getMaxAlignment()) |
| 973 | Align = AttrAlign; |
| 974 | else |
| 975 | Align = Info.second; |
Douglas Gregor | df1367a | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 976 | Width = Info.first; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 977 | break; |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 978 | } |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 979 | |
| 980 | case Type::TypeOfExpr: |
| 981 | return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType() |
| 982 | .getTypePtr()); |
| 983 | |
| 984 | case Type::TypeOf: |
| 985 | return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr()); |
| 986 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 987 | case Type::Decltype: |
| 988 | return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType() |
| 989 | .getTypePtr()); |
| 990 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 991 | case Type::UnaryTransform: |
| 992 | return getTypeInfo(cast<UnaryTransformType>(T)->getUnderlyingType()); |
| 993 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 994 | case Type::Elaborated: |
| 995 | return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 996 | |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 997 | case Type::Attributed: |
| 998 | return getTypeInfo( |
| 999 | cast<AttributedType>(T)->getEquivalentType().getTypePtr()); |
| 1000 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1001 | case Type::TemplateSpecialization: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1002 | assert(getCanonicalType(T) != T && |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1003 | "Cannot request the size of a dependent type"); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1004 | const TemplateSpecializationType *TST = cast<TemplateSpecializationType>(T); |
| 1005 | // A type alias template specialization may refer to a typedef with the |
| 1006 | // aligned attribute on it. |
| 1007 | if (TST->isTypeAlias()) |
| 1008 | return getTypeInfo(TST->getAliasedType().getTypePtr()); |
| 1009 | else |
| 1010 | return getTypeInfo(getCanonicalType(T)); |
| 1011 | } |
| 1012 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1013 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1014 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 1015 | assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2"); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1016 | return std::make_pair(Width, Align); |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1019 | /// toCharUnitsFromBits - Convert a size in bits to a size in characters. |
| 1020 | CharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const { |
| 1021 | return CharUnits::fromQuantity(BitSize / getCharWidth()); |
| 1022 | } |
| 1023 | |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1024 | /// toBits - Convert a size in characters to a size in characters. |
| 1025 | int64_t ASTContext::toBits(CharUnits CharSize) const { |
| 1026 | return CharSize.getQuantity() * getCharWidth(); |
| 1027 | } |
| 1028 | |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1029 | /// getTypeSizeInChars - Return the size of the specified type, in characters. |
| 1030 | /// This method does not work on incomplete types. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1031 | CharUnits ASTContext::getTypeSizeInChars(QualType T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1032 | return toCharUnitsFromBits(getTypeSize(T)); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1033 | } |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1034 | CharUnits ASTContext::getTypeSizeInChars(const Type *T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1035 | return toCharUnitsFromBits(getTypeSize(T)); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
Ken Dyck | 16e20cc | 2010-01-26 17:25:18 +0000 | [diff] [blame] | 1038 | /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1039 | /// characters. This method does not work on incomplete types. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1040 | CharUnits ASTContext::getTypeAlignInChars(QualType T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1041 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1042 | } |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1043 | CharUnits ASTContext::getTypeAlignInChars(const Type *T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1044 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1047 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 1048 | /// type for the current target in bits. This can be different than the ABI |
| 1049 | /// alignment in cases where it is beneficial for performance to overalign |
| 1050 | /// a data type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1051 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) const { |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1052 | unsigned ABIAlign = getTypeAlign(T); |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1053 | |
| 1054 | // Double and long long should be naturally aligned if possible. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1055 | if (const ComplexType* CT = T->getAs<ComplexType>()) |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1056 | T = CT->getElementType().getTypePtr(); |
| 1057 | if (T->isSpecificBuiltinType(BuiltinType::Double) || |
| 1058 | T->isSpecificBuiltinType(BuiltinType::LongLong)) |
| 1059 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); |
| 1060 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1061 | return ABIAlign; |
| 1062 | } |
| 1063 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 1064 | /// ShallowCollectObjCIvars - |
| 1065 | /// Collect all ivars, including those synthesized, in the current class. |
| 1066 | /// |
| 1067 | void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1068 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) const { |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1069 | // FIXME. This need be removed but there are two many places which |
| 1070 | // assume const-ness of ObjCInterfaceDecl |
| 1071 | ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI); |
| 1072 | for (ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv; |
| 1073 | Iv= Iv->getNextIvar()) |
| 1074 | Ivars.push_back(Iv); |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1077 | /// DeepCollectObjCIvars - |
| 1078 | /// This routine first collects all declared, but not synthesized, ivars in |
| 1079 | /// super class and then collects all ivars, including those synthesized for |
| 1080 | /// current class. This routine is used for implementation of current class |
| 1081 | /// when all ivars, declared and synthesized are known. |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 1082 | /// |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1083 | void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 1084 | bool leafClass, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1085 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) const { |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1086 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
| 1087 | DeepCollectObjCIvars(SuperClass, false, Ivars); |
| 1088 | if (!leafClass) { |
| 1089 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 1090 | E = OI->ivar_end(); I != E; ++I) |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 1091 | Ivars.push_back(*I); |
| 1092 | } |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 1093 | else { |
| 1094 | ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI); |
| 1095 | for (ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv; |
| 1096 | Iv= Iv->getNextIvar()) |
| 1097 | Ivars.push_back(Iv); |
| 1098 | } |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1101 | /// CollectInheritedProtocols - Collect all protocols in current class and |
| 1102 | /// those inherited by it. |
| 1103 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 1104 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1105 | if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1106 | // We can use protocol_iterator here instead of |
| 1107 | // all_referenced_protocol_iterator since we are walking all categories. |
| 1108 | for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(), |
| 1109 | PE = OI->all_referenced_protocol_end(); P != PE; ++P) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1110 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 1111 | Protocols.insert(Proto); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1112 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 1113 | PE = Proto->protocol_end(); P != PE; ++P) { |
| 1114 | Protocols.insert(*P); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1115 | CollectInheritedProtocols(*P, Protocols); |
| 1116 | } |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 1117 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1118 | |
| 1119 | // Categories of this Interface. |
| 1120 | for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList(); |
| 1121 | CDeclChain; CDeclChain = CDeclChain->getNextClassCategory()) |
| 1122 | CollectInheritedProtocols(CDeclChain, Protocols); |
| 1123 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) |
| 1124 | while (SD) { |
| 1125 | CollectInheritedProtocols(SD, Protocols); |
| 1126 | SD = SD->getSuperClass(); |
| 1127 | } |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1128 | } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1129 | for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(), |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1130 | PE = OC->protocol_end(); P != PE; ++P) { |
| 1131 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 1132 | Protocols.insert(Proto); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1133 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 1134 | PE = Proto->protocol_end(); P != PE; ++P) |
| 1135 | CollectInheritedProtocols(*P, Protocols); |
| 1136 | } |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1137 | } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1138 | for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(), |
| 1139 | PE = OP->protocol_end(); P != PE; ++P) { |
| 1140 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 1141 | Protocols.insert(Proto); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1142 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 1143 | PE = Proto->protocol_end(); P != PE; ++P) |
| 1144 | CollectInheritedProtocols(*P, Protocols); |
| 1145 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1146 | } |
| 1147 | } |
| 1148 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1149 | unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const { |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1150 | unsigned count = 0; |
| 1151 | // Count ivars declared in class extension. |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 1152 | for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl; |
| 1153 | CDecl = CDecl->getNextClassExtension()) |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1154 | count += CDecl->ivar_size(); |
| 1155 | |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1156 | // Count ivar defined in this class's implementation. This |
| 1157 | // includes synthesized ivars. |
| 1158 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1159 | count += ImplDecl->ivar_size(); |
| 1160 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 1161 | return count; |
| 1162 | } |
| 1163 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1164 | /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. |
| 1165 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
| 1166 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1167 | I = ObjCImpls.find(D); |
| 1168 | if (I != ObjCImpls.end()) |
| 1169 | return cast<ObjCImplementationDecl>(I->second); |
| 1170 | return 0; |
| 1171 | } |
| 1172 | /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. |
| 1173 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
| 1174 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1175 | I = ObjCImpls.find(D); |
| 1176 | if (I != ObjCImpls.end()) |
| 1177 | return cast<ObjCCategoryImplDecl>(I->second); |
| 1178 | return 0; |
| 1179 | } |
| 1180 | |
| 1181 | /// \brief Set the implementation of ObjCInterfaceDecl. |
| 1182 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
| 1183 | ObjCImplementationDecl *ImplD) { |
| 1184 | assert(IFaceD && ImplD && "Passed null params"); |
| 1185 | ObjCImpls[IFaceD] = ImplD; |
| 1186 | } |
| 1187 | /// \brief Set the implementation of ObjCCategoryDecl. |
| 1188 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
| 1189 | ObjCCategoryImplDecl *ImplD) { |
| 1190 | assert(CatD && ImplD && "Passed null params"); |
| 1191 | ObjCImpls[CatD] = ImplD; |
| 1192 | } |
| 1193 | |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1194 | /// \brief Get the copy initialization expression of VarDecl,or NULL if |
| 1195 | /// none exists. |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1196 | Expr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) { |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1197 | assert(VD && "Passed null params"); |
| 1198 | assert(VD->hasAttr<BlocksAttr>() && |
| 1199 | "getBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1200 | llvm::DenseMap<const VarDecl*, Expr*>::iterator |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1201 | I = BlockVarCopyInits.find(VD); |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1202 | return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0; |
| 1203 | } |
| 1204 | |
| 1205 | /// \brief Set the copy inialization expression of a block var decl. |
| 1206 | void ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) { |
| 1207 | assert(VD && Init && "Passed null params"); |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1208 | assert(VD->hasAttr<BlocksAttr>() && |
| 1209 | "setBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1210 | BlockVarCopyInits[VD] = Init; |
| 1211 | } |
| 1212 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1213 | /// \brief Allocate an uninitialized TypeSourceInfo. |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1214 | /// |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1215 | /// The caller should initialize the memory held by TypeSourceInfo using |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1216 | /// the TypeLoc wrappers. |
| 1217 | /// |
| 1218 | /// \param T the type that will be the basis for type source info. This type |
| 1219 | /// should refer to how the declarator was written in source code, not to |
| 1220 | /// what type semantic analysis resolved the declarator to. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1221 | TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1222 | unsigned DataSize) const { |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1223 | if (!DataSize) |
| 1224 | DataSize = TypeLoc::getFullDataSizeForType(T); |
| 1225 | else |
| 1226 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1227 | "incorrect data size provided to CreateTypeSourceInfo!"); |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1228 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1229 | TypeSourceInfo *TInfo = |
| 1230 | (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8); |
| 1231 | new (TInfo) TypeSourceInfo(T); |
| 1232 | return TInfo; |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1233 | } |
| 1234 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1235 | TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1236 | SourceLocation L) const { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1237 | TypeSourceInfo *DI = CreateTypeSourceInfo(T); |
Douglas Gregor | c21c7e9 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 1238 | DI->getTypeLoc().initialize(const_cast<ASTContext &>(*this), L); |
John McCall | a4eb74d | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 1239 | return DI; |
| 1240 | } |
| 1241 | |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1242 | const ASTRecordLayout & |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1243 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const { |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1244 | return getObjCLayout(D, 0); |
| 1245 | } |
| 1246 | |
| 1247 | const ASTRecordLayout & |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1248 | ASTContext::getASTObjCImplementationLayout( |
| 1249 | const ObjCImplementationDecl *D) const { |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1250 | return getObjCLayout(D->getClassInterface(), D); |
| 1251 | } |
| 1252 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1253 | //===----------------------------------------------------------------------===// |
| 1254 | // Type creation/memoization methods |
| 1255 | //===----------------------------------------------------------------------===// |
| 1256 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1257 | QualType |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1258 | ASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const { |
| 1259 | unsigned fastQuals = quals.getFastQualifiers(); |
| 1260 | quals.removeFastQualifiers(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1261 | |
| 1262 | // Check if we've already instantiated this type. |
| 1263 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1264 | ExtQuals::Profile(ID, baseType, quals); |
| 1265 | void *insertPos = 0; |
| 1266 | if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, insertPos)) { |
| 1267 | assert(eq->getQualifiers() == quals); |
| 1268 | return QualType(eq, fastQuals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1271 | // If the base type is not canonical, make the appropriate canonical type. |
| 1272 | QualType canon; |
| 1273 | if (!baseType->isCanonicalUnqualified()) { |
| 1274 | SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split(); |
| 1275 | canonSplit.second.addConsistentQualifiers(quals); |
| 1276 | canon = getExtQualType(canonSplit.first, canonSplit.second); |
| 1277 | |
| 1278 | // Re-find the insert position. |
| 1279 | (void) ExtQualNodes.FindNodeOrInsertPos(ID, insertPos); |
| 1280 | } |
| 1281 | |
| 1282 | ExtQuals *eq = new (*this, TypeAlignment) ExtQuals(baseType, canon, quals); |
| 1283 | ExtQualNodes.InsertNode(eq, insertPos); |
| 1284 | return QualType(eq, fastQuals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1287 | QualType |
| 1288 | ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) const { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1289 | QualType CanT = getCanonicalType(T); |
| 1290 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1291 | return T; |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1292 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1293 | // If we are composing extended qualifiers together, merge together |
| 1294 | // into one ExtQuals node. |
| 1295 | QualifierCollector Quals; |
| 1296 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1297 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1298 | // If this type already has an address space specified, it cannot get |
| 1299 | // another one. |
| 1300 | assert(!Quals.hasAddressSpace() && |
| 1301 | "Type cannot be in multiple addr spaces!"); |
| 1302 | Quals.addAddressSpace(AddressSpace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1303 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1304 | return getExtQualType(TypeNode, Quals); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1305 | } |
| 1306 | |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1307 | QualType ASTContext::getObjCGCQualType(QualType T, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1308 | Qualifiers::GC GCAttr) const { |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1309 | QualType CanT = getCanonicalType(T); |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1310 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1311 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1312 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1313 | if (const PointerType *ptr = T->getAs<PointerType>()) { |
| 1314 | QualType Pointee = ptr->getPointeeType(); |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 1315 | if (Pointee->isAnyPointerType()) { |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1316 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 1317 | return getPointerType(ResultType); |
| 1318 | } |
| 1319 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1320 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1321 | // If we are composing extended qualifiers together, merge together |
| 1322 | // into one ExtQuals node. |
| 1323 | QualifierCollector Quals; |
| 1324 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1325 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1326 | // If this type already has an ObjCGC specified, it cannot get |
| 1327 | // another one. |
| 1328 | assert(!Quals.hasObjCGCAttr() && |
| 1329 | "Type cannot have multiple ObjCGCs!"); |
| 1330 | Quals.addObjCGCAttr(GCAttr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1331 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1332 | return getExtQualType(TypeNode, Quals); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1333 | } |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1334 | |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 1335 | const FunctionType *ASTContext::adjustFunctionType(const FunctionType *T, |
| 1336 | FunctionType::ExtInfo Info) { |
| 1337 | if (T->getExtInfo() == Info) |
| 1338 | return T; |
| 1339 | |
| 1340 | QualType Result; |
| 1341 | if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) { |
| 1342 | Result = getFunctionNoProtoType(FNPT->getResultType(), Info); |
| 1343 | } else { |
| 1344 | const FunctionProtoType *FPT = cast<FunctionProtoType>(T); |
| 1345 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 1346 | EPI.ExtInfo = Info; |
| 1347 | Result = getFunctionType(FPT->getResultType(), FPT->arg_type_begin(), |
| 1348 | FPT->getNumArgs(), EPI); |
| 1349 | } |
| 1350 | |
| 1351 | return cast<FunctionType>(Result.getTypePtr()); |
| 1352 | } |
| 1353 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1354 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 1355 | /// number with the specified element type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1356 | QualType ASTContext::getComplexType(QualType T) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1357 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1358 | // structure. |
| 1359 | llvm::FoldingSetNodeID ID; |
| 1360 | ComplexType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1361 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1362 | void *InsertPos = 0; |
| 1363 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1364 | return QualType(CT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1365 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1366 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1367 | // so fill in the canonical type field. |
| 1368 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1369 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1370 | Canonical = getComplexType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1371 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1372 | // Get the new insert position for the node we care about. |
| 1373 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1374 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1375 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1376 | ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1377 | Types.push_back(New); |
| 1378 | ComplexTypes.InsertNode(New, InsertPos); |
| 1379 | return QualType(New, 0); |
| 1380 | } |
| 1381 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1382 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 1383 | /// the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1384 | QualType ASTContext::getPointerType(QualType T) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1385 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1386 | // structure. |
| 1387 | llvm::FoldingSetNodeID ID; |
| 1388 | PointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1389 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1390 | void *InsertPos = 0; |
| 1391 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1392 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1393 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1394 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1395 | // so fill in the canonical type field. |
| 1396 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1397 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1398 | Canonical = getPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1399 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1400 | // Get the new insert position for the node we care about. |
| 1401 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1402 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1403 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1404 | PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1405 | Types.push_back(New); |
| 1406 | PointerTypes.InsertNode(New, InsertPos); |
| 1407 | return QualType(New, 0); |
| 1408 | } |
| 1409 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1410 | /// getBlockPointerType - Return the uniqued reference to the type for |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1411 | /// a pointer to the specified block. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1412 | QualType ASTContext::getBlockPointerType(QualType T) const { |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1413 | assert(T->isFunctionType() && "block of function types only"); |
| 1414 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1415 | // structure. |
| 1416 | llvm::FoldingSetNodeID ID; |
| 1417 | BlockPointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1418 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1419 | void *InsertPos = 0; |
| 1420 | if (BlockPointerType *PT = |
| 1421 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1422 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1423 | |
| 1424 | // If the block pointee type isn't canonical, this won't be a canonical |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1425 | // type either so fill in the canonical type field. |
| 1426 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1427 | if (!T.isCanonical()) { |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1428 | Canonical = getBlockPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1429 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1430 | // Get the new insert position for the node we care about. |
| 1431 | BlockPointerType *NewIP = |
| 1432 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1433 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1434 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1435 | BlockPointerType *New |
| 1436 | = new (*this, TypeAlignment) BlockPointerType(T, Canonical); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1437 | Types.push_back(New); |
| 1438 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 1439 | return QualType(New, 0); |
| 1440 | } |
| 1441 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1442 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 1443 | /// lvalue reference to the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1444 | QualType |
| 1445 | ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const { |
Douglas Gregor | 9625e44 | 2011-05-21 22:16:50 +0000 | [diff] [blame] | 1446 | assert(getCanonicalType(T) != OverloadTy && |
| 1447 | "Unresolved overloaded function type"); |
| 1448 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1449 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1450 | // structure. |
| 1451 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1452 | ReferenceType::Profile(ID, T, SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1453 | |
| 1454 | void *InsertPos = 0; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1455 | if (LValueReferenceType *RT = |
| 1456 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1457 | return QualType(RT, 0); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1458 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1459 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1460 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1461 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1462 | // either, so fill in the canonical type field. |
| 1463 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1464 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
| 1465 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1466 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1467 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1468 | // Get the new insert position for the node we care about. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1469 | LValueReferenceType *NewIP = |
| 1470 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1471 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1474 | LValueReferenceType *New |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1475 | = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, |
| 1476 | SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1477 | Types.push_back(New); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1478 | LValueReferenceTypes.InsertNode(New, InsertPos); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1479 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1480 | return QualType(New, 0); |
| 1481 | } |
| 1482 | |
| 1483 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 1484 | /// rvalue reference to the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1485 | QualType ASTContext::getRValueReferenceType(QualType T) const { |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1486 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1487 | // structure. |
| 1488 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1489 | ReferenceType::Profile(ID, T, false); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1490 | |
| 1491 | void *InsertPos = 0; |
| 1492 | if (RValueReferenceType *RT = |
| 1493 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1494 | return QualType(RT, 0); |
| 1495 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1496 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1497 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1498 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1499 | // either, so fill in the canonical type field. |
| 1500 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1501 | if (InnerRef || !T.isCanonical()) { |
| 1502 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1503 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1504 | |
| 1505 | // Get the new insert position for the node we care about. |
| 1506 | RValueReferenceType *NewIP = |
| 1507 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1508 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1511 | RValueReferenceType *New |
| 1512 | = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1513 | Types.push_back(New); |
| 1514 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1515 | return QualType(New, 0); |
| 1516 | } |
| 1517 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1518 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 1519 | /// member pointer to the specified type, in the specified class. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1520 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1521 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1522 | // structure. |
| 1523 | llvm::FoldingSetNodeID ID; |
| 1524 | MemberPointerType::Profile(ID, T, Cls); |
| 1525 | |
| 1526 | void *InsertPos = 0; |
| 1527 | if (MemberPointerType *PT = |
| 1528 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1529 | return QualType(PT, 0); |
| 1530 | |
| 1531 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 1532 | // type either, so fill in the canonical type field. |
| 1533 | QualType Canonical; |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1534 | if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1535 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 1536 | |
| 1537 | // Get the new insert position for the node we care about. |
| 1538 | MemberPointerType *NewIP = |
| 1539 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1540 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1541 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1542 | MemberPointerType *New |
| 1543 | = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1544 | Types.push_back(New); |
| 1545 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 1546 | return QualType(New, 0); |
| 1547 | } |
| 1548 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1549 | /// getConstantArrayType - Return the unique reference to the type for an |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1550 | /// array of the specified element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1551 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1552 | const llvm::APInt &ArySizeIn, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1553 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1554 | unsigned IndexTypeQuals) const { |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1555 | assert((EltTy->isDependentType() || |
| 1556 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && |
Eli Friedman | 587cbdf | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1557 | "Constant array of VLAs is illegal!"); |
| 1558 | |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1559 | // Convert the array size into a canonical width matching the pointer size for |
| 1560 | // the target. |
| 1561 | llvm::APInt ArySize(ArySizeIn); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1562 | ArySize = |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1563 | ArySize.zextOrTrunc(Target.getPointerWidth(getTargetAddressSpace(EltTy))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1564 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1565 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1566 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, IndexTypeQuals); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1567 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1568 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1569 | if (ConstantArrayType *ATP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1570 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1571 | return QualType(ATP, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1572 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1573 | // If the element type isn't canonical or has qualifiers, this won't |
| 1574 | // be a canonical type either, so fill in the canonical type field. |
| 1575 | QualType Canon; |
| 1576 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 1577 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
| 1578 | Canon = getConstantArrayType(QualType(canonSplit.first, 0), ArySize, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1579 | ASM, IndexTypeQuals); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1580 | Canon = getQualifiedType(Canon, canonSplit.second); |
| 1581 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1582 | // Get the new insert position for the node we care about. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1583 | ConstantArrayType *NewIP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1584 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1585 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1586 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1587 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1588 | ConstantArrayType *New = new(*this,TypeAlignment) |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1589 | ConstantArrayType(EltTy, Canon, ArySize, ASM, IndexTypeQuals); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1590 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1591 | Types.push_back(New); |
| 1592 | return QualType(New, 0); |
| 1593 | } |
| 1594 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1595 | /// getVariableArrayDecayedType - Turns the given type, which may be |
| 1596 | /// variably-modified, into the corresponding type with all the known |
| 1597 | /// sizes replaced with [*]. |
| 1598 | QualType ASTContext::getVariableArrayDecayedType(QualType type) const { |
| 1599 | // Vastly most common case. |
| 1600 | if (!type->isVariablyModifiedType()) return type; |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1601 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1602 | QualType result; |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1603 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1604 | SplitQualType split = type.getSplitDesugaredType(); |
| 1605 | const Type *ty = split.first; |
| 1606 | switch (ty->getTypeClass()) { |
| 1607 | #define TYPE(Class, Base) |
| 1608 | #define ABSTRACT_TYPE(Class, Base) |
| 1609 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 1610 | #include "clang/AST/TypeNodes.def" |
| 1611 | llvm_unreachable("didn't desugar past all non-canonical types?"); |
| 1612 | |
| 1613 | // These types should never be variably-modified. |
| 1614 | case Type::Builtin: |
| 1615 | case Type::Complex: |
| 1616 | case Type::Vector: |
| 1617 | case Type::ExtVector: |
| 1618 | case Type::DependentSizedExtVector: |
| 1619 | case Type::ObjCObject: |
| 1620 | case Type::ObjCInterface: |
| 1621 | case Type::ObjCObjectPointer: |
| 1622 | case Type::Record: |
| 1623 | case Type::Enum: |
| 1624 | case Type::UnresolvedUsing: |
| 1625 | case Type::TypeOfExpr: |
| 1626 | case Type::TypeOf: |
| 1627 | case Type::Decltype: |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1628 | case Type::UnaryTransform: |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1629 | case Type::DependentName: |
| 1630 | case Type::InjectedClassName: |
| 1631 | case Type::TemplateSpecialization: |
| 1632 | case Type::DependentTemplateSpecialization: |
| 1633 | case Type::TemplateTypeParm: |
| 1634 | case Type::SubstTemplateTypeParmPack: |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1635 | case Type::Auto: |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1636 | case Type::PackExpansion: |
| 1637 | llvm_unreachable("type should never be variably-modified"); |
| 1638 | |
| 1639 | // These types can be variably-modified but should never need to |
| 1640 | // further decay. |
| 1641 | case Type::FunctionNoProto: |
| 1642 | case Type::FunctionProto: |
| 1643 | case Type::BlockPointer: |
| 1644 | case Type::MemberPointer: |
| 1645 | return type; |
| 1646 | |
| 1647 | // These types can be variably-modified. All these modifications |
| 1648 | // preserve structure except as noted by comments. |
| 1649 | // TODO: if we ever care about optimizing VLAs, there are no-op |
| 1650 | // optimizations available here. |
| 1651 | case Type::Pointer: |
| 1652 | result = getPointerType(getVariableArrayDecayedType( |
| 1653 | cast<PointerType>(ty)->getPointeeType())); |
| 1654 | break; |
| 1655 | |
| 1656 | case Type::LValueReference: { |
| 1657 | const LValueReferenceType *lv = cast<LValueReferenceType>(ty); |
| 1658 | result = getLValueReferenceType( |
| 1659 | getVariableArrayDecayedType(lv->getPointeeType()), |
| 1660 | lv->isSpelledAsLValue()); |
| 1661 | break; |
| 1662 | } |
| 1663 | |
| 1664 | case Type::RValueReference: { |
| 1665 | const RValueReferenceType *lv = cast<RValueReferenceType>(ty); |
| 1666 | result = getRValueReferenceType( |
| 1667 | getVariableArrayDecayedType(lv->getPointeeType())); |
| 1668 | break; |
| 1669 | } |
| 1670 | |
| 1671 | case Type::ConstantArray: { |
| 1672 | const ConstantArrayType *cat = cast<ConstantArrayType>(ty); |
| 1673 | result = getConstantArrayType( |
| 1674 | getVariableArrayDecayedType(cat->getElementType()), |
| 1675 | cat->getSize(), |
| 1676 | cat->getSizeModifier(), |
| 1677 | cat->getIndexTypeCVRQualifiers()); |
| 1678 | break; |
| 1679 | } |
| 1680 | |
| 1681 | case Type::DependentSizedArray: { |
| 1682 | const DependentSizedArrayType *dat = cast<DependentSizedArrayType>(ty); |
| 1683 | result = getDependentSizedArrayType( |
| 1684 | getVariableArrayDecayedType(dat->getElementType()), |
| 1685 | dat->getSizeExpr(), |
| 1686 | dat->getSizeModifier(), |
| 1687 | dat->getIndexTypeCVRQualifiers(), |
| 1688 | dat->getBracketsRange()); |
| 1689 | break; |
| 1690 | } |
| 1691 | |
| 1692 | // Turn incomplete types into [*] types. |
| 1693 | case Type::IncompleteArray: { |
| 1694 | const IncompleteArrayType *iat = cast<IncompleteArrayType>(ty); |
| 1695 | result = getVariableArrayType( |
| 1696 | getVariableArrayDecayedType(iat->getElementType()), |
| 1697 | /*size*/ 0, |
| 1698 | ArrayType::Normal, |
| 1699 | iat->getIndexTypeCVRQualifiers(), |
| 1700 | SourceRange()); |
| 1701 | break; |
| 1702 | } |
| 1703 | |
| 1704 | // Turn VLA types into [*] types. |
| 1705 | case Type::VariableArray: { |
| 1706 | const VariableArrayType *vat = cast<VariableArrayType>(ty); |
| 1707 | result = getVariableArrayType( |
| 1708 | getVariableArrayDecayedType(vat->getElementType()), |
| 1709 | /*size*/ 0, |
| 1710 | ArrayType::Star, |
| 1711 | vat->getIndexTypeCVRQualifiers(), |
| 1712 | vat->getBracketsRange()); |
| 1713 | break; |
| 1714 | } |
| 1715 | } |
| 1716 | |
| 1717 | // Apply the top-level qualifiers from the original. |
| 1718 | return getQualifiedType(result, split.second); |
| 1719 | } |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1720 | |
Steve Naroff | bdbf7b0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 1721 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 1722 | /// variable array of the specified element type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1723 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 1724 | Expr *NumElts, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1725 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1726 | unsigned IndexTypeQuals, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1727 | SourceRange Brackets) const { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1728 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 1729 | // that have an expression provided for their size. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1730 | QualType Canon; |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 1731 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1732 | // Be sure to pull qualifiers off the element type. |
| 1733 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 1734 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
| 1735 | Canon = getVariableArrayType(QualType(canonSplit.first, 0), NumElts, ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1736 | IndexTypeQuals, Brackets); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1737 | Canon = getQualifiedType(Canon, canonSplit.second); |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 1738 | } |
| 1739 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1740 | VariableArrayType *New = new(*this, TypeAlignment) |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1741 | VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1742 | |
| 1743 | VariableArrayTypes.push_back(New); |
| 1744 | Types.push_back(New); |
| 1745 | return QualType(New, 0); |
| 1746 | } |
| 1747 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1748 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 1749 | /// the type for a dependently-sized array of the specified element |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1750 | /// type. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1751 | QualType ASTContext::getDependentSizedArrayType(QualType elementType, |
| 1752 | Expr *numElements, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1753 | ArrayType::ArraySizeModifier ASM, |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1754 | unsigned elementTypeQuals, |
| 1755 | SourceRange brackets) const { |
| 1756 | assert((!numElements || numElements->isTypeDependent() || |
| 1757 | numElements->isValueDependent()) && |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1758 | "Size must be type- or value-dependent!"); |
| 1759 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1760 | // Dependently-sized array types that do not have a specified number |
| 1761 | // of elements will have their sizes deduced from a dependent |
| 1762 | // initializer. We do no canonicalization here at all, which is okay |
| 1763 | // because they can't be used in most locations. |
| 1764 | if (!numElements) { |
| 1765 | DependentSizedArrayType *newType |
| 1766 | = new (*this, TypeAlignment) |
| 1767 | DependentSizedArrayType(*this, elementType, QualType(), |
| 1768 | numElements, ASM, elementTypeQuals, |
| 1769 | brackets); |
| 1770 | Types.push_back(newType); |
| 1771 | return QualType(newType, 0); |
| 1772 | } |
| 1773 | |
| 1774 | // Otherwise, we actually build a new type every time, but we |
| 1775 | // also build a canonical type. |
| 1776 | |
| 1777 | SplitQualType canonElementType = getCanonicalType(elementType).split(); |
| 1778 | |
| 1779 | void *insertPos = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1780 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1781 | DependentSizedArrayType::Profile(ID, *this, |
| 1782 | QualType(canonElementType.first, 0), |
| 1783 | ASM, elementTypeQuals, numElements); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1784 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1785 | // Look for an existing type with these properties. |
| 1786 | DependentSizedArrayType *canonTy = |
| 1787 | DependentSizedArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1788 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1789 | // If we don't have one, build one. |
| 1790 | if (!canonTy) { |
| 1791 | canonTy = new (*this, TypeAlignment) |
| 1792 | DependentSizedArrayType(*this, QualType(canonElementType.first, 0), |
| 1793 | QualType(), numElements, ASM, elementTypeQuals, |
| 1794 | brackets); |
| 1795 | DependentSizedArrayTypes.InsertNode(canonTy, insertPos); |
| 1796 | Types.push_back(canonTy); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1799 | // Apply qualifiers from the element type to the array. |
| 1800 | QualType canon = getQualifiedType(QualType(canonTy,0), |
| 1801 | canonElementType.second); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1802 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1803 | // If we didn't need extra canonicalization for the element type, |
| 1804 | // then just use that as our result. |
| 1805 | if (QualType(canonElementType.first, 0) == elementType) |
| 1806 | return canon; |
| 1807 | |
| 1808 | // Otherwise, we need to build a type which follows the spelling |
| 1809 | // of the element type. |
| 1810 | DependentSizedArrayType *sugaredType |
| 1811 | = new (*this, TypeAlignment) |
| 1812 | DependentSizedArrayType(*this, elementType, canon, numElements, |
| 1813 | ASM, elementTypeQuals, brackets); |
| 1814 | Types.push_back(sugaredType); |
| 1815 | return QualType(sugaredType, 0); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1818 | QualType ASTContext::getIncompleteArrayType(QualType elementType, |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1819 | ArrayType::ArraySizeModifier ASM, |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1820 | unsigned elementTypeQuals) const { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1821 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1822 | IncompleteArrayType::Profile(ID, elementType, ASM, elementTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1823 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1824 | void *insertPos = 0; |
| 1825 | if (IncompleteArrayType *iat = |
| 1826 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos)) |
| 1827 | return QualType(iat, 0); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1828 | |
| 1829 | // If the element type isn't canonical, this won't be a canonical type |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1830 | // either, so fill in the canonical type field. We also have to pull |
| 1831 | // qualifiers off the element type. |
| 1832 | QualType canon; |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1833 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1834 | if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) { |
| 1835 | SplitQualType canonSplit = getCanonicalType(elementType).split(); |
| 1836 | canon = getIncompleteArrayType(QualType(canonSplit.first, 0), |
| 1837 | ASM, elementTypeQuals); |
| 1838 | canon = getQualifiedType(canon, canonSplit.second); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1839 | |
| 1840 | // Get the new insert position for the node we care about. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1841 | IncompleteArrayType *existing = |
| 1842 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
| 1843 | assert(!existing && "Shouldn't be in the map!"); (void) existing; |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1844 | } |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1845 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1846 | IncompleteArrayType *newType = new (*this, TypeAlignment) |
| 1847 | IncompleteArrayType(elementType, canon, ASM, elementTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1848 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1849 | IncompleteArrayTypes.InsertNode(newType, insertPos); |
| 1850 | Types.push_back(newType); |
| 1851 | return QualType(newType, 0); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1852 | } |
| 1853 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1854 | /// getVectorType - Return the unique reference to a vector type of |
| 1855 | /// the specified element type and size. VectorType must be a built-in type. |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1856 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1857 | VectorType::VectorKind VecKind) const { |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1858 | assert(vecType->isBuiltinType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1859 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1860 | // Check if we've already instantiated a vector of this type. |
| 1861 | llvm::FoldingSetNodeID ID; |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1862 | VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind); |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1863 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1864 | void *InsertPos = 0; |
| 1865 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1866 | return QualType(VTP, 0); |
| 1867 | |
| 1868 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1869 | // so fill in the canonical type field. |
| 1870 | QualType Canonical; |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1871 | if (!vecType.isCanonical()) { |
Bob Wilson | 231da7e | 2010-11-16 00:32:20 +0000 | [diff] [blame] | 1872 | Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1873 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1874 | // Get the new insert position for the node we care about. |
| 1875 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1876 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1877 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1878 | VectorType *New = new (*this, TypeAlignment) |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1879 | VectorType(vecType, NumElts, Canonical, VecKind); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1880 | VectorTypes.InsertNode(New, InsertPos); |
| 1881 | Types.push_back(New); |
| 1882 | return QualType(New, 0); |
| 1883 | } |
| 1884 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1885 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1886 | /// the specified element type and size. VectorType must be a built-in type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1887 | QualType |
| 1888 | ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) const { |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1889 | assert(vecType->isBuiltinType() || vecType->isDependentType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1890 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1891 | // Check if we've already instantiated a vector of this type. |
| 1892 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1893 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1894 | VectorType::GenericVector); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1895 | void *InsertPos = 0; |
| 1896 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1897 | return QualType(VTP, 0); |
| 1898 | |
| 1899 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1900 | // so fill in the canonical type field. |
| 1901 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1902 | if (!vecType.isCanonical()) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1903 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1904 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1905 | // Get the new insert position for the node we care about. |
| 1906 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1907 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1908 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1909 | ExtVectorType *New = new (*this, TypeAlignment) |
| 1910 | ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1911 | VectorTypes.InsertNode(New, InsertPos); |
| 1912 | Types.push_back(New); |
| 1913 | return QualType(New, 0); |
| 1914 | } |
| 1915 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1916 | QualType |
| 1917 | ASTContext::getDependentSizedExtVectorType(QualType vecType, |
| 1918 | Expr *SizeExpr, |
| 1919 | SourceLocation AttrLoc) const { |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1920 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1921 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1922 | SizeExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1923 | |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1924 | void *InsertPos = 0; |
| 1925 | DependentSizedExtVectorType *Canon |
| 1926 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1927 | DependentSizedExtVectorType *New; |
| 1928 | if (Canon) { |
| 1929 | // We already have a canonical version of this array type; use it as |
| 1930 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1931 | New = new (*this, TypeAlignment) |
| 1932 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), |
| 1933 | SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1934 | } else { |
| 1935 | QualType CanonVecTy = getCanonicalType(vecType); |
| 1936 | if (CanonVecTy == vecType) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1937 | New = new (*this, TypeAlignment) |
| 1938 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, |
| 1939 | AttrLoc); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1940 | |
| 1941 | DependentSizedExtVectorType *CanonCheck |
| 1942 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1943 | assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken"); |
| 1944 | (void)CanonCheck; |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1945 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); |
| 1946 | } else { |
| 1947 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 1948 | SourceLocation()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1949 | New = new (*this, TypeAlignment) |
| 1950 | DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1951 | } |
| 1952 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1953 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1954 | Types.push_back(New); |
| 1955 | return QualType(New, 0); |
| 1956 | } |
| 1957 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1958 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1959 | /// |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1960 | QualType |
| 1961 | ASTContext::getFunctionNoProtoType(QualType ResultTy, |
| 1962 | const FunctionType::ExtInfo &Info) const { |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 1963 | const CallingConv DefaultCC = Info.getCC(); |
| 1964 | const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ? |
| 1965 | CC_X86StdCall : DefaultCC; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1966 | // Unique functions, to guarantee there is only one function of a particular |
| 1967 | // structure. |
| 1968 | llvm::FoldingSetNodeID ID; |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1969 | FunctionNoProtoType::Profile(ID, ResultTy, Info); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1970 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1971 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1972 | if (FunctionNoProtoType *FT = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1973 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1974 | return QualType(FT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1975 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1976 | QualType Canonical; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1977 | if (!ResultTy.isCanonical() || |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1978 | getCanonicalCallConv(CallConv) != CallConv) { |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1979 | Canonical = |
| 1980 | getFunctionNoProtoType(getCanonicalType(ResultTy), |
| 1981 | Info.withCallingConv(getCanonicalCallConv(CallConv))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1982 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1983 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1984 | FunctionNoProtoType *NewIP = |
| 1985 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1986 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1987 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1988 | |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 1989 | FunctionProtoType::ExtInfo newInfo = Info.withCallingConv(CallConv); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1990 | FunctionNoProtoType *New = new (*this, TypeAlignment) |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 1991 | FunctionNoProtoType(ResultTy, Canonical, newInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1992 | Types.push_back(New); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1993 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1994 | return QualType(New, 0); |
| 1995 | } |
| 1996 | |
| 1997 | /// getFunctionType - Return a normal function type with a typed argument |
| 1998 | /// list. isVariadic indicates whether the argument list includes '...'. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1999 | QualType |
| 2000 | ASTContext::getFunctionType(QualType ResultTy, |
| 2001 | const QualType *ArgArray, unsigned NumArgs, |
| 2002 | const FunctionProtoType::ExtProtoInfo &EPI) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2003 | // Unique functions, to guarantee there is only one function of a particular |
| 2004 | // structure. |
| 2005 | llvm::FoldingSetNodeID ID; |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2006 | FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, EPI, *this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2007 | |
| 2008 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2009 | if (FunctionProtoType *FTP = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2010 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2011 | return QualType(FTP, 0); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2012 | |
| 2013 | // Determine whether the type being created is already canonical or not. |
Sebastian Redl | 8b5b409 | 2011-03-06 10:52:04 +0000 | [diff] [blame] | 2014 | bool isCanonical= EPI.ExceptionSpecType == EST_None && ResultTy.isCanonical(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2015 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2016 | if (!ArgArray[i].isCanonicalAsParam()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2017 | isCanonical = false; |
| 2018 | |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2019 | const CallingConv DefaultCC = EPI.ExtInfo.getCC(); |
| 2020 | const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ? |
| 2021 | CC_X86StdCall : DefaultCC; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2022 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2023 | // If this type isn't canonical, get the canonical version of it. |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2024 | // The exception spec is not part of the canonical type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2025 | QualType Canonical; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2026 | if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2027 | llvm::SmallVector<QualType, 16> CanonicalArgs; |
| 2028 | CanonicalArgs.reserve(NumArgs); |
| 2029 | for (unsigned i = 0; i != NumArgs; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2030 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2031 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2032 | FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI; |
Sebastian Redl | 8b5b409 | 2011-03-06 10:52:04 +0000 | [diff] [blame] | 2033 | CanonicalEPI.ExceptionSpecType = EST_None; |
| 2034 | CanonicalEPI.NumExceptions = 0; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2035 | CanonicalEPI.ExtInfo |
| 2036 | = CanonicalEPI.ExtInfo.withCallingConv(getCanonicalCallConv(CallConv)); |
| 2037 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2038 | Canonical = getFunctionType(getCanonicalType(ResultTy), |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2039 | CanonicalArgs.data(), NumArgs, |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2040 | CanonicalEPI); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2041 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2042 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2043 | FunctionProtoType *NewIP = |
| 2044 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2045 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2046 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2047 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2048 | // FunctionProtoType objects are allocated with extra bytes after |
| 2049 | // them for three variable size arrays at the end: |
| 2050 | // - parameter types |
| 2051 | // - exception types |
| 2052 | // - consumed-arguments flags |
| 2053 | // Instead of the exception types, there could be a noexcept |
| 2054 | // expression. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2055 | size_t Size = sizeof(FunctionProtoType) + |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2056 | NumArgs * sizeof(QualType); |
| 2057 | if (EPI.ExceptionSpecType == EST_Dynamic) |
| 2058 | Size += EPI.NumExceptions * sizeof(QualType); |
| 2059 | else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) { |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2060 | Size += sizeof(Expr*); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2061 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2062 | if (EPI.ConsumedArguments) |
| 2063 | Size += NumArgs * sizeof(bool); |
| 2064 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2065 | FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment); |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2066 | FunctionProtoType::ExtProtoInfo newEPI = EPI; |
| 2067 | newEPI.ExtInfo = EPI.ExtInfo.withCallingConv(CallConv); |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2068 | new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, Canonical, newEPI); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2069 | Types.push_back(FTP); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2070 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2071 | return QualType(FTP, 0); |
| 2072 | } |
| 2073 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2074 | #ifndef NDEBUG |
| 2075 | static bool NeedsInjectedClassNameType(const RecordDecl *D) { |
| 2076 | if (!isa<CXXRecordDecl>(D)) return false; |
| 2077 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 2078 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) |
| 2079 | return true; |
| 2080 | if (RD->getDescribedClassTemplate() && |
| 2081 | !isa<ClassTemplateSpecializationDecl>(RD)) |
| 2082 | return true; |
| 2083 | return false; |
| 2084 | } |
| 2085 | #endif |
| 2086 | |
| 2087 | /// getInjectedClassNameType - Return the unique reference to the |
| 2088 | /// injected class name type for the specified templated declaration. |
| 2089 | QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2090 | QualType TST) const { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2091 | assert(NeedsInjectedClassNameType(Decl)); |
| 2092 | if (Decl->TypeForDecl) { |
| 2093 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
Argyrios Kyrtzidis | 37ffed3 | 2010-07-02 11:55:32 +0000 | [diff] [blame] | 2094 | } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDeclaration()) { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2095 | assert(PrevDecl->TypeForDecl && "previous declaration has no type"); |
| 2096 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 2097 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
| 2098 | } else { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2099 | Type *newType = |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2100 | new (*this, TypeAlignment) InjectedClassNameType(Decl, TST); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2101 | Decl->TypeForDecl = newType; |
| 2102 | Types.push_back(newType); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2103 | } |
| 2104 | return QualType(Decl->TypeForDecl, 0); |
| 2105 | } |
| 2106 | |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2107 | /// getTypeDeclType - Return the unique reference to the type for the |
| 2108 | /// specified type declaration. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2109 | QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const { |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 2110 | assert(Decl && "Passed null for Decl param"); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2111 | assert(!Decl->TypeForDecl && "TypeForDecl present in slow case"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2112 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2113 | if (const TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2114 | return getTypedefType(Typedef); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2115 | |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2116 | assert(!isa<TemplateTypeParmDecl>(Decl) && |
| 2117 | "Template type parameter types are always available."); |
| 2118 | |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2119 | if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2120 | assert(!Record->getPreviousDeclaration() && |
| 2121 | "struct/union has previous declaration"); |
| 2122 | assert(!NeedsInjectedClassNameType(Record)); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2123 | return getRecordType(Record); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2124 | } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2125 | assert(!Enum->getPreviousDeclaration() && |
| 2126 | "enum has previous declaration"); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2127 | return getEnumType(Enum); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2128 | } else if (const UnresolvedUsingTypenameDecl *Using = |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2129 | dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2130 | Type *newType = new (*this, TypeAlignment) UnresolvedUsingType(Using); |
| 2131 | Decl->TypeForDecl = newType; |
| 2132 | Types.push_back(newType); |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 2133 | } else |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2134 | llvm_unreachable("TypeDecl without a type?"); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 2135 | |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 2136 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2137 | } |
| 2138 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2139 | /// getTypedefType - Return the unique reference to the type for the |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2140 | /// specified typedef name decl. |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2141 | QualType |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2142 | ASTContext::getTypedefType(const TypedefNameDecl *Decl, |
| 2143 | QualType Canonical) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2144 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2145 | |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2146 | if (Canonical.isNull()) |
| 2147 | Canonical = getCanonicalType(Decl->getUnderlyingType()); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2148 | TypedefType *newType = new(*this, TypeAlignment) |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2149 | TypedefType(Type::Typedef, Decl, Canonical); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2150 | Decl->TypeForDecl = newType; |
| 2151 | Types.push_back(newType); |
| 2152 | return QualType(newType, 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2153 | } |
| 2154 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2155 | QualType ASTContext::getRecordType(const RecordDecl *Decl) const { |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2156 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 2157 | |
| 2158 | if (const RecordDecl *PrevDecl = Decl->getPreviousDeclaration()) |
| 2159 | if (PrevDecl->TypeForDecl) |
| 2160 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 2161 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2162 | RecordType *newType = new (*this, TypeAlignment) RecordType(Decl); |
| 2163 | Decl->TypeForDecl = newType; |
| 2164 | Types.push_back(newType); |
| 2165 | return QualType(newType, 0); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2166 | } |
| 2167 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2168 | QualType ASTContext::getEnumType(const EnumDecl *Decl) const { |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2169 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 2170 | |
| 2171 | if (const EnumDecl *PrevDecl = Decl->getPreviousDeclaration()) |
| 2172 | if (PrevDecl->TypeForDecl) |
| 2173 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 2174 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2175 | EnumType *newType = new (*this, TypeAlignment) EnumType(Decl); |
| 2176 | Decl->TypeForDecl = newType; |
| 2177 | Types.push_back(newType); |
| 2178 | return QualType(newType, 0); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2179 | } |
| 2180 | |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 2181 | QualType ASTContext::getAttributedType(AttributedType::Kind attrKind, |
| 2182 | QualType modifiedType, |
| 2183 | QualType equivalentType) { |
| 2184 | llvm::FoldingSetNodeID id; |
| 2185 | AttributedType::Profile(id, attrKind, modifiedType, equivalentType); |
| 2186 | |
| 2187 | void *insertPos = 0; |
| 2188 | AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos); |
| 2189 | if (type) return QualType(type, 0); |
| 2190 | |
| 2191 | QualType canon = getCanonicalType(equivalentType); |
| 2192 | type = new (*this, TypeAlignment) |
| 2193 | AttributedType(canon, attrKind, modifiedType, equivalentType); |
| 2194 | |
| 2195 | Types.push_back(type); |
| 2196 | AttributedTypes.InsertNode(type, insertPos); |
| 2197 | |
| 2198 | return QualType(type, 0); |
| 2199 | } |
| 2200 | |
| 2201 | |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2202 | /// \brief Retrieve a substitution-result type. |
| 2203 | QualType |
| 2204 | ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2205 | QualType Replacement) const { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2206 | assert(Replacement.isCanonical() |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2207 | && "replacement types must always be canonical"); |
| 2208 | |
| 2209 | llvm::FoldingSetNodeID ID; |
| 2210 | SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); |
| 2211 | void *InsertPos = 0; |
| 2212 | SubstTemplateTypeParmType *SubstParm |
| 2213 | = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2214 | |
| 2215 | if (!SubstParm) { |
| 2216 | SubstParm = new (*this, TypeAlignment) |
| 2217 | SubstTemplateTypeParmType(Parm, Replacement); |
| 2218 | Types.push_back(SubstParm); |
| 2219 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 2220 | } |
| 2221 | |
| 2222 | return QualType(SubstParm, 0); |
| 2223 | } |
| 2224 | |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 2225 | /// \brief Retrieve a |
| 2226 | QualType ASTContext::getSubstTemplateTypeParmPackType( |
| 2227 | const TemplateTypeParmType *Parm, |
| 2228 | const TemplateArgument &ArgPack) { |
| 2229 | #ifndef NDEBUG |
| 2230 | for (TemplateArgument::pack_iterator P = ArgPack.pack_begin(), |
| 2231 | PEnd = ArgPack.pack_end(); |
| 2232 | P != PEnd; ++P) { |
| 2233 | assert(P->getKind() == TemplateArgument::Type &&"Pack contains a non-type"); |
| 2234 | assert(P->getAsType().isCanonical() && "Pack contains non-canonical type"); |
| 2235 | } |
| 2236 | #endif |
| 2237 | |
| 2238 | llvm::FoldingSetNodeID ID; |
| 2239 | SubstTemplateTypeParmPackType::Profile(ID, Parm, ArgPack); |
| 2240 | void *InsertPos = 0; |
| 2241 | if (SubstTemplateTypeParmPackType *SubstParm |
| 2242 | = SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2243 | return QualType(SubstParm, 0); |
| 2244 | |
| 2245 | QualType Canon; |
| 2246 | if (!Parm->isCanonicalUnqualified()) { |
| 2247 | Canon = getCanonicalType(QualType(Parm, 0)); |
| 2248 | Canon = getSubstTemplateTypeParmPackType(cast<TemplateTypeParmType>(Canon), |
| 2249 | ArgPack); |
| 2250 | SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2251 | } |
| 2252 | |
| 2253 | SubstTemplateTypeParmPackType *SubstParm |
| 2254 | = new (*this, TypeAlignment) SubstTemplateTypeParmPackType(Parm, Canon, |
| 2255 | ArgPack); |
| 2256 | Types.push_back(SubstParm); |
| 2257 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 2258 | return QualType(SubstParm, 0); |
| 2259 | } |
| 2260 | |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2261 | /// \brief Retrieve the template type parameter type for a template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2262 | /// parameter or parameter pack with the given depth, index, and (optionally) |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2263 | /// name. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2264 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2265 | bool ParameterPack, |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2266 | TemplateTypeParmDecl *TTPDecl) const { |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2267 | llvm::FoldingSetNodeID ID; |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2268 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, TTPDecl); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2269 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2270 | TemplateTypeParmType *TypeParm |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2271 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2272 | |
| 2273 | if (TypeParm) |
| 2274 | return QualType(TypeParm, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2275 | |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2276 | if (TTPDecl) { |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2277 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2278 | TypeParm = new (*this, TypeAlignment) TemplateTypeParmType(TTPDecl, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2279 | |
| 2280 | TemplateTypeParmType *TypeCheck |
| 2281 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2282 | assert(!TypeCheck && "Template type parameter canonical type broken"); |
| 2283 | (void)TypeCheck; |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2284 | } else |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2285 | TypeParm = new (*this, TypeAlignment) |
| 2286 | TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2287 | |
| 2288 | Types.push_back(TypeParm); |
| 2289 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 2290 | |
| 2291 | return QualType(TypeParm, 0); |
| 2292 | } |
| 2293 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2294 | TypeSourceInfo * |
| 2295 | ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name, |
| 2296 | SourceLocation NameLoc, |
| 2297 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2298 | QualType Underlying) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2299 | assert(!Name.getAsDependentTemplateName() && |
| 2300 | "No dependent template names here!"); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2301 | QualType TST = getTemplateSpecializationType(Name, Args, Underlying); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2302 | |
| 2303 | TypeSourceInfo *DI = CreateTypeSourceInfo(TST); |
| 2304 | TemplateSpecializationTypeLoc TL |
| 2305 | = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc()); |
| 2306 | TL.setTemplateNameLoc(NameLoc); |
| 2307 | TL.setLAngleLoc(Args.getLAngleLoc()); |
| 2308 | TL.setRAngleLoc(Args.getRAngleLoc()); |
| 2309 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 2310 | TL.setArgLocInfo(i, Args[i].getLocInfo()); |
| 2311 | return DI; |
| 2312 | } |
| 2313 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2314 | QualType |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2315 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2316 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2317 | QualType Underlying) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2318 | assert(!Template.getAsDependentTemplateName() && |
| 2319 | "No dependent template names here!"); |
| 2320 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2321 | unsigned NumArgs = Args.size(); |
| 2322 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2323 | llvm::SmallVector<TemplateArgument, 4> ArgVec; |
| 2324 | ArgVec.reserve(NumArgs); |
| 2325 | for (unsigned i = 0; i != NumArgs; ++i) |
| 2326 | ArgVec.push_back(Args[i].getArgument()); |
| 2327 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2328 | return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2329 | Underlying); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2330 | } |
| 2331 | |
| 2332 | QualType |
| 2333 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2334 | const TemplateArgument *Args, |
| 2335 | unsigned NumArgs, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2336 | QualType Underlying) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2337 | assert(!Template.getAsDependentTemplateName() && |
| 2338 | "No dependent template names here!"); |
Douglas Gregor | 0f0ea2a | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 2339 | // Look through qualified template names. |
| 2340 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 2341 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2342 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2343 | bool isTypeAlias = |
| 2344 | Template.getAsTemplateDecl() && |
| 2345 | isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl()); |
| 2346 | |
| 2347 | QualType CanonType; |
| 2348 | if (!Underlying.isNull()) |
| 2349 | CanonType = getCanonicalType(Underlying); |
| 2350 | else { |
| 2351 | assert(!isTypeAlias && |
| 2352 | "Underlying type for template alias must be computed by caller"); |
| 2353 | CanonType = getCanonicalTemplateSpecializationType(Template, Args, |
| 2354 | NumArgs); |
| 2355 | } |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 2356 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2357 | // Allocate the (non-canonical) template specialization type, but don't |
| 2358 | // try to unique it: these types typically have location information that |
| 2359 | // we don't unique and don't want to lose. |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2360 | void *Mem = Allocate(sizeof(TemplateSpecializationType) + |
| 2361 | sizeof(TemplateArgument) * NumArgs + |
| 2362 | (isTypeAlias ? sizeof(QualType) : 0), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2363 | TypeAlignment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2364 | TemplateSpecializationType *Spec |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 2365 | = new (Mem) TemplateSpecializationType(Template, |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2366 | Args, NumArgs, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2367 | CanonType, |
| 2368 | isTypeAlias ? Underlying : QualType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2369 | |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2370 | Types.push_back(Spec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2371 | return QualType(Spec, 0); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2372 | } |
| 2373 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2374 | QualType |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2375 | ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template, |
| 2376 | const TemplateArgument *Args, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2377 | unsigned NumArgs) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2378 | assert(!Template.getAsDependentTemplateName() && |
| 2379 | "No dependent template names here!"); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2380 | assert((!Template.getAsTemplateDecl() || |
| 2381 | !isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) && |
| 2382 | "Underlying type for template alias must be computed by caller"); |
| 2383 | |
Douglas Gregor | 0f0ea2a | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 2384 | // Look through qualified template names. |
| 2385 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 2386 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2387 | |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2388 | // Build the canonical template specialization type. |
| 2389 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); |
| 2390 | llvm::SmallVector<TemplateArgument, 4> CanonArgs; |
| 2391 | CanonArgs.reserve(NumArgs); |
| 2392 | for (unsigned I = 0; I != NumArgs; ++I) |
| 2393 | CanonArgs.push_back(getCanonicalTemplateArgument(Args[I])); |
| 2394 | |
| 2395 | // Determine whether this canonical template specialization type already |
| 2396 | // exists. |
| 2397 | llvm::FoldingSetNodeID ID; |
| 2398 | TemplateSpecializationType::Profile(ID, CanonTemplate, |
| 2399 | CanonArgs.data(), NumArgs, *this); |
| 2400 | |
| 2401 | void *InsertPos = 0; |
| 2402 | TemplateSpecializationType *Spec |
| 2403 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2404 | |
| 2405 | if (!Spec) { |
| 2406 | // Allocate a new canonical template specialization type. |
| 2407 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
| 2408 | sizeof(TemplateArgument) * NumArgs), |
| 2409 | TypeAlignment); |
| 2410 | Spec = new (Mem) TemplateSpecializationType(CanonTemplate, |
| 2411 | CanonArgs.data(), NumArgs, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2412 | QualType(), QualType()); |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2413 | Types.push_back(Spec); |
| 2414 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
| 2415 | } |
| 2416 | |
| 2417 | assert(Spec->isDependentType() && |
| 2418 | "Non-dependent template-id type must have a canonical type"); |
| 2419 | return QualType(Spec, 0); |
| 2420 | } |
| 2421 | |
| 2422 | QualType |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2423 | ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword, |
| 2424 | NestedNameSpecifier *NNS, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2425 | QualType NamedType) const { |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2426 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2427 | ElaboratedType::Profile(ID, Keyword, NNS, NamedType); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2428 | |
| 2429 | void *InsertPos = 0; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2430 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2431 | if (T) |
| 2432 | return QualType(T, 0); |
| 2433 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2434 | QualType Canon = NamedType; |
| 2435 | if (!Canon.isCanonical()) { |
| 2436 | Canon = getCanonicalType(NamedType); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2437 | ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2438 | assert(!CheckT && "Elaborated canonical type broken"); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2439 | (void)CheckT; |
| 2440 | } |
| 2441 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2442 | T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2443 | Types.push_back(T); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2444 | ElaboratedTypes.InsertNode(T, InsertPos); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2445 | return QualType(T, 0); |
| 2446 | } |
| 2447 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 2448 | QualType |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2449 | ASTContext::getParenType(QualType InnerType) const { |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 2450 | llvm::FoldingSetNodeID ID; |
| 2451 | ParenType::Profile(ID, InnerType); |
| 2452 | |
| 2453 | void *InsertPos = 0; |
| 2454 | ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2455 | if (T) |
| 2456 | return QualType(T, 0); |
| 2457 | |
| 2458 | QualType Canon = InnerType; |
| 2459 | if (!Canon.isCanonical()) { |
| 2460 | Canon = getCanonicalType(InnerType); |
| 2461 | ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2462 | assert(!CheckT && "Paren canonical type broken"); |
| 2463 | (void)CheckT; |
| 2464 | } |
| 2465 | |
| 2466 | T = new (*this) ParenType(InnerType, Canon); |
| 2467 | Types.push_back(T); |
| 2468 | ParenTypes.InsertNode(T, InsertPos); |
| 2469 | return QualType(T, 0); |
| 2470 | } |
| 2471 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2472 | QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, |
| 2473 | NestedNameSpecifier *NNS, |
| 2474 | const IdentifierInfo *Name, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2475 | QualType Canon) const { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2476 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 2477 | |
| 2478 | if (Canon.isNull()) { |
| 2479 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2480 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 2481 | if (Keyword == ETK_None) |
| 2482 | CanonKeyword = ETK_Typename; |
| 2483 | |
| 2484 | if (CanonNNS != NNS || CanonKeyword != Keyword) |
| 2485 | Canon = getDependentNameType(CanonKeyword, CanonNNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2486 | } |
| 2487 | |
| 2488 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2489 | DependentNameType::Profile(ID, Keyword, NNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2490 | |
| 2491 | void *InsertPos = 0; |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2492 | DependentNameType *T |
| 2493 | = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2494 | if (T) |
| 2495 | return QualType(T, 0); |
| 2496 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2497 | T = new (*this) DependentNameType(Keyword, NNS, Name, Canon); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2498 | Types.push_back(T); |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2499 | DependentNameTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2500 | return QualType(T, 0); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2501 | } |
| 2502 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2503 | QualType |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2504 | ASTContext::getDependentTemplateSpecializationType( |
| 2505 | ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2506 | NestedNameSpecifier *NNS, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2507 | const IdentifierInfo *Name, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2508 | const TemplateArgumentListInfo &Args) const { |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2509 | // TODO: avoid this copy |
| 2510 | llvm::SmallVector<TemplateArgument, 16> ArgCopy; |
| 2511 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
| 2512 | ArgCopy.push_back(Args[I].getArgument()); |
| 2513 | return getDependentTemplateSpecializationType(Keyword, NNS, Name, |
| 2514 | ArgCopy.size(), |
| 2515 | ArgCopy.data()); |
| 2516 | } |
| 2517 | |
| 2518 | QualType |
| 2519 | ASTContext::getDependentTemplateSpecializationType( |
| 2520 | ElaboratedTypeKeyword Keyword, |
| 2521 | NestedNameSpecifier *NNS, |
| 2522 | const IdentifierInfo *Name, |
| 2523 | unsigned NumArgs, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2524 | const TemplateArgument *Args) const { |
Douglas Gregor | aa2187d | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 2525 | assert((!NNS || NNS->isDependent()) && |
| 2526 | "nested-name-specifier must be dependent"); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2527 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2528 | llvm::FoldingSetNodeID ID; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2529 | DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS, |
| 2530 | Name, NumArgs, Args); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2531 | |
| 2532 | void *InsertPos = 0; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2533 | DependentTemplateSpecializationType *T |
| 2534 | = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2535 | if (T) |
| 2536 | return QualType(T, 0); |
| 2537 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2538 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2539 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2540 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 2541 | if (Keyword == ETK_None) CanonKeyword = ETK_Typename; |
| 2542 | |
| 2543 | bool AnyNonCanonArgs = false; |
| 2544 | llvm::SmallVector<TemplateArgument, 16> CanonArgs(NumArgs); |
| 2545 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 2546 | CanonArgs[I] = getCanonicalTemplateArgument(Args[I]); |
| 2547 | if (!CanonArgs[I].structurallyEquals(Args[I])) |
| 2548 | AnyNonCanonArgs = true; |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2549 | } |
| 2550 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2551 | QualType Canon; |
| 2552 | if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) { |
| 2553 | Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS, |
| 2554 | Name, NumArgs, |
| 2555 | CanonArgs.data()); |
| 2556 | |
| 2557 | // Find the insert position again. |
| 2558 | DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2559 | } |
| 2560 | |
| 2561 | void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) + |
| 2562 | sizeof(TemplateArgument) * NumArgs), |
| 2563 | TypeAlignment); |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 2564 | T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2565 | Name, NumArgs, Args, Canon); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2566 | Types.push_back(T); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2567 | DependentTemplateSpecializationTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2568 | return QualType(T, 0); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2569 | } |
| 2570 | |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2571 | QualType ASTContext::getPackExpansionType(QualType Pattern, |
| 2572 | llvm::Optional<unsigned> NumExpansions) { |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2573 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2574 | PackExpansionType::Profile(ID, Pattern, NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2575 | |
| 2576 | assert(Pattern->containsUnexpandedParameterPack() && |
| 2577 | "Pack expansions must expand one or more parameter packs"); |
| 2578 | void *InsertPos = 0; |
| 2579 | PackExpansionType *T |
| 2580 | = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2581 | if (T) |
| 2582 | return QualType(T, 0); |
| 2583 | |
| 2584 | QualType Canon; |
| 2585 | if (!Pattern.isCanonical()) { |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2586 | Canon = getPackExpansionType(getCanonicalType(Pattern), NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2587 | |
| 2588 | // Find the insert position again. |
| 2589 | PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2590 | } |
| 2591 | |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2592 | T = new (*this) PackExpansionType(Pattern, Canon, NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2593 | Types.push_back(T); |
| 2594 | PackExpansionTypes.InsertNode(T, InsertPos); |
| 2595 | return QualType(T, 0); |
| 2596 | } |
| 2597 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2598 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 2599 | /// alphabetically. |
| 2600 | static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, |
| 2601 | const ObjCProtocolDecl *RHS) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2602 | return LHS->getDeclName() < RHS->getDeclName(); |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2603 | } |
| 2604 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2605 | static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols, |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2606 | unsigned NumProtocols) { |
| 2607 | if (NumProtocols == 0) return true; |
| 2608 | |
| 2609 | for (unsigned i = 1; i != NumProtocols; ++i) |
| 2610 | if (!CmpProtocolNames(Protocols[i-1], Protocols[i])) |
| 2611 | return false; |
| 2612 | return true; |
| 2613 | } |
| 2614 | |
| 2615 | static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols, |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2616 | unsigned &NumProtocols) { |
| 2617 | ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2618 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2619 | // Sort protocols, keyed by name. |
| 2620 | std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); |
| 2621 | |
| 2622 | // Remove duplicates. |
| 2623 | ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); |
| 2624 | NumProtocols = ProtocolsEnd-Protocols; |
| 2625 | } |
| 2626 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2627 | QualType ASTContext::getObjCObjectType(QualType BaseType, |
| 2628 | ObjCProtocolDecl * const *Protocols, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2629 | unsigned NumProtocols) const { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2630 | // If the base type is an interface and there aren't any protocols |
| 2631 | // to add, then the interface type will do just fine. |
| 2632 | if (!NumProtocols && isa<ObjCInterfaceType>(BaseType)) |
| 2633 | return BaseType; |
| 2634 | |
| 2635 | // Look in the folding set for an existing type. |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2636 | llvm::FoldingSetNodeID ID; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2637 | ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2638 | void *InsertPos = 0; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2639 | if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2640 | return QualType(QT, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2641 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2642 | // Build the canonical type, which has the canonical base type and |
| 2643 | // a sorted-and-uniqued list of protocols. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2644 | QualType Canonical; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2645 | bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols); |
| 2646 | if (!ProtocolsSorted || !BaseType.isCanonical()) { |
| 2647 | if (!ProtocolsSorted) { |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 2648 | llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols, |
| 2649 | Protocols + NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2650 | unsigned UniqueCount = NumProtocols; |
| 2651 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2652 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2653 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 2654 | &Sorted[0], UniqueCount); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2655 | } else { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2656 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 2657 | Protocols, NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2658 | } |
| 2659 | |
| 2660 | // Regenerate InsertPos. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2661 | ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2662 | } |
| 2663 | |
| 2664 | unsigned Size = sizeof(ObjCObjectTypeImpl); |
| 2665 | Size += NumProtocols * sizeof(ObjCProtocolDecl *); |
| 2666 | void *Mem = Allocate(Size, TypeAlignment); |
| 2667 | ObjCObjectTypeImpl *T = |
| 2668 | new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols); |
| 2669 | |
| 2670 | Types.push_back(T); |
| 2671 | ObjCObjectTypes.InsertNode(T, InsertPos); |
| 2672 | return QualType(T, 0); |
| 2673 | } |
| 2674 | |
| 2675 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 2676 | /// the given object type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2677 | QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2678 | llvm::FoldingSetNodeID ID; |
| 2679 | ObjCObjectPointerType::Profile(ID, ObjectT); |
| 2680 | |
| 2681 | void *InsertPos = 0; |
| 2682 | if (ObjCObjectPointerType *QT = |
| 2683 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2684 | return QualType(QT, 0); |
| 2685 | |
| 2686 | // Find the canonical object type. |
| 2687 | QualType Canonical; |
| 2688 | if (!ObjectT.isCanonical()) { |
| 2689 | Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT)); |
| 2690 | |
| 2691 | // Regenerate InsertPos. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2692 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2693 | } |
| 2694 | |
Douglas Gregor | fd6a088 | 2010-02-08 22:59:26 +0000 | [diff] [blame] | 2695 | // No match. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2696 | void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment); |
| 2697 | ObjCObjectPointerType *QType = |
| 2698 | new (Mem) ObjCObjectPointerType(Canonical, ObjectT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2699 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2700 | Types.push_back(QType); |
| 2701 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2702 | return QualType(QType, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2703 | } |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2704 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2705 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 2706 | /// specified ObjC interface decl. The list of protocols is optional. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2707 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) const { |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2708 | if (Decl->TypeForDecl) |
| 2709 | return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2710 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2711 | // FIXME: redeclarations? |
| 2712 | void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment); |
| 2713 | ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl); |
| 2714 | Decl->TypeForDecl = T; |
| 2715 | Types.push_back(T); |
| 2716 | return QualType(T, 0); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2717 | } |
| 2718 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2719 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 2720 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2721 | /// multiple declarations that refer to "typeof(x)" all contain different |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2722 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2723 | /// on canonical type's (which are always unique). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2724 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) const { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2725 | TypeOfExprType *toe; |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2726 | if (tofExpr->isTypeDependent()) { |
| 2727 | llvm::FoldingSetNodeID ID; |
| 2728 | DependentTypeOfExprType::Profile(ID, *this, tofExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2729 | |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2730 | void *InsertPos = 0; |
| 2731 | DependentTypeOfExprType *Canon |
| 2732 | = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2733 | if (Canon) { |
| 2734 | // We already have a "canonical" version of an identical, dependent |
| 2735 | // typeof(expr) type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2736 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2737 | QualType((TypeOfExprType*)Canon, 0)); |
| 2738 | } |
| 2739 | else { |
| 2740 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2741 | Canon |
| 2742 | = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2743 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); |
| 2744 | toe = Canon; |
| 2745 | } |
| 2746 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2747 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2748 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2749 | } |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2750 | Types.push_back(toe); |
| 2751 | return QualType(toe, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2752 | } |
| 2753 | |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2754 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 2755 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 2756 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2757 | /// an issue. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2758 | /// on canonical type's (which are always unique). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2759 | QualType ASTContext::getTypeOfType(QualType tofType) const { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2760 | QualType Canonical = getCanonicalType(tofType); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2761 | TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2762 | Types.push_back(tot); |
| 2763 | return QualType(tot, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2764 | } |
| 2765 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2766 | /// getDecltypeForExpr - Given an expr, will return the decltype for that |
| 2767 | /// expression, according to the rules in C++0x [dcl.type.simple]p4 |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2768 | static QualType getDecltypeForExpr(const Expr *e, const ASTContext &Context) { |
Anders Carlsson | a07c33e | 2009-06-25 15:00:34 +0000 | [diff] [blame] | 2769 | if (e->isTypeDependent()) |
| 2770 | return Context.DependentTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2771 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2772 | // If e is an id expression or a class member access, decltype(e) is defined |
| 2773 | // as the type of the entity named by e. |
| 2774 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) { |
| 2775 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) |
| 2776 | return VD->getType(); |
| 2777 | } |
| 2778 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) { |
| 2779 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) |
| 2780 | return FD->getType(); |
| 2781 | } |
| 2782 | // If e is a function call or an invocation of an overloaded operator, |
| 2783 | // (parentheses around e are ignored), decltype(e) is defined as the |
| 2784 | // return type of that function. |
| 2785 | if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens())) |
| 2786 | return CE->getCallReturnType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2787 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2788 | QualType T = e->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2789 | |
| 2790 | // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2791 | // defined as T&, otherwise decltype(e) is defined as T. |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 2792 | if (e->isLValue()) |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2793 | T = Context.getLValueReferenceType(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2794 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2795 | return T; |
| 2796 | } |
| 2797 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2798 | /// getDecltypeType - Unlike many "get<Type>" functions, we don't unique |
| 2799 | /// DecltypeType AST's. The only motivation to unique these nodes would be |
| 2800 | /// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2801 | /// an issue. This doesn't effect the type checker, since it operates |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2802 | /// on canonical type's (which are always unique). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2803 | QualType ASTContext::getDecltypeType(Expr *e) const { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2804 | DecltypeType *dt; |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2805 | |
| 2806 | // C++0x [temp.type]p2: |
| 2807 | // If an expression e involves a template parameter, decltype(e) denotes a |
| 2808 | // unique dependent type. Two such decltype-specifiers refer to the same |
| 2809 | // type only if their expressions are equivalent (14.5.6.1). |
| 2810 | if (e->isInstantiationDependent()) { |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2811 | llvm::FoldingSetNodeID ID; |
| 2812 | DependentDecltypeType::Profile(ID, *this, e); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2813 | |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2814 | void *InsertPos = 0; |
| 2815 | DependentDecltypeType *Canon |
| 2816 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2817 | if (Canon) { |
| 2818 | // We already have a "canonical" version of an equivalent, dependent |
| 2819 | // decltype type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2820 | dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy, |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2821 | QualType((DecltypeType*)Canon, 0)); |
| 2822 | } |
| 2823 | else { |
| 2824 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2825 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2826 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); |
| 2827 | dt = Canon; |
| 2828 | } |
| 2829 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2830 | QualType T = getDecltypeForExpr(e, *this); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2831 | dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T)); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2832 | } |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2833 | Types.push_back(dt); |
| 2834 | return QualType(dt, 0); |
| 2835 | } |
| 2836 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 2837 | /// getUnaryTransformationType - We don't unique these, since the memory |
| 2838 | /// savings are minimal and these are rare. |
| 2839 | QualType ASTContext::getUnaryTransformType(QualType BaseType, |
| 2840 | QualType UnderlyingType, |
| 2841 | UnaryTransformType::UTTKind Kind) |
| 2842 | const { |
| 2843 | UnaryTransformType *Ty = |
Douglas Gregor | 69d9775 | 2011-05-25 17:51:54 +0000 | [diff] [blame] | 2844 | new (*this, TypeAlignment) UnaryTransformType (BaseType, UnderlyingType, |
| 2845 | Kind, |
| 2846 | UnderlyingType->isDependentType() ? |
| 2847 | QualType() : UnderlyingType); |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 2848 | Types.push_back(Ty); |
| 2849 | return QualType(Ty, 0); |
| 2850 | } |
| 2851 | |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 2852 | /// getAutoType - We only unique auto types after they've been deduced. |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2853 | QualType ASTContext::getAutoType(QualType DeducedType) const { |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 2854 | void *InsertPos = 0; |
| 2855 | if (!DeducedType.isNull()) { |
| 2856 | // Look in the folding set for an existing type. |
| 2857 | llvm::FoldingSetNodeID ID; |
| 2858 | AutoType::Profile(ID, DeducedType); |
| 2859 | if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2860 | return QualType(AT, 0); |
| 2861 | } |
| 2862 | |
| 2863 | AutoType *AT = new (*this, TypeAlignment) AutoType(DeducedType); |
| 2864 | Types.push_back(AT); |
| 2865 | if (InsertPos) |
| 2866 | AutoTypes.InsertNode(AT, InsertPos); |
| 2867 | return QualType(AT, 0); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2868 | } |
| 2869 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2870 | /// getAutoDeductType - Get type pattern for deducing against 'auto'. |
| 2871 | QualType ASTContext::getAutoDeductType() const { |
| 2872 | if (AutoDeductTy.isNull()) |
| 2873 | AutoDeductTy = getAutoType(QualType()); |
| 2874 | assert(!AutoDeductTy.isNull() && "can't build 'auto' pattern"); |
| 2875 | return AutoDeductTy; |
| 2876 | } |
| 2877 | |
| 2878 | /// getAutoRRefDeductType - Get type pattern for deducing against 'auto &&'. |
| 2879 | QualType ASTContext::getAutoRRefDeductType() const { |
| 2880 | if (AutoRRefDeductTy.isNull()) |
| 2881 | AutoRRefDeductTy = getRValueReferenceType(getAutoDeductType()); |
| 2882 | assert(!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern"); |
| 2883 | return AutoRRefDeductTy; |
| 2884 | } |
| 2885 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2886 | /// getTagDeclType - Return the unique reference to the type for the |
| 2887 | /// specified TagDecl (struct/union/class/enum) decl. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2888 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) const { |
Ted Kremenek | d778f88 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 2889 | assert (Decl); |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 2890 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 2891 | // away const? mutable? |
| 2892 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2893 | } |
| 2894 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2895 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 2896 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 2897 | /// needs to agree with the definition in <stddef.h>. |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 2898 | CanQualType ASTContext::getSizeType() const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2899 | return getFromTargetType(Target.getSizeType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2900 | } |
| 2901 | |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 2902 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 2903 | /// Used when in C++, as a GCC extension. |
| 2904 | QualType ASTContext::getSignedWCharType() const { |
| 2905 | // FIXME: derive from "Target" ? |
| 2906 | return WCharTy; |
| 2907 | } |
| 2908 | |
| 2909 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 2910 | /// Used when in C++, as a GCC extension. |
| 2911 | QualType ASTContext::getUnsignedWCharType() const { |
| 2912 | // FIXME: derive from "Target" ? |
| 2913 | return UnsignedIntTy; |
| 2914 | } |
| 2915 | |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 2916 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) |
| 2917 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 2918 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2919 | return getFromTargetType(Target.getPtrDiffType(0)); |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 2920 | } |
| 2921 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2922 | //===----------------------------------------------------------------------===// |
| 2923 | // Type Operators |
| 2924 | //===----------------------------------------------------------------------===// |
| 2925 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2926 | CanQualType ASTContext::getCanonicalParamType(QualType T) const { |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2927 | // Push qualifiers into arrays, and then discard any remaining |
| 2928 | // qualifiers. |
| 2929 | T = getCanonicalType(T); |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2930 | T = getVariableArrayDecayedType(T); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2931 | const Type *Ty = T.getTypePtr(); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2932 | QualType Result; |
| 2933 | if (isa<ArrayType>(Ty)) { |
| 2934 | Result = getArrayDecayedType(QualType(Ty,0)); |
| 2935 | } else if (isa<FunctionType>(Ty)) { |
| 2936 | Result = getPointerType(QualType(Ty, 0)); |
| 2937 | } else { |
| 2938 | Result = QualType(Ty, 0); |
| 2939 | } |
| 2940 | |
| 2941 | return CanQualType::CreateUnsafe(Result); |
| 2942 | } |
| 2943 | |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 2944 | QualType ASTContext::getUnqualifiedArrayType(QualType type, |
| 2945 | Qualifiers &quals) { |
| 2946 | SplitQualType splitType = type.getSplitUnqualifiedType(); |
| 2947 | |
| 2948 | // FIXME: getSplitUnqualifiedType() actually walks all the way to |
| 2949 | // the unqualified desugared type and then drops it on the floor. |
| 2950 | // We then have to strip that sugar back off with |
| 2951 | // getUnqualifiedDesugaredType(), which is silly. |
| 2952 | const ArrayType *AT = |
| 2953 | dyn_cast<ArrayType>(splitType.first->getUnqualifiedDesugaredType()); |
| 2954 | |
| 2955 | // If we don't have an array, just use the results in splitType. |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2956 | if (!AT) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 2957 | quals = splitType.second; |
| 2958 | return QualType(splitType.first, 0); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2959 | } |
| 2960 | |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 2961 | // Otherwise, recurse on the array's element type. |
| 2962 | QualType elementType = AT->getElementType(); |
| 2963 | QualType unqualElementType = getUnqualifiedArrayType(elementType, quals); |
| 2964 | |
| 2965 | // If that didn't change the element type, AT has no qualifiers, so we |
| 2966 | // can just use the results in splitType. |
| 2967 | if (elementType == unqualElementType) { |
| 2968 | assert(quals.empty()); // from the recursive call |
| 2969 | quals = splitType.second; |
| 2970 | return QualType(splitType.first, 0); |
| 2971 | } |
| 2972 | |
| 2973 | // Otherwise, add in the qualifiers from the outermost type, then |
| 2974 | // build the type back up. |
| 2975 | quals.addConsistentQualifiers(splitType.second); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2976 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2977 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 2978 | return getConstantArrayType(unqualElementType, CAT->getSize(), |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2979 | CAT->getSizeModifier(), 0); |
| 2980 | } |
| 2981 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2982 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 2983 | return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2984 | } |
| 2985 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2986 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 2987 | return getVariableArrayType(unqualElementType, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 2988 | VAT->getSizeExpr(), |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2989 | VAT->getSizeModifier(), |
| 2990 | VAT->getIndexTypeCVRQualifiers(), |
| 2991 | VAT->getBracketsRange()); |
| 2992 | } |
| 2993 | |
| 2994 | const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT); |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 2995 | return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(), |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2996 | DSAT->getSizeModifier(), 0, |
| 2997 | SourceRange()); |
| 2998 | } |
| 2999 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3000 | /// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that |
| 3001 | /// may be similar (C++ 4.4), replaces T1 and T2 with the type that |
| 3002 | /// they point to and return true. If T1 and T2 aren't pointer types |
| 3003 | /// or pointer-to-member types, or if they are not similar at this |
| 3004 | /// level, returns false and leaves T1 and T2 unchanged. Top-level |
| 3005 | /// qualifiers on T1 and T2 are ignored. This function will typically |
| 3006 | /// be called in a loop that successively "unwraps" pointer and |
| 3007 | /// pointer-to-member types to compare them at each level. |
| 3008 | bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) { |
| 3009 | const PointerType *T1PtrType = T1->getAs<PointerType>(), |
| 3010 | *T2PtrType = T2->getAs<PointerType>(); |
| 3011 | if (T1PtrType && T2PtrType) { |
| 3012 | T1 = T1PtrType->getPointeeType(); |
| 3013 | T2 = T2PtrType->getPointeeType(); |
| 3014 | return true; |
| 3015 | } |
| 3016 | |
| 3017 | const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(), |
| 3018 | *T2MPType = T2->getAs<MemberPointerType>(); |
| 3019 | if (T1MPType && T2MPType && |
| 3020 | hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0), |
| 3021 | QualType(T2MPType->getClass(), 0))) { |
| 3022 | T1 = T1MPType->getPointeeType(); |
| 3023 | T2 = T2MPType->getPointeeType(); |
| 3024 | return true; |
| 3025 | } |
| 3026 | |
| 3027 | if (getLangOptions().ObjC1) { |
| 3028 | const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(), |
| 3029 | *T2OPType = T2->getAs<ObjCObjectPointerType>(); |
| 3030 | if (T1OPType && T2OPType) { |
| 3031 | T1 = T1OPType->getPointeeType(); |
| 3032 | T2 = T2OPType->getPointeeType(); |
| 3033 | return true; |
| 3034 | } |
| 3035 | } |
| 3036 | |
| 3037 | // FIXME: Block pointers, too? |
| 3038 | |
| 3039 | return false; |
| 3040 | } |
| 3041 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3042 | DeclarationNameInfo |
| 3043 | ASTContext::getNameForTemplate(TemplateName Name, |
| 3044 | SourceLocation NameLoc) const { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3045 | switch (Name.getKind()) { |
| 3046 | case TemplateName::QualifiedTemplate: |
| 3047 | case TemplateName::Template: |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3048 | // DNInfo work in progress: CHECKME: what about DNLoc? |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3049 | return DeclarationNameInfo(Name.getAsTemplateDecl()->getDeclName(), |
| 3050 | NameLoc); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3051 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3052 | case TemplateName::OverloadedTemplate: { |
| 3053 | OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate(); |
| 3054 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 3055 | return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc); |
| 3056 | } |
| 3057 | |
| 3058 | case TemplateName::DependentTemplate: { |
| 3059 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3060 | DeclarationName DName; |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3061 | if (DTN->isIdentifier()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3062 | DName = DeclarationNames.getIdentifier(DTN->getIdentifier()); |
| 3063 | return DeclarationNameInfo(DName, NameLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3064 | } else { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3065 | DName = DeclarationNames.getCXXOperatorName(DTN->getOperator()); |
| 3066 | // DNInfo work in progress: FIXME: source locations? |
| 3067 | DeclarationNameLoc DNLoc; |
| 3068 | DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding(); |
| 3069 | DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding(); |
| 3070 | return DeclarationNameInfo(DName, NameLoc, DNLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3071 | } |
| 3072 | } |
| 3073 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3074 | case TemplateName::SubstTemplateTemplateParm: { |
| 3075 | SubstTemplateTemplateParmStorage *subst |
| 3076 | = Name.getAsSubstTemplateTemplateParm(); |
| 3077 | return DeclarationNameInfo(subst->getParameter()->getDeclName(), |
| 3078 | NameLoc); |
| 3079 | } |
| 3080 | |
| 3081 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 3082 | SubstTemplateTemplateParmPackStorage *subst |
| 3083 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 3084 | return DeclarationNameInfo(subst->getParameterPack()->getDeclName(), |
| 3085 | NameLoc); |
| 3086 | } |
| 3087 | } |
| 3088 | |
| 3089 | llvm_unreachable("bad template name kind!"); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3090 | } |
| 3091 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3092 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3093 | switch (Name.getKind()) { |
| 3094 | case TemplateName::QualifiedTemplate: |
| 3095 | case TemplateName::Template: { |
| 3096 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3097 | if (TemplateTemplateParmDecl *TTP |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3098 | = dyn_cast<TemplateTemplateParmDecl>(Template)) |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3099 | Template = getCanonicalTemplateTemplateParmDecl(TTP); |
| 3100 | |
| 3101 | // The canonical template name is the canonical template declaration. |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 3102 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3103 | } |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 3104 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3105 | case TemplateName::OverloadedTemplate: |
| 3106 | llvm_unreachable("cannot canonicalize overloaded template"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3107 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3108 | case TemplateName::DependentTemplate: { |
| 3109 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 3110 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 3111 | return DTN->CanonicalTemplateName; |
| 3112 | } |
| 3113 | |
| 3114 | case TemplateName::SubstTemplateTemplateParm: { |
| 3115 | SubstTemplateTemplateParmStorage *subst |
| 3116 | = Name.getAsSubstTemplateTemplateParm(); |
| 3117 | return getCanonicalTemplateName(subst->getReplacement()); |
| 3118 | } |
| 3119 | |
| 3120 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 3121 | SubstTemplateTemplateParmPackStorage *subst |
| 3122 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 3123 | TemplateTemplateParmDecl *canonParameter |
| 3124 | = getCanonicalTemplateTemplateParmDecl(subst->getParameterPack()); |
| 3125 | TemplateArgument canonArgPack |
| 3126 | = getCanonicalTemplateArgument(subst->getArgumentPack()); |
| 3127 | return getSubstTemplateTemplateParmPack(canonParameter, canonArgPack); |
| 3128 | } |
| 3129 | } |
| 3130 | |
| 3131 | llvm_unreachable("bad template name!"); |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 3132 | } |
| 3133 | |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 3134 | bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) { |
| 3135 | X = getCanonicalTemplateName(X); |
| 3136 | Y = getCanonicalTemplateName(Y); |
| 3137 | return X.getAsVoidPointer() == Y.getAsVoidPointer(); |
| 3138 | } |
| 3139 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3140 | TemplateArgument |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3141 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const { |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3142 | switch (Arg.getKind()) { |
| 3143 | case TemplateArgument::Null: |
| 3144 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3145 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3146 | case TemplateArgument::Expression: |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3147 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3148 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3149 | case TemplateArgument::Declaration: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3150 | return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3151 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3152 | case TemplateArgument::Template: |
| 3153 | return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate())); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3154 | |
| 3155 | case TemplateArgument::TemplateExpansion: |
| 3156 | return TemplateArgument(getCanonicalTemplateName( |
| 3157 | Arg.getAsTemplateOrTemplatePattern()), |
Douglas Gregor | 2be29f4 | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 3158 | Arg.getNumTemplateExpansions()); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3159 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3160 | case TemplateArgument::Integral: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3161 | return TemplateArgument(*Arg.getAsIntegral(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3162 | getCanonicalType(Arg.getIntegralType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3163 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3164 | case TemplateArgument::Type: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3165 | return TemplateArgument(getCanonicalType(Arg.getAsType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3166 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3167 | case TemplateArgument::Pack: { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 3168 | if (Arg.pack_size() == 0) |
| 3169 | return Arg; |
| 3170 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3171 | TemplateArgument *CanonArgs |
| 3172 | = new (*this) TemplateArgument[Arg.pack_size()]; |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3173 | unsigned Idx = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3174 | for (TemplateArgument::pack_iterator A = Arg.pack_begin(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3175 | AEnd = Arg.pack_end(); |
| 3176 | A != AEnd; (void)++A, ++Idx) |
| 3177 | CanonArgs[Idx] = getCanonicalTemplateArgument(*A); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3178 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3179 | return TemplateArgument(CanonArgs, Arg.pack_size()); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3180 | } |
| 3181 | } |
| 3182 | |
| 3183 | // Silence GCC warning |
| 3184 | assert(false && "Unhandled template argument kind"); |
| 3185 | return TemplateArgument(); |
| 3186 | } |
| 3187 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3188 | NestedNameSpecifier * |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3189 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3190 | if (!NNS) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3191 | return 0; |
| 3192 | |
| 3193 | switch (NNS->getKind()) { |
| 3194 | case NestedNameSpecifier::Identifier: |
| 3195 | // Canonicalize the prefix but keep the identifier the same. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3196 | return NestedNameSpecifier::Create(*this, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3197 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 3198 | NNS->getAsIdentifier()); |
| 3199 | |
| 3200 | case NestedNameSpecifier::Namespace: |
| 3201 | // A namespace is canonical; build a nested-name-specifier with |
| 3202 | // this namespace and no prefix. |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 3203 | return NestedNameSpecifier::Create(*this, 0, |
| 3204 | NNS->getAsNamespace()->getOriginalNamespace()); |
| 3205 | |
| 3206 | case NestedNameSpecifier::NamespaceAlias: |
| 3207 | // A namespace is canonical; build a nested-name-specifier with |
| 3208 | // this namespace and no prefix. |
| 3209 | return NestedNameSpecifier::Create(*this, 0, |
| 3210 | NNS->getAsNamespaceAlias()->getNamespace() |
| 3211 | ->getOriginalNamespace()); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3212 | |
| 3213 | case NestedNameSpecifier::TypeSpec: |
| 3214 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 3215 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3216 | |
| 3217 | // If we have some kind of dependent-named type (e.g., "typename T::type"), |
| 3218 | // break it apart into its prefix and identifier, then reconsititute those |
| 3219 | // as the canonical nested-name-specifier. This is required to canonicalize |
| 3220 | // a dependent nested-name-specifier involving typedefs of dependent-name |
| 3221 | // types, e.g., |
| 3222 | // typedef typename T::type T1; |
| 3223 | // typedef typename T1::type T2; |
| 3224 | if (const DependentNameType *DNT = T->getAs<DependentNameType>()) { |
| 3225 | NestedNameSpecifier *Prefix |
| 3226 | = getCanonicalNestedNameSpecifier(DNT->getQualifier()); |
| 3227 | return NestedNameSpecifier::Create(*this, Prefix, |
| 3228 | const_cast<IdentifierInfo *>(DNT->getIdentifier())); |
| 3229 | } |
| 3230 | |
Douglas Gregor | 643f843 | 2010-11-04 00:14:23 +0000 | [diff] [blame] | 3231 | // Do the same thing as above, but with dependent-named specializations. |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3232 | if (const DependentTemplateSpecializationType *DTST |
| 3233 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 3234 | NestedNameSpecifier *Prefix |
| 3235 | = getCanonicalNestedNameSpecifier(DTST->getQualifier()); |
Douglas Gregor | aa2187d | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 3236 | |
| 3237 | T = getDependentTemplateSpecializationType(DTST->getKeyword(), |
| 3238 | Prefix, DTST->getIdentifier(), |
| 3239 | DTST->getNumArgs(), |
| 3240 | DTST->getArgs()); |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3241 | T = getCanonicalType(T); |
| 3242 | } |
| 3243 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3244 | return NestedNameSpecifier::Create(*this, 0, false, |
| 3245 | const_cast<Type*>(T.getTypePtr())); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3246 | } |
| 3247 | |
| 3248 | case NestedNameSpecifier::Global: |
| 3249 | // The global specifier is canonical and unique. |
| 3250 | return NNS; |
| 3251 | } |
| 3252 | |
| 3253 | // Required to silence a GCC warning |
| 3254 | return 0; |
| 3255 | } |
| 3256 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3257 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3258 | const ArrayType *ASTContext::getAsArrayType(QualType T) const { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3259 | // Handle the non-qualified case efficiently. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3260 | if (!T.hasLocalQualifiers()) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3261 | // Handle the common positive case fast. |
| 3262 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 3263 | return AT; |
| 3264 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3265 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3266 | // Handle the common negative case fast. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3267 | if (!isa<ArrayType>(T.getCanonicalType())) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3268 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3269 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3270 | // Apply any qualifiers from the array type to the element type. This |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3271 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 3272 | // any type qualifiers, the element type is so qualified, not the array type." |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3273 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3274 | // If we get here, we either have type qualifiers on the type, or we have |
| 3275 | // sugar such as a typedef in the way. If we have type qualifiers on the type |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 3276 | // we must propagate them down into the element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3277 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3278 | SplitQualType split = T.getSplitDesugaredType(); |
| 3279 | Qualifiers qs = split.second; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3280 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3281 | // If we have a simple case, just return now. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3282 | const ArrayType *ATy = dyn_cast<ArrayType>(split.first); |
| 3283 | if (ATy == 0 || qs.empty()) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3284 | return ATy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3285 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3286 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 3287 | // qualifiers into the array element type and return a new array type. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3288 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3289 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3290 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 3291 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 3292 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3293 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3294 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 3295 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 3296 | IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3297 | IAT->getIndexTypeCVRQualifiers())); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3298 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3299 | if (const DependentSizedArrayType *DSAT |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3300 | = dyn_cast<DependentSizedArrayType>(ATy)) |
| 3301 | return cast<ArrayType>( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3302 | getDependentSizedArrayType(NewEltTy, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3303 | DSAT->getSizeExpr(), |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3304 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3305 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3306 | DSAT->getBracketsRange())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3307 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3308 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3309 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3310 | VAT->getSizeExpr(), |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3311 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3312 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3313 | VAT->getBracketsRange())); |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 3314 | } |
| 3315 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3316 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 3317 | /// specified array type to a pointer. This operation is non-trivial when |
| 3318 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 3319 | /// this returns a pointer to a properly qualified element of the array. |
| 3320 | /// |
| 3321 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3322 | QualType ASTContext::getArrayDecayedType(QualType Ty) const { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3323 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 3324 | // typedefs in the element type of the array. This also handles propagation |
| 3325 | // of type qualifiers from the array type into the element type if present |
| 3326 | // (C99 6.7.3p8). |
| 3327 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 3328 | assert(PrettyArrayType && "Not an array type!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3329 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3330 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3331 | |
| 3332 | // int x[restrict 4] -> int *restrict |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3333 | return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3334 | } |
| 3335 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3336 | QualType ASTContext::getBaseElementType(const ArrayType *array) const { |
| 3337 | return getBaseElementType(array->getElementType()); |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 3338 | } |
| 3339 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3340 | QualType ASTContext::getBaseElementType(QualType type) const { |
| 3341 | Qualifiers qs; |
| 3342 | while (true) { |
| 3343 | SplitQualType split = type.getSplitDesugaredType(); |
| 3344 | const ArrayType *array = split.first->getAsArrayTypeUnsafe(); |
| 3345 | if (!array) break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3346 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3347 | type = array->getElementType(); |
| 3348 | qs.addConsistentQualifiers(split.second); |
| 3349 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3350 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3351 | return getQualifiedType(type, qs); |
Anders Carlsson | 6183a99 | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 3352 | } |
| 3353 | |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 3354 | /// getConstantArrayElementCount - Returns number of constant array elements. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3355 | uint64_t |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 3356 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
| 3357 | uint64_t ElementCount = 1; |
| 3358 | do { |
| 3359 | ElementCount *= CA->getSize().getZExtValue(); |
| 3360 | CA = dyn_cast<ConstantArrayType>(CA->getElementType()); |
| 3361 | } while (CA); |
| 3362 | return ElementCount; |
| 3363 | } |
| 3364 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3365 | /// getFloatingRank - Return a relative rank for floating point types. |
| 3366 | /// This routine will assert if passed a built-in type that isn't a float. |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3367 | static FloatingRank getFloatingRank(QualType T) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3368 | if (const ComplexType *CT = T->getAs<ComplexType>()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3369 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3370 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3371 | assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"); |
| 3372 | switch (T->getAs<BuiltinType>()->getKind()) { |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3373 | default: assert(0 && "getFloatingRank(): not a floating type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3374 | case BuiltinType::Float: return FloatRank; |
| 3375 | case BuiltinType::Double: return DoubleRank; |
| 3376 | case BuiltinType::LongDouble: return LongDoubleRank; |
| 3377 | } |
| 3378 | } |
| 3379 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3380 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 3381 | /// point or a complex type (based on typeDomain/typeSize). |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 3382 | /// 'typeDomain' is a real floating point or complex type. |
| 3383 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 3384 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 3385 | QualType Domain) const { |
| 3386 | FloatingRank EltRank = getFloatingRank(Size); |
| 3387 | if (Domain->isComplexType()) { |
| 3388 | switch (EltRank) { |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 3389 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 3390 | case FloatRank: return FloatComplexTy; |
| 3391 | case DoubleRank: return DoubleComplexTy; |
| 3392 | case LongDoubleRank: return LongDoubleComplexTy; |
| 3393 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3394 | } |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 3395 | |
| 3396 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 3397 | switch (EltRank) { |
| 3398 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
| 3399 | case FloatRank: return FloatTy; |
| 3400 | case DoubleRank: return DoubleTy; |
| 3401 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 3402 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3403 | } |
| 3404 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3405 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 3406 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 3407 | /// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3408 | /// LHS < RHS, return -1. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3409 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const { |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3410 | FloatingRank LHSR = getFloatingRank(LHS); |
| 3411 | FloatingRank RHSR = getFloatingRank(RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3412 | |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3413 | if (LHSR == RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 3414 | return 0; |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3415 | if (LHSR > RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 3416 | return 1; |
| 3417 | return -1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3418 | } |
| 3419 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3420 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 3421 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 3422 | /// or if it is not canonicalized. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3423 | unsigned ASTContext::getIntegerRank(const Type *T) const { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 3424 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3425 | if (const EnumType* ET = dyn_cast<EnumType>(T)) |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 3426 | T = ET->getDecl()->getPromotionType().getTypePtr(); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3427 | |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 3428 | if (T->isSpecificBuiltinType(BuiltinType::WChar_S) || |
| 3429 | T->isSpecificBuiltinType(BuiltinType::WChar_U)) |
Eli Friedman | a342675 | 2009-07-05 23:44:27 +0000 | [diff] [blame] | 3430 | T = getFromTargetType(Target.getWCharType()).getTypePtr(); |
| 3431 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3432 | if (T->isSpecificBuiltinType(BuiltinType::Char16)) |
| 3433 | T = getFromTargetType(Target.getChar16Type()).getTypePtr(); |
| 3434 | |
| 3435 | if (T->isSpecificBuiltinType(BuiltinType::Char32)) |
| 3436 | T = getFromTargetType(Target.getChar32Type()).getTypePtr(); |
| 3437 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3438 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3439 | default: assert(0 && "getIntegerRank(): not a built-in integer"); |
| 3440 | case BuiltinType::Bool: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3441 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3442 | case BuiltinType::Char_S: |
| 3443 | case BuiltinType::Char_U: |
| 3444 | case BuiltinType::SChar: |
| 3445 | case BuiltinType::UChar: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3446 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3447 | case BuiltinType::Short: |
| 3448 | case BuiltinType::UShort: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3449 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3450 | case BuiltinType::Int: |
| 3451 | case BuiltinType::UInt: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3452 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3453 | case BuiltinType::Long: |
| 3454 | case BuiltinType::ULong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3455 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3456 | case BuiltinType::LongLong: |
| 3457 | case BuiltinType::ULongLong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3458 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 3459 | case BuiltinType::Int128: |
| 3460 | case BuiltinType::UInt128: |
| 3461 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3462 | } |
| 3463 | } |
| 3464 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3465 | /// \brief Whether this is a promotable bitfield reference according |
| 3466 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
| 3467 | /// |
| 3468 | /// \returns the type this bit-field will promote to, or NULL if no |
| 3469 | /// promotion occurs. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3470 | QualType ASTContext::isPromotableBitField(Expr *E) const { |
Douglas Gregor | ceafbde | 2010-05-24 20:13:53 +0000 | [diff] [blame] | 3471 | if (E->isTypeDependent() || E->isValueDependent()) |
| 3472 | return QualType(); |
| 3473 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3474 | FieldDecl *Field = E->getBitField(); |
| 3475 | if (!Field) |
| 3476 | return QualType(); |
| 3477 | |
| 3478 | QualType FT = Field->getType(); |
| 3479 | |
| 3480 | llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this); |
| 3481 | uint64_t BitWidth = BitWidthAP.getZExtValue(); |
| 3482 | uint64_t IntSize = getTypeSize(IntTy); |
| 3483 | // GCC extension compatibility: if the bit-field size is less than or equal |
| 3484 | // to the size of int, it gets promoted no matter what its type is. |
| 3485 | // For instance, unsigned long bf : 4 gets promoted to signed int. |
| 3486 | if (BitWidth < IntSize) |
| 3487 | return IntTy; |
| 3488 | |
| 3489 | if (BitWidth == IntSize) |
| 3490 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
| 3491 | |
| 3492 | // Types bigger than int are not subject to promotions, and therefore act |
| 3493 | // like the base type. |
| 3494 | // FIXME: This doesn't quite match what gcc does, but what gcc does here |
| 3495 | // is ridiculous. |
| 3496 | return QualType(); |
| 3497 | } |
| 3498 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3499 | /// getPromotedIntegerType - Returns the type that Promotable will |
| 3500 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
| 3501 | /// integer type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3502 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) const { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3503 | assert(!Promotable.isNull()); |
| 3504 | assert(Promotable->isPromotableIntegerType()); |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 3505 | if (const EnumType *ET = Promotable->getAs<EnumType>()) |
| 3506 | return ET->getDecl()->getPromotionType(); |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3507 | if (Promotable->isSignedIntegerType()) |
| 3508 | return IntTy; |
| 3509 | uint64_t PromotableSize = getTypeSize(Promotable); |
| 3510 | uint64_t IntSize = getTypeSize(IntTy); |
| 3511 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
| 3512 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
| 3513 | } |
| 3514 | |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame^] | 3515 | /// \brief Recurses in pointer/array types until it finds an objc retainable |
| 3516 | /// type and returns its ownership. |
| 3517 | Qualifiers::ObjCLifetime ASTContext::getInnerObjCOwnership(QualType T) const { |
| 3518 | while (!T.isNull()) { |
| 3519 | if (T.getObjCLifetime() != Qualifiers::OCL_None) |
| 3520 | return T.getObjCLifetime(); |
| 3521 | if (T->isArrayType()) |
| 3522 | T = getBaseElementType(T); |
| 3523 | else if (const PointerType *PT = T->getAs<PointerType>()) |
| 3524 | T = PT->getPointeeType(); |
| 3525 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 3526 | RT->getPointeeType(); |
| 3527 | else |
| 3528 | break; |
| 3529 | } |
| 3530 | |
| 3531 | return Qualifiers::OCL_None; |
| 3532 | } |
| 3533 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3534 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3535 | /// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3536 | /// LHS < RHS, return -1. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3537 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3538 | const Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 3539 | const Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3540 | if (LHSC == RHSC) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3541 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3542 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 3543 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3544 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3545 | unsigned LHSRank = getIntegerRank(LHSC); |
| 3546 | unsigned RHSRank = getIntegerRank(RHSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3547 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3548 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 3549 | if (LHSRank == RHSRank) return 0; |
| 3550 | return LHSRank > RHSRank ? 1 : -1; |
| 3551 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3552 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3553 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 3554 | if (LHSUnsigned) { |
| 3555 | // If the unsigned [LHS] type is larger, return it. |
| 3556 | if (LHSRank >= RHSRank) |
| 3557 | return 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3558 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3559 | // If the signed type can represent all values of the unsigned type, it |
| 3560 | // wins. Because we are dealing with 2's complement and types that are |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3561 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3562 | return -1; |
| 3563 | } |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3564 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3565 | // If the unsigned [RHS] type is larger, return it. |
| 3566 | if (RHSRank >= LHSRank) |
| 3567 | return -1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3568 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3569 | // If the signed type can represent all values of the unsigned type, it |
| 3570 | // wins. Because we are dealing with 2's complement and types that are |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3571 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3572 | return 1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3573 | } |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3574 | |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3575 | static RecordDecl * |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3576 | CreateRecordDecl(const ASTContext &Ctx, RecordDecl::TagKind TK, |
| 3577 | DeclContext *DC, IdentifierInfo *Id) { |
| 3578 | SourceLocation Loc; |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3579 | if (Ctx.getLangOptions().CPlusPlus) |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3580 | return CXXRecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3581 | else |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3582 | return RecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3583 | } |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3584 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3585 | // getCFConstantStringType - Return the type used for constant CFStrings. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3586 | QualType ASTContext::getCFConstantStringType() const { |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3587 | if (!CFConstantStringTypeDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3588 | CFConstantStringTypeDecl = |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3589 | CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3590 | &Idents.get("NSConstantString")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3591 | CFConstantStringTypeDecl->startDefinition(); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3592 | |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 3593 | QualType FieldTypes[4]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3594 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3595 | // const int *isa; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3596 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 3597 | // int flags; |
| 3598 | FieldTypes[1] = IntTy; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3599 | // const char *str; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3600 | FieldTypes[2] = getPointerType(CharTy.withConst()); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3601 | // long length; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3602 | FieldTypes[3] = LongTy; |
| 3603 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3604 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3605 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3606 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3607 | SourceLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3608 | SourceLocation(), 0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3609 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3610 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3611 | /*Mutable=*/false, |
| 3612 | /*HasInit=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3613 | Field->setAccess(AS_public); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3614 | CFConstantStringTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3615 | } |
| 3616 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3617 | CFConstantStringTypeDecl->completeDefinition(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3618 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3619 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3620 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 8467583 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 3621 | } |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 3622 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3623 | void ASTContext::setCFConstantStringType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3624 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3625 | assert(Rec && "Invalid CFConstantStringType"); |
| 3626 | CFConstantStringTypeDecl = Rec->getDecl(); |
| 3627 | } |
| 3628 | |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 3629 | // getNSConstantStringType - Return the type used for constant NSStrings. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3630 | QualType ASTContext::getNSConstantStringType() const { |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 3631 | if (!NSConstantStringTypeDecl) { |
| 3632 | NSConstantStringTypeDecl = |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3633 | CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 3634 | &Idents.get("__builtin_NSString")); |
| 3635 | NSConstantStringTypeDecl->startDefinition(); |
| 3636 | |
| 3637 | QualType FieldTypes[3]; |
| 3638 | |
| 3639 | // const int *isa; |
| 3640 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
| 3641 | // const char *str; |
| 3642 | FieldTypes[1] = getPointerType(CharTy.withConst()); |
| 3643 | // unsigned int length; |
| 3644 | FieldTypes[2] = UnsignedIntTy; |
| 3645 | |
| 3646 | // Create fields |
| 3647 | for (unsigned i = 0; i < 3; ++i) { |
| 3648 | FieldDecl *Field = FieldDecl::Create(*this, NSConstantStringTypeDecl, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3649 | SourceLocation(), |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 3650 | SourceLocation(), 0, |
| 3651 | FieldTypes[i], /*TInfo=*/0, |
| 3652 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3653 | /*Mutable=*/false, |
| 3654 | /*HasInit=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3655 | Field->setAccess(AS_public); |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 3656 | NSConstantStringTypeDecl->addDecl(Field); |
| 3657 | } |
| 3658 | |
| 3659 | NSConstantStringTypeDecl->completeDefinition(); |
| 3660 | } |
| 3661 | |
| 3662 | return getTagDeclType(NSConstantStringTypeDecl); |
| 3663 | } |
| 3664 | |
| 3665 | void ASTContext::setNSConstantStringType(QualType T) { |
| 3666 | const RecordType *Rec = T->getAs<RecordType>(); |
| 3667 | assert(Rec && "Invalid NSConstantStringType"); |
| 3668 | NSConstantStringTypeDecl = Rec->getDecl(); |
| 3669 | } |
| 3670 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3671 | QualType ASTContext::getObjCFastEnumerationStateType() const { |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3672 | if (!ObjCFastEnumerationStateTypeDecl) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3673 | ObjCFastEnumerationStateTypeDecl = |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3674 | CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3675 | &Idents.get("__objcFastEnumerationState")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3676 | ObjCFastEnumerationStateTypeDecl->startDefinition(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3677 | |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3678 | QualType FieldTypes[] = { |
| 3679 | UnsignedLongTy, |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3680 | getPointerType(ObjCIdTypedefType), |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3681 | getPointerType(UnsignedLongTy), |
| 3682 | getConstantArrayType(UnsignedLongTy, |
| 3683 | llvm::APInt(32, 5), ArrayType::Normal, 0) |
| 3684 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3685 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3686 | for (size_t i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3687 | FieldDecl *Field = FieldDecl::Create(*this, |
| 3688 | ObjCFastEnumerationStateTypeDecl, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3689 | SourceLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3690 | SourceLocation(), 0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3691 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3692 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3693 | /*Mutable=*/false, |
| 3694 | /*HasInit=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3695 | Field->setAccess(AS_public); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3696 | ObjCFastEnumerationStateTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3697 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3698 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3699 | ObjCFastEnumerationStateTypeDecl->completeDefinition(); |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3700 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3701 | |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3702 | return getTagDeclType(ObjCFastEnumerationStateTypeDecl); |
| 3703 | } |
| 3704 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3705 | QualType ASTContext::getBlockDescriptorType() const { |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3706 | if (BlockDescriptorType) |
| 3707 | return getTagDeclType(BlockDescriptorType); |
| 3708 | |
| 3709 | RecordDecl *T; |
| 3710 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3711 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3712 | &Idents.get("__block_descriptor")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3713 | T->startDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3714 | |
| 3715 | QualType FieldTypes[] = { |
| 3716 | UnsignedLongTy, |
| 3717 | UnsignedLongTy, |
| 3718 | }; |
| 3719 | |
| 3720 | const char *FieldNames[] = { |
| 3721 | "reserved", |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3722 | "Size" |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3723 | }; |
| 3724 | |
| 3725 | for (size_t i = 0; i < 2; ++i) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3726 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3727 | SourceLocation(), |
| 3728 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3729 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3730 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3731 | /*Mutable=*/false, |
| 3732 | /*HasInit=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3733 | Field->setAccess(AS_public); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3734 | T->addDecl(Field); |
| 3735 | } |
| 3736 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3737 | T->completeDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3738 | |
| 3739 | BlockDescriptorType = T; |
| 3740 | |
| 3741 | return getTagDeclType(BlockDescriptorType); |
| 3742 | } |
| 3743 | |
| 3744 | void ASTContext::setBlockDescriptorType(QualType T) { |
| 3745 | const RecordType *Rec = T->getAs<RecordType>(); |
| 3746 | assert(Rec && "Invalid BlockDescriptorType"); |
| 3747 | BlockDescriptorType = Rec->getDecl(); |
| 3748 | } |
| 3749 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3750 | QualType ASTContext::getBlockDescriptorExtendedType() const { |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3751 | if (BlockDescriptorExtendedType) |
| 3752 | return getTagDeclType(BlockDescriptorExtendedType); |
| 3753 | |
| 3754 | RecordDecl *T; |
| 3755 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3756 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3757 | &Idents.get("__block_descriptor_withcopydispose")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3758 | T->startDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3759 | |
| 3760 | QualType FieldTypes[] = { |
| 3761 | UnsignedLongTy, |
| 3762 | UnsignedLongTy, |
| 3763 | getPointerType(VoidPtrTy), |
| 3764 | getPointerType(VoidPtrTy) |
| 3765 | }; |
| 3766 | |
| 3767 | const char *FieldNames[] = { |
| 3768 | "reserved", |
| 3769 | "Size", |
| 3770 | "CopyFuncPtr", |
| 3771 | "DestroyFuncPtr" |
| 3772 | }; |
| 3773 | |
| 3774 | for (size_t i = 0; i < 4; ++i) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3775 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3776 | SourceLocation(), |
| 3777 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3778 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3779 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3780 | /*Mutable=*/false, |
| 3781 | /*HasInit=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3782 | Field->setAccess(AS_public); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3783 | T->addDecl(Field); |
| 3784 | } |
| 3785 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3786 | T->completeDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3787 | |
| 3788 | BlockDescriptorExtendedType = T; |
| 3789 | |
| 3790 | return getTagDeclType(BlockDescriptorExtendedType); |
| 3791 | } |
| 3792 | |
| 3793 | void ASTContext::setBlockDescriptorExtendedType(QualType T) { |
| 3794 | const RecordType *Rec = T->getAs<RecordType>(); |
| 3795 | assert(Rec && "Invalid BlockDescriptorType"); |
| 3796 | BlockDescriptorExtendedType = Rec->getDecl(); |
| 3797 | } |
| 3798 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3799 | bool ASTContext::BlockRequiresCopying(QualType Ty) const { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3800 | if (Ty->isObjCRetainableType()) |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3801 | return true; |
Fariborz Jahanian | e38be61 | 2010-11-17 00:21:28 +0000 | [diff] [blame] | 3802 | if (getLangOptions().CPlusPlus) { |
| 3803 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3804 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 3805 | return RD->hasConstCopyConstructor(); |
Fariborz Jahanian | e38be61 | 2010-11-17 00:21:28 +0000 | [diff] [blame] | 3806 | |
| 3807 | } |
| 3808 | } |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3809 | return false; |
| 3810 | } |
| 3811 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3812 | QualType |
| 3813 | ASTContext::BuildByRefType(llvm::StringRef DeclName, QualType Ty) const { |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3814 | // type = struct __Block_byref_1_X { |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3815 | // void *__isa; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3816 | // struct __Block_byref_1_X *__forwarding; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3817 | // unsigned int __flags; |
| 3818 | // unsigned int __size; |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 3819 | // void *__copy_helper; // as needed |
| 3820 | // void *__destroy_help // as needed |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3821 | // int X; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3822 | // } * |
| 3823 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3824 | bool HasCopyAndDispose = BlockRequiresCopying(Ty); |
| 3825 | |
| 3826 | // FIXME: Move up |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 3827 | llvm::SmallString<36> Name; |
| 3828 | llvm::raw_svector_ostream(Name) << "__Block_byref_" << |
| 3829 | ++UniqueBlockByRefTypeID << '_' << DeclName; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3830 | RecordDecl *T; |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3831 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, &Idents.get(Name.str())); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3832 | T->startDefinition(); |
| 3833 | QualType Int32Ty = IntTy; |
| 3834 | assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported"); |
| 3835 | QualType FieldTypes[] = { |
| 3836 | getPointerType(VoidPtrTy), |
| 3837 | getPointerType(getTagDeclType(T)), |
| 3838 | Int32Ty, |
| 3839 | Int32Ty, |
| 3840 | getPointerType(VoidPtrTy), |
| 3841 | getPointerType(VoidPtrTy), |
| 3842 | Ty |
| 3843 | }; |
| 3844 | |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3845 | llvm::StringRef FieldNames[] = { |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3846 | "__isa", |
| 3847 | "__forwarding", |
| 3848 | "__flags", |
| 3849 | "__size", |
| 3850 | "__copy_helper", |
| 3851 | "__destroy_helper", |
| 3852 | DeclName, |
| 3853 | }; |
| 3854 | |
| 3855 | for (size_t i = 0; i < 7; ++i) { |
| 3856 | if (!HasCopyAndDispose && i >=4 && i <= 5) |
| 3857 | continue; |
| 3858 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3859 | SourceLocation(), |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3860 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3861 | FieldTypes[i], /*TInfo=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3862 | /*BitWidth=*/0, /*Mutable=*/false, |
| 3863 | /*HasInit=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3864 | Field->setAccess(AS_public); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3865 | T->addDecl(Field); |
| 3866 | } |
| 3867 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3868 | T->completeDefinition(); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3869 | |
| 3870 | return getPointerType(getTagDeclType(T)); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3871 | } |
| 3872 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3873 | void ASTContext::setObjCFastEnumerationStateType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3874 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3875 | assert(Rec && "Invalid ObjCFAstEnumerationStateType"); |
| 3876 | ObjCFastEnumerationStateTypeDecl = Rec->getDecl(); |
| 3877 | } |
| 3878 | |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3879 | // This returns true if a type has been typedefed to BOOL: |
| 3880 | // typedef <type> BOOL; |
Chris Lattner | 2d99833 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 3881 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3882 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | bb49c3e | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 3883 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 3884 | return II->isStr("BOOL"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3885 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3886 | return false; |
| 3887 | } |
| 3888 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3889 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3890 | /// purpose. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3891 | CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const { |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 3892 | if (!type->isIncompleteArrayType() && type->isIncompleteType()) |
| 3893 | return CharUnits::Zero(); |
| 3894 | |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3895 | CharUnits sz = getTypeSizeInChars(type); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3896 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3897 | // Make all integer and enum types at least as large as an int |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3898 | if (sz.isPositive() && type->isIntegralOrEnumerationType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3899 | sz = std::max(sz, getTypeSizeInChars(IntTy)); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3900 | // Treat arrays as pointers, since that's how they're passed in. |
| 3901 | else if (type->isArrayType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3902 | sz = getTypeSizeInChars(VoidPtrTy); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3903 | return sz; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3904 | } |
| 3905 | |
| 3906 | static inline |
| 3907 | std::string charUnitsToString(const CharUnits &CU) { |
| 3908 | return llvm::itostr(CU.getQuantity()); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3909 | } |
| 3910 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 3911 | /// getObjCEncodingForBlock - Return the encoded type for this block |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3912 | /// declaration. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 3913 | std::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const { |
| 3914 | std::string S; |
| 3915 | |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3916 | const BlockDecl *Decl = Expr->getBlockDecl(); |
| 3917 | QualType BlockTy = |
| 3918 | Expr->getType()->getAs<BlockPointerType>()->getPointeeType(); |
| 3919 | // Encode result type. |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 3920 | getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3921 | // Compute size of all parameters. |
| 3922 | // Start with computing size of a pointer in number of bytes. |
| 3923 | // FIXME: There might(should) be a better way of doing this computation! |
| 3924 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3925 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
| 3926 | CharUnits ParmOffset = PtrSize; |
Fariborz Jahanian | 6f46c26 | 2010-04-08 18:06:22 +0000 | [diff] [blame] | 3927 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3928 | E = Decl->param_end(); PI != E; ++PI) { |
| 3929 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3930 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3931 | assert (sz.isPositive() && "BlockExpr - Incomplete param type"); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3932 | ParmOffset += sz; |
| 3933 | } |
| 3934 | // Size of the argument frame |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3935 | S += charUnitsToString(ParmOffset); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3936 | // Block pointer and offset. |
| 3937 | S += "@?0"; |
| 3938 | ParmOffset = PtrSize; |
| 3939 | |
| 3940 | // Argument types. |
| 3941 | ParmOffset = PtrSize; |
| 3942 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E = |
| 3943 | Decl->param_end(); PI != E; ++PI) { |
| 3944 | ParmVarDecl *PVDecl = *PI; |
| 3945 | QualType PType = PVDecl->getOriginalType(); |
| 3946 | if (const ArrayType *AT = |
| 3947 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3948 | // Use array's original type only if it has known number of |
| 3949 | // elements. |
| 3950 | if (!isa<ConstantArrayType>(AT)) |
| 3951 | PType = PVDecl->getType(); |
| 3952 | } else if (PType->isFunctionType()) |
| 3953 | PType = PVDecl->getType(); |
| 3954 | getObjCEncodingForType(PType, S); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3955 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3956 | ParmOffset += getObjCEncodingTypeSize(PType); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3957 | } |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 3958 | |
| 3959 | return S; |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3960 | } |
| 3961 | |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 3962 | bool ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl, |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 3963 | std::string& S) { |
| 3964 | // Encode result type. |
| 3965 | getObjCEncodingForType(Decl->getResultType(), S); |
| 3966 | CharUnits ParmOffset; |
| 3967 | // Compute size of all parameters. |
| 3968 | for (FunctionDecl::param_const_iterator PI = Decl->param_begin(), |
| 3969 | E = Decl->param_end(); PI != E; ++PI) { |
| 3970 | QualType PType = (*PI)->getType(); |
| 3971 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 3972 | if (sz.isZero()) |
| 3973 | return true; |
| 3974 | |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 3975 | assert (sz.isPositive() && |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 3976 | "getObjCEncodingForFunctionDecl - Incomplete param type"); |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 3977 | ParmOffset += sz; |
| 3978 | } |
| 3979 | S += charUnitsToString(ParmOffset); |
| 3980 | ParmOffset = CharUnits::Zero(); |
| 3981 | |
| 3982 | // Argument types. |
| 3983 | for (FunctionDecl::param_const_iterator PI = Decl->param_begin(), |
| 3984 | E = Decl->param_end(); PI != E; ++PI) { |
| 3985 | ParmVarDecl *PVDecl = *PI; |
| 3986 | QualType PType = PVDecl->getOriginalType(); |
| 3987 | if (const ArrayType *AT = |
| 3988 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3989 | // Use array's original type only if it has known number of |
| 3990 | // elements. |
| 3991 | if (!isa<ConstantArrayType>(AT)) |
| 3992 | PType = PVDecl->getType(); |
| 3993 | } else if (PType->isFunctionType()) |
| 3994 | PType = PVDecl->getType(); |
| 3995 | getObjCEncodingForType(PType, S); |
| 3996 | S += charUnitsToString(ParmOffset); |
| 3997 | ParmOffset += getObjCEncodingTypeSize(PType); |
| 3998 | } |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 3999 | |
| 4000 | return false; |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4001 | } |
| 4002 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4003 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4004 | /// declaration. |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4005 | bool ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4006 | std::string& S) const { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4007 | // FIXME: This is not very efficient. |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 4008 | // Encode type qualifer, 'in', 'inout', etc. for the return type. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4009 | getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4010 | // Encode result type. |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 4011 | getObjCEncodingForType(Decl->getResultType(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4012 | // Compute size of all parameters. |
| 4013 | // Start with computing size of a pointer in number of bytes. |
| 4014 | // FIXME: There might(should) be a better way of doing this computation! |
| 4015 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4016 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4017 | // The first two arguments (self and _cmd) are pointers; account for |
| 4018 | // their size. |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4019 | CharUnits ParmOffset = 2 * PtrSize; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 4020 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 4021 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 4022 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4023 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4024 | if (sz.isZero()) |
| 4025 | return true; |
| 4026 | |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4027 | assert (sz.isPositive() && |
| 4028 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4029 | ParmOffset += sz; |
| 4030 | } |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4031 | S += charUnitsToString(ParmOffset); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4032 | S += "@0:"; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4033 | S += charUnitsToString(PtrSize); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4034 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4035 | // Argument types. |
| 4036 | ParmOffset = 2 * PtrSize; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 4037 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 4038 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 4039 | ParmVarDecl *PVDecl = *PI; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4040 | QualType PType = PVDecl->getOriginalType(); |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 4041 | if (const ArrayType *AT = |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 4042 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4043 | // Use array's original type only if it has known number of |
| 4044 | // elements. |
Steve Naroff | bb3fde3 | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 4045 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 4046 | PType = PVDecl->getType(); |
| 4047 | } else if (PType->isFunctionType()) |
| 4048 | PType = PVDecl->getType(); |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 4049 | // Process argument qualifiers for user supplied arguments; such as, |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4050 | // 'in', 'inout', etc. |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 4051 | getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 4052 | getObjCEncodingForType(PType, S); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4053 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4054 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4055 | } |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4056 | |
| 4057 | return false; |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4058 | } |
| 4059 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4060 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4061 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4062 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 4063 | /// NULL when getting encodings for protocol properties. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4064 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 4065 | /// attributes readonly and bycopy are encoded as single characters. The |
| 4066 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 4067 | /// encoded as single characters, followed by an identifier. Property types |
| 4068 | /// are also encoded as a parametrized attribute. The characters used to encode |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4069 | /// these attributes are defined by the following enumeration: |
| 4070 | /// @code |
| 4071 | /// enum PropertyAttributes { |
| 4072 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 4073 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 4074 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 4075 | /// kPropertyDynamic = 'D', // property is dynamic |
| 4076 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 4077 | /// kPropertySetter = 'S', // followed by setter selector name |
| 4078 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
| 4079 | /// kPropertyType = 't' // followed by old-style type encoding. |
| 4080 | /// kPropertyWeak = 'W' // 'weak' property |
| 4081 | /// kPropertyStrong = 'P' // property GC'able |
| 4082 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 4083 | /// }; |
| 4084 | /// @endcode |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4085 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4086 | const Decl *Container, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4087 | std::string& S) const { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4088 | // Collect information from the property implementation decl(s). |
| 4089 | bool Dynamic = false; |
| 4090 | ObjCPropertyImplDecl *SynthesizePID = 0; |
| 4091 | |
| 4092 | // FIXME: Duplicated code due to poor abstraction. |
| 4093 | if (Container) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4094 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4095 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
| 4096 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4097 | i = CID->propimpl_begin(), e = CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4098 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4099 | ObjCPropertyImplDecl *PID = *i; |
| 4100 | if (PID->getPropertyDecl() == PD) { |
| 4101 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 4102 | Dynamic = true; |
| 4103 | } else { |
| 4104 | SynthesizePID = PID; |
| 4105 | } |
| 4106 | } |
| 4107 | } |
| 4108 | } else { |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4109 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4110 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4111 | i = OID->propimpl_begin(), e = OID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4112 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4113 | ObjCPropertyImplDecl *PID = *i; |
| 4114 | if (PID->getPropertyDecl() == PD) { |
| 4115 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 4116 | Dynamic = true; |
| 4117 | } else { |
| 4118 | SynthesizePID = PID; |
| 4119 | } |
| 4120 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4121 | } |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4122 | } |
| 4123 | } |
| 4124 | |
| 4125 | // FIXME: This is not very efficient. |
| 4126 | S = "T"; |
| 4127 | |
| 4128 | // Encode result type. |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4129 | // GCC has some special rules regarding encoding of properties which |
| 4130 | // closely resembles encoding of ivars. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4131 | getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4132 | true /* outermost type */, |
| 4133 | true /* encoding for property */); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4134 | |
| 4135 | if (PD->isReadOnly()) { |
| 4136 | S += ",R"; |
| 4137 | } else { |
| 4138 | switch (PD->getSetterKind()) { |
| 4139 | case ObjCPropertyDecl::Assign: break; |
| 4140 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4141 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4142 | } |
| 4143 | } |
| 4144 | |
| 4145 | // It really isn't clear at all what this means, since properties |
| 4146 | // are "dynamic by default". |
| 4147 | if (Dynamic) |
| 4148 | S += ",D"; |
| 4149 | |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4150 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 4151 | S += ",N"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4152 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4153 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 4154 | S += ",G"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4155 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4156 | } |
| 4157 | |
| 4158 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 4159 | S += ",S"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4160 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4161 | } |
| 4162 | |
| 4163 | if (SynthesizePID) { |
| 4164 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 4165 | S += ",V"; |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 4166 | S += OID->getNameAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4167 | } |
| 4168 | |
| 4169 | // FIXME: OBJCGC: weak & strong |
| 4170 | } |
| 4171 | |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4172 | /// getLegacyIntegralTypeEncoding - |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4173 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
| 4174 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4175 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
| 4176 | /// |
| 4177 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 4178 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4179 | if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) { |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4180 | if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4181 | PointeeTy = UnsignedIntTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4182 | else |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4183 | if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4184 | PointeeTy = IntTy; |
| 4185 | } |
| 4186 | } |
| 4187 | } |
| 4188 | |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4189 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4190 | const FieldDecl *Field) const { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4191 | // We follow the behavior of gcc, expanding structures which are |
| 4192 | // directly pointed to, and expanding embedded structures. Note that |
| 4193 | // these rules are sufficient to prevent recursive encoding of the |
| 4194 | // same type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4195 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
Fariborz Jahanian | 5b8c7d9 | 2008-12-22 23:22:27 +0000 | [diff] [blame] | 4196 | true /* outermost type */); |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4197 | } |
| 4198 | |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4199 | static char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) { |
| 4200 | switch (T->getAs<BuiltinType>()->getKind()) { |
| 4201 | default: assert(0 && "Unhandled builtin type kind"); |
| 4202 | case BuiltinType::Void: return 'v'; |
| 4203 | case BuiltinType::Bool: return 'B'; |
| 4204 | case BuiltinType::Char_U: |
| 4205 | case BuiltinType::UChar: return 'C'; |
| 4206 | case BuiltinType::UShort: return 'S'; |
| 4207 | case BuiltinType::UInt: return 'I'; |
| 4208 | case BuiltinType::ULong: |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4209 | return C->getIntWidth(T) == 32 ? 'L' : 'Q'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4210 | case BuiltinType::UInt128: return 'T'; |
| 4211 | case BuiltinType::ULongLong: return 'Q'; |
| 4212 | case BuiltinType::Char_S: |
| 4213 | case BuiltinType::SChar: return 'c'; |
| 4214 | case BuiltinType::Short: return 's'; |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 4215 | case BuiltinType::WChar_S: |
| 4216 | case BuiltinType::WChar_U: |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4217 | case BuiltinType::Int: return 'i'; |
| 4218 | case BuiltinType::Long: |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4219 | return C->getIntWidth(T) == 32 ? 'l' : 'q'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4220 | case BuiltinType::LongLong: return 'q'; |
| 4221 | case BuiltinType::Int128: return 't'; |
| 4222 | case BuiltinType::Float: return 'f'; |
| 4223 | case BuiltinType::Double: return 'd'; |
Daniel Dunbar | 3a0be84 | 2010-10-11 21:13:48 +0000 | [diff] [blame] | 4224 | case BuiltinType::LongDouble: return 'D'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4225 | } |
| 4226 | } |
| 4227 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4228 | static void EncodeBitField(const ASTContext *Ctx, std::string& S, |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4229 | QualType T, const FieldDecl *FD) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4230 | const Expr *E = FD->getBitWidth(); |
| 4231 | assert(E && "bitfield width not there - getObjCEncodingForTypeImpl"); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4232 | S += 'b'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4233 | // The NeXT runtime encodes bit fields as b followed by the number of bits. |
| 4234 | // The GNU runtime requires more information; bitfields are encoded as b, |
| 4235 | // then the offset (in bits) of the first element, then the type of the |
| 4236 | // bitfield, then the size in bits. For example, in this structure: |
| 4237 | // |
| 4238 | // struct |
| 4239 | // { |
| 4240 | // int integer; |
| 4241 | // int flags:2; |
| 4242 | // }; |
| 4243 | // On a 32-bit system, the encoding for flags would be b2 for the NeXT |
| 4244 | // runtime, but b32i2 for the GNU runtime. The reason for this extra |
| 4245 | // information is not especially sensible, but we're stuck with it for |
| 4246 | // compatibility with GCC, although providing it breaks anything that |
| 4247 | // actually uses runtime introspection and wants to work on both runtimes... |
| 4248 | if (!Ctx->getLangOptions().NeXTRuntime) { |
| 4249 | const RecordDecl *RD = FD->getParent(); |
| 4250 | const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD); |
| 4251 | // FIXME: This same linear search is also used in ExprConstant - it might |
| 4252 | // be better if the FieldDecl stored its offset. We'd be increasing the |
| 4253 | // size of the object slightly, but saving some time every time it is used. |
| 4254 | unsigned i = 0; |
| 4255 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 4256 | FieldEnd = RD->field_end(); |
| 4257 | Field != FieldEnd; (void)++Field, ++i) { |
| 4258 | if (*Field == FD) |
| 4259 | break; |
| 4260 | } |
| 4261 | S += llvm::utostr(RL.getFieldOffset(i)); |
David Chisnall | c7ff82c | 2010-12-26 20:12:30 +0000 | [diff] [blame] | 4262 | if (T->isEnumeralType()) |
| 4263 | S += 'i'; |
| 4264 | else |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4265 | S += ObjCEncodingForPrimitiveKind(Ctx, T); |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4266 | } |
| 4267 | unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue(); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4268 | S += llvm::utostr(N); |
| 4269 | } |
| 4270 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 4271 | // FIXME: Use SmallString for accumulating string. |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4272 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 4273 | bool ExpandPointedToStructures, |
| 4274 | bool ExpandStructures, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 4275 | const FieldDecl *FD, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4276 | bool OutermostType, |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4277 | bool EncodingProperty, |
| 4278 | bool StructField) const { |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4279 | if (T->getAs<BuiltinType>()) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4280 | if (FD && FD->isBitField()) |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4281 | return EncodeBitField(this, S, T, FD); |
| 4282 | S += ObjCEncodingForPrimitiveKind(this, T); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4283 | return; |
| 4284 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4285 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4286 | if (const ComplexType *CT = T->getAs<ComplexType>()) { |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 4287 | S += 'j'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4288 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false, |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 4289 | false); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4290 | return; |
| 4291 | } |
Fariborz Jahanian | 60bce3e | 2009-11-23 20:40:50 +0000 | [diff] [blame] | 4292 | |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4293 | // encoding for pointer or r3eference types. |
| 4294 | QualType PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4295 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Fariborz Jahanian | 8d2c0a9 | 2009-11-30 18:43:52 +0000 | [diff] [blame] | 4296 | if (PT->isObjCSelType()) { |
| 4297 | S += ':'; |
| 4298 | return; |
| 4299 | } |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4300 | PointeeTy = PT->getPointeeType(); |
| 4301 | } |
| 4302 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 4303 | PointeeTy = RT->getPointeeType(); |
| 4304 | if (!PointeeTy.isNull()) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4305 | bool isReadOnly = false; |
| 4306 | // For historical/compatibility reasons, the read-only qualifier of the |
| 4307 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 4308 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4309 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 4310 | if (isa<TypedefType>(T.getTypePtr())) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4311 | if (OutermostType && T.isConstQualified()) { |
| 4312 | isReadOnly = true; |
| 4313 | S += 'r'; |
| 4314 | } |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 4315 | } else if (OutermostType) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4316 | QualType P = PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4317 | while (P->getAs<PointerType>()) |
| 4318 | P = P->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4319 | if (P.isConstQualified()) { |
| 4320 | isReadOnly = true; |
| 4321 | S += 'r'; |
| 4322 | } |
| 4323 | } |
| 4324 | if (isReadOnly) { |
| 4325 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 4326 | // combinations need to be rearranged. |
| 4327 | // Rewrite "in const" from "nr" to "rn" |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 4328 | if (llvm::StringRef(S).endswith("nr")) |
| 4329 | S.replace(S.end()-2, S.end(), "rn"); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4330 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4331 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4332 | if (PointeeTy->isCharType()) { |
| 4333 | // char pointer types should be encoded as '*' unless it is a |
| 4334 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 4335 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4336 | S += '*'; |
| 4337 | return; |
| 4338 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4339 | } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) { |
Steve Naroff | 9533a7f | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 4340 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 4341 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 4342 | S += '#'; |
| 4343 | return; |
| 4344 | } |
| 4345 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 4346 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 4347 | S += '@'; |
| 4348 | return; |
| 4349 | } |
| 4350 | // fall through... |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4351 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4352 | S += '^'; |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4353 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 4354 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4355 | getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4356 | NULL); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4357 | return; |
| 4358 | } |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4359 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4360 | if (const ArrayType *AT = |
| 4361 | // Ignore type qualifiers etc. |
| 4362 | dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) { |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4363 | if (isa<IncompleteArrayType>(AT) && !StructField) { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4364 | // Incomplete arrays are encoded as a pointer to the array element. |
| 4365 | S += '^'; |
| 4366 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4367 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4368 | false, ExpandStructures, FD); |
| 4369 | } else { |
| 4370 | S += '['; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4371 | |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4372 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
| 4373 | if (getTypeSize(CAT->getElementType()) == 0) |
| 4374 | S += '0'; |
| 4375 | else |
| 4376 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 4377 | } else { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4378 | //Variable length arrays are encoded as a regular array with 0 elements. |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4379 | assert((isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) && |
| 4380 | "Unknown array type!"); |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4381 | S += '0'; |
| 4382 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4383 | |
| 4384 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4385 | false, ExpandStructures, FD); |
| 4386 | S += ']'; |
| 4387 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4388 | return; |
| 4389 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4390 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4391 | if (T->getAs<FunctionType>()) { |
Anders Carlsson | c0a87b7 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 4392 | S += '?'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4393 | return; |
| 4394 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4395 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4396 | if (const RecordType *RTy = T->getAs<RecordType>()) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4397 | RecordDecl *RDecl = RTy->getDecl(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4398 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 4399 | // Anonymous structures print as '?' |
| 4400 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 4401 | S += II->getName(); |
Fariborz Jahanian | 6fb9439 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 4402 | if (ClassTemplateSpecializationDecl *Spec |
| 4403 | = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) { |
| 4404 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 4405 | std::string TemplateArgsStr |
| 4406 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4407 | TemplateArgs.data(), |
| 4408 | TemplateArgs.size(), |
Fariborz Jahanian | 6fb9439 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 4409 | (*this).PrintingPolicy); |
| 4410 | |
| 4411 | S += TemplateArgsStr; |
| 4412 | } |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 4413 | } else { |
| 4414 | S += '?'; |
| 4415 | } |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 4416 | if (ExpandStructures) { |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4417 | S += '='; |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4418 | if (!RDecl->isUnion()) { |
| 4419 | getObjCEncodingForStructureImpl(RDecl, S, FD); |
| 4420 | } else { |
| 4421 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 4422 | FieldEnd = RDecl->field_end(); |
| 4423 | Field != FieldEnd; ++Field) { |
| 4424 | if (FD) { |
| 4425 | S += '"'; |
| 4426 | S += Field->getNameAsString(); |
| 4427 | S += '"'; |
| 4428 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4429 | |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4430 | // Special case bit-fields. |
| 4431 | if (Field->isBitField()) { |
| 4432 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
| 4433 | (*Field)); |
| 4434 | } else { |
| 4435 | QualType qt = Field->getType(); |
| 4436 | getLegacyIntegralTypeEncoding(qt); |
| 4437 | getObjCEncodingForTypeImpl(qt, S, false, true, |
| 4438 | FD, /*OutermostType*/false, |
| 4439 | /*EncodingProperty*/false, |
| 4440 | /*StructField*/true); |
| 4441 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4442 | } |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4443 | } |
Fariborz Jahanian | 6de88a8 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 4444 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4445 | S += RDecl->isUnion() ? ')' : '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4446 | return; |
| 4447 | } |
Fariborz Jahanian | e6012c7 | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 4448 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4449 | if (T->isEnumeralType()) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4450 | if (FD && FD->isBitField()) |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4451 | EncodeBitField(this, S, T, FD); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4452 | else |
| 4453 | S += 'i'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4454 | return; |
| 4455 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4456 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4457 | if (T->isBlockPointerType()) { |
Steve Naroff | 21a98b1 | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 4458 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4459 | return; |
| 4460 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4461 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4462 | // Ignore protocol qualifiers when mangling at this level. |
| 4463 | if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>()) |
| 4464 | T = OT->getBaseType(); |
| 4465 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4466 | if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4467 | // @encode(class_name) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4468 | ObjCInterfaceDecl *OI = OIT->getDecl(); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4469 | S += '{'; |
| 4470 | const IdentifierInfo *II = OI->getIdentifier(); |
| 4471 | S += II->getName(); |
| 4472 | S += '='; |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4473 | llvm::SmallVector<ObjCIvarDecl*, 32> Ivars; |
| 4474 | DeepCollectObjCIvars(OI, true, Ivars); |
| 4475 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) { |
| 4476 | FieldDecl *Field = cast<FieldDecl>(Ivars[i]); |
| 4477 | if (Field->isBitField()) |
| 4478 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4479 | else |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4480 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4481 | } |
| 4482 | S += '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4483 | return; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4484 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4485 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4486 | if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4487 | if (OPT->isObjCIdType()) { |
| 4488 | S += '@'; |
| 4489 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4490 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4491 | |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4492 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
| 4493 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
| 4494 | // Since this is a binary compatibility issue, need to consult with runtime |
| 4495 | // folks. Fortunately, this is a *very* obsure construct. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4496 | S += '#'; |
| 4497 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4498 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4499 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4500 | if (OPT->isObjCQualifiedIdType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4501 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4502 | ExpandPointedToStructures, |
| 4503 | ExpandStructures, FD); |
| 4504 | if (FD || EncodingProperty) { |
| 4505 | // Note that we do extended encoding of protocol qualifer list |
| 4506 | // Only when doing ivar or property encoding. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4507 | S += '"'; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4508 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 4509 | E = OPT->qual_end(); I != E; ++I) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4510 | S += '<'; |
| 4511 | S += (*I)->getNameAsString(); |
| 4512 | S += '>'; |
| 4513 | } |
| 4514 | S += '"'; |
| 4515 | } |
| 4516 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4517 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4518 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4519 | QualType PointeeTy = OPT->getPointeeType(); |
| 4520 | if (!EncodingProperty && |
| 4521 | isa<TypedefType>(PointeeTy.getTypePtr())) { |
| 4522 | // Another historical/compatibility reason. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4523 | // We encode the underlying type which comes out as |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4524 | // {...}; |
| 4525 | S += '^'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4526 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 4527 | false, ExpandPointedToStructures, |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4528 | NULL); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4529 | return; |
| 4530 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4531 | |
| 4532 | S += '@'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4533 | if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4534 | S += '"'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4535 | S += OPT->getInterfaceDecl()->getIdentifier()->getName(); |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4536 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 4537 | E = OPT->qual_end(); I != E; ++I) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4538 | S += '<'; |
| 4539 | S += (*I)->getNameAsString(); |
| 4540 | S += '>'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4541 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4542 | S += '"'; |
| 4543 | } |
| 4544 | return; |
| 4545 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4546 | |
John McCall | 532ec7b | 2010-05-17 23:56:34 +0000 | [diff] [blame] | 4547 | // gcc just blithely ignores member pointers. |
| 4548 | // TODO: maybe there should be a mangling for these |
| 4549 | if (T->getAs<MemberPointerType>()) |
| 4550 | return; |
Fariborz Jahanian | e6012c7 | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 4551 | |
| 4552 | if (T->isVectorType()) { |
| 4553 | // This matches gcc's encoding, even though technically it is |
| 4554 | // insufficient. |
| 4555 | // FIXME. We should do a better job than gcc. |
| 4556 | return; |
| 4557 | } |
| 4558 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4559 | assert(0 && "@encode for type not implemented!"); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4560 | } |
| 4561 | |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4562 | void ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl, |
| 4563 | std::string &S, |
| 4564 | const FieldDecl *FD, |
| 4565 | bool includeVBases) const { |
| 4566 | assert(RDecl && "Expected non-null RecordDecl"); |
| 4567 | assert(!RDecl->isUnion() && "Should not be called for unions"); |
| 4568 | if (!RDecl->getDefinition()) |
| 4569 | return; |
| 4570 | |
| 4571 | CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(RDecl); |
| 4572 | std::multimap<uint64_t, NamedDecl *> FieldOrBaseOffsets; |
| 4573 | const ASTRecordLayout &layout = getASTRecordLayout(RDecl); |
| 4574 | |
| 4575 | if (CXXRec) { |
| 4576 | for (CXXRecordDecl::base_class_iterator |
| 4577 | BI = CXXRec->bases_begin(), |
| 4578 | BE = CXXRec->bases_end(); BI != BE; ++BI) { |
| 4579 | if (!BI->isVirtual()) { |
| 4580 | CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 829f200 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 4581 | if (base->isEmpty()) |
| 4582 | continue; |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4583 | uint64_t offs = layout.getBaseClassOffsetInBits(base); |
| 4584 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 4585 | std::make_pair(offs, base)); |
| 4586 | } |
| 4587 | } |
| 4588 | } |
| 4589 | |
| 4590 | unsigned i = 0; |
| 4591 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 4592 | FieldEnd = RDecl->field_end(); |
| 4593 | Field != FieldEnd; ++Field, ++i) { |
| 4594 | uint64_t offs = layout.getFieldOffset(i); |
| 4595 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 4596 | std::make_pair(offs, *Field)); |
| 4597 | } |
| 4598 | |
| 4599 | if (CXXRec && includeVBases) { |
| 4600 | for (CXXRecordDecl::base_class_iterator |
| 4601 | BI = CXXRec->vbases_begin(), |
| 4602 | BE = CXXRec->vbases_end(); BI != BE; ++BI) { |
| 4603 | CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 829f200 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 4604 | if (base->isEmpty()) |
| 4605 | continue; |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4606 | uint64_t offs = layout.getVBaseClassOffsetInBits(base); |
| 4607 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 4608 | std::make_pair(offs, base)); |
| 4609 | } |
| 4610 | } |
| 4611 | |
| 4612 | CharUnits size; |
| 4613 | if (CXXRec) { |
| 4614 | size = includeVBases ? layout.getSize() : layout.getNonVirtualSize(); |
| 4615 | } else { |
| 4616 | size = layout.getSize(); |
| 4617 | } |
| 4618 | |
| 4619 | uint64_t CurOffs = 0; |
| 4620 | std::multimap<uint64_t, NamedDecl *>::iterator |
| 4621 | CurLayObj = FieldOrBaseOffsets.begin(); |
| 4622 | |
| 4623 | if (CurLayObj != FieldOrBaseOffsets.end() && CurLayObj->first != 0) { |
| 4624 | assert(CXXRec && CXXRec->isDynamicClass() && |
| 4625 | "Offset 0 was empty but no VTable ?"); |
| 4626 | if (FD) { |
| 4627 | S += "\"_vptr$"; |
| 4628 | std::string recname = CXXRec->getNameAsString(); |
| 4629 | if (recname.empty()) recname = "?"; |
| 4630 | S += recname; |
| 4631 | S += '"'; |
| 4632 | } |
| 4633 | S += "^^?"; |
| 4634 | CurOffs += getTypeSize(VoidPtrTy); |
| 4635 | } |
| 4636 | |
| 4637 | if (!RDecl->hasFlexibleArrayMember()) { |
| 4638 | // Mark the end of the structure. |
| 4639 | uint64_t offs = toBits(size); |
| 4640 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 4641 | std::make_pair(offs, (NamedDecl*)0)); |
| 4642 | } |
| 4643 | |
| 4644 | for (; CurLayObj != FieldOrBaseOffsets.end(); ++CurLayObj) { |
| 4645 | assert(CurOffs <= CurLayObj->first); |
| 4646 | |
| 4647 | if (CurOffs < CurLayObj->first) { |
| 4648 | uint64_t padding = CurLayObj->first - CurOffs; |
| 4649 | // FIXME: There doesn't seem to be a way to indicate in the encoding that |
| 4650 | // packing/alignment of members is different that normal, in which case |
| 4651 | // the encoding will be out-of-sync with the real layout. |
| 4652 | // If the runtime switches to just consider the size of types without |
| 4653 | // taking into account alignment, we could make padding explicit in the |
| 4654 | // encoding (e.g. using arrays of chars). The encoding strings would be |
| 4655 | // longer then though. |
| 4656 | CurOffs += padding; |
| 4657 | } |
| 4658 | |
| 4659 | NamedDecl *dcl = CurLayObj->second; |
| 4660 | if (dcl == 0) |
| 4661 | break; // reached end of structure. |
| 4662 | |
| 4663 | if (CXXRecordDecl *base = dyn_cast<CXXRecordDecl>(dcl)) { |
| 4664 | // We expand the bases without their virtual bases since those are going |
| 4665 | // in the initial structure. Note that this differs from gcc which |
| 4666 | // expands virtual bases each time one is encountered in the hierarchy, |
| 4667 | // making the encoding type bigger than it really is. |
| 4668 | getObjCEncodingForStructureImpl(base, S, FD, /*includeVBases*/false); |
Argyrios Kyrtzidis | 829f200 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 4669 | assert(!base->isEmpty()); |
| 4670 | CurOffs += toBits(getASTRecordLayout(base).getNonVirtualSize()); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4671 | } else { |
| 4672 | FieldDecl *field = cast<FieldDecl>(dcl); |
| 4673 | if (FD) { |
| 4674 | S += '"'; |
| 4675 | S += field->getNameAsString(); |
| 4676 | S += '"'; |
| 4677 | } |
| 4678 | |
| 4679 | if (field->isBitField()) { |
| 4680 | EncodeBitField(this, S, field->getType(), field); |
| 4681 | CurOffs += field->getBitWidth()->EvaluateAsInt(*this).getZExtValue(); |
| 4682 | } else { |
| 4683 | QualType qt = field->getType(); |
| 4684 | getLegacyIntegralTypeEncoding(qt); |
| 4685 | getObjCEncodingForTypeImpl(qt, S, false, true, FD, |
| 4686 | /*OutermostType*/false, |
| 4687 | /*EncodingProperty*/false, |
| 4688 | /*StructField*/true); |
| 4689 | CurOffs += getTypeSize(field->getType()); |
| 4690 | } |
| 4691 | } |
| 4692 | } |
| 4693 | } |
| 4694 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4695 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 4696 | std::string& S) const { |
| 4697 | if (QT & Decl::OBJC_TQ_In) |
| 4698 | S += 'n'; |
| 4699 | if (QT & Decl::OBJC_TQ_Inout) |
| 4700 | S += 'N'; |
| 4701 | if (QT & Decl::OBJC_TQ_Out) |
| 4702 | S += 'o'; |
| 4703 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 4704 | S += 'O'; |
| 4705 | if (QT & Decl::OBJC_TQ_Byref) |
| 4706 | S += 'R'; |
| 4707 | if (QT & Decl::OBJC_TQ_Oneway) |
| 4708 | S += 'V'; |
| 4709 | } |
| 4710 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4711 | void ASTContext::setBuiltinVaListType(QualType T) { |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 4712 | assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4713 | |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 4714 | BuiltinVaListType = T; |
| 4715 | } |
| 4716 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4717 | void ASTContext::setObjCIdType(QualType T) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 4718 | ObjCIdTypedefType = T; |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 4719 | } |
| 4720 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4721 | void ASTContext::setObjCSelType(QualType T) { |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 4722 | ObjCSelTypedefType = T; |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 4723 | } |
| 4724 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4725 | void ASTContext::setObjCProtoType(QualType QT) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4726 | ObjCProtoType = QT; |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 4727 | } |
| 4728 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4729 | void ASTContext::setObjCClassType(QualType T) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 4730 | ObjCClassTypedefType = T; |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 4731 | } |
| 4732 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4733 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4734 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 4735 | "'NSConstantString' type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4736 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4737 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 4738 | } |
| 4739 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4740 | /// \brief Retrieve the template name that corresponds to a non-empty |
| 4741 | /// lookup. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4742 | TemplateName |
| 4743 | ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin, |
| 4744 | UnresolvedSetIterator End) const { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4745 | unsigned size = End - Begin; |
| 4746 | assert(size > 1 && "set is not overloaded!"); |
| 4747 | |
| 4748 | void *memory = Allocate(sizeof(OverloadedTemplateStorage) + |
| 4749 | size * sizeof(FunctionTemplateDecl*)); |
| 4750 | OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size); |
| 4751 | |
| 4752 | NamedDecl **Storage = OT->getStorage(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4753 | for (UnresolvedSetIterator I = Begin; I != End; ++I) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4754 | NamedDecl *D = *I; |
| 4755 | assert(isa<FunctionTemplateDecl>(D) || |
| 4756 | (isa<UsingShadowDecl>(D) && |
| 4757 | isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))); |
| 4758 | *Storage++ = D; |
| 4759 | } |
| 4760 | |
| 4761 | return TemplateName(OT); |
| 4762 | } |
| 4763 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4764 | /// \brief Retrieve the template name that represents a qualified |
| 4765 | /// template name such as \c std::vector. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4766 | TemplateName |
| 4767 | ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
| 4768 | bool TemplateKeyword, |
| 4769 | TemplateDecl *Template) const { |
Douglas Gregor | 0f0ea2a | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 4770 | assert(NNS && "Missing nested-name-specifier in qualified template name"); |
| 4771 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4772 | // FIXME: Canonicalization? |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4773 | llvm::FoldingSetNodeID ID; |
| 4774 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 4775 | |
| 4776 | void *InsertPos = 0; |
| 4777 | QualifiedTemplateName *QTN = |
| 4778 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4779 | if (!QTN) { |
| 4780 | QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); |
| 4781 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 4782 | } |
| 4783 | |
| 4784 | return TemplateName(QTN); |
| 4785 | } |
| 4786 | |
| 4787 | /// \brief Retrieve the template name that represents a dependent |
| 4788 | /// template name such as \c MetaFun::template apply. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4789 | TemplateName |
| 4790 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
| 4791 | const IdentifierInfo *Name) const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4792 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 4793 | "Nested name specifier must be dependent"); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4794 | |
| 4795 | llvm::FoldingSetNodeID ID; |
| 4796 | DependentTemplateName::Profile(ID, NNS, Name); |
| 4797 | |
| 4798 | void *InsertPos = 0; |
| 4799 | DependentTemplateName *QTN = |
| 4800 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4801 | |
| 4802 | if (QTN) |
| 4803 | return TemplateName(QTN); |
| 4804 | |
| 4805 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 4806 | if (CanonNNS == NNS) { |
| 4807 | QTN = new (*this,4) DependentTemplateName(NNS, Name); |
| 4808 | } else { |
| 4809 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
| 4810 | QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4811 | DependentTemplateName *CheckQTN = |
| 4812 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4813 | assert(!CheckQTN && "Dependent type name canonicalization broken"); |
| 4814 | (void)CheckQTN; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4815 | } |
| 4816 | |
| 4817 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 4818 | return TemplateName(QTN); |
| 4819 | } |
| 4820 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4821 | /// \brief Retrieve the template name that represents a dependent |
| 4822 | /// template name such as \c MetaFun::template operator+. |
| 4823 | TemplateName |
| 4824 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4825 | OverloadedOperatorKind Operator) const { |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4826 | assert((!NNS || NNS->isDependent()) && |
| 4827 | "Nested name specifier must be dependent"); |
| 4828 | |
| 4829 | llvm::FoldingSetNodeID ID; |
| 4830 | DependentTemplateName::Profile(ID, NNS, Operator); |
| 4831 | |
| 4832 | void *InsertPos = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4833 | DependentTemplateName *QTN |
| 4834 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4835 | |
| 4836 | if (QTN) |
| 4837 | return TemplateName(QTN); |
| 4838 | |
| 4839 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 4840 | if (CanonNNS == NNS) { |
| 4841 | QTN = new (*this,4) DependentTemplateName(NNS, Operator); |
| 4842 | } else { |
| 4843 | TemplateName Canon = getDependentTemplateName(CanonNNS, Operator); |
| 4844 | QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4845 | |
| 4846 | DependentTemplateName *CheckQTN |
| 4847 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4848 | assert(!CheckQTN && "Dependent template name canonicalization broken"); |
| 4849 | (void)CheckQTN; |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4850 | } |
| 4851 | |
| 4852 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 4853 | return TemplateName(QTN); |
| 4854 | } |
| 4855 | |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 4856 | TemplateName |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 4857 | ASTContext::getSubstTemplateTemplateParm(TemplateTemplateParmDecl *param, |
| 4858 | TemplateName replacement) const { |
| 4859 | llvm::FoldingSetNodeID ID; |
| 4860 | SubstTemplateTemplateParmStorage::Profile(ID, param, replacement); |
| 4861 | |
| 4862 | void *insertPos = 0; |
| 4863 | SubstTemplateTemplateParmStorage *subst |
| 4864 | = SubstTemplateTemplateParms.FindNodeOrInsertPos(ID, insertPos); |
| 4865 | |
| 4866 | if (!subst) { |
| 4867 | subst = new (*this) SubstTemplateTemplateParmStorage(param, replacement); |
| 4868 | SubstTemplateTemplateParms.InsertNode(subst, insertPos); |
| 4869 | } |
| 4870 | |
| 4871 | return TemplateName(subst); |
| 4872 | } |
| 4873 | |
| 4874 | TemplateName |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 4875 | ASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param, |
| 4876 | const TemplateArgument &ArgPack) const { |
| 4877 | ASTContext &Self = const_cast<ASTContext &>(*this); |
| 4878 | llvm::FoldingSetNodeID ID; |
| 4879 | SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack); |
| 4880 | |
| 4881 | void *InsertPos = 0; |
| 4882 | SubstTemplateTemplateParmPackStorage *Subst |
| 4883 | = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos); |
| 4884 | |
| 4885 | if (!Subst) { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 4886 | Subst = new (*this) SubstTemplateTemplateParmPackStorage(Param, |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 4887 | ArgPack.pack_size(), |
| 4888 | ArgPack.pack_begin()); |
| 4889 | SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos); |
| 4890 | } |
| 4891 | |
| 4892 | return TemplateName(Subst); |
| 4893 | } |
| 4894 | |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4895 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | d934112 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 4896 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 4897 | /// is actually a value of type @c TargetInfo::IntType. |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 4898 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4899 | switch (Type) { |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 4900 | case TargetInfo::NoInt: return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4901 | case TargetInfo::SignedShort: return ShortTy; |
| 4902 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 4903 | case TargetInfo::SignedInt: return IntTy; |
| 4904 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 4905 | case TargetInfo::SignedLong: return LongTy; |
| 4906 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 4907 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 4908 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 4909 | } |
| 4910 | |
| 4911 | assert(false && "Unhandled TargetInfo::IntType value"); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 4912 | return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4913 | } |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 4914 | |
| 4915 | //===----------------------------------------------------------------------===// |
| 4916 | // Type Predicates. |
| 4917 | //===----------------------------------------------------------------------===// |
| 4918 | |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4919 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 4920 | /// garbage collection attribute. |
| 4921 | /// |
John McCall | ae278a3 | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 4922 | Qualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const { |
| 4923 | if (getLangOptions().getGCMode() == LangOptions::NonGC) |
| 4924 | return Qualifiers::GCNone; |
| 4925 | |
| 4926 | assert(getLangOptions().ObjC1); |
| 4927 | Qualifiers::GC GCAttrs = Ty.getObjCGCAttr(); |
| 4928 | |
| 4929 | // Default behaviour under objective-C's gc is for ObjC pointers |
| 4930 | // (or pointers to them) be treated as though they were declared |
| 4931 | // as __strong. |
| 4932 | if (GCAttrs == Qualifiers::GCNone) { |
| 4933 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
| 4934 | return Qualifiers::Strong; |
| 4935 | else if (Ty->isPointerType()) |
| 4936 | return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); |
| 4937 | } else { |
| 4938 | // It's not valid to set GC attributes on anything that isn't a |
| 4939 | // pointer. |
| 4940 | #ifndef NDEBUG |
| 4941 | QualType CT = Ty->getCanonicalTypeInternal(); |
| 4942 | while (const ArrayType *AT = dyn_cast<ArrayType>(CT)) |
| 4943 | CT = AT->getElementType(); |
| 4944 | assert(CT->isAnyPointerType() || CT->isBlockPointerType()); |
| 4945 | #endif |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4946 | } |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 4947 | return GCAttrs; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4948 | } |
| 4949 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4950 | //===----------------------------------------------------------------------===// |
| 4951 | // Type Compatibility Testing |
| 4952 | //===----------------------------------------------------------------------===// |
Chris Lattner | 770951b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 4953 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4954 | /// areCompatVectorTypes - Return true if the two specified vector types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4955 | /// compatible. |
| 4956 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 4957 | const VectorType *RHS) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 4958 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4959 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4960 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4961 | } |
| 4962 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 4963 | bool ASTContext::areCompatibleVectorTypes(QualType FirstVec, |
| 4964 | QualType SecondVec) { |
| 4965 | assert(FirstVec->isVectorType() && "FirstVec should be a vector type"); |
| 4966 | assert(SecondVec->isVectorType() && "SecondVec should be a vector type"); |
| 4967 | |
| 4968 | if (hasSameUnqualifiedType(FirstVec, SecondVec)) |
| 4969 | return true; |
| 4970 | |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 4971 | // Treat Neon vector types and most AltiVec vector types as if they are the |
| 4972 | // equivalent GCC vector types. |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 4973 | const VectorType *First = FirstVec->getAs<VectorType>(); |
| 4974 | const VectorType *Second = SecondVec->getAs<VectorType>(); |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 4975 | if (First->getNumElements() == Second->getNumElements() && |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 4976 | hasSameType(First->getElementType(), Second->getElementType()) && |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 4977 | First->getVectorKind() != VectorType::AltiVecPixel && |
| 4978 | First->getVectorKind() != VectorType::AltiVecBool && |
| 4979 | Second->getVectorKind() != VectorType::AltiVecPixel && |
| 4980 | Second->getVectorKind() != VectorType::AltiVecBool) |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 4981 | return true; |
| 4982 | |
| 4983 | return false; |
| 4984 | } |
| 4985 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4986 | //===----------------------------------------------------------------------===// |
| 4987 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 4988 | //===----------------------------------------------------------------------===// |
| 4989 | |
| 4990 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 4991 | /// inheritance hierarchy of 'rProto'. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4992 | bool |
| 4993 | ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 4994 | ObjCProtocolDecl *rProto) const { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4995 | if (lProto == rProto) |
| 4996 | return true; |
| 4997 | for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), |
| 4998 | E = rProto->protocol_end(); PI != E; ++PI) |
| 4999 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 5000 | return true; |
| 5001 | return false; |
| 5002 | } |
| 5003 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5004 | /// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...> |
| 5005 | /// return true if lhs's protocols conform to rhs's protocol; false |
| 5006 | /// otherwise. |
| 5007 | bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) { |
| 5008 | if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType()) |
| 5009 | return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false); |
| 5010 | return false; |
| 5011 | } |
| 5012 | |
Fariborz Jahanian | a8f8dac | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 5013 | /// ObjCQualifiedClassTypesAreCompatible - compare Class<p,...> and |
| 5014 | /// Class<p1, ...>. |
| 5015 | bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs, |
| 5016 | QualType rhs) { |
| 5017 | const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>(); |
| 5018 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
| 5019 | assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible"); |
| 5020 | |
| 5021 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5022 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5023 | bool match = false; |
| 5024 | ObjCProtocolDecl *lhsProto = *I; |
| 5025 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 5026 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 5027 | ObjCProtocolDecl *rhsProto = *J; |
| 5028 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) { |
| 5029 | match = true; |
| 5030 | break; |
| 5031 | } |
| 5032 | } |
| 5033 | if (!match) |
| 5034 | return false; |
| 5035 | } |
| 5036 | return true; |
| 5037 | } |
| 5038 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5039 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 5040 | /// ObjCQualifiedIDType. |
| 5041 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 5042 | bool compare) { |
| 5043 | // Allow id<P..> and an 'id' or void* type in all cases. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5044 | if (lhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5045 | lhs->isObjCIdType() || lhs->isObjCClassType()) |
| 5046 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5047 | else if (rhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5048 | rhs->isObjCIdType() || rhs->isObjCClassType()) |
| 5049 | return true; |
| 5050 | |
| 5051 | if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5052 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5053 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5054 | if (!rhsOPT) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5055 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5056 | if (rhsOPT->qual_empty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5057 | // If the RHS is a unqualified interface pointer "NSString*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5058 | // make sure we check the class hierarchy. |
| 5059 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 5060 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5061 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5062 | // when comparing an id<P> on lhs with a static type on rhs, |
| 5063 | // see if static class implements all of id's protocols, directly or |
| 5064 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 5065 | if (!rhsID->ClassImplementsProtocol(*I, true)) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5066 | return false; |
| 5067 | } |
| 5068 | } |
| 5069 | // If there are no qualifiers and no interface, we have an 'id'. |
| 5070 | return true; |
| 5071 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5072 | // Both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5073 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5074 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5075 | ObjCProtocolDecl *lhsProto = *I; |
| 5076 | bool match = false; |
| 5077 | |
| 5078 | // when comparing an id<P> on lhs with a static type on rhs, |
| 5079 | // see if static class implements all of id's protocols, directly or |
| 5080 | // through its super class and categories. |
| 5081 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 5082 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 5083 | ObjCProtocolDecl *rhsProto = *J; |
| 5084 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 5085 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 5086 | match = true; |
| 5087 | break; |
| 5088 | } |
| 5089 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5090 | // If the RHS is a qualified interface pointer "NSString<P>*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5091 | // make sure we check the class hierarchy. |
| 5092 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 5093 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5094 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5095 | // when comparing an id<P> on lhs with a static type on rhs, |
| 5096 | // see if static class implements all of id's protocols, directly or |
| 5097 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 5098 | if (rhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5099 | match = true; |
| 5100 | break; |
| 5101 | } |
| 5102 | } |
| 5103 | } |
| 5104 | if (!match) |
| 5105 | return false; |
| 5106 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5107 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5108 | return true; |
| 5109 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5110 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5111 | const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 5112 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 5113 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5114 | if (const ObjCObjectPointerType *lhsOPT = |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5115 | lhs->getAsObjCInterfacePointerType()) { |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5116 | // If both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5117 | for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(), |
| 5118 | E = lhsOPT->qual_end(); I != E; ++I) { |
| 5119 | ObjCProtocolDecl *lhsProto = *I; |
| 5120 | bool match = false; |
| 5121 | |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5122 | // when comparing an id<P> on rhs with a static type on lhs, |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5123 | // see if static class implements all of id's protocols, directly or |
| 5124 | // through its super class and categories. |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5125 | // First, lhs protocols in the qualifier list must be found, direct |
| 5126 | // or indirect in rhs's qualifier list or it is a mismatch. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5127 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 5128 | E = rhsQID->qual_end(); J != E; ++J) { |
| 5129 | ObjCProtocolDecl *rhsProto = *J; |
| 5130 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 5131 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 5132 | match = true; |
| 5133 | break; |
| 5134 | } |
| 5135 | } |
| 5136 | if (!match) |
| 5137 | return false; |
| 5138 | } |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5139 | |
| 5140 | // Static class's protocols, or its super class or category protocols |
| 5141 | // must be found, direct or indirect in rhs's qualifier list or it is a mismatch. |
| 5142 | if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { |
| 5143 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
| 5144 | CollectInheritedProtocols(lhsID, LHSInheritedProtocols); |
| 5145 | // This is rather dubious but matches gcc's behavior. If lhs has |
| 5146 | // no type qualifier and its class has no static protocol(s) |
| 5147 | // assume that it is mismatch. |
| 5148 | if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty()) |
| 5149 | return false; |
| 5150 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 5151 | LHSInheritedProtocols.begin(), |
| 5152 | E = LHSInheritedProtocols.end(); I != E; ++I) { |
| 5153 | bool match = false; |
| 5154 | ObjCProtocolDecl *lhsProto = (*I); |
| 5155 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 5156 | E = rhsQID->qual_end(); J != E; ++J) { |
| 5157 | ObjCProtocolDecl *rhsProto = *J; |
| 5158 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 5159 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 5160 | match = true; |
| 5161 | break; |
| 5162 | } |
| 5163 | } |
| 5164 | if (!match) |
| 5165 | return false; |
| 5166 | } |
| 5167 | } |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5168 | return true; |
| 5169 | } |
| 5170 | return false; |
| 5171 | } |
| 5172 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5173 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5174 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 5175 | /// protocol qualifiers on the LHS or RHS. |
| 5176 | /// |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5177 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 5178 | const ObjCObjectPointerType *RHSOPT) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5179 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 5180 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 5181 | |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 5182 | // If either type represents the built-in 'id' or 'Class' types, return true. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5183 | if (LHS->isObjCUnqualifiedIdOrClass() || |
| 5184 | RHS->isObjCUnqualifiedIdOrClass()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5185 | return true; |
| 5186 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5187 | if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5188 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 5189 | QualType(RHSOPT,0), |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5190 | false); |
Fariborz Jahanian | a8f8dac | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 5191 | |
| 5192 | if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass()) |
| 5193 | return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0), |
| 5194 | QualType(RHSOPT,0)); |
| 5195 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5196 | // If we have 2 user-defined types, fall into that path. |
| 5197 | if (LHS->getInterface() && RHS->getInterface()) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5198 | return canAssignObjCInterfaces(LHS, RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5199 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5200 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5201 | } |
| 5202 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5203 | /// canAssignObjCInterfacesInBlockPointer - This routine is specifically written |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 5204 | /// for providing type-safety for objective-c pointers used to pass/return |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5205 | /// arguments in block literals. When passed as arguments, passing 'A*' where |
| 5206 | /// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is |
| 5207 | /// not OK. For the return type, the opposite is not OK. |
| 5208 | bool ASTContext::canAssignObjCInterfacesInBlockPointer( |
| 5209 | const ObjCObjectPointerType *LHSOPT, |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5210 | const ObjCObjectPointerType *RHSOPT, |
| 5211 | bool BlockReturnType) { |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 5212 | if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5213 | return true; |
| 5214 | |
| 5215 | if (LHSOPT->isObjCBuiltinType()) { |
| 5216 | return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType(); |
| 5217 | } |
| 5218 | |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 5219 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5220 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 5221 | QualType(RHSOPT,0), |
| 5222 | false); |
| 5223 | |
| 5224 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 5225 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 5226 | if (LHS && RHS) { // We have 2 user-defined types. |
| 5227 | if (LHS != RHS) { |
| 5228 | if (LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5229 | return BlockReturnType; |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5230 | if (RHS->getDecl()->isSuperClassOf(LHS->getDecl())) |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5231 | return !BlockReturnType; |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5232 | } |
| 5233 | else |
| 5234 | return true; |
| 5235 | } |
| 5236 | return false; |
| 5237 | } |
| 5238 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5239 | /// getIntersectionOfProtocols - This routine finds the intersection of set |
| 5240 | /// of protocols inherited from two distinct objective-c pointer objects. |
| 5241 | /// It is used to build composite qualifier list of the composite type of |
| 5242 | /// the conditional expression involving two objective-c pointer objects. |
| 5243 | static |
| 5244 | void getIntersectionOfProtocols(ASTContext &Context, |
| 5245 | const ObjCObjectPointerType *LHSOPT, |
| 5246 | const ObjCObjectPointerType *RHSOPT, |
| 5247 | llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) { |
| 5248 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5249 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 5250 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 5251 | assert(LHS->getInterface() && "LHS must have an interface base"); |
| 5252 | assert(RHS->getInterface() && "RHS must have an interface base"); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5253 | |
| 5254 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet; |
| 5255 | unsigned LHSNumProtocols = LHS->getNumProtocols(); |
| 5256 | if (LHSNumProtocols > 0) |
| 5257 | InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end()); |
| 5258 | else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 5259 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5260 | Context.CollectInheritedProtocols(LHS->getInterface(), |
| 5261 | LHSInheritedProtocols); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5262 | InheritedProtocolSet.insert(LHSInheritedProtocols.begin(), |
| 5263 | LHSInheritedProtocols.end()); |
| 5264 | } |
| 5265 | |
| 5266 | unsigned RHSNumProtocols = RHS->getNumProtocols(); |
| 5267 | if (RHSNumProtocols > 0) { |
Dan Gohman | cb421fa | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 5268 | ObjCProtocolDecl **RHSProtocols = |
| 5269 | const_cast<ObjCProtocolDecl **>(RHS->qual_begin()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5270 | for (unsigned i = 0; i < RHSNumProtocols; ++i) |
| 5271 | if (InheritedProtocolSet.count(RHSProtocols[i])) |
| 5272 | IntersectionOfProtocols.push_back(RHSProtocols[i]); |
| 5273 | } |
| 5274 | else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 5275 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5276 | Context.CollectInheritedProtocols(RHS->getInterface(), |
| 5277 | RHSInheritedProtocols); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 5278 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 5279 | RHSInheritedProtocols.begin(), |
| 5280 | E = RHSInheritedProtocols.end(); I != E; ++I) |
| 5281 | if (InheritedProtocolSet.count((*I))) |
| 5282 | IntersectionOfProtocols.push_back((*I)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5283 | } |
| 5284 | } |
| 5285 | |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 5286 | /// areCommonBaseCompatible - Returns common base class of the two classes if |
| 5287 | /// one found. Note that this is O'2 algorithm. But it will be called as the |
| 5288 | /// last type comparison in a ?-exp of ObjC pointer types before a |
| 5289 | /// warning is issued. So, its invokation is extremely rare. |
| 5290 | QualType ASTContext::areCommonBaseCompatible( |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5291 | const ObjCObjectPointerType *Lptr, |
| 5292 | const ObjCObjectPointerType *Rptr) { |
| 5293 | const ObjCObjectType *LHS = Lptr->getObjectType(); |
| 5294 | const ObjCObjectType *RHS = Rptr->getObjectType(); |
| 5295 | const ObjCInterfaceDecl* LDecl = LHS->getInterface(); |
| 5296 | const ObjCInterfaceDecl* RDecl = RHS->getInterface(); |
Fariborz Jahanian | 7c2bdcb | 2011-04-18 21:16:59 +0000 | [diff] [blame] | 5297 | if (!LDecl || !RDecl || (LDecl == RDecl)) |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 5298 | return QualType(); |
| 5299 | |
Fariborz Jahanian | 7c2bdcb | 2011-04-18 21:16:59 +0000 | [diff] [blame] | 5300 | do { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5301 | LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5302 | if (canAssignObjCInterfaces(LHS, RHS)) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5303 | llvm::SmallVector<ObjCProtocolDecl *, 8> Protocols; |
| 5304 | getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols); |
| 5305 | |
| 5306 | QualType Result = QualType(LHS, 0); |
| 5307 | if (!Protocols.empty()) |
| 5308 | Result = getObjCObjectType(Result, Protocols.data(), Protocols.size()); |
| 5309 | Result = getObjCObjectPointerType(Result); |
| 5310 | return Result; |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5311 | } |
Fariborz Jahanian | 7c2bdcb | 2011-04-18 21:16:59 +0000 | [diff] [blame] | 5312 | } while ((LDecl = LDecl->getSuperClass())); |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 5313 | |
| 5314 | return QualType(); |
| 5315 | } |
| 5316 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5317 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS, |
| 5318 | const ObjCObjectType *RHS) { |
| 5319 | assert(LHS->getInterface() && "LHS is not an interface type"); |
| 5320 | assert(RHS->getInterface() && "RHS is not an interface type"); |
| 5321 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5322 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 5323 | // the LHS. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5324 | if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface())) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5325 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5326 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5327 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 5328 | // protocol qualified at all, then we are good. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 5329 | if (LHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5330 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5331 | |
Fariborz Jahanian | b7bc34a | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 5332 | // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, |
| 5333 | // more detailed analysis is required. |
| 5334 | if (RHS->getNumProtocols() == 0) { |
| 5335 | // OK, if LHS is a superclass of RHS *and* |
| 5336 | // this superclass is assignment compatible with LHS. |
| 5337 | // false otherwise. |
Fariborz Jahanian | 627788c | 2011-04-12 16:34:14 +0000 | [diff] [blame] | 5338 | bool IsSuperClass = |
| 5339 | LHS->getInterface()->isSuperClassOf(RHS->getInterface()); |
| 5340 | if (IsSuperClass) { |
Fariborz Jahanian | b7bc34a | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 5341 | // OK if conversion of LHS to SuperClass results in narrowing of types |
| 5342 | // ; i.e., SuperClass may implement at least one of the protocols |
| 5343 | // in LHS's protocol list. Example, SuperObj<P1> = lhs<P1,P2> is ok. |
| 5344 | // But not SuperObj<P1,P2,P3> = lhs<P1,P2>. |
| 5345 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> SuperClassInheritedProtocols; |
Fariborz Jahanian | 627788c | 2011-04-12 16:34:14 +0000 | [diff] [blame] | 5346 | CollectInheritedProtocols(RHS->getInterface(), SuperClassInheritedProtocols); |
Fariborz Jahanian | b7bc34a | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 5347 | // If super class has no protocols, it is not a match. |
| 5348 | if (SuperClassInheritedProtocols.empty()) |
| 5349 | return false; |
| 5350 | |
| 5351 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 5352 | LHSPE = LHS->qual_end(); |
| 5353 | LHSPI != LHSPE; LHSPI++) { |
| 5354 | bool SuperImplementsProtocol = false; |
| 5355 | ObjCProtocolDecl *LHSProto = (*LHSPI); |
| 5356 | |
| 5357 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 5358 | SuperClassInheritedProtocols.begin(), |
| 5359 | E = SuperClassInheritedProtocols.end(); I != E; ++I) { |
| 5360 | ObjCProtocolDecl *SuperClassProto = (*I); |
| 5361 | if (SuperClassProto->lookupProtocolNamed(LHSProto->getIdentifier())) { |
| 5362 | SuperImplementsProtocol = true; |
| 5363 | break; |
| 5364 | } |
| 5365 | } |
| 5366 | if (!SuperImplementsProtocol) |
| 5367 | return false; |
| 5368 | } |
| 5369 | return true; |
| 5370 | } |
| 5371 | return false; |
| 5372 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5373 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5374 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 5375 | LHSPE = LHS->qual_end(); |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 5376 | LHSPI != LHSPE; LHSPI++) { |
| 5377 | bool RHSImplementsProtocol = false; |
| 5378 | |
| 5379 | // If the RHS doesn't implement the protocol on the left, the types |
| 5380 | // are incompatible. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5381 | for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(), |
| 5382 | RHSPE = RHS->qual_end(); |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 5383 | RHSPI != RHSPE; RHSPI++) { |
| 5384 | if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) { |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 5385 | RHSImplementsProtocol = true; |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 5386 | break; |
| 5387 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 5388 | } |
| 5389 | // FIXME: For better diagnostics, consider passing back the protocol name. |
| 5390 | if (!RHSImplementsProtocol) |
| 5391 | return false; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5392 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 5393 | // The RHS implements all protocols listed on the LHS. |
| 5394 | return true; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5395 | } |
| 5396 | |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 5397 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 5398 | // get the "pointed to" types |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5399 | const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
| 5400 | const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5401 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5402 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 5403 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5404 | |
| 5405 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 5406 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 5407 | } |
| 5408 | |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 5409 | bool ASTContext::canBindObjCObjectType(QualType To, QualType From) { |
| 5410 | return canAssignObjCInterfaces( |
| 5411 | getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(), |
| 5412 | getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>()); |
| 5413 | } |
| 5414 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5415 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5416 | /// both shall have the identically qualified version of a compatible type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5417 | /// C99 6.2.7p1: Two types have compatible types if their types are the |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5418 | /// same. See 6.7.[2,3,5] for additional rules. |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5419 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS, |
| 5420 | bool CompareUnqualified) { |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 5421 | if (getLangOptions().CPlusPlus) |
| 5422 | return hasSameType(LHS, RHS); |
| 5423 | |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5424 | return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5425 | } |
| 5426 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5427 | bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) { |
| 5428 | return !mergeTypes(LHS, RHS, true).isNull(); |
| 5429 | } |
| 5430 | |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 5431 | /// mergeTransparentUnionType - if T is a transparent union type and a member |
| 5432 | /// of T is compatible with SubType, return the merged type, else return |
| 5433 | /// QualType() |
| 5434 | QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType, |
| 5435 | bool OfBlockPointer, |
| 5436 | bool Unqualified) { |
| 5437 | if (const RecordType *UT = T->getAsUnionType()) { |
| 5438 | RecordDecl *UD = UT->getDecl(); |
| 5439 | if (UD->hasAttr<TransparentUnionAttr>()) { |
| 5440 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 5441 | itend = UD->field_end(); it != itend; ++it) { |
Peter Collingbourne | f91d757 | 2010-12-02 21:00:06 +0000 | [diff] [blame] | 5442 | QualType ET = it->getType().getUnqualifiedType(); |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 5443 | QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified); |
| 5444 | if (!MT.isNull()) |
| 5445 | return MT; |
| 5446 | } |
| 5447 | } |
| 5448 | } |
| 5449 | |
| 5450 | return QualType(); |
| 5451 | } |
| 5452 | |
| 5453 | /// mergeFunctionArgumentTypes - merge two types which appear as function |
| 5454 | /// argument types |
| 5455 | QualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs, |
| 5456 | bool OfBlockPointer, |
| 5457 | bool Unqualified) { |
| 5458 | // GNU extension: two types are compatible if they appear as a function |
| 5459 | // argument, one of the types is a transparent union type and the other |
| 5460 | // type is compatible with a union member |
| 5461 | QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer, |
| 5462 | Unqualified); |
| 5463 | if (!lmerge.isNull()) |
| 5464 | return lmerge; |
| 5465 | |
| 5466 | QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer, |
| 5467 | Unqualified); |
| 5468 | if (!rmerge.isNull()) |
| 5469 | return rmerge; |
| 5470 | |
| 5471 | return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified); |
| 5472 | } |
| 5473 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5474 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs, |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5475 | bool OfBlockPointer, |
| 5476 | bool Unqualified) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5477 | const FunctionType *lbase = lhs->getAs<FunctionType>(); |
| 5478 | const FunctionType *rbase = rhs->getAs<FunctionType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5479 | const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 5480 | const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5481 | bool allLTypes = true; |
| 5482 | bool allRTypes = true; |
| 5483 | |
| 5484 | // Check return type |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5485 | QualType retType; |
Fariborz Jahanian | d263fd1 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 5486 | if (OfBlockPointer) { |
| 5487 | QualType RHS = rbase->getResultType(); |
| 5488 | QualType LHS = lbase->getResultType(); |
| 5489 | bool UnqualifiedResult = Unqualified; |
| 5490 | if (!UnqualifiedResult) |
| 5491 | UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers()); |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5492 | retType = mergeTypes(LHS, RHS, true, UnqualifiedResult, true); |
Fariborz Jahanian | d263fd1 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 5493 | } |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5494 | else |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5495 | retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), false, |
| 5496 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5497 | if (retType.isNull()) return QualType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5498 | |
| 5499 | if (Unqualified) |
| 5500 | retType = retType.getUnqualifiedType(); |
| 5501 | |
| 5502 | CanQualType LRetType = getCanonicalType(lbase->getResultType()); |
| 5503 | CanQualType RRetType = getCanonicalType(rbase->getResultType()); |
| 5504 | if (Unqualified) { |
| 5505 | LRetType = LRetType.getUnqualifiedType(); |
| 5506 | RRetType = RRetType.getUnqualifiedType(); |
| 5507 | } |
| 5508 | |
| 5509 | if (getCanonicalType(retType) != LRetType) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5510 | allLTypes = false; |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5511 | if (getCanonicalType(retType) != RRetType) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5512 | allRTypes = false; |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5513 | |
Daniel Dunbar | 6a15c85 | 2010-04-28 16:20:58 +0000 | [diff] [blame] | 5514 | // FIXME: double check this |
| 5515 | // FIXME: should we error if lbase->getRegParmAttr() != 0 && |
| 5516 | // rbase->getRegParmAttr() != 0 && |
| 5517 | // lbase->getRegParmAttr() != rbase->getRegParmAttr()? |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 5518 | FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo(); |
| 5519 | FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo(); |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5520 | |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 5521 | // Compatible functions must have compatible calling conventions |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5522 | if (!isSameCallConv(lbaseInfo.getCC(), rbaseInfo.getCC())) |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 5523 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5524 | |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5525 | // Regparm is part of the calling convention. |
Eli Friedman | a49218e | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 5526 | if (lbaseInfo.getHasRegParm() != rbaseInfo.getHasRegParm()) |
| 5527 | return QualType(); |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5528 | if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm()) |
| 5529 | return QualType(); |
| 5530 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5531 | if (lbaseInfo.getProducesResult() != rbaseInfo.getProducesResult()) |
| 5532 | return QualType(); |
| 5533 | |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5534 | // It's noreturn if either type is. |
| 5535 | // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'. |
| 5536 | bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn(); |
| 5537 | if (NoReturn != lbaseInfo.getNoReturn()) |
| 5538 | allLTypes = false; |
| 5539 | if (NoReturn != rbaseInfo.getNoReturn()) |
| 5540 | allRTypes = false; |
| 5541 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5542 | FunctionType::ExtInfo einfo = lbaseInfo.withNoReturn(NoReturn); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5543 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5544 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 5545 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 5546 | "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5547 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 5548 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 5549 | |
| 5550 | // Compatible functions must have the same number of arguments |
| 5551 | if (lproto_nargs != rproto_nargs) |
| 5552 | return QualType(); |
| 5553 | |
| 5554 | // Variadic and non-variadic functions aren't compatible |
| 5555 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 5556 | return QualType(); |
| 5557 | |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 5558 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 5559 | return QualType(); |
| 5560 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5561 | // Check argument compatibility |
| 5562 | llvm::SmallVector<QualType, 10> types; |
| 5563 | for (unsigned i = 0; i < lproto_nargs; i++) { |
| 5564 | QualType largtype = lproto->getArgType(i).getUnqualifiedType(); |
| 5565 | QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 5566 | QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype, |
| 5567 | OfBlockPointer, |
| 5568 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5569 | if (argtype.isNull()) return QualType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5570 | |
| 5571 | if (Unqualified) |
| 5572 | argtype = argtype.getUnqualifiedType(); |
| 5573 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5574 | types.push_back(argtype); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5575 | if (Unqualified) { |
| 5576 | largtype = largtype.getUnqualifiedType(); |
| 5577 | rargtype = rargtype.getUnqualifiedType(); |
| 5578 | } |
| 5579 | |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5580 | if (getCanonicalType(argtype) != getCanonicalType(largtype)) |
| 5581 | allLTypes = false; |
| 5582 | if (getCanonicalType(argtype) != getCanonicalType(rargtype)) |
| 5583 | allRTypes = false; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5584 | } |
| 5585 | if (allLTypes) return lhs; |
| 5586 | if (allRTypes) return rhs; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5587 | |
| 5588 | FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo(); |
| 5589 | EPI.ExtInfo = einfo; |
| 5590 | return getFunctionType(retType, types.begin(), types.size(), EPI); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5591 | } |
| 5592 | |
| 5593 | if (lproto) allRTypes = false; |
| 5594 | if (rproto) allLTypes = false; |
| 5595 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5596 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5597 | if (proto) { |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 5598 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5599 | if (proto->isVariadic()) return QualType(); |
| 5600 | // Check that the types are compatible with the types that |
| 5601 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 5602 | // The only types actually affected are promotable integer |
| 5603 | // types and floats, which would be passed as a different |
| 5604 | // type depending on whether the prototype is visible. |
| 5605 | unsigned proto_nargs = proto->getNumArgs(); |
| 5606 | for (unsigned i = 0; i < proto_nargs; ++i) { |
| 5607 | QualType argTy = proto->getArgType(i); |
Douglas Gregor | b0f8eac | 2010-02-03 19:27:29 +0000 | [diff] [blame] | 5608 | |
| 5609 | // Look at the promotion type of enum types, since that is the type used |
| 5610 | // to pass enum values. |
| 5611 | if (const EnumType *Enum = argTy->getAs<EnumType>()) |
| 5612 | argTy = Enum->getDecl()->getPromotionType(); |
| 5613 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5614 | if (argTy->isPromotableIntegerType() || |
| 5615 | getCanonicalType(argTy).getUnqualifiedType() == FloatTy) |
| 5616 | return QualType(); |
| 5617 | } |
| 5618 | |
| 5619 | if (allLTypes) return lhs; |
| 5620 | if (allRTypes) return rhs; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5621 | |
| 5622 | FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo(); |
| 5623 | EPI.ExtInfo = einfo; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5624 | return getFunctionType(retType, proto->arg_type_begin(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5625 | proto->getNumArgs(), EPI); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5626 | } |
| 5627 | |
| 5628 | if (allLTypes) return lhs; |
| 5629 | if (allRTypes) return rhs; |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5630 | return getFunctionNoProtoType(retType, einfo); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5631 | } |
| 5632 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5633 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5634 | bool OfBlockPointer, |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5635 | bool Unqualified, bool BlockReturnType) { |
Bill Wendling | 43d6975 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 5636 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 5637 | // type is adjusted to "T" prior to any further analysis, the expression |
| 5638 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 5639 | // expression is an lvalue unless the reference is an rvalue reference and |
| 5640 | // the expression is a function call (possibly inside parentheses). |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 5641 | assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?"); |
| 5642 | assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?"); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5643 | |
| 5644 | if (Unqualified) { |
| 5645 | LHS = LHS.getUnqualifiedType(); |
| 5646 | RHS = RHS.getUnqualifiedType(); |
| 5647 | } |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 5648 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5649 | QualType LHSCan = getCanonicalType(LHS), |
| 5650 | RHSCan = getCanonicalType(RHS); |
| 5651 | |
| 5652 | // If two types are identical, they are compatible. |
| 5653 | if (LHSCan == RHSCan) |
| 5654 | return LHS; |
| 5655 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5656 | // If the qualifiers are different, the types aren't compatible... mostly. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 5657 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 5658 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5659 | if (LQuals != RQuals) { |
| 5660 | // If any of these qualifiers are different, we have a type |
| 5661 | // mismatch. |
| 5662 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5663 | LQuals.getAddressSpace() != RQuals.getAddressSpace() || |
| 5664 | LQuals.getObjCLifetime() != RQuals.getObjCLifetime()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5665 | return QualType(); |
| 5666 | |
| 5667 | // Exactly one GC qualifier difference is allowed: __strong is |
| 5668 | // okay if the other type has no GC qualifier but is an Objective |
| 5669 | // C object pointer (i.e. implicitly strong by default). We fix |
| 5670 | // this by pretending that the unqualified type was actually |
| 5671 | // qualified __strong. |
| 5672 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 5673 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 5674 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 5675 | |
| 5676 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 5677 | return QualType(); |
| 5678 | |
| 5679 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
| 5680 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); |
| 5681 | } |
| 5682 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
| 5683 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); |
| 5684 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5685 | return QualType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5686 | } |
| 5687 | |
| 5688 | // Okay, qualifiers are equal. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5689 | |
Eli Friedman | 852d63b | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 5690 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 5691 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5692 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5693 | // We want to consider the two function types to be the same for these |
| 5694 | // comparisons, just force one to the other. |
| 5695 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 5696 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 4c721d3 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 5697 | |
| 5698 | // Same as above for arrays |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 5699 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 5700 | LHSClass = Type::ConstantArray; |
| 5701 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 5702 | RHSClass = Type::ConstantArray; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5703 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5704 | // ObjCInterfaces are just specialized ObjCObjects. |
| 5705 | if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject; |
| 5706 | if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject; |
| 5707 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 5708 | // Canonicalize ExtVector -> Vector. |
| 5709 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 5710 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5711 | |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 5712 | // If the canonical type classes don't match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5713 | if (LHSClass != RHSClass) { |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5714 | // C99 6.7.2.2p4: Each enumerated type shall be compatible with char, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5715 | // a signed integer type, or an unsigned integer type. |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 5716 | // Compatibility is based on the underlying type, not the promotion |
| 5717 | // type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5718 | if (const EnumType* ETy = LHS->getAs<EnumType>()) { |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5719 | if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType()) |
| 5720 | return RHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 5721 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5722 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5723 | if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType()) |
| 5724 | return LHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 5725 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5726 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5727 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5728 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5729 | |
Steve Naroff | 4a74678 | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 5730 | // The canonical type classes match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5731 | switch (LHSClass) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5732 | #define TYPE(Class, Base) |
| 5733 | #define ABSTRACT_TYPE(Class, Base) |
John McCall | ad5e738 | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 5734 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5735 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 5736 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 5737 | #include "clang/AST/TypeNodes.def" |
| 5738 | assert(false && "Non-canonical and dependent types shouldn't get here"); |
| 5739 | return QualType(); |
| 5740 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 5741 | case Type::LValueReference: |
| 5742 | case Type::RValueReference: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5743 | case Type::MemberPointer: |
| 5744 | assert(false && "C++ should never be in mergeTypes"); |
| 5745 | return QualType(); |
| 5746 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5747 | case Type::ObjCInterface: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5748 | case Type::IncompleteArray: |
| 5749 | case Type::VariableArray: |
| 5750 | case Type::FunctionProto: |
| 5751 | case Type::ExtVector: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5752 | assert(false && "Types are eliminated above"); |
| 5753 | return QualType(); |
| 5754 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5755 | case Type::Pointer: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5756 | { |
| 5757 | // Merge two pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5758 | QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); |
| 5759 | QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5760 | if (Unqualified) { |
| 5761 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 5762 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 5763 | } |
| 5764 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false, |
| 5765 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5766 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 5767 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5768 | return LHS; |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 5769 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5770 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5771 | return getPointerType(ResultType); |
| 5772 | } |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 5773 | case Type::BlockPointer: |
| 5774 | { |
| 5775 | // Merge two block pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5776 | QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); |
| 5777 | QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5778 | if (Unqualified) { |
| 5779 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 5780 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 5781 | } |
| 5782 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer, |
| 5783 | Unqualified); |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 5784 | if (ResultType.isNull()) return QualType(); |
| 5785 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 5786 | return LHS; |
| 5787 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 5788 | return RHS; |
| 5789 | return getBlockPointerType(ResultType); |
| 5790 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5791 | case Type::ConstantArray: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5792 | { |
| 5793 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 5794 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 5795 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 5796 | return QualType(); |
| 5797 | |
| 5798 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 5799 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5800 | if (Unqualified) { |
| 5801 | LHSElem = LHSElem.getUnqualifiedType(); |
| 5802 | RHSElem = RHSElem.getUnqualifiedType(); |
| 5803 | } |
| 5804 | |
| 5805 | QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5806 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5807 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 5808 | return LHS; |
| 5809 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 5810 | return RHS; |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 5811 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 5812 | ArrayType::ArraySizeModifier(), 0); |
| 5813 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 5814 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5815 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 5816 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5817 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 5818 | return LHS; |
| 5819 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 5820 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5821 | if (LVAT) { |
| 5822 | // FIXME: This isn't correct! But tricky to implement because |
| 5823 | // the array's size has to be the size of LHS, but the type |
| 5824 | // has to be different. |
| 5825 | return LHS; |
| 5826 | } |
| 5827 | if (RVAT) { |
| 5828 | // FIXME: This isn't correct! But tricky to implement because |
| 5829 | // the array's size has to be the size of RHS, but the type |
| 5830 | // has to be different. |
| 5831 | return RHS; |
| 5832 | } |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 5833 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 5834 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 5835 | return getIncompleteArrayType(ResultType, |
| 5836 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5837 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5838 | case Type::FunctionNoProto: |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5839 | return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5840 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5841 | case Type::Enum: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5842 | return QualType(); |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5843 | case Type::Builtin: |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 5844 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5845 | return QualType(); |
Daniel Dunbar | 64cfdb7 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 5846 | case Type::Complex: |
| 5847 | // Distinct complex types are incompatible. |
| 5848 | return QualType(); |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 5849 | case Type::Vector: |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 5850 | // FIXME: The merged type should be an ExtVector! |
John McCall | 1c471f3 | 2010-03-12 23:14:13 +0000 | [diff] [blame] | 5851 | if (areCompatVectorTypes(LHSCan->getAs<VectorType>(), |
| 5852 | RHSCan->getAs<VectorType>())) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5853 | return LHS; |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5854 | return QualType(); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5855 | case Type::ObjCObject: { |
| 5856 | // Check if the types are assignment compatible. |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 5857 | // FIXME: This should be type compatibility, e.g. whether |
| 5858 | // "LHS x; RHS x;" at global scope is legal. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5859 | const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>(); |
| 5860 | const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>(); |
| 5861 | if (canAssignObjCInterfaces(LHSIface, RHSIface)) |
Steve Naroff | 5fd659d | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 5862 | return LHS; |
| 5863 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5864 | return QualType(); |
Cedric Venet | 61490e9 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 5865 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5866 | case Type::ObjCObjectPointer: { |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5867 | if (OfBlockPointer) { |
| 5868 | if (canAssignObjCInterfacesInBlockPointer( |
| 5869 | LHS->getAs<ObjCObjectPointerType>(), |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5870 | RHS->getAs<ObjCObjectPointerType>(), |
| 5871 | BlockReturnType)) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5872 | return LHS; |
| 5873 | return QualType(); |
| 5874 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5875 | if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(), |
| 5876 | RHS->getAs<ObjCObjectPointerType>())) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5877 | return LHS; |
| 5878 | |
Steve Naroff | bc76dd0 | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 5879 | return QualType(); |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5880 | } |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5881 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5882 | |
| 5883 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5884 | } |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 5885 | |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 5886 | /// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and |
| 5887 | /// 'RHS' attributes and returns the merged version; including for function |
| 5888 | /// return types. |
| 5889 | QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) { |
| 5890 | QualType LHSCan = getCanonicalType(LHS), |
| 5891 | RHSCan = getCanonicalType(RHS); |
| 5892 | // If two types are identical, they are compatible. |
| 5893 | if (LHSCan == RHSCan) |
| 5894 | return LHS; |
| 5895 | if (RHSCan->isFunctionType()) { |
| 5896 | if (!LHSCan->isFunctionType()) |
| 5897 | return QualType(); |
| 5898 | QualType OldReturnType = |
| 5899 | cast<FunctionType>(RHSCan.getTypePtr())->getResultType(); |
| 5900 | QualType NewReturnType = |
| 5901 | cast<FunctionType>(LHSCan.getTypePtr())->getResultType(); |
| 5902 | QualType ResReturnType = |
| 5903 | mergeObjCGCQualifiers(NewReturnType, OldReturnType); |
| 5904 | if (ResReturnType.isNull()) |
| 5905 | return QualType(); |
| 5906 | if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) { |
| 5907 | // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo(); |
| 5908 | // In either case, use OldReturnType to build the new function type. |
| 5909 | const FunctionType *F = LHS->getAs<FunctionType>(); |
| 5910 | if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5911 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 5912 | EPI.ExtInfo = getFunctionExtInfo(LHS); |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 5913 | QualType ResultType |
| 5914 | = getFunctionType(OldReturnType, FPT->arg_type_begin(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5915 | FPT->getNumArgs(), EPI); |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 5916 | return ResultType; |
| 5917 | } |
| 5918 | } |
| 5919 | return QualType(); |
| 5920 | } |
| 5921 | |
| 5922 | // If the qualifiers are different, the types can still be merged. |
| 5923 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 5924 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
| 5925 | if (LQuals != RQuals) { |
| 5926 | // If any of these qualifiers are different, we have a type mismatch. |
| 5927 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 5928 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 5929 | return QualType(); |
| 5930 | |
| 5931 | // Exactly one GC qualifier difference is allowed: __strong is |
| 5932 | // okay if the other type has no GC qualifier but is an Objective |
| 5933 | // C object pointer (i.e. implicitly strong by default). We fix |
| 5934 | // this by pretending that the unqualified type was actually |
| 5935 | // qualified __strong. |
| 5936 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 5937 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 5938 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 5939 | |
| 5940 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 5941 | return QualType(); |
| 5942 | |
| 5943 | if (GC_L == Qualifiers::Strong) |
| 5944 | return LHS; |
| 5945 | if (GC_R == Qualifiers::Strong) |
| 5946 | return RHS; |
| 5947 | return QualType(); |
| 5948 | } |
| 5949 | |
| 5950 | if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) { |
| 5951 | QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5952 | QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5953 | QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT); |
| 5954 | if (ResQT == LHSBaseQT) |
| 5955 | return LHS; |
| 5956 | if (ResQT == RHSBaseQT) |
| 5957 | return RHS; |
| 5958 | } |
| 5959 | return QualType(); |
| 5960 | } |
| 5961 | |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 5962 | //===----------------------------------------------------------------------===// |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5963 | // Integer Predicates |
| 5964 | //===----------------------------------------------------------------------===// |
Chris Lattner | 88054de | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 5965 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5966 | unsigned ASTContext::getIntWidth(QualType T) const { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 5967 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
Eli Friedman | 29a7f33 | 2009-12-10 22:29:29 +0000 | [diff] [blame] | 5968 | T = ET->getDecl()->getIntegerType(); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 5969 | if (T->isBooleanType()) |
| 5970 | return 1; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 5971 | // For builtin types, just use the standard type sizing method |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5972 | return (unsigned)getTypeSize(T); |
| 5973 | } |
| 5974 | |
| 5975 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) { |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5976 | assert(T->hasSignedIntegerRepresentation() && "Unexpected type"); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5977 | |
| 5978 | // Turn <4 x signed int> -> <4 x unsigned int> |
| 5979 | if (const VectorType *VTy = T->getAs<VectorType>()) |
| 5980 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 5981 | VTy->getNumElements(), VTy->getVectorKind()); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5982 | |
| 5983 | // For enums, we return the unsigned version of the base type. |
| 5984 | if (const EnumType *ETy = T->getAs<EnumType>()) |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5985 | T = ETy->getDecl()->getIntegerType(); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5986 | |
| 5987 | const BuiltinType *BTy = T->getAs<BuiltinType>(); |
| 5988 | assert(BTy && "Unexpected signed integer type"); |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5989 | switch (BTy->getKind()) { |
| 5990 | case BuiltinType::Char_S: |
| 5991 | case BuiltinType::SChar: |
| 5992 | return UnsignedCharTy; |
| 5993 | case BuiltinType::Short: |
| 5994 | return UnsignedShortTy; |
| 5995 | case BuiltinType::Int: |
| 5996 | return UnsignedIntTy; |
| 5997 | case BuiltinType::Long: |
| 5998 | return UnsignedLongTy; |
| 5999 | case BuiltinType::LongLong: |
| 6000 | return UnsignedLongLongTy; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 6001 | case BuiltinType::Int128: |
| 6002 | return UnsignedInt128Ty; |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6003 | default: |
| 6004 | assert(0 && "Unexpected signed integer type"); |
| 6005 | return QualType(); |
| 6006 | } |
| 6007 | } |
| 6008 | |
Argyrios Kyrtzidis | 7b90340 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 6009 | ASTMutationListener::~ASTMutationListener() { } |
| 6010 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6011 | |
| 6012 | //===----------------------------------------------------------------------===// |
| 6013 | // Builtin Type Computation |
| 6014 | //===----------------------------------------------------------------------===// |
| 6015 | |
| 6016 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6017 | /// pointer over the consumed characters. This returns the resultant type. If |
| 6018 | /// AllowTypeModifiers is false then modifier like * are not parsed, just basic |
| 6019 | /// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of |
| 6020 | /// a vector of "i*". |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6021 | /// |
| 6022 | /// RequiresICE is filled in on return to indicate whether the value is required |
| 6023 | /// to be an Integer Constant Expression. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6024 | static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context, |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6025 | ASTContext::GetBuiltinTypeError &Error, |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6026 | bool &RequiresICE, |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6027 | bool AllowTypeModifiers) { |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6028 | // Modifiers. |
| 6029 | int HowLong = 0; |
| 6030 | bool Signed = false, Unsigned = false; |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6031 | RequiresICE = false; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6032 | |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6033 | // Read the prefixed modifiers first. |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6034 | bool Done = false; |
| 6035 | while (!Done) { |
| 6036 | switch (*Str++) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6037 | default: Done = true; --Str; break; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6038 | case 'I': |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6039 | RequiresICE = true; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6040 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6041 | case 'S': |
| 6042 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 6043 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 6044 | Signed = true; |
| 6045 | break; |
| 6046 | case 'U': |
| 6047 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
| 6048 | assert(!Unsigned && "Can't use 'S' modifier multiple times!"); |
| 6049 | Unsigned = true; |
| 6050 | break; |
| 6051 | case 'L': |
| 6052 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 6053 | ++HowLong; |
| 6054 | break; |
| 6055 | } |
| 6056 | } |
| 6057 | |
| 6058 | QualType Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6059 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6060 | // Read the base type. |
| 6061 | switch (*Str++) { |
| 6062 | default: assert(0 && "Unknown builtin type letter!"); |
| 6063 | case 'v': |
| 6064 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 6065 | "Bad modifiers used with 'v'!"); |
| 6066 | Type = Context.VoidTy; |
| 6067 | break; |
| 6068 | case 'f': |
| 6069 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 6070 | "Bad modifiers used with 'f'!"); |
| 6071 | Type = Context.FloatTy; |
| 6072 | break; |
| 6073 | case 'd': |
| 6074 | assert(HowLong < 2 && !Signed && !Unsigned && |
| 6075 | "Bad modifiers used with 'd'!"); |
| 6076 | if (HowLong) |
| 6077 | Type = Context.LongDoubleTy; |
| 6078 | else |
| 6079 | Type = Context.DoubleTy; |
| 6080 | break; |
| 6081 | case 's': |
| 6082 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 6083 | if (Unsigned) |
| 6084 | Type = Context.UnsignedShortTy; |
| 6085 | else |
| 6086 | Type = Context.ShortTy; |
| 6087 | break; |
| 6088 | case 'i': |
| 6089 | if (HowLong == 3) |
| 6090 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 6091 | else if (HowLong == 2) |
| 6092 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 6093 | else if (HowLong == 1) |
| 6094 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 6095 | else |
| 6096 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 6097 | break; |
| 6098 | case 'c': |
| 6099 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 6100 | if (Signed) |
| 6101 | Type = Context.SignedCharTy; |
| 6102 | else if (Unsigned) |
| 6103 | Type = Context.UnsignedCharTy; |
| 6104 | else |
| 6105 | Type = Context.CharTy; |
| 6106 | break; |
| 6107 | case 'b': // boolean |
| 6108 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 6109 | Type = Context.BoolTy; |
| 6110 | break; |
| 6111 | case 'z': // size_t. |
| 6112 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 6113 | Type = Context.getSizeType(); |
| 6114 | break; |
| 6115 | case 'F': |
| 6116 | Type = Context.getCFConstantStringType(); |
| 6117 | break; |
Fariborz Jahanian | ba8bda0 | 2010-11-09 21:38:20 +0000 | [diff] [blame] | 6118 | case 'G': |
| 6119 | Type = Context.getObjCIdType(); |
| 6120 | break; |
| 6121 | case 'H': |
| 6122 | Type = Context.getObjCSelType(); |
| 6123 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6124 | case 'a': |
| 6125 | Type = Context.getBuiltinVaListType(); |
| 6126 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 6127 | break; |
| 6128 | case 'A': |
| 6129 | // This is a "reference" to a va_list; however, what exactly |
| 6130 | // this means depends on how va_list is defined. There are two |
| 6131 | // different kinds of va_list: ones passed by value, and ones |
| 6132 | // passed by reference. An example of a by-value va_list is |
| 6133 | // x86, where va_list is a char*. An example of by-ref va_list |
| 6134 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 6135 | // we want this argument to be a char*&; for x86-64, we want |
| 6136 | // it to be a __va_list_tag*. |
| 6137 | Type = Context.getBuiltinVaListType(); |
| 6138 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6139 | if (Type->isArrayType()) |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6140 | Type = Context.getArrayDecayedType(Type); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6141 | else |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6142 | Type = Context.getLValueReferenceType(Type); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6143 | break; |
| 6144 | case 'V': { |
| 6145 | char *End; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6146 | unsigned NumElements = strtoul(Str, &End, 10); |
| 6147 | assert(End != Str && "Missing vector size"); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6148 | Str = End; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6149 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6150 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, |
| 6151 | RequiresICE, false); |
| 6152 | assert(!RequiresICE && "Can't require vector ICE"); |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6153 | |
| 6154 | // TODO: No way to make AltiVec vectors in builtins yet. |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 6155 | Type = Context.getVectorType(ElementType, NumElements, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 6156 | VectorType::GenericVector); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6157 | break; |
| 6158 | } |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 6159 | case 'X': { |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6160 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
| 6161 | false); |
| 6162 | assert(!RequiresICE && "Can't require complex ICE"); |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 6163 | Type = Context.getComplexType(ElementType); |
| 6164 | break; |
| 6165 | } |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 6166 | case 'P': |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 6167 | Type = Context.getFILEType(); |
| 6168 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6169 | Error = ASTContext::GE_Missing_stdio; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6170 | return QualType(); |
| 6171 | } |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 6172 | break; |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 6173 | case 'J': |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6174 | if (Signed) |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 6175 | Type = Context.getsigjmp_bufType(); |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6176 | else |
| 6177 | Type = Context.getjmp_bufType(); |
| 6178 | |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 6179 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6180 | Error = ASTContext::GE_Missing_setjmp; |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 6181 | return QualType(); |
| 6182 | } |
| 6183 | break; |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 6184 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6185 | |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6186 | // If there are modifiers and if we're allowed to parse them, go for it. |
| 6187 | Done = !AllowTypeModifiers; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6188 | while (!Done) { |
John McCall | 187ab37 | 2010-03-12 04:21:28 +0000 | [diff] [blame] | 6189 | switch (char c = *Str++) { |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6190 | default: Done = true; --Str; break; |
| 6191 | case '*': |
| 6192 | case '&': { |
| 6193 | // Both pointers and references can have their pointee types |
| 6194 | // qualified with an address space. |
| 6195 | char *End; |
| 6196 | unsigned AddrSpace = strtoul(Str, &End, 10); |
| 6197 | if (End != Str && AddrSpace != 0) { |
| 6198 | Type = Context.getAddrSpaceQualType(Type, AddrSpace); |
| 6199 | Str = End; |
| 6200 | } |
| 6201 | if (c == '*') |
| 6202 | Type = Context.getPointerType(Type); |
| 6203 | else |
| 6204 | Type = Context.getLValueReferenceType(Type); |
| 6205 | break; |
| 6206 | } |
| 6207 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 6208 | case 'C': |
| 6209 | Type = Type.withConst(); |
| 6210 | break; |
| 6211 | case 'D': |
| 6212 | Type = Context.getVolatileType(Type); |
| 6213 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6214 | } |
| 6215 | } |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6216 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6217 | assert((!RequiresICE || Type->isIntegralOrEnumerationType()) && |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6218 | "Integer constant 'I' type must be an integer"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6219 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6220 | return Type; |
| 6221 | } |
| 6222 | |
| 6223 | /// GetBuiltinType - Return the type for the specified builtin. |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6224 | QualType ASTContext::GetBuiltinType(unsigned Id, |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6225 | GetBuiltinTypeError &Error, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6226 | unsigned *IntegerConstantArgs) const { |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6227 | const char *TypeStr = BuiltinInfo.GetTypeString(Id); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6228 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6229 | llvm::SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6230 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6231 | bool RequiresICE = false; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6232 | Error = GE_None; |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6233 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error, |
| 6234 | RequiresICE, true); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6235 | if (Error != GE_None) |
| 6236 | return QualType(); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6237 | |
| 6238 | assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE"); |
| 6239 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6240 | while (TypeStr[0] && TypeStr[0] != '.') { |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6241 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6242 | if (Error != GE_None) |
| 6243 | return QualType(); |
| 6244 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6245 | // If this argument is required to be an IntegerConstantExpression and the |
| 6246 | // caller cares, fill in the bitmask we return. |
| 6247 | if (RequiresICE && IntegerConstantArgs) |
| 6248 | *IntegerConstantArgs |= 1 << ArgTypes.size(); |
| 6249 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6250 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 6251 | if (Ty->isArrayType()) |
| 6252 | Ty = getArrayDecayedType(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6253 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6254 | ArgTypes.push_back(Ty); |
| 6255 | } |
| 6256 | |
| 6257 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 6258 | "'.' should only occur at end of builtin type list!"); |
| 6259 | |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 6260 | FunctionType::ExtInfo EI; |
| 6261 | if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true); |
| 6262 | |
| 6263 | bool Variadic = (TypeStr[0] == '.'); |
| 6264 | |
| 6265 | // We really shouldn't be making a no-proto type here, especially in C++. |
| 6266 | if (ArgTypes.empty() && Variadic) |
| 6267 | return getFunctionNoProtoType(ResType, EI); |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 6268 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6269 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 6270 | EPI.ExtInfo = EI; |
| 6271 | EPI.Variadic = Variadic; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6272 | |
| 6273 | return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), EPI); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6274 | } |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 6275 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6276 | GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) { |
| 6277 | GVALinkage External = GVA_StrongExternal; |
| 6278 | |
| 6279 | Linkage L = FD->getLinkage(); |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6280 | switch (L) { |
| 6281 | case NoLinkage: |
| 6282 | case InternalLinkage: |
| 6283 | case UniqueExternalLinkage: |
| 6284 | return GVA_Internal; |
| 6285 | |
| 6286 | case ExternalLinkage: |
| 6287 | switch (FD->getTemplateSpecializationKind()) { |
| 6288 | case TSK_Undeclared: |
| 6289 | case TSK_ExplicitSpecialization: |
| 6290 | External = GVA_StrongExternal; |
| 6291 | break; |
| 6292 | |
| 6293 | case TSK_ExplicitInstantiationDefinition: |
| 6294 | return GVA_ExplicitTemplateInstantiation; |
| 6295 | |
| 6296 | case TSK_ExplicitInstantiationDeclaration: |
| 6297 | case TSK_ImplicitInstantiation: |
| 6298 | External = GVA_TemplateInstantiation; |
| 6299 | break; |
| 6300 | } |
| 6301 | } |
| 6302 | |
| 6303 | if (!FD->isInlined()) |
| 6304 | return External; |
| 6305 | |
| 6306 | if (!getLangOptions().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) { |
| 6307 | // GNU or C99 inline semantics. Determine whether this symbol should be |
| 6308 | // externally visible. |
| 6309 | if (FD->isInlineDefinitionExternallyVisible()) |
| 6310 | return External; |
| 6311 | |
| 6312 | // C99 inline semantics, where the symbol is not externally visible. |
| 6313 | return GVA_C99Inline; |
| 6314 | } |
| 6315 | |
| 6316 | // C++0x [temp.explicit]p9: |
| 6317 | // [ Note: The intent is that an inline function that is the subject of |
| 6318 | // an explicit instantiation declaration will still be implicitly |
| 6319 | // instantiated when used so that the body can be considered for |
| 6320 | // inlining, but that no out-of-line copy of the inline function would be |
| 6321 | // generated in the translation unit. -- end note ] |
| 6322 | if (FD->getTemplateSpecializationKind() |
| 6323 | == TSK_ExplicitInstantiationDeclaration) |
| 6324 | return GVA_C99Inline; |
| 6325 | |
| 6326 | return GVA_CXXInline; |
| 6327 | } |
| 6328 | |
| 6329 | GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) { |
| 6330 | // If this is a static data member, compute the kind of template |
| 6331 | // specialization. Otherwise, this variable is not part of a |
| 6332 | // template. |
| 6333 | TemplateSpecializationKind TSK = TSK_Undeclared; |
| 6334 | if (VD->isStaticDataMember()) |
| 6335 | TSK = VD->getTemplateSpecializationKind(); |
| 6336 | |
| 6337 | Linkage L = VD->getLinkage(); |
| 6338 | if (L == ExternalLinkage && getLangOptions().CPlusPlus && |
| 6339 | VD->getType()->getLinkage() == UniqueExternalLinkage) |
| 6340 | L = UniqueExternalLinkage; |
| 6341 | |
| 6342 | switch (L) { |
| 6343 | case NoLinkage: |
| 6344 | case InternalLinkage: |
| 6345 | case UniqueExternalLinkage: |
| 6346 | return GVA_Internal; |
| 6347 | |
| 6348 | case ExternalLinkage: |
| 6349 | switch (TSK) { |
| 6350 | case TSK_Undeclared: |
| 6351 | case TSK_ExplicitSpecialization: |
| 6352 | return GVA_StrongExternal; |
| 6353 | |
| 6354 | case TSK_ExplicitInstantiationDeclaration: |
| 6355 | llvm_unreachable("Variable should not be instantiated"); |
| 6356 | // Fall through to treat this like any other instantiation. |
| 6357 | |
| 6358 | case TSK_ExplicitInstantiationDefinition: |
| 6359 | return GVA_ExplicitTemplateInstantiation; |
| 6360 | |
| 6361 | case TSK_ImplicitInstantiation: |
| 6362 | return GVA_TemplateInstantiation; |
| 6363 | } |
| 6364 | } |
| 6365 | |
| 6366 | return GVA_StrongExternal; |
| 6367 | } |
| 6368 | |
Argyrios Kyrtzidis | 4ac7c0b | 2010-07-29 20:08:05 +0000 | [diff] [blame] | 6369 | bool ASTContext::DeclMustBeEmitted(const Decl *D) { |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6370 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 6371 | if (!VD->isFileVarDecl()) |
| 6372 | return false; |
| 6373 | } else if (!isa<FunctionDecl>(D)) |
| 6374 | return false; |
| 6375 | |
Argyrios Kyrtzidis | ab411c8 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 6376 | // Weak references don't produce any output by themselves. |
| 6377 | if (D->hasAttr<WeakRefAttr>()) |
| 6378 | return false; |
| 6379 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6380 | // Aliases and used decls are required. |
| 6381 | if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>()) |
| 6382 | return true; |
| 6383 | |
| 6384 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 6385 | // Forward declarations aren't required. |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 6386 | if (!FD->doesThisDeclarationHaveABody()) |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6387 | return false; |
| 6388 | |
| 6389 | // Constructors and destructors are required. |
| 6390 | if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>()) |
| 6391 | return true; |
| 6392 | |
| 6393 | // The key function for a class is required. |
| 6394 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 6395 | const CXXRecordDecl *RD = MD->getParent(); |
| 6396 | if (MD->isOutOfLine() && RD->isDynamicClass()) { |
| 6397 | const CXXMethodDecl *KeyFunc = getKeyFunction(RD); |
| 6398 | if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl()) |
| 6399 | return true; |
| 6400 | } |
| 6401 | } |
| 6402 | |
| 6403 | GVALinkage Linkage = GetGVALinkageForFunction(FD); |
| 6404 | |
| 6405 | // static, static inline, always_inline, and extern inline functions can |
| 6406 | // always be deferred. Normal inline functions can be deferred in C99/C++. |
| 6407 | // Implicit template instantiations can also be deferred in C++. |
| 6408 | if (Linkage == GVA_Internal || Linkage == GVA_C99Inline || |
| 6409 | Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) |
| 6410 | return false; |
| 6411 | return true; |
| 6412 | } |
| 6413 | |
| 6414 | const VarDecl *VD = cast<VarDecl>(D); |
| 6415 | assert(VD->isFileVarDecl() && "Expected file scoped var"); |
| 6416 | |
Argyrios Kyrtzidis | ab411c8 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 6417 | if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly) |
| 6418 | return false; |
| 6419 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6420 | // Structs that have non-trivial constructors or destructors are required. |
| 6421 | |
| 6422 | // FIXME: Handle references. |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 6423 | // FIXME: Be more selective about which constructors we care about. |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6424 | if (const RecordType *RT = VD->getType()->getAs<RecordType>()) { |
| 6425 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 6426 | if (RD->hasDefinition() && !(RD->hasTrivialDefaultConstructor() && |
| 6427 | RD->hasTrivialCopyConstructor() && |
| 6428 | RD->hasTrivialMoveConstructor() && |
| 6429 | RD->hasTrivialDestructor())) |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6430 | return true; |
| 6431 | } |
| 6432 | } |
| 6433 | |
| 6434 | GVALinkage L = GetGVALinkageForVariable(VD); |
| 6435 | if (L == GVA_Internal || L == GVA_TemplateInstantiation) { |
| 6436 | if (!(VD->getInit() && VD->getInit()->HasSideEffects(*this))) |
| 6437 | return false; |
| 6438 | } |
| 6439 | |
| 6440 | return true; |
| 6441 | } |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 6442 | |
Charles Davis | ee743f9 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 6443 | CallingConv ASTContext::getDefaultMethodCallConv() { |
| 6444 | // Pass through to the C++ ABI object |
| 6445 | return ABI->getDefaultMethodCallConv(); |
| 6446 | } |
| 6447 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6448 | bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const { |
Anders Carlsson | dae0cb5 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 6449 | // Pass through to the C++ ABI object |
| 6450 | return ABI->isNearlyEmpty(RD); |
| 6451 | } |
| 6452 | |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 6453 | MangleContext *ASTContext::createMangleContext() { |
| 6454 | switch (Target.getCXXABI()) { |
| 6455 | case CXXABI_ARM: |
| 6456 | case CXXABI_Itanium: |
| 6457 | return createItaniumMangleContext(*this, getDiagnostics()); |
| 6458 | case CXXABI_Microsoft: |
| 6459 | return createMicrosoftMangleContext(*this, getDiagnostics()); |
| 6460 | } |
| 6461 | assert(0 && "Unsupported ABI"); |
| 6462 | return 0; |
| 6463 | } |
| 6464 | |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 6465 | CXXABI::~CXXABI() {} |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 6466 | |
| 6467 | size_t ASTContext::getSideTableAllocatedMemory() const { |
| 6468 | size_t bytes = 0; |
| 6469 | bytes += ASTRecordLayouts.getMemorySize(); |
| 6470 | bytes += ObjCLayouts.getMemorySize(); |
| 6471 | bytes += KeyFunctions.getMemorySize(); |
| 6472 | bytes += ObjCImpls.getMemorySize(); |
| 6473 | bytes += BlockVarCopyInits.getMemorySize(); |
| 6474 | bytes += DeclAttrs.getMemorySize(); |
| 6475 | bytes += InstantiatedFromStaticDataMember.getMemorySize(); |
| 6476 | bytes += InstantiatedFromUsingDecl.getMemorySize(); |
| 6477 | bytes += InstantiatedFromUsingShadowDecl.getMemorySize(); |
| 6478 | bytes += InstantiatedFromUnnamedFieldDecl.getMemorySize(); |
| 6479 | return bytes; |
| 6480 | } |
| 6481 | |