Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 1 | //===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the ASTContext interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/ASTContext.h" |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 15 | #include "clang/AST/CharUnits.h" |
Argyrios Kyrtzidis | faf0876 | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclCXX.h" |
Steve Naroff | 67391b8 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclTemplate.h" |
Argyrios Kyrtzidis | 3f79ad7 | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeLoc.h" |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 20 | #include "clang/AST/Expr.h" |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 21 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExternalASTSource.h" |
Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 23 | #include "clang/AST/ASTMutationListener.h" |
Anders Carlsson | 15b73de | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 24 | #include "clang/AST/RecordLayout.h" |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 25 | #include "clang/AST/Mangle.h" |
Chris Lattner | 15ba949 | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 26 | #include "clang/Basic/Builtins.h" |
Chris Lattner | d286851 | 2009-03-28 03:45:20 +0000 | [diff] [blame] | 27 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 4dc8a6f | 2007-05-20 23:50:58 +0000 | [diff] [blame] | 28 | #include "clang/Basic/TargetInfo.h" |
Benjamin Kramer | 1402ce3 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/SmallString.h" |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/StringExtras.h" |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 31 | #include "llvm/Support/MathExtras.h" |
Benjamin Kramer | 1402ce3 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 32 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | 7d39c9a | 2011-07-27 18:41:12 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Capacity.h" |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 34 | #include "CXXABI.h" |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 35 | #include <map> |
Anders Carlsson | a4267a6 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 36 | |
Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 37 | using namespace clang; |
| 38 | |
Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 39 | unsigned ASTContext::NumImplicitDefaultConstructors; |
| 40 | unsigned ASTContext::NumImplicitDefaultConstructorsDeclared; |
Douglas Gregor | a6d6950 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 41 | unsigned ASTContext::NumImplicitCopyConstructors; |
| 42 | unsigned ASTContext::NumImplicitCopyConstructorsDeclared; |
Alexis Hunt | fcaeae4 | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 43 | unsigned ASTContext::NumImplicitMoveConstructors; |
| 44 | unsigned ASTContext::NumImplicitMoveConstructorsDeclared; |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 45 | unsigned ASTContext::NumImplicitCopyAssignmentOperators; |
| 46 | unsigned ASTContext::NumImplicitCopyAssignmentOperatorsDeclared; |
Alexis Hunt | fcaeae4 | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 47 | unsigned ASTContext::NumImplicitMoveAssignmentOperators; |
| 48 | unsigned ASTContext::NumImplicitMoveAssignmentOperatorsDeclared; |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 49 | unsigned ASTContext::NumImplicitDestructors; |
| 50 | unsigned ASTContext::NumImplicitDestructorsDeclared; |
| 51 | |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 52 | enum FloatingRank { |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 53 | HalfRank, FloatRank, DoubleRank, LongDoubleRank |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 56 | void |
| 57 | ASTContext::CanonicalTemplateTemplateParm::Profile(llvm::FoldingSetNodeID &ID, |
| 58 | TemplateTemplateParmDecl *Parm) { |
| 59 | ID.AddInteger(Parm->getDepth()); |
| 60 | ID.AddInteger(Parm->getPosition()); |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 61 | ID.AddBoolean(Parm->isParameterPack()); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 62 | |
| 63 | TemplateParameterList *Params = Parm->getTemplateParameters(); |
| 64 | ID.AddInteger(Params->size()); |
| 65 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 66 | PEnd = Params->end(); |
| 67 | P != PEnd; ++P) { |
| 68 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
| 69 | ID.AddInteger(0); |
| 70 | ID.AddBoolean(TTP->isParameterPack()); |
| 71 | continue; |
| 72 | } |
| 73 | |
| 74 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 75 | ID.AddInteger(1); |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 76 | ID.AddBoolean(NTTP->isParameterPack()); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 77 | ID.AddPointer(NTTP->getType().getAsOpaquePtr()); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 78 | if (NTTP->isExpandedParameterPack()) { |
| 79 | ID.AddBoolean(true); |
| 80 | ID.AddInteger(NTTP->getNumExpansionTypes()); |
| 81 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) |
| 82 | ID.AddPointer(NTTP->getExpansionType(I).getAsOpaquePtr()); |
| 83 | } else |
| 84 | ID.AddBoolean(false); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 85 | continue; |
| 86 | } |
| 87 | |
| 88 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); |
| 89 | ID.AddInteger(2); |
| 90 | Profile(ID, TTP); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | TemplateTemplateParmDecl * |
| 95 | ASTContext::getCanonicalTemplateTemplateParmDecl( |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 96 | TemplateTemplateParmDecl *TTP) const { |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 97 | // Check if we already have a canonical template template parameter. |
| 98 | llvm::FoldingSetNodeID ID; |
| 99 | CanonicalTemplateTemplateParm::Profile(ID, TTP); |
| 100 | void *InsertPos = 0; |
| 101 | CanonicalTemplateTemplateParm *Canonical |
| 102 | = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
| 103 | if (Canonical) |
| 104 | return Canonical->getParam(); |
| 105 | |
| 106 | // Build a canonical template parameter list. |
| 107 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 108 | SmallVector<NamedDecl *, 4> CanonParams; |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 109 | CanonParams.reserve(Params->size()); |
| 110 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 111 | PEnd = Params->end(); |
| 112 | P != PEnd; ++P) { |
| 113 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) |
| 114 | CanonParams.push_back( |
| 115 | TemplateTypeParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 116 | SourceLocation(), |
| 117 | SourceLocation(), |
| 118 | TTP->getDepth(), |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 119 | TTP->getIndex(), 0, false, |
| 120 | TTP->isParameterPack())); |
| 121 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 122 | = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 123 | QualType T = getCanonicalType(NTTP->getType()); |
| 124 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T); |
| 125 | NonTypeTemplateParmDecl *Param; |
| 126 | if (NTTP->isExpandedParameterPack()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 127 | SmallVector<QualType, 2> ExpandedTypes; |
| 128 | SmallVector<TypeSourceInfo *, 2> ExpandedTInfos; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 129 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) { |
| 130 | ExpandedTypes.push_back(getCanonicalType(NTTP->getExpansionType(I))); |
| 131 | ExpandedTInfos.push_back( |
| 132 | getTrivialTypeSourceInfo(ExpandedTypes.back())); |
| 133 | } |
| 134 | |
| 135 | Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 136 | SourceLocation(), |
| 137 | SourceLocation(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 138 | NTTP->getDepth(), |
| 139 | NTTP->getPosition(), 0, |
| 140 | T, |
| 141 | TInfo, |
| 142 | ExpandedTypes.data(), |
| 143 | ExpandedTypes.size(), |
| 144 | ExpandedTInfos.data()); |
| 145 | } else { |
| 146 | Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 147 | SourceLocation(), |
| 148 | SourceLocation(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 149 | NTTP->getDepth(), |
| 150 | NTTP->getPosition(), 0, |
| 151 | T, |
| 152 | NTTP->isParameterPack(), |
| 153 | TInfo); |
| 154 | } |
| 155 | CanonParams.push_back(Param); |
| 156 | |
| 157 | } else |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 158 | CanonParams.push_back(getCanonicalTemplateTemplateParmDecl( |
| 159 | cast<TemplateTemplateParmDecl>(*P))); |
| 160 | } |
| 161 | |
| 162 | TemplateTemplateParmDecl *CanonTTP |
| 163 | = TemplateTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
| 164 | SourceLocation(), TTP->getDepth(), |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 165 | TTP->getPosition(), |
| 166 | TTP->isParameterPack(), |
| 167 | 0, |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 168 | TemplateParameterList::Create(*this, SourceLocation(), |
| 169 | SourceLocation(), |
| 170 | CanonParams.data(), |
| 171 | CanonParams.size(), |
| 172 | SourceLocation())); |
| 173 | |
| 174 | // Get the new insert position for the node we care about. |
| 175 | Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
| 176 | assert(Canonical == 0 && "Shouldn't be in the map!"); |
| 177 | (void)Canonical; |
| 178 | |
| 179 | // Create the canonical template template parameter entry. |
| 180 | Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP); |
| 181 | CanonTemplateTemplateParms.InsertNode(Canonical, InsertPos); |
| 182 | return CanonTTP; |
| 183 | } |
| 184 | |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 185 | CXXABI *ASTContext::createCXXABI(const TargetInfo &T) { |
John McCall | 8635341 | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 186 | if (!LangOpts.CPlusPlus) return 0; |
| 187 | |
Charles Davis | 6bcb07a | 2010-08-19 02:18:14 +0000 | [diff] [blame] | 188 | switch (T.getCXXABI()) { |
John McCall | 8635341 | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 189 | case CXXABI_ARM: |
| 190 | return CreateARMCXXABI(*this); |
| 191 | case CXXABI_Itanium: |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 192 | return CreateItaniumCXXABI(*this); |
Charles Davis | 6bcb07a | 2010-08-19 02:18:14 +0000 | [diff] [blame] | 193 | case CXXABI_Microsoft: |
| 194 | return CreateMicrosoftCXXABI(*this); |
| 195 | } |
John McCall | 8635341 | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 196 | return 0; |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 199 | static const LangAS::Map *getAddressSpaceMap(const TargetInfo &T, |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 200 | const LangOptions &LOpts) { |
| 201 | if (LOpts.FakeAddressSpaceMap) { |
| 202 | // The fake address space map must have a distinct entry for each |
| 203 | // language-specific address space. |
| 204 | static const unsigned FakeAddrSpaceMap[] = { |
| 205 | 1, // opencl_global |
| 206 | 2, // opencl_local |
| 207 | 3 // opencl_constant |
| 208 | }; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 209 | return &FakeAddrSpaceMap; |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 210 | } else { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 211 | return &T.getAddressSpaceMap(); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
Douglas Gregor | 7018d5b | 2011-09-01 20:23:19 +0000 | [diff] [blame] | 215 | ASTContext::ASTContext(LangOptions& LOpts, SourceManager &SM, |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 216 | const TargetInfo *t, |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 217 | IdentifierTable &idents, SelectorTable &sels, |
Chris Lattner | 15ba949 | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 218 | Builtin::Context &builtins, |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 219 | unsigned size_reserve, |
| 220 | bool DelayInitialization) |
| 221 | : FunctionProtoTypes(this_()), |
| 222 | TemplateSpecializationTypes(this_()), |
| 223 | DependentTemplateSpecializationTypes(this_()), |
| 224 | SubstTemplateTemplateParmPacks(this_()), |
| 225 | GlobalNestedNameSpecifier(0), |
| 226 | Int128Decl(0), UInt128Decl(0), |
| 227 | ObjCIdDecl(0), ObjCSelDecl(0), ObjCClassDecl(0), |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 228 | CFConstantStringTypeDecl(0), ObjCInstanceTypeDecl(0), |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 229 | FILEDecl(0), |
| 230 | jmp_bufDecl(0), sigjmp_bufDecl(0), BlockDescriptorType(0), |
| 231 | BlockDescriptorExtendedType(0), cudaConfigureCallDecl(0), |
| 232 | NullTypeSourceInfo(QualType()), |
| 233 | SourceMgr(SM), LangOpts(LOpts), |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 234 | AddrSpaceMap(0), Target(t), PrintingPolicy(LOpts), |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 235 | Idents(idents), Selectors(sels), |
| 236 | BuiltinInfo(builtins), |
| 237 | DeclarationNames(*this), |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 238 | ExternalSource(0), Listener(0), |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 239 | LastSDM(0, 0), |
| 240 | UniqueBlockByRefTypeID(0) |
| 241 | { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 242 | if (size_reserve > 0) Types.reserve(size_reserve); |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 243 | TUDecl = TranslationUnitDecl::Create(*this); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 244 | |
| 245 | if (!DelayInitialization) { |
| 246 | assert(t && "No target supplied for ASTContext initialization"); |
| 247 | InitBuiltinTypes(*t); |
| 248 | } |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 251 | ASTContext::~ASTContext() { |
Ted Kremenek | da4e0d3 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 252 | // Release the DenseMaps associated with DeclContext objects. |
| 253 | // FIXME: Is this the ideal solution? |
| 254 | ReleaseDeclContextMaps(); |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 255 | |
Douglas Gregor | 5b11d49 | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 256 | // Call all of the deallocation functions. |
| 257 | for (unsigned I = 0, N = Deallocations.size(); I != N; ++I) |
| 258 | Deallocations[I].first(Deallocations[I].second); |
Douglas Gregor | 1a80933 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 259 | |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 260 | // Release all of the memory associated with overridden C++ methods. |
| 261 | for (llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::iterator |
| 262 | OM = OverriddenMethods.begin(), OMEnd = OverriddenMethods.end(); |
| 263 | OM != OMEnd; ++OM) |
| 264 | OM->second.Destroy(); |
Ted Kremenek | da4e0d3 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 265 | |
Ted Kremenek | 076baeb | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 266 | // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed |
Douglas Gregor | 5b11d49 | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 267 | // because they can contain DenseMaps. |
| 268 | for (llvm::DenseMap<const ObjCContainerDecl*, |
| 269 | const ASTRecordLayout*>::iterator |
| 270 | I = ObjCLayouts.begin(), E = ObjCLayouts.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 | |
Ted Kremenek | 076baeb | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 275 | for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator |
| 276 | I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) { |
| 277 | // Increment in loop to prevent using deallocated memory. |
| 278 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 279 | R->Destroy(*this); |
| 280 | } |
Douglas Gregor | 561eceb | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 281 | |
| 282 | for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(), |
| 283 | AEnd = DeclAttrs.end(); |
| 284 | A != AEnd; ++A) |
| 285 | A->second->~AttrVec(); |
| 286 | } |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 287 | |
Douglas Gregor | 1a80933 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 288 | void ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) { |
| 289 | Deallocations.push_back(std::make_pair(Callback, Data)); |
| 290 | } |
| 291 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 292 | void |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 293 | ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) { |
| 294 | ExternalSource.reset(Source.take()); |
| 295 | } |
| 296 | |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 297 | void ASTContext::PrintStats() const { |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 298 | llvm::errs() << "\n*** AST Context Stats:\n"; |
| 299 | llvm::errs() << " " << Types.size() << " types total.\n"; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 300 | |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 301 | unsigned counts[] = { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 302 | #define TYPE(Name, Parent) 0, |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 303 | #define ABSTRACT_TYPE(Name, Parent) |
| 304 | #include "clang/AST/TypeNodes.def" |
| 305 | 0 // Extra |
| 306 | }; |
Douglas Gregor | b1fe2c9 | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 307 | |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 308 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 309 | Type *T = Types[i]; |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 310 | counts[(unsigned)T->getTypeClass()]++; |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 313 | unsigned Idx = 0; |
| 314 | unsigned TotalBytes = 0; |
| 315 | #define TYPE(Name, Parent) \ |
| 316 | if (counts[Idx]) \ |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 317 | llvm::errs() << " " << counts[Idx] << " " << #Name \ |
| 318 | << " types\n"; \ |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 319 | TotalBytes += counts[Idx] * sizeof(Name##Type); \ |
| 320 | ++Idx; |
| 321 | #define ABSTRACT_TYPE(Name, Parent) |
| 322 | #include "clang/AST/TypeNodes.def" |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 323 | |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 324 | llvm::errs() << "Total bytes = " << TotalBytes << "\n"; |
| 325 | |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 326 | // Implicit special member functions. |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 327 | llvm::errs() << NumImplicitDefaultConstructorsDeclared << "/" |
| 328 | << NumImplicitDefaultConstructors |
| 329 | << " implicit default constructors created\n"; |
| 330 | llvm::errs() << NumImplicitCopyConstructorsDeclared << "/" |
| 331 | << NumImplicitCopyConstructors |
| 332 | << " implicit copy constructors created\n"; |
Alexis Hunt | fcaeae4 | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 333 | if (getLangOptions().CPlusPlus) |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 334 | llvm::errs() << NumImplicitMoveConstructorsDeclared << "/" |
| 335 | << NumImplicitMoveConstructors |
| 336 | << " implicit move constructors created\n"; |
| 337 | llvm::errs() << NumImplicitCopyAssignmentOperatorsDeclared << "/" |
| 338 | << NumImplicitCopyAssignmentOperators |
| 339 | << " implicit copy assignment operators created\n"; |
Alexis Hunt | fcaeae4 | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 340 | if (getLangOptions().CPlusPlus) |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 341 | llvm::errs() << NumImplicitMoveAssignmentOperatorsDeclared << "/" |
| 342 | << NumImplicitMoveAssignmentOperators |
| 343 | << " implicit move assignment operators created\n"; |
| 344 | llvm::errs() << NumImplicitDestructorsDeclared << "/" |
| 345 | << NumImplicitDestructors |
| 346 | << " implicit destructors created\n"; |
| 347 | |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 348 | if (ExternalSource.get()) { |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 349 | llvm::errs() << "\n"; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 350 | ExternalSource->PrintStats(); |
| 351 | } |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 352 | |
Douglas Gregor | 5b11d49 | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 353 | BumpAlloc.PrintStats(); |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Douglas Gregor | 801c99d | 2011-08-12 06:49:56 +0000 | [diff] [blame] | 356 | TypedefDecl *ASTContext::getInt128Decl() const { |
| 357 | if (!Int128Decl) { |
| 358 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(Int128Ty); |
| 359 | Int128Decl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 360 | getTranslationUnitDecl(), |
| 361 | SourceLocation(), |
| 362 | SourceLocation(), |
| 363 | &Idents.get("__int128_t"), |
| 364 | TInfo); |
| 365 | } |
| 366 | |
| 367 | return Int128Decl; |
| 368 | } |
| 369 | |
| 370 | TypedefDecl *ASTContext::getUInt128Decl() const { |
| 371 | if (!UInt128Decl) { |
| 372 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(UnsignedInt128Ty); |
| 373 | UInt128Decl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 374 | getTranslationUnitDecl(), |
| 375 | SourceLocation(), |
| 376 | SourceLocation(), |
| 377 | &Idents.get("__uint128_t"), |
| 378 | TInfo); |
| 379 | } |
| 380 | |
| 381 | return UInt128Decl; |
| 382 | } |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 383 | |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 384 | void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) { |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 385 | BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K); |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 386 | R = CanQualType::CreateUnsafe(QualType(Ty, 0)); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 387 | Types.push_back(Ty); |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 390 | void ASTContext::InitBuiltinTypes(const TargetInfo &Target) { |
| 391 | assert((!this->Target || this->Target == &Target) && |
| 392 | "Incorrect target reinitialization"); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 393 | assert(VoidTy.isNull() && "Context reinitialized?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 394 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 395 | this->Target = &Target; |
| 396 | |
| 397 | ABI.reset(createCXXABI(Target)); |
| 398 | AddrSpaceMap = getAddressSpaceMap(Target, LangOpts); |
| 399 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 400 | // C99 6.2.5p19. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 401 | InitBuiltinType(VoidTy, BuiltinType::Void); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 402 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 403 | // C99 6.2.5p2. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 404 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 405 | // C99 6.2.5p3. |
Eli Friedman | 9ffd4a9 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 406 | if (LangOpts.CharIsSigned) |
Chris Lattner | b16f455 | 2007-06-03 07:25:34 +0000 | [diff] [blame] | 407 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 408 | else |
| 409 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 410 | // C99 6.2.5p4. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 411 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 412 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 413 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 414 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 415 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 416 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 417 | // C99 6.2.5p6. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 418 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 419 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 420 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 421 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 422 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 423 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 424 | // C99 6.2.5p10. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 425 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 426 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 427 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 428 | |
Chris Lattner | f122cef | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 429 | // GNU extension, 128-bit integers. |
| 430 | InitBuiltinType(Int128Ty, BuiltinType::Int128); |
| 431 | InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128); |
| 432 | |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 433 | if (LangOpts.CPlusPlus) { // C++ 3.9.1p5 |
Eli Friedman | 786d087 | 2011-04-30 19:24:24 +0000 | [diff] [blame] | 434 | if (TargetInfo::isTypeSigned(Target.getWCharType())) |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 435 | InitBuiltinType(WCharTy, BuiltinType::WChar_S); |
| 436 | else // -fshort-wchar makes wchar_t be unsigned. |
| 437 | InitBuiltinType(WCharTy, BuiltinType::WChar_U); |
| 438 | } else // C99 |
Chris Lattner | 007cb02 | 2009-02-26 23:43:47 +0000 | [diff] [blame] | 439 | WCharTy = getFromTargetType(Target.getWCharType()); |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 440 | |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 441 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 442 | InitBuiltinType(Char16Ty, BuiltinType::Char16); |
| 443 | else // C99 |
| 444 | Char16Ty = getFromTargetType(Target.getChar16Type()); |
| 445 | |
| 446 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 447 | InitBuiltinType(Char32Ty, BuiltinType::Char32); |
| 448 | else // C99 |
| 449 | Char32Ty = getFromTargetType(Target.getChar32Type()); |
| 450 | |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 451 | // Placeholder type for type-dependent expressions whose type is |
| 452 | // completely unknown. No code should ever check a type against |
| 453 | // DependentTy and users should never see it; however, it is here to |
| 454 | // help diagnose failures to properly check for type-dependent |
| 455 | // expressions. |
| 456 | InitBuiltinType(DependentTy, BuiltinType::Dependent); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 457 | |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 458 | // Placeholder type for functions. |
| 459 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
| 460 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 461 | // Placeholder type for bound members. |
| 462 | InitBuiltinType(BoundMemberTy, BuiltinType::BoundMember); |
| 463 | |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 464 | // "any" type; useful for debugger-like clients. |
| 465 | InitBuiltinType(UnknownAnyTy, BuiltinType::UnknownAny); |
| 466 | |
John McCall | 8a6b59a | 2011-10-17 18:09:15 +0000 | [diff] [blame^] | 467 | // Placeholder type for unbridged ARC casts. |
| 468 | InitBuiltinType(ARCUnbridgedCastTy, BuiltinType::ARCUnbridgedCast); |
| 469 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 470 | // C99 6.2.5p11. |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 471 | FloatComplexTy = getComplexType(FloatTy); |
| 472 | DoubleComplexTy = getComplexType(DoubleTy); |
| 473 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 474 | |
Steve Naroff | 66e9f33 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 475 | BuiltinVaListType = QualType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 476 | |
Fariborz Jahanian | 252ba5f | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 477 | // Builtin types for 'id', 'Class', and 'SEL'. |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 478 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); |
| 479 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); |
Fariborz Jahanian | 252ba5f | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 480 | InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 481 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 482 | ObjCConstantStringType = QualType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 483 | |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 484 | // void * type |
| 485 | VoidPtrTy = getPointerType(VoidTy); |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 486 | |
| 487 | // nullptr type (C++0x 2.14.7) |
| 488 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 489 | |
| 490 | // half type (OpenCL 6.1.1.1) / ARM NEON __fp16 |
| 491 | InitBuiltinType(HalfTy, BuiltinType::Half); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 492 | } |
| 493 | |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 494 | DiagnosticsEngine &ASTContext::getDiagnostics() const { |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 495 | return SourceMgr.getDiagnostics(); |
| 496 | } |
| 497 | |
Douglas Gregor | 561eceb | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 498 | AttrVec& ASTContext::getDeclAttrs(const Decl *D) { |
| 499 | AttrVec *&Result = DeclAttrs[D]; |
| 500 | if (!Result) { |
| 501 | void *Mem = Allocate(sizeof(AttrVec)); |
| 502 | Result = new (Mem) AttrVec; |
| 503 | } |
| 504 | |
| 505 | return *Result; |
| 506 | } |
| 507 | |
| 508 | /// \brief Erase the attributes corresponding to the given declaration. |
| 509 | void ASTContext::eraseDeclAttrs(const Decl *D) { |
| 510 | llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D); |
| 511 | if (Pos != DeclAttrs.end()) { |
| 512 | Pos->second->~AttrVec(); |
| 513 | DeclAttrs.erase(Pos); |
| 514 | } |
| 515 | } |
| 516 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 517 | MemberSpecializationInfo * |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 518 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 519 | assert(Var->isStaticDataMember() && "Not a static data member"); |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 520 | llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 521 | = InstantiatedFromStaticDataMember.find(Var); |
| 522 | if (Pos == InstantiatedFromStaticDataMember.end()) |
| 523 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 524 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 525 | return Pos->second; |
| 526 | } |
| 527 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 528 | void |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 529 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, |
Argyrios Kyrtzidis | cdb8b3f | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 530 | TemplateSpecializationKind TSK, |
| 531 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 532 | assert(Inst->isStaticDataMember() && "Not a static data member"); |
| 533 | assert(Tmpl->isStaticDataMember() && "Not a static data member"); |
| 534 | assert(!InstantiatedFromStaticDataMember[Inst] && |
| 535 | "Already noted what static data member was instantiated from"); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 536 | InstantiatedFromStaticDataMember[Inst] |
Argyrios Kyrtzidis | cdb8b3f | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 537 | = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation); |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 540 | FunctionDecl *ASTContext::getClassScopeSpecializationPattern( |
| 541 | const FunctionDecl *FD){ |
| 542 | assert(FD && "Specialization is 0"); |
| 543 | llvm::DenseMap<const FunctionDecl*, FunctionDecl *>::const_iterator Pos |
Francois Pichet | 5792825 | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 544 | = ClassScopeSpecializationPattern.find(FD); |
| 545 | if (Pos == ClassScopeSpecializationPattern.end()) |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 546 | return 0; |
| 547 | |
| 548 | return Pos->second; |
| 549 | } |
| 550 | |
| 551 | void ASTContext::setClassScopeSpecializationPattern(FunctionDecl *FD, |
| 552 | FunctionDecl *Pattern) { |
| 553 | assert(FD && "Specialization is 0"); |
| 554 | assert(Pattern && "Class scope specialization pattern is 0"); |
Francois Pichet | 5792825 | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 555 | ClassScopeSpecializationPattern[FD] = Pattern; |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 556 | } |
| 557 | |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 558 | NamedDecl * |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 559 | ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) { |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 560 | llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 561 | = InstantiatedFromUsingDecl.find(UUD); |
| 562 | if (Pos == InstantiatedFromUsingDecl.end()) |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 563 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 564 | |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 565 | return Pos->second; |
| 566 | } |
| 567 | |
| 568 | void |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 569 | ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) { |
| 570 | assert((isa<UsingDecl>(Pattern) || |
| 571 | isa<UnresolvedUsingValueDecl>(Pattern) || |
| 572 | isa<UnresolvedUsingTypenameDecl>(Pattern)) && |
| 573 | "pattern decl is not a using decl"); |
| 574 | assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists"); |
| 575 | InstantiatedFromUsingDecl[Inst] = Pattern; |
| 576 | } |
| 577 | |
| 578 | UsingShadowDecl * |
| 579 | ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) { |
| 580 | llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos |
| 581 | = InstantiatedFromUsingShadowDecl.find(Inst); |
| 582 | if (Pos == InstantiatedFromUsingShadowDecl.end()) |
| 583 | return 0; |
| 584 | |
| 585 | return Pos->second; |
| 586 | } |
| 587 | |
| 588 | void |
| 589 | ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, |
| 590 | UsingShadowDecl *Pattern) { |
| 591 | assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists"); |
| 592 | InstantiatedFromUsingShadowDecl[Inst] = Pattern; |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 595 | FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { |
| 596 | llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos |
| 597 | = InstantiatedFromUnnamedFieldDecl.find(Field); |
| 598 | if (Pos == InstantiatedFromUnnamedFieldDecl.end()) |
| 599 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 600 | |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 601 | return Pos->second; |
| 602 | } |
| 603 | |
| 604 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, |
| 605 | FieldDecl *Tmpl) { |
| 606 | assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed"); |
| 607 | assert(!Tmpl->getDeclName() && "Template field decl is not unnamed"); |
| 608 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] && |
| 609 | "Already noted what unnamed field was instantiated from"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 610 | |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 611 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; |
| 612 | } |
| 613 | |
Fariborz Jahanian | 595ec5d | 2011-04-27 17:14:21 +0000 | [diff] [blame] | 614 | bool ASTContext::ZeroBitfieldFollowsNonBitfield(const FieldDecl *FD, |
| 615 | const FieldDecl *LastFD) const { |
| 616 | return (FD->isBitField() && LastFD && !LastFD->isBitField() && |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 617 | FD->getBitWidthValue(*this) == 0); |
Fariborz Jahanian | 595ec5d | 2011-04-27 17:14:21 +0000 | [diff] [blame] | 618 | } |
| 619 | |
Fariborz Jahanian | eb39741 | 2011-05-02 17:20:56 +0000 | [diff] [blame] | 620 | bool ASTContext::ZeroBitfieldFollowsBitfield(const FieldDecl *FD, |
| 621 | const FieldDecl *LastFD) const { |
| 622 | return (FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 623 | FD->getBitWidthValue(*this) == 0 && |
| 624 | LastFD->getBitWidthValue(*this) != 0); |
Fariborz Jahanian | eb39741 | 2011-05-02 17:20:56 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Fariborz Jahanian | 84335f7 | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 627 | bool ASTContext::BitfieldFollowsBitfield(const FieldDecl *FD, |
| 628 | const FieldDecl *LastFD) const { |
| 629 | return (FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 630 | FD->getBitWidthValue(*this) && |
| 631 | LastFD->getBitWidthValue(*this)); |
Fariborz Jahanian | 84335f7 | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Chad Rosier | f01a7dd | 2011-08-04 23:34:15 +0000 | [diff] [blame] | 634 | bool ASTContext::NonBitfieldFollowsBitfield(const FieldDecl *FD, |
Fariborz Jahanian | b7a2879 | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 635 | const FieldDecl *LastFD) const { |
| 636 | return (!FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 637 | LastFD->getBitWidthValue(*this)); |
Fariborz Jahanian | b7a2879 | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Chad Rosier | f01a7dd | 2011-08-04 23:34:15 +0000 | [diff] [blame] | 640 | bool ASTContext::BitfieldFollowsNonBitfield(const FieldDecl *FD, |
Fariborz Jahanian | b7a2879 | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 641 | const FieldDecl *LastFD) const { |
| 642 | return (FD->isBitField() && LastFD && !LastFD->isBitField() && |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 643 | FD->getBitWidthValue(*this)); |
Fariborz Jahanian | b7a2879 | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 646 | ASTContext::overridden_cxx_method_iterator |
| 647 | ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const { |
| 648 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 649 | = OverriddenMethods.find(Method); |
| 650 | if (Pos == OverriddenMethods.end()) |
| 651 | return 0; |
| 652 | |
| 653 | return Pos->second.begin(); |
| 654 | } |
| 655 | |
| 656 | ASTContext::overridden_cxx_method_iterator |
| 657 | ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const { |
| 658 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 659 | = OverriddenMethods.find(Method); |
| 660 | if (Pos == OverriddenMethods.end()) |
| 661 | return 0; |
| 662 | |
| 663 | return Pos->second.end(); |
| 664 | } |
| 665 | |
Argyrios Kyrtzidis | 6685e8a | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 666 | unsigned |
| 667 | ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const { |
| 668 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 669 | = OverriddenMethods.find(Method); |
| 670 | if (Pos == OverriddenMethods.end()) |
| 671 | return 0; |
| 672 | |
| 673 | return Pos->second.size(); |
| 674 | } |
| 675 | |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 676 | void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method, |
| 677 | const CXXMethodDecl *Overridden) { |
| 678 | OverriddenMethods[Method].push_back(Overridden); |
| 679 | } |
| 680 | |
Chris Lattner | 53cfe80 | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 681 | //===----------------------------------------------------------------------===// |
| 682 | // Type Sizing and Analysis |
| 683 | //===----------------------------------------------------------------------===// |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 684 | |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 685 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 686 | /// scalar floating point type. |
| 687 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 688 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 689 | assert(BT && "Not a floating point type!"); |
| 690 | switch (BT->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 691 | default: llvm_unreachable("Not a floating point type!"); |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 692 | case BuiltinType::Half: return Target->getHalfFormat(); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 693 | case BuiltinType::Float: return Target->getFloatFormat(); |
| 694 | case BuiltinType::Double: return Target->getDoubleFormat(); |
| 695 | case BuiltinType::LongDouble: return Target->getLongDoubleFormat(); |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 696 | } |
| 697 | } |
| 698 | |
Ken Dyck | 160146e | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 699 | /// getDeclAlign - Return a conservative estimate of the alignment of the |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 700 | /// specified decl. Note that bitfields do not have a valid alignment, so |
| 701 | /// this method will assert on them. |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 702 | /// If @p RefAsPointee, references are treated like their underlying type |
| 703 | /// (for alignof), else they're treated like pointers (for CodeGen). |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 704 | CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) const { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 705 | unsigned Align = Target->getCharWidth(); |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 706 | |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 707 | bool UseAlignAttrOnly = false; |
| 708 | if (unsigned AlignFromAttr = D->getMaxAlignment()) { |
| 709 | Align = AlignFromAttr; |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 710 | |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 711 | // __attribute__((aligned)) can increase or decrease alignment |
| 712 | // *except* on a struct or struct member, where it only increases |
| 713 | // alignment unless 'packed' is also specified. |
| 714 | // |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 715 | // It is an error for alignas to decrease alignment, so we can |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 716 | // ignore that possibility; Sema should diagnose it. |
| 717 | if (isa<FieldDecl>(D)) { |
| 718 | UseAlignAttrOnly = D->hasAttr<PackedAttr>() || |
| 719 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
| 720 | } else { |
| 721 | UseAlignAttrOnly = true; |
| 722 | } |
| 723 | } |
Fariborz Jahanian | 9f10718 | 2011-05-05 21:19:14 +0000 | [diff] [blame] | 724 | else if (isa<FieldDecl>(D)) |
| 725 | UseAlignAttrOnly = |
| 726 | D->hasAttr<PackedAttr>() || |
| 727 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 728 | |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 729 | // If we're using the align attribute only, just ignore everything |
| 730 | // else about the declaration and its type. |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 731 | if (UseAlignAttrOnly) { |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 732 | // do nothing |
| 733 | |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 734 | } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 735 | QualType T = VD->getType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 736 | if (const ReferenceType* RT = T->getAs<ReferenceType>()) { |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 737 | if (RefAsPointee) |
| 738 | T = RT->getPointeeType(); |
| 739 | else |
| 740 | T = getPointerType(RT->getPointeeType()); |
| 741 | } |
| 742 | if (!T->isIncompleteType() && !T->isFunctionType()) { |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 743 | // Adjust alignments of declarations with array type by the |
| 744 | // large-array alignment on the target. |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 745 | unsigned MinWidth = Target->getLargeArrayMinWidth(); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 746 | const ArrayType *arrayType; |
| 747 | if (MinWidth && (arrayType = getAsArrayType(T))) { |
| 748 | if (isa<VariableArrayType>(arrayType)) |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 749 | Align = std::max(Align, Target->getLargeArrayAlign()); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 750 | else if (isa<ConstantArrayType>(arrayType) && |
| 751 | MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType))) |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 752 | Align = std::max(Align, Target->getLargeArrayAlign()); |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 753 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 754 | // Walk through any array types while we're at it. |
| 755 | T = getBaseElementType(arrayType); |
| 756 | } |
Chad Rosier | 99ee782 | 2011-07-26 07:03:04 +0000 | [diff] [blame] | 757 | Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 758 | } |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 759 | |
| 760 | // Fields can be subject to extra alignment constraints, like if |
| 761 | // the field is packed, the struct is packed, or the struct has a |
| 762 | // a max-field-alignment constraint (#pragma pack). So calculate |
| 763 | // the actual alignment of the field within the struct, and then |
| 764 | // (as we're expected to) constrain that by the alignment of the type. |
| 765 | if (const FieldDecl *field = dyn_cast<FieldDecl>(VD)) { |
| 766 | // So calculate the alignment of the field. |
| 767 | const ASTRecordLayout &layout = getASTRecordLayout(field->getParent()); |
| 768 | |
| 769 | // Start with the record's overall alignment. |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 770 | unsigned fieldAlign = toBits(layout.getAlignment()); |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 771 | |
| 772 | // Use the GCD of that and the offset within the record. |
| 773 | uint64_t offset = layout.getFieldOffset(field->getFieldIndex()); |
| 774 | if (offset > 0) { |
| 775 | // Alignment is always a power of 2, so the GCD will be a power of 2, |
| 776 | // which means we get to do this crazy thing instead of Euclid's. |
| 777 | uint64_t lowBitOfOffset = offset & (~offset + 1); |
| 778 | if (lowBitOfOffset < fieldAlign) |
| 779 | fieldAlign = static_cast<unsigned>(lowBitOfOffset); |
| 780 | } |
| 781 | |
| 782 | Align = std::min(Align, fieldAlign); |
Charles Davis | 3fc5107 | 2010-02-23 04:52:00 +0000 | [diff] [blame] | 783 | } |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 784 | } |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 785 | |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 786 | return toCharUnitsFromBits(Align); |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 787 | } |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 788 | |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 789 | std::pair<CharUnits, CharUnits> |
Ken Dyck | d24099d | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 790 | ASTContext::getTypeInfoInChars(const Type *T) const { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 791 | std::pair<uint64_t, unsigned> Info = getTypeInfo(T); |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 792 | return std::make_pair(toCharUnitsFromBits(Info.first), |
| 793 | toCharUnitsFromBits(Info.second)); |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | std::pair<CharUnits, CharUnits> |
Ken Dyck | d24099d | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 797 | ASTContext::getTypeInfoInChars(QualType T) const { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 798 | return getTypeInfoInChars(T.getTypePtr()); |
| 799 | } |
| 800 | |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 801 | /// getTypeSize - Return the size of the specified type, in bits. This method |
| 802 | /// does not work on incomplete types. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 803 | /// |
| 804 | /// FIXME: Pointers into different addr spaces could have different sizes and |
| 805 | /// alignment requirements: getPointerInfo should take an AddrSpace, this |
| 806 | /// should take a QualType, &c. |
Chris Lattner | 4481b42 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 807 | std::pair<uint64_t, unsigned> |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 808 | ASTContext::getTypeInfo(const Type *T) const { |
Mike Stump | 5b9a3d5 | 2009-02-27 18:32:39 +0000 | [diff] [blame] | 809 | uint64_t Width=0; |
| 810 | unsigned Align=8; |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 811 | switch (T->getTypeClass()) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 812 | #define TYPE(Class, Base) |
| 813 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 814 | #define NON_CANONICAL_TYPE(Class, Base) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 815 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 816 | #include "clang/AST/TypeNodes.def" |
John McCall | 15547bb | 2011-06-28 16:49:23 +0000 | [diff] [blame] | 817 | llvm_unreachable("Should not see dependent types"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 818 | break; |
| 819 | |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 820 | case Type::FunctionNoProto: |
| 821 | case Type::FunctionProto: |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 822 | // GCC extension: alignof(function) = 32 bits |
| 823 | Width = 0; |
| 824 | Align = 32; |
| 825 | break; |
| 826 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 827 | case Type::IncompleteArray: |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 828 | case Type::VariableArray: |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 829 | Width = 0; |
| 830 | Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); |
| 831 | break; |
| 832 | |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 833 | case Type::ConstantArray: { |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 834 | const ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 835 | |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 836 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 837 | Width = EltInfo.first*CAT->getSize().getZExtValue(); |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 838 | Align = EltInfo.second; |
Argyrios Kyrtzidis | ae40e4e | 2011-04-26 21:05:39 +0000 | [diff] [blame] | 839 | Width = llvm::RoundUpToAlignment(Width, Align); |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 840 | break; |
Christopher Lamb | c5fafa2 | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 841 | } |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 842 | case Type::ExtVector: |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 843 | case Type::Vector: { |
Chris Lattner | 63d2b36 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 844 | const VectorType *VT = cast<VectorType>(T); |
| 845 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType()); |
| 846 | Width = EltInfo.first*VT->getNumElements(); |
Eli Friedman | 3df5efe | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 847 | Align = Width; |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 848 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 849 | // This happens for non-power-of-2 length vectors. |
Dan Gohman | ef78c8e | 2010-04-21 23:32:43 +0000 | [diff] [blame] | 850 | if (Align & (Align-1)) { |
Chris Lattner | 63d2b36 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 851 | Align = llvm::NextPowerOf2(Align); |
| 852 | Width = llvm::RoundUpToAlignment(Width, Align); |
| 853 | } |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 854 | break; |
| 855 | } |
Chris Lattner | 647fb22 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 856 | |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 857 | case Type::Builtin: |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 858 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 859 | default: llvm_unreachable("Unknown builtin type!"); |
Chris Lattner | 4481b42 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 860 | case BuiltinType::Void: |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 861 | // GCC extension: alignof(void) = 8 bits. |
| 862 | Width = 0; |
| 863 | Align = 8; |
| 864 | break; |
| 865 | |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 866 | case BuiltinType::Bool: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 867 | Width = Target->getBoolWidth(); |
| 868 | Align = Target->getBoolAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 869 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 870 | case BuiltinType::Char_S: |
| 871 | case BuiltinType::Char_U: |
| 872 | case BuiltinType::UChar: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 873 | case BuiltinType::SChar: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 874 | Width = Target->getCharWidth(); |
| 875 | Align = Target->getCharAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 876 | break; |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 877 | case BuiltinType::WChar_S: |
| 878 | case BuiltinType::WChar_U: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 879 | Width = Target->getWCharWidth(); |
| 880 | Align = Target->getWCharAlign(); |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 881 | break; |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 882 | case BuiltinType::Char16: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 883 | Width = Target->getChar16Width(); |
| 884 | Align = Target->getChar16Align(); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 885 | break; |
| 886 | case BuiltinType::Char32: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 887 | Width = Target->getChar32Width(); |
| 888 | Align = Target->getChar32Align(); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 889 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 890 | case BuiltinType::UShort: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 891 | case BuiltinType::Short: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 892 | Width = Target->getShortWidth(); |
| 893 | Align = Target->getShortAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 894 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 895 | case BuiltinType::UInt: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 896 | case BuiltinType::Int: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 897 | Width = Target->getIntWidth(); |
| 898 | Align = Target->getIntAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 899 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 900 | case BuiltinType::ULong: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 901 | case BuiltinType::Long: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 902 | Width = Target->getLongWidth(); |
| 903 | Align = Target->getLongAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 904 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 905 | case BuiltinType::ULongLong: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 906 | case BuiltinType::LongLong: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 907 | Width = Target->getLongLongWidth(); |
| 908 | Align = Target->getLongLongAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 909 | break; |
Chris Lattner | 0a415ec | 2009-04-30 02:55:13 +0000 | [diff] [blame] | 910 | case BuiltinType::Int128: |
| 911 | case BuiltinType::UInt128: |
| 912 | Width = 128; |
| 913 | Align = 128; // int128_t is 128-bit aligned on all targets. |
| 914 | break; |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 915 | case BuiltinType::Half: |
| 916 | Width = Target->getHalfWidth(); |
| 917 | Align = Target->getHalfAlign(); |
| 918 | break; |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 919 | case BuiltinType::Float: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 920 | Width = Target->getFloatWidth(); |
| 921 | Align = Target->getFloatAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 922 | break; |
| 923 | case BuiltinType::Double: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 924 | Width = Target->getDoubleWidth(); |
| 925 | Align = Target->getDoubleAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 926 | break; |
| 927 | case BuiltinType::LongDouble: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 928 | Width = Target->getLongDoubleWidth(); |
| 929 | Align = Target->getLongDoubleAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 930 | break; |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 931 | case BuiltinType::NullPtr: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 932 | Width = Target->getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) |
| 933 | Align = Target->getPointerAlign(0); // == sizeof(void*) |
Sebastian Redl | a81b0b7 | 2009-05-27 19:34:06 +0000 | [diff] [blame] | 934 | break; |
Fariborz Jahanian | 9c6a39e | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 935 | case BuiltinType::ObjCId: |
| 936 | case BuiltinType::ObjCClass: |
| 937 | case BuiltinType::ObjCSel: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 938 | Width = Target->getPointerWidth(0); |
| 939 | Align = Target->getPointerAlign(0); |
Fariborz Jahanian | 9c6a39e | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 940 | break; |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 941 | } |
Chris Lattner | 48f84b8 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 942 | break; |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 943 | case Type::ObjCObjectPointer: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 944 | Width = Target->getPointerWidth(0); |
| 945 | Align = Target->getPointerAlign(0); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 946 | break; |
Steve Naroff | 921a45c | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 947 | case Type::BlockPointer: { |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 948 | unsigned AS = getTargetAddressSpace( |
| 949 | cast<BlockPointerType>(T)->getPointeeType()); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 950 | Width = Target->getPointerWidth(AS); |
| 951 | Align = Target->getPointerAlign(AS); |
Steve Naroff | 921a45c | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 952 | break; |
| 953 | } |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 954 | case Type::LValueReference: |
| 955 | case Type::RValueReference: { |
| 956 | // alignof and sizeof should never enter this code path here, so we go |
| 957 | // the pointer route. |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 958 | unsigned AS = getTargetAddressSpace( |
| 959 | cast<ReferenceType>(T)->getPointeeType()); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 960 | Width = Target->getPointerWidth(AS); |
| 961 | Align = Target->getPointerAlign(AS); |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 962 | break; |
| 963 | } |
Chris Lattner | 2dca6ff | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 964 | case Type::Pointer: { |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 965 | unsigned AS = getTargetAddressSpace(cast<PointerType>(T)->getPointeeType()); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 966 | Width = Target->getPointerWidth(AS); |
| 967 | Align = Target->getPointerAlign(AS); |
Chris Lattner | 2dca6ff | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 968 | break; |
| 969 | } |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 970 | case Type::MemberPointer: { |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 971 | const MemberPointerType *MPT = cast<MemberPointerType>(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 972 | std::pair<uint64_t, unsigned> PtrDiffInfo = |
Anders Carlsson | 32440a0 | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 973 | getTypeInfo(getPointerDiffType()); |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 974 | Width = PtrDiffInfo.first * ABI->getMemberPointerSize(MPT); |
Anders Carlsson | 32440a0 | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 975 | Align = PtrDiffInfo.second; |
| 976 | break; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 977 | } |
Chris Lattner | 647fb22 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 978 | case Type::Complex: { |
| 979 | // Complex types have the same alignment as their elements, but twice the |
| 980 | // size. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 981 | std::pair<uint64_t, unsigned> EltInfo = |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 982 | getTypeInfo(cast<ComplexType>(T)->getElementType()); |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 983 | Width = EltInfo.first*2; |
Chris Lattner | 647fb22 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 984 | Align = EltInfo.second; |
| 985 | break; |
| 986 | } |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 987 | case Type::ObjCObject: |
| 988 | return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr()); |
Devang Patel | dbb7263 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 989 | case Type::ObjCInterface: { |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 990 | const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | dbb7263 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 991 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 992 | Width = toBits(Layout.getSize()); |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 993 | Align = toBits(Layout.getAlignment()); |
Devang Patel | dbb7263 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 994 | break; |
| 995 | } |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 996 | case Type::Record: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 997 | case Type::Enum: { |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 998 | const TagType *TT = cast<TagType>(T); |
| 999 | |
| 1000 | if (TT->getDecl()->isInvalidDecl()) { |
Douglas Gregor | 7f97189 | 2011-04-20 17:29:44 +0000 | [diff] [blame] | 1001 | Width = 8; |
| 1002 | Align = 8; |
Chris Lattner | 572100b | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 1003 | break; |
| 1004 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1005 | |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1006 | if (const EnumType *ET = dyn_cast<EnumType>(TT)) |
Chris Lattner | 8b23c25 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1007 | return getTypeInfo(ET->getDecl()->getIntegerType()); |
| 1008 | |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1009 | const RecordType *RT = cast<RecordType>(TT); |
Chris Lattner | 8b23c25 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1010 | const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1011 | Width = toBits(Layout.getSize()); |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 1012 | Align = toBits(Layout.getAlignment()); |
Chris Lattner | 49a953a | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 1013 | break; |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1014 | } |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1015 | |
Chris Lattner | 63d2b36 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1016 | case Type::SubstTemplateTypeParm: |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1017 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> |
| 1018 | getReplacementType().getTypePtr()); |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1019 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1020 | case Type::Auto: { |
| 1021 | const AutoType *A = cast<AutoType>(T); |
| 1022 | assert(A->isDeduced() && "Cannot request the size of a dependent type"); |
Matt Beaumont-Gay | 7a24210e | 2011-02-22 20:00:16 +0000 | [diff] [blame] | 1023 | return getTypeInfo(A->getDeducedType().getTypePtr()); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1026 | case Type::Paren: |
| 1027 | return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr()); |
| 1028 | |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1029 | case Type::Typedef: { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1030 | const TypedefNameDecl *Typedef = cast<TypedefType>(T)->getDecl(); |
Douglas Gregor | f5bae22 | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 1031 | std::pair<uint64_t, unsigned> Info |
| 1032 | = getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); |
Chris Lattner | 29eb47b | 2011-02-19 22:55:41 +0000 | [diff] [blame] | 1033 | // If the typedef has an aligned attribute on it, it overrides any computed |
| 1034 | // alignment we have. This violates the GCC documentation (which says that |
| 1035 | // attribute(aligned) can only round up) but matches its implementation. |
| 1036 | if (unsigned AttrAlign = Typedef->getMaxAlignment()) |
| 1037 | Align = AttrAlign; |
| 1038 | else |
| 1039 | Align = Info.second; |
Douglas Gregor | f5bae22 | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 1040 | Width = Info.first; |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1041 | break; |
Chris Lattner | 8b23c25 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1042 | } |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1043 | |
| 1044 | case Type::TypeOfExpr: |
| 1045 | return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType() |
| 1046 | .getTypePtr()); |
| 1047 | |
| 1048 | case Type::TypeOf: |
| 1049 | return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr()); |
| 1050 | |
Anders Carlsson | 81df7b8 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 1051 | case Type::Decltype: |
| 1052 | return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType() |
| 1053 | .getTypePtr()); |
| 1054 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1055 | case Type::UnaryTransform: |
| 1056 | return getTypeInfo(cast<UnaryTransformType>(T)->getUnderlyingType()); |
| 1057 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1058 | case Type::Elaborated: |
| 1059 | return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1060 | |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 1061 | case Type::Attributed: |
| 1062 | return getTypeInfo( |
| 1063 | cast<AttributedType>(T)->getEquivalentType().getTypePtr()); |
| 1064 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1065 | case Type::TemplateSpecialization: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1066 | assert(getCanonicalType(T) != T && |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1067 | "Cannot request the size of a dependent type"); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1068 | const TemplateSpecializationType *TST = cast<TemplateSpecializationType>(T); |
| 1069 | // A type alias template specialization may refer to a typedef with the |
| 1070 | // aligned attribute on it. |
| 1071 | if (TST->isTypeAlias()) |
| 1072 | return getTypeInfo(TST->getAliasedType().getTypePtr()); |
| 1073 | else |
| 1074 | return getTypeInfo(getCanonicalType(T)); |
| 1075 | } |
| 1076 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1077 | case Type::Atomic: { |
Eli Friedman | 4b72fdd | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 1078 | std::pair<uint64_t, unsigned> Info |
| 1079 | = getTypeInfo(cast<AtomicType>(T)->getValueType()); |
| 1080 | Width = Info.first; |
| 1081 | Align = Info.second; |
| 1082 | if (Width != 0 && Width <= Target->getMaxAtomicPromoteWidth() && |
| 1083 | llvm::isPowerOf2_64(Width)) { |
| 1084 | // We can potentially perform lock-free atomic operations for this |
| 1085 | // type; promote the alignment appropriately. |
| 1086 | // FIXME: We could potentially promote the width here as well... |
| 1087 | // is that worthwhile? (Non-struct atomic types generally have |
| 1088 | // power-of-two size anyway, but structs might not. Requires a bit |
| 1089 | // of implementation work to make sure we zero out the extra bits.) |
| 1090 | Align = static_cast<unsigned>(Width); |
| 1091 | } |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1094 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1095 | |
Eli Friedman | 4b72fdd | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 1096 | assert(llvm::isPowerOf2_32(Align) && "Alignment must be power of 2"); |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1097 | return std::make_pair(Width, Align); |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1100 | /// toCharUnitsFromBits - Convert a size in bits to a size in characters. |
| 1101 | CharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const { |
| 1102 | return CharUnits::fromQuantity(BitSize / getCharWidth()); |
| 1103 | } |
| 1104 | |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1105 | /// toBits - Convert a size in characters to a size in characters. |
| 1106 | int64_t ASTContext::toBits(CharUnits CharSize) const { |
| 1107 | return CharSize.getQuantity() * getCharWidth(); |
| 1108 | } |
| 1109 | |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1110 | /// getTypeSizeInChars - Return the size of the specified type, in characters. |
| 1111 | /// This method does not work on incomplete types. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1112 | CharUnits ASTContext::getTypeSizeInChars(QualType T) const { |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1113 | return toCharUnitsFromBits(getTypeSize(T)); |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1114 | } |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1115 | CharUnits ASTContext::getTypeSizeInChars(const Type *T) const { |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1116 | return toCharUnitsFromBits(getTypeSize(T)); |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
Ken Dyck | a6046ab | 2010-01-26 17:25:18 +0000 | [diff] [blame] | 1119 | /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in |
Ken Dyck | 24d28d6 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1120 | /// characters. This method does not work on incomplete types. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1121 | CharUnits ASTContext::getTypeAlignInChars(QualType T) const { |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1122 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 24d28d6 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1123 | } |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1124 | CharUnits ASTContext::getTypeAlignInChars(const Type *T) const { |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1125 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 24d28d6 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
Chris Lattner | a3402cd | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1128 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 1129 | /// type for the current target in bits. This can be different than the ABI |
| 1130 | /// alignment in cases where it is beneficial for performance to overalign |
| 1131 | /// a data type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1132 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) const { |
Chris Lattner | a3402cd | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1133 | unsigned ABIAlign = getTypeAlign(T); |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1134 | |
| 1135 | // Double and long long should be naturally aligned if possible. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1136 | if (const ComplexType* CT = T->getAs<ComplexType>()) |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1137 | T = CT->getElementType().getTypePtr(); |
| 1138 | if (T->isSpecificBuiltinType(BuiltinType::Double) || |
| 1139 | T->isSpecificBuiltinType(BuiltinType::LongLong)) |
| 1140 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); |
| 1141 | |
Chris Lattner | a3402cd | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1142 | return ABIAlign; |
| 1143 | } |
| 1144 | |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1145 | /// DeepCollectObjCIvars - |
| 1146 | /// This routine first collects all declared, but not synthesized, ivars in |
| 1147 | /// super class and then collects all ivars, including those synthesized for |
| 1148 | /// current class. This routine is used for implementation of current class |
| 1149 | /// when all ivars, declared and synthesized are known. |
Fariborz Jahanian | 0f44d81 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 1150 | /// |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1151 | void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 1152 | bool leafClass, |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 1153 | SmallVectorImpl<const ObjCIvarDecl*> &Ivars) const { |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1154 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
| 1155 | DeepCollectObjCIvars(SuperClass, false, Ivars); |
| 1156 | if (!leafClass) { |
| 1157 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 1158 | E = OI->ivar_end(); I != E; ++I) |
Fariborz Jahanian | aef6622 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 1159 | Ivars.push_back(*I); |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 1160 | } else { |
Fariborz Jahanian | b26d578 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 1161 | ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI); |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 1162 | for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv; |
Fariborz Jahanian | b26d578 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 1163 | Iv= Iv->getNextIvar()) |
| 1164 | Ivars.push_back(Iv); |
| 1165 | } |
Fariborz Jahanian | 0f44d81 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1168 | /// CollectInheritedProtocols - Collect all protocols in current class and |
| 1169 | /// those inherited by it. |
| 1170 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 1171 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) { |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1172 | if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
Ted Kremenek | 0ef508d | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1173 | // We can use protocol_iterator here instead of |
| 1174 | // all_referenced_protocol_iterator since we are walking all categories. |
| 1175 | for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(), |
| 1176 | PE = OI->all_referenced_protocol_end(); P != PE; ++P) { |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1177 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 1178 | Protocols.insert(Proto); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1179 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
Fariborz Jahanian | 8e3b9db | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 1180 | PE = Proto->protocol_end(); P != PE; ++P) { |
| 1181 | Protocols.insert(*P); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1182 | CollectInheritedProtocols(*P, Protocols); |
| 1183 | } |
Fariborz Jahanian | 8e3b9db | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 1184 | } |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1185 | |
| 1186 | // Categories of this Interface. |
| 1187 | for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList(); |
| 1188 | CDeclChain; CDeclChain = CDeclChain->getNextClassCategory()) |
| 1189 | CollectInheritedProtocols(CDeclChain, Protocols); |
| 1190 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) |
| 1191 | while (SD) { |
| 1192 | CollectInheritedProtocols(SD, Protocols); |
| 1193 | SD = SD->getSuperClass(); |
| 1194 | } |
Benjamin Kramer | 0a3fe04 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1195 | } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Ted Kremenek | 0ef508d | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1196 | for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(), |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1197 | PE = OC->protocol_end(); P != PE; ++P) { |
| 1198 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 1199 | Protocols.insert(Proto); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1200 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 1201 | PE = Proto->protocol_end(); P != PE; ++P) |
| 1202 | CollectInheritedProtocols(*P, Protocols); |
| 1203 | } |
Benjamin Kramer | 0a3fe04 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1204 | } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1205 | for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(), |
| 1206 | PE = OP->protocol_end(); P != PE; ++P) { |
| 1207 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 1208 | Protocols.insert(Proto); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1209 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 1210 | PE = Proto->protocol_end(); P != PE; ++P) |
| 1211 | CollectInheritedProtocols(*P, Protocols); |
| 1212 | } |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1213 | } |
| 1214 | } |
| 1215 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1216 | unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const { |
Fariborz Jahanian | d2ae2d0 | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1217 | unsigned count = 0; |
| 1218 | // Count ivars declared in class extension. |
Fariborz Jahanian | 3bf0ded | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 1219 | for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl; |
| 1220 | CDecl = CDecl->getNextClassExtension()) |
Benjamin Kramer | 0a3fe04 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1221 | count += CDecl->ivar_size(); |
| 1222 | |
Fariborz Jahanian | d2ae2d0 | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1223 | // Count ivar defined in this class's implementation. This |
| 1224 | // includes synthesized ivars. |
| 1225 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) |
Benjamin Kramer | 0a3fe04 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1226 | count += ImplDecl->ivar_size(); |
| 1227 | |
Fariborz Jahanian | 7c80959 | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 1228 | return count; |
| 1229 | } |
| 1230 | |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1231 | /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. |
| 1232 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
| 1233 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1234 | I = ObjCImpls.find(D); |
| 1235 | if (I != ObjCImpls.end()) |
| 1236 | return cast<ObjCImplementationDecl>(I->second); |
| 1237 | return 0; |
| 1238 | } |
| 1239 | /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. |
| 1240 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
| 1241 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1242 | I = ObjCImpls.find(D); |
| 1243 | if (I != ObjCImpls.end()) |
| 1244 | return cast<ObjCCategoryImplDecl>(I->second); |
| 1245 | return 0; |
| 1246 | } |
| 1247 | |
| 1248 | /// \brief Set the implementation of ObjCInterfaceDecl. |
| 1249 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
| 1250 | ObjCImplementationDecl *ImplD) { |
| 1251 | assert(IFaceD && ImplD && "Passed null params"); |
| 1252 | ObjCImpls[IFaceD] = ImplD; |
| 1253 | } |
| 1254 | /// \brief Set the implementation of ObjCCategoryDecl. |
| 1255 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
| 1256 | ObjCCategoryImplDecl *ImplD) { |
| 1257 | assert(CatD && ImplD && "Passed null params"); |
| 1258 | ObjCImpls[CatD] = ImplD; |
| 1259 | } |
| 1260 | |
Fariborz Jahanian | 7cfe767 | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1261 | /// \brief Get the copy initialization expression of VarDecl,or NULL if |
| 1262 | /// none exists. |
Fariborz Jahanian | 5019809 | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1263 | Expr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) { |
Fariborz Jahanian | 1321cbb | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1264 | assert(VD && "Passed null params"); |
| 1265 | assert(VD->hasAttr<BlocksAttr>() && |
| 1266 | "getBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 5019809 | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1267 | llvm::DenseMap<const VarDecl*, Expr*>::iterator |
Fariborz Jahanian | 1321cbb | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1268 | I = BlockVarCopyInits.find(VD); |
Fariborz Jahanian | 7cfe767 | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1269 | return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0; |
| 1270 | } |
| 1271 | |
| 1272 | /// \brief Set the copy inialization expression of a block var decl. |
| 1273 | void ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) { |
| 1274 | assert(VD && Init && "Passed null params"); |
Fariborz Jahanian | 1321cbb | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1275 | assert(VD->hasAttr<BlocksAttr>() && |
| 1276 | "setBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 7cfe767 | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1277 | BlockVarCopyInits[VD] = Init; |
| 1278 | } |
| 1279 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1280 | /// \brief Allocate an uninitialized TypeSourceInfo. |
Argyrios Kyrtzidis | 3f79ad7 | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1281 | /// |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1282 | /// The caller should initialize the memory held by TypeSourceInfo using |
Argyrios Kyrtzidis | 3f79ad7 | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1283 | /// the TypeLoc wrappers. |
| 1284 | /// |
| 1285 | /// \param T the type that will be the basis for type source info. This type |
| 1286 | /// should refer to how the declarator was written in source code, not to |
| 1287 | /// what type semantic analysis resolved the declarator to. |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1288 | TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1289 | unsigned DataSize) const { |
John McCall | 26fe7e0 | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1290 | if (!DataSize) |
| 1291 | DataSize = TypeLoc::getFullDataSizeForType(T); |
| 1292 | else |
| 1293 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1294 | "incorrect data size provided to CreateTypeSourceInfo!"); |
John McCall | 26fe7e0 | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1295 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1296 | TypeSourceInfo *TInfo = |
| 1297 | (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8); |
| 1298 | new (TInfo) TypeSourceInfo(T); |
| 1299 | return TInfo; |
Argyrios Kyrtzidis | 3f79ad7 | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1302 | TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1303 | SourceLocation L) const { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1304 | TypeSourceInfo *DI = CreateTypeSourceInfo(T); |
Douglas Gregor | 2d525f0 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 1305 | DI->getTypeLoc().initialize(const_cast<ASTContext &>(*this), L); |
John McCall | 3665e00 | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 1306 | return DI; |
| 1307 | } |
| 1308 | |
Daniel Dunbar | 02f7f5f | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1309 | const ASTRecordLayout & |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1310 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const { |
Daniel Dunbar | 02f7f5f | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1311 | return getObjCLayout(D, 0); |
| 1312 | } |
| 1313 | |
| 1314 | const ASTRecordLayout & |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1315 | ASTContext::getASTObjCImplementationLayout( |
| 1316 | const ObjCImplementationDecl *D) const { |
Daniel Dunbar | 02f7f5f | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1317 | return getObjCLayout(D->getClassInterface(), D); |
| 1318 | } |
| 1319 | |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1320 | //===----------------------------------------------------------------------===// |
| 1321 | // Type creation/memoization methods |
| 1322 | //===----------------------------------------------------------------------===// |
| 1323 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1324 | QualType |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1325 | ASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const { |
| 1326 | unsigned fastQuals = quals.getFastQualifiers(); |
| 1327 | quals.removeFastQualifiers(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1328 | |
| 1329 | // Check if we've already instantiated this type. |
| 1330 | llvm::FoldingSetNodeID ID; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1331 | ExtQuals::Profile(ID, baseType, quals); |
| 1332 | void *insertPos = 0; |
| 1333 | if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, insertPos)) { |
| 1334 | assert(eq->getQualifiers() == quals); |
| 1335 | return QualType(eq, fastQuals); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1338 | // If the base type is not canonical, make the appropriate canonical type. |
| 1339 | QualType canon; |
| 1340 | if (!baseType->isCanonicalUnqualified()) { |
| 1341 | SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split(); |
| 1342 | canonSplit.second.addConsistentQualifiers(quals); |
| 1343 | canon = getExtQualType(canonSplit.first, canonSplit.second); |
| 1344 | |
| 1345 | // Re-find the insert position. |
| 1346 | (void) ExtQualNodes.FindNodeOrInsertPos(ID, insertPos); |
| 1347 | } |
| 1348 | |
| 1349 | ExtQuals *eq = new (*this, TypeAlignment) ExtQuals(baseType, canon, quals); |
| 1350 | ExtQualNodes.InsertNode(eq, insertPos); |
| 1351 | return QualType(eq, fastQuals); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1354 | QualType |
| 1355 | ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) const { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1356 | QualType CanT = getCanonicalType(T); |
| 1357 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | 445fcab | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1358 | return T; |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1359 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1360 | // If we are composing extended qualifiers together, merge together |
| 1361 | // into one ExtQuals node. |
| 1362 | QualifierCollector Quals; |
| 1363 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1364 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1365 | // If this type already has an address space specified, it cannot get |
| 1366 | // another one. |
| 1367 | assert(!Quals.hasAddressSpace() && |
| 1368 | "Type cannot be in multiple addr spaces!"); |
| 1369 | Quals.addAddressSpace(AddressSpace); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1370 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1371 | return getExtQualType(TypeNode, Quals); |
Christopher Lamb | 025b5fb | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1374 | QualType ASTContext::getObjCGCQualType(QualType T, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1375 | Qualifiers::GC GCAttr) const { |
Fariborz Jahanian | e27e934 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1376 | QualType CanT = getCanonicalType(T); |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1377 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | e27e934 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1378 | return T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1379 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1380 | if (const PointerType *ptr = T->getAs<PointerType>()) { |
| 1381 | QualType Pointee = ptr->getPointeeType(); |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 1382 | if (Pointee->isAnyPointerType()) { |
Fariborz Jahanian | b68215c | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1383 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 1384 | return getPointerType(ResultType); |
| 1385 | } |
| 1386 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1387 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1388 | // If we are composing extended qualifiers together, merge together |
| 1389 | // into one ExtQuals node. |
| 1390 | QualifierCollector Quals; |
| 1391 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1392 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1393 | // If this type already has an ObjCGC specified, it cannot get |
| 1394 | // another one. |
| 1395 | assert(!Quals.hasObjCGCAttr() && |
| 1396 | "Type cannot have multiple ObjCGCs!"); |
| 1397 | Quals.addObjCGCAttr(GCAttr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1398 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1399 | return getExtQualType(TypeNode, Quals); |
Fariborz Jahanian | e27e934 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1400 | } |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1401 | |
John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 1402 | const FunctionType *ASTContext::adjustFunctionType(const FunctionType *T, |
| 1403 | FunctionType::ExtInfo Info) { |
| 1404 | if (T->getExtInfo() == Info) |
| 1405 | return T; |
| 1406 | |
| 1407 | QualType Result; |
| 1408 | if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) { |
| 1409 | Result = getFunctionNoProtoType(FNPT->getResultType(), Info); |
| 1410 | } else { |
| 1411 | const FunctionProtoType *FPT = cast<FunctionProtoType>(T); |
| 1412 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 1413 | EPI.ExtInfo = Info; |
| 1414 | Result = getFunctionType(FPT->getResultType(), FPT->arg_type_begin(), |
| 1415 | FPT->getNumArgs(), EPI); |
| 1416 | } |
| 1417 | |
| 1418 | return cast<FunctionType>(Result.getTypePtr()); |
| 1419 | } |
| 1420 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1421 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 1422 | /// number with the specified element type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1423 | QualType ASTContext::getComplexType(QualType T) const { |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1424 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1425 | // structure. |
| 1426 | llvm::FoldingSetNodeID ID; |
| 1427 | ComplexType::Profile(ID, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1428 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1429 | void *InsertPos = 0; |
| 1430 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1431 | return QualType(CT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1432 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1433 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1434 | // so fill in the canonical type field. |
| 1435 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1436 | if (!T.isCanonical()) { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1437 | Canonical = getComplexType(getCanonicalType(T)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1438 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1439 | // Get the new insert position for the node we care about. |
| 1440 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1441 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1442 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1443 | ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical); |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1444 | Types.push_back(New); |
| 1445 | ComplexTypes.InsertNode(New, InsertPos); |
| 1446 | return QualType(New, 0); |
| 1447 | } |
| 1448 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1449 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 1450 | /// the specified type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1451 | QualType ASTContext::getPointerType(QualType T) const { |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 1452 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1453 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 1454 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 1455 | PointerType::Profile(ID, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1456 | |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 1457 | void *InsertPos = 0; |
| 1458 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1459 | return QualType(PT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1460 | |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 1461 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1462 | // so fill in the canonical type field. |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1463 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1464 | if (!T.isCanonical()) { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1465 | Canonical = getPointerType(getCanonicalType(T)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1466 | |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 1467 | // Get the new insert position for the node we care about. |
| 1468 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1469 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 1470 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1471 | PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical); |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 1472 | Types.push_back(New); |
| 1473 | PointerTypes.InsertNode(New, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1474 | return QualType(New, 0); |
Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 1475 | } |
| 1476 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1477 | /// getBlockPointerType - Return the uniqued reference to the type for |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1478 | /// a pointer to the specified block. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1479 | QualType ASTContext::getBlockPointerType(QualType T) const { |
Steve Naroff | 0ac01283 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1480 | assert(T->isFunctionType() && "block of function types only"); |
| 1481 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1482 | // structure. |
| 1483 | llvm::FoldingSetNodeID ID; |
| 1484 | BlockPointerType::Profile(ID, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1485 | |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1486 | void *InsertPos = 0; |
| 1487 | if (BlockPointerType *PT = |
| 1488 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1489 | return QualType(PT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1490 | |
| 1491 | // If the block pointee type isn't canonical, this won't be a canonical |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1492 | // type either so fill in the canonical type field. |
| 1493 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1494 | if (!T.isCanonical()) { |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1495 | Canonical = getBlockPointerType(getCanonicalType(T)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1496 | |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1497 | // Get the new insert position for the node we care about. |
| 1498 | BlockPointerType *NewIP = |
| 1499 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1500 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1501 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1502 | BlockPointerType *New |
| 1503 | = new (*this, TypeAlignment) BlockPointerType(T, Canonical); |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1504 | Types.push_back(New); |
| 1505 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 1506 | return QualType(New, 0); |
| 1507 | } |
| 1508 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1509 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 1510 | /// lvalue reference to the specified type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1511 | QualType |
| 1512 | ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const { |
Douglas Gregor | 291e8ee | 2011-05-21 22:16:50 +0000 | [diff] [blame] | 1513 | assert(getCanonicalType(T) != OverloadTy && |
| 1514 | "Unresolved overloaded function type"); |
| 1515 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1516 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1517 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 1518 | llvm::FoldingSetNodeID ID; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1519 | ReferenceType::Profile(ID, T, SpelledAsLValue); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1520 | |
| 1521 | void *InsertPos = 0; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1522 | if (LValueReferenceType *RT = |
| 1523 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1524 | return QualType(RT, 0); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1525 | |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1526 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1527 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1528 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1529 | // either, so fill in the canonical type field. |
| 1530 | QualType Canonical; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1531 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
| 1532 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1533 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1534 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1535 | // Get the new insert position for the node we care about. |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1536 | LValueReferenceType *NewIP = |
| 1537 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1538 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1541 | LValueReferenceType *New |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1542 | = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, |
| 1543 | SpelledAsLValue); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1544 | Types.push_back(New); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1545 | LValueReferenceTypes.InsertNode(New, InsertPos); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1546 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1547 | return QualType(New, 0); |
| 1548 | } |
| 1549 | |
| 1550 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 1551 | /// rvalue reference to the specified type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1552 | QualType ASTContext::getRValueReferenceType(QualType T) const { |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1553 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1554 | // structure. |
| 1555 | llvm::FoldingSetNodeID ID; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1556 | ReferenceType::Profile(ID, T, false); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1557 | |
| 1558 | void *InsertPos = 0; |
| 1559 | if (RValueReferenceType *RT = |
| 1560 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1561 | return QualType(RT, 0); |
| 1562 | |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1563 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1564 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1565 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1566 | // either, so fill in the canonical type field. |
| 1567 | QualType Canonical; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1568 | if (InnerRef || !T.isCanonical()) { |
| 1569 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1570 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1571 | |
| 1572 | // Get the new insert position for the node we care about. |
| 1573 | RValueReferenceType *NewIP = |
| 1574 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1575 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1576 | } |
| 1577 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1578 | RValueReferenceType *New |
| 1579 | = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1580 | Types.push_back(New); |
| 1581 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1582 | return QualType(New, 0); |
| 1583 | } |
| 1584 | |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1585 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 1586 | /// member pointer to the specified type, in the specified class. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1587 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const { |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1588 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1589 | // structure. |
| 1590 | llvm::FoldingSetNodeID ID; |
| 1591 | MemberPointerType::Profile(ID, T, Cls); |
| 1592 | |
| 1593 | void *InsertPos = 0; |
| 1594 | if (MemberPointerType *PT = |
| 1595 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1596 | return QualType(PT, 0); |
| 1597 | |
| 1598 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 1599 | // type either, so fill in the canonical type field. |
| 1600 | QualType Canonical; |
Douglas Gregor | 615ac67 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1601 | if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) { |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1602 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 1603 | |
| 1604 | // Get the new insert position for the node we care about. |
| 1605 | MemberPointerType *NewIP = |
| 1606 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1607 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1608 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1609 | MemberPointerType *New |
| 1610 | = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1611 | Types.push_back(New); |
| 1612 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 1613 | return QualType(New, 0); |
| 1614 | } |
| 1615 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1616 | /// getConstantArrayType - Return the unique reference to the type for an |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1617 | /// array of the specified element type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1618 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | e2df3f9 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1619 | const llvm::APInt &ArySizeIn, |
Steve Naroff | 90dfdd5 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1620 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1621 | unsigned IndexTypeQuals) const { |
Sebastian Redl | 2dfdb82 | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1622 | assert((EltTy->isDependentType() || |
| 1623 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && |
Eli Friedman | be7e42b | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1624 | "Constant array of VLAs is illegal!"); |
| 1625 | |
Chris Lattner | e2df3f9 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1626 | // Convert the array size into a canonical width matching the pointer size for |
| 1627 | // the target. |
| 1628 | llvm::APInt ArySize(ArySizeIn); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1629 | ArySize = |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1630 | ArySize.zextOrTrunc(Target->getPointerWidth(getTargetAddressSpace(EltTy))); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1631 | |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 1632 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1633 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, IndexTypeQuals); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1634 | |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 1635 | void *InsertPos = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1636 | if (ConstantArrayType *ATP = |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1637 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1638 | return QualType(ATP, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1639 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1640 | // If the element type isn't canonical or has qualifiers, this won't |
| 1641 | // be a canonical type either, so fill in the canonical type field. |
| 1642 | QualType Canon; |
| 1643 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 1644 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
| 1645 | Canon = getConstantArrayType(QualType(canonSplit.first, 0), ArySize, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1646 | ASM, IndexTypeQuals); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1647 | Canon = getQualifiedType(Canon, canonSplit.second); |
| 1648 | |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 1649 | // Get the new insert position for the node we care about. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1650 | ConstantArrayType *NewIP = |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1651 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1652 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 1653 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1654 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1655 | ConstantArrayType *New = new(*this,TypeAlignment) |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1656 | ConstantArrayType(EltTy, Canon, ArySize, ASM, IndexTypeQuals); |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1657 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 1658 | Types.push_back(New); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1659 | return QualType(New, 0); |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1662 | /// getVariableArrayDecayedType - Turns the given type, which may be |
| 1663 | /// variably-modified, into the corresponding type with all the known |
| 1664 | /// sizes replaced with [*]. |
| 1665 | QualType ASTContext::getVariableArrayDecayedType(QualType type) const { |
| 1666 | // Vastly most common case. |
| 1667 | if (!type->isVariablyModifiedType()) return type; |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1668 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1669 | QualType result; |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1670 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1671 | SplitQualType split = type.getSplitDesugaredType(); |
| 1672 | const Type *ty = split.first; |
| 1673 | switch (ty->getTypeClass()) { |
| 1674 | #define TYPE(Class, Base) |
| 1675 | #define ABSTRACT_TYPE(Class, Base) |
| 1676 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 1677 | #include "clang/AST/TypeNodes.def" |
| 1678 | llvm_unreachable("didn't desugar past all non-canonical types?"); |
| 1679 | |
| 1680 | // These types should never be variably-modified. |
| 1681 | case Type::Builtin: |
| 1682 | case Type::Complex: |
| 1683 | case Type::Vector: |
| 1684 | case Type::ExtVector: |
| 1685 | case Type::DependentSizedExtVector: |
| 1686 | case Type::ObjCObject: |
| 1687 | case Type::ObjCInterface: |
| 1688 | case Type::ObjCObjectPointer: |
| 1689 | case Type::Record: |
| 1690 | case Type::Enum: |
| 1691 | case Type::UnresolvedUsing: |
| 1692 | case Type::TypeOfExpr: |
| 1693 | case Type::TypeOf: |
| 1694 | case Type::Decltype: |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1695 | case Type::UnaryTransform: |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1696 | case Type::DependentName: |
| 1697 | case Type::InjectedClassName: |
| 1698 | case Type::TemplateSpecialization: |
| 1699 | case Type::DependentTemplateSpecialization: |
| 1700 | case Type::TemplateTypeParm: |
| 1701 | case Type::SubstTemplateTypeParmPack: |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1702 | case Type::Auto: |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1703 | case Type::PackExpansion: |
| 1704 | llvm_unreachable("type should never be variably-modified"); |
| 1705 | |
| 1706 | // These types can be variably-modified but should never need to |
| 1707 | // further decay. |
| 1708 | case Type::FunctionNoProto: |
| 1709 | case Type::FunctionProto: |
| 1710 | case Type::BlockPointer: |
| 1711 | case Type::MemberPointer: |
| 1712 | return type; |
| 1713 | |
| 1714 | // These types can be variably-modified. All these modifications |
| 1715 | // preserve structure except as noted by comments. |
| 1716 | // TODO: if we ever care about optimizing VLAs, there are no-op |
| 1717 | // optimizations available here. |
| 1718 | case Type::Pointer: |
| 1719 | result = getPointerType(getVariableArrayDecayedType( |
| 1720 | cast<PointerType>(ty)->getPointeeType())); |
| 1721 | break; |
| 1722 | |
| 1723 | case Type::LValueReference: { |
| 1724 | const LValueReferenceType *lv = cast<LValueReferenceType>(ty); |
| 1725 | result = getLValueReferenceType( |
| 1726 | getVariableArrayDecayedType(lv->getPointeeType()), |
| 1727 | lv->isSpelledAsLValue()); |
| 1728 | break; |
| 1729 | } |
| 1730 | |
| 1731 | case Type::RValueReference: { |
| 1732 | const RValueReferenceType *lv = cast<RValueReferenceType>(ty); |
| 1733 | result = getRValueReferenceType( |
| 1734 | getVariableArrayDecayedType(lv->getPointeeType())); |
| 1735 | break; |
| 1736 | } |
| 1737 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1738 | case Type::Atomic: { |
| 1739 | const AtomicType *at = cast<AtomicType>(ty); |
| 1740 | result = getAtomicType(getVariableArrayDecayedType(at->getValueType())); |
| 1741 | break; |
| 1742 | } |
| 1743 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1744 | case Type::ConstantArray: { |
| 1745 | const ConstantArrayType *cat = cast<ConstantArrayType>(ty); |
| 1746 | result = getConstantArrayType( |
| 1747 | getVariableArrayDecayedType(cat->getElementType()), |
| 1748 | cat->getSize(), |
| 1749 | cat->getSizeModifier(), |
| 1750 | cat->getIndexTypeCVRQualifiers()); |
| 1751 | break; |
| 1752 | } |
| 1753 | |
| 1754 | case Type::DependentSizedArray: { |
| 1755 | const DependentSizedArrayType *dat = cast<DependentSizedArrayType>(ty); |
| 1756 | result = getDependentSizedArrayType( |
| 1757 | getVariableArrayDecayedType(dat->getElementType()), |
| 1758 | dat->getSizeExpr(), |
| 1759 | dat->getSizeModifier(), |
| 1760 | dat->getIndexTypeCVRQualifiers(), |
| 1761 | dat->getBracketsRange()); |
| 1762 | break; |
| 1763 | } |
| 1764 | |
| 1765 | // Turn incomplete types into [*] types. |
| 1766 | case Type::IncompleteArray: { |
| 1767 | const IncompleteArrayType *iat = cast<IncompleteArrayType>(ty); |
| 1768 | result = getVariableArrayType( |
| 1769 | getVariableArrayDecayedType(iat->getElementType()), |
| 1770 | /*size*/ 0, |
| 1771 | ArrayType::Normal, |
| 1772 | iat->getIndexTypeCVRQualifiers(), |
| 1773 | SourceRange()); |
| 1774 | break; |
| 1775 | } |
| 1776 | |
| 1777 | // Turn VLA types into [*] types. |
| 1778 | case Type::VariableArray: { |
| 1779 | const VariableArrayType *vat = cast<VariableArrayType>(ty); |
| 1780 | result = getVariableArrayType( |
| 1781 | getVariableArrayDecayedType(vat->getElementType()), |
| 1782 | /*size*/ 0, |
| 1783 | ArrayType::Star, |
| 1784 | vat->getIndexTypeCVRQualifiers(), |
| 1785 | vat->getBracketsRange()); |
| 1786 | break; |
| 1787 | } |
| 1788 | } |
| 1789 | |
| 1790 | // Apply the top-level qualifiers from the original. |
| 1791 | return getQualifiedType(result, split.second); |
| 1792 | } |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1793 | |
Steve Naroff | cadebd0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 1794 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 1795 | /// variable array of the specified element type. |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1796 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 1797 | Expr *NumElts, |
Steve Naroff | 90dfdd5 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1798 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1799 | unsigned IndexTypeQuals, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1800 | SourceRange Brackets) const { |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1801 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 1802 | // that have an expression provided for their size. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1803 | QualType Canon; |
Douglas Gregor | 5e8c8c0 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 1804 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1805 | // Be sure to pull qualifiers off the element type. |
| 1806 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 1807 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
| 1808 | Canon = getVariableArrayType(QualType(canonSplit.first, 0), NumElts, ASM, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1809 | IndexTypeQuals, Brackets); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1810 | Canon = getQualifiedType(Canon, canonSplit.second); |
Douglas Gregor | 5e8c8c0 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 1811 | } |
| 1812 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1813 | VariableArrayType *New = new(*this, TypeAlignment) |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1814 | VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1815 | |
| 1816 | VariableArrayTypes.push_back(New); |
| 1817 | Types.push_back(New); |
| 1818 | return QualType(New, 0); |
| 1819 | } |
| 1820 | |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1821 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 1822 | /// the type for a dependently-sized array of the specified element |
Douglas Gregor | f3f9552 | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1823 | /// type. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1824 | QualType ASTContext::getDependentSizedArrayType(QualType elementType, |
| 1825 | Expr *numElements, |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1826 | ArrayType::ArraySizeModifier ASM, |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1827 | unsigned elementTypeQuals, |
| 1828 | SourceRange brackets) const { |
| 1829 | assert((!numElements || numElements->isTypeDependent() || |
| 1830 | numElements->isValueDependent()) && |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1831 | "Size must be type- or value-dependent!"); |
| 1832 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1833 | // Dependently-sized array types that do not have a specified number |
| 1834 | // of elements will have their sizes deduced from a dependent |
| 1835 | // initializer. We do no canonicalization here at all, which is okay |
| 1836 | // because they can't be used in most locations. |
| 1837 | if (!numElements) { |
| 1838 | DependentSizedArrayType *newType |
| 1839 | = new (*this, TypeAlignment) |
| 1840 | DependentSizedArrayType(*this, elementType, QualType(), |
| 1841 | numElements, ASM, elementTypeQuals, |
| 1842 | brackets); |
| 1843 | Types.push_back(newType); |
| 1844 | return QualType(newType, 0); |
| 1845 | } |
| 1846 | |
| 1847 | // Otherwise, we actually build a new type every time, but we |
| 1848 | // also build a canonical type. |
| 1849 | |
| 1850 | SplitQualType canonElementType = getCanonicalType(elementType).split(); |
| 1851 | |
| 1852 | void *insertPos = 0; |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1853 | llvm::FoldingSetNodeID ID; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1854 | DependentSizedArrayType::Profile(ID, *this, |
| 1855 | QualType(canonElementType.first, 0), |
| 1856 | ASM, elementTypeQuals, numElements); |
Douglas Gregor | ad2956c | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1857 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1858 | // Look for an existing type with these properties. |
| 1859 | DependentSizedArrayType *canonTy = |
| 1860 | DependentSizedArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
Douglas Gregor | ad2956c | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1861 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1862 | // If we don't have one, build one. |
| 1863 | if (!canonTy) { |
| 1864 | canonTy = new (*this, TypeAlignment) |
| 1865 | DependentSizedArrayType(*this, QualType(canonElementType.first, 0), |
| 1866 | QualType(), numElements, ASM, elementTypeQuals, |
| 1867 | brackets); |
| 1868 | DependentSizedArrayTypes.InsertNode(canonTy, insertPos); |
| 1869 | Types.push_back(canonTy); |
Douglas Gregor | ad2956c | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1870 | } |
| 1871 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1872 | // Apply qualifiers from the element type to the array. |
| 1873 | QualType canon = getQualifiedType(QualType(canonTy,0), |
| 1874 | canonElementType.second); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1875 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1876 | // If we didn't need extra canonicalization for the element type, |
| 1877 | // then just use that as our result. |
| 1878 | if (QualType(canonElementType.first, 0) == elementType) |
| 1879 | return canon; |
| 1880 | |
| 1881 | // Otherwise, we need to build a type which follows the spelling |
| 1882 | // of the element type. |
| 1883 | DependentSizedArrayType *sugaredType |
| 1884 | = new (*this, TypeAlignment) |
| 1885 | DependentSizedArrayType(*this, elementType, canon, numElements, |
| 1886 | ASM, elementTypeQuals, brackets); |
| 1887 | Types.push_back(sugaredType); |
| 1888 | return QualType(sugaredType, 0); |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1889 | } |
| 1890 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1891 | QualType ASTContext::getIncompleteArrayType(QualType elementType, |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1892 | ArrayType::ArraySizeModifier ASM, |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1893 | unsigned elementTypeQuals) const { |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1894 | llvm::FoldingSetNodeID ID; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1895 | IncompleteArrayType::Profile(ID, elementType, ASM, elementTypeQuals); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1896 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1897 | void *insertPos = 0; |
| 1898 | if (IncompleteArrayType *iat = |
| 1899 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos)) |
| 1900 | return QualType(iat, 0); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1901 | |
| 1902 | // If the element type isn't canonical, this won't be a canonical type |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1903 | // either, so fill in the canonical type field. We also have to pull |
| 1904 | // qualifiers off the element type. |
| 1905 | QualType canon; |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1906 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1907 | if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) { |
| 1908 | SplitQualType canonSplit = getCanonicalType(elementType).split(); |
| 1909 | canon = getIncompleteArrayType(QualType(canonSplit.first, 0), |
| 1910 | ASM, elementTypeQuals); |
| 1911 | canon = getQualifiedType(canon, canonSplit.second); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1912 | |
| 1913 | // Get the new insert position for the node we care about. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1914 | IncompleteArrayType *existing = |
| 1915 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
| 1916 | assert(!existing && "Shouldn't be in the map!"); (void) existing; |
Ted Kremenek | 843ebedd | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1917 | } |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1918 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1919 | IncompleteArrayType *newType = new (*this, TypeAlignment) |
| 1920 | IncompleteArrayType(elementType, canon, ASM, elementTypeQuals); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1921 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1922 | IncompleteArrayTypes.InsertNode(newType, insertPos); |
| 1923 | Types.push_back(newType); |
| 1924 | return QualType(newType, 0); |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1925 | } |
| 1926 | |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1927 | /// getVectorType - Return the unique reference to a vector type of |
| 1928 | /// the specified element type and size. VectorType must be a built-in type. |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1929 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1930 | VectorType::VectorKind VecKind) const { |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1931 | assert(vecType->isBuiltinType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1932 | |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 1933 | // Check if we've already instantiated a vector of this type. |
| 1934 | llvm::FoldingSetNodeID ID; |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1935 | VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind); |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1936 | |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 1937 | void *InsertPos = 0; |
| 1938 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1939 | return QualType(VTP, 0); |
| 1940 | |
| 1941 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1942 | // so fill in the canonical type field. |
| 1943 | QualType Canonical; |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1944 | if (!vecType.isCanonical()) { |
Bob Wilson | 7795480 | 2010-11-16 00:32:20 +0000 | [diff] [blame] | 1945 | Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1946 | |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 1947 | // Get the new insert position for the node we care about. |
| 1948 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1949 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 1950 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1951 | VectorType *New = new (*this, TypeAlignment) |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1952 | VectorType(vecType, NumElts, Canonical, VecKind); |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 1953 | VectorTypes.InsertNode(New, InsertPos); |
| 1954 | Types.push_back(New); |
| 1955 | return QualType(New, 0); |
| 1956 | } |
| 1957 | |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1958 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1959 | /// the specified element type and size. VectorType must be a built-in type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1960 | QualType |
| 1961 | ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) const { |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1962 | assert(vecType->isBuiltinType() || vecType->isDependentType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1963 | |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1964 | // Check if we've already instantiated a vector of this type. |
| 1965 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1966 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1967 | VectorType::GenericVector); |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1968 | void *InsertPos = 0; |
| 1969 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1970 | return QualType(VTP, 0); |
| 1971 | |
| 1972 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1973 | // so fill in the canonical type field. |
| 1974 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1975 | if (!vecType.isCanonical()) { |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1976 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1977 | |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1978 | // Get the new insert position for the node we care about. |
| 1979 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1980 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1981 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1982 | ExtVectorType *New = new (*this, TypeAlignment) |
| 1983 | ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1984 | VectorTypes.InsertNode(New, InsertPos); |
| 1985 | Types.push_back(New); |
| 1986 | return QualType(New, 0); |
| 1987 | } |
| 1988 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1989 | QualType |
| 1990 | ASTContext::getDependentSizedExtVectorType(QualType vecType, |
| 1991 | Expr *SizeExpr, |
| 1992 | SourceLocation AttrLoc) const { |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1993 | llvm::FoldingSetNodeID ID; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1994 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1995 | SizeExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1996 | |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1997 | void *InsertPos = 0; |
| 1998 | DependentSizedExtVectorType *Canon |
| 1999 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2000 | DependentSizedExtVectorType *New; |
| 2001 | if (Canon) { |
| 2002 | // We already have a canonical version of this array type; use it as |
| 2003 | // the canonical type for a newly-built type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2004 | New = new (*this, TypeAlignment) |
| 2005 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), |
| 2006 | SizeExpr, AttrLoc); |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2007 | } else { |
| 2008 | QualType CanonVecTy = getCanonicalType(vecType); |
| 2009 | if (CanonVecTy == vecType) { |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2010 | New = new (*this, TypeAlignment) |
| 2011 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, |
| 2012 | AttrLoc); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2013 | |
| 2014 | DependentSizedExtVectorType *CanonCheck |
| 2015 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2016 | assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken"); |
| 2017 | (void)CanonCheck; |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2018 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); |
| 2019 | } else { |
| 2020 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 2021 | SourceLocation()); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2022 | New = new (*this, TypeAlignment) |
| 2023 | DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2024 | } |
| 2025 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2026 | |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 2027 | Types.push_back(New); |
| 2028 | return QualType(New, 0); |
| 2029 | } |
| 2030 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2031 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2032 | /// |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2033 | QualType |
| 2034 | ASTContext::getFunctionNoProtoType(QualType ResultTy, |
| 2035 | const FunctionType::ExtInfo &Info) const { |
Roman Divacky | 65b88cd | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2036 | const CallingConv DefaultCC = Info.getCC(); |
| 2037 | const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ? |
| 2038 | CC_X86StdCall : DefaultCC; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2039 | // Unique functions, to guarantee there is only one function of a particular |
| 2040 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 2041 | llvm::FoldingSetNodeID ID; |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2042 | FunctionNoProtoType::Profile(ID, ResultTy, Info); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2043 | |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 2044 | void *InsertPos = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2045 | if (FunctionNoProtoType *FT = |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2046 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2047 | return QualType(FT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2048 | |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2049 | QualType Canonical; |
Douglas Gregor | 8c94086 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 2050 | if (!ResultTy.isCanonical() || |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2051 | getCanonicalCallConv(CallConv) != CallConv) { |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2052 | Canonical = |
| 2053 | getFunctionNoProtoType(getCanonicalType(ResultTy), |
| 2054 | Info.withCallingConv(getCanonicalCallConv(CallConv))); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2055 | |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 2056 | // Get the new insert position for the node we care about. |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2057 | FunctionNoProtoType *NewIP = |
| 2058 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2059 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 2060 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2061 | |
Roman Divacky | 65b88cd | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2062 | FunctionProtoType::ExtInfo newInfo = Info.withCallingConv(CallConv); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2063 | FunctionNoProtoType *New = new (*this, TypeAlignment) |
Roman Divacky | 65b88cd | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2064 | FunctionNoProtoType(ResultTy, Canonical, newInfo); |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 2065 | Types.push_back(New); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2066 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2067 | return QualType(New, 0); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2068 | } |
| 2069 | |
| 2070 | /// getFunctionType - Return a normal function type with a typed argument |
| 2071 | /// list. isVariadic indicates whether the argument list includes '...'. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2072 | QualType |
| 2073 | ASTContext::getFunctionType(QualType ResultTy, |
| 2074 | const QualType *ArgArray, unsigned NumArgs, |
| 2075 | const FunctionProtoType::ExtProtoInfo &EPI) const { |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2076 | // Unique functions, to guarantee there is only one function of a particular |
| 2077 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 2078 | llvm::FoldingSetNodeID ID; |
Sebastian Redl | 31ad754 | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2079 | FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, EPI, *this); |
Chris Lattner | fd4de79 | 2007-01-27 01:15:32 +0000 | [diff] [blame] | 2080 | |
| 2081 | void *InsertPos = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2082 | if (FunctionProtoType *FTP = |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2083 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2084 | return QualType(FTP, 0); |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2085 | |
| 2086 | // Determine whether the type being created is already canonical or not. |
Sebastian Redl | 7c6c9e9 | 2011-03-06 10:52:04 +0000 | [diff] [blame] | 2087 | bool isCanonical= EPI.ExceptionSpecType == EST_None && ResultTy.isCanonical(); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2088 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2089 | if (!ArgArray[i].isCanonicalAsParam()) |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2090 | isCanonical = false; |
| 2091 | |
Roman Divacky | 65b88cd | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2092 | const CallingConv DefaultCC = EPI.ExtInfo.getCC(); |
| 2093 | const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ? |
| 2094 | CC_X86StdCall : DefaultCC; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2095 | |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2096 | // If this type isn't canonical, get the canonical version of it. |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2097 | // The exception spec is not part of the canonical type. |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2098 | QualType Canonical; |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2099 | if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2100 | SmallVector<QualType, 16> CanonicalArgs; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2101 | CanonicalArgs.reserve(NumArgs); |
| 2102 | for (unsigned i = 0; i != NumArgs; ++i) |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2103 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2104 | |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2105 | FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI; |
Sebastian Redl | 7c6c9e9 | 2011-03-06 10:52:04 +0000 | [diff] [blame] | 2106 | CanonicalEPI.ExceptionSpecType = EST_None; |
| 2107 | CanonicalEPI.NumExceptions = 0; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2108 | CanonicalEPI.ExtInfo |
| 2109 | = CanonicalEPI.ExtInfo.withCallingConv(getCanonicalCallConv(CallConv)); |
| 2110 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2111 | Canonical = getFunctionType(getCanonicalType(ResultTy), |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2112 | CanonicalArgs.data(), NumArgs, |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2113 | CanonicalEPI); |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2114 | |
Chris Lattner | fd4de79 | 2007-01-27 01:15:32 +0000 | [diff] [blame] | 2115 | // Get the new insert position for the node we care about. |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2116 | FunctionProtoType *NewIP = |
| 2117 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2118 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2119 | } |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2120 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2121 | // FunctionProtoType objects are allocated with extra bytes after |
| 2122 | // them for three variable size arrays at the end: |
| 2123 | // - parameter types |
| 2124 | // - exception types |
| 2125 | // - consumed-arguments flags |
| 2126 | // Instead of the exception types, there could be a noexcept |
| 2127 | // expression. |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2128 | size_t Size = sizeof(FunctionProtoType) + |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2129 | NumArgs * sizeof(QualType); |
| 2130 | if (EPI.ExceptionSpecType == EST_Dynamic) |
| 2131 | Size += EPI.NumExceptions * sizeof(QualType); |
| 2132 | else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) { |
Sebastian Redl | 31ad754 | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2133 | Size += sizeof(Expr*); |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2134 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2135 | if (EPI.ConsumedArguments) |
| 2136 | Size += NumArgs * sizeof(bool); |
| 2137 | |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2138 | FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment); |
Roman Divacky | 65b88cd | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2139 | FunctionProtoType::ExtProtoInfo newEPI = EPI; |
| 2140 | newEPI.ExtInfo = EPI.ExtInfo.withCallingConv(CallConv); |
Sebastian Redl | 31ad754 | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2141 | new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, Canonical, newEPI); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2142 | Types.push_back(FTP); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2143 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2144 | return QualType(FTP, 0); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2145 | } |
Chris Lattner | ef51c20 | 2006-11-10 07:17:23 +0000 | [diff] [blame] | 2146 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2147 | #ifndef NDEBUG |
| 2148 | static bool NeedsInjectedClassNameType(const RecordDecl *D) { |
| 2149 | if (!isa<CXXRecordDecl>(D)) return false; |
| 2150 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 2151 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) |
| 2152 | return true; |
| 2153 | if (RD->getDescribedClassTemplate() && |
| 2154 | !isa<ClassTemplateSpecializationDecl>(RD)) |
| 2155 | return true; |
| 2156 | return false; |
| 2157 | } |
| 2158 | #endif |
| 2159 | |
| 2160 | /// getInjectedClassNameType - Return the unique reference to the |
| 2161 | /// injected class name type for the specified templated declaration. |
| 2162 | QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2163 | QualType TST) const { |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2164 | assert(NeedsInjectedClassNameType(Decl)); |
| 2165 | if (Decl->TypeForDecl) { |
| 2166 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
Argyrios Kyrtzidis | 2c2167a | 2010-07-02 11:55:32 +0000 | [diff] [blame] | 2167 | } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDeclaration()) { |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2168 | assert(PrevDecl->TypeForDecl && "previous declaration has no type"); |
| 2169 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 2170 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
| 2171 | } else { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2172 | Type *newType = |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2173 | new (*this, TypeAlignment) InjectedClassNameType(Decl, TST); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2174 | Decl->TypeForDecl = newType; |
| 2175 | Types.push_back(newType); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2176 | } |
| 2177 | return QualType(Decl->TypeForDecl, 0); |
| 2178 | } |
| 2179 | |
Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2180 | /// getTypeDeclType - Return the unique reference to the type for the |
| 2181 | /// specified type declaration. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2182 | QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const { |
Argyrios Kyrtzidis | 89656d2 | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 2183 | assert(Decl && "Passed null for Decl param"); |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2184 | assert(!Decl->TypeForDecl && "TypeForDecl present in slow case"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2185 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2186 | if (const TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Decl)) |
Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2187 | return getTypedefType(Typedef); |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2188 | |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2189 | assert(!isa<TemplateTypeParmDecl>(Decl) && |
| 2190 | "Template type parameter types are always available."); |
| 2191 | |
John McCall | 81e3850 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2192 | if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2193 | assert(!Record->getPreviousDeclaration() && |
| 2194 | "struct/union has previous declaration"); |
| 2195 | assert(!NeedsInjectedClassNameType(Record)); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2196 | return getRecordType(Record); |
John McCall | 81e3850 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2197 | } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2198 | assert(!Enum->getPreviousDeclaration() && |
| 2199 | "enum has previous declaration"); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2200 | return getEnumType(Enum); |
John McCall | 81e3850 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2201 | } else if (const UnresolvedUsingTypenameDecl *Using = |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2202 | dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2203 | Type *newType = new (*this, TypeAlignment) UnresolvedUsingType(Using); |
| 2204 | Decl->TypeForDecl = newType; |
| 2205 | Types.push_back(newType); |
Mike Stump | e9c6ffc | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 2206 | } else |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2207 | llvm_unreachable("TypeDecl without a type?"); |
Argyrios Kyrtzidis | faf0876 | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 2208 | |
Argyrios Kyrtzidis | faf0876 | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 2209 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2210 | } |
| 2211 | |
Chris Lattner | 32d920b | 2007-01-26 02:01:53 +0000 | [diff] [blame] | 2212 | /// getTypedefType - Return the unique reference to the type for the |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2213 | /// specified typedef name decl. |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2214 | QualType |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2215 | ASTContext::getTypedefType(const TypedefNameDecl *Decl, |
| 2216 | QualType Canonical) const { |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2217 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2218 | |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2219 | if (Canonical.isNull()) |
| 2220 | Canonical = getCanonicalType(Decl->getUnderlyingType()); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2221 | TypedefType *newType = new(*this, TypeAlignment) |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2222 | TypedefType(Type::Typedef, Decl, Canonical); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2223 | Decl->TypeForDecl = newType; |
| 2224 | Types.push_back(newType); |
| 2225 | return QualType(newType, 0); |
Chris Lattner | d0342e5 | 2006-11-20 04:02:15 +0000 | [diff] [blame] | 2226 | } |
| 2227 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2228 | QualType ASTContext::getRecordType(const RecordDecl *Decl) const { |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2229 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 2230 | |
| 2231 | if (const RecordDecl *PrevDecl = Decl->getPreviousDeclaration()) |
| 2232 | if (PrevDecl->TypeForDecl) |
| 2233 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 2234 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2235 | RecordType *newType = new (*this, TypeAlignment) RecordType(Decl); |
| 2236 | Decl->TypeForDecl = newType; |
| 2237 | Types.push_back(newType); |
| 2238 | return QualType(newType, 0); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2239 | } |
| 2240 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2241 | QualType ASTContext::getEnumType(const EnumDecl *Decl) const { |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2242 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 2243 | |
| 2244 | if (const EnumDecl *PrevDecl = Decl->getPreviousDeclaration()) |
| 2245 | if (PrevDecl->TypeForDecl) |
| 2246 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 2247 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2248 | EnumType *newType = new (*this, TypeAlignment) EnumType(Decl); |
| 2249 | Decl->TypeForDecl = newType; |
| 2250 | Types.push_back(newType); |
| 2251 | return QualType(newType, 0); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2252 | } |
| 2253 | |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 2254 | QualType ASTContext::getAttributedType(AttributedType::Kind attrKind, |
| 2255 | QualType modifiedType, |
| 2256 | QualType equivalentType) { |
| 2257 | llvm::FoldingSetNodeID id; |
| 2258 | AttributedType::Profile(id, attrKind, modifiedType, equivalentType); |
| 2259 | |
| 2260 | void *insertPos = 0; |
| 2261 | AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos); |
| 2262 | if (type) return QualType(type, 0); |
| 2263 | |
| 2264 | QualType canon = getCanonicalType(equivalentType); |
| 2265 | type = new (*this, TypeAlignment) |
| 2266 | AttributedType(canon, attrKind, modifiedType, equivalentType); |
| 2267 | |
| 2268 | Types.push_back(type); |
| 2269 | AttributedTypes.InsertNode(type, insertPos); |
| 2270 | |
| 2271 | return QualType(type, 0); |
| 2272 | } |
| 2273 | |
| 2274 | |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2275 | /// \brief Retrieve a substitution-result type. |
| 2276 | QualType |
| 2277 | ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2278 | QualType Replacement) const { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2279 | assert(Replacement.isCanonical() |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2280 | && "replacement types must always be canonical"); |
| 2281 | |
| 2282 | llvm::FoldingSetNodeID ID; |
| 2283 | SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); |
| 2284 | void *InsertPos = 0; |
| 2285 | SubstTemplateTypeParmType *SubstParm |
| 2286 | = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2287 | |
| 2288 | if (!SubstParm) { |
| 2289 | SubstParm = new (*this, TypeAlignment) |
| 2290 | SubstTemplateTypeParmType(Parm, Replacement); |
| 2291 | Types.push_back(SubstParm); |
| 2292 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 2293 | } |
| 2294 | |
| 2295 | return QualType(SubstParm, 0); |
| 2296 | } |
| 2297 | |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 2298 | /// \brief Retrieve a |
| 2299 | QualType ASTContext::getSubstTemplateTypeParmPackType( |
| 2300 | const TemplateTypeParmType *Parm, |
| 2301 | const TemplateArgument &ArgPack) { |
| 2302 | #ifndef NDEBUG |
| 2303 | for (TemplateArgument::pack_iterator P = ArgPack.pack_begin(), |
| 2304 | PEnd = ArgPack.pack_end(); |
| 2305 | P != PEnd; ++P) { |
| 2306 | assert(P->getKind() == TemplateArgument::Type &&"Pack contains a non-type"); |
| 2307 | assert(P->getAsType().isCanonical() && "Pack contains non-canonical type"); |
| 2308 | } |
| 2309 | #endif |
| 2310 | |
| 2311 | llvm::FoldingSetNodeID ID; |
| 2312 | SubstTemplateTypeParmPackType::Profile(ID, Parm, ArgPack); |
| 2313 | void *InsertPos = 0; |
| 2314 | if (SubstTemplateTypeParmPackType *SubstParm |
| 2315 | = SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2316 | return QualType(SubstParm, 0); |
| 2317 | |
| 2318 | QualType Canon; |
| 2319 | if (!Parm->isCanonicalUnqualified()) { |
| 2320 | Canon = getCanonicalType(QualType(Parm, 0)); |
| 2321 | Canon = getSubstTemplateTypeParmPackType(cast<TemplateTypeParmType>(Canon), |
| 2322 | ArgPack); |
| 2323 | SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2324 | } |
| 2325 | |
| 2326 | SubstTemplateTypeParmPackType *SubstParm |
| 2327 | = new (*this, TypeAlignment) SubstTemplateTypeParmPackType(Parm, Canon, |
| 2328 | ArgPack); |
| 2329 | Types.push_back(SubstParm); |
| 2330 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 2331 | return QualType(SubstParm, 0); |
| 2332 | } |
| 2333 | |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2334 | /// \brief Retrieve the template type parameter type for a template |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2335 | /// parameter or parameter pack with the given depth, index, and (optionally) |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2336 | /// name. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2337 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2338 | bool ParameterPack, |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2339 | TemplateTypeParmDecl *TTPDecl) const { |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2340 | llvm::FoldingSetNodeID ID; |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2341 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, TTPDecl); |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2342 | void *InsertPos = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2343 | TemplateTypeParmType *TypeParm |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2344 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2345 | |
| 2346 | if (TypeParm) |
| 2347 | return QualType(TypeParm, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2348 | |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2349 | if (TTPDecl) { |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2350 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2351 | TypeParm = new (*this, TypeAlignment) TemplateTypeParmType(TTPDecl, Canon); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2352 | |
| 2353 | TemplateTypeParmType *TypeCheck |
| 2354 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2355 | assert(!TypeCheck && "Template type parameter canonical type broken"); |
| 2356 | (void)TypeCheck; |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2357 | } else |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2358 | TypeParm = new (*this, TypeAlignment) |
| 2359 | TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2360 | |
| 2361 | Types.push_back(TypeParm); |
| 2362 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 2363 | |
| 2364 | return QualType(TypeParm, 0); |
| 2365 | } |
| 2366 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2367 | TypeSourceInfo * |
| 2368 | ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name, |
| 2369 | SourceLocation NameLoc, |
| 2370 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2371 | QualType Underlying) const { |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2372 | assert(!Name.getAsDependentTemplateName() && |
| 2373 | "No dependent template names here!"); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2374 | QualType TST = getTemplateSpecializationType(Name, Args, Underlying); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2375 | |
| 2376 | TypeSourceInfo *DI = CreateTypeSourceInfo(TST); |
| 2377 | TemplateSpecializationTypeLoc TL |
| 2378 | = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc()); |
| 2379 | TL.setTemplateNameLoc(NameLoc); |
| 2380 | TL.setLAngleLoc(Args.getLAngleLoc()); |
| 2381 | TL.setRAngleLoc(Args.getRAngleLoc()); |
| 2382 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 2383 | TL.setArgLocInfo(i, Args[i].getLocInfo()); |
| 2384 | return DI; |
| 2385 | } |
| 2386 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2387 | QualType |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2388 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2389 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2390 | QualType Underlying) const { |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2391 | assert(!Template.getAsDependentTemplateName() && |
| 2392 | "No dependent template names here!"); |
| 2393 | |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2394 | unsigned NumArgs = Args.size(); |
| 2395 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2396 | SmallVector<TemplateArgument, 4> ArgVec; |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2397 | ArgVec.reserve(NumArgs); |
| 2398 | for (unsigned i = 0; i != NumArgs; ++i) |
| 2399 | ArgVec.push_back(Args[i].getArgument()); |
| 2400 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2401 | return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2402 | Underlying); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2403 | } |
| 2404 | |
| 2405 | QualType |
| 2406 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2407 | const TemplateArgument *Args, |
| 2408 | unsigned NumArgs, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2409 | QualType Underlying) const { |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2410 | assert(!Template.getAsDependentTemplateName() && |
| 2411 | "No dependent template names here!"); |
Douglas Gregor | e29139c | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 2412 | // Look through qualified template names. |
| 2413 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 2414 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2415 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2416 | bool isTypeAlias = |
| 2417 | Template.getAsTemplateDecl() && |
| 2418 | isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl()); |
| 2419 | |
| 2420 | QualType CanonType; |
| 2421 | if (!Underlying.isNull()) |
| 2422 | CanonType = getCanonicalType(Underlying); |
| 2423 | else { |
| 2424 | assert(!isTypeAlias && |
| 2425 | "Underlying type for template alias must be computed by caller"); |
| 2426 | CanonType = getCanonicalTemplateSpecializationType(Template, Args, |
| 2427 | NumArgs); |
| 2428 | } |
Douglas Gregor | d56a91e | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 2429 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2430 | // Allocate the (non-canonical) template specialization type, but don't |
| 2431 | // try to unique it: these types typically have location information that |
| 2432 | // we don't unique and don't want to lose. |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2433 | void *Mem = Allocate(sizeof(TemplateSpecializationType) + |
| 2434 | sizeof(TemplateArgument) * NumArgs + |
| 2435 | (isTypeAlias ? sizeof(QualType) : 0), |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2436 | TypeAlignment); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2437 | TemplateSpecializationType *Spec |
John McCall | 773cc98 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 2438 | = new (Mem) TemplateSpecializationType(Template, |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2439 | Args, NumArgs, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2440 | CanonType, |
| 2441 | isTypeAlias ? Underlying : QualType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2442 | |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2443 | Types.push_back(Spec); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2444 | return QualType(Spec, 0); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2445 | } |
| 2446 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2447 | QualType |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2448 | ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template, |
| 2449 | const TemplateArgument *Args, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2450 | unsigned NumArgs) const { |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2451 | assert(!Template.getAsDependentTemplateName() && |
| 2452 | "No dependent template names here!"); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2453 | assert((!Template.getAsTemplateDecl() || |
| 2454 | !isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) && |
| 2455 | "Underlying type for template alias must be computed by caller"); |
| 2456 | |
Douglas Gregor | e29139c | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 2457 | // Look through qualified template names. |
| 2458 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 2459 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2460 | |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2461 | // Build the canonical template specialization type. |
| 2462 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2463 | SmallVector<TemplateArgument, 4> CanonArgs; |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2464 | CanonArgs.reserve(NumArgs); |
| 2465 | for (unsigned I = 0; I != NumArgs; ++I) |
| 2466 | CanonArgs.push_back(getCanonicalTemplateArgument(Args[I])); |
| 2467 | |
| 2468 | // Determine whether this canonical template specialization type already |
| 2469 | // exists. |
| 2470 | llvm::FoldingSetNodeID ID; |
| 2471 | TemplateSpecializationType::Profile(ID, CanonTemplate, |
| 2472 | CanonArgs.data(), NumArgs, *this); |
| 2473 | |
| 2474 | void *InsertPos = 0; |
| 2475 | TemplateSpecializationType *Spec |
| 2476 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2477 | |
| 2478 | if (!Spec) { |
| 2479 | // Allocate a new canonical template specialization type. |
| 2480 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
| 2481 | sizeof(TemplateArgument) * NumArgs), |
| 2482 | TypeAlignment); |
| 2483 | Spec = new (Mem) TemplateSpecializationType(CanonTemplate, |
| 2484 | CanonArgs.data(), NumArgs, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2485 | QualType(), QualType()); |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2486 | Types.push_back(Spec); |
| 2487 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
| 2488 | } |
| 2489 | |
| 2490 | assert(Spec->isDependentType() && |
| 2491 | "Non-dependent template-id type must have a canonical type"); |
| 2492 | return QualType(Spec, 0); |
| 2493 | } |
| 2494 | |
| 2495 | QualType |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2496 | ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword, |
| 2497 | NestedNameSpecifier *NNS, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2498 | QualType NamedType) const { |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2499 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2500 | ElaboratedType::Profile(ID, Keyword, NNS, NamedType); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2501 | |
| 2502 | void *InsertPos = 0; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2503 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2504 | if (T) |
| 2505 | return QualType(T, 0); |
| 2506 | |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2507 | QualType Canon = NamedType; |
| 2508 | if (!Canon.isCanonical()) { |
| 2509 | Canon = getCanonicalType(NamedType); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2510 | ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2511 | assert(!CheckT && "Elaborated canonical type broken"); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2512 | (void)CheckT; |
| 2513 | } |
| 2514 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2515 | T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2516 | Types.push_back(T); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2517 | ElaboratedTypes.InsertNode(T, InsertPos); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2518 | return QualType(T, 0); |
| 2519 | } |
| 2520 | |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 2521 | QualType |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2522 | ASTContext::getParenType(QualType InnerType) const { |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 2523 | llvm::FoldingSetNodeID ID; |
| 2524 | ParenType::Profile(ID, InnerType); |
| 2525 | |
| 2526 | void *InsertPos = 0; |
| 2527 | ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2528 | if (T) |
| 2529 | return QualType(T, 0); |
| 2530 | |
| 2531 | QualType Canon = InnerType; |
| 2532 | if (!Canon.isCanonical()) { |
| 2533 | Canon = getCanonicalType(InnerType); |
| 2534 | ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2535 | assert(!CheckT && "Paren canonical type broken"); |
| 2536 | (void)CheckT; |
| 2537 | } |
| 2538 | |
| 2539 | T = new (*this) ParenType(InnerType, Canon); |
| 2540 | Types.push_back(T); |
| 2541 | ParenTypes.InsertNode(T, InsertPos); |
| 2542 | return QualType(T, 0); |
| 2543 | } |
| 2544 | |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2545 | QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, |
| 2546 | NestedNameSpecifier *NNS, |
| 2547 | const IdentifierInfo *Name, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2548 | QualType Canon) const { |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2549 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 2550 | |
| 2551 | if (Canon.isNull()) { |
| 2552 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2553 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 2554 | if (Keyword == ETK_None) |
| 2555 | CanonKeyword = ETK_Typename; |
| 2556 | |
| 2557 | if (CanonNNS != NNS || CanonKeyword != Keyword) |
| 2558 | Canon = getDependentNameType(CanonKeyword, CanonNNS, Name); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2559 | } |
| 2560 | |
| 2561 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2562 | DependentNameType::Profile(ID, Keyword, NNS, Name); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2563 | |
| 2564 | void *InsertPos = 0; |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2565 | DependentNameType *T |
| 2566 | = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2567 | if (T) |
| 2568 | return QualType(T, 0); |
| 2569 | |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2570 | T = new (*this) DependentNameType(Keyword, NNS, Name, Canon); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2571 | Types.push_back(T); |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2572 | DependentNameTypes.InsertNode(T, InsertPos); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2573 | return QualType(T, 0); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2574 | } |
| 2575 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2576 | QualType |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2577 | ASTContext::getDependentTemplateSpecializationType( |
| 2578 | ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2579 | NestedNameSpecifier *NNS, |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2580 | const IdentifierInfo *Name, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2581 | const TemplateArgumentListInfo &Args) const { |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2582 | // TODO: avoid this copy |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2583 | SmallVector<TemplateArgument, 16> ArgCopy; |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2584 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
| 2585 | ArgCopy.push_back(Args[I].getArgument()); |
| 2586 | return getDependentTemplateSpecializationType(Keyword, NNS, Name, |
| 2587 | ArgCopy.size(), |
| 2588 | ArgCopy.data()); |
| 2589 | } |
| 2590 | |
| 2591 | QualType |
| 2592 | ASTContext::getDependentTemplateSpecializationType( |
| 2593 | ElaboratedTypeKeyword Keyword, |
| 2594 | NestedNameSpecifier *NNS, |
| 2595 | const IdentifierInfo *Name, |
| 2596 | unsigned NumArgs, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2597 | const TemplateArgument *Args) const { |
Douglas Gregor | 6e06801 | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 2598 | assert((!NNS || NNS->isDependent()) && |
| 2599 | "nested-name-specifier must be dependent"); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2600 | |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2601 | llvm::FoldingSetNodeID ID; |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2602 | DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS, |
| 2603 | Name, NumArgs, Args); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2604 | |
| 2605 | void *InsertPos = 0; |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2606 | DependentTemplateSpecializationType *T |
| 2607 | = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2608 | if (T) |
| 2609 | return QualType(T, 0); |
| 2610 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2611 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2612 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2613 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 2614 | if (Keyword == ETK_None) CanonKeyword = ETK_Typename; |
| 2615 | |
| 2616 | bool AnyNonCanonArgs = false; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2617 | SmallVector<TemplateArgument, 16> CanonArgs(NumArgs); |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2618 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 2619 | CanonArgs[I] = getCanonicalTemplateArgument(Args[I]); |
| 2620 | if (!CanonArgs[I].structurallyEquals(Args[I])) |
| 2621 | AnyNonCanonArgs = true; |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2622 | } |
| 2623 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2624 | QualType Canon; |
| 2625 | if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) { |
| 2626 | Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS, |
| 2627 | Name, NumArgs, |
| 2628 | CanonArgs.data()); |
| 2629 | |
| 2630 | // Find the insert position again. |
| 2631 | DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2632 | } |
| 2633 | |
| 2634 | void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) + |
| 2635 | sizeof(TemplateArgument) * NumArgs), |
| 2636 | TypeAlignment); |
John McCall | 773cc98 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 2637 | T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS, |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2638 | Name, NumArgs, Args, Canon); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2639 | Types.push_back(T); |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2640 | DependentTemplateSpecializationTypes.InsertNode(T, InsertPos); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2641 | return QualType(T, 0); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2642 | } |
| 2643 | |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2644 | QualType ASTContext::getPackExpansionType(QualType Pattern, |
| 2645 | llvm::Optional<unsigned> NumExpansions) { |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2646 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2647 | PackExpansionType::Profile(ID, Pattern, NumExpansions); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2648 | |
| 2649 | assert(Pattern->containsUnexpandedParameterPack() && |
| 2650 | "Pack expansions must expand one or more parameter packs"); |
| 2651 | void *InsertPos = 0; |
| 2652 | PackExpansionType *T |
| 2653 | = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2654 | if (T) |
| 2655 | return QualType(T, 0); |
| 2656 | |
| 2657 | QualType Canon; |
| 2658 | if (!Pattern.isCanonical()) { |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2659 | Canon = getPackExpansionType(getCanonicalType(Pattern), NumExpansions); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2660 | |
| 2661 | // Find the insert position again. |
| 2662 | PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2663 | } |
| 2664 | |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2665 | T = new (*this) PackExpansionType(Pattern, Canon, NumExpansions); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2666 | Types.push_back(T); |
| 2667 | PackExpansionTypes.InsertNode(T, InsertPos); |
| 2668 | return QualType(T, 0); |
| 2669 | } |
| 2670 | |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2671 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 2672 | /// alphabetically. |
| 2673 | static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, |
| 2674 | const ObjCProtocolDecl *RHS) { |
Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2675 | return LHS->getDeclName() < RHS->getDeclName(); |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2676 | } |
| 2677 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2678 | static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols, |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2679 | unsigned NumProtocols) { |
| 2680 | if (NumProtocols == 0) return true; |
| 2681 | |
| 2682 | for (unsigned i = 1; i != NumProtocols; ++i) |
| 2683 | if (!CmpProtocolNames(Protocols[i-1], Protocols[i])) |
| 2684 | return false; |
| 2685 | return true; |
| 2686 | } |
| 2687 | |
| 2688 | static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols, |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2689 | unsigned &NumProtocols) { |
| 2690 | ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2691 | |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2692 | // Sort protocols, keyed by name. |
| 2693 | std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); |
| 2694 | |
| 2695 | // Remove duplicates. |
| 2696 | ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); |
| 2697 | NumProtocols = ProtocolsEnd-Protocols; |
| 2698 | } |
| 2699 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2700 | QualType ASTContext::getObjCObjectType(QualType BaseType, |
| 2701 | ObjCProtocolDecl * const *Protocols, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2702 | unsigned NumProtocols) const { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2703 | // If the base type is an interface and there aren't any protocols |
| 2704 | // to add, then the interface type will do just fine. |
| 2705 | if (!NumProtocols && isa<ObjCInterfaceType>(BaseType)) |
| 2706 | return BaseType; |
| 2707 | |
| 2708 | // Look in the folding set for an existing type. |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2709 | llvm::FoldingSetNodeID ID; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2710 | ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols); |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2711 | void *InsertPos = 0; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2712 | if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2713 | return QualType(QT, 0); |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2714 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2715 | // Build the canonical type, which has the canonical base type and |
| 2716 | // a sorted-and-uniqued list of protocols. |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2717 | QualType Canonical; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2718 | bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols); |
| 2719 | if (!ProtocolsSorted || !BaseType.isCanonical()) { |
| 2720 | if (!ProtocolsSorted) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2721 | SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols, |
Benjamin Kramer | 2e3197e | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 2722 | Protocols + NumProtocols); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2723 | unsigned UniqueCount = NumProtocols; |
| 2724 | |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2725 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2726 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 2727 | &Sorted[0], UniqueCount); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2728 | } else { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2729 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 2730 | Protocols, NumProtocols); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2731 | } |
| 2732 | |
| 2733 | // Regenerate InsertPos. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2734 | ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2735 | } |
| 2736 | |
| 2737 | unsigned Size = sizeof(ObjCObjectTypeImpl); |
| 2738 | Size += NumProtocols * sizeof(ObjCProtocolDecl *); |
| 2739 | void *Mem = Allocate(Size, TypeAlignment); |
| 2740 | ObjCObjectTypeImpl *T = |
| 2741 | new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols); |
| 2742 | |
| 2743 | Types.push_back(T); |
| 2744 | ObjCObjectTypes.InsertNode(T, InsertPos); |
| 2745 | return QualType(T, 0); |
| 2746 | } |
| 2747 | |
| 2748 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 2749 | /// the given object type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2750 | QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2751 | llvm::FoldingSetNodeID ID; |
| 2752 | ObjCObjectPointerType::Profile(ID, ObjectT); |
| 2753 | |
| 2754 | void *InsertPos = 0; |
| 2755 | if (ObjCObjectPointerType *QT = |
| 2756 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2757 | return QualType(QT, 0); |
| 2758 | |
| 2759 | // Find the canonical object type. |
| 2760 | QualType Canonical; |
| 2761 | if (!ObjectT.isCanonical()) { |
| 2762 | Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT)); |
| 2763 | |
| 2764 | // Regenerate InsertPos. |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2765 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2766 | } |
| 2767 | |
Douglas Gregor | f85bee6 | 2010-02-08 22:59:26 +0000 | [diff] [blame] | 2768 | // No match. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2769 | void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment); |
| 2770 | ObjCObjectPointerType *QType = |
| 2771 | new (Mem) ObjCObjectPointerType(Canonical, ObjectT); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2772 | |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2773 | Types.push_back(QType); |
| 2774 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2775 | return QualType(QType, 0); |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2776 | } |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2777 | |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2778 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 2779 | /// specified ObjC interface decl. The list of protocols is optional. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2780 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) const { |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2781 | if (Decl->TypeForDecl) |
| 2782 | return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2783 | |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2784 | // FIXME: redeclarations? |
| 2785 | void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment); |
| 2786 | ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl); |
| 2787 | Decl->TypeForDecl = T; |
| 2788 | Types.push_back(T); |
| 2789 | return QualType(T, 0); |
Fariborz Jahanian | 70e8f10 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2790 | } |
| 2791 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2792 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 2793 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2794 | /// multiple declarations that refer to "typeof(x)" all contain different |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2795 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2796 | /// on canonical type's (which are always unique). |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2797 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) const { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2798 | TypeOfExprType *toe; |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2799 | if (tofExpr->isTypeDependent()) { |
| 2800 | llvm::FoldingSetNodeID ID; |
| 2801 | DependentTypeOfExprType::Profile(ID, *this, tofExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2802 | |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2803 | void *InsertPos = 0; |
| 2804 | DependentTypeOfExprType *Canon |
| 2805 | = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2806 | if (Canon) { |
| 2807 | // We already have a "canonical" version of an identical, dependent |
| 2808 | // typeof(expr) type. Use that as our canonical type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2809 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2810 | QualType((TypeOfExprType*)Canon, 0)); |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 2811 | } else { |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2812 | // Build a new, canonical typeof(expr) type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2813 | Canon |
| 2814 | = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2815 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); |
| 2816 | toe = Canon; |
| 2817 | } |
| 2818 | } else { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2819 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2820 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2821 | } |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2822 | Types.push_back(toe); |
| 2823 | return QualType(toe, 0); |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2824 | } |
| 2825 | |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2826 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 2827 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 2828 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2829 | /// an issue. This doesn't effect the type checker, since it operates |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2830 | /// on canonical type's (which are always unique). |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2831 | QualType ASTContext::getTypeOfType(QualType tofType) const { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2832 | QualType Canonical = getCanonicalType(tofType); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2833 | TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2834 | Types.push_back(tot); |
| 2835 | return QualType(tot, 0); |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2836 | } |
| 2837 | |
Anders Carlsson | ad6bd35 | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2838 | /// getDecltypeForExpr - Given an expr, will return the decltype for that |
| 2839 | /// expression, according to the rules in C++0x [dcl.type.simple]p4 |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2840 | static QualType getDecltypeForExpr(const Expr *e, const ASTContext &Context) { |
Anders Carlsson | 7d20957 | 2009-06-25 15:00:34 +0000 | [diff] [blame] | 2841 | if (e->isTypeDependent()) |
| 2842 | return Context.DependentTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2843 | |
Anders Carlsson | ad6bd35 | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2844 | // If e is an id expression or a class member access, decltype(e) is defined |
| 2845 | // as the type of the entity named by e. |
| 2846 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) { |
| 2847 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) |
| 2848 | return VD->getType(); |
| 2849 | } |
| 2850 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) { |
| 2851 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) |
| 2852 | return FD->getType(); |
| 2853 | } |
| 2854 | // If e is a function call or an invocation of an overloaded operator, |
| 2855 | // (parentheses around e are ignored), decltype(e) is defined as the |
| 2856 | // return type of that function. |
| 2857 | if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens())) |
| 2858 | return CE->getCallReturnType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2859 | |
Anders Carlsson | ad6bd35 | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2860 | QualType T = e->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2861 | |
| 2862 | // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is |
Anders Carlsson | ad6bd35 | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2863 | // defined as T&, otherwise decltype(e) is defined as T. |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 2864 | if (e->isLValue()) |
Anders Carlsson | ad6bd35 | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2865 | T = Context.getLValueReferenceType(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2866 | |
Anders Carlsson | ad6bd35 | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2867 | return T; |
| 2868 | } |
| 2869 | |
Anders Carlsson | 81df7b8 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2870 | /// getDecltypeType - Unlike many "get<Type>" functions, we don't unique |
| 2871 | /// DecltypeType AST's. The only motivation to unique these nodes would be |
| 2872 | /// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2873 | /// an issue. This doesn't effect the type checker, since it operates |
Anders Carlsson | 81df7b8 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2874 | /// on canonical type's (which are always unique). |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2875 | QualType ASTContext::getDecltypeType(Expr *e) const { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2876 | DecltypeType *dt; |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2877 | |
| 2878 | // C++0x [temp.type]p2: |
| 2879 | // If an expression e involves a template parameter, decltype(e) denotes a |
| 2880 | // unique dependent type. Two such decltype-specifiers refer to the same |
| 2881 | // type only if their expressions are equivalent (14.5.6.1). |
| 2882 | if (e->isInstantiationDependent()) { |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2883 | llvm::FoldingSetNodeID ID; |
| 2884 | DependentDecltypeType::Profile(ID, *this, e); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2885 | |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2886 | void *InsertPos = 0; |
| 2887 | DependentDecltypeType *Canon |
| 2888 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2889 | if (Canon) { |
| 2890 | // We already have a "canonical" version of an equivalent, dependent |
| 2891 | // decltype type. Use that as our canonical type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2892 | dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy, |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2893 | QualType((DecltypeType*)Canon, 0)); |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 2894 | } else { |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2895 | // Build a new, canonical typeof(expr) type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2896 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2897 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); |
| 2898 | dt = Canon; |
| 2899 | } |
| 2900 | } else { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2901 | QualType T = getDecltypeForExpr(e, *this); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2902 | dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T)); |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2903 | } |
Anders Carlsson | 81df7b8 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2904 | Types.push_back(dt); |
| 2905 | return QualType(dt, 0); |
| 2906 | } |
| 2907 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 2908 | /// getUnaryTransformationType - We don't unique these, since the memory |
| 2909 | /// savings are minimal and these are rare. |
| 2910 | QualType ASTContext::getUnaryTransformType(QualType BaseType, |
| 2911 | QualType UnderlyingType, |
| 2912 | UnaryTransformType::UTTKind Kind) |
| 2913 | const { |
| 2914 | UnaryTransformType *Ty = |
Douglas Gregor | 6c6e676 | 2011-05-25 17:51:54 +0000 | [diff] [blame] | 2915 | new (*this, TypeAlignment) UnaryTransformType (BaseType, UnderlyingType, |
| 2916 | Kind, |
| 2917 | UnderlyingType->isDependentType() ? |
| 2918 | QualType() : UnderlyingType); |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 2919 | Types.push_back(Ty); |
| 2920 | return QualType(Ty, 0); |
| 2921 | } |
| 2922 | |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 2923 | /// getAutoType - We only unique auto types after they've been deduced. |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2924 | QualType ASTContext::getAutoType(QualType DeducedType) const { |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 2925 | void *InsertPos = 0; |
| 2926 | if (!DeducedType.isNull()) { |
| 2927 | // Look in the folding set for an existing type. |
| 2928 | llvm::FoldingSetNodeID ID; |
| 2929 | AutoType::Profile(ID, DeducedType); |
| 2930 | if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2931 | return QualType(AT, 0); |
| 2932 | } |
| 2933 | |
| 2934 | AutoType *AT = new (*this, TypeAlignment) AutoType(DeducedType); |
| 2935 | Types.push_back(AT); |
| 2936 | if (InsertPos) |
| 2937 | AutoTypes.InsertNode(AT, InsertPos); |
| 2938 | return QualType(AT, 0); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2939 | } |
| 2940 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 2941 | /// getAtomicType - Return the uniqued reference to the atomic type for |
| 2942 | /// the given value type. |
| 2943 | QualType ASTContext::getAtomicType(QualType T) const { |
| 2944 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2945 | // structure. |
| 2946 | llvm::FoldingSetNodeID ID; |
| 2947 | AtomicType::Profile(ID, T); |
| 2948 | |
| 2949 | void *InsertPos = 0; |
| 2950 | if (AtomicType *AT = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2951 | return QualType(AT, 0); |
| 2952 | |
| 2953 | // If the atomic value type isn't canonical, this won't be a canonical type |
| 2954 | // either, so fill in the canonical type field. |
| 2955 | QualType Canonical; |
| 2956 | if (!T.isCanonical()) { |
| 2957 | Canonical = getAtomicType(getCanonicalType(T)); |
| 2958 | |
| 2959 | // Get the new insert position for the node we care about. |
| 2960 | AtomicType *NewIP = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2961 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
| 2962 | } |
| 2963 | AtomicType *New = new (*this, TypeAlignment) AtomicType(T, Canonical); |
| 2964 | Types.push_back(New); |
| 2965 | AtomicTypes.InsertNode(New, InsertPos); |
| 2966 | return QualType(New, 0); |
| 2967 | } |
| 2968 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2969 | /// getAutoDeductType - Get type pattern for deducing against 'auto'. |
| 2970 | QualType ASTContext::getAutoDeductType() const { |
| 2971 | if (AutoDeductTy.isNull()) |
| 2972 | AutoDeductTy = getAutoType(QualType()); |
| 2973 | assert(!AutoDeductTy.isNull() && "can't build 'auto' pattern"); |
| 2974 | return AutoDeductTy; |
| 2975 | } |
| 2976 | |
| 2977 | /// getAutoRRefDeductType - Get type pattern for deducing against 'auto &&'. |
| 2978 | QualType ASTContext::getAutoRRefDeductType() const { |
| 2979 | if (AutoRRefDeductTy.isNull()) |
| 2980 | AutoRRefDeductTy = getRValueReferenceType(getAutoDeductType()); |
| 2981 | assert(!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern"); |
| 2982 | return AutoRRefDeductTy; |
| 2983 | } |
| 2984 | |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 2985 | /// getTagDeclType - Return the unique reference to the type for the |
| 2986 | /// specified TagDecl (struct/union/class/enum) decl. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2987 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) const { |
Ted Kremenek | 2b0ce11 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 2988 | assert (Decl); |
Mike Stump | b93185d | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 2989 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 2990 | // away const? mutable? |
| 2991 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 2992 | } |
| 2993 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2994 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 2995 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 2996 | /// needs to agree with the definition in <stddef.h>. |
Anders Carlsson | 22f443f | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 2997 | CanQualType ASTContext::getSizeType() const { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2998 | return getFromTargetType(Target->getSizeType()); |
Steve Naroff | 92e30f8 | 2007-04-02 22:35:25 +0000 | [diff] [blame] | 2999 | } |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 3000 | |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3001 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 3002 | /// Used when in C++, as a GCC extension. |
| 3003 | QualType ASTContext::getSignedWCharType() const { |
| 3004 | // FIXME: derive from "Target" ? |
| 3005 | return WCharTy; |
| 3006 | } |
| 3007 | |
| 3008 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 3009 | /// Used when in C++, as a GCC extension. |
| 3010 | QualType ASTContext::getUnsignedWCharType() const { |
| 3011 | // FIXME: derive from "Target" ? |
| 3012 | return UnsignedIntTy; |
| 3013 | } |
| 3014 | |
Chris Lattner | d2b88ab | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 3015 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) |
| 3016 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 3017 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3018 | return getFromTargetType(Target->getPtrDiffType(0)); |
Chris Lattner | d2b88ab | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 3019 | } |
| 3020 | |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3021 | //===----------------------------------------------------------------------===// |
| 3022 | // Type Operators |
| 3023 | //===----------------------------------------------------------------------===// |
| 3024 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3025 | CanQualType ASTContext::getCanonicalParamType(QualType T) const { |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3026 | // Push qualifiers into arrays, and then discard any remaining |
| 3027 | // qualifiers. |
| 3028 | T = getCanonicalType(T); |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3029 | T = getVariableArrayDecayedType(T); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3030 | const Type *Ty = T.getTypePtr(); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3031 | QualType Result; |
| 3032 | if (isa<ArrayType>(Ty)) { |
| 3033 | Result = getArrayDecayedType(QualType(Ty,0)); |
| 3034 | } else if (isa<FunctionType>(Ty)) { |
| 3035 | Result = getPointerType(QualType(Ty, 0)); |
| 3036 | } else { |
| 3037 | Result = QualType(Ty, 0); |
| 3038 | } |
| 3039 | |
| 3040 | return CanQualType::CreateUnsafe(Result); |
| 3041 | } |
| 3042 | |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3043 | QualType ASTContext::getUnqualifiedArrayType(QualType type, |
| 3044 | Qualifiers &quals) { |
| 3045 | SplitQualType splitType = type.getSplitUnqualifiedType(); |
| 3046 | |
| 3047 | // FIXME: getSplitUnqualifiedType() actually walks all the way to |
| 3048 | // the unqualified desugared type and then drops it on the floor. |
| 3049 | // We then have to strip that sugar back off with |
| 3050 | // getUnqualifiedDesugaredType(), which is silly. |
| 3051 | const ArrayType *AT = |
| 3052 | dyn_cast<ArrayType>(splitType.first->getUnqualifiedDesugaredType()); |
| 3053 | |
| 3054 | // If we don't have an array, just use the results in splitType. |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3055 | if (!AT) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3056 | quals = splitType.second; |
| 3057 | return QualType(splitType.first, 0); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3058 | } |
| 3059 | |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3060 | // Otherwise, recurse on the array's element type. |
| 3061 | QualType elementType = AT->getElementType(); |
| 3062 | QualType unqualElementType = getUnqualifiedArrayType(elementType, quals); |
| 3063 | |
| 3064 | // If that didn't change the element type, AT has no qualifiers, so we |
| 3065 | // can just use the results in splitType. |
| 3066 | if (elementType == unqualElementType) { |
| 3067 | assert(quals.empty()); // from the recursive call |
| 3068 | quals = splitType.second; |
| 3069 | return QualType(splitType.first, 0); |
| 3070 | } |
| 3071 | |
| 3072 | // Otherwise, add in the qualifiers from the outermost type, then |
| 3073 | // build the type back up. |
| 3074 | quals.addConsistentQualifiers(splitType.second); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3075 | |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3076 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3077 | return getConstantArrayType(unqualElementType, CAT->getSize(), |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3078 | CAT->getSizeModifier(), 0); |
| 3079 | } |
| 3080 | |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3081 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3082 | return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3083 | } |
| 3084 | |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3085 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3086 | return getVariableArrayType(unqualElementType, |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3087 | VAT->getSizeExpr(), |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3088 | VAT->getSizeModifier(), |
| 3089 | VAT->getIndexTypeCVRQualifiers(), |
| 3090 | VAT->getBracketsRange()); |
| 3091 | } |
| 3092 | |
| 3093 | const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT); |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3094 | return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(), |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3095 | DSAT->getSizeModifier(), 0, |
| 3096 | SourceRange()); |
| 3097 | } |
| 3098 | |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3099 | /// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that |
| 3100 | /// may be similar (C++ 4.4), replaces T1 and T2 with the type that |
| 3101 | /// they point to and return true. If T1 and T2 aren't pointer types |
| 3102 | /// or pointer-to-member types, or if they are not similar at this |
| 3103 | /// level, returns false and leaves T1 and T2 unchanged. Top-level |
| 3104 | /// qualifiers on T1 and T2 are ignored. This function will typically |
| 3105 | /// be called in a loop that successively "unwraps" pointer and |
| 3106 | /// pointer-to-member types to compare them at each level. |
| 3107 | bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) { |
| 3108 | const PointerType *T1PtrType = T1->getAs<PointerType>(), |
| 3109 | *T2PtrType = T2->getAs<PointerType>(); |
| 3110 | if (T1PtrType && T2PtrType) { |
| 3111 | T1 = T1PtrType->getPointeeType(); |
| 3112 | T2 = T2PtrType->getPointeeType(); |
| 3113 | return true; |
| 3114 | } |
| 3115 | |
| 3116 | const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(), |
| 3117 | *T2MPType = T2->getAs<MemberPointerType>(); |
| 3118 | if (T1MPType && T2MPType && |
| 3119 | hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0), |
| 3120 | QualType(T2MPType->getClass(), 0))) { |
| 3121 | T1 = T1MPType->getPointeeType(); |
| 3122 | T2 = T2MPType->getPointeeType(); |
| 3123 | return true; |
| 3124 | } |
| 3125 | |
| 3126 | if (getLangOptions().ObjC1) { |
| 3127 | const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(), |
| 3128 | *T2OPType = T2->getAs<ObjCObjectPointerType>(); |
| 3129 | if (T1OPType && T2OPType) { |
| 3130 | T1 = T1OPType->getPointeeType(); |
| 3131 | T2 = T2OPType->getPointeeType(); |
| 3132 | return true; |
| 3133 | } |
| 3134 | } |
| 3135 | |
| 3136 | // FIXME: Block pointers, too? |
| 3137 | |
| 3138 | return false; |
| 3139 | } |
| 3140 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3141 | DeclarationNameInfo |
| 3142 | ASTContext::getNameForTemplate(TemplateName Name, |
| 3143 | SourceLocation NameLoc) const { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3144 | switch (Name.getKind()) { |
| 3145 | case TemplateName::QualifiedTemplate: |
| 3146 | case TemplateName::Template: |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3147 | // DNInfo work in progress: CHECKME: what about DNLoc? |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3148 | return DeclarationNameInfo(Name.getAsTemplateDecl()->getDeclName(), |
| 3149 | NameLoc); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3150 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3151 | case TemplateName::OverloadedTemplate: { |
| 3152 | OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate(); |
| 3153 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 3154 | return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc); |
| 3155 | } |
| 3156 | |
| 3157 | case TemplateName::DependentTemplate: { |
| 3158 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3159 | DeclarationName DName; |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3160 | if (DTN->isIdentifier()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3161 | DName = DeclarationNames.getIdentifier(DTN->getIdentifier()); |
| 3162 | return DeclarationNameInfo(DName, NameLoc); |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3163 | } else { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3164 | DName = DeclarationNames.getCXXOperatorName(DTN->getOperator()); |
| 3165 | // DNInfo work in progress: FIXME: source locations? |
| 3166 | DeclarationNameLoc DNLoc; |
| 3167 | DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding(); |
| 3168 | DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding(); |
| 3169 | return DeclarationNameInfo(DName, NameLoc, DNLoc); |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3170 | } |
| 3171 | } |
| 3172 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3173 | case TemplateName::SubstTemplateTemplateParm: { |
| 3174 | SubstTemplateTemplateParmStorage *subst |
| 3175 | = Name.getAsSubstTemplateTemplateParm(); |
| 3176 | return DeclarationNameInfo(subst->getParameter()->getDeclName(), |
| 3177 | NameLoc); |
| 3178 | } |
| 3179 | |
| 3180 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 3181 | SubstTemplateTemplateParmPackStorage *subst |
| 3182 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 3183 | return DeclarationNameInfo(subst->getParameterPack()->getDeclName(), |
| 3184 | NameLoc); |
| 3185 | } |
| 3186 | } |
| 3187 | |
| 3188 | llvm_unreachable("bad template name kind!"); |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3189 | } |
| 3190 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3191 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3192 | switch (Name.getKind()) { |
| 3193 | case TemplateName::QualifiedTemplate: |
| 3194 | case TemplateName::Template: { |
| 3195 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3196 | if (TemplateTemplateParmDecl *TTP |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3197 | = dyn_cast<TemplateTemplateParmDecl>(Template)) |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3198 | Template = getCanonicalTemplateTemplateParmDecl(TTP); |
| 3199 | |
| 3200 | // The canonical template name is the canonical template declaration. |
Argyrios Kyrtzidis | 6b7e376 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 3201 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3202 | } |
Douglas Gregor | 6bc5058 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 3203 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3204 | case TemplateName::OverloadedTemplate: |
| 3205 | llvm_unreachable("cannot canonicalize overloaded template"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3206 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3207 | case TemplateName::DependentTemplate: { |
| 3208 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 3209 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 3210 | return DTN->CanonicalTemplateName; |
| 3211 | } |
| 3212 | |
| 3213 | case TemplateName::SubstTemplateTemplateParm: { |
| 3214 | SubstTemplateTemplateParmStorage *subst |
| 3215 | = Name.getAsSubstTemplateTemplateParm(); |
| 3216 | return getCanonicalTemplateName(subst->getReplacement()); |
| 3217 | } |
| 3218 | |
| 3219 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 3220 | SubstTemplateTemplateParmPackStorage *subst |
| 3221 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 3222 | TemplateTemplateParmDecl *canonParameter |
| 3223 | = getCanonicalTemplateTemplateParmDecl(subst->getParameterPack()); |
| 3224 | TemplateArgument canonArgPack |
| 3225 | = getCanonicalTemplateArgument(subst->getArgumentPack()); |
| 3226 | return getSubstTemplateTemplateParmPack(canonParameter, canonArgPack); |
| 3227 | } |
| 3228 | } |
| 3229 | |
| 3230 | llvm_unreachable("bad template name!"); |
Douglas Gregor | 6bc5058 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 3231 | } |
| 3232 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 3233 | bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) { |
| 3234 | X = getCanonicalTemplateName(X); |
| 3235 | Y = getCanonicalTemplateName(Y); |
| 3236 | return X.getAsVoidPointer() == Y.getAsVoidPointer(); |
| 3237 | } |
| 3238 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3239 | TemplateArgument |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3240 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const { |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3241 | switch (Arg.getKind()) { |
| 3242 | case TemplateArgument::Null: |
| 3243 | return Arg; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3244 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3245 | case TemplateArgument::Expression: |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3246 | return Arg; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3247 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3248 | case TemplateArgument::Declaration: |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3249 | return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3250 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3251 | case TemplateArgument::Template: |
| 3252 | return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate())); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3253 | |
| 3254 | case TemplateArgument::TemplateExpansion: |
| 3255 | return TemplateArgument(getCanonicalTemplateName( |
| 3256 | Arg.getAsTemplateOrTemplatePattern()), |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 3257 | Arg.getNumTemplateExpansions()); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3258 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3259 | case TemplateArgument::Integral: |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3260 | return TemplateArgument(*Arg.getAsIntegral(), |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3261 | getCanonicalType(Arg.getIntegralType())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3262 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3263 | case TemplateArgument::Type: |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3264 | return TemplateArgument(getCanonicalType(Arg.getAsType())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3265 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3266 | case TemplateArgument::Pack: { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 3267 | if (Arg.pack_size() == 0) |
| 3268 | return Arg; |
| 3269 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3270 | TemplateArgument *CanonArgs |
| 3271 | = new (*this) TemplateArgument[Arg.pack_size()]; |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3272 | unsigned Idx = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3273 | for (TemplateArgument::pack_iterator A = Arg.pack_begin(), |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3274 | AEnd = Arg.pack_end(); |
| 3275 | A != AEnd; (void)++A, ++Idx) |
| 3276 | CanonArgs[Idx] = getCanonicalTemplateArgument(*A); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3277 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3278 | return TemplateArgument(CanonArgs, Arg.pack_size()); |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3279 | } |
| 3280 | } |
| 3281 | |
| 3282 | // Silence GCC warning |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3283 | llvm_unreachable("Unhandled template argument kind"); |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3284 | } |
| 3285 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3286 | NestedNameSpecifier * |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3287 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3288 | if (!NNS) |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3289 | return 0; |
| 3290 | |
| 3291 | switch (NNS->getKind()) { |
| 3292 | case NestedNameSpecifier::Identifier: |
| 3293 | // Canonicalize the prefix but keep the identifier the same. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3294 | return NestedNameSpecifier::Create(*this, |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3295 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 3296 | NNS->getAsIdentifier()); |
| 3297 | |
| 3298 | case NestedNameSpecifier::Namespace: |
| 3299 | // A namespace is canonical; build a nested-name-specifier with |
| 3300 | // this namespace and no prefix. |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 3301 | return NestedNameSpecifier::Create(*this, 0, |
| 3302 | NNS->getAsNamespace()->getOriginalNamespace()); |
| 3303 | |
| 3304 | case NestedNameSpecifier::NamespaceAlias: |
| 3305 | // A namespace is canonical; build a nested-name-specifier with |
| 3306 | // this namespace and no prefix. |
| 3307 | return NestedNameSpecifier::Create(*this, 0, |
| 3308 | NNS->getAsNamespaceAlias()->getNamespace() |
| 3309 | ->getOriginalNamespace()); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3310 | |
| 3311 | case NestedNameSpecifier::TypeSpec: |
| 3312 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 3313 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
Douglas Gregor | 3ade570 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3314 | |
| 3315 | // If we have some kind of dependent-named type (e.g., "typename T::type"), |
| 3316 | // break it apart into its prefix and identifier, then reconsititute those |
| 3317 | // as the canonical nested-name-specifier. This is required to canonicalize |
| 3318 | // a dependent nested-name-specifier involving typedefs of dependent-name |
| 3319 | // types, e.g., |
| 3320 | // typedef typename T::type T1; |
| 3321 | // typedef typename T1::type T2; |
| 3322 | if (const DependentNameType *DNT = T->getAs<DependentNameType>()) { |
| 3323 | NestedNameSpecifier *Prefix |
| 3324 | = getCanonicalNestedNameSpecifier(DNT->getQualifier()); |
| 3325 | return NestedNameSpecifier::Create(*this, Prefix, |
| 3326 | const_cast<IdentifierInfo *>(DNT->getIdentifier())); |
| 3327 | } |
| 3328 | |
Douglas Gregor | cc10cbf | 2010-11-04 00:14:23 +0000 | [diff] [blame] | 3329 | // Do the same thing as above, but with dependent-named specializations. |
Douglas Gregor | 3ade570 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3330 | if (const DependentTemplateSpecializationType *DTST |
| 3331 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 3332 | NestedNameSpecifier *Prefix |
| 3333 | = getCanonicalNestedNameSpecifier(DTST->getQualifier()); |
Douglas Gregor | 6e06801 | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 3334 | |
| 3335 | T = getDependentTemplateSpecializationType(DTST->getKeyword(), |
| 3336 | Prefix, DTST->getIdentifier(), |
| 3337 | DTST->getNumArgs(), |
| 3338 | DTST->getArgs()); |
Douglas Gregor | 3ade570 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3339 | T = getCanonicalType(T); |
| 3340 | } |
| 3341 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3342 | return NestedNameSpecifier::Create(*this, 0, false, |
| 3343 | const_cast<Type*>(T.getTypePtr())); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3344 | } |
| 3345 | |
| 3346 | case NestedNameSpecifier::Global: |
| 3347 | // The global specifier is canonical and unique. |
| 3348 | return NNS; |
| 3349 | } |
| 3350 | |
| 3351 | // Required to silence a GCC warning |
| 3352 | return 0; |
| 3353 | } |
| 3354 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3355 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3356 | const ArrayType *ASTContext::getAsArrayType(QualType T) const { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3357 | // Handle the non-qualified case efficiently. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3358 | if (!T.hasLocalQualifiers()) { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3359 | // Handle the common positive case fast. |
| 3360 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 3361 | return AT; |
| 3362 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3363 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3364 | // Handle the common negative case fast. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3365 | if (!isa<ArrayType>(T.getCanonicalType())) |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3366 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3367 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3368 | // Apply any qualifiers from the array type to the element type. This |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3369 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 3370 | // any type qualifiers, the element type is so qualified, not the array type." |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3371 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3372 | // If we get here, we either have type qualifiers on the type, or we have |
| 3373 | // sugar such as a typedef in the way. If we have type qualifiers on the type |
Douglas Gregor | 2211d34 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 3374 | // we must propagate them down into the element type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3375 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3376 | SplitQualType split = T.getSplitDesugaredType(); |
| 3377 | Qualifiers qs = split.second; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3378 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3379 | // If we have a simple case, just return now. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3380 | const ArrayType *ATy = dyn_cast<ArrayType>(split.first); |
| 3381 | if (ATy == 0 || qs.empty()) |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3382 | return ATy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3383 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3384 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 3385 | // qualifiers into the array element type and return a new array type. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3386 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3387 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3388 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 3389 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 3390 | CAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3391 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3392 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 3393 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 3394 | IAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3395 | IAT->getIndexTypeCVRQualifiers())); |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3396 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3397 | if (const DependentSizedArrayType *DSAT |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3398 | = dyn_cast<DependentSizedArrayType>(ATy)) |
| 3399 | return cast<ArrayType>( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3400 | getDependentSizedArrayType(NewEltTy, |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3401 | DSAT->getSizeExpr(), |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3402 | DSAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3403 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3404 | DSAT->getBracketsRange())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3405 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3406 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3407 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3408 | VAT->getSizeExpr(), |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3409 | VAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3410 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3411 | VAT->getBracketsRange())); |
Chris Lattner | ed0d079 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 3412 | } |
| 3413 | |
Douglas Gregor | 8428064 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 3414 | QualType ASTContext::getAdjustedParameterType(QualType T) { |
| 3415 | // C99 6.7.5.3p7: |
| 3416 | // A declaration of a parameter as "array of type" shall be |
| 3417 | // adjusted to "qualified pointer to type", where the type |
| 3418 | // qualifiers (if any) are those specified within the [ and ] of |
| 3419 | // the array type derivation. |
| 3420 | if (T->isArrayType()) |
| 3421 | return getArrayDecayedType(T); |
| 3422 | |
| 3423 | // C99 6.7.5.3p8: |
| 3424 | // A declaration of a parameter as "function returning type" |
| 3425 | // shall be adjusted to "pointer to function returning type", as |
| 3426 | // in 6.3.2.1. |
| 3427 | if (T->isFunctionType()) |
| 3428 | return getPointerType(T); |
| 3429 | |
| 3430 | return T; |
| 3431 | } |
| 3432 | |
| 3433 | QualType ASTContext::getSignatureParameterType(QualType T) { |
| 3434 | T = getVariableArrayDecayedType(T); |
| 3435 | T = getAdjustedParameterType(T); |
| 3436 | return T.getUnqualifiedType(); |
| 3437 | } |
| 3438 | |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3439 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 3440 | /// specified array type to a pointer. This operation is non-trivial when |
| 3441 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 3442 | /// this returns a pointer to a properly qualified element of the array. |
| 3443 | /// |
| 3444 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3445 | QualType ASTContext::getArrayDecayedType(QualType Ty) const { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3446 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 3447 | // typedefs in the element type of the array. This also handles propagation |
| 3448 | // of type qualifiers from the array type into the element type if present |
| 3449 | // (C99 6.7.3p8). |
| 3450 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 3451 | assert(PrettyArrayType && "Not an array type!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3452 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3453 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3454 | |
| 3455 | // int x[restrict 4] -> int *restrict |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3456 | return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers()); |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3457 | } |
| 3458 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3459 | QualType ASTContext::getBaseElementType(const ArrayType *array) const { |
| 3460 | return getBaseElementType(array->getElementType()); |
Douglas Gregor | 79f83ed | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 3461 | } |
| 3462 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3463 | QualType ASTContext::getBaseElementType(QualType type) const { |
| 3464 | Qualifiers qs; |
| 3465 | while (true) { |
| 3466 | SplitQualType split = type.getSplitDesugaredType(); |
| 3467 | const ArrayType *array = split.first->getAsArrayTypeUnsafe(); |
| 3468 | if (!array) break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3469 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3470 | type = array->getElementType(); |
| 3471 | qs.addConsistentQualifiers(split.second); |
| 3472 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3473 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3474 | return getQualifiedType(type, qs); |
Anders Carlsson | e0808df | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 3475 | } |
| 3476 | |
Fariborz Jahanian | 6c9e5a2 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 3477 | /// getConstantArrayElementCount - Returns number of constant array elements. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3478 | uint64_t |
Fariborz Jahanian | 6c9e5a2 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 3479 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
| 3480 | uint64_t ElementCount = 1; |
| 3481 | do { |
| 3482 | ElementCount *= CA->getSize().getZExtValue(); |
| 3483 | CA = dyn_cast<ConstantArrayType>(CA->getElementType()); |
| 3484 | } while (CA); |
| 3485 | return ElementCount; |
| 3486 | } |
| 3487 | |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 3488 | /// getFloatingRank - Return a relative rank for floating point types. |
| 3489 | /// This routine will assert if passed a built-in type that isn't a float. |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3490 | static FloatingRank getFloatingRank(QualType T) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3491 | if (const ComplexType *CT = T->getAs<ComplexType>()) |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 3492 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3493 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3494 | assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"); |
| 3495 | switch (T->getAs<BuiltinType>()->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3496 | default: llvm_unreachable("getFloatingRank(): not a floating type"); |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3497 | case BuiltinType::Half: return HalfRank; |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 3498 | case BuiltinType::Float: return FloatRank; |
| 3499 | case BuiltinType::Double: return DoubleRank; |
| 3500 | case BuiltinType::LongDouble: return LongDoubleRank; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 3501 | } |
| 3502 | } |
| 3503 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3504 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 3505 | /// point or a complex type (based on typeDomain/typeSize). |
Steve Naroff | fc6ffa2 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 3506 | /// 'typeDomain' is a real floating point or complex type. |
| 3507 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | b9dfb03 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 3508 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 3509 | QualType Domain) const { |
| 3510 | FloatingRank EltRank = getFloatingRank(Size); |
| 3511 | if (Domain->isComplexType()) { |
| 3512 | switch (EltRank) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3513 | default: llvm_unreachable("getFloatingRank(): illegal value for rank"); |
Steve Naroff | 9091ef7 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 3514 | case FloatRank: return FloatComplexTy; |
| 3515 | case DoubleRank: return DoubleComplexTy; |
| 3516 | case LongDoubleRank: return LongDoubleComplexTy; |
| 3517 | } |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 3518 | } |
Chris Lattner | b9dfb03 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 3519 | |
| 3520 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 3521 | switch (EltRank) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3522 | default: llvm_unreachable("getFloatingRank(): illegal value for rank"); |
Chris Lattner | b9dfb03 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 3523 | case FloatRank: return FloatTy; |
| 3524 | case DoubleRank: return DoubleTy; |
| 3525 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | 9091ef7 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 3526 | } |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 3527 | } |
| 3528 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3529 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 3530 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 3531 | /// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3532 | /// LHS < RHS, return -1. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3533 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const { |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3534 | FloatingRank LHSR = getFloatingRank(LHS); |
| 3535 | FloatingRank RHSR = getFloatingRank(RHS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3536 | |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3537 | if (LHSR == RHSR) |
Steve Naroff | 7af82d4 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 3538 | return 0; |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3539 | if (LHSR > RHSR) |
Steve Naroff | 7af82d4 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 3540 | return 1; |
| 3541 | return -1; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 3542 | } |
| 3543 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3544 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 3545 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 3546 | /// or if it is not canonicalized. |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3547 | unsigned ASTContext::getIntegerRank(const Type *T) const { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 3548 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3549 | if (const EnumType* ET = dyn_cast<EnumType>(T)) |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 3550 | T = ET->getDecl()->getPromotionType().getTypePtr(); |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3551 | |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 3552 | if (T->isSpecificBuiltinType(BuiltinType::WChar_S) || |
| 3553 | T->isSpecificBuiltinType(BuiltinType::WChar_U)) |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3554 | T = getFromTargetType(Target->getWCharType()).getTypePtr(); |
Eli Friedman | c131d3b | 2009-07-05 23:44:27 +0000 | [diff] [blame] | 3555 | |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3556 | if (T->isSpecificBuiltinType(BuiltinType::Char16)) |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3557 | T = getFromTargetType(Target->getChar16Type()).getTypePtr(); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3558 | |
| 3559 | if (T->isSpecificBuiltinType(BuiltinType::Char32)) |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3560 | T = getFromTargetType(Target->getChar32Type()).getTypePtr(); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3561 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3562 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3563 | default: llvm_unreachable("getIntegerRank(): not a built-in integer"); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3564 | case BuiltinType::Bool: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3565 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3566 | case BuiltinType::Char_S: |
| 3567 | case BuiltinType::Char_U: |
| 3568 | case BuiltinType::SChar: |
| 3569 | case BuiltinType::UChar: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3570 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3571 | case BuiltinType::Short: |
| 3572 | case BuiltinType::UShort: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3573 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3574 | case BuiltinType::Int: |
| 3575 | case BuiltinType::UInt: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3576 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3577 | case BuiltinType::Long: |
| 3578 | case BuiltinType::ULong: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3579 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3580 | case BuiltinType::LongLong: |
| 3581 | case BuiltinType::ULongLong: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3582 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | f122cef | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 3583 | case BuiltinType::Int128: |
| 3584 | case BuiltinType::UInt128: |
| 3585 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3586 | } |
| 3587 | } |
| 3588 | |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3589 | /// \brief Whether this is a promotable bitfield reference according |
| 3590 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
| 3591 | /// |
| 3592 | /// \returns the type this bit-field will promote to, or NULL if no |
| 3593 | /// promotion occurs. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3594 | QualType ASTContext::isPromotableBitField(Expr *E) const { |
Douglas Gregor | e05d3cb | 2010-05-24 20:13:53 +0000 | [diff] [blame] | 3595 | if (E->isTypeDependent() || E->isValueDependent()) |
| 3596 | return QualType(); |
| 3597 | |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3598 | FieldDecl *Field = E->getBitField(); |
| 3599 | if (!Field) |
| 3600 | return QualType(); |
| 3601 | |
| 3602 | QualType FT = Field->getType(); |
| 3603 | |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3604 | uint64_t BitWidth = Field->getBitWidthValue(*this); |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3605 | uint64_t IntSize = getTypeSize(IntTy); |
| 3606 | // GCC extension compatibility: if the bit-field size is less than or equal |
| 3607 | // to the size of int, it gets promoted no matter what its type is. |
| 3608 | // For instance, unsigned long bf : 4 gets promoted to signed int. |
| 3609 | if (BitWidth < IntSize) |
| 3610 | return IntTy; |
| 3611 | |
| 3612 | if (BitWidth == IntSize) |
| 3613 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
| 3614 | |
| 3615 | // Types bigger than int are not subject to promotions, and therefore act |
| 3616 | // like the base type. |
| 3617 | // FIXME: This doesn't quite match what gcc does, but what gcc does here |
| 3618 | // is ridiculous. |
| 3619 | return QualType(); |
| 3620 | } |
| 3621 | |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3622 | /// getPromotedIntegerType - Returns the type that Promotable will |
| 3623 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
| 3624 | /// integer type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3625 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) const { |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3626 | assert(!Promotable.isNull()); |
| 3627 | assert(Promotable->isPromotableIntegerType()); |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 3628 | if (const EnumType *ET = Promotable->getAs<EnumType>()) |
| 3629 | return ET->getDecl()->getPromotionType(); |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3630 | if (Promotable->isSignedIntegerType()) |
| 3631 | return IntTy; |
| 3632 | uint64_t PromotableSize = getTypeSize(Promotable); |
| 3633 | uint64_t IntSize = getTypeSize(IntTy); |
| 3634 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
| 3635 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
| 3636 | } |
| 3637 | |
Argyrios Kyrtzidis | 7451d1c | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 3638 | /// \brief Recurses in pointer/array types until it finds an objc retainable |
| 3639 | /// type and returns its ownership. |
| 3640 | Qualifiers::ObjCLifetime ASTContext::getInnerObjCOwnership(QualType T) const { |
| 3641 | while (!T.isNull()) { |
| 3642 | if (T.getObjCLifetime() != Qualifiers::OCL_None) |
| 3643 | return T.getObjCLifetime(); |
| 3644 | if (T->isArrayType()) |
| 3645 | T = getBaseElementType(T); |
| 3646 | else if (const PointerType *PT = T->getAs<PointerType>()) |
| 3647 | T = PT->getPointeeType(); |
| 3648 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
Argyrios Kyrtzidis | 8e25253 | 2011-07-01 23:01:46 +0000 | [diff] [blame] | 3649 | T = RT->getPointeeType(); |
Argyrios Kyrtzidis | 7451d1c | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 3650 | else |
| 3651 | break; |
| 3652 | } |
| 3653 | |
| 3654 | return Qualifiers::OCL_None; |
| 3655 | } |
| 3656 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3657 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3658 | /// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3659 | /// LHS < RHS, return -1. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3660 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3661 | const Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 3662 | const Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3663 | if (LHSC == RHSC) return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3664 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3665 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 3666 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3667 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3668 | unsigned LHSRank = getIntegerRank(LHSC); |
| 3669 | unsigned RHSRank = getIntegerRank(RHSC); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3670 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3671 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 3672 | if (LHSRank == RHSRank) return 0; |
| 3673 | return LHSRank > RHSRank ? 1 : -1; |
| 3674 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3675 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3676 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 3677 | if (LHSUnsigned) { |
| 3678 | // If the unsigned [LHS] type is larger, return it. |
| 3679 | if (LHSRank >= RHSRank) |
| 3680 | return 1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3681 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3682 | // If the signed type can represent all values of the unsigned type, it |
| 3683 | // wins. Because we are dealing with 2's complement and types that are |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3684 | // powers of two larger than each other, this is always safe. |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3685 | return -1; |
| 3686 | } |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3687 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3688 | // If the unsigned [RHS] type is larger, return it. |
| 3689 | if (RHSRank >= LHSRank) |
| 3690 | return -1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3691 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3692 | // If the signed type can represent all values of the unsigned type, it |
| 3693 | // wins. Because we are dealing with 2's complement and types that are |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3694 | // powers of two larger than each other, this is always safe. |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3695 | return 1; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 3696 | } |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3697 | |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3698 | static RecordDecl * |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3699 | CreateRecordDecl(const ASTContext &Ctx, RecordDecl::TagKind TK, |
| 3700 | DeclContext *DC, IdentifierInfo *Id) { |
| 3701 | SourceLocation Loc; |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3702 | if (Ctx.getLangOptions().CPlusPlus) |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3703 | return CXXRecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id); |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3704 | else |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3705 | return RecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id); |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3706 | } |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3707 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3708 | // getCFConstantStringType - Return the type used for constant CFStrings. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3709 | QualType ASTContext::getCFConstantStringType() const { |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3710 | if (!CFConstantStringTypeDecl) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3711 | CFConstantStringTypeDecl = |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3712 | CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3713 | &Idents.get("NSConstantString")); |
John McCall | ae580fe | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3714 | CFConstantStringTypeDecl->startDefinition(); |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3715 | |
Anders Carlsson | 9c1011c | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 3716 | QualType FieldTypes[4]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3717 | |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3718 | // const int *isa; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3719 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
Anders Carlsson | 9c1011c | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 3720 | // int flags; |
| 3721 | FieldTypes[1] = IntTy; |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3722 | // const char *str; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3723 | FieldTypes[2] = getPointerType(CharTy.withConst()); |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3724 | // long length; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3725 | FieldTypes[3] = LongTy; |
| 3726 | |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3727 | // Create fields |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3728 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3729 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3730 | SourceLocation(), |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3731 | SourceLocation(), 0, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3732 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3733 | /*BitWidth=*/0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3734 | /*Mutable=*/false, |
| 3735 | /*HasInit=*/false); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3736 | Field->setAccess(AS_public); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3737 | CFConstantStringTypeDecl->addDecl(Field); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3738 | } |
| 3739 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3740 | CFConstantStringTypeDecl->completeDefinition(); |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3741 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3742 | |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3743 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 412af03 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 3744 | } |
Anders Carlsson | 87c149b | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 3745 | |
Douglas Gregor | 512b077 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3746 | void ASTContext::setCFConstantStringType(QualType T) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3747 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 512b077 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3748 | assert(Rec && "Invalid CFConstantStringType"); |
| 3749 | CFConstantStringTypeDecl = Rec->getDecl(); |
| 3750 | } |
| 3751 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3752 | QualType ASTContext::getBlockDescriptorType() const { |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3753 | if (BlockDescriptorType) |
| 3754 | return getTagDeclType(BlockDescriptorType); |
| 3755 | |
| 3756 | RecordDecl *T; |
| 3757 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3758 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3759 | &Idents.get("__block_descriptor")); |
John McCall | ae580fe | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3760 | T->startDefinition(); |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3761 | |
| 3762 | QualType FieldTypes[] = { |
| 3763 | UnsignedLongTy, |
| 3764 | UnsignedLongTy, |
| 3765 | }; |
| 3766 | |
| 3767 | const char *FieldNames[] = { |
| 3768 | "reserved", |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3769 | "Size" |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3770 | }; |
| 3771 | |
| 3772 | for (size_t i = 0; i < 2; ++i) { |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3773 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3774 | SourceLocation(), |
| 3775 | &Idents.get(FieldNames[i]), |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3776 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3777 | /*BitWidth=*/0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3778 | /*Mutable=*/false, |
| 3779 | /*HasInit=*/false); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3780 | Field->setAccess(AS_public); |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3781 | T->addDecl(Field); |
| 3782 | } |
| 3783 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3784 | T->completeDefinition(); |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3785 | |
| 3786 | BlockDescriptorType = T; |
| 3787 | |
| 3788 | return getTagDeclType(BlockDescriptorType); |
| 3789 | } |
| 3790 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3791 | QualType ASTContext::getBlockDescriptorExtendedType() const { |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3792 | if (BlockDescriptorExtendedType) |
| 3793 | return getTagDeclType(BlockDescriptorExtendedType); |
| 3794 | |
| 3795 | RecordDecl *T; |
| 3796 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3797 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3798 | &Idents.get("__block_descriptor_withcopydispose")); |
John McCall | ae580fe | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3799 | T->startDefinition(); |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3800 | |
| 3801 | QualType FieldTypes[] = { |
| 3802 | UnsignedLongTy, |
| 3803 | UnsignedLongTy, |
| 3804 | getPointerType(VoidPtrTy), |
| 3805 | getPointerType(VoidPtrTy) |
| 3806 | }; |
| 3807 | |
| 3808 | const char *FieldNames[] = { |
| 3809 | "reserved", |
| 3810 | "Size", |
| 3811 | "CopyFuncPtr", |
| 3812 | "DestroyFuncPtr" |
| 3813 | }; |
| 3814 | |
| 3815 | for (size_t i = 0; i < 4; ++i) { |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3816 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3817 | SourceLocation(), |
| 3818 | &Idents.get(FieldNames[i]), |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3819 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3820 | /*BitWidth=*/0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3821 | /*Mutable=*/false, |
| 3822 | /*HasInit=*/false); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3823 | Field->setAccess(AS_public); |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3824 | T->addDecl(Field); |
| 3825 | } |
| 3826 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3827 | T->completeDefinition(); |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3828 | |
| 3829 | BlockDescriptorExtendedType = T; |
| 3830 | |
| 3831 | return getTagDeclType(BlockDescriptorExtendedType); |
| 3832 | } |
| 3833 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3834 | bool ASTContext::BlockRequiresCopying(QualType Ty) const { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3835 | if (Ty->isObjCRetainableType()) |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3836 | return true; |
Fariborz Jahanian | a00076c | 2010-11-17 00:21:28 +0000 | [diff] [blame] | 3837 | if (getLangOptions().CPlusPlus) { |
| 3838 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3839 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Alexis Hunt | fcaeae4 | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 3840 | return RD->hasConstCopyConstructor(); |
Fariborz Jahanian | a00076c | 2010-11-17 00:21:28 +0000 | [diff] [blame] | 3841 | |
| 3842 | } |
| 3843 | } |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3844 | return false; |
| 3845 | } |
| 3846 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3847 | QualType |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3848 | ASTContext::BuildByRefType(StringRef DeclName, QualType Ty) const { |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3849 | // type = struct __Block_byref_1_X { |
Mike Stump | 7fe9cc1 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3850 | // void *__isa; |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3851 | // struct __Block_byref_1_X *__forwarding; |
Mike Stump | 7fe9cc1 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3852 | // unsigned int __flags; |
| 3853 | // unsigned int __size; |
Eli Friedman | 0483192 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 3854 | // void *__copy_helper; // as needed |
| 3855 | // void *__destroy_help // as needed |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3856 | // int X; |
Mike Stump | 7fe9cc1 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3857 | // } * |
| 3858 | |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3859 | bool HasCopyAndDispose = BlockRequiresCopying(Ty); |
| 3860 | |
| 3861 | // FIXME: Move up |
Benjamin Kramer | 1402ce3 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 3862 | llvm::SmallString<36> Name; |
| 3863 | llvm::raw_svector_ostream(Name) << "__Block_byref_" << |
| 3864 | ++UniqueBlockByRefTypeID << '_' << DeclName; |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3865 | RecordDecl *T; |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3866 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, &Idents.get(Name.str())); |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3867 | T->startDefinition(); |
| 3868 | QualType Int32Ty = IntTy; |
| 3869 | assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported"); |
| 3870 | QualType FieldTypes[] = { |
| 3871 | getPointerType(VoidPtrTy), |
| 3872 | getPointerType(getTagDeclType(T)), |
| 3873 | Int32Ty, |
| 3874 | Int32Ty, |
| 3875 | getPointerType(VoidPtrTy), |
| 3876 | getPointerType(VoidPtrTy), |
| 3877 | Ty |
| 3878 | }; |
| 3879 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3880 | StringRef FieldNames[] = { |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3881 | "__isa", |
| 3882 | "__forwarding", |
| 3883 | "__flags", |
| 3884 | "__size", |
| 3885 | "__copy_helper", |
| 3886 | "__destroy_helper", |
| 3887 | DeclName, |
| 3888 | }; |
| 3889 | |
| 3890 | for (size_t i = 0; i < 7; ++i) { |
| 3891 | if (!HasCopyAndDispose && i >=4 && i <= 5) |
| 3892 | continue; |
| 3893 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3894 | SourceLocation(), |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3895 | &Idents.get(FieldNames[i]), |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3896 | FieldTypes[i], /*TInfo=*/0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3897 | /*BitWidth=*/0, /*Mutable=*/false, |
| 3898 | /*HasInit=*/false); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3899 | Field->setAccess(AS_public); |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3900 | T->addDecl(Field); |
| 3901 | } |
| 3902 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3903 | T->completeDefinition(); |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3904 | |
| 3905 | return getPointerType(getTagDeclType(T)); |
Mike Stump | 7fe9cc1 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3906 | } |
| 3907 | |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 3908 | TypedefDecl *ASTContext::getObjCInstanceTypeDecl() { |
| 3909 | if (!ObjCInstanceTypeDecl) |
| 3910 | ObjCInstanceTypeDecl = TypedefDecl::Create(*this, |
| 3911 | getTranslationUnitDecl(), |
| 3912 | SourceLocation(), |
| 3913 | SourceLocation(), |
| 3914 | &Idents.get("instancetype"), |
| 3915 | getTrivialTypeSourceInfo(getObjCIdType())); |
| 3916 | return ObjCInstanceTypeDecl; |
| 3917 | } |
| 3918 | |
Anders Carlsson | 18acd44 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3919 | // This returns true if a type has been typedefed to BOOL: |
| 3920 | // typedef <type> BOOL; |
Chris Lattner | e021899 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 3921 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | 18acd44 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3922 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | 9b1f279 | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 3923 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 3924 | return II->isStr("BOOL"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3925 | |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3926 | return false; |
| 3927 | } |
| 3928 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3929 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3930 | /// purpose. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3931 | CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const { |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 3932 | if (!type->isIncompleteArrayType() && type->isIncompleteType()) |
| 3933 | return CharUnits::Zero(); |
| 3934 | |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3935 | CharUnits sz = getTypeSizeInChars(type); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3936 | |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3937 | // Make all integer and enum types at least as large as an int |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3938 | if (sz.isPositive() && type->isIntegralOrEnumerationType()) |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3939 | sz = std::max(sz, getTypeSizeInChars(IntTy)); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3940 | // Treat arrays as pointers, since that's how they're passed in. |
| 3941 | else if (type->isArrayType()) |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3942 | sz = getTypeSizeInChars(VoidPtrTy); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3943 | return sz; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3944 | } |
| 3945 | |
| 3946 | static inline |
| 3947 | std::string charUnitsToString(const CharUnits &CU) { |
| 3948 | return llvm::itostr(CU.getQuantity()); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3949 | } |
| 3950 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 3951 | /// getObjCEncodingForBlock - Return the encoded type for this block |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3952 | /// declaration. |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 3953 | std::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const { |
| 3954 | std::string S; |
| 3955 | |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3956 | const BlockDecl *Decl = Expr->getBlockDecl(); |
| 3957 | QualType BlockTy = |
| 3958 | Expr->getType()->getAs<BlockPointerType>()->getPointeeType(); |
| 3959 | // Encode result type. |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 3960 | getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3961 | // Compute size of all parameters. |
| 3962 | // Start with computing size of a pointer in number of bytes. |
| 3963 | // FIXME: There might(should) be a better way of doing this computation! |
| 3964 | SourceLocation Loc; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3965 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
| 3966 | CharUnits ParmOffset = PtrSize; |
Fariborz Jahanian | 590c352 | 2010-04-08 18:06:22 +0000 | [diff] [blame] | 3967 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3968 | E = Decl->param_end(); PI != E; ++PI) { |
| 3969 | QualType PType = (*PI)->getType(); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3970 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3971 | assert (sz.isPositive() && "BlockExpr - Incomplete param type"); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3972 | ParmOffset += sz; |
| 3973 | } |
| 3974 | // Size of the argument frame |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3975 | S += charUnitsToString(ParmOffset); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3976 | // Block pointer and offset. |
| 3977 | S += "@?0"; |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3978 | |
| 3979 | // Argument types. |
| 3980 | ParmOffset = PtrSize; |
| 3981 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E = |
| 3982 | Decl->param_end(); PI != E; ++PI) { |
| 3983 | ParmVarDecl *PVDecl = *PI; |
| 3984 | QualType PType = PVDecl->getOriginalType(); |
| 3985 | if (const ArrayType *AT = |
| 3986 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3987 | // Use array's original type only if it has known number of |
| 3988 | // elements. |
| 3989 | if (!isa<ConstantArrayType>(AT)) |
| 3990 | PType = PVDecl->getType(); |
| 3991 | } else if (PType->isFunctionType()) |
| 3992 | PType = PVDecl->getType(); |
| 3993 | getObjCEncodingForType(PType, S); |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3994 | S += charUnitsToString(ParmOffset); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3995 | ParmOffset += getObjCEncodingTypeSize(PType); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3996 | } |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 3997 | |
| 3998 | return S; |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3999 | } |
| 4000 | |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4001 | bool ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl, |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4002 | std::string& S) { |
| 4003 | // Encode result type. |
| 4004 | getObjCEncodingForType(Decl->getResultType(), S); |
| 4005 | CharUnits ParmOffset; |
| 4006 | // Compute size of all parameters. |
| 4007 | for (FunctionDecl::param_const_iterator PI = Decl->param_begin(), |
| 4008 | E = Decl->param_end(); PI != E; ++PI) { |
| 4009 | QualType PType = (*PI)->getType(); |
| 4010 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4011 | if (sz.isZero()) |
| 4012 | return true; |
| 4013 | |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4014 | assert (sz.isPositive() && |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4015 | "getObjCEncodingForFunctionDecl - Incomplete param type"); |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4016 | ParmOffset += sz; |
| 4017 | } |
| 4018 | S += charUnitsToString(ParmOffset); |
| 4019 | ParmOffset = CharUnits::Zero(); |
| 4020 | |
| 4021 | // Argument types. |
| 4022 | for (FunctionDecl::param_const_iterator PI = Decl->param_begin(), |
| 4023 | E = Decl->param_end(); PI != E; ++PI) { |
| 4024 | ParmVarDecl *PVDecl = *PI; |
| 4025 | QualType PType = PVDecl->getOriginalType(); |
| 4026 | if (const ArrayType *AT = |
| 4027 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4028 | // Use array's original type only if it has known number of |
| 4029 | // elements. |
| 4030 | if (!isa<ConstantArrayType>(AT)) |
| 4031 | PType = PVDecl->getType(); |
| 4032 | } else if (PType->isFunctionType()) |
| 4033 | PType = PVDecl->getType(); |
| 4034 | getObjCEncodingForType(PType, S); |
| 4035 | S += charUnitsToString(ParmOffset); |
| 4036 | ParmOffset += getObjCEncodingTypeSize(PType); |
| 4037 | } |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4038 | |
| 4039 | return false; |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4040 | } |
| 4041 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4042 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4043 | /// declaration. |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4044 | bool ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4045 | std::string& S) const { |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4046 | // FIXME: This is not very efficient. |
Fariborz Jahanian | ac73ff8 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 4047 | // Encode type qualifer, 'in', 'inout', etc. for the return type. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4048 | getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4049 | // Encode result type. |
Daniel Dunbar | fc1066d | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 4050 | getObjCEncodingForType(Decl->getResultType(), S); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4051 | // Compute size of all parameters. |
| 4052 | // Start with computing size of a pointer in number of bytes. |
| 4053 | // FIXME: There might(should) be a better way of doing this computation! |
| 4054 | SourceLocation Loc; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4055 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4056 | // The first two arguments (self and _cmd) are pointers; account for |
| 4057 | // their size. |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4058 | CharUnits ParmOffset = 2 * PtrSize; |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4059 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | d9235db | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 4060 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 4061 | QualType PType = (*PI)->getType(); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4062 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4063 | if (sz.isZero()) |
| 4064 | return true; |
| 4065 | |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4066 | assert (sz.isPositive() && |
| 4067 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4068 | ParmOffset += sz; |
| 4069 | } |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4070 | S += charUnitsToString(ParmOffset); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4071 | S += "@0:"; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4072 | S += charUnitsToString(PtrSize); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4073 | |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4074 | // Argument types. |
| 4075 | ParmOffset = 2 * PtrSize; |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4076 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | d9235db | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 4077 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4078 | const ParmVarDecl *PVDecl = *PI; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4079 | QualType PType = PVDecl->getOriginalType(); |
Fariborz Jahanian | a0befc0 | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 4080 | if (const ArrayType *AT = |
Steve Naroff | e4e55d2 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 4081 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4082 | // Use array's original type only if it has known number of |
| 4083 | // elements. |
Steve Naroff | 323827e | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 4084 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | e4e55d2 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 4085 | PType = PVDecl->getType(); |
| 4086 | } else if (PType->isFunctionType()) |
| 4087 | PType = PVDecl->getType(); |
Fariborz Jahanian | ac73ff8 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 4088 | // Process argument qualifiers for user supplied arguments; such as, |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4089 | // 'in', 'inout', etc. |
Fariborz Jahanian | a0befc0 | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 4090 | getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S); |
Daniel Dunbar | fc1066d | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 4091 | getObjCEncodingForType(PType, S); |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4092 | S += charUnitsToString(ParmOffset); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4093 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4094 | } |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4095 | |
| 4096 | return false; |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4097 | } |
| 4098 | |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4099 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 2f85a64 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4100 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4101 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 4102 | /// NULL when getting encodings for protocol properties. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4103 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 4104 | /// attributes readonly and bycopy are encoded as single characters. The |
| 4105 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 4106 | /// encoded as single characters, followed by an identifier. Property types |
| 4107 | /// are also encoded as a parametrized attribute. The characters used to encode |
Fariborz Jahanian | 2f85a64 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4108 | /// these attributes are defined by the following enumeration: |
| 4109 | /// @code |
| 4110 | /// enum PropertyAttributes { |
| 4111 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 4112 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 4113 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 4114 | /// kPropertyDynamic = 'D', // property is dynamic |
| 4115 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 4116 | /// kPropertySetter = 'S', // followed by setter selector name |
| 4117 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
| 4118 | /// kPropertyType = 't' // followed by old-style type encoding. |
| 4119 | /// kPropertyWeak = 'W' // 'weak' property |
| 4120 | /// kPropertyStrong = 'P' // property GC'able |
| 4121 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 4122 | /// }; |
| 4123 | /// @endcode |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4124 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4125 | const Decl *Container, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4126 | std::string& S) const { |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4127 | // Collect information from the property implementation decl(s). |
| 4128 | bool Dynamic = false; |
| 4129 | ObjCPropertyImplDecl *SynthesizePID = 0; |
| 4130 | |
| 4131 | // FIXME: Duplicated code due to poor abstraction. |
| 4132 | if (Container) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4133 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4134 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
| 4135 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4136 | i = CID->propimpl_begin(), e = CID->propimpl_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4137 | i != e; ++i) { |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4138 | ObjCPropertyImplDecl *PID = *i; |
| 4139 | if (PID->getPropertyDecl() == PD) { |
| 4140 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 4141 | Dynamic = true; |
| 4142 | } else { |
| 4143 | SynthesizePID = PID; |
| 4144 | } |
| 4145 | } |
| 4146 | } |
| 4147 | } else { |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4148 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4149 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4150 | i = OID->propimpl_begin(), e = OID->propimpl_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4151 | i != e; ++i) { |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4152 | ObjCPropertyImplDecl *PID = *i; |
| 4153 | if (PID->getPropertyDecl() == PD) { |
| 4154 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 4155 | Dynamic = true; |
| 4156 | } else { |
| 4157 | SynthesizePID = PID; |
| 4158 | } |
| 4159 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4160 | } |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4161 | } |
| 4162 | } |
| 4163 | |
| 4164 | // FIXME: This is not very efficient. |
| 4165 | S = "T"; |
| 4166 | |
| 4167 | // Encode result type. |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4168 | // GCC has some special rules regarding encoding of properties which |
| 4169 | // closely resembles encoding of ivars. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4170 | getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0, |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4171 | true /* outermost type */, |
| 4172 | true /* encoding for property */); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4173 | |
| 4174 | if (PD->isReadOnly()) { |
| 4175 | S += ",R"; |
| 4176 | } else { |
| 4177 | switch (PD->getSetterKind()) { |
| 4178 | case ObjCPropertyDecl::Assign: break; |
| 4179 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4180 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
Fariborz Jahanian | 70a315c | 2011-08-12 20:47:08 +0000 | [diff] [blame] | 4181 | case ObjCPropertyDecl::Weak: S += ",W"; break; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4182 | } |
| 4183 | } |
| 4184 | |
| 4185 | // It really isn't clear at all what this means, since properties |
| 4186 | // are "dynamic by default". |
| 4187 | if (Dynamic) |
| 4188 | S += ",D"; |
| 4189 | |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4190 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 4191 | S += ",N"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4192 | |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4193 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 4194 | S += ",G"; |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4195 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4196 | } |
| 4197 | |
| 4198 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 4199 | S += ",S"; |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4200 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4201 | } |
| 4202 | |
| 4203 | if (SynthesizePID) { |
| 4204 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 4205 | S += ",V"; |
Chris Lattner | 1cbaacc | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 4206 | S += OID->getNameAsString(); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4207 | } |
| 4208 | |
| 4209 | // FIXME: OBJCGC: weak & strong |
| 4210 | } |
| 4211 | |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4212 | /// getLegacyIntegralTypeEncoding - |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4213 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
| 4214 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4215 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
| 4216 | /// |
| 4217 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 212005c | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 4218 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4219 | if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) { |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4220 | if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4221 | PointeeTy = UnsignedIntTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4222 | else |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4223 | if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4224 | PointeeTy = IntTy; |
| 4225 | } |
| 4226 | } |
| 4227 | } |
| 4228 | |
Fariborz Jahanian | 0a71ad2 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4229 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4230 | const FieldDecl *Field) const { |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4231 | // We follow the behavior of gcc, expanding structures which are |
| 4232 | // directly pointed to, and expanding embedded structures. Note that |
| 4233 | // these rules are sufficient to prevent recursive encoding of the |
| 4234 | // same type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4235 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
Fariborz Jahanian | daef00b | 2008-12-22 23:22:27 +0000 | [diff] [blame] | 4236 | true /* outermost type */); |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4237 | } |
| 4238 | |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4239 | static char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) { |
| 4240 | switch (T->getAs<BuiltinType>()->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4241 | default: llvm_unreachable("Unhandled builtin type kind"); |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4242 | case BuiltinType::Void: return 'v'; |
| 4243 | case BuiltinType::Bool: return 'B'; |
| 4244 | case BuiltinType::Char_U: |
| 4245 | case BuiltinType::UChar: return 'C'; |
| 4246 | case BuiltinType::UShort: return 'S'; |
| 4247 | case BuiltinType::UInt: return 'I'; |
| 4248 | case BuiltinType::ULong: |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4249 | return C->getIntWidth(T) == 32 ? 'L' : 'Q'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4250 | case BuiltinType::UInt128: return 'T'; |
| 4251 | case BuiltinType::ULongLong: return 'Q'; |
| 4252 | case BuiltinType::Char_S: |
| 4253 | case BuiltinType::SChar: return 'c'; |
| 4254 | case BuiltinType::Short: return 's'; |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 4255 | case BuiltinType::WChar_S: |
| 4256 | case BuiltinType::WChar_U: |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4257 | case BuiltinType::Int: return 'i'; |
| 4258 | case BuiltinType::Long: |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4259 | return C->getIntWidth(T) == 32 ? 'l' : 'q'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4260 | case BuiltinType::LongLong: return 'q'; |
| 4261 | case BuiltinType::Int128: return 't'; |
| 4262 | case BuiltinType::Float: return 'f'; |
| 4263 | case BuiltinType::Double: return 'd'; |
Daniel Dunbar | 7cba5a7 | 2010-10-11 21:13:48 +0000 | [diff] [blame] | 4264 | case BuiltinType::LongDouble: return 'D'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4265 | } |
| 4266 | } |
| 4267 | |
Douglas Gregor | 8b7d403 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4268 | static char ObjCEncodingForEnumType(const ASTContext *C, const EnumType *ET) { |
| 4269 | EnumDecl *Enum = ET->getDecl(); |
| 4270 | |
| 4271 | // The encoding of an non-fixed enum type is always 'i', regardless of size. |
| 4272 | if (!Enum->isFixed()) |
| 4273 | return 'i'; |
| 4274 | |
| 4275 | // The encoding of a fixed enum type matches its fixed underlying type. |
| 4276 | return ObjCEncodingForPrimitiveKind(C, Enum->getIntegerType()); |
| 4277 | } |
| 4278 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4279 | static void EncodeBitField(const ASTContext *Ctx, std::string& S, |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4280 | QualType T, const FieldDecl *FD) { |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4281 | assert(FD->isBitField() && "not a bitfield - getObjCEncodingForTypeImpl"); |
Fariborz Jahanian | 5c76772 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4282 | S += 'b'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4283 | // The NeXT runtime encodes bit fields as b followed by the number of bits. |
| 4284 | // The GNU runtime requires more information; bitfields are encoded as b, |
| 4285 | // then the offset (in bits) of the first element, then the type of the |
| 4286 | // bitfield, then the size in bits. For example, in this structure: |
| 4287 | // |
| 4288 | // struct |
| 4289 | // { |
| 4290 | // int integer; |
| 4291 | // int flags:2; |
| 4292 | // }; |
| 4293 | // On a 32-bit system, the encoding for flags would be b2 for the NeXT |
| 4294 | // runtime, but b32i2 for the GNU runtime. The reason for this extra |
| 4295 | // information is not especially sensible, but we're stuck with it for |
| 4296 | // compatibility with GCC, although providing it breaks anything that |
| 4297 | // actually uses runtime introspection and wants to work on both runtimes... |
| 4298 | if (!Ctx->getLangOptions().NeXTRuntime) { |
| 4299 | const RecordDecl *RD = FD->getParent(); |
| 4300 | const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD); |
Eli Friedman | a3c122d | 2011-07-07 01:54:01 +0000 | [diff] [blame] | 4301 | S += llvm::utostr(RL.getFieldOffset(FD->getFieldIndex())); |
Douglas Gregor | 8b7d403 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4302 | if (const EnumType *ET = T->getAs<EnumType>()) |
| 4303 | S += ObjCEncodingForEnumType(Ctx, ET); |
David Chisnall | 6f0a7d2 | 2010-12-26 20:12:30 +0000 | [diff] [blame] | 4304 | else |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4305 | S += ObjCEncodingForPrimitiveKind(Ctx, T); |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4306 | } |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4307 | S += llvm::utostr(FD->getBitWidthValue(*Ctx)); |
Fariborz Jahanian | 5c76772 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4308 | } |
| 4309 | |
Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 4310 | // FIXME: Use SmallString for accumulating string. |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4311 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 4312 | bool ExpandPointedToStructures, |
| 4313 | bool ExpandStructures, |
Daniel Dunbar | c040ce4 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 4314 | const FieldDecl *FD, |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4315 | bool OutermostType, |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4316 | bool EncodingProperty, |
| 4317 | bool StructField) const { |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4318 | if (T->getAs<BuiltinType>()) { |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4319 | if (FD && FD->isBitField()) |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4320 | return EncodeBitField(this, S, T, FD); |
| 4321 | S += ObjCEncodingForPrimitiveKind(this, T); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4322 | return; |
| 4323 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4324 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4325 | if (const ComplexType *CT = T->getAs<ComplexType>()) { |
Anders Carlsson | 39b2e13 | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 4326 | S += 'j'; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4327 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false, |
Anders Carlsson | 39b2e13 | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 4328 | false); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4329 | return; |
| 4330 | } |
Fariborz Jahanian | d25c219 | 2009-11-23 20:40:50 +0000 | [diff] [blame] | 4331 | |
Fariborz Jahanian | 9ffd706 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4332 | // encoding for pointer or r3eference types. |
| 4333 | QualType PointeeTy; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4334 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Fariborz Jahanian | 89b660c | 2009-11-30 18:43:52 +0000 | [diff] [blame] | 4335 | if (PT->isObjCSelType()) { |
| 4336 | S += ':'; |
| 4337 | return; |
| 4338 | } |
Fariborz Jahanian | 9ffd706 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4339 | PointeeTy = PT->getPointeeType(); |
| 4340 | } |
| 4341 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 4342 | PointeeTy = RT->getPointeeType(); |
| 4343 | if (!PointeeTy.isNull()) { |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4344 | bool isReadOnly = false; |
| 4345 | // For historical/compatibility reasons, the read-only qualifier of the |
| 4346 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 4347 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4348 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 212005c | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 4349 | if (isa<TypedefType>(T.getTypePtr())) { |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4350 | if (OutermostType && T.isConstQualified()) { |
| 4351 | isReadOnly = true; |
| 4352 | S += 'r'; |
| 4353 | } |
Mike Stump | e9c6ffc | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 4354 | } else if (OutermostType) { |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4355 | QualType P = PointeeTy; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4356 | while (P->getAs<PointerType>()) |
| 4357 | P = P->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4358 | if (P.isConstQualified()) { |
| 4359 | isReadOnly = true; |
| 4360 | S += 'r'; |
| 4361 | } |
| 4362 | } |
| 4363 | if (isReadOnly) { |
| 4364 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 4365 | // combinations need to be rearranged. |
| 4366 | // Rewrite "in const" from "nr" to "rn" |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4367 | if (StringRef(S).endswith("nr")) |
Benjamin Kramer | 2e3197e | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 4368 | S.replace(S.end()-2, S.end(), "rn"); |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4369 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4370 | |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4371 | if (PointeeTy->isCharType()) { |
| 4372 | // char pointer types should be encoded as '*' unless it is a |
| 4373 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | 18acd44 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 4374 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4375 | S += '*'; |
| 4376 | return; |
| 4377 | } |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4378 | } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) { |
Steve Naroff | 3de6b70 | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 4379 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 4380 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 4381 | S += '#'; |
| 4382 | return; |
| 4383 | } |
| 4384 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 4385 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 4386 | S += '@'; |
| 4387 | return; |
| 4388 | } |
| 4389 | // fall through... |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4390 | } |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4391 | S += '^'; |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4392 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 4393 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4394 | getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4395 | NULL); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4396 | return; |
| 4397 | } |
Fariborz Jahanian | 9ffd706 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4398 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4399 | if (const ArrayType *AT = |
| 4400 | // Ignore type qualifiers etc. |
| 4401 | dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) { |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4402 | if (isa<IncompleteArrayType>(AT) && !StructField) { |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4403 | // Incomplete arrays are encoded as a pointer to the array element. |
| 4404 | S += '^'; |
| 4405 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4406 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4407 | false, ExpandStructures, FD); |
| 4408 | } else { |
| 4409 | S += '['; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4410 | |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4411 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
| 4412 | if (getTypeSize(CAT->getElementType()) == 0) |
| 4413 | S += '0'; |
| 4414 | else |
| 4415 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 4416 | } else { |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4417 | //Variable length arrays are encoded as a regular array with 0 elements. |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4418 | assert((isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) && |
| 4419 | "Unknown array type!"); |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4420 | S += '0'; |
| 4421 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4422 | |
| 4423 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4424 | false, ExpandStructures, FD); |
| 4425 | S += ']'; |
| 4426 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4427 | return; |
| 4428 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4429 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4430 | if (T->getAs<FunctionType>()) { |
Anders Carlsson | df4cc61 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 4431 | S += '?'; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4432 | return; |
| 4433 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4434 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4435 | if (const RecordType *RTy = T->getAs<RecordType>()) { |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4436 | RecordDecl *RDecl = RTy->getDecl(); |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4437 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 40cac77 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 4438 | // Anonymous structures print as '?' |
| 4439 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 4440 | S += II->getName(); |
Fariborz Jahanian | c515820 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 4441 | if (ClassTemplateSpecializationDecl *Spec |
| 4442 | = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) { |
| 4443 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 4444 | std::string TemplateArgsStr |
| 4445 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4446 | TemplateArgs.data(), |
| 4447 | TemplateArgs.size(), |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 4448 | (*this).getPrintingPolicy()); |
Fariborz Jahanian | c515820 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 4449 | |
| 4450 | S += TemplateArgsStr; |
| 4451 | } |
Daniel Dunbar | 40cac77 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 4452 | } else { |
| 4453 | S += '?'; |
| 4454 | } |
Daniel Dunbar | fc1066d | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 4455 | if (ExpandStructures) { |
Fariborz Jahanian | 0a71ad2 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4456 | S += '='; |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4457 | if (!RDecl->isUnion()) { |
| 4458 | getObjCEncodingForStructureImpl(RDecl, S, FD); |
| 4459 | } else { |
| 4460 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 4461 | FieldEnd = RDecl->field_end(); |
| 4462 | Field != FieldEnd; ++Field) { |
| 4463 | if (FD) { |
| 4464 | S += '"'; |
| 4465 | S += Field->getNameAsString(); |
| 4466 | S += '"'; |
| 4467 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4468 | |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4469 | // Special case bit-fields. |
| 4470 | if (Field->isBitField()) { |
| 4471 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
| 4472 | (*Field)); |
| 4473 | } else { |
| 4474 | QualType qt = Field->getType(); |
| 4475 | getLegacyIntegralTypeEncoding(qt); |
| 4476 | getObjCEncodingForTypeImpl(qt, S, false, true, |
| 4477 | FD, /*OutermostType*/false, |
| 4478 | /*EncodingProperty*/false, |
| 4479 | /*StructField*/true); |
| 4480 | } |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4481 | } |
Fariborz Jahanian | 0a71ad2 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4482 | } |
Fariborz Jahanian | bc92fd7 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 4483 | } |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4484 | S += RDecl->isUnion() ? ')' : '}'; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4485 | return; |
| 4486 | } |
Fariborz Jahanian | e0587be | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 4487 | |
Douglas Gregor | 8b7d403 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4488 | if (const EnumType *ET = T->getAs<EnumType>()) { |
Fariborz Jahanian | 5c76772 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4489 | if (FD && FD->isBitField()) |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4490 | EncodeBitField(this, S, T, FD); |
Fariborz Jahanian | 5c76772 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4491 | else |
Douglas Gregor | 8b7d403 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4492 | S += ObjCEncodingForEnumType(this, ET); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4493 | return; |
| 4494 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4495 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4496 | if (T->isBlockPointerType()) { |
Steve Naroff | 49140cb | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 4497 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4498 | return; |
| 4499 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4500 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4501 | // Ignore protocol qualifiers when mangling at this level. |
| 4502 | if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>()) |
| 4503 | T = OT->getBaseType(); |
| 4504 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4505 | if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) { |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4506 | // @encode(class_name) |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4507 | ObjCInterfaceDecl *OI = OIT->getDecl(); |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4508 | S += '{'; |
| 4509 | const IdentifierInfo *II = OI->getIdentifier(); |
| 4510 | S += II->getName(); |
| 4511 | S += '='; |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4512 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4513 | DeepCollectObjCIvars(OI, true, Ivars); |
| 4514 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) { |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4515 | const FieldDecl *Field = cast<FieldDecl>(Ivars[i]); |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4516 | if (Field->isBitField()) |
| 4517 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field); |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4518 | else |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4519 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD); |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4520 | } |
| 4521 | S += '}'; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4522 | return; |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4523 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4524 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4525 | if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4526 | if (OPT->isObjCIdType()) { |
| 4527 | S += '@'; |
| 4528 | return; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4529 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4530 | |
Steve Naroff | f0c8611 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4531 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
| 4532 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
| 4533 | // Since this is a binary compatibility issue, need to consult with runtime |
| 4534 | // folks. Fortunately, this is a *very* obsure construct. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4535 | S += '#'; |
| 4536 | return; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4537 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4538 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4539 | if (OPT->isObjCQualifiedIdType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4540 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4541 | ExpandPointedToStructures, |
| 4542 | ExpandStructures, FD); |
| 4543 | if (FD || EncodingProperty) { |
| 4544 | // Note that we do extended encoding of protocol qualifer list |
| 4545 | // Only when doing ivar or property encoding. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4546 | S += '"'; |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4547 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 4548 | E = OPT->qual_end(); I != E; ++I) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4549 | S += '<'; |
| 4550 | S += (*I)->getNameAsString(); |
| 4551 | S += '>'; |
| 4552 | } |
| 4553 | S += '"'; |
| 4554 | } |
| 4555 | return; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4556 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4557 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4558 | QualType PointeeTy = OPT->getPointeeType(); |
| 4559 | if (!EncodingProperty && |
| 4560 | isa<TypedefType>(PointeeTy.getTypePtr())) { |
| 4561 | // Another historical/compatibility reason. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4562 | // We encode the underlying type which comes out as |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4563 | // {...}; |
| 4564 | S += '^'; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4565 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 4566 | false, ExpandPointedToStructures, |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4567 | NULL); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4568 | return; |
| 4569 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4570 | |
| 4571 | S += '@'; |
Steve Naroff | f0c8611 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4572 | if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) { |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4573 | S += '"'; |
Steve Naroff | f0c8611 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4574 | S += OPT->getInterfaceDecl()->getIdentifier()->getName(); |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4575 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 4576 | E = OPT->qual_end(); I != E; ++I) { |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4577 | S += '<'; |
| 4578 | S += (*I)->getNameAsString(); |
| 4579 | S += '>'; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4580 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4581 | S += '"'; |
| 4582 | } |
| 4583 | return; |
| 4584 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4585 | |
John McCall | a9e6e8d | 2010-05-17 23:56:34 +0000 | [diff] [blame] | 4586 | // gcc just blithely ignores member pointers. |
| 4587 | // TODO: maybe there should be a mangling for these |
| 4588 | if (T->getAs<MemberPointerType>()) |
| 4589 | return; |
Fariborz Jahanian | e0587be | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 4590 | |
| 4591 | if (T->isVectorType()) { |
| 4592 | // This matches gcc's encoding, even though technically it is |
| 4593 | // insufficient. |
| 4594 | // FIXME. We should do a better job than gcc. |
| 4595 | return; |
| 4596 | } |
| 4597 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4598 | llvm_unreachable("@encode for type not implemented!"); |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4599 | } |
| 4600 | |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4601 | void ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl, |
| 4602 | std::string &S, |
| 4603 | const FieldDecl *FD, |
| 4604 | bool includeVBases) const { |
| 4605 | assert(RDecl && "Expected non-null RecordDecl"); |
| 4606 | assert(!RDecl->isUnion() && "Should not be called for unions"); |
| 4607 | if (!RDecl->getDefinition()) |
| 4608 | return; |
| 4609 | |
| 4610 | CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(RDecl); |
| 4611 | std::multimap<uint64_t, NamedDecl *> FieldOrBaseOffsets; |
| 4612 | const ASTRecordLayout &layout = getASTRecordLayout(RDecl); |
| 4613 | |
| 4614 | if (CXXRec) { |
| 4615 | for (CXXRecordDecl::base_class_iterator |
| 4616 | BI = CXXRec->bases_begin(), |
| 4617 | BE = CXXRec->bases_end(); BI != BE; ++BI) { |
| 4618 | if (!BI->isVirtual()) { |
| 4619 | CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 95a76f3 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 4620 | if (base->isEmpty()) |
| 4621 | continue; |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4622 | uint64_t offs = layout.getBaseClassOffsetInBits(base); |
| 4623 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 4624 | std::make_pair(offs, base)); |
| 4625 | } |
| 4626 | } |
| 4627 | } |
| 4628 | |
| 4629 | unsigned i = 0; |
| 4630 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 4631 | FieldEnd = RDecl->field_end(); |
| 4632 | Field != FieldEnd; ++Field, ++i) { |
| 4633 | uint64_t offs = layout.getFieldOffset(i); |
| 4634 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 4635 | std::make_pair(offs, *Field)); |
| 4636 | } |
| 4637 | |
| 4638 | if (CXXRec && includeVBases) { |
| 4639 | for (CXXRecordDecl::base_class_iterator |
| 4640 | BI = CXXRec->vbases_begin(), |
| 4641 | BE = CXXRec->vbases_end(); BI != BE; ++BI) { |
| 4642 | CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 95a76f3 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 4643 | if (base->isEmpty()) |
| 4644 | continue; |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4645 | uint64_t offs = layout.getVBaseClassOffsetInBits(base); |
Argyrios Kyrtzidis | 81c0b5c | 2011-09-26 18:14:24 +0000 | [diff] [blame] | 4646 | if (FieldOrBaseOffsets.find(offs) == FieldOrBaseOffsets.end()) |
| 4647 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.end(), |
| 4648 | std::make_pair(offs, base)); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4649 | } |
| 4650 | } |
| 4651 | |
| 4652 | CharUnits size; |
| 4653 | if (CXXRec) { |
| 4654 | size = includeVBases ? layout.getSize() : layout.getNonVirtualSize(); |
| 4655 | } else { |
| 4656 | size = layout.getSize(); |
| 4657 | } |
| 4658 | |
| 4659 | uint64_t CurOffs = 0; |
| 4660 | std::multimap<uint64_t, NamedDecl *>::iterator |
| 4661 | CurLayObj = FieldOrBaseOffsets.begin(); |
| 4662 | |
Argyrios Kyrtzidis | c7e50c5 | 2011-08-22 16:03:14 +0000 | [diff] [blame] | 4663 | if ((CurLayObj != FieldOrBaseOffsets.end() && CurLayObj->first != 0) || |
| 4664 | (CurLayObj == FieldOrBaseOffsets.end() && |
| 4665 | CXXRec && CXXRec->isDynamicClass())) { |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4666 | assert(CXXRec && CXXRec->isDynamicClass() && |
| 4667 | "Offset 0 was empty but no VTable ?"); |
| 4668 | if (FD) { |
| 4669 | S += "\"_vptr$"; |
| 4670 | std::string recname = CXXRec->getNameAsString(); |
| 4671 | if (recname.empty()) recname = "?"; |
| 4672 | S += recname; |
| 4673 | S += '"'; |
| 4674 | } |
| 4675 | S += "^^?"; |
| 4676 | CurOffs += getTypeSize(VoidPtrTy); |
| 4677 | } |
| 4678 | |
| 4679 | if (!RDecl->hasFlexibleArrayMember()) { |
| 4680 | // Mark the end of the structure. |
| 4681 | uint64_t offs = toBits(size); |
| 4682 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 4683 | std::make_pair(offs, (NamedDecl*)0)); |
| 4684 | } |
| 4685 | |
| 4686 | for (; CurLayObj != FieldOrBaseOffsets.end(); ++CurLayObj) { |
| 4687 | assert(CurOffs <= CurLayObj->first); |
| 4688 | |
| 4689 | if (CurOffs < CurLayObj->first) { |
| 4690 | uint64_t padding = CurLayObj->first - CurOffs; |
| 4691 | // FIXME: There doesn't seem to be a way to indicate in the encoding that |
| 4692 | // packing/alignment of members is different that normal, in which case |
| 4693 | // the encoding will be out-of-sync with the real layout. |
| 4694 | // If the runtime switches to just consider the size of types without |
| 4695 | // taking into account alignment, we could make padding explicit in the |
| 4696 | // encoding (e.g. using arrays of chars). The encoding strings would be |
| 4697 | // longer then though. |
| 4698 | CurOffs += padding; |
| 4699 | } |
| 4700 | |
| 4701 | NamedDecl *dcl = CurLayObj->second; |
| 4702 | if (dcl == 0) |
| 4703 | break; // reached end of structure. |
| 4704 | |
| 4705 | if (CXXRecordDecl *base = dyn_cast<CXXRecordDecl>(dcl)) { |
| 4706 | // We expand the bases without their virtual bases since those are going |
| 4707 | // in the initial structure. Note that this differs from gcc which |
| 4708 | // expands virtual bases each time one is encountered in the hierarchy, |
| 4709 | // making the encoding type bigger than it really is. |
| 4710 | getObjCEncodingForStructureImpl(base, S, FD, /*includeVBases*/false); |
Argyrios Kyrtzidis | 95a76f3 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 4711 | assert(!base->isEmpty()); |
| 4712 | CurOffs += toBits(getASTRecordLayout(base).getNonVirtualSize()); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4713 | } else { |
| 4714 | FieldDecl *field = cast<FieldDecl>(dcl); |
| 4715 | if (FD) { |
| 4716 | S += '"'; |
| 4717 | S += field->getNameAsString(); |
| 4718 | S += '"'; |
| 4719 | } |
| 4720 | |
| 4721 | if (field->isBitField()) { |
| 4722 | EncodeBitField(this, S, field->getType(), field); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4723 | CurOffs += field->getBitWidthValue(*this); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4724 | } else { |
| 4725 | QualType qt = field->getType(); |
| 4726 | getLegacyIntegralTypeEncoding(qt); |
| 4727 | getObjCEncodingForTypeImpl(qt, S, false, true, FD, |
| 4728 | /*OutermostType*/false, |
| 4729 | /*EncodingProperty*/false, |
| 4730 | /*StructField*/true); |
| 4731 | CurOffs += getTypeSize(field->getType()); |
| 4732 | } |
| 4733 | } |
| 4734 | } |
| 4735 | } |
| 4736 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4737 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ac73ff8 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 4738 | std::string& S) const { |
| 4739 | if (QT & Decl::OBJC_TQ_In) |
| 4740 | S += 'n'; |
| 4741 | if (QT & Decl::OBJC_TQ_Inout) |
| 4742 | S += 'N'; |
| 4743 | if (QT & Decl::OBJC_TQ_Out) |
| 4744 | S += 'o'; |
| 4745 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 4746 | S += 'O'; |
| 4747 | if (QT & Decl::OBJC_TQ_Byref) |
| 4748 | S += 'R'; |
| 4749 | if (QT & Decl::OBJC_TQ_Oneway) |
| 4750 | S += 'V'; |
| 4751 | } |
| 4752 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4753 | void ASTContext::setBuiltinVaListType(QualType T) { |
Anders Carlsson | 87c149b | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 4754 | assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4755 | |
Anders Carlsson | 87c149b | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 4756 | BuiltinVaListType = T; |
| 4757 | } |
| 4758 | |
Douglas Gregor | 3ea7269 | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 4759 | TypedefDecl *ASTContext::getObjCIdDecl() const { |
| 4760 | if (!ObjCIdDecl) { |
| 4761 | QualType T = getObjCObjectType(ObjCBuiltinIdTy, 0, 0); |
| 4762 | T = getObjCObjectPointerType(T); |
| 4763 | TypeSourceInfo *IdInfo = getTrivialTypeSourceInfo(T); |
| 4764 | ObjCIdDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 4765 | getTranslationUnitDecl(), |
| 4766 | SourceLocation(), SourceLocation(), |
| 4767 | &Idents.get("id"), IdInfo); |
| 4768 | } |
| 4769 | |
| 4770 | return ObjCIdDecl; |
Steve Naroff | 66e9f33 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 4771 | } |
| 4772 | |
Douglas Gregor | 52e0280 | 2011-08-12 06:17:30 +0000 | [diff] [blame] | 4773 | TypedefDecl *ASTContext::getObjCSelDecl() const { |
| 4774 | if (!ObjCSelDecl) { |
| 4775 | QualType SelT = getPointerType(ObjCBuiltinSelTy); |
| 4776 | TypeSourceInfo *SelInfo = getTrivialTypeSourceInfo(SelT); |
| 4777 | ObjCSelDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 4778 | getTranslationUnitDecl(), |
| 4779 | SourceLocation(), SourceLocation(), |
| 4780 | &Idents.get("SEL"), SelInfo); |
| 4781 | } |
| 4782 | return ObjCSelDecl; |
Fariborz Jahanian | 4bef462 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 4783 | } |
| 4784 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4785 | void ASTContext::setObjCProtoType(QualType QT) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4786 | ObjCProtoType = QT; |
Fariborz Jahanian | a32aaef | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 4787 | } |
| 4788 | |
Douglas Gregor | 0a58618 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 4789 | TypedefDecl *ASTContext::getObjCClassDecl() const { |
| 4790 | if (!ObjCClassDecl) { |
| 4791 | QualType T = getObjCObjectType(ObjCBuiltinClassTy, 0, 0); |
| 4792 | T = getObjCObjectPointerType(T); |
| 4793 | TypeSourceInfo *ClassInfo = getTrivialTypeSourceInfo(T); |
| 4794 | ObjCClassDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 4795 | getTranslationUnitDecl(), |
| 4796 | SourceLocation(), SourceLocation(), |
| 4797 | &Idents.get("Class"), ClassInfo); |
| 4798 | } |
| 4799 | |
| 4800 | return ObjCClassDecl; |
Anders Carlsson | f56a7ae | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 4801 | } |
| 4802 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4803 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4804 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | f73b784 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 4805 | "'NSConstantString' type already set!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4806 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4807 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | f73b784 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 4808 | } |
| 4809 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4810 | /// \brief Retrieve the template name that corresponds to a non-empty |
| 4811 | /// lookup. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4812 | TemplateName |
| 4813 | ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin, |
| 4814 | UnresolvedSetIterator End) const { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4815 | unsigned size = End - Begin; |
| 4816 | assert(size > 1 && "set is not overloaded!"); |
| 4817 | |
| 4818 | void *memory = Allocate(sizeof(OverloadedTemplateStorage) + |
| 4819 | size * sizeof(FunctionTemplateDecl*)); |
| 4820 | OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size); |
| 4821 | |
| 4822 | NamedDecl **Storage = OT->getStorage(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4823 | for (UnresolvedSetIterator I = Begin; I != End; ++I) { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4824 | NamedDecl *D = *I; |
| 4825 | assert(isa<FunctionTemplateDecl>(D) || |
| 4826 | (isa<UsingShadowDecl>(D) && |
| 4827 | isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))); |
| 4828 | *Storage++ = D; |
| 4829 | } |
| 4830 | |
| 4831 | return TemplateName(OT); |
| 4832 | } |
| 4833 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4834 | /// \brief Retrieve the template name that represents a qualified |
| 4835 | /// template name such as \c std::vector. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4836 | TemplateName |
| 4837 | ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
| 4838 | bool TemplateKeyword, |
| 4839 | TemplateDecl *Template) const { |
Douglas Gregor | e29139c | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 4840 | assert(NNS && "Missing nested-name-specifier in qualified template name"); |
| 4841 | |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4842 | // FIXME: Canonicalization? |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4843 | llvm::FoldingSetNodeID ID; |
| 4844 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 4845 | |
| 4846 | void *InsertPos = 0; |
| 4847 | QualifiedTemplateName *QTN = |
| 4848 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4849 | if (!QTN) { |
| 4850 | QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); |
| 4851 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 4852 | } |
| 4853 | |
| 4854 | return TemplateName(QTN); |
| 4855 | } |
| 4856 | |
| 4857 | /// \brief Retrieve the template name that represents a dependent |
| 4858 | /// template name such as \c MetaFun::template apply. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4859 | TemplateName |
| 4860 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
| 4861 | const IdentifierInfo *Name) const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4862 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 308047d | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 4863 | "Nested name specifier must be dependent"); |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4864 | |
| 4865 | llvm::FoldingSetNodeID ID; |
| 4866 | DependentTemplateName::Profile(ID, NNS, Name); |
| 4867 | |
| 4868 | void *InsertPos = 0; |
| 4869 | DependentTemplateName *QTN = |
| 4870 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4871 | |
| 4872 | if (QTN) |
| 4873 | return TemplateName(QTN); |
| 4874 | |
| 4875 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 4876 | if (CanonNNS == NNS) { |
| 4877 | QTN = new (*this,4) DependentTemplateName(NNS, Name); |
| 4878 | } else { |
| 4879 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
| 4880 | QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4881 | DependentTemplateName *CheckQTN = |
| 4882 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4883 | assert(!CheckQTN && "Dependent type name canonicalization broken"); |
| 4884 | (void)CheckQTN; |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4885 | } |
| 4886 | |
| 4887 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 4888 | return TemplateName(QTN); |
| 4889 | } |
| 4890 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4891 | /// \brief Retrieve the template name that represents a dependent |
| 4892 | /// template name such as \c MetaFun::template operator+. |
| 4893 | TemplateName |
| 4894 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4895 | OverloadedOperatorKind Operator) const { |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4896 | assert((!NNS || NNS->isDependent()) && |
| 4897 | "Nested name specifier must be dependent"); |
| 4898 | |
| 4899 | llvm::FoldingSetNodeID ID; |
| 4900 | DependentTemplateName::Profile(ID, NNS, Operator); |
| 4901 | |
| 4902 | void *InsertPos = 0; |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4903 | DependentTemplateName *QTN |
| 4904 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4905 | |
| 4906 | if (QTN) |
| 4907 | return TemplateName(QTN); |
| 4908 | |
| 4909 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 4910 | if (CanonNNS == NNS) { |
| 4911 | QTN = new (*this,4) DependentTemplateName(NNS, Operator); |
| 4912 | } else { |
| 4913 | TemplateName Canon = getDependentTemplateName(CanonNNS, Operator); |
| 4914 | QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4915 | |
| 4916 | DependentTemplateName *CheckQTN |
| 4917 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4918 | assert(!CheckQTN && "Dependent template name canonicalization broken"); |
| 4919 | (void)CheckQTN; |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4920 | } |
| 4921 | |
| 4922 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 4923 | return TemplateName(QTN); |
| 4924 | } |
| 4925 | |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 4926 | TemplateName |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 4927 | ASTContext::getSubstTemplateTemplateParm(TemplateTemplateParmDecl *param, |
| 4928 | TemplateName replacement) const { |
| 4929 | llvm::FoldingSetNodeID ID; |
| 4930 | SubstTemplateTemplateParmStorage::Profile(ID, param, replacement); |
| 4931 | |
| 4932 | void *insertPos = 0; |
| 4933 | SubstTemplateTemplateParmStorage *subst |
| 4934 | = SubstTemplateTemplateParms.FindNodeOrInsertPos(ID, insertPos); |
| 4935 | |
| 4936 | if (!subst) { |
| 4937 | subst = new (*this) SubstTemplateTemplateParmStorage(param, replacement); |
| 4938 | SubstTemplateTemplateParms.InsertNode(subst, insertPos); |
| 4939 | } |
| 4940 | |
| 4941 | return TemplateName(subst); |
| 4942 | } |
| 4943 | |
| 4944 | TemplateName |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 4945 | ASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param, |
| 4946 | const TemplateArgument &ArgPack) const { |
| 4947 | ASTContext &Self = const_cast<ASTContext &>(*this); |
| 4948 | llvm::FoldingSetNodeID ID; |
| 4949 | SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack); |
| 4950 | |
| 4951 | void *InsertPos = 0; |
| 4952 | SubstTemplateTemplateParmPackStorage *Subst |
| 4953 | = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos); |
| 4954 | |
| 4955 | if (!Subst) { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 4956 | Subst = new (*this) SubstTemplateTemplateParmPackStorage(Param, |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 4957 | ArgPack.pack_size(), |
| 4958 | ArgPack.pack_begin()); |
| 4959 | SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos); |
| 4960 | } |
| 4961 | |
| 4962 | return TemplateName(Subst); |
| 4963 | } |
| 4964 | |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4965 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | ab13857 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 4966 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 4967 | /// is actually a value of type @c TargetInfo::IntType. |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 4968 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4969 | switch (Type) { |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 4970 | case TargetInfo::NoInt: return CanQualType(); |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4971 | case TargetInfo::SignedShort: return ShortTy; |
| 4972 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 4973 | case TargetInfo::SignedInt: return IntTy; |
| 4974 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 4975 | case TargetInfo::SignedLong: return LongTy; |
| 4976 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 4977 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 4978 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 4979 | } |
| 4980 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4981 | llvm_unreachable("Unhandled TargetInfo::IntType value"); |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4982 | } |
Ted Kremenek | 77c51b2 | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 4983 | |
| 4984 | //===----------------------------------------------------------------------===// |
| 4985 | // Type Predicates. |
| 4986 | //===----------------------------------------------------------------------===// |
| 4987 | |
Fariborz Jahanian | 9620769 | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4988 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 4989 | /// garbage collection attribute. |
| 4990 | /// |
John McCall | 553d45a | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 4991 | Qualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const { |
Douglas Gregor | 79a9141 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 4992 | if (getLangOptions().getGC() == LangOptions::NonGC) |
John McCall | 553d45a | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 4993 | return Qualifiers::GCNone; |
| 4994 | |
| 4995 | assert(getLangOptions().ObjC1); |
| 4996 | Qualifiers::GC GCAttrs = Ty.getObjCGCAttr(); |
| 4997 | |
| 4998 | // Default behaviour under objective-C's gc is for ObjC pointers |
| 4999 | // (or pointers to them) be treated as though they were declared |
| 5000 | // as __strong. |
| 5001 | if (GCAttrs == Qualifiers::GCNone) { |
| 5002 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
| 5003 | return Qualifiers::Strong; |
| 5004 | else if (Ty->isPointerType()) |
| 5005 | return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); |
| 5006 | } else { |
| 5007 | // It's not valid to set GC attributes on anything that isn't a |
| 5008 | // pointer. |
| 5009 | #ifndef NDEBUG |
| 5010 | QualType CT = Ty->getCanonicalTypeInternal(); |
| 5011 | while (const ArrayType *AT = dyn_cast<ArrayType>(CT)) |
| 5012 | CT = AT->getElementType(); |
| 5013 | assert(CT->isAnyPointerType() || CT->isBlockPointerType()); |
| 5014 | #endif |
Fariborz Jahanian | 9620769 | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 5015 | } |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 5016 | return GCAttrs; |
Fariborz Jahanian | 9620769 | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 5017 | } |
| 5018 | |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5019 | //===----------------------------------------------------------------------===// |
| 5020 | // Type Compatibility Testing |
| 5021 | //===----------------------------------------------------------------------===// |
Chris Lattner | b338a6b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 5022 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5023 | /// areCompatVectorTypes - Return true if the two specified vector types are |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5024 | /// compatible. |
| 5025 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 5026 | const VectorType *RHS) { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 5027 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5028 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5029 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5030 | } |
| 5031 | |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5032 | bool ASTContext::areCompatibleVectorTypes(QualType FirstVec, |
| 5033 | QualType SecondVec) { |
| 5034 | assert(FirstVec->isVectorType() && "FirstVec should be a vector type"); |
| 5035 | assert(SecondVec->isVectorType() && "SecondVec should be a vector type"); |
| 5036 | |
| 5037 | if (hasSameUnqualifiedType(FirstVec, SecondVec)) |
| 5038 | return true; |
| 5039 | |
Bob Wilson | e6aeebb | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 5040 | // Treat Neon vector types and most AltiVec vector types as if they are the |
| 5041 | // equivalent GCC vector types. |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5042 | const VectorType *First = FirstVec->getAs<VectorType>(); |
| 5043 | const VectorType *Second = SecondVec->getAs<VectorType>(); |
Bob Wilson | e6aeebb | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 5044 | if (First->getNumElements() == Second->getNumElements() && |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5045 | hasSameType(First->getElementType(), Second->getElementType()) && |
Bob Wilson | e6aeebb | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 5046 | First->getVectorKind() != VectorType::AltiVecPixel && |
| 5047 | First->getVectorKind() != VectorType::AltiVecBool && |
| 5048 | Second->getVectorKind() != VectorType::AltiVecPixel && |
| 5049 | Second->getVectorKind() != VectorType::AltiVecBool) |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5050 | return true; |
| 5051 | |
| 5052 | return false; |
| 5053 | } |
| 5054 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5055 | //===----------------------------------------------------------------------===// |
| 5056 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 5057 | //===----------------------------------------------------------------------===// |
| 5058 | |
| 5059 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 5060 | /// inheritance hierarchy of 'rProto'. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5061 | bool |
| 5062 | ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 5063 | ObjCProtocolDecl *rProto) const { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5064 | if (lProto == rProto) |
| 5065 | return true; |
| 5066 | for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), |
| 5067 | E = rProto->protocol_end(); PI != E; ++PI) |
| 5068 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 5069 | return true; |
| 5070 | return false; |
| 5071 | } |
| 5072 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5073 | /// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...> |
| 5074 | /// return true if lhs's protocols conform to rhs's protocol; false |
| 5075 | /// otherwise. |
| 5076 | bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) { |
| 5077 | if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType()) |
| 5078 | return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false); |
| 5079 | return false; |
| 5080 | } |
| 5081 | |
Fariborz Jahanian | 3c7ebc3 | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 5082 | /// ObjCQualifiedClassTypesAreCompatible - compare Class<p,...> and |
| 5083 | /// Class<p1, ...>. |
| 5084 | bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs, |
| 5085 | QualType rhs) { |
| 5086 | const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>(); |
| 5087 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
| 5088 | assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible"); |
| 5089 | |
| 5090 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5091 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5092 | bool match = false; |
| 5093 | ObjCProtocolDecl *lhsProto = *I; |
| 5094 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 5095 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 5096 | ObjCProtocolDecl *rhsProto = *J; |
| 5097 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) { |
| 5098 | match = true; |
| 5099 | break; |
| 5100 | } |
| 5101 | } |
| 5102 | if (!match) |
| 5103 | return false; |
| 5104 | } |
| 5105 | return true; |
| 5106 | } |
| 5107 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5108 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 5109 | /// ObjCQualifiedIDType. |
| 5110 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 5111 | bool compare) { |
| 5112 | // Allow id<P..> and an 'id' or void* type in all cases. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5113 | if (lhs->isVoidPointerType() || |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5114 | lhs->isObjCIdType() || lhs->isObjCClassType()) |
| 5115 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5116 | else if (rhs->isVoidPointerType() || |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5117 | rhs->isObjCIdType() || rhs->isObjCClassType()) |
| 5118 | return true; |
| 5119 | |
| 5120 | if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5121 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5122 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5123 | if (!rhsOPT) return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5124 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5125 | if (rhsOPT->qual_empty()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5126 | // If the RHS is a unqualified interface pointer "NSString*", |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5127 | // make sure we check the class hierarchy. |
| 5128 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 5129 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5130 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5131 | // when comparing an id<P> on lhs with a static type on rhs, |
| 5132 | // see if static class implements all of id's protocols, directly or |
| 5133 | // through its super class and categories. |
Fariborz Jahanian | 3f8917a | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 5134 | if (!rhsID->ClassImplementsProtocol(*I, true)) |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5135 | return false; |
| 5136 | } |
| 5137 | } |
| 5138 | // If there are no qualifiers and no interface, we have an 'id'. |
| 5139 | return true; |
| 5140 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5141 | // Both the right and left sides have qualifiers. |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5142 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5143 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5144 | ObjCProtocolDecl *lhsProto = *I; |
| 5145 | bool match = false; |
| 5146 | |
| 5147 | // when comparing an id<P> on lhs with a static type on rhs, |
| 5148 | // see if static class implements all of id's protocols, directly or |
| 5149 | // through its super class and categories. |
| 5150 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 5151 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 5152 | ObjCProtocolDecl *rhsProto = *J; |
| 5153 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 5154 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 5155 | match = true; |
| 5156 | break; |
| 5157 | } |
| 5158 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5159 | // If the RHS is a qualified interface pointer "NSString<P>*", |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5160 | // make sure we check the class hierarchy. |
| 5161 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 5162 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5163 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5164 | // when comparing an id<P> on lhs with a static type on rhs, |
| 5165 | // see if static class implements all of id's protocols, directly or |
| 5166 | // through its super class and categories. |
Fariborz Jahanian | 3f8917a | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 5167 | if (rhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5168 | match = true; |
| 5169 | break; |
| 5170 | } |
| 5171 | } |
| 5172 | } |
| 5173 | if (!match) |
| 5174 | return false; |
| 5175 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5176 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5177 | return true; |
| 5178 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5179 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5180 | const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 5181 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 5182 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5183 | if (const ObjCObjectPointerType *lhsOPT = |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5184 | lhs->getAsObjCInterfacePointerType()) { |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5185 | // If both the right and left sides have qualifiers. |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5186 | for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(), |
| 5187 | E = lhsOPT->qual_end(); I != E; ++I) { |
| 5188 | ObjCProtocolDecl *lhsProto = *I; |
| 5189 | bool match = false; |
| 5190 | |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5191 | // when comparing an id<P> on rhs with a static type on lhs, |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5192 | // see if static class implements all of id's protocols, directly or |
| 5193 | // through its super class and categories. |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5194 | // First, lhs protocols in the qualifier list must be found, direct |
| 5195 | // or indirect in rhs's qualifier list or it is a mismatch. |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5196 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 5197 | E = rhsQID->qual_end(); J != E; ++J) { |
| 5198 | ObjCProtocolDecl *rhsProto = *J; |
| 5199 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 5200 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 5201 | match = true; |
| 5202 | break; |
| 5203 | } |
| 5204 | } |
| 5205 | if (!match) |
| 5206 | return false; |
| 5207 | } |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5208 | |
| 5209 | // Static class's protocols, or its super class or category protocols |
| 5210 | // must be found, direct or indirect in rhs's qualifier list or it is a mismatch. |
| 5211 | if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { |
| 5212 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
| 5213 | CollectInheritedProtocols(lhsID, LHSInheritedProtocols); |
| 5214 | // This is rather dubious but matches gcc's behavior. If lhs has |
| 5215 | // no type qualifier and its class has no static protocol(s) |
| 5216 | // assume that it is mismatch. |
| 5217 | if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty()) |
| 5218 | return false; |
| 5219 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 5220 | LHSInheritedProtocols.begin(), |
| 5221 | E = LHSInheritedProtocols.end(); I != E; ++I) { |
| 5222 | bool match = false; |
| 5223 | ObjCProtocolDecl *lhsProto = (*I); |
| 5224 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 5225 | E = rhsQID->qual_end(); J != E; ++J) { |
| 5226 | ObjCProtocolDecl *rhsProto = *J; |
| 5227 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 5228 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 5229 | match = true; |
| 5230 | break; |
| 5231 | } |
| 5232 | } |
| 5233 | if (!match) |
| 5234 | return false; |
| 5235 | } |
| 5236 | } |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5237 | return true; |
| 5238 | } |
| 5239 | return false; |
| 5240 | } |
| 5241 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5242 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5243 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 5244 | /// protocol qualifiers on the LHS or RHS. |
| 5245 | /// |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5246 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 5247 | const ObjCObjectPointerType *RHSOPT) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5248 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 5249 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 5250 | |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 5251 | // If either type represents the built-in 'id' or 'Class' types, return true. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5252 | if (LHS->isObjCUnqualifiedIdOrClass() || |
| 5253 | RHS->isObjCUnqualifiedIdOrClass()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5254 | return true; |
| 5255 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5256 | if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5257 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 5258 | QualType(RHSOPT,0), |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5259 | false); |
Fariborz Jahanian | 3c7ebc3 | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 5260 | |
| 5261 | if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass()) |
| 5262 | return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0), |
| 5263 | QualType(RHSOPT,0)); |
| 5264 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5265 | // If we have 2 user-defined types, fall into that path. |
| 5266 | if (LHS->getInterface() && RHS->getInterface()) |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5267 | return canAssignObjCInterfaces(LHS, RHS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5268 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5269 | return false; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5270 | } |
| 5271 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5272 | /// canAssignObjCInterfacesInBlockPointer - This routine is specifically written |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 5273 | /// for providing type-safety for objective-c pointers used to pass/return |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5274 | /// arguments in block literals. When passed as arguments, passing 'A*' where |
| 5275 | /// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is |
| 5276 | /// not OK. For the return type, the opposite is not OK. |
| 5277 | bool ASTContext::canAssignObjCInterfacesInBlockPointer( |
| 5278 | const ObjCObjectPointerType *LHSOPT, |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5279 | const ObjCObjectPointerType *RHSOPT, |
| 5280 | bool BlockReturnType) { |
Fariborz Jahanian | 440a683 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 5281 | if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType()) |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5282 | return true; |
| 5283 | |
| 5284 | if (LHSOPT->isObjCBuiltinType()) { |
| 5285 | return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType(); |
| 5286 | } |
| 5287 | |
Fariborz Jahanian | 440a683 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 5288 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5289 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 5290 | QualType(RHSOPT,0), |
| 5291 | false); |
| 5292 | |
| 5293 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 5294 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 5295 | if (LHS && RHS) { // We have 2 user-defined types. |
| 5296 | if (LHS != RHS) { |
| 5297 | if (LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5298 | return BlockReturnType; |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5299 | if (RHS->getDecl()->isSuperClassOf(LHS->getDecl())) |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5300 | return !BlockReturnType; |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5301 | } |
| 5302 | else |
| 5303 | return true; |
| 5304 | } |
| 5305 | return false; |
| 5306 | } |
| 5307 | |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5308 | /// getIntersectionOfProtocols - This routine finds the intersection of set |
| 5309 | /// of protocols inherited from two distinct objective-c pointer objects. |
| 5310 | /// It is used to build composite qualifier list of the composite type of |
| 5311 | /// the conditional expression involving two objective-c pointer objects. |
| 5312 | static |
| 5313 | void getIntersectionOfProtocols(ASTContext &Context, |
| 5314 | const ObjCObjectPointerType *LHSOPT, |
| 5315 | const ObjCObjectPointerType *RHSOPT, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5316 | SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) { |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5317 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5318 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 5319 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 5320 | assert(LHS->getInterface() && "LHS must have an interface base"); |
| 5321 | assert(RHS->getInterface() && "RHS must have an interface base"); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5322 | |
| 5323 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet; |
| 5324 | unsigned LHSNumProtocols = LHS->getNumProtocols(); |
| 5325 | if (LHSNumProtocols > 0) |
| 5326 | InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end()); |
| 5327 | else { |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 5328 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5329 | Context.CollectInheritedProtocols(LHS->getInterface(), |
| 5330 | LHSInheritedProtocols); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5331 | InheritedProtocolSet.insert(LHSInheritedProtocols.begin(), |
| 5332 | LHSInheritedProtocols.end()); |
| 5333 | } |
| 5334 | |
| 5335 | unsigned RHSNumProtocols = RHS->getNumProtocols(); |
| 5336 | if (RHSNumProtocols > 0) { |
Dan Gohman | 145f3f1 | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 5337 | ObjCProtocolDecl **RHSProtocols = |
| 5338 | const_cast<ObjCProtocolDecl **>(RHS->qual_begin()); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5339 | for (unsigned i = 0; i < RHSNumProtocols; ++i) |
| 5340 | if (InheritedProtocolSet.count(RHSProtocols[i])) |
| 5341 | IntersectionOfProtocols.push_back(RHSProtocols[i]); |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 5342 | } else { |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 5343 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5344 | Context.CollectInheritedProtocols(RHS->getInterface(), |
| 5345 | RHSInheritedProtocols); |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 5346 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 5347 | RHSInheritedProtocols.begin(), |
| 5348 | E = RHSInheritedProtocols.end(); I != E; ++I) |
| 5349 | if (InheritedProtocolSet.count((*I))) |
| 5350 | IntersectionOfProtocols.push_back((*I)); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5351 | } |
| 5352 | } |
| 5353 | |
Fariborz Jahanian | ef8b8ce | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 5354 | /// areCommonBaseCompatible - Returns common base class of the two classes if |
| 5355 | /// one found. Note that this is O'2 algorithm. But it will be called as the |
| 5356 | /// last type comparison in a ?-exp of ObjC pointer types before a |
| 5357 | /// warning is issued. So, its invokation is extremely rare. |
| 5358 | QualType ASTContext::areCommonBaseCompatible( |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5359 | const ObjCObjectPointerType *Lptr, |
| 5360 | const ObjCObjectPointerType *Rptr) { |
| 5361 | const ObjCObjectType *LHS = Lptr->getObjectType(); |
| 5362 | const ObjCObjectType *RHS = Rptr->getObjectType(); |
| 5363 | const ObjCInterfaceDecl* LDecl = LHS->getInterface(); |
| 5364 | const ObjCInterfaceDecl* RDecl = RHS->getInterface(); |
Fariborz Jahanian | b107143 | 2011-04-18 21:16:59 +0000 | [diff] [blame] | 5365 | if (!LDecl || !RDecl || (LDecl == RDecl)) |
Fariborz Jahanian | ef8b8ce | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 5366 | return QualType(); |
| 5367 | |
Fariborz Jahanian | b107143 | 2011-04-18 21:16:59 +0000 | [diff] [blame] | 5368 | do { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5369 | LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl)); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5370 | if (canAssignObjCInterfaces(LHS, RHS)) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5371 | SmallVector<ObjCProtocolDecl *, 8> Protocols; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5372 | getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols); |
| 5373 | |
| 5374 | QualType Result = QualType(LHS, 0); |
| 5375 | if (!Protocols.empty()) |
| 5376 | Result = getObjCObjectType(Result, Protocols.data(), Protocols.size()); |
| 5377 | Result = getObjCObjectPointerType(Result); |
| 5378 | return Result; |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5379 | } |
Fariborz Jahanian | b107143 | 2011-04-18 21:16:59 +0000 | [diff] [blame] | 5380 | } while ((LDecl = LDecl->getSuperClass())); |
Fariborz Jahanian | ef8b8ce | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 5381 | |
| 5382 | return QualType(); |
| 5383 | } |
| 5384 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5385 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS, |
| 5386 | const ObjCObjectType *RHS) { |
| 5387 | assert(LHS->getInterface() && "LHS is not an interface type"); |
| 5388 | assert(RHS->getInterface() && "RHS is not an interface type"); |
| 5389 | |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5390 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 5391 | // the LHS. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5392 | if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface())) |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5393 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5394 | |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5395 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 5396 | // protocol qualified at all, then we are good. |
Steve Naroff | c277ad1 | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 5397 | if (LHS->getNumProtocols() == 0) |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5398 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5399 | |
Fariborz Jahanian | 4806ff8 | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 5400 | // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, |
| 5401 | // more detailed analysis is required. |
| 5402 | if (RHS->getNumProtocols() == 0) { |
| 5403 | // OK, if LHS is a superclass of RHS *and* |
| 5404 | // this superclass is assignment compatible with LHS. |
| 5405 | // false otherwise. |
Fariborz Jahanian | 240400b | 2011-04-12 16:34:14 +0000 | [diff] [blame] | 5406 | bool IsSuperClass = |
| 5407 | LHS->getInterface()->isSuperClassOf(RHS->getInterface()); |
| 5408 | if (IsSuperClass) { |
Fariborz Jahanian | 4806ff8 | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 5409 | // OK if conversion of LHS to SuperClass results in narrowing of types |
| 5410 | // ; i.e., SuperClass may implement at least one of the protocols |
| 5411 | // in LHS's protocol list. Example, SuperObj<P1> = lhs<P1,P2> is ok. |
| 5412 | // But not SuperObj<P1,P2,P3> = lhs<P1,P2>. |
| 5413 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> SuperClassInheritedProtocols; |
Fariborz Jahanian | 240400b | 2011-04-12 16:34:14 +0000 | [diff] [blame] | 5414 | CollectInheritedProtocols(RHS->getInterface(), SuperClassInheritedProtocols); |
Fariborz Jahanian | 4806ff8 | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 5415 | // If super class has no protocols, it is not a match. |
| 5416 | if (SuperClassInheritedProtocols.empty()) |
| 5417 | return false; |
| 5418 | |
| 5419 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 5420 | LHSPE = LHS->qual_end(); |
| 5421 | LHSPI != LHSPE; LHSPI++) { |
| 5422 | bool SuperImplementsProtocol = false; |
| 5423 | ObjCProtocolDecl *LHSProto = (*LHSPI); |
| 5424 | |
| 5425 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 5426 | SuperClassInheritedProtocols.begin(), |
| 5427 | E = SuperClassInheritedProtocols.end(); I != E; ++I) { |
| 5428 | ObjCProtocolDecl *SuperClassProto = (*I); |
| 5429 | if (SuperClassProto->lookupProtocolNamed(LHSProto->getIdentifier())) { |
| 5430 | SuperImplementsProtocol = true; |
| 5431 | break; |
| 5432 | } |
| 5433 | } |
| 5434 | if (!SuperImplementsProtocol) |
| 5435 | return false; |
| 5436 | } |
| 5437 | return true; |
| 5438 | } |
| 5439 | return false; |
| 5440 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5441 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5442 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 5443 | LHSPE = LHS->qual_end(); |
Steve Naroff | 114aecb | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 5444 | LHSPI != LHSPE; LHSPI++) { |
| 5445 | bool RHSImplementsProtocol = false; |
| 5446 | |
| 5447 | // If the RHS doesn't implement the protocol on the left, the types |
| 5448 | // are incompatible. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5449 | for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(), |
| 5450 | RHSPE = RHS->qual_end(); |
Steve Naroff | fac5bc9 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 5451 | RHSPI != RHSPE; RHSPI++) { |
| 5452 | if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) { |
Steve Naroff | 114aecb | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 5453 | RHSImplementsProtocol = true; |
Steve Naroff | fac5bc9 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 5454 | break; |
| 5455 | } |
Steve Naroff | 114aecb | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 5456 | } |
| 5457 | // FIXME: For better diagnostics, consider passing back the protocol name. |
| 5458 | if (!RHSImplementsProtocol) |
| 5459 | return false; |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5460 | } |
Steve Naroff | 114aecb | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 5461 | // The RHS implements all protocols listed on the LHS. |
| 5462 | return true; |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5463 | } |
| 5464 | |
Steve Naroff | b760515 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 5465 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 5466 | // get the "pointed to" types |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5467 | const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
| 5468 | const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5469 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5470 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | b760515 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 5471 | return false; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5472 | |
| 5473 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 5474 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | b760515 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 5475 | } |
| 5476 | |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 5477 | bool ASTContext::canBindObjCObjectType(QualType To, QualType From) { |
| 5478 | return canAssignObjCInterfaces( |
| 5479 | getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(), |
| 5480 | getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>()); |
| 5481 | } |
| 5482 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5483 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5484 | /// both shall have the identically qualified version of a compatible type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5485 | /// C99 6.2.7p1: Two types have compatible types if their types are the |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5486 | /// same. See 6.7.[2,3,5] for additional rules. |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5487 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS, |
| 5488 | bool CompareUnqualified) { |
Douglas Gregor | 21e771e | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 5489 | if (getLangOptions().CPlusPlus) |
| 5490 | return hasSameType(LHS, RHS); |
| 5491 | |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5492 | return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull(); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5493 | } |
| 5494 | |
Fariborz Jahanian | c0f6af2 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 5495 | bool ASTContext::propertyTypesAreCompatible(QualType LHS, QualType RHS) { |
Fariborz Jahanian | c87c879 | 2011-07-12 23:20:13 +0000 | [diff] [blame] | 5496 | return typesAreCompatible(LHS, RHS); |
Fariborz Jahanian | c0f6af2 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 5497 | } |
| 5498 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5499 | bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) { |
| 5500 | return !mergeTypes(LHS, RHS, true).isNull(); |
| 5501 | } |
| 5502 | |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 5503 | /// mergeTransparentUnionType - if T is a transparent union type and a member |
| 5504 | /// of T is compatible with SubType, return the merged type, else return |
| 5505 | /// QualType() |
| 5506 | QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType, |
| 5507 | bool OfBlockPointer, |
| 5508 | bool Unqualified) { |
| 5509 | if (const RecordType *UT = T->getAsUnionType()) { |
| 5510 | RecordDecl *UD = UT->getDecl(); |
| 5511 | if (UD->hasAttr<TransparentUnionAttr>()) { |
| 5512 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 5513 | itend = UD->field_end(); it != itend; ++it) { |
Peter Collingbourne | 19b961d | 2010-12-02 21:00:06 +0000 | [diff] [blame] | 5514 | QualType ET = it->getType().getUnqualifiedType(); |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 5515 | QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified); |
| 5516 | if (!MT.isNull()) |
| 5517 | return MT; |
| 5518 | } |
| 5519 | } |
| 5520 | } |
| 5521 | |
| 5522 | return QualType(); |
| 5523 | } |
| 5524 | |
| 5525 | /// mergeFunctionArgumentTypes - merge two types which appear as function |
| 5526 | /// argument types |
| 5527 | QualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs, |
| 5528 | bool OfBlockPointer, |
| 5529 | bool Unqualified) { |
| 5530 | // GNU extension: two types are compatible if they appear as a function |
| 5531 | // argument, one of the types is a transparent union type and the other |
| 5532 | // type is compatible with a union member |
| 5533 | QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer, |
| 5534 | Unqualified); |
| 5535 | if (!lmerge.isNull()) |
| 5536 | return lmerge; |
| 5537 | |
| 5538 | QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer, |
| 5539 | Unqualified); |
| 5540 | if (!rmerge.isNull()) |
| 5541 | return rmerge; |
| 5542 | |
| 5543 | return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified); |
| 5544 | } |
| 5545 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5546 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs, |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5547 | bool OfBlockPointer, |
| 5548 | bool Unqualified) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5549 | const FunctionType *lbase = lhs->getAs<FunctionType>(); |
| 5550 | const FunctionType *rbase = rhs->getAs<FunctionType>(); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5551 | const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 5552 | const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5553 | bool allLTypes = true; |
| 5554 | bool allRTypes = true; |
| 5555 | |
| 5556 | // Check return type |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5557 | QualType retType; |
Fariborz Jahanian | 1782597 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 5558 | if (OfBlockPointer) { |
| 5559 | QualType RHS = rbase->getResultType(); |
| 5560 | QualType LHS = lbase->getResultType(); |
| 5561 | bool UnqualifiedResult = Unqualified; |
| 5562 | if (!UnqualifiedResult) |
| 5563 | UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers()); |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5564 | retType = mergeTypes(LHS, RHS, true, UnqualifiedResult, true); |
Fariborz Jahanian | 1782597 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 5565 | } |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5566 | else |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5567 | retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), false, |
| 5568 | Unqualified); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5569 | if (retType.isNull()) return QualType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5570 | |
| 5571 | if (Unqualified) |
| 5572 | retType = retType.getUnqualifiedType(); |
| 5573 | |
| 5574 | CanQualType LRetType = getCanonicalType(lbase->getResultType()); |
| 5575 | CanQualType RRetType = getCanonicalType(rbase->getResultType()); |
| 5576 | if (Unqualified) { |
| 5577 | LRetType = LRetType.getUnqualifiedType(); |
| 5578 | RRetType = RRetType.getUnqualifiedType(); |
| 5579 | } |
| 5580 | |
| 5581 | if (getCanonicalType(retType) != LRetType) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5582 | allLTypes = false; |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5583 | if (getCanonicalType(retType) != RRetType) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5584 | allRTypes = false; |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5585 | |
Daniel Dunbar | edd5bae | 2010-04-28 16:20:58 +0000 | [diff] [blame] | 5586 | // FIXME: double check this |
| 5587 | // FIXME: should we error if lbase->getRegParmAttr() != 0 && |
| 5588 | // rbase->getRegParmAttr() != 0 && |
| 5589 | // lbase->getRegParmAttr() != rbase->getRegParmAttr()? |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 5590 | FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo(); |
| 5591 | FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo(); |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5592 | |
Douglas Gregor | 8c94086 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 5593 | // Compatible functions must have compatible calling conventions |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5594 | if (!isSameCallConv(lbaseInfo.getCC(), rbaseInfo.getCC())) |
Douglas Gregor | 8c94086 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 5595 | return QualType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5596 | |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5597 | // Regparm is part of the calling convention. |
Eli Friedman | c5b20b5 | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 5598 | if (lbaseInfo.getHasRegParm() != rbaseInfo.getHasRegParm()) |
| 5599 | return QualType(); |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5600 | if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm()) |
| 5601 | return QualType(); |
| 5602 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5603 | if (lbaseInfo.getProducesResult() != rbaseInfo.getProducesResult()) |
| 5604 | return QualType(); |
| 5605 | |
Fariborz Jahanian | 48c6910 | 2011-10-05 00:05:34 +0000 | [diff] [blame] | 5606 | // functypes which return are preferred over those that do not. |
| 5607 | if (lbaseInfo.getNoReturn() && !rbaseInfo.getNoReturn()) |
| 5608 | allLTypes = false; |
| 5609 | else if (!lbaseInfo.getNoReturn() && rbaseInfo.getNoReturn()) |
| 5610 | allRTypes = false; |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5611 | // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'. |
| 5612 | bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn(); |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5613 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5614 | FunctionType::ExtInfo einfo = lbaseInfo.withNoReturn(NoReturn); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5615 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5616 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 5617 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 5618 | "C++ shouldn't be here"); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5619 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 5620 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 5621 | |
| 5622 | // Compatible functions must have the same number of arguments |
| 5623 | if (lproto_nargs != rproto_nargs) |
| 5624 | return QualType(); |
| 5625 | |
| 5626 | // Variadic and non-variadic functions aren't compatible |
| 5627 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 5628 | return QualType(); |
| 5629 | |
Argyrios Kyrtzidis | 22a3735 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 5630 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 5631 | return QualType(); |
| 5632 | |
Fariborz Jahanian | 9767697 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 5633 | if (LangOpts.ObjCAutoRefCount && |
| 5634 | !FunctionTypesMatchOnNSConsumedAttrs(rproto, lproto)) |
| 5635 | return QualType(); |
| 5636 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5637 | // Check argument compatibility |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5638 | SmallVector<QualType, 10> types; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5639 | for (unsigned i = 0; i < lproto_nargs; i++) { |
| 5640 | QualType largtype = lproto->getArgType(i).getUnqualifiedType(); |
| 5641 | QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 5642 | QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype, |
| 5643 | OfBlockPointer, |
| 5644 | Unqualified); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5645 | if (argtype.isNull()) return QualType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5646 | |
| 5647 | if (Unqualified) |
| 5648 | argtype = argtype.getUnqualifiedType(); |
| 5649 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5650 | types.push_back(argtype); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5651 | if (Unqualified) { |
| 5652 | largtype = largtype.getUnqualifiedType(); |
| 5653 | rargtype = rargtype.getUnqualifiedType(); |
| 5654 | } |
| 5655 | |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5656 | if (getCanonicalType(argtype) != getCanonicalType(largtype)) |
| 5657 | allLTypes = false; |
| 5658 | if (getCanonicalType(argtype) != getCanonicalType(rargtype)) |
| 5659 | allRTypes = false; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5660 | } |
Fariborz Jahanian | 9767697 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 5661 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5662 | if (allLTypes) return lhs; |
| 5663 | if (allRTypes) return rhs; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5664 | |
| 5665 | FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo(); |
| 5666 | EPI.ExtInfo = einfo; |
| 5667 | return getFunctionType(retType, types.begin(), types.size(), EPI); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5668 | } |
| 5669 | |
| 5670 | if (lproto) allRTypes = false; |
| 5671 | if (rproto) allLTypes = false; |
| 5672 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5673 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5674 | if (proto) { |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 5675 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5676 | if (proto->isVariadic()) return QualType(); |
| 5677 | // Check that the types are compatible with the types that |
| 5678 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 5679 | // The only types actually affected are promotable integer |
| 5680 | // types and floats, which would be passed as a different |
| 5681 | // type depending on whether the prototype is visible. |
| 5682 | unsigned proto_nargs = proto->getNumArgs(); |
| 5683 | for (unsigned i = 0; i < proto_nargs; ++i) { |
| 5684 | QualType argTy = proto->getArgType(i); |
Douglas Gregor | 2973d40 | 2010-02-03 19:27:29 +0000 | [diff] [blame] | 5685 | |
| 5686 | // Look at the promotion type of enum types, since that is the type used |
| 5687 | // to pass enum values. |
| 5688 | if (const EnumType *Enum = argTy->getAs<EnumType>()) |
| 5689 | argTy = Enum->getDecl()->getPromotionType(); |
| 5690 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5691 | if (argTy->isPromotableIntegerType() || |
| 5692 | getCanonicalType(argTy).getUnqualifiedType() == FloatTy) |
| 5693 | return QualType(); |
| 5694 | } |
| 5695 | |
| 5696 | if (allLTypes) return lhs; |
| 5697 | if (allRTypes) return rhs; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5698 | |
| 5699 | FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo(); |
| 5700 | EPI.ExtInfo = einfo; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5701 | return getFunctionType(retType, proto->arg_type_begin(), |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5702 | proto->getNumArgs(), EPI); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5703 | } |
| 5704 | |
| 5705 | if (allLTypes) return lhs; |
| 5706 | if (allRTypes) return rhs; |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5707 | return getFunctionNoProtoType(retType, einfo); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5708 | } |
| 5709 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5710 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5711 | bool OfBlockPointer, |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5712 | bool Unqualified, bool BlockReturnType) { |
Bill Wendling | db4e349 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 5713 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 5714 | // type is adjusted to "T" prior to any further analysis, the expression |
| 5715 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 5716 | // expression is an lvalue unless the reference is an rvalue reference and |
| 5717 | // the expression is a function call (possibly inside parentheses). |
Douglas Gregor | 21e771e | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 5718 | assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?"); |
| 5719 | assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?"); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5720 | |
| 5721 | if (Unqualified) { |
| 5722 | LHS = LHS.getUnqualifiedType(); |
| 5723 | RHS = RHS.getUnqualifiedType(); |
| 5724 | } |
Douglas Gregor | 21e771e | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 5725 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5726 | QualType LHSCan = getCanonicalType(LHS), |
| 5727 | RHSCan = getCanonicalType(RHS); |
| 5728 | |
| 5729 | // If two types are identical, they are compatible. |
| 5730 | if (LHSCan == RHSCan) |
| 5731 | return LHS; |
| 5732 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5733 | // If the qualifiers are different, the types aren't compatible... mostly. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 5734 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 5735 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5736 | if (LQuals != RQuals) { |
| 5737 | // If any of these qualifiers are different, we have a type |
| 5738 | // mismatch. |
| 5739 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5740 | LQuals.getAddressSpace() != RQuals.getAddressSpace() || |
| 5741 | LQuals.getObjCLifetime() != RQuals.getObjCLifetime()) |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5742 | return QualType(); |
| 5743 | |
| 5744 | // Exactly one GC qualifier difference is allowed: __strong is |
| 5745 | // okay if the other type has no GC qualifier but is an Objective |
| 5746 | // C object pointer (i.e. implicitly strong by default). We fix |
| 5747 | // this by pretending that the unqualified type was actually |
| 5748 | // qualified __strong. |
| 5749 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 5750 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 5751 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 5752 | |
| 5753 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 5754 | return QualType(); |
| 5755 | |
| 5756 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
| 5757 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); |
| 5758 | } |
| 5759 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
| 5760 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); |
| 5761 | } |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5762 | return QualType(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5763 | } |
| 5764 | |
| 5765 | // Okay, qualifiers are equal. |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5766 | |
Eli Friedman | dcca633 | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 5767 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 5768 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5769 | |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5770 | // We want to consider the two function types to be the same for these |
| 5771 | // comparisons, just force one to the other. |
| 5772 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 5773 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 16f9096 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 5774 | |
| 5775 | // Same as above for arrays |
Chris Lattner | 9555466 | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 5776 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 5777 | LHSClass = Type::ConstantArray; |
| 5778 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 5779 | RHSClass = Type::ConstantArray; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5780 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5781 | // ObjCInterfaces are just specialized ObjCObjects. |
| 5782 | if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject; |
| 5783 | if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject; |
| 5784 | |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 5785 | // Canonicalize ExtVector -> Vector. |
| 5786 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 5787 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5788 | |
Chris Lattner | 9555466 | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 5789 | // If the canonical type classes don't match. |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5790 | if (LHSClass != RHSClass) { |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5791 | // C99 6.7.2.2p4: Each enumerated type shall be compatible with char, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5792 | // a signed integer type, or an unsigned integer type. |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 5793 | // Compatibility is based on the underlying type, not the promotion |
| 5794 | // type. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5795 | if (const EnumType* ETy = LHS->getAs<EnumType>()) { |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5796 | if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType()) |
| 5797 | return RHS; |
Eli Friedman | a7bf7ed | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 5798 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5799 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5800 | if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType()) |
| 5801 | return LHS; |
Eli Friedman | a7bf7ed | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 5802 | } |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5803 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5804 | return QualType(); |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5805 | } |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5806 | |
Steve Naroff | c6edcbd | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 5807 | // The canonical type classes match. |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5808 | switch (LHSClass) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5809 | #define TYPE(Class, Base) |
| 5810 | #define ABSTRACT_TYPE(Class, Base) |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 5811 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5812 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 5813 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 5814 | #include "clang/AST/TypeNodes.def" |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5815 | llvm_unreachable("Non-canonical and dependent types shouldn't get here"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5816 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 5817 | case Type::LValueReference: |
| 5818 | case Type::RValueReference: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5819 | case Type::MemberPointer: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5820 | llvm_unreachable("C++ should never be in mergeTypes"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5821 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5822 | case Type::ObjCInterface: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5823 | case Type::IncompleteArray: |
| 5824 | case Type::VariableArray: |
| 5825 | case Type::FunctionProto: |
| 5826 | case Type::ExtVector: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5827 | llvm_unreachable("Types are eliminated above"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5828 | |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5829 | case Type::Pointer: |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5830 | { |
| 5831 | // Merge two pointer types, while trying to preserve typedef info |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5832 | QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); |
| 5833 | QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5834 | if (Unqualified) { |
| 5835 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 5836 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 5837 | } |
| 5838 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false, |
| 5839 | Unqualified); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5840 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | 091a9ac | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 5841 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5842 | return LHS; |
Eli Friedman | 091a9ac | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 5843 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5844 | return RHS; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5845 | return getPointerType(ResultType); |
| 5846 | } |
Steve Naroff | 68e167d | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 5847 | case Type::BlockPointer: |
| 5848 | { |
| 5849 | // Merge two block pointer types, while trying to preserve typedef info |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5850 | QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); |
| 5851 | QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5852 | if (Unqualified) { |
| 5853 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 5854 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 5855 | } |
| 5856 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer, |
| 5857 | Unqualified); |
Steve Naroff | 68e167d | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 5858 | if (ResultType.isNull()) return QualType(); |
| 5859 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 5860 | return LHS; |
| 5861 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 5862 | return RHS; |
| 5863 | return getBlockPointerType(ResultType); |
| 5864 | } |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5865 | case Type::Atomic: |
| 5866 | { |
| 5867 | // Merge two pointer types, while trying to preserve typedef info |
| 5868 | QualType LHSValue = LHS->getAs<AtomicType>()->getValueType(); |
| 5869 | QualType RHSValue = RHS->getAs<AtomicType>()->getValueType(); |
| 5870 | if (Unqualified) { |
| 5871 | LHSValue = LHSValue.getUnqualifiedType(); |
| 5872 | RHSValue = RHSValue.getUnqualifiedType(); |
| 5873 | } |
| 5874 | QualType ResultType = mergeTypes(LHSValue, RHSValue, false, |
| 5875 | Unqualified); |
| 5876 | if (ResultType.isNull()) return QualType(); |
| 5877 | if (getCanonicalType(LHSValue) == getCanonicalType(ResultType)) |
| 5878 | return LHS; |
| 5879 | if (getCanonicalType(RHSValue) == getCanonicalType(ResultType)) |
| 5880 | return RHS; |
| 5881 | return getAtomicType(ResultType); |
| 5882 | } |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5883 | case Type::ConstantArray: |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5884 | { |
| 5885 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 5886 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 5887 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 5888 | return QualType(); |
| 5889 | |
| 5890 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 5891 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5892 | if (Unqualified) { |
| 5893 | LHSElem = LHSElem.getUnqualifiedType(); |
| 5894 | RHSElem = RHSElem.getUnqualifiedType(); |
| 5895 | } |
| 5896 | |
| 5897 | QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5898 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5899 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 5900 | return LHS; |
| 5901 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 5902 | return RHS; |
Eli Friedman | 3e62c21 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 5903 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 5904 | ArrayType::ArraySizeModifier(), 0); |
| 5905 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 5906 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5907 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 5908 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5909 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 5910 | return LHS; |
| 5911 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 5912 | return RHS; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5913 | if (LVAT) { |
| 5914 | // FIXME: This isn't correct! But tricky to implement because |
| 5915 | // the array's size has to be the size of LHS, but the type |
| 5916 | // has to be different. |
| 5917 | return LHS; |
| 5918 | } |
| 5919 | if (RVAT) { |
| 5920 | // FIXME: This isn't correct! But tricky to implement because |
| 5921 | // the array's size has to be the size of RHS, but the type |
| 5922 | // has to be different. |
| 5923 | return RHS; |
| 5924 | } |
Eli Friedman | 3e62c21 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 5925 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 5926 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 5927 | return getIncompleteArrayType(ResultType, |
| 5928 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5929 | } |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5930 | case Type::FunctionNoProto: |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5931 | return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5932 | case Type::Record: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5933 | case Type::Enum: |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5934 | return QualType(); |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5935 | case Type::Builtin: |
Chris Lattner | 7bbd3d7 | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 5936 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5937 | return QualType(); |
Daniel Dunbar | 804c044 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 5938 | case Type::Complex: |
| 5939 | // Distinct complex types are incompatible. |
| 5940 | return QualType(); |
Chris Lattner | 7bbd3d7 | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 5941 | case Type::Vector: |
Eli Friedman | cad9638 | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 5942 | // FIXME: The merged type should be an ExtVector! |
John McCall | 44c064b | 2010-03-12 23:14:13 +0000 | [diff] [blame] | 5943 | if (areCompatVectorTypes(LHSCan->getAs<VectorType>(), |
| 5944 | RHSCan->getAs<VectorType>())) |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5945 | return LHS; |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5946 | return QualType(); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5947 | case Type::ObjCObject: { |
| 5948 | // Check if the types are assignment compatible. |
Eli Friedman | cad9638 | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 5949 | // FIXME: This should be type compatibility, e.g. whether |
| 5950 | // "LHS x; RHS x;" at global scope is legal. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5951 | const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>(); |
| 5952 | const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>(); |
| 5953 | if (canAssignObjCInterfaces(LHSIface, RHSIface)) |
Steve Naroff | 7a7814c | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 5954 | return LHS; |
| 5955 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5956 | return QualType(); |
Cedric Venet | 4fc88b7 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 5957 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5958 | case Type::ObjCObjectPointer: { |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5959 | if (OfBlockPointer) { |
| 5960 | if (canAssignObjCInterfacesInBlockPointer( |
| 5961 | LHS->getAs<ObjCObjectPointerType>(), |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5962 | RHS->getAs<ObjCObjectPointerType>(), |
| 5963 | BlockReturnType)) |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5964 | return LHS; |
| 5965 | return QualType(); |
| 5966 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5967 | if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(), |
| 5968 | RHS->getAs<ObjCObjectPointerType>())) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5969 | return LHS; |
| 5970 | |
Steve Naroff | c68cfcf | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 5971 | return QualType(); |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5972 | } |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5973 | } |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5974 | |
| 5975 | return QualType(); |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5976 | } |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 5977 | |
Fariborz Jahanian | 9767697 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 5978 | bool ASTContext::FunctionTypesMatchOnNSConsumedAttrs( |
| 5979 | const FunctionProtoType *FromFunctionType, |
| 5980 | const FunctionProtoType *ToFunctionType) { |
| 5981 | if (FromFunctionType->hasAnyConsumedArgs() != |
| 5982 | ToFunctionType->hasAnyConsumedArgs()) |
| 5983 | return false; |
| 5984 | FunctionProtoType::ExtProtoInfo FromEPI = |
| 5985 | FromFunctionType->getExtProtoInfo(); |
| 5986 | FunctionProtoType::ExtProtoInfo ToEPI = |
| 5987 | ToFunctionType->getExtProtoInfo(); |
| 5988 | if (FromEPI.ConsumedArguments && ToEPI.ConsumedArguments) |
| 5989 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 5990 | ArgIdx != NumArgs; ++ArgIdx) { |
| 5991 | if (FromEPI.ConsumedArguments[ArgIdx] != |
| 5992 | ToEPI.ConsumedArguments[ArgIdx]) |
| 5993 | return false; |
| 5994 | } |
| 5995 | return true; |
| 5996 | } |
| 5997 | |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 5998 | /// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and |
| 5999 | /// 'RHS' attributes and returns the merged version; including for function |
| 6000 | /// return types. |
| 6001 | QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) { |
| 6002 | QualType LHSCan = getCanonicalType(LHS), |
| 6003 | RHSCan = getCanonicalType(RHS); |
| 6004 | // If two types are identical, they are compatible. |
| 6005 | if (LHSCan == RHSCan) |
| 6006 | return LHS; |
| 6007 | if (RHSCan->isFunctionType()) { |
| 6008 | if (!LHSCan->isFunctionType()) |
| 6009 | return QualType(); |
| 6010 | QualType OldReturnType = |
| 6011 | cast<FunctionType>(RHSCan.getTypePtr())->getResultType(); |
| 6012 | QualType NewReturnType = |
| 6013 | cast<FunctionType>(LHSCan.getTypePtr())->getResultType(); |
| 6014 | QualType ResReturnType = |
| 6015 | mergeObjCGCQualifiers(NewReturnType, OldReturnType); |
| 6016 | if (ResReturnType.isNull()) |
| 6017 | return QualType(); |
| 6018 | if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) { |
| 6019 | // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo(); |
| 6020 | // In either case, use OldReturnType to build the new function type. |
| 6021 | const FunctionType *F = LHS->getAs<FunctionType>(); |
| 6022 | if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) { |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6023 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 6024 | EPI.ExtInfo = getFunctionExtInfo(LHS); |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 6025 | QualType ResultType |
| 6026 | = getFunctionType(OldReturnType, FPT->arg_type_begin(), |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6027 | FPT->getNumArgs(), EPI); |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 6028 | return ResultType; |
| 6029 | } |
| 6030 | } |
| 6031 | return QualType(); |
| 6032 | } |
| 6033 | |
| 6034 | // If the qualifiers are different, the types can still be merged. |
| 6035 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 6036 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
| 6037 | if (LQuals != RQuals) { |
| 6038 | // If any of these qualifiers are different, we have a type mismatch. |
| 6039 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 6040 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 6041 | return QualType(); |
| 6042 | |
| 6043 | // Exactly one GC qualifier difference is allowed: __strong is |
| 6044 | // okay if the other type has no GC qualifier but is an Objective |
| 6045 | // C object pointer (i.e. implicitly strong by default). We fix |
| 6046 | // this by pretending that the unqualified type was actually |
| 6047 | // qualified __strong. |
| 6048 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 6049 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 6050 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 6051 | |
| 6052 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 6053 | return QualType(); |
| 6054 | |
| 6055 | if (GC_L == Qualifiers::Strong) |
| 6056 | return LHS; |
| 6057 | if (GC_R == Qualifiers::Strong) |
| 6058 | return RHS; |
| 6059 | return QualType(); |
| 6060 | } |
| 6061 | |
| 6062 | if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) { |
| 6063 | QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 6064 | QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 6065 | QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT); |
| 6066 | if (ResQT == LHSBaseQT) |
| 6067 | return LHS; |
| 6068 | if (ResQT == RHSBaseQT) |
| 6069 | return RHS; |
| 6070 | } |
| 6071 | return QualType(); |
| 6072 | } |
| 6073 | |
Chris Lattner | 4ba0cef | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 6074 | //===----------------------------------------------------------------------===// |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6075 | // Integer Predicates |
| 6076 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3c91971 | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 6077 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6078 | unsigned ASTContext::getIntWidth(QualType T) const { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 6079 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
Eli Friedman | ee275c8 | 2009-12-10 22:29:29 +0000 | [diff] [blame] | 6080 | T = ET->getDecl()->getIntegerType(); |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6081 | if (T->isBooleanType()) |
| 6082 | return 1; |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 6083 | // For builtin types, just use the standard type sizing method |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6084 | return (unsigned)getTypeSize(T); |
| 6085 | } |
| 6086 | |
| 6087 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) { |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6088 | assert(T->hasSignedIntegerRepresentation() && "Unexpected type"); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6089 | |
| 6090 | // Turn <4 x signed int> -> <4 x unsigned int> |
| 6091 | if (const VectorType *VTy = T->getAs<VectorType>()) |
| 6092 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 6093 | VTy->getNumElements(), VTy->getVectorKind()); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6094 | |
| 6095 | // For enums, we return the unsigned version of the base type. |
| 6096 | if (const EnumType *ETy = T->getAs<EnumType>()) |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6097 | T = ETy->getDecl()->getIntegerType(); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6098 | |
| 6099 | const BuiltinType *BTy = T->getAs<BuiltinType>(); |
| 6100 | assert(BTy && "Unexpected signed integer type"); |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6101 | switch (BTy->getKind()) { |
| 6102 | case BuiltinType::Char_S: |
| 6103 | case BuiltinType::SChar: |
| 6104 | return UnsignedCharTy; |
| 6105 | case BuiltinType::Short: |
| 6106 | return UnsignedShortTy; |
| 6107 | case BuiltinType::Int: |
| 6108 | return UnsignedIntTy; |
| 6109 | case BuiltinType::Long: |
| 6110 | return UnsignedLongTy; |
| 6111 | case BuiltinType::LongLong: |
| 6112 | return UnsignedLongLongTy; |
Chris Lattner | f122cef | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 6113 | case BuiltinType::Int128: |
| 6114 | return UnsignedInt128Ty; |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6115 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6116 | llvm_unreachable("Unexpected signed integer type"); |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6117 | } |
| 6118 | } |
| 6119 | |
Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 6120 | ASTMutationListener::~ASTMutationListener() { } |
| 6121 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6122 | |
| 6123 | //===----------------------------------------------------------------------===// |
| 6124 | // Builtin Type Computation |
| 6125 | //===----------------------------------------------------------------------===// |
| 6126 | |
| 6127 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6128 | /// pointer over the consumed characters. This returns the resultant type. If |
| 6129 | /// AllowTypeModifiers is false then modifier like * are not parsed, just basic |
| 6130 | /// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of |
| 6131 | /// a vector of "i*". |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6132 | /// |
| 6133 | /// RequiresICE is filled in on return to indicate whether the value is required |
| 6134 | /// to be an Integer Constant Expression. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6135 | static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context, |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6136 | ASTContext::GetBuiltinTypeError &Error, |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6137 | bool &RequiresICE, |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6138 | bool AllowTypeModifiers) { |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6139 | // Modifiers. |
| 6140 | int HowLong = 0; |
| 6141 | bool Signed = false, Unsigned = false; |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6142 | RequiresICE = false; |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6143 | |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6144 | // Read the prefixed modifiers first. |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6145 | bool Done = false; |
| 6146 | while (!Done) { |
| 6147 | switch (*Str++) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6148 | default: Done = true; --Str; break; |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6149 | case 'I': |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6150 | RequiresICE = true; |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6151 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6152 | case 'S': |
| 6153 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 6154 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 6155 | Signed = true; |
| 6156 | break; |
| 6157 | case 'U': |
| 6158 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
| 6159 | assert(!Unsigned && "Can't use 'S' modifier multiple times!"); |
| 6160 | Unsigned = true; |
| 6161 | break; |
| 6162 | case 'L': |
| 6163 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 6164 | ++HowLong; |
| 6165 | break; |
| 6166 | } |
| 6167 | } |
| 6168 | |
| 6169 | QualType Type; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6170 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6171 | // Read the base type. |
| 6172 | switch (*Str++) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6173 | default: llvm_unreachable("Unknown builtin type letter!"); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6174 | case 'v': |
| 6175 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 6176 | "Bad modifiers used with 'v'!"); |
| 6177 | Type = Context.VoidTy; |
| 6178 | break; |
| 6179 | case 'f': |
| 6180 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 6181 | "Bad modifiers used with 'f'!"); |
| 6182 | Type = Context.FloatTy; |
| 6183 | break; |
| 6184 | case 'd': |
| 6185 | assert(HowLong < 2 && !Signed && !Unsigned && |
| 6186 | "Bad modifiers used with 'd'!"); |
| 6187 | if (HowLong) |
| 6188 | Type = Context.LongDoubleTy; |
| 6189 | else |
| 6190 | Type = Context.DoubleTy; |
| 6191 | break; |
| 6192 | case 's': |
| 6193 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 6194 | if (Unsigned) |
| 6195 | Type = Context.UnsignedShortTy; |
| 6196 | else |
| 6197 | Type = Context.ShortTy; |
| 6198 | break; |
| 6199 | case 'i': |
| 6200 | if (HowLong == 3) |
| 6201 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 6202 | else if (HowLong == 2) |
| 6203 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 6204 | else if (HowLong == 1) |
| 6205 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 6206 | else |
| 6207 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 6208 | break; |
| 6209 | case 'c': |
| 6210 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 6211 | if (Signed) |
| 6212 | Type = Context.SignedCharTy; |
| 6213 | else if (Unsigned) |
| 6214 | Type = Context.UnsignedCharTy; |
| 6215 | else |
| 6216 | Type = Context.CharTy; |
| 6217 | break; |
| 6218 | case 'b': // boolean |
| 6219 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 6220 | Type = Context.BoolTy; |
| 6221 | break; |
| 6222 | case 'z': // size_t. |
| 6223 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 6224 | Type = Context.getSizeType(); |
| 6225 | break; |
| 6226 | case 'F': |
| 6227 | Type = Context.getCFConstantStringType(); |
| 6228 | break; |
Fariborz Jahanian | d11da7e | 2010-11-09 21:38:20 +0000 | [diff] [blame] | 6229 | case 'G': |
| 6230 | Type = Context.getObjCIdType(); |
| 6231 | break; |
| 6232 | case 'H': |
| 6233 | Type = Context.getObjCSelType(); |
| 6234 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6235 | case 'a': |
| 6236 | Type = Context.getBuiltinVaListType(); |
| 6237 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 6238 | break; |
| 6239 | case 'A': |
| 6240 | // This is a "reference" to a va_list; however, what exactly |
| 6241 | // this means depends on how va_list is defined. There are two |
| 6242 | // different kinds of va_list: ones passed by value, and ones |
| 6243 | // passed by reference. An example of a by-value va_list is |
| 6244 | // x86, where va_list is a char*. An example of by-ref va_list |
| 6245 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 6246 | // we want this argument to be a char*&; for x86-64, we want |
| 6247 | // it to be a __va_list_tag*. |
| 6248 | Type = Context.getBuiltinVaListType(); |
| 6249 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6250 | if (Type->isArrayType()) |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6251 | Type = Context.getArrayDecayedType(Type); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6252 | else |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6253 | Type = Context.getLValueReferenceType(Type); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6254 | break; |
| 6255 | case 'V': { |
| 6256 | char *End; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6257 | unsigned NumElements = strtoul(Str, &End, 10); |
| 6258 | assert(End != Str && "Missing vector size"); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6259 | Str = End; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6260 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6261 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, |
| 6262 | RequiresICE, false); |
| 6263 | assert(!RequiresICE && "Can't require vector ICE"); |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6264 | |
| 6265 | // TODO: No way to make AltiVec vectors in builtins yet. |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 6266 | Type = Context.getVectorType(ElementType, NumElements, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 6267 | VectorType::GenericVector); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6268 | break; |
| 6269 | } |
Douglas Gregor | 40ef7c5 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 6270 | case 'X': { |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6271 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
| 6272 | false); |
| 6273 | assert(!RequiresICE && "Can't require complex ICE"); |
Douglas Gregor | 40ef7c5 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 6274 | Type = Context.getComplexType(ElementType); |
| 6275 | break; |
Fariborz Jahanian | 73952fc | 2011-08-23 23:33:09 +0000 | [diff] [blame] | 6276 | } |
| 6277 | case 'Y' : { |
| 6278 | Type = Context.getPointerDiffType(); |
| 6279 | break; |
| 6280 | } |
Chris Lattner | a58b3af | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 6281 | case 'P': |
Douglas Gregor | 27821ce | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 6282 | Type = Context.getFILEType(); |
| 6283 | if (Type.isNull()) { |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6284 | Error = ASTContext::GE_Missing_stdio; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6285 | return QualType(); |
| 6286 | } |
Mike Stump | 2adb4da | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 6287 | break; |
Chris Lattner | a58b3af | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 6288 | case 'J': |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6289 | if (Signed) |
Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 6290 | Type = Context.getsigjmp_bufType(); |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6291 | else |
| 6292 | Type = Context.getjmp_bufType(); |
| 6293 | |
Mike Stump | 2adb4da | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 6294 | if (Type.isNull()) { |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6295 | Error = ASTContext::GE_Missing_setjmp; |
Mike Stump | 2adb4da | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 6296 | return QualType(); |
| 6297 | } |
| 6298 | break; |
Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 6299 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6300 | |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6301 | // If there are modifiers and if we're allowed to parse them, go for it. |
| 6302 | Done = !AllowTypeModifiers; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6303 | while (!Done) { |
John McCall | b8b9466 | 2010-03-12 04:21:28 +0000 | [diff] [blame] | 6304 | switch (char c = *Str++) { |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6305 | default: Done = true; --Str; break; |
| 6306 | case '*': |
| 6307 | case '&': { |
| 6308 | // Both pointers and references can have their pointee types |
| 6309 | // qualified with an address space. |
| 6310 | char *End; |
| 6311 | unsigned AddrSpace = strtoul(Str, &End, 10); |
| 6312 | if (End != Str && AddrSpace != 0) { |
| 6313 | Type = Context.getAddrSpaceQualType(Type, AddrSpace); |
| 6314 | Str = End; |
| 6315 | } |
| 6316 | if (c == '*') |
| 6317 | Type = Context.getPointerType(Type); |
| 6318 | else |
| 6319 | Type = Context.getLValueReferenceType(Type); |
| 6320 | break; |
| 6321 | } |
| 6322 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 6323 | case 'C': |
| 6324 | Type = Type.withConst(); |
| 6325 | break; |
| 6326 | case 'D': |
| 6327 | Type = Context.getVolatileType(Type); |
| 6328 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6329 | } |
| 6330 | } |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6331 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6332 | assert((!RequiresICE || Type->isIntegralOrEnumerationType()) && |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6333 | "Integer constant 'I' type must be an integer"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6334 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6335 | return Type; |
| 6336 | } |
| 6337 | |
| 6338 | /// GetBuiltinType - Return the type for the specified builtin. |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6339 | QualType ASTContext::GetBuiltinType(unsigned Id, |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6340 | GetBuiltinTypeError &Error, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6341 | unsigned *IntegerConstantArgs) const { |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6342 | const char *TypeStr = BuiltinInfo.GetTypeString(Id); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6343 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6344 | SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6345 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6346 | bool RequiresICE = false; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6347 | Error = GE_None; |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6348 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error, |
| 6349 | RequiresICE, true); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6350 | if (Error != GE_None) |
| 6351 | return QualType(); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6352 | |
| 6353 | assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE"); |
| 6354 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6355 | while (TypeStr[0] && TypeStr[0] != '.') { |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6356 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6357 | if (Error != GE_None) |
| 6358 | return QualType(); |
| 6359 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6360 | // If this argument is required to be an IntegerConstantExpression and the |
| 6361 | // caller cares, fill in the bitmask we return. |
| 6362 | if (RequiresICE && IntegerConstantArgs) |
| 6363 | *IntegerConstantArgs |= 1 << ArgTypes.size(); |
| 6364 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6365 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 6366 | if (Ty->isArrayType()) |
| 6367 | Ty = getArrayDecayedType(Ty); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6368 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6369 | ArgTypes.push_back(Ty); |
| 6370 | } |
| 6371 | |
| 6372 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 6373 | "'.' should only occur at end of builtin type list!"); |
| 6374 | |
John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 6375 | FunctionType::ExtInfo EI; |
| 6376 | if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true); |
| 6377 | |
| 6378 | bool Variadic = (TypeStr[0] == '.'); |
| 6379 | |
| 6380 | // We really shouldn't be making a no-proto type here, especially in C++. |
| 6381 | if (ArgTypes.empty() && Variadic) |
| 6382 | return getFunctionNoProtoType(ResType, EI); |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 6383 | |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6384 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 6385 | EPI.ExtInfo = EI; |
| 6386 | EPI.Variadic = Variadic; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6387 | |
| 6388 | return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), EPI); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6389 | } |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 6390 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6391 | GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) { |
| 6392 | GVALinkage External = GVA_StrongExternal; |
| 6393 | |
| 6394 | Linkage L = FD->getLinkage(); |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6395 | switch (L) { |
| 6396 | case NoLinkage: |
| 6397 | case InternalLinkage: |
| 6398 | case UniqueExternalLinkage: |
| 6399 | return GVA_Internal; |
| 6400 | |
| 6401 | case ExternalLinkage: |
| 6402 | switch (FD->getTemplateSpecializationKind()) { |
| 6403 | case TSK_Undeclared: |
| 6404 | case TSK_ExplicitSpecialization: |
| 6405 | External = GVA_StrongExternal; |
| 6406 | break; |
| 6407 | |
| 6408 | case TSK_ExplicitInstantiationDefinition: |
| 6409 | return GVA_ExplicitTemplateInstantiation; |
| 6410 | |
| 6411 | case TSK_ExplicitInstantiationDeclaration: |
| 6412 | case TSK_ImplicitInstantiation: |
| 6413 | External = GVA_TemplateInstantiation; |
| 6414 | break; |
| 6415 | } |
| 6416 | } |
| 6417 | |
| 6418 | if (!FD->isInlined()) |
| 6419 | return External; |
| 6420 | |
| 6421 | if (!getLangOptions().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) { |
| 6422 | // GNU or C99 inline semantics. Determine whether this symbol should be |
| 6423 | // externally visible. |
| 6424 | if (FD->isInlineDefinitionExternallyVisible()) |
| 6425 | return External; |
| 6426 | |
| 6427 | // C99 inline semantics, where the symbol is not externally visible. |
| 6428 | return GVA_C99Inline; |
| 6429 | } |
| 6430 | |
| 6431 | // C++0x [temp.explicit]p9: |
| 6432 | // [ Note: The intent is that an inline function that is the subject of |
| 6433 | // an explicit instantiation declaration will still be implicitly |
| 6434 | // instantiated when used so that the body can be considered for |
| 6435 | // inlining, but that no out-of-line copy of the inline function would be |
| 6436 | // generated in the translation unit. -- end note ] |
| 6437 | if (FD->getTemplateSpecializationKind() |
| 6438 | == TSK_ExplicitInstantiationDeclaration) |
| 6439 | return GVA_C99Inline; |
| 6440 | |
| 6441 | return GVA_CXXInline; |
| 6442 | } |
| 6443 | |
| 6444 | GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) { |
| 6445 | // If this is a static data member, compute the kind of template |
| 6446 | // specialization. Otherwise, this variable is not part of a |
| 6447 | // template. |
| 6448 | TemplateSpecializationKind TSK = TSK_Undeclared; |
| 6449 | if (VD->isStaticDataMember()) |
| 6450 | TSK = VD->getTemplateSpecializationKind(); |
| 6451 | |
| 6452 | Linkage L = VD->getLinkage(); |
| 6453 | if (L == ExternalLinkage && getLangOptions().CPlusPlus && |
| 6454 | VD->getType()->getLinkage() == UniqueExternalLinkage) |
| 6455 | L = UniqueExternalLinkage; |
| 6456 | |
| 6457 | switch (L) { |
| 6458 | case NoLinkage: |
| 6459 | case InternalLinkage: |
| 6460 | case UniqueExternalLinkage: |
| 6461 | return GVA_Internal; |
| 6462 | |
| 6463 | case ExternalLinkage: |
| 6464 | switch (TSK) { |
| 6465 | case TSK_Undeclared: |
| 6466 | case TSK_ExplicitSpecialization: |
| 6467 | return GVA_StrongExternal; |
| 6468 | |
| 6469 | case TSK_ExplicitInstantiationDeclaration: |
| 6470 | llvm_unreachable("Variable should not be instantiated"); |
| 6471 | // Fall through to treat this like any other instantiation. |
| 6472 | |
| 6473 | case TSK_ExplicitInstantiationDefinition: |
| 6474 | return GVA_ExplicitTemplateInstantiation; |
| 6475 | |
| 6476 | case TSK_ImplicitInstantiation: |
| 6477 | return GVA_TemplateInstantiation; |
| 6478 | } |
| 6479 | } |
| 6480 | |
| 6481 | return GVA_StrongExternal; |
| 6482 | } |
| 6483 | |
Argyrios Kyrtzidis | c904933 | 2010-07-29 20:08:05 +0000 | [diff] [blame] | 6484 | bool ASTContext::DeclMustBeEmitted(const Decl *D) { |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6485 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 6486 | if (!VD->isFileVarDecl()) |
| 6487 | return false; |
Argyrios Kyrtzidis | a98e861 | 2011-09-13 21:35:00 +0000 | [diff] [blame] | 6488 | } else if (!isa<FunctionDecl>(D)) |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6489 | return false; |
| 6490 | |
Argyrios Kyrtzidis | 6e03a74 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 6491 | // Weak references don't produce any output by themselves. |
| 6492 | if (D->hasAttr<WeakRefAttr>()) |
| 6493 | return false; |
| 6494 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6495 | // Aliases and used decls are required. |
| 6496 | if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>()) |
| 6497 | return true; |
| 6498 | |
| 6499 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 6500 | // Forward declarations aren't required. |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 6501 | if (!FD->doesThisDeclarationHaveABody()) |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 6502 | return FD->doesDeclarationForceExternallyVisibleDefinition(); |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6503 | |
| 6504 | // Constructors and destructors are required. |
| 6505 | if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>()) |
| 6506 | return true; |
| 6507 | |
| 6508 | // The key function for a class is required. |
| 6509 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 6510 | const CXXRecordDecl *RD = MD->getParent(); |
| 6511 | if (MD->isOutOfLine() && RD->isDynamicClass()) { |
| 6512 | const CXXMethodDecl *KeyFunc = getKeyFunction(RD); |
| 6513 | if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl()) |
| 6514 | return true; |
| 6515 | } |
| 6516 | } |
| 6517 | |
| 6518 | GVALinkage Linkage = GetGVALinkageForFunction(FD); |
| 6519 | |
| 6520 | // static, static inline, always_inline, and extern inline functions can |
| 6521 | // always be deferred. Normal inline functions can be deferred in C99/C++. |
| 6522 | // Implicit template instantiations can also be deferred in C++. |
| 6523 | if (Linkage == GVA_Internal || Linkage == GVA_C99Inline || |
| 6524 | Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) |
| 6525 | return false; |
| 6526 | return true; |
| 6527 | } |
Douglas Gregor | 87d8124 | 2011-09-10 00:22:34 +0000 | [diff] [blame] | 6528 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6529 | const VarDecl *VD = cast<VarDecl>(D); |
| 6530 | assert(VD->isFileVarDecl() && "Expected file scoped var"); |
| 6531 | |
Argyrios Kyrtzidis | 6e03a74 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 6532 | if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly) |
| 6533 | return false; |
| 6534 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6535 | // Structs that have non-trivial constructors or destructors are required. |
| 6536 | |
| 6537 | // FIXME: Handle references. |
Alexis Hunt | f479f1b | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 6538 | // FIXME: Be more selective about which constructors we care about. |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6539 | if (const RecordType *RT = VD->getType()->getAs<RecordType>()) { |
| 6540 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Alexis Hunt | f479f1b | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 6541 | if (RD->hasDefinition() && !(RD->hasTrivialDefaultConstructor() && |
| 6542 | RD->hasTrivialCopyConstructor() && |
| 6543 | RD->hasTrivialMoveConstructor() && |
| 6544 | RD->hasTrivialDestructor())) |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6545 | return true; |
| 6546 | } |
| 6547 | } |
| 6548 | |
| 6549 | GVALinkage L = GetGVALinkageForVariable(VD); |
| 6550 | if (L == GVA_Internal || L == GVA_TemplateInstantiation) { |
| 6551 | if (!(VD->getInit() && VD->getInit()->HasSideEffects(*this))) |
| 6552 | return false; |
| 6553 | } |
| 6554 | |
| 6555 | return true; |
| 6556 | } |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 6557 | |
Charles Davis | 99202b3 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 6558 | CallingConv ASTContext::getDefaultMethodCallConv() { |
| 6559 | // Pass through to the C++ ABI object |
| 6560 | return ABI->getDefaultMethodCallConv(); |
| 6561 | } |
| 6562 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6563 | bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const { |
Anders Carlsson | 60a6263 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 6564 | // Pass through to the C++ ABI object |
| 6565 | return ABI->isNearlyEmpty(RD); |
| 6566 | } |
| 6567 | |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 6568 | MangleContext *ASTContext::createMangleContext() { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 6569 | switch (Target->getCXXABI()) { |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 6570 | case CXXABI_ARM: |
| 6571 | case CXXABI_Itanium: |
| 6572 | return createItaniumMangleContext(*this, getDiagnostics()); |
| 6573 | case CXXABI_Microsoft: |
| 6574 | return createMicrosoftMangleContext(*this, getDiagnostics()); |
| 6575 | } |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6576 | llvm_unreachable("Unsupported ABI"); |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 6577 | } |
| 6578 | |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 6579 | CXXABI::~CXXABI() {} |
Ted Kremenek | f5df0ce | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 6580 | |
| 6581 | size_t ASTContext::getSideTableAllocatedMemory() const { |
Ted Kremenek | 7d39c9a | 2011-07-27 18:41:12 +0000 | [diff] [blame] | 6582 | return ASTRecordLayouts.getMemorySize() |
| 6583 | + llvm::capacity_in_bytes(ObjCLayouts) |
| 6584 | + llvm::capacity_in_bytes(KeyFunctions) |
| 6585 | + llvm::capacity_in_bytes(ObjCImpls) |
| 6586 | + llvm::capacity_in_bytes(BlockVarCopyInits) |
| 6587 | + llvm::capacity_in_bytes(DeclAttrs) |
| 6588 | + llvm::capacity_in_bytes(InstantiatedFromStaticDataMember) |
| 6589 | + llvm::capacity_in_bytes(InstantiatedFromUsingDecl) |
| 6590 | + llvm::capacity_in_bytes(InstantiatedFromUsingShadowDecl) |
| 6591 | + llvm::capacity_in_bytes(InstantiatedFromUnnamedFieldDecl) |
| 6592 | + llvm::capacity_in_bytes(OverriddenMethods) |
| 6593 | + llvm::capacity_in_bytes(Types) |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 6594 | + llvm::capacity_in_bytes(VariableArrayTypes) |
Francois Pichet | 5792825 | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 6595 | + llvm::capacity_in_bytes(ClassScopeSpecializationPattern); |
Ted Kremenek | f5df0ce | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 6596 | } |
Ted Kremenek | 540017e | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 6597 | |
| 6598 | void ASTContext::setParameterIndex(const ParmVarDecl *D, unsigned int index) { |
| 6599 | ParamIndices[D] = index; |
| 6600 | } |
| 6601 | |
| 6602 | unsigned ASTContext::getParameterIndex(const ParmVarDecl *D) const { |
| 6603 | ParameterIndexTable::const_iterator I = ParamIndices.find(D); |
| 6604 | assert(I != ParamIndices.end() && |
| 6605 | "ParmIndices lacks entry set by ParmVarDecl"); |
| 6606 | return I->second; |
| 6607 | } |