Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the ASTContext interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/ASTContext.h" |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 15 | #include "clang/AST/CharUnits.h" |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclCXX.h" |
Steve Naroff | 980e508 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclTemplate.h" |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeLoc.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 20 | #include "clang/AST/Expr.h" |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 21 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExternalASTSource.h" |
Argyrios Kyrtzidis | 7b90340 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 23 | #include "clang/AST/ASTMutationListener.h" |
Anders Carlsson | 19cc4ab | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 24 | #include "clang/AST/RecordLayout.h" |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 25 | #include "clang/AST/Mangle.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 26 | #include "clang/Basic/Builtins.h" |
Chris Lattner | a9376d4 | 2009-03-28 03:45:20 +0000 | [diff] [blame] | 27 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 28 | #include "clang/Basic/TargetInfo.h" |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/SmallString.h" |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/StringExtras.h" |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 31 | #include "llvm/Support/MathExtras.h" |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 32 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | 0c8cd1a | 2011-07-27 18:41:12 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Capacity.h" |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 34 | #include "CXXABI.h" |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 35 | #include <map> |
Anders Carlsson | 29445a0 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 36 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 37 | using namespace clang; |
| 38 | |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 39 | unsigned ASTContext::NumImplicitDefaultConstructors; |
| 40 | unsigned ASTContext::NumImplicitDefaultConstructorsDeclared; |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 41 | unsigned ASTContext::NumImplicitCopyConstructors; |
| 42 | unsigned ASTContext::NumImplicitCopyConstructorsDeclared; |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 43 | unsigned ASTContext::NumImplicitMoveConstructors; |
| 44 | unsigned ASTContext::NumImplicitMoveConstructorsDeclared; |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 45 | unsigned ASTContext::NumImplicitCopyAssignmentOperators; |
| 46 | unsigned ASTContext::NumImplicitCopyAssignmentOperatorsDeclared; |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 47 | unsigned ASTContext::NumImplicitMoveAssignmentOperators; |
| 48 | unsigned ASTContext::NumImplicitMoveAssignmentOperatorsDeclared; |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 49 | unsigned ASTContext::NumImplicitDestructors; |
| 50 | unsigned ASTContext::NumImplicitDestructorsDeclared; |
| 51 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 52 | enum FloatingRank { |
| 53 | FloatRank, DoubleRank, LongDoubleRank |
| 54 | }; |
| 55 | |
Douglas Gregor | 3e1274f | 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 | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 61 | ID.AddBoolean(Parm->isParameterPack()); |
Douglas Gregor | 3e1274f | 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 | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 76 | ID.AddBoolean(NTTP->isParameterPack()); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 77 | ID.AddPointer(NTTP->getType().getAsOpaquePtr()); |
Douglas Gregor | 6952f1e | 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 | 3e1274f | 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 | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 96 | TemplateTemplateParmDecl *TTP) const { |
Douglas Gregor | 3e1274f | 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 | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 108 | SmallVector<NamedDecl *, 4> CanonParams; |
Douglas Gregor | 3e1274f | 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 | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 116 | SourceLocation(), |
| 117 | SourceLocation(), |
| 118 | TTP->getDepth(), |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 119 | TTP->getIndex(), 0, false, |
| 120 | TTP->isParameterPack())); |
| 121 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 6952f1e | 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 | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 127 | SmallVector<QualType, 2> ExpandedTypes; |
| 128 | SmallVector<TypeSourceInfo *, 2> ExpandedTInfos; |
Douglas Gregor | 6952f1e | 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 | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 136 | SourceLocation(), |
| 137 | SourceLocation(), |
Douglas Gregor | 6952f1e | 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 | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 147 | SourceLocation(), |
| 148 | SourceLocation(), |
Douglas Gregor | 6952f1e | 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 | 3e1274f | 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 | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 165 | TTP->getPosition(), |
| 166 | TTP->isParameterPack(), |
| 167 | 0, |
Douglas Gregor | 3e1274f | 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 | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 185 | CXXABI *ASTContext::createCXXABI(const TargetInfo &T) { |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 186 | if (!LangOpts.CPlusPlus) return 0; |
| 187 | |
Charles Davis | 20cf717 | 2010-08-19 02:18:14 +0000 | [diff] [blame] | 188 | switch (T.getCXXABI()) { |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 189 | case CXXABI_ARM: |
| 190 | return CreateARMCXXABI(*this); |
| 191 | case CXXABI_Itanium: |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 192 | return CreateItaniumCXXABI(*this); |
Charles Davis | 20cf717 | 2010-08-19 02:18:14 +0000 | [diff] [blame] | 193 | case CXXABI_Microsoft: |
| 194 | return CreateMicrosoftCXXABI(*this); |
| 195 | } |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 196 | return 0; |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 199 | static const LangAS::Map *getAddressSpaceMap(const TargetInfo &T, |
Peter Collingbourne | 207f4d8 | 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 | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 209 | return &FakeAddrSpaceMap; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 210 | } else { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 211 | return &T.getAddressSpaceMap(); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
Douglas Gregor | 3e3cd93 | 2011-09-01 20:23:19 +0000 | [diff] [blame] | 215 | ASTContext::ASTContext(LangOptions& LOpts, SourceManager &SM, |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 216 | const TargetInfo *t, |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 217 | IdentifierTable &idents, SelectorTable &sels, |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 218 | Builtin::Context &builtins, |
Douglas Gregor | bcfd1f5 | 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 | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 228 | CFConstantStringTypeDecl(0), ObjCInstanceTypeDecl(0), |
Douglas Gregor | bcfd1f5 | 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 | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 234 | AddrSpaceMap(0), Target(t), PrintingPolicy(LOpts), |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 235 | Idents(idents), Selectors(sels), |
| 236 | BuiltinInfo(builtins), |
| 237 | DeclarationNames(*this), |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 238 | ExternalSource(0), Listener(0), |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 239 | LastSDM(0, 0), |
| 240 | UniqueBlockByRefTypeID(0) |
| 241 | { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 242 | if (size_reserve > 0) Types.reserve(size_reserve); |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 243 | TUDecl = TranslationUnitDecl::Create(*this); |
Douglas Gregor | bcfd1f5 | 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 | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 251 | ASTContext::~ASTContext() { |
Ted Kremenek | 3478eb6 | 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 | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 255 | |
Douglas Gregor | 63fe86b | 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 | 0054531 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 259 | |
Douglas Gregor | 7d10b7e | 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 | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 265 | |
Ted Kremenek | dcfcfbe | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 266 | // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed |
Douglas Gregor | 63fe86b | 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 | dcfcfbe | 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 | 6320064 | 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 | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 287 | |
Douglas Gregor | 0054531 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 292 | void |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 293 | ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) { |
| 294 | ExternalSource.reset(Source.take()); |
| 295 | } |
| 296 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 297 | void ASTContext::PrintStats() const { |
Chandler Carruth | cd92a65 | 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 | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 300 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 301 | unsigned counts[] = { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 302 | #define TYPE(Name, Parent) 0, |
Douglas Gregor | dbe833d | 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 | c2ee10d | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 307 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 308 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 309 | Type *T = Types[i]; |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 310 | counts[(unsigned)T->getTypeClass()]++; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Douglas Gregor | dbe833d | 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 | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 317 | llvm::errs() << " " << counts[Idx] << " " << #Name \ |
| 318 | << " types\n"; \ |
Douglas Gregor | dbe833d | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 323 | |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 324 | llvm::errs() << "Total bytes = " << TotalBytes << "\n"; |
| 325 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 326 | // Implicit special member functions. |
Chandler Carruth | cd92a65 | 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"; |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 333 | if (getLangOptions().CPlusPlus) |
Chandler Carruth | cd92a65 | 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"; |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 340 | if (getLangOptions().CPlusPlus) |
Chandler Carruth | cd92a65 | 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 | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 348 | if (ExternalSource.get()) { |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 349 | llvm::errs() << "\n"; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 350 | ExternalSource->PrintStats(); |
| 351 | } |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 352 | |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 353 | BumpAlloc.PrintStats(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Douglas Gregor | 772eeae | 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 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 383 | |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 384 | void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 385 | BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 386 | R = CanQualType::CreateUnsafe(QualType(Ty, 0)); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 387 | Types.push_back(Ty); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Douglas Gregor | bcfd1f5 | 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"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 393 | assert(VoidTy.isNull() && "Context reinitialized?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 394 | |
Douglas Gregor | bcfd1f5 | 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 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 400 | // C99 6.2.5p19. |
| 401 | InitBuiltinType(VoidTy, BuiltinType::Void); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 402 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 403 | // C99 6.2.5p2. |
| 404 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
| 405 | // C99 6.2.5p3. |
Eli Friedman | 15b9176 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 406 | if (LangOpts.CharIsSigned) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 407 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 408 | else |
| 409 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
| 410 | // C99 6.2.5p4. |
| 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 416 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 417 | // C99 6.2.5p6. |
| 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 423 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 424 | // C99 6.2.5p10. |
| 425 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 426 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 427 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 428 | |
Chris Lattner | 2df9ced | 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 | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 433 | if (LangOpts.CPlusPlus) { // C++ 3.9.1p5 |
Eli Friedman | d3d77cd | 2011-04-30 19:24:24 +0000 | [diff] [blame] | 434 | if (TargetInfo::isTypeSigned(Target.getWCharType())) |
Chris Lattner | 3f59c97 | 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 | 3a25032 | 2009-02-26 23:43:47 +0000 | [diff] [blame] | 439 | WCharTy = getFromTargetType(Target.getWCharType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 440 | |
Alisdair Meredith | f5c209d | 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 | 898574e | 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 | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 457 | |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 458 | // Placeholder type for functions. |
| 459 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
| 460 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 461 | // Placeholder type for bound members. |
| 462 | InitBuiltinType(BoundMemberTy, BuiltinType::BoundMember); |
| 463 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 464 | // "any" type; useful for debugger-like clients. |
| 465 | InitBuiltinType(UnknownAnyTy, BuiltinType::UnknownAny); |
| 466 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 467 | // C99 6.2.5p11. |
| 468 | FloatComplexTy = getComplexType(FloatTy); |
| 469 | DoubleComplexTy = getComplexType(DoubleTy); |
| 470 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 471 | |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 472 | BuiltinVaListType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 473 | |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 474 | // Builtin types for 'id', 'Class', and 'SEL'. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 475 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); |
| 476 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 477 | InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 478 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 479 | ObjCConstantStringType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 480 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 481 | // void * type |
| 482 | VoidPtrTy = getPointerType(VoidTy); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 483 | |
| 484 | // nullptr type (C++0x 2.14.7) |
| 485 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 486 | } |
| 487 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 488 | DiagnosticsEngine &ASTContext::getDiagnostics() const { |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 489 | return SourceMgr.getDiagnostics(); |
| 490 | } |
| 491 | |
Douglas Gregor | 6320064 | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 492 | AttrVec& ASTContext::getDeclAttrs(const Decl *D) { |
| 493 | AttrVec *&Result = DeclAttrs[D]; |
| 494 | if (!Result) { |
| 495 | void *Mem = Allocate(sizeof(AttrVec)); |
| 496 | Result = new (Mem) AttrVec; |
| 497 | } |
| 498 | |
| 499 | return *Result; |
| 500 | } |
| 501 | |
| 502 | /// \brief Erase the attributes corresponding to the given declaration. |
| 503 | void ASTContext::eraseDeclAttrs(const Decl *D) { |
| 504 | llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D); |
| 505 | if (Pos != DeclAttrs.end()) { |
| 506 | Pos->second->~AttrVec(); |
| 507 | DeclAttrs.erase(Pos); |
| 508 | } |
| 509 | } |
| 510 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 511 | MemberSpecializationInfo * |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 512 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 513 | assert(Var->isStaticDataMember() && "Not a static data member"); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 514 | llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 515 | = InstantiatedFromStaticDataMember.find(Var); |
| 516 | if (Pos == InstantiatedFromStaticDataMember.end()) |
| 517 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 518 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 519 | return Pos->second; |
| 520 | } |
| 521 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 522 | void |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 523 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, |
Argyrios Kyrtzidis | 9421adc | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 524 | TemplateSpecializationKind TSK, |
| 525 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 526 | assert(Inst->isStaticDataMember() && "Not a static data member"); |
| 527 | assert(Tmpl->isStaticDataMember() && "Not a static data member"); |
| 528 | assert(!InstantiatedFromStaticDataMember[Inst] && |
| 529 | "Already noted what static data member was instantiated from"); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 530 | InstantiatedFromStaticDataMember[Inst] |
Argyrios Kyrtzidis | 9421adc | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 531 | = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 534 | FunctionDecl *ASTContext::getClassScopeSpecializationPattern( |
| 535 | const FunctionDecl *FD){ |
| 536 | assert(FD && "Specialization is 0"); |
| 537 | llvm::DenseMap<const FunctionDecl*, FunctionDecl *>::const_iterator Pos |
Francois Pichet | 0d95f0d | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 538 | = ClassScopeSpecializationPattern.find(FD); |
| 539 | if (Pos == ClassScopeSpecializationPattern.end()) |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 540 | return 0; |
| 541 | |
| 542 | return Pos->second; |
| 543 | } |
| 544 | |
| 545 | void ASTContext::setClassScopeSpecializationPattern(FunctionDecl *FD, |
| 546 | FunctionDecl *Pattern) { |
| 547 | assert(FD && "Specialization is 0"); |
| 548 | assert(Pattern && "Class scope specialization pattern is 0"); |
Francois Pichet | 0d95f0d | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 549 | ClassScopeSpecializationPattern[FD] = Pattern; |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 550 | } |
| 551 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 552 | NamedDecl * |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 553 | ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 554 | llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 555 | = InstantiatedFromUsingDecl.find(UUD); |
| 556 | if (Pos == InstantiatedFromUsingDecl.end()) |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 557 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 558 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 559 | return Pos->second; |
| 560 | } |
| 561 | |
| 562 | void |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 563 | ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) { |
| 564 | assert((isa<UsingDecl>(Pattern) || |
| 565 | isa<UnresolvedUsingValueDecl>(Pattern) || |
| 566 | isa<UnresolvedUsingTypenameDecl>(Pattern)) && |
| 567 | "pattern decl is not a using decl"); |
| 568 | assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists"); |
| 569 | InstantiatedFromUsingDecl[Inst] = Pattern; |
| 570 | } |
| 571 | |
| 572 | UsingShadowDecl * |
| 573 | ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) { |
| 574 | llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos |
| 575 | = InstantiatedFromUsingShadowDecl.find(Inst); |
| 576 | if (Pos == InstantiatedFromUsingShadowDecl.end()) |
| 577 | return 0; |
| 578 | |
| 579 | return Pos->second; |
| 580 | } |
| 581 | |
| 582 | void |
| 583 | ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, |
| 584 | UsingShadowDecl *Pattern) { |
| 585 | assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists"); |
| 586 | InstantiatedFromUsingShadowDecl[Inst] = Pattern; |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 589 | FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { |
| 590 | llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos |
| 591 | = InstantiatedFromUnnamedFieldDecl.find(Field); |
| 592 | if (Pos == InstantiatedFromUnnamedFieldDecl.end()) |
| 593 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 594 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 595 | return Pos->second; |
| 596 | } |
| 597 | |
| 598 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, |
| 599 | FieldDecl *Tmpl) { |
| 600 | assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed"); |
| 601 | assert(!Tmpl->getDeclName() && "Template field decl is not unnamed"); |
| 602 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] && |
| 603 | "Already noted what unnamed field was instantiated from"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 604 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 605 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; |
| 606 | } |
| 607 | |
Fariborz Jahanian | 14d56ef | 2011-04-27 17:14:21 +0000 | [diff] [blame] | 608 | bool ASTContext::ZeroBitfieldFollowsNonBitfield(const FieldDecl *FD, |
| 609 | const FieldDecl *LastFD) const { |
| 610 | return (FD->isBitField() && LastFD && !LastFD->isBitField() && |
Chad Rosier | 682b7f8 | 2011-08-04 21:50:29 +0000 | [diff] [blame] | 611 | FD->getBitWidth()->EvaluateAsInt(*this).getZExtValue() == 0); |
Fariborz Jahanian | 14d56ef | 2011-04-27 17:14:21 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Fariborz Jahanian | 340fa24 | 2011-05-02 17:20:56 +0000 | [diff] [blame] | 614 | bool ASTContext::ZeroBitfieldFollowsBitfield(const FieldDecl *FD, |
| 615 | const FieldDecl *LastFD) const { |
| 616 | return (FD->isBitField() && LastFD && LastFD->isBitField() && |
Chad Rosier | 682b7f8 | 2011-08-04 21:50:29 +0000 | [diff] [blame] | 617 | FD->getBitWidth()->EvaluateAsInt(*this).getZExtValue() == 0 && |
| 618 | LastFD->getBitWidth()->EvaluateAsInt(*this).getZExtValue() != 0); |
Fariborz Jahanian | 340fa24 | 2011-05-02 17:20:56 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Fariborz Jahanian | 9b3acaa | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 621 | bool ASTContext::BitfieldFollowsBitfield(const FieldDecl *FD, |
| 622 | const FieldDecl *LastFD) const { |
| 623 | return (FD->isBitField() && LastFD && LastFD->isBitField() && |
Chad Rosier | 682b7f8 | 2011-08-04 21:50:29 +0000 | [diff] [blame] | 624 | FD->getBitWidth()->EvaluateAsInt(*this).getZExtValue() && |
| 625 | LastFD->getBitWidth()->EvaluateAsInt(*this).getZExtValue()); |
Fariborz Jahanian | 9b3acaa | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 626 | } |
| 627 | |
Chad Rosier | dd7fddb | 2011-08-04 23:34:15 +0000 | [diff] [blame] | 628 | bool ASTContext::NonBitfieldFollowsBitfield(const FieldDecl *FD, |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 629 | const FieldDecl *LastFD) const { |
| 630 | return (!FD->isBitField() && LastFD && LastFD->isBitField() && |
Chad Rosier | 682b7f8 | 2011-08-04 21:50:29 +0000 | [diff] [blame] | 631 | LastFD->getBitWidth()->EvaluateAsInt(*this).getZExtValue()); |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Chad Rosier | dd7fddb | 2011-08-04 23:34:15 +0000 | [diff] [blame] | 634 | bool ASTContext::BitfieldFollowsNonBitfield(const FieldDecl *FD, |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 635 | const FieldDecl *LastFD) const { |
| 636 | return (FD->isBitField() && LastFD && !LastFD->isBitField() && |
Chad Rosier | 682b7f8 | 2011-08-04 21:50:29 +0000 | [diff] [blame] | 637 | FD->getBitWidth()->EvaluateAsInt(*this).getZExtValue()); |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 640 | ASTContext::overridden_cxx_method_iterator |
| 641 | ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const { |
| 642 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 643 | = OverriddenMethods.find(Method); |
| 644 | if (Pos == OverriddenMethods.end()) |
| 645 | return 0; |
| 646 | |
| 647 | return Pos->second.begin(); |
| 648 | } |
| 649 | |
| 650 | ASTContext::overridden_cxx_method_iterator |
| 651 | ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const { |
| 652 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 653 | = OverriddenMethods.find(Method); |
| 654 | if (Pos == OverriddenMethods.end()) |
| 655 | return 0; |
| 656 | |
| 657 | return Pos->second.end(); |
| 658 | } |
| 659 | |
Argyrios Kyrtzidis | c91e9f4 | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 660 | unsigned |
| 661 | ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const { |
| 662 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 663 | = OverriddenMethods.find(Method); |
| 664 | if (Pos == OverriddenMethods.end()) |
| 665 | return 0; |
| 666 | |
| 667 | return Pos->second.size(); |
| 668 | } |
| 669 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 670 | void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method, |
| 671 | const CXXMethodDecl *Overridden) { |
| 672 | OverriddenMethods[Method].push_back(Overridden); |
| 673 | } |
| 674 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 675 | //===----------------------------------------------------------------------===// |
| 676 | // Type Sizing and Analysis |
| 677 | //===----------------------------------------------------------------------===// |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 678 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 679 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 680 | /// scalar floating point type. |
| 681 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 682 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 683 | assert(BT && "Not a floating point type!"); |
| 684 | switch (BT->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 685 | default: llvm_unreachable("Not a floating point type!"); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 686 | case BuiltinType::Float: return Target->getFloatFormat(); |
| 687 | case BuiltinType::Double: return Target->getDoubleFormat(); |
| 688 | case BuiltinType::LongDouble: return Target->getLongDoubleFormat(); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 692 | /// getDeclAlign - Return a conservative estimate of the alignment of the |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 693 | /// specified decl. Note that bitfields do not have a valid alignment, so |
| 694 | /// this method will assert on them. |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 695 | /// If @p RefAsPointee, references are treated like their underlying type |
| 696 | /// (for alignof), else they're treated like pointers (for CodeGen). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 697 | CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) const { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 698 | unsigned Align = Target->getCharWidth(); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 699 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 700 | bool UseAlignAttrOnly = false; |
| 701 | if (unsigned AlignFromAttr = D->getMaxAlignment()) { |
| 702 | Align = AlignFromAttr; |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 703 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 704 | // __attribute__((aligned)) can increase or decrease alignment |
| 705 | // *except* on a struct or struct member, where it only increases |
| 706 | // alignment unless 'packed' is also specified. |
| 707 | // |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 708 | // It is an error for alignas to decrease alignment, so we can |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 709 | // ignore that possibility; Sema should diagnose it. |
| 710 | if (isa<FieldDecl>(D)) { |
| 711 | UseAlignAttrOnly = D->hasAttr<PackedAttr>() || |
| 712 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
| 713 | } else { |
| 714 | UseAlignAttrOnly = true; |
| 715 | } |
| 716 | } |
Fariborz Jahanian | 78a7d7d | 2011-05-05 21:19:14 +0000 | [diff] [blame] | 717 | else if (isa<FieldDecl>(D)) |
| 718 | UseAlignAttrOnly = |
| 719 | D->hasAttr<PackedAttr>() || |
| 720 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 721 | |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 722 | // If we're using the align attribute only, just ignore everything |
| 723 | // else about the declaration and its type. |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 724 | if (UseAlignAttrOnly) { |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 725 | // do nothing |
| 726 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 727 | } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 728 | QualType T = VD->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 729 | if (const ReferenceType* RT = T->getAs<ReferenceType>()) { |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 730 | if (RefAsPointee) |
| 731 | T = RT->getPointeeType(); |
| 732 | else |
| 733 | T = getPointerType(RT->getPointeeType()); |
| 734 | } |
| 735 | if (!T->isIncompleteType() && !T->isFunctionType()) { |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 736 | // Adjust alignments of declarations with array type by the |
| 737 | // large-array alignment on the target. |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 738 | unsigned MinWidth = Target->getLargeArrayMinWidth(); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 739 | const ArrayType *arrayType; |
| 740 | if (MinWidth && (arrayType = getAsArrayType(T))) { |
| 741 | if (isa<VariableArrayType>(arrayType)) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 742 | Align = std::max(Align, Target->getLargeArrayAlign()); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 743 | else if (isa<ConstantArrayType>(arrayType) && |
| 744 | MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType))) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 745 | Align = std::max(Align, Target->getLargeArrayAlign()); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 746 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 747 | // Walk through any array types while we're at it. |
| 748 | T = getBaseElementType(arrayType); |
| 749 | } |
Chad Rosier | 9f1210c | 2011-07-26 07:03:04 +0000 | [diff] [blame] | 750 | Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 751 | } |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 752 | |
| 753 | // Fields can be subject to extra alignment constraints, like if |
| 754 | // the field is packed, the struct is packed, or the struct has a |
| 755 | // a max-field-alignment constraint (#pragma pack). So calculate |
| 756 | // the actual alignment of the field within the struct, and then |
| 757 | // (as we're expected to) constrain that by the alignment of the type. |
| 758 | if (const FieldDecl *field = dyn_cast<FieldDecl>(VD)) { |
| 759 | // So calculate the alignment of the field. |
| 760 | const ASTRecordLayout &layout = getASTRecordLayout(field->getParent()); |
| 761 | |
| 762 | // Start with the record's overall alignment. |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 763 | unsigned fieldAlign = toBits(layout.getAlignment()); |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 764 | |
| 765 | // Use the GCD of that and the offset within the record. |
| 766 | uint64_t offset = layout.getFieldOffset(field->getFieldIndex()); |
| 767 | if (offset > 0) { |
| 768 | // Alignment is always a power of 2, so the GCD will be a power of 2, |
| 769 | // which means we get to do this crazy thing instead of Euclid's. |
| 770 | uint64_t lowBitOfOffset = offset & (~offset + 1); |
| 771 | if (lowBitOfOffset < fieldAlign) |
| 772 | fieldAlign = static_cast<unsigned>(lowBitOfOffset); |
| 773 | } |
| 774 | |
| 775 | Align = std::min(Align, fieldAlign); |
Charles Davis | 05f6247 | 2010-02-23 04:52:00 +0000 | [diff] [blame] | 776 | } |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 777 | } |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 778 | |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 779 | return toCharUnitsFromBits(Align); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 780 | } |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 781 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 782 | std::pair<CharUnits, CharUnits> |
Ken Dyck | bee5a79 | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 783 | ASTContext::getTypeInfoInChars(const Type *T) const { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 784 | std::pair<uint64_t, unsigned> Info = getTypeInfo(T); |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 785 | return std::make_pair(toCharUnitsFromBits(Info.first), |
| 786 | toCharUnitsFromBits(Info.second)); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | std::pair<CharUnits, CharUnits> |
Ken Dyck | bee5a79 | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 790 | ASTContext::getTypeInfoInChars(QualType T) const { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 791 | return getTypeInfoInChars(T.getTypePtr()); |
| 792 | } |
| 793 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 794 | /// getTypeSize - Return the size of the specified type, in bits. This method |
| 795 | /// does not work on incomplete types. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 796 | /// |
| 797 | /// FIXME: Pointers into different addr spaces could have different sizes and |
| 798 | /// alignment requirements: getPointerInfo should take an AddrSpace, this |
| 799 | /// should take a QualType, &c. |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 800 | std::pair<uint64_t, unsigned> |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 801 | ASTContext::getTypeInfo(const Type *T) const { |
Mike Stump | 5e30100 | 2009-02-27 18:32:39 +0000 | [diff] [blame] | 802 | uint64_t Width=0; |
| 803 | unsigned Align=8; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 804 | switch (T->getTypeClass()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 805 | #define TYPE(Class, Base) |
| 806 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 807 | #define NON_CANONICAL_TYPE(Class, Base) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 808 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 809 | #include "clang/AST/TypeNodes.def" |
John McCall | d3d49bb | 2011-06-28 16:49:23 +0000 | [diff] [blame] | 810 | llvm_unreachable("Should not see dependent types"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 811 | break; |
| 812 | |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 813 | case Type::FunctionNoProto: |
| 814 | case Type::FunctionProto: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 815 | // GCC extension: alignof(function) = 32 bits |
| 816 | Width = 0; |
| 817 | Align = 32; |
| 818 | break; |
| 819 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 820 | case Type::IncompleteArray: |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 821 | case Type::VariableArray: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 822 | Width = 0; |
| 823 | Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); |
| 824 | break; |
| 825 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 826 | case Type::ConstantArray: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 827 | const ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 828 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 829 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 830 | Width = EltInfo.first*CAT->getSize().getZExtValue(); |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 831 | Align = EltInfo.second; |
Argyrios Kyrtzidis | cd88b41 | 2011-04-26 21:05:39 +0000 | [diff] [blame] | 832 | Width = llvm::RoundUpToAlignment(Width, Align); |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 833 | break; |
Christopher Lamb | 5c09a02 | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 834 | } |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 835 | case Type::ExtVector: |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 836 | case Type::Vector: { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 837 | const VectorType *VT = cast<VectorType>(T); |
| 838 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType()); |
| 839 | Width = EltInfo.first*VT->getNumElements(); |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 840 | Align = Width; |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 841 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 842 | // This happens for non-power-of-2 length vectors. |
Dan Gohman | 8eefcd3 | 2010-04-21 23:32:43 +0000 | [diff] [blame] | 843 | if (Align & (Align-1)) { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 844 | Align = llvm::NextPowerOf2(Align); |
| 845 | Width = llvm::RoundUpToAlignment(Width, Align); |
| 846 | } |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 847 | break; |
| 848 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 849 | |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 850 | case Type::Builtin: |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 851 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 852 | default: llvm_unreachable("Unknown builtin type!"); |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 853 | case BuiltinType::Void: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 854 | // GCC extension: alignof(void) = 8 bits. |
| 855 | Width = 0; |
| 856 | Align = 8; |
| 857 | break; |
| 858 | |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 859 | case BuiltinType::Bool: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 860 | Width = Target->getBoolWidth(); |
| 861 | Align = Target->getBoolAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 862 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 863 | case BuiltinType::Char_S: |
| 864 | case BuiltinType::Char_U: |
| 865 | case BuiltinType::UChar: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 866 | case BuiltinType::SChar: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 867 | Width = Target->getCharWidth(); |
| 868 | Align = Target->getCharAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 869 | break; |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 870 | case BuiltinType::WChar_S: |
| 871 | case BuiltinType::WChar_U: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 872 | Width = Target->getWCharWidth(); |
| 873 | Align = Target->getWCharAlign(); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 874 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 875 | case BuiltinType::Char16: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 876 | Width = Target->getChar16Width(); |
| 877 | Align = Target->getChar16Align(); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 878 | break; |
| 879 | case BuiltinType::Char32: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 880 | Width = Target->getChar32Width(); |
| 881 | Align = Target->getChar32Align(); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 882 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 883 | case BuiltinType::UShort: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 884 | case BuiltinType::Short: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 885 | Width = Target->getShortWidth(); |
| 886 | Align = Target->getShortAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 887 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 888 | case BuiltinType::UInt: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 889 | case BuiltinType::Int: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 890 | Width = Target->getIntWidth(); |
| 891 | Align = Target->getIntAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 892 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 893 | case BuiltinType::ULong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 894 | case BuiltinType::Long: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 895 | Width = Target->getLongWidth(); |
| 896 | Align = Target->getLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 897 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 898 | case BuiltinType::ULongLong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 899 | case BuiltinType::LongLong: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 900 | Width = Target->getLongLongWidth(); |
| 901 | Align = Target->getLongLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 902 | break; |
Chris Lattner | ec16cb9 | 2009-04-30 02:55:13 +0000 | [diff] [blame] | 903 | case BuiltinType::Int128: |
| 904 | case BuiltinType::UInt128: |
| 905 | Width = 128; |
| 906 | Align = 128; // int128_t is 128-bit aligned on all targets. |
| 907 | break; |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 908 | case BuiltinType::Float: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 909 | Width = Target->getFloatWidth(); |
| 910 | Align = Target->getFloatAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 911 | break; |
| 912 | case BuiltinType::Double: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 913 | Width = Target->getDoubleWidth(); |
| 914 | Align = Target->getDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 915 | break; |
| 916 | case BuiltinType::LongDouble: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 917 | Width = Target->getLongDoubleWidth(); |
| 918 | Align = Target->getLongDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 919 | break; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 920 | case BuiltinType::NullPtr: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 921 | Width = Target->getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) |
| 922 | Align = Target->getPointerAlign(0); // == sizeof(void*) |
Sebastian Redl | 1590d9c | 2009-05-27 19:34:06 +0000 | [diff] [blame] | 923 | break; |
Fariborz Jahanian | e04f5fc | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 924 | case BuiltinType::ObjCId: |
| 925 | case BuiltinType::ObjCClass: |
| 926 | case BuiltinType::ObjCSel: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 927 | Width = Target->getPointerWidth(0); |
| 928 | Align = Target->getPointerAlign(0); |
Fariborz Jahanian | e04f5fc | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 929 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 930 | } |
Chris Lattner | bfef6d7 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 931 | break; |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 932 | case Type::ObjCObjectPointer: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 933 | Width = Target->getPointerWidth(0); |
| 934 | Align = Target->getPointerAlign(0); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 935 | break; |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 936 | case Type::BlockPointer: { |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 937 | unsigned AS = getTargetAddressSpace( |
| 938 | cast<BlockPointerType>(T)->getPointeeType()); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 939 | Width = Target->getPointerWidth(AS); |
| 940 | Align = Target->getPointerAlign(AS); |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 941 | break; |
| 942 | } |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 943 | case Type::LValueReference: |
| 944 | case Type::RValueReference: { |
| 945 | // alignof and sizeof should never enter this code path here, so we go |
| 946 | // the pointer route. |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 947 | unsigned AS = getTargetAddressSpace( |
| 948 | cast<ReferenceType>(T)->getPointeeType()); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 949 | Width = Target->getPointerWidth(AS); |
| 950 | Align = Target->getPointerAlign(AS); |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 951 | break; |
| 952 | } |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 953 | case Type::Pointer: { |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 954 | unsigned AS = getTargetAddressSpace(cast<PointerType>(T)->getPointeeType()); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 955 | Width = Target->getPointerWidth(AS); |
| 956 | Align = Target->getPointerAlign(AS); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 957 | break; |
| 958 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 959 | case Type::MemberPointer: { |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 960 | const MemberPointerType *MPT = cast<MemberPointerType>(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 961 | std::pair<uint64_t, unsigned> PtrDiffInfo = |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 962 | getTypeInfo(getPointerDiffType()); |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 963 | Width = PtrDiffInfo.first * ABI->getMemberPointerSize(MPT); |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 964 | Align = PtrDiffInfo.second; |
| 965 | break; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 966 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 967 | case Type::Complex: { |
| 968 | // Complex types have the same alignment as their elements, but twice the |
| 969 | // size. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 970 | std::pair<uint64_t, unsigned> EltInfo = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 971 | getTypeInfo(cast<ComplexType>(T)->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 972 | Width = EltInfo.first*2; |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 973 | Align = EltInfo.second; |
| 974 | break; |
| 975 | } |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 976 | case Type::ObjCObject: |
| 977 | return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr()); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 978 | case Type::ObjCInterface: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 979 | const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 980 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 981 | Width = toBits(Layout.getSize()); |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 982 | Align = toBits(Layout.getAlignment()); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 983 | break; |
| 984 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 985 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 986 | case Type::Enum: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 987 | const TagType *TT = cast<TagType>(T); |
| 988 | |
| 989 | if (TT->getDecl()->isInvalidDecl()) { |
Douglas Gregor | 22ce41d | 2011-04-20 17:29:44 +0000 | [diff] [blame] | 990 | Width = 8; |
| 991 | Align = 8; |
Chris Lattner | 8389eab | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 992 | break; |
| 993 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 994 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 995 | if (const EnumType *ET = dyn_cast<EnumType>(TT)) |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 996 | return getTypeInfo(ET->getDecl()->getIntegerType()); |
| 997 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 998 | const RecordType *RT = cast<RecordType>(TT); |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 999 | const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1000 | Width = toBits(Layout.getSize()); |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 1001 | Align = toBits(Layout.getAlignment()); |
Chris Lattner | dc0d73e | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 1002 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1003 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1004 | |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1005 | case Type::SubstTemplateTypeParm: |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1006 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> |
| 1007 | getReplacementType().getTypePtr()); |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1008 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1009 | case Type::Auto: { |
| 1010 | const AutoType *A = cast<AutoType>(T); |
| 1011 | assert(A->isDeduced() && "Cannot request the size of a dependent type"); |
Matt Beaumont-Gay | dc856af | 2011-02-22 20:00:16 +0000 | [diff] [blame] | 1012 | return getTypeInfo(A->getDeducedType().getTypePtr()); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1015 | case Type::Paren: |
| 1016 | return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr()); |
| 1017 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1018 | case Type::Typedef: { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1019 | const TypedefNameDecl *Typedef = cast<TypedefType>(T)->getDecl(); |
Douglas Gregor | df1367a | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 1020 | std::pair<uint64_t, unsigned> Info |
| 1021 | = getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); |
Chris Lattner | c1de52d | 2011-02-19 22:55:41 +0000 | [diff] [blame] | 1022 | // If the typedef has an aligned attribute on it, it overrides any computed |
| 1023 | // alignment we have. This violates the GCC documentation (which says that |
| 1024 | // attribute(aligned) can only round up) but matches its implementation. |
| 1025 | if (unsigned AttrAlign = Typedef->getMaxAlignment()) |
| 1026 | Align = AttrAlign; |
| 1027 | else |
| 1028 | Align = Info.second; |
Douglas Gregor | df1367a | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 1029 | Width = Info.first; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1030 | break; |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1031 | } |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1032 | |
| 1033 | case Type::TypeOfExpr: |
| 1034 | return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType() |
| 1035 | .getTypePtr()); |
| 1036 | |
| 1037 | case Type::TypeOf: |
| 1038 | return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr()); |
| 1039 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 1040 | case Type::Decltype: |
| 1041 | return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType() |
| 1042 | .getTypePtr()); |
| 1043 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1044 | case Type::UnaryTransform: |
| 1045 | return getTypeInfo(cast<UnaryTransformType>(T)->getUnderlyingType()); |
| 1046 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1047 | case Type::Elaborated: |
| 1048 | return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1049 | |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 1050 | case Type::Attributed: |
| 1051 | return getTypeInfo( |
| 1052 | cast<AttributedType>(T)->getEquivalentType().getTypePtr()); |
| 1053 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1054 | case Type::TemplateSpecialization: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1055 | assert(getCanonicalType(T) != T && |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1056 | "Cannot request the size of a dependent type"); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1057 | const TemplateSpecializationType *TST = cast<TemplateSpecializationType>(T); |
| 1058 | // A type alias template specialization may refer to a typedef with the |
| 1059 | // aligned attribute on it. |
| 1060 | if (TST->isTypeAlias()) |
| 1061 | return getTypeInfo(TST->getAliasedType().getTypePtr()); |
| 1062 | else |
| 1063 | return getTypeInfo(getCanonicalType(T)); |
| 1064 | } |
| 1065 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1066 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1067 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 1068 | assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2"); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1069 | return std::make_pair(Width, Align); |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1072 | /// toCharUnitsFromBits - Convert a size in bits to a size in characters. |
| 1073 | CharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const { |
| 1074 | return CharUnits::fromQuantity(BitSize / getCharWidth()); |
| 1075 | } |
| 1076 | |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1077 | /// toBits - Convert a size in characters to a size in characters. |
| 1078 | int64_t ASTContext::toBits(CharUnits CharSize) const { |
| 1079 | return CharSize.getQuantity() * getCharWidth(); |
| 1080 | } |
| 1081 | |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1082 | /// getTypeSizeInChars - Return the size of the specified type, in characters. |
| 1083 | /// This method does not work on incomplete types. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1084 | CharUnits ASTContext::getTypeSizeInChars(QualType T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1085 | return toCharUnitsFromBits(getTypeSize(T)); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1086 | } |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1087 | CharUnits ASTContext::getTypeSizeInChars(const Type *T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1088 | return toCharUnitsFromBits(getTypeSize(T)); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
Ken Dyck | 16e20cc | 2010-01-26 17:25:18 +0000 | [diff] [blame] | 1091 | /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1092 | /// characters. This method does not work on incomplete types. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1093 | CharUnits ASTContext::getTypeAlignInChars(QualType T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1094 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1095 | } |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1096 | CharUnits ASTContext::getTypeAlignInChars(const Type *T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1097 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1100 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 1101 | /// type for the current target in bits. This can be different than the ABI |
| 1102 | /// alignment in cases where it is beneficial for performance to overalign |
| 1103 | /// a data type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1104 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) const { |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1105 | unsigned ABIAlign = getTypeAlign(T); |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1106 | |
| 1107 | // Double and long long should be naturally aligned if possible. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1108 | if (const ComplexType* CT = T->getAs<ComplexType>()) |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1109 | T = CT->getElementType().getTypePtr(); |
| 1110 | if (T->isSpecificBuiltinType(BuiltinType::Double) || |
| 1111 | T->isSpecificBuiltinType(BuiltinType::LongLong)) |
| 1112 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); |
| 1113 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1114 | return ABIAlign; |
| 1115 | } |
| 1116 | |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1117 | /// DeepCollectObjCIvars - |
| 1118 | /// This routine first collects all declared, but not synthesized, ivars in |
| 1119 | /// super class and then collects all ivars, including those synthesized for |
| 1120 | /// current class. This routine is used for implementation of current class |
| 1121 | /// when all ivars, declared and synthesized are known. |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 1122 | /// |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1123 | void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 1124 | bool leafClass, |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 1125 | SmallVectorImpl<const ObjCIvarDecl*> &Ivars) const { |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1126 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
| 1127 | DeepCollectObjCIvars(SuperClass, false, Ivars); |
| 1128 | if (!leafClass) { |
| 1129 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 1130 | E = OI->ivar_end(); I != E; ++I) |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 1131 | Ivars.push_back(*I); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 1132 | } else { |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 1133 | ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI); |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 1134 | for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv; |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 1135 | Iv= Iv->getNextIvar()) |
| 1136 | Ivars.push_back(Iv); |
| 1137 | } |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1140 | /// CollectInheritedProtocols - Collect all protocols in current class and |
| 1141 | /// those inherited by it. |
| 1142 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 1143 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1144 | if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1145 | // We can use protocol_iterator here instead of |
| 1146 | // all_referenced_protocol_iterator since we are walking all categories. |
| 1147 | for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(), |
| 1148 | PE = OI->all_referenced_protocol_end(); P != PE; ++P) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1149 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 1150 | Protocols.insert(Proto); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1151 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 1152 | PE = Proto->protocol_end(); P != PE; ++P) { |
| 1153 | Protocols.insert(*P); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1154 | CollectInheritedProtocols(*P, Protocols); |
| 1155 | } |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 1156 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1157 | |
| 1158 | // Categories of this Interface. |
| 1159 | for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList(); |
| 1160 | CDeclChain; CDeclChain = CDeclChain->getNextClassCategory()) |
| 1161 | CollectInheritedProtocols(CDeclChain, Protocols); |
| 1162 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) |
| 1163 | while (SD) { |
| 1164 | CollectInheritedProtocols(SD, Protocols); |
| 1165 | SD = SD->getSuperClass(); |
| 1166 | } |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1167 | } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1168 | for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(), |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1169 | PE = OC->protocol_end(); P != PE; ++P) { |
| 1170 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 1171 | Protocols.insert(Proto); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1172 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 1173 | PE = Proto->protocol_end(); P != PE; ++P) |
| 1174 | CollectInheritedProtocols(*P, Protocols); |
| 1175 | } |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1176 | } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1177 | for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(), |
| 1178 | PE = OP->protocol_end(); P != PE; ++P) { |
| 1179 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 1180 | Protocols.insert(Proto); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1181 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 1182 | PE = Proto->protocol_end(); P != PE; ++P) |
| 1183 | CollectInheritedProtocols(*P, Protocols); |
| 1184 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1185 | } |
| 1186 | } |
| 1187 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1188 | unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const { |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1189 | unsigned count = 0; |
| 1190 | // Count ivars declared in class extension. |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 1191 | for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl; |
| 1192 | CDecl = CDecl->getNextClassExtension()) |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1193 | count += CDecl->ivar_size(); |
| 1194 | |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1195 | // Count ivar defined in this class's implementation. This |
| 1196 | // includes synthesized ivars. |
| 1197 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1198 | count += ImplDecl->ivar_size(); |
| 1199 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 1200 | return count; |
| 1201 | } |
| 1202 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1203 | /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. |
| 1204 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
| 1205 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1206 | I = ObjCImpls.find(D); |
| 1207 | if (I != ObjCImpls.end()) |
| 1208 | return cast<ObjCImplementationDecl>(I->second); |
| 1209 | return 0; |
| 1210 | } |
| 1211 | /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. |
| 1212 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
| 1213 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1214 | I = ObjCImpls.find(D); |
| 1215 | if (I != ObjCImpls.end()) |
| 1216 | return cast<ObjCCategoryImplDecl>(I->second); |
| 1217 | return 0; |
| 1218 | } |
| 1219 | |
| 1220 | /// \brief Set the implementation of ObjCInterfaceDecl. |
| 1221 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
| 1222 | ObjCImplementationDecl *ImplD) { |
| 1223 | assert(IFaceD && ImplD && "Passed null params"); |
| 1224 | ObjCImpls[IFaceD] = ImplD; |
| 1225 | } |
| 1226 | /// \brief Set the implementation of ObjCCategoryDecl. |
| 1227 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
| 1228 | ObjCCategoryImplDecl *ImplD) { |
| 1229 | assert(CatD && ImplD && "Passed null params"); |
| 1230 | ObjCImpls[CatD] = ImplD; |
| 1231 | } |
| 1232 | |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1233 | /// \brief Get the copy initialization expression of VarDecl,or NULL if |
| 1234 | /// none exists. |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1235 | Expr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) { |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1236 | assert(VD && "Passed null params"); |
| 1237 | assert(VD->hasAttr<BlocksAttr>() && |
| 1238 | "getBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1239 | llvm::DenseMap<const VarDecl*, Expr*>::iterator |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1240 | I = BlockVarCopyInits.find(VD); |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1241 | return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0; |
| 1242 | } |
| 1243 | |
| 1244 | /// \brief Set the copy inialization expression of a block var decl. |
| 1245 | void ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) { |
| 1246 | assert(VD && Init && "Passed null params"); |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1247 | assert(VD->hasAttr<BlocksAttr>() && |
| 1248 | "setBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1249 | BlockVarCopyInits[VD] = Init; |
| 1250 | } |
| 1251 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1252 | /// \brief Allocate an uninitialized TypeSourceInfo. |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1253 | /// |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1254 | /// The caller should initialize the memory held by TypeSourceInfo using |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1255 | /// the TypeLoc wrappers. |
| 1256 | /// |
| 1257 | /// \param T the type that will be the basis for type source info. This type |
| 1258 | /// should refer to how the declarator was written in source code, not to |
| 1259 | /// what type semantic analysis resolved the declarator to. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1260 | TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1261 | unsigned DataSize) const { |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1262 | if (!DataSize) |
| 1263 | DataSize = TypeLoc::getFullDataSizeForType(T); |
| 1264 | else |
| 1265 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1266 | "incorrect data size provided to CreateTypeSourceInfo!"); |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1267 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1268 | TypeSourceInfo *TInfo = |
| 1269 | (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8); |
| 1270 | new (TInfo) TypeSourceInfo(T); |
| 1271 | return TInfo; |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1274 | TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1275 | SourceLocation L) const { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1276 | TypeSourceInfo *DI = CreateTypeSourceInfo(T); |
Douglas Gregor | c21c7e9 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 1277 | DI->getTypeLoc().initialize(const_cast<ASTContext &>(*this), L); |
John McCall | a4eb74d | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 1278 | return DI; |
| 1279 | } |
| 1280 | |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1281 | const ASTRecordLayout & |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1282 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const { |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1283 | return getObjCLayout(D, 0); |
| 1284 | } |
| 1285 | |
| 1286 | const ASTRecordLayout & |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1287 | ASTContext::getASTObjCImplementationLayout( |
| 1288 | const ObjCImplementationDecl *D) const { |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1289 | return getObjCLayout(D->getClassInterface(), D); |
| 1290 | } |
| 1291 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1292 | //===----------------------------------------------------------------------===// |
| 1293 | // Type creation/memoization methods |
| 1294 | //===----------------------------------------------------------------------===// |
| 1295 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1296 | QualType |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1297 | ASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const { |
| 1298 | unsigned fastQuals = quals.getFastQualifiers(); |
| 1299 | quals.removeFastQualifiers(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1300 | |
| 1301 | // Check if we've already instantiated this type. |
| 1302 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1303 | ExtQuals::Profile(ID, baseType, quals); |
| 1304 | void *insertPos = 0; |
| 1305 | if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, insertPos)) { |
| 1306 | assert(eq->getQualifiers() == quals); |
| 1307 | return QualType(eq, fastQuals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1310 | // If the base type is not canonical, make the appropriate canonical type. |
| 1311 | QualType canon; |
| 1312 | if (!baseType->isCanonicalUnqualified()) { |
| 1313 | SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split(); |
| 1314 | canonSplit.second.addConsistentQualifiers(quals); |
| 1315 | canon = getExtQualType(canonSplit.first, canonSplit.second); |
| 1316 | |
| 1317 | // Re-find the insert position. |
| 1318 | (void) ExtQualNodes.FindNodeOrInsertPos(ID, insertPos); |
| 1319 | } |
| 1320 | |
| 1321 | ExtQuals *eq = new (*this, TypeAlignment) ExtQuals(baseType, canon, quals); |
| 1322 | ExtQualNodes.InsertNode(eq, insertPos); |
| 1323 | return QualType(eq, fastQuals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1326 | QualType |
| 1327 | ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) const { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1328 | QualType CanT = getCanonicalType(T); |
| 1329 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1330 | return T; |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1331 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1332 | // If we are composing extended qualifiers together, merge together |
| 1333 | // into one ExtQuals node. |
| 1334 | QualifierCollector Quals; |
| 1335 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1336 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1337 | // If this type already has an address space specified, it cannot get |
| 1338 | // another one. |
| 1339 | assert(!Quals.hasAddressSpace() && |
| 1340 | "Type cannot be in multiple addr spaces!"); |
| 1341 | Quals.addAddressSpace(AddressSpace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1342 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1343 | return getExtQualType(TypeNode, Quals); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1346 | QualType ASTContext::getObjCGCQualType(QualType T, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1347 | Qualifiers::GC GCAttr) const { |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1348 | QualType CanT = getCanonicalType(T); |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1349 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1350 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1351 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1352 | if (const PointerType *ptr = T->getAs<PointerType>()) { |
| 1353 | QualType Pointee = ptr->getPointeeType(); |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 1354 | if (Pointee->isAnyPointerType()) { |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1355 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 1356 | return getPointerType(ResultType); |
| 1357 | } |
| 1358 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1359 | |
John McCall | 0953e76 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1364 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1365 | // If this type already has an ObjCGC specified, it cannot get |
| 1366 | // another one. |
| 1367 | assert(!Quals.hasObjCGCAttr() && |
| 1368 | "Type cannot have multiple ObjCGCs!"); |
| 1369 | Quals.addObjCGCAttr(GCAttr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1370 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1371 | return getExtQualType(TypeNode, Quals); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1372 | } |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1373 | |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 1374 | const FunctionType *ASTContext::adjustFunctionType(const FunctionType *T, |
| 1375 | FunctionType::ExtInfo Info) { |
| 1376 | if (T->getExtInfo() == Info) |
| 1377 | return T; |
| 1378 | |
| 1379 | QualType Result; |
| 1380 | if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) { |
| 1381 | Result = getFunctionNoProtoType(FNPT->getResultType(), Info); |
| 1382 | } else { |
| 1383 | const FunctionProtoType *FPT = cast<FunctionProtoType>(T); |
| 1384 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 1385 | EPI.ExtInfo = Info; |
| 1386 | Result = getFunctionType(FPT->getResultType(), FPT->arg_type_begin(), |
| 1387 | FPT->getNumArgs(), EPI); |
| 1388 | } |
| 1389 | |
| 1390 | return cast<FunctionType>(Result.getTypePtr()); |
| 1391 | } |
| 1392 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1393 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 1394 | /// number with the specified element type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1395 | QualType ASTContext::getComplexType(QualType T) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1396 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1397 | // structure. |
| 1398 | llvm::FoldingSetNodeID ID; |
| 1399 | ComplexType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1400 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1401 | void *InsertPos = 0; |
| 1402 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1403 | return QualType(CT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1404 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1405 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1406 | // so fill in the canonical type field. |
| 1407 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1408 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1409 | Canonical = getComplexType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1410 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1411 | // Get the new insert position for the node we care about. |
| 1412 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1413 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1414 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1415 | ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1416 | Types.push_back(New); |
| 1417 | ComplexTypes.InsertNode(New, InsertPos); |
| 1418 | return QualType(New, 0); |
| 1419 | } |
| 1420 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1421 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 1422 | /// the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1423 | QualType ASTContext::getPointerType(QualType T) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1424 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1425 | // structure. |
| 1426 | llvm::FoldingSetNodeID ID; |
| 1427 | PointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1428 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1429 | void *InsertPos = 0; |
| 1430 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1431 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1432 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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 | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1436 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1437 | Canonical = getPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1438 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1439 | // Get the new insert position for the node we care about. |
| 1440 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1441 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1442 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1443 | PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1444 | Types.push_back(New); |
| 1445 | PointerTypes.InsertNode(New, InsertPos); |
| 1446 | return QualType(New, 0); |
| 1447 | } |
| 1448 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1449 | /// getBlockPointerType - Return the uniqued reference to the type for |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1450 | /// a pointer to the specified block. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1451 | QualType ASTContext::getBlockPointerType(QualType T) const { |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1452 | assert(T->isFunctionType() && "block of function types only"); |
| 1453 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1454 | // structure. |
| 1455 | llvm::FoldingSetNodeID ID; |
| 1456 | BlockPointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1457 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1458 | void *InsertPos = 0; |
| 1459 | if (BlockPointerType *PT = |
| 1460 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1461 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1462 | |
| 1463 | // If the block pointee type isn't canonical, this won't be a canonical |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1464 | // type either so fill in the canonical type field. |
| 1465 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1466 | if (!T.isCanonical()) { |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1467 | Canonical = getBlockPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1468 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1469 | // Get the new insert position for the node we care about. |
| 1470 | BlockPointerType *NewIP = |
| 1471 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1472 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1473 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1474 | BlockPointerType *New |
| 1475 | = new (*this, TypeAlignment) BlockPointerType(T, Canonical); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1476 | Types.push_back(New); |
| 1477 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 1478 | return QualType(New, 0); |
| 1479 | } |
| 1480 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1481 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 1482 | /// lvalue reference to the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1483 | QualType |
| 1484 | ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const { |
Douglas Gregor | 9625e44 | 2011-05-21 22:16:50 +0000 | [diff] [blame] | 1485 | assert(getCanonicalType(T) != OverloadTy && |
| 1486 | "Unresolved overloaded function type"); |
| 1487 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1488 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1489 | // structure. |
| 1490 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1491 | ReferenceType::Profile(ID, T, SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1492 | |
| 1493 | void *InsertPos = 0; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1494 | if (LValueReferenceType *RT = |
| 1495 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1496 | return QualType(RT, 0); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1497 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1498 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1499 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1500 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1501 | // either, so fill in the canonical type field. |
| 1502 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1503 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
| 1504 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1505 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1506 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1507 | // Get the new insert position for the node we care about. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1508 | LValueReferenceType *NewIP = |
| 1509 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1510 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1513 | LValueReferenceType *New |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1514 | = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, |
| 1515 | SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1516 | Types.push_back(New); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1517 | LValueReferenceTypes.InsertNode(New, InsertPos); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1518 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1519 | return QualType(New, 0); |
| 1520 | } |
| 1521 | |
| 1522 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 1523 | /// rvalue reference to the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1524 | QualType ASTContext::getRValueReferenceType(QualType T) const { |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1525 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1526 | // structure. |
| 1527 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1528 | ReferenceType::Profile(ID, T, false); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1529 | |
| 1530 | void *InsertPos = 0; |
| 1531 | if (RValueReferenceType *RT = |
| 1532 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1533 | return QualType(RT, 0); |
| 1534 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1535 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1536 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1537 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1538 | // either, so fill in the canonical type field. |
| 1539 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1540 | if (InnerRef || !T.isCanonical()) { |
| 1541 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1542 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1543 | |
| 1544 | // Get the new insert position for the node we care about. |
| 1545 | RValueReferenceType *NewIP = |
| 1546 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1547 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1550 | RValueReferenceType *New |
| 1551 | = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1552 | Types.push_back(New); |
| 1553 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1554 | return QualType(New, 0); |
| 1555 | } |
| 1556 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1557 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 1558 | /// member pointer to the specified type, in the specified class. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1559 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1560 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1561 | // structure. |
| 1562 | llvm::FoldingSetNodeID ID; |
| 1563 | MemberPointerType::Profile(ID, T, Cls); |
| 1564 | |
| 1565 | void *InsertPos = 0; |
| 1566 | if (MemberPointerType *PT = |
| 1567 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1568 | return QualType(PT, 0); |
| 1569 | |
| 1570 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 1571 | // type either, so fill in the canonical type field. |
| 1572 | QualType Canonical; |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1573 | if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1574 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 1575 | |
| 1576 | // Get the new insert position for the node we care about. |
| 1577 | MemberPointerType *NewIP = |
| 1578 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1579 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1580 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1581 | MemberPointerType *New |
| 1582 | = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1583 | Types.push_back(New); |
| 1584 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 1585 | return QualType(New, 0); |
| 1586 | } |
| 1587 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1588 | /// getConstantArrayType - Return the unique reference to the type for an |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1589 | /// array of the specified element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1590 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1591 | const llvm::APInt &ArySizeIn, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1592 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1593 | unsigned IndexTypeQuals) const { |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1594 | assert((EltTy->isDependentType() || |
| 1595 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && |
Eli Friedman | 587cbdf | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1596 | "Constant array of VLAs is illegal!"); |
| 1597 | |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1598 | // Convert the array size into a canonical width matching the pointer size for |
| 1599 | // the target. |
| 1600 | llvm::APInt ArySize(ArySizeIn); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1601 | ArySize = |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1602 | ArySize.zextOrTrunc(Target->getPointerWidth(getTargetAddressSpace(EltTy))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1603 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1604 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1605 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, IndexTypeQuals); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1606 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1607 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1608 | if (ConstantArrayType *ATP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1609 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1610 | return QualType(ATP, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1611 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1612 | // If the element type isn't canonical or has qualifiers, this won't |
| 1613 | // be a canonical type either, so fill in the canonical type field. |
| 1614 | QualType Canon; |
| 1615 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 1616 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
| 1617 | Canon = getConstantArrayType(QualType(canonSplit.first, 0), ArySize, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1618 | ASM, IndexTypeQuals); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1619 | Canon = getQualifiedType(Canon, canonSplit.second); |
| 1620 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1621 | // Get the new insert position for the node we care about. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1622 | ConstantArrayType *NewIP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1623 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1624 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1625 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1626 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1627 | ConstantArrayType *New = new(*this,TypeAlignment) |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1628 | ConstantArrayType(EltTy, Canon, ArySize, ASM, IndexTypeQuals); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1629 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1630 | Types.push_back(New); |
| 1631 | return QualType(New, 0); |
| 1632 | } |
| 1633 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1634 | /// getVariableArrayDecayedType - Turns the given type, which may be |
| 1635 | /// variably-modified, into the corresponding type with all the known |
| 1636 | /// sizes replaced with [*]. |
| 1637 | QualType ASTContext::getVariableArrayDecayedType(QualType type) const { |
| 1638 | // Vastly most common case. |
| 1639 | if (!type->isVariablyModifiedType()) return type; |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1640 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1641 | QualType result; |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1642 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1643 | SplitQualType split = type.getSplitDesugaredType(); |
| 1644 | const Type *ty = split.first; |
| 1645 | switch (ty->getTypeClass()) { |
| 1646 | #define TYPE(Class, Base) |
| 1647 | #define ABSTRACT_TYPE(Class, Base) |
| 1648 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 1649 | #include "clang/AST/TypeNodes.def" |
| 1650 | llvm_unreachable("didn't desugar past all non-canonical types?"); |
| 1651 | |
| 1652 | // These types should never be variably-modified. |
| 1653 | case Type::Builtin: |
| 1654 | case Type::Complex: |
| 1655 | case Type::Vector: |
| 1656 | case Type::ExtVector: |
| 1657 | case Type::DependentSizedExtVector: |
| 1658 | case Type::ObjCObject: |
| 1659 | case Type::ObjCInterface: |
| 1660 | case Type::ObjCObjectPointer: |
| 1661 | case Type::Record: |
| 1662 | case Type::Enum: |
| 1663 | case Type::UnresolvedUsing: |
| 1664 | case Type::TypeOfExpr: |
| 1665 | case Type::TypeOf: |
| 1666 | case Type::Decltype: |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1667 | case Type::UnaryTransform: |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1668 | case Type::DependentName: |
| 1669 | case Type::InjectedClassName: |
| 1670 | case Type::TemplateSpecialization: |
| 1671 | case Type::DependentTemplateSpecialization: |
| 1672 | case Type::TemplateTypeParm: |
| 1673 | case Type::SubstTemplateTypeParmPack: |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1674 | case Type::Auto: |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1675 | case Type::PackExpansion: |
| 1676 | llvm_unreachable("type should never be variably-modified"); |
| 1677 | |
| 1678 | // These types can be variably-modified but should never need to |
| 1679 | // further decay. |
| 1680 | case Type::FunctionNoProto: |
| 1681 | case Type::FunctionProto: |
| 1682 | case Type::BlockPointer: |
| 1683 | case Type::MemberPointer: |
| 1684 | return type; |
| 1685 | |
| 1686 | // These types can be variably-modified. All these modifications |
| 1687 | // preserve structure except as noted by comments. |
| 1688 | // TODO: if we ever care about optimizing VLAs, there are no-op |
| 1689 | // optimizations available here. |
| 1690 | case Type::Pointer: |
| 1691 | result = getPointerType(getVariableArrayDecayedType( |
| 1692 | cast<PointerType>(ty)->getPointeeType())); |
| 1693 | break; |
| 1694 | |
| 1695 | case Type::LValueReference: { |
| 1696 | const LValueReferenceType *lv = cast<LValueReferenceType>(ty); |
| 1697 | result = getLValueReferenceType( |
| 1698 | getVariableArrayDecayedType(lv->getPointeeType()), |
| 1699 | lv->isSpelledAsLValue()); |
| 1700 | break; |
| 1701 | } |
| 1702 | |
| 1703 | case Type::RValueReference: { |
| 1704 | const RValueReferenceType *lv = cast<RValueReferenceType>(ty); |
| 1705 | result = getRValueReferenceType( |
| 1706 | getVariableArrayDecayedType(lv->getPointeeType())); |
| 1707 | break; |
| 1708 | } |
| 1709 | |
| 1710 | case Type::ConstantArray: { |
| 1711 | const ConstantArrayType *cat = cast<ConstantArrayType>(ty); |
| 1712 | result = getConstantArrayType( |
| 1713 | getVariableArrayDecayedType(cat->getElementType()), |
| 1714 | cat->getSize(), |
| 1715 | cat->getSizeModifier(), |
| 1716 | cat->getIndexTypeCVRQualifiers()); |
| 1717 | break; |
| 1718 | } |
| 1719 | |
| 1720 | case Type::DependentSizedArray: { |
| 1721 | const DependentSizedArrayType *dat = cast<DependentSizedArrayType>(ty); |
| 1722 | result = getDependentSizedArrayType( |
| 1723 | getVariableArrayDecayedType(dat->getElementType()), |
| 1724 | dat->getSizeExpr(), |
| 1725 | dat->getSizeModifier(), |
| 1726 | dat->getIndexTypeCVRQualifiers(), |
| 1727 | dat->getBracketsRange()); |
| 1728 | break; |
| 1729 | } |
| 1730 | |
| 1731 | // Turn incomplete types into [*] types. |
| 1732 | case Type::IncompleteArray: { |
| 1733 | const IncompleteArrayType *iat = cast<IncompleteArrayType>(ty); |
| 1734 | result = getVariableArrayType( |
| 1735 | getVariableArrayDecayedType(iat->getElementType()), |
| 1736 | /*size*/ 0, |
| 1737 | ArrayType::Normal, |
| 1738 | iat->getIndexTypeCVRQualifiers(), |
| 1739 | SourceRange()); |
| 1740 | break; |
| 1741 | } |
| 1742 | |
| 1743 | // Turn VLA types into [*] types. |
| 1744 | case Type::VariableArray: { |
| 1745 | const VariableArrayType *vat = cast<VariableArrayType>(ty); |
| 1746 | result = getVariableArrayType( |
| 1747 | getVariableArrayDecayedType(vat->getElementType()), |
| 1748 | /*size*/ 0, |
| 1749 | ArrayType::Star, |
| 1750 | vat->getIndexTypeCVRQualifiers(), |
| 1751 | vat->getBracketsRange()); |
| 1752 | break; |
| 1753 | } |
| 1754 | } |
| 1755 | |
| 1756 | // Apply the top-level qualifiers from the original. |
| 1757 | return getQualifiedType(result, split.second); |
| 1758 | } |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1759 | |
Steve Naroff | bdbf7b0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 1760 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 1761 | /// variable array of the specified element type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1762 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 1763 | Expr *NumElts, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1764 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1765 | unsigned IndexTypeQuals, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1766 | SourceRange Brackets) const { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1767 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 1768 | // that have an expression provided for their size. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1769 | QualType Canon; |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 1770 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1771 | // Be sure to pull qualifiers off the element type. |
| 1772 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 1773 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
| 1774 | Canon = getVariableArrayType(QualType(canonSplit.first, 0), NumElts, ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1775 | IndexTypeQuals, Brackets); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1776 | Canon = getQualifiedType(Canon, canonSplit.second); |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 1777 | } |
| 1778 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1779 | VariableArrayType *New = new(*this, TypeAlignment) |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1780 | VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1781 | |
| 1782 | VariableArrayTypes.push_back(New); |
| 1783 | Types.push_back(New); |
| 1784 | return QualType(New, 0); |
| 1785 | } |
| 1786 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1787 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 1788 | /// the type for a dependently-sized array of the specified element |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1789 | /// type. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1790 | QualType ASTContext::getDependentSizedArrayType(QualType elementType, |
| 1791 | Expr *numElements, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1792 | ArrayType::ArraySizeModifier ASM, |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1793 | unsigned elementTypeQuals, |
| 1794 | SourceRange brackets) const { |
| 1795 | assert((!numElements || numElements->isTypeDependent() || |
| 1796 | numElements->isValueDependent()) && |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1797 | "Size must be type- or value-dependent!"); |
| 1798 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1799 | // Dependently-sized array types that do not have a specified number |
| 1800 | // of elements will have their sizes deduced from a dependent |
| 1801 | // initializer. We do no canonicalization here at all, which is okay |
| 1802 | // because they can't be used in most locations. |
| 1803 | if (!numElements) { |
| 1804 | DependentSizedArrayType *newType |
| 1805 | = new (*this, TypeAlignment) |
| 1806 | DependentSizedArrayType(*this, elementType, QualType(), |
| 1807 | numElements, ASM, elementTypeQuals, |
| 1808 | brackets); |
| 1809 | Types.push_back(newType); |
| 1810 | return QualType(newType, 0); |
| 1811 | } |
| 1812 | |
| 1813 | // Otherwise, we actually build a new type every time, but we |
| 1814 | // also build a canonical type. |
| 1815 | |
| 1816 | SplitQualType canonElementType = getCanonicalType(elementType).split(); |
| 1817 | |
| 1818 | void *insertPos = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1819 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1820 | DependentSizedArrayType::Profile(ID, *this, |
| 1821 | QualType(canonElementType.first, 0), |
| 1822 | ASM, elementTypeQuals, numElements); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1823 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1824 | // Look for an existing type with these properties. |
| 1825 | DependentSizedArrayType *canonTy = |
| 1826 | DependentSizedArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1827 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1828 | // If we don't have one, build one. |
| 1829 | if (!canonTy) { |
| 1830 | canonTy = new (*this, TypeAlignment) |
| 1831 | DependentSizedArrayType(*this, QualType(canonElementType.first, 0), |
| 1832 | QualType(), numElements, ASM, elementTypeQuals, |
| 1833 | brackets); |
| 1834 | DependentSizedArrayTypes.InsertNode(canonTy, insertPos); |
| 1835 | Types.push_back(canonTy); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1838 | // Apply qualifiers from the element type to the array. |
| 1839 | QualType canon = getQualifiedType(QualType(canonTy,0), |
| 1840 | canonElementType.second); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1841 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1842 | // If we didn't need extra canonicalization for the element type, |
| 1843 | // then just use that as our result. |
| 1844 | if (QualType(canonElementType.first, 0) == elementType) |
| 1845 | return canon; |
| 1846 | |
| 1847 | // Otherwise, we need to build a type which follows the spelling |
| 1848 | // of the element type. |
| 1849 | DependentSizedArrayType *sugaredType |
| 1850 | = new (*this, TypeAlignment) |
| 1851 | DependentSizedArrayType(*this, elementType, canon, numElements, |
| 1852 | ASM, elementTypeQuals, brackets); |
| 1853 | Types.push_back(sugaredType); |
| 1854 | return QualType(sugaredType, 0); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1855 | } |
| 1856 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1857 | QualType ASTContext::getIncompleteArrayType(QualType elementType, |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1858 | ArrayType::ArraySizeModifier ASM, |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1859 | unsigned elementTypeQuals) const { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1860 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1861 | IncompleteArrayType::Profile(ID, elementType, ASM, elementTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1862 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1863 | void *insertPos = 0; |
| 1864 | if (IncompleteArrayType *iat = |
| 1865 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos)) |
| 1866 | return QualType(iat, 0); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1867 | |
| 1868 | // If the element type isn't canonical, this won't be a canonical type |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1869 | // either, so fill in the canonical type field. We also have to pull |
| 1870 | // qualifiers off the element type. |
| 1871 | QualType canon; |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1872 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1873 | if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) { |
| 1874 | SplitQualType canonSplit = getCanonicalType(elementType).split(); |
| 1875 | canon = getIncompleteArrayType(QualType(canonSplit.first, 0), |
| 1876 | ASM, elementTypeQuals); |
| 1877 | canon = getQualifiedType(canon, canonSplit.second); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1878 | |
| 1879 | // Get the new insert position for the node we care about. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1880 | IncompleteArrayType *existing = |
| 1881 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
| 1882 | assert(!existing && "Shouldn't be in the map!"); (void) existing; |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1883 | } |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1884 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1885 | IncompleteArrayType *newType = new (*this, TypeAlignment) |
| 1886 | IncompleteArrayType(elementType, canon, ASM, elementTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1887 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1888 | IncompleteArrayTypes.InsertNode(newType, insertPos); |
| 1889 | Types.push_back(newType); |
| 1890 | return QualType(newType, 0); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1891 | } |
| 1892 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1893 | /// getVectorType - Return the unique reference to a vector type of |
| 1894 | /// the specified element type and size. VectorType must be a built-in type. |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1895 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1896 | VectorType::VectorKind VecKind) const { |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1897 | assert(vecType->isBuiltinType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1898 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1899 | // Check if we've already instantiated a vector of this type. |
| 1900 | llvm::FoldingSetNodeID ID; |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1901 | VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind); |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1902 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1903 | void *InsertPos = 0; |
| 1904 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1905 | return QualType(VTP, 0); |
| 1906 | |
| 1907 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1908 | // so fill in the canonical type field. |
| 1909 | QualType Canonical; |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1910 | if (!vecType.isCanonical()) { |
Bob Wilson | 231da7e | 2010-11-16 00:32:20 +0000 | [diff] [blame] | 1911 | Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1912 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1913 | // Get the new insert position for the node we care about. |
| 1914 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1915 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1916 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1917 | VectorType *New = new (*this, TypeAlignment) |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1918 | VectorType(vecType, NumElts, Canonical, VecKind); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1919 | VectorTypes.InsertNode(New, InsertPos); |
| 1920 | Types.push_back(New); |
| 1921 | return QualType(New, 0); |
| 1922 | } |
| 1923 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1924 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1925 | /// the specified element type and size. VectorType must be a built-in type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1926 | QualType |
| 1927 | ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) const { |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1928 | assert(vecType->isBuiltinType() || vecType->isDependentType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1929 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1930 | // Check if we've already instantiated a vector of this type. |
| 1931 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1932 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1933 | VectorType::GenericVector); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1934 | void *InsertPos = 0; |
| 1935 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1936 | return QualType(VTP, 0); |
| 1937 | |
| 1938 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1939 | // so fill in the canonical type field. |
| 1940 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1941 | if (!vecType.isCanonical()) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1942 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1943 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1944 | // Get the new insert position for the node we care about. |
| 1945 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1946 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1947 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1948 | ExtVectorType *New = new (*this, TypeAlignment) |
| 1949 | ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1950 | VectorTypes.InsertNode(New, InsertPos); |
| 1951 | Types.push_back(New); |
| 1952 | return QualType(New, 0); |
| 1953 | } |
| 1954 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1955 | QualType |
| 1956 | ASTContext::getDependentSizedExtVectorType(QualType vecType, |
| 1957 | Expr *SizeExpr, |
| 1958 | SourceLocation AttrLoc) const { |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1959 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1960 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1961 | SizeExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1962 | |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1963 | void *InsertPos = 0; |
| 1964 | DependentSizedExtVectorType *Canon |
| 1965 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1966 | DependentSizedExtVectorType *New; |
| 1967 | if (Canon) { |
| 1968 | // We already have a canonical version of this array type; use it as |
| 1969 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1970 | New = new (*this, TypeAlignment) |
| 1971 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), |
| 1972 | SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1973 | } else { |
| 1974 | QualType CanonVecTy = getCanonicalType(vecType); |
| 1975 | if (CanonVecTy == vecType) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1976 | New = new (*this, TypeAlignment) |
| 1977 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, |
| 1978 | AttrLoc); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1979 | |
| 1980 | DependentSizedExtVectorType *CanonCheck |
| 1981 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1982 | assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken"); |
| 1983 | (void)CanonCheck; |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1984 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); |
| 1985 | } else { |
| 1986 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 1987 | SourceLocation()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1988 | New = new (*this, TypeAlignment) |
| 1989 | DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1990 | } |
| 1991 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1992 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1993 | Types.push_back(New); |
| 1994 | return QualType(New, 0); |
| 1995 | } |
| 1996 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1997 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1998 | /// |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1999 | QualType |
| 2000 | ASTContext::getFunctionNoProtoType(QualType ResultTy, |
| 2001 | const FunctionType::ExtInfo &Info) const { |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2002 | const CallingConv DefaultCC = Info.getCC(); |
| 2003 | const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ? |
| 2004 | CC_X86StdCall : DefaultCC; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2005 | // Unique functions, to guarantee there is only one function of a particular |
| 2006 | // structure. |
| 2007 | llvm::FoldingSetNodeID ID; |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2008 | FunctionNoProtoType::Profile(ID, ResultTy, Info); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2009 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2010 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2011 | if (FunctionNoProtoType *FT = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2012 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2013 | return QualType(FT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2014 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2015 | QualType Canonical; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 2016 | if (!ResultTy.isCanonical() || |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2017 | getCanonicalCallConv(CallConv) != CallConv) { |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2018 | Canonical = |
| 2019 | getFunctionNoProtoType(getCanonicalType(ResultTy), |
| 2020 | Info.withCallingConv(getCanonicalCallConv(CallConv))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2021 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2022 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2023 | FunctionNoProtoType *NewIP = |
| 2024 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2025 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2026 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2027 | |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2028 | FunctionProtoType::ExtInfo newInfo = Info.withCallingConv(CallConv); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2029 | FunctionNoProtoType *New = new (*this, TypeAlignment) |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2030 | FunctionNoProtoType(ResultTy, Canonical, newInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2031 | Types.push_back(New); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2032 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2033 | return QualType(New, 0); |
| 2034 | } |
| 2035 | |
| 2036 | /// getFunctionType - Return a normal function type with a typed argument |
| 2037 | /// list. isVariadic indicates whether the argument list includes '...'. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2038 | QualType |
| 2039 | ASTContext::getFunctionType(QualType ResultTy, |
| 2040 | const QualType *ArgArray, unsigned NumArgs, |
| 2041 | const FunctionProtoType::ExtProtoInfo &EPI) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2042 | // Unique functions, to guarantee there is only one function of a particular |
| 2043 | // structure. |
| 2044 | llvm::FoldingSetNodeID ID; |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2045 | FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, EPI, *this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2046 | |
| 2047 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2048 | if (FunctionProtoType *FTP = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2049 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2050 | return QualType(FTP, 0); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2051 | |
| 2052 | // Determine whether the type being created is already canonical or not. |
Sebastian Redl | 8b5b409 | 2011-03-06 10:52:04 +0000 | [diff] [blame] | 2053 | bool isCanonical= EPI.ExceptionSpecType == EST_None && ResultTy.isCanonical(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2054 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2055 | if (!ArgArray[i].isCanonicalAsParam()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2056 | isCanonical = false; |
| 2057 | |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2058 | const CallingConv DefaultCC = EPI.ExtInfo.getCC(); |
| 2059 | const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ? |
| 2060 | CC_X86StdCall : DefaultCC; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2061 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2062 | // If this type isn't canonical, get the canonical version of it. |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2063 | // The exception spec is not part of the canonical type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2064 | QualType Canonical; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2065 | if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2066 | SmallVector<QualType, 16> CanonicalArgs; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2067 | CanonicalArgs.reserve(NumArgs); |
| 2068 | for (unsigned i = 0; i != NumArgs; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2069 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2070 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2071 | FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI; |
Sebastian Redl | 8b5b409 | 2011-03-06 10:52:04 +0000 | [diff] [blame] | 2072 | CanonicalEPI.ExceptionSpecType = EST_None; |
| 2073 | CanonicalEPI.NumExceptions = 0; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2074 | CanonicalEPI.ExtInfo |
| 2075 | = CanonicalEPI.ExtInfo.withCallingConv(getCanonicalCallConv(CallConv)); |
| 2076 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2077 | Canonical = getFunctionType(getCanonicalType(ResultTy), |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2078 | CanonicalArgs.data(), NumArgs, |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2079 | CanonicalEPI); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2080 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2081 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2082 | FunctionProtoType *NewIP = |
| 2083 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2084 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2085 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2086 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2087 | // FunctionProtoType objects are allocated with extra bytes after |
| 2088 | // them for three variable size arrays at the end: |
| 2089 | // - parameter types |
| 2090 | // - exception types |
| 2091 | // - consumed-arguments flags |
| 2092 | // Instead of the exception types, there could be a noexcept |
| 2093 | // expression. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2094 | size_t Size = sizeof(FunctionProtoType) + |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2095 | NumArgs * sizeof(QualType); |
| 2096 | if (EPI.ExceptionSpecType == EST_Dynamic) |
| 2097 | Size += EPI.NumExceptions * sizeof(QualType); |
| 2098 | else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) { |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2099 | Size += sizeof(Expr*); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2100 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2101 | if (EPI.ConsumedArguments) |
| 2102 | Size += NumArgs * sizeof(bool); |
| 2103 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2104 | FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment); |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2105 | FunctionProtoType::ExtProtoInfo newEPI = EPI; |
| 2106 | newEPI.ExtInfo = EPI.ExtInfo.withCallingConv(CallConv); |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2107 | new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, Canonical, newEPI); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2108 | Types.push_back(FTP); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2109 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2110 | return QualType(FTP, 0); |
| 2111 | } |
| 2112 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2113 | #ifndef NDEBUG |
| 2114 | static bool NeedsInjectedClassNameType(const RecordDecl *D) { |
| 2115 | if (!isa<CXXRecordDecl>(D)) return false; |
| 2116 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 2117 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) |
| 2118 | return true; |
| 2119 | if (RD->getDescribedClassTemplate() && |
| 2120 | !isa<ClassTemplateSpecializationDecl>(RD)) |
| 2121 | return true; |
| 2122 | return false; |
| 2123 | } |
| 2124 | #endif |
| 2125 | |
| 2126 | /// getInjectedClassNameType - Return the unique reference to the |
| 2127 | /// injected class name type for the specified templated declaration. |
| 2128 | QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2129 | QualType TST) const { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2130 | assert(NeedsInjectedClassNameType(Decl)); |
| 2131 | if (Decl->TypeForDecl) { |
| 2132 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
Argyrios Kyrtzidis | 37ffed3 | 2010-07-02 11:55:32 +0000 | [diff] [blame] | 2133 | } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDeclaration()) { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2134 | assert(PrevDecl->TypeForDecl && "previous declaration has no type"); |
| 2135 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 2136 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
| 2137 | } else { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2138 | Type *newType = |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2139 | new (*this, TypeAlignment) InjectedClassNameType(Decl, TST); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2140 | Decl->TypeForDecl = newType; |
| 2141 | Types.push_back(newType); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2142 | } |
| 2143 | return QualType(Decl->TypeForDecl, 0); |
| 2144 | } |
| 2145 | |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2146 | /// getTypeDeclType - Return the unique reference to the type for the |
| 2147 | /// specified type declaration. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2148 | QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const { |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 2149 | assert(Decl && "Passed null for Decl param"); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2150 | assert(!Decl->TypeForDecl && "TypeForDecl present in slow case"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2151 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2152 | if (const TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2153 | return getTypedefType(Typedef); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2154 | |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2155 | assert(!isa<TemplateTypeParmDecl>(Decl) && |
| 2156 | "Template type parameter types are always available."); |
| 2157 | |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2158 | if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2159 | assert(!Record->getPreviousDeclaration() && |
| 2160 | "struct/union has previous declaration"); |
| 2161 | assert(!NeedsInjectedClassNameType(Record)); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2162 | return getRecordType(Record); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2163 | } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2164 | assert(!Enum->getPreviousDeclaration() && |
| 2165 | "enum has previous declaration"); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2166 | return getEnumType(Enum); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2167 | } else if (const UnresolvedUsingTypenameDecl *Using = |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2168 | dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2169 | Type *newType = new (*this, TypeAlignment) UnresolvedUsingType(Using); |
| 2170 | Decl->TypeForDecl = newType; |
| 2171 | Types.push_back(newType); |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 2172 | } else |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2173 | llvm_unreachable("TypeDecl without a type?"); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 2174 | |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 2175 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2176 | } |
| 2177 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2178 | /// getTypedefType - Return the unique reference to the type for the |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2179 | /// specified typedef name decl. |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2180 | QualType |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2181 | ASTContext::getTypedefType(const TypedefNameDecl *Decl, |
| 2182 | QualType Canonical) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2183 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2184 | |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2185 | if (Canonical.isNull()) |
| 2186 | Canonical = getCanonicalType(Decl->getUnderlyingType()); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2187 | TypedefType *newType = new(*this, TypeAlignment) |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2188 | TypedefType(Type::Typedef, Decl, Canonical); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2189 | Decl->TypeForDecl = newType; |
| 2190 | Types.push_back(newType); |
| 2191 | return QualType(newType, 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2192 | } |
| 2193 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2194 | QualType ASTContext::getRecordType(const RecordDecl *Decl) const { |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2195 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 2196 | |
| 2197 | if (const RecordDecl *PrevDecl = Decl->getPreviousDeclaration()) |
| 2198 | if (PrevDecl->TypeForDecl) |
| 2199 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 2200 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2201 | RecordType *newType = new (*this, TypeAlignment) RecordType(Decl); |
| 2202 | Decl->TypeForDecl = newType; |
| 2203 | Types.push_back(newType); |
| 2204 | return QualType(newType, 0); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2205 | } |
| 2206 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2207 | QualType ASTContext::getEnumType(const EnumDecl *Decl) const { |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2208 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 2209 | |
| 2210 | if (const EnumDecl *PrevDecl = Decl->getPreviousDeclaration()) |
| 2211 | if (PrevDecl->TypeForDecl) |
| 2212 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 2213 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2214 | EnumType *newType = new (*this, TypeAlignment) EnumType(Decl); |
| 2215 | Decl->TypeForDecl = newType; |
| 2216 | Types.push_back(newType); |
| 2217 | return QualType(newType, 0); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2218 | } |
| 2219 | |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 2220 | QualType ASTContext::getAttributedType(AttributedType::Kind attrKind, |
| 2221 | QualType modifiedType, |
| 2222 | QualType equivalentType) { |
| 2223 | llvm::FoldingSetNodeID id; |
| 2224 | AttributedType::Profile(id, attrKind, modifiedType, equivalentType); |
| 2225 | |
| 2226 | void *insertPos = 0; |
| 2227 | AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos); |
| 2228 | if (type) return QualType(type, 0); |
| 2229 | |
| 2230 | QualType canon = getCanonicalType(equivalentType); |
| 2231 | type = new (*this, TypeAlignment) |
| 2232 | AttributedType(canon, attrKind, modifiedType, equivalentType); |
| 2233 | |
| 2234 | Types.push_back(type); |
| 2235 | AttributedTypes.InsertNode(type, insertPos); |
| 2236 | |
| 2237 | return QualType(type, 0); |
| 2238 | } |
| 2239 | |
| 2240 | |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2241 | /// \brief Retrieve a substitution-result type. |
| 2242 | QualType |
| 2243 | ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2244 | QualType Replacement) const { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2245 | assert(Replacement.isCanonical() |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2246 | && "replacement types must always be canonical"); |
| 2247 | |
| 2248 | llvm::FoldingSetNodeID ID; |
| 2249 | SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); |
| 2250 | void *InsertPos = 0; |
| 2251 | SubstTemplateTypeParmType *SubstParm |
| 2252 | = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2253 | |
| 2254 | if (!SubstParm) { |
| 2255 | SubstParm = new (*this, TypeAlignment) |
| 2256 | SubstTemplateTypeParmType(Parm, Replacement); |
| 2257 | Types.push_back(SubstParm); |
| 2258 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 2259 | } |
| 2260 | |
| 2261 | return QualType(SubstParm, 0); |
| 2262 | } |
| 2263 | |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 2264 | /// \brief Retrieve a |
| 2265 | QualType ASTContext::getSubstTemplateTypeParmPackType( |
| 2266 | const TemplateTypeParmType *Parm, |
| 2267 | const TemplateArgument &ArgPack) { |
| 2268 | #ifndef NDEBUG |
| 2269 | for (TemplateArgument::pack_iterator P = ArgPack.pack_begin(), |
| 2270 | PEnd = ArgPack.pack_end(); |
| 2271 | P != PEnd; ++P) { |
| 2272 | assert(P->getKind() == TemplateArgument::Type &&"Pack contains a non-type"); |
| 2273 | assert(P->getAsType().isCanonical() && "Pack contains non-canonical type"); |
| 2274 | } |
| 2275 | #endif |
| 2276 | |
| 2277 | llvm::FoldingSetNodeID ID; |
| 2278 | SubstTemplateTypeParmPackType::Profile(ID, Parm, ArgPack); |
| 2279 | void *InsertPos = 0; |
| 2280 | if (SubstTemplateTypeParmPackType *SubstParm |
| 2281 | = SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2282 | return QualType(SubstParm, 0); |
| 2283 | |
| 2284 | QualType Canon; |
| 2285 | if (!Parm->isCanonicalUnqualified()) { |
| 2286 | Canon = getCanonicalType(QualType(Parm, 0)); |
| 2287 | Canon = getSubstTemplateTypeParmPackType(cast<TemplateTypeParmType>(Canon), |
| 2288 | ArgPack); |
| 2289 | SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2290 | } |
| 2291 | |
| 2292 | SubstTemplateTypeParmPackType *SubstParm |
| 2293 | = new (*this, TypeAlignment) SubstTemplateTypeParmPackType(Parm, Canon, |
| 2294 | ArgPack); |
| 2295 | Types.push_back(SubstParm); |
| 2296 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 2297 | return QualType(SubstParm, 0); |
| 2298 | } |
| 2299 | |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2300 | /// \brief Retrieve the template type parameter type for a template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2301 | /// parameter or parameter pack with the given depth, index, and (optionally) |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2302 | /// name. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2303 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2304 | bool ParameterPack, |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2305 | TemplateTypeParmDecl *TTPDecl) const { |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2306 | llvm::FoldingSetNodeID ID; |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2307 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, TTPDecl); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2308 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2309 | TemplateTypeParmType *TypeParm |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2310 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2311 | |
| 2312 | if (TypeParm) |
| 2313 | return QualType(TypeParm, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2314 | |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2315 | if (TTPDecl) { |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2316 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2317 | TypeParm = new (*this, TypeAlignment) TemplateTypeParmType(TTPDecl, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2318 | |
| 2319 | TemplateTypeParmType *TypeCheck |
| 2320 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2321 | assert(!TypeCheck && "Template type parameter canonical type broken"); |
| 2322 | (void)TypeCheck; |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2323 | } else |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2324 | TypeParm = new (*this, TypeAlignment) |
| 2325 | TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2326 | |
| 2327 | Types.push_back(TypeParm); |
| 2328 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 2329 | |
| 2330 | return QualType(TypeParm, 0); |
| 2331 | } |
| 2332 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2333 | TypeSourceInfo * |
| 2334 | ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name, |
| 2335 | SourceLocation NameLoc, |
| 2336 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2337 | QualType Underlying) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2338 | assert(!Name.getAsDependentTemplateName() && |
| 2339 | "No dependent template names here!"); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2340 | QualType TST = getTemplateSpecializationType(Name, Args, Underlying); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2341 | |
| 2342 | TypeSourceInfo *DI = CreateTypeSourceInfo(TST); |
| 2343 | TemplateSpecializationTypeLoc TL |
| 2344 | = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc()); |
| 2345 | TL.setTemplateNameLoc(NameLoc); |
| 2346 | TL.setLAngleLoc(Args.getLAngleLoc()); |
| 2347 | TL.setRAngleLoc(Args.getRAngleLoc()); |
| 2348 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 2349 | TL.setArgLocInfo(i, Args[i].getLocInfo()); |
| 2350 | return DI; |
| 2351 | } |
| 2352 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2353 | QualType |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2354 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2355 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2356 | QualType Underlying) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2357 | assert(!Template.getAsDependentTemplateName() && |
| 2358 | "No dependent template names here!"); |
| 2359 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2360 | unsigned NumArgs = Args.size(); |
| 2361 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2362 | SmallVector<TemplateArgument, 4> ArgVec; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2363 | ArgVec.reserve(NumArgs); |
| 2364 | for (unsigned i = 0; i != NumArgs; ++i) |
| 2365 | ArgVec.push_back(Args[i].getArgument()); |
| 2366 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2367 | return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2368 | Underlying); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2369 | } |
| 2370 | |
| 2371 | QualType |
| 2372 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2373 | const TemplateArgument *Args, |
| 2374 | unsigned NumArgs, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2375 | QualType Underlying) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2376 | assert(!Template.getAsDependentTemplateName() && |
| 2377 | "No dependent template names here!"); |
Douglas Gregor | 0f0ea2a | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 2378 | // Look through qualified template names. |
| 2379 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 2380 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2381 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2382 | bool isTypeAlias = |
| 2383 | Template.getAsTemplateDecl() && |
| 2384 | isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl()); |
| 2385 | |
| 2386 | QualType CanonType; |
| 2387 | if (!Underlying.isNull()) |
| 2388 | CanonType = getCanonicalType(Underlying); |
| 2389 | else { |
| 2390 | assert(!isTypeAlias && |
| 2391 | "Underlying type for template alias must be computed by caller"); |
| 2392 | CanonType = getCanonicalTemplateSpecializationType(Template, Args, |
| 2393 | NumArgs); |
| 2394 | } |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 2395 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2396 | // Allocate the (non-canonical) template specialization type, but don't |
| 2397 | // try to unique it: these types typically have location information that |
| 2398 | // we don't unique and don't want to lose. |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2399 | void *Mem = Allocate(sizeof(TemplateSpecializationType) + |
| 2400 | sizeof(TemplateArgument) * NumArgs + |
| 2401 | (isTypeAlias ? sizeof(QualType) : 0), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2402 | TypeAlignment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2403 | TemplateSpecializationType *Spec |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 2404 | = new (Mem) TemplateSpecializationType(Template, |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2405 | Args, NumArgs, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2406 | CanonType, |
| 2407 | isTypeAlias ? Underlying : QualType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2408 | |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2409 | Types.push_back(Spec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2410 | return QualType(Spec, 0); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2411 | } |
| 2412 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2413 | QualType |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2414 | ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template, |
| 2415 | const TemplateArgument *Args, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2416 | unsigned NumArgs) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2417 | assert(!Template.getAsDependentTemplateName() && |
| 2418 | "No dependent template names here!"); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2419 | assert((!Template.getAsTemplateDecl() || |
| 2420 | !isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) && |
| 2421 | "Underlying type for template alias must be computed by caller"); |
| 2422 | |
Douglas Gregor | 0f0ea2a | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 2423 | // Look through qualified template names. |
| 2424 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 2425 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2426 | |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2427 | // Build the canonical template specialization type. |
| 2428 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2429 | SmallVector<TemplateArgument, 4> CanonArgs; |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2430 | CanonArgs.reserve(NumArgs); |
| 2431 | for (unsigned I = 0; I != NumArgs; ++I) |
| 2432 | CanonArgs.push_back(getCanonicalTemplateArgument(Args[I])); |
| 2433 | |
| 2434 | // Determine whether this canonical template specialization type already |
| 2435 | // exists. |
| 2436 | llvm::FoldingSetNodeID ID; |
| 2437 | TemplateSpecializationType::Profile(ID, CanonTemplate, |
| 2438 | CanonArgs.data(), NumArgs, *this); |
| 2439 | |
| 2440 | void *InsertPos = 0; |
| 2441 | TemplateSpecializationType *Spec |
| 2442 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2443 | |
| 2444 | if (!Spec) { |
| 2445 | // Allocate a new canonical template specialization type. |
| 2446 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
| 2447 | sizeof(TemplateArgument) * NumArgs), |
| 2448 | TypeAlignment); |
| 2449 | Spec = new (Mem) TemplateSpecializationType(CanonTemplate, |
| 2450 | CanonArgs.data(), NumArgs, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2451 | QualType(), QualType()); |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2452 | Types.push_back(Spec); |
| 2453 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
| 2454 | } |
| 2455 | |
| 2456 | assert(Spec->isDependentType() && |
| 2457 | "Non-dependent template-id type must have a canonical type"); |
| 2458 | return QualType(Spec, 0); |
| 2459 | } |
| 2460 | |
| 2461 | QualType |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2462 | ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword, |
| 2463 | NestedNameSpecifier *NNS, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2464 | QualType NamedType) const { |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2465 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2466 | ElaboratedType::Profile(ID, Keyword, NNS, NamedType); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2467 | |
| 2468 | void *InsertPos = 0; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2469 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2470 | if (T) |
| 2471 | return QualType(T, 0); |
| 2472 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2473 | QualType Canon = NamedType; |
| 2474 | if (!Canon.isCanonical()) { |
| 2475 | Canon = getCanonicalType(NamedType); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2476 | ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2477 | assert(!CheckT && "Elaborated canonical type broken"); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2478 | (void)CheckT; |
| 2479 | } |
| 2480 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2481 | T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2482 | Types.push_back(T); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2483 | ElaboratedTypes.InsertNode(T, InsertPos); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2484 | return QualType(T, 0); |
| 2485 | } |
| 2486 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 2487 | QualType |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2488 | ASTContext::getParenType(QualType InnerType) const { |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 2489 | llvm::FoldingSetNodeID ID; |
| 2490 | ParenType::Profile(ID, InnerType); |
| 2491 | |
| 2492 | void *InsertPos = 0; |
| 2493 | ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2494 | if (T) |
| 2495 | return QualType(T, 0); |
| 2496 | |
| 2497 | QualType Canon = InnerType; |
| 2498 | if (!Canon.isCanonical()) { |
| 2499 | Canon = getCanonicalType(InnerType); |
| 2500 | ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2501 | assert(!CheckT && "Paren canonical type broken"); |
| 2502 | (void)CheckT; |
| 2503 | } |
| 2504 | |
| 2505 | T = new (*this) ParenType(InnerType, Canon); |
| 2506 | Types.push_back(T); |
| 2507 | ParenTypes.InsertNode(T, InsertPos); |
| 2508 | return QualType(T, 0); |
| 2509 | } |
| 2510 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2511 | QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, |
| 2512 | NestedNameSpecifier *NNS, |
| 2513 | const IdentifierInfo *Name, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2514 | QualType Canon) const { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2515 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 2516 | |
| 2517 | if (Canon.isNull()) { |
| 2518 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2519 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 2520 | if (Keyword == ETK_None) |
| 2521 | CanonKeyword = ETK_Typename; |
| 2522 | |
| 2523 | if (CanonNNS != NNS || CanonKeyword != Keyword) |
| 2524 | Canon = getDependentNameType(CanonKeyword, CanonNNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2525 | } |
| 2526 | |
| 2527 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2528 | DependentNameType::Profile(ID, Keyword, NNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2529 | |
| 2530 | void *InsertPos = 0; |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2531 | DependentNameType *T |
| 2532 | = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2533 | if (T) |
| 2534 | return QualType(T, 0); |
| 2535 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2536 | T = new (*this) DependentNameType(Keyword, NNS, Name, Canon); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2537 | Types.push_back(T); |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2538 | DependentNameTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2539 | return QualType(T, 0); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2540 | } |
| 2541 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2542 | QualType |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2543 | ASTContext::getDependentTemplateSpecializationType( |
| 2544 | ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2545 | NestedNameSpecifier *NNS, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2546 | const IdentifierInfo *Name, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2547 | const TemplateArgumentListInfo &Args) const { |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2548 | // TODO: avoid this copy |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2549 | SmallVector<TemplateArgument, 16> ArgCopy; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2550 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
| 2551 | ArgCopy.push_back(Args[I].getArgument()); |
| 2552 | return getDependentTemplateSpecializationType(Keyword, NNS, Name, |
| 2553 | ArgCopy.size(), |
| 2554 | ArgCopy.data()); |
| 2555 | } |
| 2556 | |
| 2557 | QualType |
| 2558 | ASTContext::getDependentTemplateSpecializationType( |
| 2559 | ElaboratedTypeKeyword Keyword, |
| 2560 | NestedNameSpecifier *NNS, |
| 2561 | const IdentifierInfo *Name, |
| 2562 | unsigned NumArgs, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2563 | const TemplateArgument *Args) const { |
Douglas Gregor | aa2187d | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 2564 | assert((!NNS || NNS->isDependent()) && |
| 2565 | "nested-name-specifier must be dependent"); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2566 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2567 | llvm::FoldingSetNodeID ID; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2568 | DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS, |
| 2569 | Name, NumArgs, Args); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2570 | |
| 2571 | void *InsertPos = 0; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2572 | DependentTemplateSpecializationType *T |
| 2573 | = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2574 | if (T) |
| 2575 | return QualType(T, 0); |
| 2576 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2577 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2578 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2579 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 2580 | if (Keyword == ETK_None) CanonKeyword = ETK_Typename; |
| 2581 | |
| 2582 | bool AnyNonCanonArgs = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2583 | SmallVector<TemplateArgument, 16> CanonArgs(NumArgs); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2584 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 2585 | CanonArgs[I] = getCanonicalTemplateArgument(Args[I]); |
| 2586 | if (!CanonArgs[I].structurallyEquals(Args[I])) |
| 2587 | AnyNonCanonArgs = true; |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2588 | } |
| 2589 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2590 | QualType Canon; |
| 2591 | if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) { |
| 2592 | Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS, |
| 2593 | Name, NumArgs, |
| 2594 | CanonArgs.data()); |
| 2595 | |
| 2596 | // Find the insert position again. |
| 2597 | DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2598 | } |
| 2599 | |
| 2600 | void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) + |
| 2601 | sizeof(TemplateArgument) * NumArgs), |
| 2602 | TypeAlignment); |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 2603 | T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2604 | Name, NumArgs, Args, Canon); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2605 | Types.push_back(T); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2606 | DependentTemplateSpecializationTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2607 | return QualType(T, 0); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2608 | } |
| 2609 | |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2610 | QualType ASTContext::getPackExpansionType(QualType Pattern, |
| 2611 | llvm::Optional<unsigned> NumExpansions) { |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2612 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2613 | PackExpansionType::Profile(ID, Pattern, NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2614 | |
| 2615 | assert(Pattern->containsUnexpandedParameterPack() && |
| 2616 | "Pack expansions must expand one or more parameter packs"); |
| 2617 | void *InsertPos = 0; |
| 2618 | PackExpansionType *T |
| 2619 | = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2620 | if (T) |
| 2621 | return QualType(T, 0); |
| 2622 | |
| 2623 | QualType Canon; |
| 2624 | if (!Pattern.isCanonical()) { |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2625 | Canon = getPackExpansionType(getCanonicalType(Pattern), NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2626 | |
| 2627 | // Find the insert position again. |
| 2628 | PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2629 | } |
| 2630 | |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2631 | T = new (*this) PackExpansionType(Pattern, Canon, NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2632 | Types.push_back(T); |
| 2633 | PackExpansionTypes.InsertNode(T, InsertPos); |
| 2634 | return QualType(T, 0); |
| 2635 | } |
| 2636 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2637 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 2638 | /// alphabetically. |
| 2639 | static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, |
| 2640 | const ObjCProtocolDecl *RHS) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2641 | return LHS->getDeclName() < RHS->getDeclName(); |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2642 | } |
| 2643 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2644 | static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols, |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2645 | unsigned NumProtocols) { |
| 2646 | if (NumProtocols == 0) return true; |
| 2647 | |
| 2648 | for (unsigned i = 1; i != NumProtocols; ++i) |
| 2649 | if (!CmpProtocolNames(Protocols[i-1], Protocols[i])) |
| 2650 | return false; |
| 2651 | return true; |
| 2652 | } |
| 2653 | |
| 2654 | static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols, |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2655 | unsigned &NumProtocols) { |
| 2656 | ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2657 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2658 | // Sort protocols, keyed by name. |
| 2659 | std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); |
| 2660 | |
| 2661 | // Remove duplicates. |
| 2662 | ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); |
| 2663 | NumProtocols = ProtocolsEnd-Protocols; |
| 2664 | } |
| 2665 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2666 | QualType ASTContext::getObjCObjectType(QualType BaseType, |
| 2667 | ObjCProtocolDecl * const *Protocols, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2668 | unsigned NumProtocols) const { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2669 | // If the base type is an interface and there aren't any protocols |
| 2670 | // to add, then the interface type will do just fine. |
| 2671 | if (!NumProtocols && isa<ObjCInterfaceType>(BaseType)) |
| 2672 | return BaseType; |
| 2673 | |
| 2674 | // Look in the folding set for an existing type. |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2675 | llvm::FoldingSetNodeID ID; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2676 | ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2677 | void *InsertPos = 0; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2678 | if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2679 | return QualType(QT, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2680 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2681 | // Build the canonical type, which has the canonical base type and |
| 2682 | // a sorted-and-uniqued list of protocols. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2683 | QualType Canonical; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2684 | bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols); |
| 2685 | if (!ProtocolsSorted || !BaseType.isCanonical()) { |
| 2686 | if (!ProtocolsSorted) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2687 | SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols, |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 2688 | Protocols + NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2689 | unsigned UniqueCount = NumProtocols; |
| 2690 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2691 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2692 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 2693 | &Sorted[0], UniqueCount); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2694 | } else { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2695 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 2696 | Protocols, NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2697 | } |
| 2698 | |
| 2699 | // Regenerate InsertPos. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2700 | ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2701 | } |
| 2702 | |
| 2703 | unsigned Size = sizeof(ObjCObjectTypeImpl); |
| 2704 | Size += NumProtocols * sizeof(ObjCProtocolDecl *); |
| 2705 | void *Mem = Allocate(Size, TypeAlignment); |
| 2706 | ObjCObjectTypeImpl *T = |
| 2707 | new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols); |
| 2708 | |
| 2709 | Types.push_back(T); |
| 2710 | ObjCObjectTypes.InsertNode(T, InsertPos); |
| 2711 | return QualType(T, 0); |
| 2712 | } |
| 2713 | |
| 2714 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 2715 | /// the given object type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2716 | QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2717 | llvm::FoldingSetNodeID ID; |
| 2718 | ObjCObjectPointerType::Profile(ID, ObjectT); |
| 2719 | |
| 2720 | void *InsertPos = 0; |
| 2721 | if (ObjCObjectPointerType *QT = |
| 2722 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2723 | return QualType(QT, 0); |
| 2724 | |
| 2725 | // Find the canonical object type. |
| 2726 | QualType Canonical; |
| 2727 | if (!ObjectT.isCanonical()) { |
| 2728 | Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT)); |
| 2729 | |
| 2730 | // Regenerate InsertPos. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2731 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2732 | } |
| 2733 | |
Douglas Gregor | fd6a088 | 2010-02-08 22:59:26 +0000 | [diff] [blame] | 2734 | // No match. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2735 | void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment); |
| 2736 | ObjCObjectPointerType *QType = |
| 2737 | new (Mem) ObjCObjectPointerType(Canonical, ObjectT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2738 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2739 | Types.push_back(QType); |
| 2740 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2741 | return QualType(QType, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2742 | } |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2743 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2744 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 2745 | /// specified ObjC interface decl. The list of protocols is optional. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2746 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) const { |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2747 | if (Decl->TypeForDecl) |
| 2748 | return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2749 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2750 | // FIXME: redeclarations? |
| 2751 | void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment); |
| 2752 | ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl); |
| 2753 | Decl->TypeForDecl = T; |
| 2754 | Types.push_back(T); |
| 2755 | return QualType(T, 0); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2756 | } |
| 2757 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2758 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 2759 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2760 | /// multiple declarations that refer to "typeof(x)" all contain different |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2761 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2762 | /// on canonical type's (which are always unique). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2763 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) const { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2764 | TypeOfExprType *toe; |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2765 | if (tofExpr->isTypeDependent()) { |
| 2766 | llvm::FoldingSetNodeID ID; |
| 2767 | DependentTypeOfExprType::Profile(ID, *this, tofExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2768 | |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2769 | void *InsertPos = 0; |
| 2770 | DependentTypeOfExprType *Canon |
| 2771 | = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2772 | if (Canon) { |
| 2773 | // We already have a "canonical" version of an identical, dependent |
| 2774 | // typeof(expr) type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2775 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2776 | QualType((TypeOfExprType*)Canon, 0)); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 2777 | } else { |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2778 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2779 | Canon |
| 2780 | = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2781 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); |
| 2782 | toe = Canon; |
| 2783 | } |
| 2784 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2785 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2786 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2787 | } |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2788 | Types.push_back(toe); |
| 2789 | return QualType(toe, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2790 | } |
| 2791 | |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2792 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 2793 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 2794 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2795 | /// an issue. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2796 | /// on canonical type's (which are always unique). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2797 | QualType ASTContext::getTypeOfType(QualType tofType) const { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2798 | QualType Canonical = getCanonicalType(tofType); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2799 | TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2800 | Types.push_back(tot); |
| 2801 | return QualType(tot, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2802 | } |
| 2803 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2804 | /// getDecltypeForExpr - Given an expr, will return the decltype for that |
| 2805 | /// expression, according to the rules in C++0x [dcl.type.simple]p4 |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2806 | static QualType getDecltypeForExpr(const Expr *e, const ASTContext &Context) { |
Anders Carlsson | a07c33e | 2009-06-25 15:00:34 +0000 | [diff] [blame] | 2807 | if (e->isTypeDependent()) |
| 2808 | return Context.DependentTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2809 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2810 | // If e is an id expression or a class member access, decltype(e) is defined |
| 2811 | // as the type of the entity named by e. |
| 2812 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) { |
| 2813 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) |
| 2814 | return VD->getType(); |
| 2815 | } |
| 2816 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) { |
| 2817 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) |
| 2818 | return FD->getType(); |
| 2819 | } |
| 2820 | // If e is a function call or an invocation of an overloaded operator, |
| 2821 | // (parentheses around e are ignored), decltype(e) is defined as the |
| 2822 | // return type of that function. |
| 2823 | if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens())) |
| 2824 | return CE->getCallReturnType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2825 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2826 | QualType T = e->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2827 | |
| 2828 | // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2829 | // defined as T&, otherwise decltype(e) is defined as T. |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 2830 | if (e->isLValue()) |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2831 | T = Context.getLValueReferenceType(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2832 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2833 | return T; |
| 2834 | } |
| 2835 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2836 | /// getDecltypeType - Unlike many "get<Type>" functions, we don't unique |
| 2837 | /// DecltypeType AST's. The only motivation to unique these nodes would be |
| 2838 | /// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2839 | /// an issue. This doesn't effect the type checker, since it operates |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2840 | /// on canonical type's (which are always unique). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2841 | QualType ASTContext::getDecltypeType(Expr *e) const { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2842 | DecltypeType *dt; |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2843 | |
| 2844 | // C++0x [temp.type]p2: |
| 2845 | // If an expression e involves a template parameter, decltype(e) denotes a |
| 2846 | // unique dependent type. Two such decltype-specifiers refer to the same |
| 2847 | // type only if their expressions are equivalent (14.5.6.1). |
| 2848 | if (e->isInstantiationDependent()) { |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2849 | llvm::FoldingSetNodeID ID; |
| 2850 | DependentDecltypeType::Profile(ID, *this, e); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2851 | |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2852 | void *InsertPos = 0; |
| 2853 | DependentDecltypeType *Canon |
| 2854 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2855 | if (Canon) { |
| 2856 | // We already have a "canonical" version of an equivalent, dependent |
| 2857 | // decltype type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2858 | dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy, |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2859 | QualType((DecltypeType*)Canon, 0)); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 2860 | } else { |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2861 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2862 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2863 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); |
| 2864 | dt = Canon; |
| 2865 | } |
| 2866 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2867 | QualType T = getDecltypeForExpr(e, *this); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2868 | dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T)); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2869 | } |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2870 | Types.push_back(dt); |
| 2871 | return QualType(dt, 0); |
| 2872 | } |
| 2873 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 2874 | /// getUnaryTransformationType - We don't unique these, since the memory |
| 2875 | /// savings are minimal and these are rare. |
| 2876 | QualType ASTContext::getUnaryTransformType(QualType BaseType, |
| 2877 | QualType UnderlyingType, |
| 2878 | UnaryTransformType::UTTKind Kind) |
| 2879 | const { |
| 2880 | UnaryTransformType *Ty = |
Douglas Gregor | 69d9775 | 2011-05-25 17:51:54 +0000 | [diff] [blame] | 2881 | new (*this, TypeAlignment) UnaryTransformType (BaseType, UnderlyingType, |
| 2882 | Kind, |
| 2883 | UnderlyingType->isDependentType() ? |
| 2884 | QualType() : UnderlyingType); |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 2885 | Types.push_back(Ty); |
| 2886 | return QualType(Ty, 0); |
| 2887 | } |
| 2888 | |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 2889 | /// getAutoType - We only unique auto types after they've been deduced. |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2890 | QualType ASTContext::getAutoType(QualType DeducedType) const { |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 2891 | void *InsertPos = 0; |
| 2892 | if (!DeducedType.isNull()) { |
| 2893 | // Look in the folding set for an existing type. |
| 2894 | llvm::FoldingSetNodeID ID; |
| 2895 | AutoType::Profile(ID, DeducedType); |
| 2896 | if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2897 | return QualType(AT, 0); |
| 2898 | } |
| 2899 | |
| 2900 | AutoType *AT = new (*this, TypeAlignment) AutoType(DeducedType); |
| 2901 | Types.push_back(AT); |
| 2902 | if (InsertPos) |
| 2903 | AutoTypes.InsertNode(AT, InsertPos); |
| 2904 | return QualType(AT, 0); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2905 | } |
| 2906 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2907 | /// getAutoDeductType - Get type pattern for deducing against 'auto'. |
| 2908 | QualType ASTContext::getAutoDeductType() const { |
| 2909 | if (AutoDeductTy.isNull()) |
| 2910 | AutoDeductTy = getAutoType(QualType()); |
| 2911 | assert(!AutoDeductTy.isNull() && "can't build 'auto' pattern"); |
| 2912 | return AutoDeductTy; |
| 2913 | } |
| 2914 | |
| 2915 | /// getAutoRRefDeductType - Get type pattern for deducing against 'auto &&'. |
| 2916 | QualType ASTContext::getAutoRRefDeductType() const { |
| 2917 | if (AutoRRefDeductTy.isNull()) |
| 2918 | AutoRRefDeductTy = getRValueReferenceType(getAutoDeductType()); |
| 2919 | assert(!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern"); |
| 2920 | return AutoRRefDeductTy; |
| 2921 | } |
| 2922 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2923 | /// getTagDeclType - Return the unique reference to the type for the |
| 2924 | /// specified TagDecl (struct/union/class/enum) decl. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2925 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) const { |
Ted Kremenek | d778f88 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 2926 | assert (Decl); |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 2927 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 2928 | // away const? mutable? |
| 2929 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2930 | } |
| 2931 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2932 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 2933 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 2934 | /// needs to agree with the definition in <stddef.h>. |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 2935 | CanQualType ASTContext::getSizeType() const { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2936 | return getFromTargetType(Target->getSizeType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2937 | } |
| 2938 | |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 2939 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 2940 | /// Used when in C++, as a GCC extension. |
| 2941 | QualType ASTContext::getSignedWCharType() const { |
| 2942 | // FIXME: derive from "Target" ? |
| 2943 | return WCharTy; |
| 2944 | } |
| 2945 | |
| 2946 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 2947 | /// Used when in C++, as a GCC extension. |
| 2948 | QualType ASTContext::getUnsignedWCharType() const { |
| 2949 | // FIXME: derive from "Target" ? |
| 2950 | return UnsignedIntTy; |
| 2951 | } |
| 2952 | |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 2953 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) |
| 2954 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 2955 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2956 | return getFromTargetType(Target->getPtrDiffType(0)); |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 2957 | } |
| 2958 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2959 | //===----------------------------------------------------------------------===// |
| 2960 | // Type Operators |
| 2961 | //===----------------------------------------------------------------------===// |
| 2962 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2963 | CanQualType ASTContext::getCanonicalParamType(QualType T) const { |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2964 | // Push qualifiers into arrays, and then discard any remaining |
| 2965 | // qualifiers. |
| 2966 | T = getCanonicalType(T); |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2967 | T = getVariableArrayDecayedType(T); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2968 | const Type *Ty = T.getTypePtr(); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2969 | QualType Result; |
| 2970 | if (isa<ArrayType>(Ty)) { |
| 2971 | Result = getArrayDecayedType(QualType(Ty,0)); |
| 2972 | } else if (isa<FunctionType>(Ty)) { |
| 2973 | Result = getPointerType(QualType(Ty, 0)); |
| 2974 | } else { |
| 2975 | Result = QualType(Ty, 0); |
| 2976 | } |
| 2977 | |
| 2978 | return CanQualType::CreateUnsafe(Result); |
| 2979 | } |
| 2980 | |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 2981 | QualType ASTContext::getUnqualifiedArrayType(QualType type, |
| 2982 | Qualifiers &quals) { |
| 2983 | SplitQualType splitType = type.getSplitUnqualifiedType(); |
| 2984 | |
| 2985 | // FIXME: getSplitUnqualifiedType() actually walks all the way to |
| 2986 | // the unqualified desugared type and then drops it on the floor. |
| 2987 | // We then have to strip that sugar back off with |
| 2988 | // getUnqualifiedDesugaredType(), which is silly. |
| 2989 | const ArrayType *AT = |
| 2990 | dyn_cast<ArrayType>(splitType.first->getUnqualifiedDesugaredType()); |
| 2991 | |
| 2992 | // If we don't have an array, just use the results in splitType. |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2993 | if (!AT) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 2994 | quals = splitType.second; |
| 2995 | return QualType(splitType.first, 0); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2996 | } |
| 2997 | |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 2998 | // Otherwise, recurse on the array's element type. |
| 2999 | QualType elementType = AT->getElementType(); |
| 3000 | QualType unqualElementType = getUnqualifiedArrayType(elementType, quals); |
| 3001 | |
| 3002 | // If that didn't change the element type, AT has no qualifiers, so we |
| 3003 | // can just use the results in splitType. |
| 3004 | if (elementType == unqualElementType) { |
| 3005 | assert(quals.empty()); // from the recursive call |
| 3006 | quals = splitType.second; |
| 3007 | return QualType(splitType.first, 0); |
| 3008 | } |
| 3009 | |
| 3010 | // Otherwise, add in the qualifiers from the outermost type, then |
| 3011 | // build the type back up. |
| 3012 | quals.addConsistentQualifiers(splitType.second); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3013 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3014 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3015 | return getConstantArrayType(unqualElementType, CAT->getSize(), |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3016 | CAT->getSizeModifier(), 0); |
| 3017 | } |
| 3018 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3019 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3020 | return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3021 | } |
| 3022 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3023 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3024 | return getVariableArrayType(unqualElementType, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3025 | VAT->getSizeExpr(), |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3026 | VAT->getSizeModifier(), |
| 3027 | VAT->getIndexTypeCVRQualifiers(), |
| 3028 | VAT->getBracketsRange()); |
| 3029 | } |
| 3030 | |
| 3031 | const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT); |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3032 | return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(), |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3033 | DSAT->getSizeModifier(), 0, |
| 3034 | SourceRange()); |
| 3035 | } |
| 3036 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3037 | /// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that |
| 3038 | /// may be similar (C++ 4.4), replaces T1 and T2 with the type that |
| 3039 | /// they point to and return true. If T1 and T2 aren't pointer types |
| 3040 | /// or pointer-to-member types, or if they are not similar at this |
| 3041 | /// level, returns false and leaves T1 and T2 unchanged. Top-level |
| 3042 | /// qualifiers on T1 and T2 are ignored. This function will typically |
| 3043 | /// be called in a loop that successively "unwraps" pointer and |
| 3044 | /// pointer-to-member types to compare them at each level. |
| 3045 | bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) { |
| 3046 | const PointerType *T1PtrType = T1->getAs<PointerType>(), |
| 3047 | *T2PtrType = T2->getAs<PointerType>(); |
| 3048 | if (T1PtrType && T2PtrType) { |
| 3049 | T1 = T1PtrType->getPointeeType(); |
| 3050 | T2 = T2PtrType->getPointeeType(); |
| 3051 | return true; |
| 3052 | } |
| 3053 | |
| 3054 | const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(), |
| 3055 | *T2MPType = T2->getAs<MemberPointerType>(); |
| 3056 | if (T1MPType && T2MPType && |
| 3057 | hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0), |
| 3058 | QualType(T2MPType->getClass(), 0))) { |
| 3059 | T1 = T1MPType->getPointeeType(); |
| 3060 | T2 = T2MPType->getPointeeType(); |
| 3061 | return true; |
| 3062 | } |
| 3063 | |
| 3064 | if (getLangOptions().ObjC1) { |
| 3065 | const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(), |
| 3066 | *T2OPType = T2->getAs<ObjCObjectPointerType>(); |
| 3067 | if (T1OPType && T2OPType) { |
| 3068 | T1 = T1OPType->getPointeeType(); |
| 3069 | T2 = T2OPType->getPointeeType(); |
| 3070 | return true; |
| 3071 | } |
| 3072 | } |
| 3073 | |
| 3074 | // FIXME: Block pointers, too? |
| 3075 | |
| 3076 | return false; |
| 3077 | } |
| 3078 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3079 | DeclarationNameInfo |
| 3080 | ASTContext::getNameForTemplate(TemplateName Name, |
| 3081 | SourceLocation NameLoc) const { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3082 | switch (Name.getKind()) { |
| 3083 | case TemplateName::QualifiedTemplate: |
| 3084 | case TemplateName::Template: |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3085 | // DNInfo work in progress: CHECKME: what about DNLoc? |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3086 | return DeclarationNameInfo(Name.getAsTemplateDecl()->getDeclName(), |
| 3087 | NameLoc); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3088 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3089 | case TemplateName::OverloadedTemplate: { |
| 3090 | OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate(); |
| 3091 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 3092 | return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc); |
| 3093 | } |
| 3094 | |
| 3095 | case TemplateName::DependentTemplate: { |
| 3096 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3097 | DeclarationName DName; |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3098 | if (DTN->isIdentifier()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3099 | DName = DeclarationNames.getIdentifier(DTN->getIdentifier()); |
| 3100 | return DeclarationNameInfo(DName, NameLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3101 | } else { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3102 | DName = DeclarationNames.getCXXOperatorName(DTN->getOperator()); |
| 3103 | // DNInfo work in progress: FIXME: source locations? |
| 3104 | DeclarationNameLoc DNLoc; |
| 3105 | DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding(); |
| 3106 | DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding(); |
| 3107 | return DeclarationNameInfo(DName, NameLoc, DNLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3108 | } |
| 3109 | } |
| 3110 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3111 | case TemplateName::SubstTemplateTemplateParm: { |
| 3112 | SubstTemplateTemplateParmStorage *subst |
| 3113 | = Name.getAsSubstTemplateTemplateParm(); |
| 3114 | return DeclarationNameInfo(subst->getParameter()->getDeclName(), |
| 3115 | NameLoc); |
| 3116 | } |
| 3117 | |
| 3118 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 3119 | SubstTemplateTemplateParmPackStorage *subst |
| 3120 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 3121 | return DeclarationNameInfo(subst->getParameterPack()->getDeclName(), |
| 3122 | NameLoc); |
| 3123 | } |
| 3124 | } |
| 3125 | |
| 3126 | llvm_unreachable("bad template name kind!"); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3127 | } |
| 3128 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3129 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3130 | switch (Name.getKind()) { |
| 3131 | case TemplateName::QualifiedTemplate: |
| 3132 | case TemplateName::Template: { |
| 3133 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3134 | if (TemplateTemplateParmDecl *TTP |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3135 | = dyn_cast<TemplateTemplateParmDecl>(Template)) |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3136 | Template = getCanonicalTemplateTemplateParmDecl(TTP); |
| 3137 | |
| 3138 | // The canonical template name is the canonical template declaration. |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 3139 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3140 | } |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 3141 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3142 | case TemplateName::OverloadedTemplate: |
| 3143 | llvm_unreachable("cannot canonicalize overloaded template"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3144 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3145 | case TemplateName::DependentTemplate: { |
| 3146 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 3147 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 3148 | return DTN->CanonicalTemplateName; |
| 3149 | } |
| 3150 | |
| 3151 | case TemplateName::SubstTemplateTemplateParm: { |
| 3152 | SubstTemplateTemplateParmStorage *subst |
| 3153 | = Name.getAsSubstTemplateTemplateParm(); |
| 3154 | return getCanonicalTemplateName(subst->getReplacement()); |
| 3155 | } |
| 3156 | |
| 3157 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 3158 | SubstTemplateTemplateParmPackStorage *subst |
| 3159 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 3160 | TemplateTemplateParmDecl *canonParameter |
| 3161 | = getCanonicalTemplateTemplateParmDecl(subst->getParameterPack()); |
| 3162 | TemplateArgument canonArgPack |
| 3163 | = getCanonicalTemplateArgument(subst->getArgumentPack()); |
| 3164 | return getSubstTemplateTemplateParmPack(canonParameter, canonArgPack); |
| 3165 | } |
| 3166 | } |
| 3167 | |
| 3168 | llvm_unreachable("bad template name!"); |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 3169 | } |
| 3170 | |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 3171 | bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) { |
| 3172 | X = getCanonicalTemplateName(X); |
| 3173 | Y = getCanonicalTemplateName(Y); |
| 3174 | return X.getAsVoidPointer() == Y.getAsVoidPointer(); |
| 3175 | } |
| 3176 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3177 | TemplateArgument |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3178 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const { |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3179 | switch (Arg.getKind()) { |
| 3180 | case TemplateArgument::Null: |
| 3181 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3182 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3183 | case TemplateArgument::Expression: |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3184 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3185 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3186 | case TemplateArgument::Declaration: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3187 | return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3188 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3189 | case TemplateArgument::Template: |
| 3190 | return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate())); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3191 | |
| 3192 | case TemplateArgument::TemplateExpansion: |
| 3193 | return TemplateArgument(getCanonicalTemplateName( |
| 3194 | Arg.getAsTemplateOrTemplatePattern()), |
Douglas Gregor | 2be29f4 | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 3195 | Arg.getNumTemplateExpansions()); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3196 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3197 | case TemplateArgument::Integral: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3198 | return TemplateArgument(*Arg.getAsIntegral(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3199 | getCanonicalType(Arg.getIntegralType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3200 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3201 | case TemplateArgument::Type: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3202 | return TemplateArgument(getCanonicalType(Arg.getAsType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3203 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3204 | case TemplateArgument::Pack: { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 3205 | if (Arg.pack_size() == 0) |
| 3206 | return Arg; |
| 3207 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3208 | TemplateArgument *CanonArgs |
| 3209 | = new (*this) TemplateArgument[Arg.pack_size()]; |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3210 | unsigned Idx = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3211 | for (TemplateArgument::pack_iterator A = Arg.pack_begin(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3212 | AEnd = Arg.pack_end(); |
| 3213 | A != AEnd; (void)++A, ++Idx) |
| 3214 | CanonArgs[Idx] = getCanonicalTemplateArgument(*A); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3215 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3216 | return TemplateArgument(CanonArgs, Arg.pack_size()); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3217 | } |
| 3218 | } |
| 3219 | |
| 3220 | // Silence GCC warning |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3221 | llvm_unreachable("Unhandled template argument kind"); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3222 | } |
| 3223 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3224 | NestedNameSpecifier * |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3225 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3226 | if (!NNS) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3227 | return 0; |
| 3228 | |
| 3229 | switch (NNS->getKind()) { |
| 3230 | case NestedNameSpecifier::Identifier: |
| 3231 | // Canonicalize the prefix but keep the identifier the same. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3232 | return NestedNameSpecifier::Create(*this, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3233 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 3234 | NNS->getAsIdentifier()); |
| 3235 | |
| 3236 | case NestedNameSpecifier::Namespace: |
| 3237 | // A namespace is canonical; build a nested-name-specifier with |
| 3238 | // this namespace and no prefix. |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 3239 | return NestedNameSpecifier::Create(*this, 0, |
| 3240 | NNS->getAsNamespace()->getOriginalNamespace()); |
| 3241 | |
| 3242 | case NestedNameSpecifier::NamespaceAlias: |
| 3243 | // A namespace is canonical; build a nested-name-specifier with |
| 3244 | // this namespace and no prefix. |
| 3245 | return NestedNameSpecifier::Create(*this, 0, |
| 3246 | NNS->getAsNamespaceAlias()->getNamespace() |
| 3247 | ->getOriginalNamespace()); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3248 | |
| 3249 | case NestedNameSpecifier::TypeSpec: |
| 3250 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 3251 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3252 | |
| 3253 | // If we have some kind of dependent-named type (e.g., "typename T::type"), |
| 3254 | // break it apart into its prefix and identifier, then reconsititute those |
| 3255 | // as the canonical nested-name-specifier. This is required to canonicalize |
| 3256 | // a dependent nested-name-specifier involving typedefs of dependent-name |
| 3257 | // types, e.g., |
| 3258 | // typedef typename T::type T1; |
| 3259 | // typedef typename T1::type T2; |
| 3260 | if (const DependentNameType *DNT = T->getAs<DependentNameType>()) { |
| 3261 | NestedNameSpecifier *Prefix |
| 3262 | = getCanonicalNestedNameSpecifier(DNT->getQualifier()); |
| 3263 | return NestedNameSpecifier::Create(*this, Prefix, |
| 3264 | const_cast<IdentifierInfo *>(DNT->getIdentifier())); |
| 3265 | } |
| 3266 | |
Douglas Gregor | 643f843 | 2010-11-04 00:14:23 +0000 | [diff] [blame] | 3267 | // Do the same thing as above, but with dependent-named specializations. |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3268 | if (const DependentTemplateSpecializationType *DTST |
| 3269 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 3270 | NestedNameSpecifier *Prefix |
| 3271 | = getCanonicalNestedNameSpecifier(DTST->getQualifier()); |
Douglas Gregor | aa2187d | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 3272 | |
| 3273 | T = getDependentTemplateSpecializationType(DTST->getKeyword(), |
| 3274 | Prefix, DTST->getIdentifier(), |
| 3275 | DTST->getNumArgs(), |
| 3276 | DTST->getArgs()); |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3277 | T = getCanonicalType(T); |
| 3278 | } |
| 3279 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3280 | return NestedNameSpecifier::Create(*this, 0, false, |
| 3281 | const_cast<Type*>(T.getTypePtr())); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3282 | } |
| 3283 | |
| 3284 | case NestedNameSpecifier::Global: |
| 3285 | // The global specifier is canonical and unique. |
| 3286 | return NNS; |
| 3287 | } |
| 3288 | |
| 3289 | // Required to silence a GCC warning |
| 3290 | return 0; |
| 3291 | } |
| 3292 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3293 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3294 | const ArrayType *ASTContext::getAsArrayType(QualType T) const { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3295 | // Handle the non-qualified case efficiently. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3296 | if (!T.hasLocalQualifiers()) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3297 | // Handle the common positive case fast. |
| 3298 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 3299 | return AT; |
| 3300 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3301 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3302 | // Handle the common negative case fast. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3303 | if (!isa<ArrayType>(T.getCanonicalType())) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3304 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3305 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3306 | // Apply any qualifiers from the array type to the element type. This |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3307 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 3308 | // any type qualifiers, the element type is so qualified, not the array type." |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3309 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3310 | // If we get here, we either have type qualifiers on the type, or we have |
| 3311 | // sugar such as a typedef in the way. If we have type qualifiers on the type |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 3312 | // we must propagate them down into the element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3313 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3314 | SplitQualType split = T.getSplitDesugaredType(); |
| 3315 | Qualifiers qs = split.second; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3316 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3317 | // If we have a simple case, just return now. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3318 | const ArrayType *ATy = dyn_cast<ArrayType>(split.first); |
| 3319 | if (ATy == 0 || qs.empty()) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3320 | return ATy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3321 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3322 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 3323 | // qualifiers into the array element type and return a new array type. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3324 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3325 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3326 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 3327 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 3328 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3329 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3330 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 3331 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 3332 | IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3333 | IAT->getIndexTypeCVRQualifiers())); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3334 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3335 | if (const DependentSizedArrayType *DSAT |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3336 | = dyn_cast<DependentSizedArrayType>(ATy)) |
| 3337 | return cast<ArrayType>( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3338 | getDependentSizedArrayType(NewEltTy, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3339 | DSAT->getSizeExpr(), |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3340 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3341 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3342 | DSAT->getBracketsRange())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3343 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3344 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3345 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3346 | VAT->getSizeExpr(), |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3347 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3348 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3349 | VAT->getBracketsRange())); |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 3350 | } |
| 3351 | |
Douglas Gregor | 79e6bd3 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 3352 | QualType ASTContext::getAdjustedParameterType(QualType T) { |
| 3353 | // C99 6.7.5.3p7: |
| 3354 | // A declaration of a parameter as "array of type" shall be |
| 3355 | // adjusted to "qualified pointer to type", where the type |
| 3356 | // qualifiers (if any) are those specified within the [ and ] of |
| 3357 | // the array type derivation. |
| 3358 | if (T->isArrayType()) |
| 3359 | return getArrayDecayedType(T); |
| 3360 | |
| 3361 | // C99 6.7.5.3p8: |
| 3362 | // A declaration of a parameter as "function returning type" |
| 3363 | // shall be adjusted to "pointer to function returning type", as |
| 3364 | // in 6.3.2.1. |
| 3365 | if (T->isFunctionType()) |
| 3366 | return getPointerType(T); |
| 3367 | |
| 3368 | return T; |
| 3369 | } |
| 3370 | |
| 3371 | QualType ASTContext::getSignatureParameterType(QualType T) { |
| 3372 | T = getVariableArrayDecayedType(T); |
| 3373 | T = getAdjustedParameterType(T); |
| 3374 | return T.getUnqualifiedType(); |
| 3375 | } |
| 3376 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3377 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 3378 | /// specified array type to a pointer. This operation is non-trivial when |
| 3379 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 3380 | /// this returns a pointer to a properly qualified element of the array. |
| 3381 | /// |
| 3382 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3383 | QualType ASTContext::getArrayDecayedType(QualType Ty) const { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3384 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 3385 | // typedefs in the element type of the array. This also handles propagation |
| 3386 | // of type qualifiers from the array type into the element type if present |
| 3387 | // (C99 6.7.3p8). |
| 3388 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 3389 | assert(PrettyArrayType && "Not an array type!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3390 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3391 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3392 | |
| 3393 | // int x[restrict 4] -> int *restrict |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3394 | return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3395 | } |
| 3396 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3397 | QualType ASTContext::getBaseElementType(const ArrayType *array) const { |
| 3398 | return getBaseElementType(array->getElementType()); |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 3399 | } |
| 3400 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3401 | QualType ASTContext::getBaseElementType(QualType type) const { |
| 3402 | Qualifiers qs; |
| 3403 | while (true) { |
| 3404 | SplitQualType split = type.getSplitDesugaredType(); |
| 3405 | const ArrayType *array = split.first->getAsArrayTypeUnsafe(); |
| 3406 | if (!array) break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3407 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3408 | type = array->getElementType(); |
| 3409 | qs.addConsistentQualifiers(split.second); |
| 3410 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3411 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3412 | return getQualifiedType(type, qs); |
Anders Carlsson | 6183a99 | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 3413 | } |
| 3414 | |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 3415 | /// getConstantArrayElementCount - Returns number of constant array elements. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3416 | uint64_t |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 3417 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
| 3418 | uint64_t ElementCount = 1; |
| 3419 | do { |
| 3420 | ElementCount *= CA->getSize().getZExtValue(); |
| 3421 | CA = dyn_cast<ConstantArrayType>(CA->getElementType()); |
| 3422 | } while (CA); |
| 3423 | return ElementCount; |
| 3424 | } |
| 3425 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3426 | /// getFloatingRank - Return a relative rank for floating point types. |
| 3427 | /// This routine will assert if passed a built-in type that isn't a float. |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3428 | static FloatingRank getFloatingRank(QualType T) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3429 | if (const ComplexType *CT = T->getAs<ComplexType>()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3430 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3431 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3432 | assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"); |
| 3433 | switch (T->getAs<BuiltinType>()->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3434 | default: llvm_unreachable("getFloatingRank(): not a floating type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3435 | case BuiltinType::Float: return FloatRank; |
| 3436 | case BuiltinType::Double: return DoubleRank; |
| 3437 | case BuiltinType::LongDouble: return LongDoubleRank; |
| 3438 | } |
| 3439 | } |
| 3440 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3441 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 3442 | /// point or a complex type (based on typeDomain/typeSize). |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 3443 | /// 'typeDomain' is a real floating point or complex type. |
| 3444 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 3445 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 3446 | QualType Domain) const { |
| 3447 | FloatingRank EltRank = getFloatingRank(Size); |
| 3448 | if (Domain->isComplexType()) { |
| 3449 | switch (EltRank) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3450 | default: llvm_unreachable("getFloatingRank(): illegal value for rank"); |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 3451 | case FloatRank: return FloatComplexTy; |
| 3452 | case DoubleRank: return DoubleComplexTy; |
| 3453 | case LongDoubleRank: return LongDoubleComplexTy; |
| 3454 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3455 | } |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 3456 | |
| 3457 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 3458 | switch (EltRank) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3459 | default: llvm_unreachable("getFloatingRank(): illegal value for rank"); |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 3460 | case FloatRank: return FloatTy; |
| 3461 | case DoubleRank: return DoubleTy; |
| 3462 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 3463 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3464 | } |
| 3465 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3466 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 3467 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 3468 | /// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3469 | /// LHS < RHS, return -1. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3470 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const { |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3471 | FloatingRank LHSR = getFloatingRank(LHS); |
| 3472 | FloatingRank RHSR = getFloatingRank(RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3473 | |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3474 | if (LHSR == RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 3475 | return 0; |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3476 | if (LHSR > RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 3477 | return 1; |
| 3478 | return -1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3479 | } |
| 3480 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3481 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 3482 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 3483 | /// or if it is not canonicalized. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3484 | unsigned ASTContext::getIntegerRank(const Type *T) const { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 3485 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3486 | if (const EnumType* ET = dyn_cast<EnumType>(T)) |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 3487 | T = ET->getDecl()->getPromotionType().getTypePtr(); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3488 | |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 3489 | if (T->isSpecificBuiltinType(BuiltinType::WChar_S) || |
| 3490 | T->isSpecificBuiltinType(BuiltinType::WChar_U)) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3491 | T = getFromTargetType(Target->getWCharType()).getTypePtr(); |
Eli Friedman | a342675 | 2009-07-05 23:44:27 +0000 | [diff] [blame] | 3492 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3493 | if (T->isSpecificBuiltinType(BuiltinType::Char16)) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3494 | T = getFromTargetType(Target->getChar16Type()).getTypePtr(); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3495 | |
| 3496 | if (T->isSpecificBuiltinType(BuiltinType::Char32)) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3497 | T = getFromTargetType(Target->getChar32Type()).getTypePtr(); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3498 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3499 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3500 | default: llvm_unreachable("getIntegerRank(): not a built-in integer"); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3501 | case BuiltinType::Bool: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3502 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3503 | case BuiltinType::Char_S: |
| 3504 | case BuiltinType::Char_U: |
| 3505 | case BuiltinType::SChar: |
| 3506 | case BuiltinType::UChar: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3507 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3508 | case BuiltinType::Short: |
| 3509 | case BuiltinType::UShort: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3510 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3511 | case BuiltinType::Int: |
| 3512 | case BuiltinType::UInt: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3513 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3514 | case BuiltinType::Long: |
| 3515 | case BuiltinType::ULong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3516 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3517 | case BuiltinType::LongLong: |
| 3518 | case BuiltinType::ULongLong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3519 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 3520 | case BuiltinType::Int128: |
| 3521 | case BuiltinType::UInt128: |
| 3522 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3523 | } |
| 3524 | } |
| 3525 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3526 | /// \brief Whether this is a promotable bitfield reference according |
| 3527 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
| 3528 | /// |
| 3529 | /// \returns the type this bit-field will promote to, or NULL if no |
| 3530 | /// promotion occurs. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3531 | QualType ASTContext::isPromotableBitField(Expr *E) const { |
Douglas Gregor | ceafbde | 2010-05-24 20:13:53 +0000 | [diff] [blame] | 3532 | if (E->isTypeDependent() || E->isValueDependent()) |
| 3533 | return QualType(); |
| 3534 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3535 | FieldDecl *Field = E->getBitField(); |
| 3536 | if (!Field) |
| 3537 | return QualType(); |
| 3538 | |
| 3539 | QualType FT = Field->getType(); |
| 3540 | |
| 3541 | llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this); |
| 3542 | uint64_t BitWidth = BitWidthAP.getZExtValue(); |
| 3543 | uint64_t IntSize = getTypeSize(IntTy); |
| 3544 | // GCC extension compatibility: if the bit-field size is less than or equal |
| 3545 | // to the size of int, it gets promoted no matter what its type is. |
| 3546 | // For instance, unsigned long bf : 4 gets promoted to signed int. |
| 3547 | if (BitWidth < IntSize) |
| 3548 | return IntTy; |
| 3549 | |
| 3550 | if (BitWidth == IntSize) |
| 3551 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
| 3552 | |
| 3553 | // Types bigger than int are not subject to promotions, and therefore act |
| 3554 | // like the base type. |
| 3555 | // FIXME: This doesn't quite match what gcc does, but what gcc does here |
| 3556 | // is ridiculous. |
| 3557 | return QualType(); |
| 3558 | } |
| 3559 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3560 | /// getPromotedIntegerType - Returns the type that Promotable will |
| 3561 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
| 3562 | /// integer type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3563 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) const { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3564 | assert(!Promotable.isNull()); |
| 3565 | assert(Promotable->isPromotableIntegerType()); |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 3566 | if (const EnumType *ET = Promotable->getAs<EnumType>()) |
| 3567 | return ET->getDecl()->getPromotionType(); |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3568 | if (Promotable->isSignedIntegerType()) |
| 3569 | return IntTy; |
| 3570 | uint64_t PromotableSize = getTypeSize(Promotable); |
| 3571 | uint64_t IntSize = getTypeSize(IntTy); |
| 3572 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
| 3573 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
| 3574 | } |
| 3575 | |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 3576 | /// \brief Recurses in pointer/array types until it finds an objc retainable |
| 3577 | /// type and returns its ownership. |
| 3578 | Qualifiers::ObjCLifetime ASTContext::getInnerObjCOwnership(QualType T) const { |
| 3579 | while (!T.isNull()) { |
| 3580 | if (T.getObjCLifetime() != Qualifiers::OCL_None) |
| 3581 | return T.getObjCLifetime(); |
| 3582 | if (T->isArrayType()) |
| 3583 | T = getBaseElementType(T); |
| 3584 | else if (const PointerType *PT = T->getAs<PointerType>()) |
| 3585 | T = PT->getPointeeType(); |
| 3586 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
Argyrios Kyrtzidis | 28445f0 | 2011-07-01 23:01:46 +0000 | [diff] [blame] | 3587 | T = RT->getPointeeType(); |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 3588 | else |
| 3589 | break; |
| 3590 | } |
| 3591 | |
| 3592 | return Qualifiers::OCL_None; |
| 3593 | } |
| 3594 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3595 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3596 | /// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3597 | /// LHS < RHS, return -1. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3598 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3599 | const Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 3600 | const Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3601 | if (LHSC == RHSC) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3602 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3603 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 3604 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3605 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3606 | unsigned LHSRank = getIntegerRank(LHSC); |
| 3607 | unsigned RHSRank = getIntegerRank(RHSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3608 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3609 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 3610 | if (LHSRank == RHSRank) return 0; |
| 3611 | return LHSRank > RHSRank ? 1 : -1; |
| 3612 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3613 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3614 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 3615 | if (LHSUnsigned) { |
| 3616 | // If the unsigned [LHS] type is larger, return it. |
| 3617 | if (LHSRank >= RHSRank) |
| 3618 | return 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3619 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3620 | // If the signed type can represent all values of the unsigned type, it |
| 3621 | // wins. Because we are dealing with 2's complement and types that are |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3622 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3623 | return -1; |
| 3624 | } |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3625 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3626 | // If the unsigned [RHS] type is larger, return it. |
| 3627 | if (RHSRank >= LHSRank) |
| 3628 | return -1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3629 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3630 | // If the signed type can represent all values of the unsigned type, it |
| 3631 | // wins. Because we are dealing with 2's complement and types that are |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3632 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3633 | return 1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3634 | } |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3635 | |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3636 | static RecordDecl * |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3637 | CreateRecordDecl(const ASTContext &Ctx, RecordDecl::TagKind TK, |
| 3638 | DeclContext *DC, IdentifierInfo *Id) { |
| 3639 | SourceLocation Loc; |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3640 | if (Ctx.getLangOptions().CPlusPlus) |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3641 | return CXXRecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3642 | else |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3643 | return RecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3644 | } |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3645 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3646 | // getCFConstantStringType - Return the type used for constant CFStrings. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3647 | QualType ASTContext::getCFConstantStringType() const { |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3648 | if (!CFConstantStringTypeDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3649 | CFConstantStringTypeDecl = |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3650 | CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3651 | &Idents.get("NSConstantString")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3652 | CFConstantStringTypeDecl->startDefinition(); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3653 | |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 3654 | QualType FieldTypes[4]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3655 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3656 | // const int *isa; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3657 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 3658 | // int flags; |
| 3659 | FieldTypes[1] = IntTy; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3660 | // const char *str; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3661 | FieldTypes[2] = getPointerType(CharTy.withConst()); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3662 | // long length; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3663 | FieldTypes[3] = LongTy; |
| 3664 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3665 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3666 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3667 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3668 | SourceLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3669 | SourceLocation(), 0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3670 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3671 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3672 | /*Mutable=*/false, |
| 3673 | /*HasInit=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3674 | Field->setAccess(AS_public); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3675 | CFConstantStringTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3676 | } |
| 3677 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3678 | CFConstantStringTypeDecl->completeDefinition(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3679 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3680 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3681 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 8467583 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 3682 | } |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 3683 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3684 | void ASTContext::setCFConstantStringType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3685 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3686 | assert(Rec && "Invalid CFConstantStringType"); |
| 3687 | CFConstantStringTypeDecl = Rec->getDecl(); |
| 3688 | } |
| 3689 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3690 | QualType ASTContext::getBlockDescriptorType() const { |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3691 | if (BlockDescriptorType) |
| 3692 | return getTagDeclType(BlockDescriptorType); |
| 3693 | |
| 3694 | RecordDecl *T; |
| 3695 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3696 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3697 | &Idents.get("__block_descriptor")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3698 | T->startDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3699 | |
| 3700 | QualType FieldTypes[] = { |
| 3701 | UnsignedLongTy, |
| 3702 | UnsignedLongTy, |
| 3703 | }; |
| 3704 | |
| 3705 | const char *FieldNames[] = { |
| 3706 | "reserved", |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3707 | "Size" |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3708 | }; |
| 3709 | |
| 3710 | for (size_t i = 0; i < 2; ++i) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3711 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3712 | SourceLocation(), |
| 3713 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3714 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3715 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3716 | /*Mutable=*/false, |
| 3717 | /*HasInit=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3718 | Field->setAccess(AS_public); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3719 | T->addDecl(Field); |
| 3720 | } |
| 3721 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3722 | T->completeDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3723 | |
| 3724 | BlockDescriptorType = T; |
| 3725 | |
| 3726 | return getTagDeclType(BlockDescriptorType); |
| 3727 | } |
| 3728 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3729 | QualType ASTContext::getBlockDescriptorExtendedType() const { |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3730 | if (BlockDescriptorExtendedType) |
| 3731 | return getTagDeclType(BlockDescriptorExtendedType); |
| 3732 | |
| 3733 | RecordDecl *T; |
| 3734 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3735 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3736 | &Idents.get("__block_descriptor_withcopydispose")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3737 | T->startDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3738 | |
| 3739 | QualType FieldTypes[] = { |
| 3740 | UnsignedLongTy, |
| 3741 | UnsignedLongTy, |
| 3742 | getPointerType(VoidPtrTy), |
| 3743 | getPointerType(VoidPtrTy) |
| 3744 | }; |
| 3745 | |
| 3746 | const char *FieldNames[] = { |
| 3747 | "reserved", |
| 3748 | "Size", |
| 3749 | "CopyFuncPtr", |
| 3750 | "DestroyFuncPtr" |
| 3751 | }; |
| 3752 | |
| 3753 | for (size_t i = 0; i < 4; ++i) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3754 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3755 | SourceLocation(), |
| 3756 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3757 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3758 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3759 | /*Mutable=*/false, |
| 3760 | /*HasInit=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3761 | Field->setAccess(AS_public); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3762 | T->addDecl(Field); |
| 3763 | } |
| 3764 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3765 | T->completeDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3766 | |
| 3767 | BlockDescriptorExtendedType = T; |
| 3768 | |
| 3769 | return getTagDeclType(BlockDescriptorExtendedType); |
| 3770 | } |
| 3771 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3772 | bool ASTContext::BlockRequiresCopying(QualType Ty) const { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3773 | if (Ty->isObjCRetainableType()) |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3774 | return true; |
Fariborz Jahanian | e38be61 | 2010-11-17 00:21:28 +0000 | [diff] [blame] | 3775 | if (getLangOptions().CPlusPlus) { |
| 3776 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3777 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 3778 | return RD->hasConstCopyConstructor(); |
Fariborz Jahanian | e38be61 | 2010-11-17 00:21:28 +0000 | [diff] [blame] | 3779 | |
| 3780 | } |
| 3781 | } |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3782 | return false; |
| 3783 | } |
| 3784 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3785 | QualType |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3786 | ASTContext::BuildByRefType(StringRef DeclName, QualType Ty) const { |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3787 | // type = struct __Block_byref_1_X { |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3788 | // void *__isa; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3789 | // struct __Block_byref_1_X *__forwarding; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3790 | // unsigned int __flags; |
| 3791 | // unsigned int __size; |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 3792 | // void *__copy_helper; // as needed |
| 3793 | // void *__destroy_help // as needed |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3794 | // int X; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3795 | // } * |
| 3796 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3797 | bool HasCopyAndDispose = BlockRequiresCopying(Ty); |
| 3798 | |
| 3799 | // FIXME: Move up |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 3800 | llvm::SmallString<36> Name; |
| 3801 | llvm::raw_svector_ostream(Name) << "__Block_byref_" << |
| 3802 | ++UniqueBlockByRefTypeID << '_' << DeclName; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3803 | RecordDecl *T; |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3804 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, &Idents.get(Name.str())); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3805 | T->startDefinition(); |
| 3806 | QualType Int32Ty = IntTy; |
| 3807 | assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported"); |
| 3808 | QualType FieldTypes[] = { |
| 3809 | getPointerType(VoidPtrTy), |
| 3810 | getPointerType(getTagDeclType(T)), |
| 3811 | Int32Ty, |
| 3812 | Int32Ty, |
| 3813 | getPointerType(VoidPtrTy), |
| 3814 | getPointerType(VoidPtrTy), |
| 3815 | Ty |
| 3816 | }; |
| 3817 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3818 | StringRef FieldNames[] = { |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3819 | "__isa", |
| 3820 | "__forwarding", |
| 3821 | "__flags", |
| 3822 | "__size", |
| 3823 | "__copy_helper", |
| 3824 | "__destroy_helper", |
| 3825 | DeclName, |
| 3826 | }; |
| 3827 | |
| 3828 | for (size_t i = 0; i < 7; ++i) { |
| 3829 | if (!HasCopyAndDispose && i >=4 && i <= 5) |
| 3830 | continue; |
| 3831 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3832 | SourceLocation(), |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3833 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3834 | FieldTypes[i], /*TInfo=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3835 | /*BitWidth=*/0, /*Mutable=*/false, |
| 3836 | /*HasInit=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3837 | Field->setAccess(AS_public); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3838 | T->addDecl(Field); |
| 3839 | } |
| 3840 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3841 | T->completeDefinition(); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3842 | |
| 3843 | return getPointerType(getTagDeclType(T)); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3844 | } |
| 3845 | |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 3846 | TypedefDecl *ASTContext::getObjCInstanceTypeDecl() { |
| 3847 | if (!ObjCInstanceTypeDecl) |
| 3848 | ObjCInstanceTypeDecl = TypedefDecl::Create(*this, |
| 3849 | getTranslationUnitDecl(), |
| 3850 | SourceLocation(), |
| 3851 | SourceLocation(), |
| 3852 | &Idents.get("instancetype"), |
| 3853 | getTrivialTypeSourceInfo(getObjCIdType())); |
| 3854 | return ObjCInstanceTypeDecl; |
| 3855 | } |
| 3856 | |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3857 | // This returns true if a type has been typedefed to BOOL: |
| 3858 | // typedef <type> BOOL; |
Chris Lattner | 2d99833 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 3859 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3860 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | bb49c3e | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 3861 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 3862 | return II->isStr("BOOL"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3863 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3864 | return false; |
| 3865 | } |
| 3866 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3867 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3868 | /// purpose. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3869 | CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const { |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 3870 | if (!type->isIncompleteArrayType() && type->isIncompleteType()) |
| 3871 | return CharUnits::Zero(); |
| 3872 | |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3873 | CharUnits sz = getTypeSizeInChars(type); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3874 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3875 | // Make all integer and enum types at least as large as an int |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3876 | if (sz.isPositive() && type->isIntegralOrEnumerationType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3877 | sz = std::max(sz, getTypeSizeInChars(IntTy)); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3878 | // Treat arrays as pointers, since that's how they're passed in. |
| 3879 | else if (type->isArrayType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3880 | sz = getTypeSizeInChars(VoidPtrTy); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3881 | return sz; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3882 | } |
| 3883 | |
| 3884 | static inline |
| 3885 | std::string charUnitsToString(const CharUnits &CU) { |
| 3886 | return llvm::itostr(CU.getQuantity()); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3887 | } |
| 3888 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 3889 | /// getObjCEncodingForBlock - Return the encoded type for this block |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3890 | /// declaration. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 3891 | std::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const { |
| 3892 | std::string S; |
| 3893 | |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3894 | const BlockDecl *Decl = Expr->getBlockDecl(); |
| 3895 | QualType BlockTy = |
| 3896 | Expr->getType()->getAs<BlockPointerType>()->getPointeeType(); |
| 3897 | // Encode result type. |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 3898 | getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3899 | // Compute size of all parameters. |
| 3900 | // Start with computing size of a pointer in number of bytes. |
| 3901 | // FIXME: There might(should) be a better way of doing this computation! |
| 3902 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3903 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
| 3904 | CharUnits ParmOffset = PtrSize; |
Fariborz Jahanian | 6f46c26 | 2010-04-08 18:06:22 +0000 | [diff] [blame] | 3905 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3906 | E = Decl->param_end(); PI != E; ++PI) { |
| 3907 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3908 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3909 | assert (sz.isPositive() && "BlockExpr - Incomplete param type"); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3910 | ParmOffset += sz; |
| 3911 | } |
| 3912 | // Size of the argument frame |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3913 | S += charUnitsToString(ParmOffset); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3914 | // Block pointer and offset. |
| 3915 | S += "@?0"; |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3916 | |
| 3917 | // Argument types. |
| 3918 | ParmOffset = PtrSize; |
| 3919 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E = |
| 3920 | Decl->param_end(); PI != E; ++PI) { |
| 3921 | ParmVarDecl *PVDecl = *PI; |
| 3922 | QualType PType = PVDecl->getOriginalType(); |
| 3923 | if (const ArrayType *AT = |
| 3924 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3925 | // Use array's original type only if it has known number of |
| 3926 | // elements. |
| 3927 | if (!isa<ConstantArrayType>(AT)) |
| 3928 | PType = PVDecl->getType(); |
| 3929 | } else if (PType->isFunctionType()) |
| 3930 | PType = PVDecl->getType(); |
| 3931 | getObjCEncodingForType(PType, S); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3932 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3933 | ParmOffset += getObjCEncodingTypeSize(PType); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3934 | } |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 3935 | |
| 3936 | return S; |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3937 | } |
| 3938 | |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 3939 | bool ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl, |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 3940 | std::string& S) { |
| 3941 | // Encode result type. |
| 3942 | getObjCEncodingForType(Decl->getResultType(), S); |
| 3943 | CharUnits ParmOffset; |
| 3944 | // Compute size of all parameters. |
| 3945 | for (FunctionDecl::param_const_iterator PI = Decl->param_begin(), |
| 3946 | E = Decl->param_end(); PI != E; ++PI) { |
| 3947 | QualType PType = (*PI)->getType(); |
| 3948 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 3949 | if (sz.isZero()) |
| 3950 | return true; |
| 3951 | |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 3952 | assert (sz.isPositive() && |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 3953 | "getObjCEncodingForFunctionDecl - Incomplete param type"); |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 3954 | ParmOffset += sz; |
| 3955 | } |
| 3956 | S += charUnitsToString(ParmOffset); |
| 3957 | ParmOffset = CharUnits::Zero(); |
| 3958 | |
| 3959 | // Argument types. |
| 3960 | for (FunctionDecl::param_const_iterator PI = Decl->param_begin(), |
| 3961 | E = Decl->param_end(); PI != E; ++PI) { |
| 3962 | ParmVarDecl *PVDecl = *PI; |
| 3963 | QualType PType = PVDecl->getOriginalType(); |
| 3964 | if (const ArrayType *AT = |
| 3965 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3966 | // Use array's original type only if it has known number of |
| 3967 | // elements. |
| 3968 | if (!isa<ConstantArrayType>(AT)) |
| 3969 | PType = PVDecl->getType(); |
| 3970 | } else if (PType->isFunctionType()) |
| 3971 | PType = PVDecl->getType(); |
| 3972 | getObjCEncodingForType(PType, S); |
| 3973 | S += charUnitsToString(ParmOffset); |
| 3974 | ParmOffset += getObjCEncodingTypeSize(PType); |
| 3975 | } |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 3976 | |
| 3977 | return false; |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 3978 | } |
| 3979 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3980 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3981 | /// declaration. |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 3982 | bool ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3983 | std::string& S) const { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3984 | // FIXME: This is not very efficient. |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3985 | // Encode type qualifer, 'in', 'inout', etc. for the return type. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3986 | getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3987 | // Encode result type. |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3988 | getObjCEncodingForType(Decl->getResultType(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3989 | // Compute size of all parameters. |
| 3990 | // Start with computing size of a pointer in number of bytes. |
| 3991 | // FIXME: There might(should) be a better way of doing this computation! |
| 3992 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3993 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3994 | // The first two arguments (self and _cmd) are pointers; account for |
| 3995 | // their size. |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3996 | CharUnits ParmOffset = 2 * PtrSize; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3997 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 3998 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3999 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4000 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4001 | if (sz.isZero()) |
| 4002 | return true; |
| 4003 | |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4004 | assert (sz.isPositive() && |
| 4005 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4006 | ParmOffset += sz; |
| 4007 | } |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4008 | S += charUnitsToString(ParmOffset); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4009 | S += "@0:"; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4010 | S += charUnitsToString(PtrSize); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4011 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4012 | // Argument types. |
| 4013 | ParmOffset = 2 * PtrSize; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 4014 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 4015 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 4016 | ParmVarDecl *PVDecl = *PI; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4017 | QualType PType = PVDecl->getOriginalType(); |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 4018 | if (const ArrayType *AT = |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 4019 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4020 | // Use array's original type only if it has known number of |
| 4021 | // elements. |
Steve Naroff | bb3fde3 | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 4022 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 4023 | PType = PVDecl->getType(); |
| 4024 | } else if (PType->isFunctionType()) |
| 4025 | PType = PVDecl->getType(); |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 4026 | // Process argument qualifiers for user supplied arguments; such as, |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4027 | // 'in', 'inout', etc. |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 4028 | getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 4029 | getObjCEncodingForType(PType, S); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4030 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4031 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4032 | } |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4033 | |
| 4034 | return false; |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4035 | } |
| 4036 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4037 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4038 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4039 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 4040 | /// NULL when getting encodings for protocol properties. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4041 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 4042 | /// attributes readonly and bycopy are encoded as single characters. The |
| 4043 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 4044 | /// encoded as single characters, followed by an identifier. Property types |
| 4045 | /// are also encoded as a parametrized attribute. The characters used to encode |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4046 | /// these attributes are defined by the following enumeration: |
| 4047 | /// @code |
| 4048 | /// enum PropertyAttributes { |
| 4049 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 4050 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 4051 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 4052 | /// kPropertyDynamic = 'D', // property is dynamic |
| 4053 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 4054 | /// kPropertySetter = 'S', // followed by setter selector name |
| 4055 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
| 4056 | /// kPropertyType = 't' // followed by old-style type encoding. |
| 4057 | /// kPropertyWeak = 'W' // 'weak' property |
| 4058 | /// kPropertyStrong = 'P' // property GC'able |
| 4059 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 4060 | /// }; |
| 4061 | /// @endcode |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4062 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4063 | const Decl *Container, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4064 | std::string& S) const { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4065 | // Collect information from the property implementation decl(s). |
| 4066 | bool Dynamic = false; |
| 4067 | ObjCPropertyImplDecl *SynthesizePID = 0; |
| 4068 | |
| 4069 | // FIXME: Duplicated code due to poor abstraction. |
| 4070 | if (Container) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4071 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4072 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
| 4073 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4074 | i = CID->propimpl_begin(), e = CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4075 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4076 | ObjCPropertyImplDecl *PID = *i; |
| 4077 | if (PID->getPropertyDecl() == PD) { |
| 4078 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 4079 | Dynamic = true; |
| 4080 | } else { |
| 4081 | SynthesizePID = PID; |
| 4082 | } |
| 4083 | } |
| 4084 | } |
| 4085 | } else { |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4086 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4087 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4088 | i = OID->propimpl_begin(), e = OID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4089 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4090 | ObjCPropertyImplDecl *PID = *i; |
| 4091 | if (PID->getPropertyDecl() == PD) { |
| 4092 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 4093 | Dynamic = true; |
| 4094 | } else { |
| 4095 | SynthesizePID = PID; |
| 4096 | } |
| 4097 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4098 | } |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4099 | } |
| 4100 | } |
| 4101 | |
| 4102 | // FIXME: This is not very efficient. |
| 4103 | S = "T"; |
| 4104 | |
| 4105 | // Encode result type. |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4106 | // GCC has some special rules regarding encoding of properties which |
| 4107 | // closely resembles encoding of ivars. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4108 | getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4109 | true /* outermost type */, |
| 4110 | true /* encoding for property */); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4111 | |
| 4112 | if (PD->isReadOnly()) { |
| 4113 | S += ",R"; |
| 4114 | } else { |
| 4115 | switch (PD->getSetterKind()) { |
| 4116 | case ObjCPropertyDecl::Assign: break; |
| 4117 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4118 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
Fariborz Jahanian | 3a02b44 | 2011-08-12 20:47:08 +0000 | [diff] [blame] | 4119 | case ObjCPropertyDecl::Weak: S += ",W"; break; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4120 | } |
| 4121 | } |
| 4122 | |
| 4123 | // It really isn't clear at all what this means, since properties |
| 4124 | // are "dynamic by default". |
| 4125 | if (Dynamic) |
| 4126 | S += ",D"; |
| 4127 | |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4128 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 4129 | S += ",N"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4130 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4131 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 4132 | S += ",G"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4133 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4134 | } |
| 4135 | |
| 4136 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 4137 | S += ",S"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4138 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4139 | } |
| 4140 | |
| 4141 | if (SynthesizePID) { |
| 4142 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 4143 | S += ",V"; |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 4144 | S += OID->getNameAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4145 | } |
| 4146 | |
| 4147 | // FIXME: OBJCGC: weak & strong |
| 4148 | } |
| 4149 | |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4150 | /// getLegacyIntegralTypeEncoding - |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4151 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
| 4152 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4153 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
| 4154 | /// |
| 4155 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 4156 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4157 | if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) { |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4158 | if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4159 | PointeeTy = UnsignedIntTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4160 | else |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4161 | if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4162 | PointeeTy = IntTy; |
| 4163 | } |
| 4164 | } |
| 4165 | } |
| 4166 | |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4167 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4168 | const FieldDecl *Field) const { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4169 | // We follow the behavior of gcc, expanding structures which are |
| 4170 | // directly pointed to, and expanding embedded structures. Note that |
| 4171 | // these rules are sufficient to prevent recursive encoding of the |
| 4172 | // same type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4173 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
Fariborz Jahanian | 5b8c7d9 | 2008-12-22 23:22:27 +0000 | [diff] [blame] | 4174 | true /* outermost type */); |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4175 | } |
| 4176 | |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4177 | static char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) { |
| 4178 | switch (T->getAs<BuiltinType>()->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4179 | default: llvm_unreachable("Unhandled builtin type kind"); |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4180 | case BuiltinType::Void: return 'v'; |
| 4181 | case BuiltinType::Bool: return 'B'; |
| 4182 | case BuiltinType::Char_U: |
| 4183 | case BuiltinType::UChar: return 'C'; |
| 4184 | case BuiltinType::UShort: return 'S'; |
| 4185 | case BuiltinType::UInt: return 'I'; |
| 4186 | case BuiltinType::ULong: |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4187 | return C->getIntWidth(T) == 32 ? 'L' : 'Q'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4188 | case BuiltinType::UInt128: return 'T'; |
| 4189 | case BuiltinType::ULongLong: return 'Q'; |
| 4190 | case BuiltinType::Char_S: |
| 4191 | case BuiltinType::SChar: return 'c'; |
| 4192 | case BuiltinType::Short: return 's'; |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 4193 | case BuiltinType::WChar_S: |
| 4194 | case BuiltinType::WChar_U: |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4195 | case BuiltinType::Int: return 'i'; |
| 4196 | case BuiltinType::Long: |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4197 | return C->getIntWidth(T) == 32 ? 'l' : 'q'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4198 | case BuiltinType::LongLong: return 'q'; |
| 4199 | case BuiltinType::Int128: return 't'; |
| 4200 | case BuiltinType::Float: return 'f'; |
| 4201 | case BuiltinType::Double: return 'd'; |
Daniel Dunbar | 3a0be84 | 2010-10-11 21:13:48 +0000 | [diff] [blame] | 4202 | case BuiltinType::LongDouble: return 'D'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4203 | } |
| 4204 | } |
| 4205 | |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4206 | static char ObjCEncodingForEnumType(const ASTContext *C, const EnumType *ET) { |
| 4207 | EnumDecl *Enum = ET->getDecl(); |
| 4208 | |
| 4209 | // The encoding of an non-fixed enum type is always 'i', regardless of size. |
| 4210 | if (!Enum->isFixed()) |
| 4211 | return 'i'; |
| 4212 | |
| 4213 | // The encoding of a fixed enum type matches its fixed underlying type. |
| 4214 | return ObjCEncodingForPrimitiveKind(C, Enum->getIntegerType()); |
| 4215 | } |
| 4216 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4217 | static void EncodeBitField(const ASTContext *Ctx, std::string& S, |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4218 | QualType T, const FieldDecl *FD) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4219 | const Expr *E = FD->getBitWidth(); |
| 4220 | assert(E && "bitfield width not there - getObjCEncodingForTypeImpl"); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4221 | S += 'b'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4222 | // The NeXT runtime encodes bit fields as b followed by the number of bits. |
| 4223 | // The GNU runtime requires more information; bitfields are encoded as b, |
| 4224 | // then the offset (in bits) of the first element, then the type of the |
| 4225 | // bitfield, then the size in bits. For example, in this structure: |
| 4226 | // |
| 4227 | // struct |
| 4228 | // { |
| 4229 | // int integer; |
| 4230 | // int flags:2; |
| 4231 | // }; |
| 4232 | // On a 32-bit system, the encoding for flags would be b2 for the NeXT |
| 4233 | // runtime, but b32i2 for the GNU runtime. The reason for this extra |
| 4234 | // information is not especially sensible, but we're stuck with it for |
| 4235 | // compatibility with GCC, although providing it breaks anything that |
| 4236 | // actually uses runtime introspection and wants to work on both runtimes... |
| 4237 | if (!Ctx->getLangOptions().NeXTRuntime) { |
| 4238 | const RecordDecl *RD = FD->getParent(); |
| 4239 | const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD); |
Eli Friedman | 8290574 | 2011-07-07 01:54:01 +0000 | [diff] [blame] | 4240 | S += llvm::utostr(RL.getFieldOffset(FD->getFieldIndex())); |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4241 | if (const EnumType *ET = T->getAs<EnumType>()) |
| 4242 | S += ObjCEncodingForEnumType(Ctx, ET); |
David Chisnall | c7ff82c | 2010-12-26 20:12:30 +0000 | [diff] [blame] | 4243 | else |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4244 | S += ObjCEncodingForPrimitiveKind(Ctx, T); |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4245 | } |
| 4246 | unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue(); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4247 | S += llvm::utostr(N); |
| 4248 | } |
| 4249 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 4250 | // FIXME: Use SmallString for accumulating string. |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4251 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 4252 | bool ExpandPointedToStructures, |
| 4253 | bool ExpandStructures, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 4254 | const FieldDecl *FD, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4255 | bool OutermostType, |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4256 | bool EncodingProperty, |
| 4257 | bool StructField) const { |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4258 | if (T->getAs<BuiltinType>()) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4259 | if (FD && FD->isBitField()) |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4260 | return EncodeBitField(this, S, T, FD); |
| 4261 | S += ObjCEncodingForPrimitiveKind(this, T); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4262 | return; |
| 4263 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4264 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4265 | if (const ComplexType *CT = T->getAs<ComplexType>()) { |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 4266 | S += 'j'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4267 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false, |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 4268 | false); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4269 | return; |
| 4270 | } |
Fariborz Jahanian | 60bce3e | 2009-11-23 20:40:50 +0000 | [diff] [blame] | 4271 | |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4272 | // encoding for pointer or r3eference types. |
| 4273 | QualType PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4274 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Fariborz Jahanian | 8d2c0a9 | 2009-11-30 18:43:52 +0000 | [diff] [blame] | 4275 | if (PT->isObjCSelType()) { |
| 4276 | S += ':'; |
| 4277 | return; |
| 4278 | } |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4279 | PointeeTy = PT->getPointeeType(); |
| 4280 | } |
| 4281 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 4282 | PointeeTy = RT->getPointeeType(); |
| 4283 | if (!PointeeTy.isNull()) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4284 | bool isReadOnly = false; |
| 4285 | // For historical/compatibility reasons, the read-only qualifier of the |
| 4286 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 4287 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4288 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 4289 | if (isa<TypedefType>(T.getTypePtr())) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4290 | if (OutermostType && T.isConstQualified()) { |
| 4291 | isReadOnly = true; |
| 4292 | S += 'r'; |
| 4293 | } |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 4294 | } else if (OutermostType) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4295 | QualType P = PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4296 | while (P->getAs<PointerType>()) |
| 4297 | P = P->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4298 | if (P.isConstQualified()) { |
| 4299 | isReadOnly = true; |
| 4300 | S += 'r'; |
| 4301 | } |
| 4302 | } |
| 4303 | if (isReadOnly) { |
| 4304 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 4305 | // combinations need to be rearranged. |
| 4306 | // Rewrite "in const" from "nr" to "rn" |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4307 | if (StringRef(S).endswith("nr")) |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 4308 | S.replace(S.end()-2, S.end(), "rn"); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4309 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4310 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4311 | if (PointeeTy->isCharType()) { |
| 4312 | // char pointer types should be encoded as '*' unless it is a |
| 4313 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 4314 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4315 | S += '*'; |
| 4316 | return; |
| 4317 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4318 | } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) { |
Steve Naroff | 9533a7f | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 4319 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 4320 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 4321 | S += '#'; |
| 4322 | return; |
| 4323 | } |
| 4324 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 4325 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 4326 | S += '@'; |
| 4327 | return; |
| 4328 | } |
| 4329 | // fall through... |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4330 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4331 | S += '^'; |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4332 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 4333 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4334 | getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4335 | NULL); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4336 | return; |
| 4337 | } |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4338 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4339 | if (const ArrayType *AT = |
| 4340 | // Ignore type qualifiers etc. |
| 4341 | dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) { |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4342 | if (isa<IncompleteArrayType>(AT) && !StructField) { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4343 | // Incomplete arrays are encoded as a pointer to the array element. |
| 4344 | S += '^'; |
| 4345 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4346 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4347 | false, ExpandStructures, FD); |
| 4348 | } else { |
| 4349 | S += '['; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4350 | |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4351 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
| 4352 | if (getTypeSize(CAT->getElementType()) == 0) |
| 4353 | S += '0'; |
| 4354 | else |
| 4355 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 4356 | } else { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4357 | //Variable length arrays are encoded as a regular array with 0 elements. |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4358 | assert((isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) && |
| 4359 | "Unknown array type!"); |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4360 | S += '0'; |
| 4361 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4362 | |
| 4363 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4364 | false, ExpandStructures, FD); |
| 4365 | S += ']'; |
| 4366 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4367 | return; |
| 4368 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4369 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4370 | if (T->getAs<FunctionType>()) { |
Anders Carlsson | c0a87b7 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 4371 | S += '?'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4372 | return; |
| 4373 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4374 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4375 | if (const RecordType *RTy = T->getAs<RecordType>()) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4376 | RecordDecl *RDecl = RTy->getDecl(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4377 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 4378 | // Anonymous structures print as '?' |
| 4379 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 4380 | S += II->getName(); |
Fariborz Jahanian | 6fb9439 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 4381 | if (ClassTemplateSpecializationDecl *Spec |
| 4382 | = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) { |
| 4383 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 4384 | std::string TemplateArgsStr |
| 4385 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4386 | TemplateArgs.data(), |
| 4387 | TemplateArgs.size(), |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 4388 | (*this).getPrintingPolicy()); |
Fariborz Jahanian | 6fb9439 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 4389 | |
| 4390 | S += TemplateArgsStr; |
| 4391 | } |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 4392 | } else { |
| 4393 | S += '?'; |
| 4394 | } |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 4395 | if (ExpandStructures) { |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4396 | S += '='; |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4397 | if (!RDecl->isUnion()) { |
| 4398 | getObjCEncodingForStructureImpl(RDecl, S, FD); |
| 4399 | } else { |
| 4400 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 4401 | FieldEnd = RDecl->field_end(); |
| 4402 | Field != FieldEnd; ++Field) { |
| 4403 | if (FD) { |
| 4404 | S += '"'; |
| 4405 | S += Field->getNameAsString(); |
| 4406 | S += '"'; |
| 4407 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4408 | |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4409 | // Special case bit-fields. |
| 4410 | if (Field->isBitField()) { |
| 4411 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
| 4412 | (*Field)); |
| 4413 | } else { |
| 4414 | QualType qt = Field->getType(); |
| 4415 | getLegacyIntegralTypeEncoding(qt); |
| 4416 | getObjCEncodingForTypeImpl(qt, S, false, true, |
| 4417 | FD, /*OutermostType*/false, |
| 4418 | /*EncodingProperty*/false, |
| 4419 | /*StructField*/true); |
| 4420 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4421 | } |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4422 | } |
Fariborz Jahanian | 6de88a8 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 4423 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4424 | S += RDecl->isUnion() ? ')' : '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4425 | return; |
| 4426 | } |
Fariborz Jahanian | e6012c7 | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 4427 | |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4428 | if (const EnumType *ET = T->getAs<EnumType>()) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4429 | if (FD && FD->isBitField()) |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4430 | EncodeBitField(this, S, T, FD); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4431 | else |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4432 | S += ObjCEncodingForEnumType(this, ET); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4433 | return; |
| 4434 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4435 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4436 | if (T->isBlockPointerType()) { |
Steve Naroff | 21a98b1 | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 4437 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4438 | return; |
| 4439 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4440 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4441 | // Ignore protocol qualifiers when mangling at this level. |
| 4442 | if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>()) |
| 4443 | T = OT->getBaseType(); |
| 4444 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4445 | if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4446 | // @encode(class_name) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4447 | ObjCInterfaceDecl *OI = OIT->getDecl(); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4448 | S += '{'; |
| 4449 | const IdentifierInfo *II = OI->getIdentifier(); |
| 4450 | S += II->getName(); |
| 4451 | S += '='; |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4452 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4453 | DeepCollectObjCIvars(OI, true, Ivars); |
| 4454 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) { |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4455 | const FieldDecl *Field = cast<FieldDecl>(Ivars[i]); |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4456 | if (Field->isBitField()) |
| 4457 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4458 | else |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4459 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4460 | } |
| 4461 | S += '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4462 | return; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4463 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4464 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4465 | if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4466 | if (OPT->isObjCIdType()) { |
| 4467 | S += '@'; |
| 4468 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4469 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4470 | |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4471 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
| 4472 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
| 4473 | // Since this is a binary compatibility issue, need to consult with runtime |
| 4474 | // folks. Fortunately, this is a *very* obsure construct. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4475 | S += '#'; |
| 4476 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4477 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4478 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4479 | if (OPT->isObjCQualifiedIdType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4480 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4481 | ExpandPointedToStructures, |
| 4482 | ExpandStructures, FD); |
| 4483 | if (FD || EncodingProperty) { |
| 4484 | // Note that we do extended encoding of protocol qualifer list |
| 4485 | // Only when doing ivar or property encoding. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4486 | S += '"'; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4487 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 4488 | E = OPT->qual_end(); I != E; ++I) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4489 | S += '<'; |
| 4490 | S += (*I)->getNameAsString(); |
| 4491 | S += '>'; |
| 4492 | } |
| 4493 | S += '"'; |
| 4494 | } |
| 4495 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4496 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4497 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4498 | QualType PointeeTy = OPT->getPointeeType(); |
| 4499 | if (!EncodingProperty && |
| 4500 | isa<TypedefType>(PointeeTy.getTypePtr())) { |
| 4501 | // Another historical/compatibility reason. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4502 | // We encode the underlying type which comes out as |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4503 | // {...}; |
| 4504 | S += '^'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4505 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 4506 | false, ExpandPointedToStructures, |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4507 | NULL); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4508 | return; |
| 4509 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4510 | |
| 4511 | S += '@'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4512 | if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4513 | S += '"'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4514 | S += OPT->getInterfaceDecl()->getIdentifier()->getName(); |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4515 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 4516 | E = OPT->qual_end(); I != E; ++I) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4517 | S += '<'; |
| 4518 | S += (*I)->getNameAsString(); |
| 4519 | S += '>'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4520 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4521 | S += '"'; |
| 4522 | } |
| 4523 | return; |
| 4524 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4525 | |
John McCall | 532ec7b | 2010-05-17 23:56:34 +0000 | [diff] [blame] | 4526 | // gcc just blithely ignores member pointers. |
| 4527 | // TODO: maybe there should be a mangling for these |
| 4528 | if (T->getAs<MemberPointerType>()) |
| 4529 | return; |
Fariborz Jahanian | e6012c7 | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 4530 | |
| 4531 | if (T->isVectorType()) { |
| 4532 | // This matches gcc's encoding, even though technically it is |
| 4533 | // insufficient. |
| 4534 | // FIXME. We should do a better job than gcc. |
| 4535 | return; |
| 4536 | } |
| 4537 | |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4538 | llvm_unreachable("@encode for type not implemented!"); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4539 | } |
| 4540 | |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4541 | void ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl, |
| 4542 | std::string &S, |
| 4543 | const FieldDecl *FD, |
| 4544 | bool includeVBases) const { |
| 4545 | assert(RDecl && "Expected non-null RecordDecl"); |
| 4546 | assert(!RDecl->isUnion() && "Should not be called for unions"); |
| 4547 | if (!RDecl->getDefinition()) |
| 4548 | return; |
| 4549 | |
| 4550 | CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(RDecl); |
| 4551 | std::multimap<uint64_t, NamedDecl *> FieldOrBaseOffsets; |
| 4552 | const ASTRecordLayout &layout = getASTRecordLayout(RDecl); |
| 4553 | |
| 4554 | if (CXXRec) { |
| 4555 | for (CXXRecordDecl::base_class_iterator |
| 4556 | BI = CXXRec->bases_begin(), |
| 4557 | BE = CXXRec->bases_end(); BI != BE; ++BI) { |
| 4558 | if (!BI->isVirtual()) { |
| 4559 | CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 829f200 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 4560 | if (base->isEmpty()) |
| 4561 | continue; |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4562 | uint64_t offs = layout.getBaseClassOffsetInBits(base); |
| 4563 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 4564 | std::make_pair(offs, base)); |
| 4565 | } |
| 4566 | } |
| 4567 | } |
| 4568 | |
| 4569 | unsigned i = 0; |
| 4570 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 4571 | FieldEnd = RDecl->field_end(); |
| 4572 | Field != FieldEnd; ++Field, ++i) { |
| 4573 | uint64_t offs = layout.getFieldOffset(i); |
| 4574 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 4575 | std::make_pair(offs, *Field)); |
| 4576 | } |
| 4577 | |
| 4578 | if (CXXRec && includeVBases) { |
| 4579 | for (CXXRecordDecl::base_class_iterator |
| 4580 | BI = CXXRec->vbases_begin(), |
| 4581 | BE = CXXRec->vbases_end(); BI != BE; ++BI) { |
| 4582 | CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 829f200 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 4583 | if (base->isEmpty()) |
| 4584 | continue; |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4585 | uint64_t offs = layout.getVBaseClassOffsetInBits(base); |
Argyrios Kyrtzidis | 19aa860 | 2011-09-26 18:14:24 +0000 | [diff] [blame] | 4586 | if (FieldOrBaseOffsets.find(offs) == FieldOrBaseOffsets.end()) |
| 4587 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.end(), |
| 4588 | std::make_pair(offs, base)); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4589 | } |
| 4590 | } |
| 4591 | |
| 4592 | CharUnits size; |
| 4593 | if (CXXRec) { |
| 4594 | size = includeVBases ? layout.getSize() : layout.getNonVirtualSize(); |
| 4595 | } else { |
| 4596 | size = layout.getSize(); |
| 4597 | } |
| 4598 | |
| 4599 | uint64_t CurOffs = 0; |
| 4600 | std::multimap<uint64_t, NamedDecl *>::iterator |
| 4601 | CurLayObj = FieldOrBaseOffsets.begin(); |
| 4602 | |
Argyrios Kyrtzidis | cb8061e | 2011-08-22 16:03:14 +0000 | [diff] [blame] | 4603 | if ((CurLayObj != FieldOrBaseOffsets.end() && CurLayObj->first != 0) || |
| 4604 | (CurLayObj == FieldOrBaseOffsets.end() && |
| 4605 | CXXRec && CXXRec->isDynamicClass())) { |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4606 | assert(CXXRec && CXXRec->isDynamicClass() && |
| 4607 | "Offset 0 was empty but no VTable ?"); |
| 4608 | if (FD) { |
| 4609 | S += "\"_vptr$"; |
| 4610 | std::string recname = CXXRec->getNameAsString(); |
| 4611 | if (recname.empty()) recname = "?"; |
| 4612 | S += recname; |
| 4613 | S += '"'; |
| 4614 | } |
| 4615 | S += "^^?"; |
| 4616 | CurOffs += getTypeSize(VoidPtrTy); |
| 4617 | } |
| 4618 | |
| 4619 | if (!RDecl->hasFlexibleArrayMember()) { |
| 4620 | // Mark the end of the structure. |
| 4621 | uint64_t offs = toBits(size); |
| 4622 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 4623 | std::make_pair(offs, (NamedDecl*)0)); |
| 4624 | } |
| 4625 | |
| 4626 | for (; CurLayObj != FieldOrBaseOffsets.end(); ++CurLayObj) { |
| 4627 | assert(CurOffs <= CurLayObj->first); |
| 4628 | |
| 4629 | if (CurOffs < CurLayObj->first) { |
| 4630 | uint64_t padding = CurLayObj->first - CurOffs; |
| 4631 | // FIXME: There doesn't seem to be a way to indicate in the encoding that |
| 4632 | // packing/alignment of members is different that normal, in which case |
| 4633 | // the encoding will be out-of-sync with the real layout. |
| 4634 | // If the runtime switches to just consider the size of types without |
| 4635 | // taking into account alignment, we could make padding explicit in the |
| 4636 | // encoding (e.g. using arrays of chars). The encoding strings would be |
| 4637 | // longer then though. |
| 4638 | CurOffs += padding; |
| 4639 | } |
| 4640 | |
| 4641 | NamedDecl *dcl = CurLayObj->second; |
| 4642 | if (dcl == 0) |
| 4643 | break; // reached end of structure. |
| 4644 | |
| 4645 | if (CXXRecordDecl *base = dyn_cast<CXXRecordDecl>(dcl)) { |
| 4646 | // We expand the bases without their virtual bases since those are going |
| 4647 | // in the initial structure. Note that this differs from gcc which |
| 4648 | // expands virtual bases each time one is encountered in the hierarchy, |
| 4649 | // making the encoding type bigger than it really is. |
| 4650 | getObjCEncodingForStructureImpl(base, S, FD, /*includeVBases*/false); |
Argyrios Kyrtzidis | 829f200 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 4651 | assert(!base->isEmpty()); |
| 4652 | CurOffs += toBits(getASTRecordLayout(base).getNonVirtualSize()); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4653 | } else { |
| 4654 | FieldDecl *field = cast<FieldDecl>(dcl); |
| 4655 | if (FD) { |
| 4656 | S += '"'; |
| 4657 | S += field->getNameAsString(); |
| 4658 | S += '"'; |
| 4659 | } |
| 4660 | |
| 4661 | if (field->isBitField()) { |
| 4662 | EncodeBitField(this, S, field->getType(), field); |
| 4663 | CurOffs += field->getBitWidth()->EvaluateAsInt(*this).getZExtValue(); |
| 4664 | } else { |
| 4665 | QualType qt = field->getType(); |
| 4666 | getLegacyIntegralTypeEncoding(qt); |
| 4667 | getObjCEncodingForTypeImpl(qt, S, false, true, FD, |
| 4668 | /*OutermostType*/false, |
| 4669 | /*EncodingProperty*/false, |
| 4670 | /*StructField*/true); |
| 4671 | CurOffs += getTypeSize(field->getType()); |
| 4672 | } |
| 4673 | } |
| 4674 | } |
| 4675 | } |
| 4676 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4677 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 4678 | std::string& S) const { |
| 4679 | if (QT & Decl::OBJC_TQ_In) |
| 4680 | S += 'n'; |
| 4681 | if (QT & Decl::OBJC_TQ_Inout) |
| 4682 | S += 'N'; |
| 4683 | if (QT & Decl::OBJC_TQ_Out) |
| 4684 | S += 'o'; |
| 4685 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 4686 | S += 'O'; |
| 4687 | if (QT & Decl::OBJC_TQ_Byref) |
| 4688 | S += 'R'; |
| 4689 | if (QT & Decl::OBJC_TQ_Oneway) |
| 4690 | S += 'V'; |
| 4691 | } |
| 4692 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4693 | void ASTContext::setBuiltinVaListType(QualType T) { |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 4694 | assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4695 | |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 4696 | BuiltinVaListType = T; |
| 4697 | } |
| 4698 | |
Douglas Gregor | 4dfd02a | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 4699 | TypedefDecl *ASTContext::getObjCIdDecl() const { |
| 4700 | if (!ObjCIdDecl) { |
| 4701 | QualType T = getObjCObjectType(ObjCBuiltinIdTy, 0, 0); |
| 4702 | T = getObjCObjectPointerType(T); |
| 4703 | TypeSourceInfo *IdInfo = getTrivialTypeSourceInfo(T); |
| 4704 | ObjCIdDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 4705 | getTranslationUnitDecl(), |
| 4706 | SourceLocation(), SourceLocation(), |
| 4707 | &Idents.get("id"), IdInfo); |
| 4708 | } |
| 4709 | |
| 4710 | return ObjCIdDecl; |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 4711 | } |
| 4712 | |
Douglas Gregor | 7a27ea5 | 2011-08-12 06:17:30 +0000 | [diff] [blame] | 4713 | TypedefDecl *ASTContext::getObjCSelDecl() const { |
| 4714 | if (!ObjCSelDecl) { |
| 4715 | QualType SelT = getPointerType(ObjCBuiltinSelTy); |
| 4716 | TypeSourceInfo *SelInfo = getTrivialTypeSourceInfo(SelT); |
| 4717 | ObjCSelDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 4718 | getTranslationUnitDecl(), |
| 4719 | SourceLocation(), SourceLocation(), |
| 4720 | &Idents.get("SEL"), SelInfo); |
| 4721 | } |
| 4722 | return ObjCSelDecl; |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 4723 | } |
| 4724 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4725 | void ASTContext::setObjCProtoType(QualType QT) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4726 | ObjCProtoType = QT; |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 4727 | } |
| 4728 | |
Douglas Gregor | 79d6726 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 4729 | TypedefDecl *ASTContext::getObjCClassDecl() const { |
| 4730 | if (!ObjCClassDecl) { |
| 4731 | QualType T = getObjCObjectType(ObjCBuiltinClassTy, 0, 0); |
| 4732 | T = getObjCObjectPointerType(T); |
| 4733 | TypeSourceInfo *ClassInfo = getTrivialTypeSourceInfo(T); |
| 4734 | ObjCClassDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 4735 | getTranslationUnitDecl(), |
| 4736 | SourceLocation(), SourceLocation(), |
| 4737 | &Idents.get("Class"), ClassInfo); |
| 4738 | } |
| 4739 | |
| 4740 | return ObjCClassDecl; |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 4741 | } |
| 4742 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4743 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4744 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 4745 | "'NSConstantString' type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4746 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4747 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 4748 | } |
| 4749 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4750 | /// \brief Retrieve the template name that corresponds to a non-empty |
| 4751 | /// lookup. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4752 | TemplateName |
| 4753 | ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin, |
| 4754 | UnresolvedSetIterator End) const { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4755 | unsigned size = End - Begin; |
| 4756 | assert(size > 1 && "set is not overloaded!"); |
| 4757 | |
| 4758 | void *memory = Allocate(sizeof(OverloadedTemplateStorage) + |
| 4759 | size * sizeof(FunctionTemplateDecl*)); |
| 4760 | OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size); |
| 4761 | |
| 4762 | NamedDecl **Storage = OT->getStorage(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4763 | for (UnresolvedSetIterator I = Begin; I != End; ++I) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4764 | NamedDecl *D = *I; |
| 4765 | assert(isa<FunctionTemplateDecl>(D) || |
| 4766 | (isa<UsingShadowDecl>(D) && |
| 4767 | isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))); |
| 4768 | *Storage++ = D; |
| 4769 | } |
| 4770 | |
| 4771 | return TemplateName(OT); |
| 4772 | } |
| 4773 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4774 | /// \brief Retrieve the template name that represents a qualified |
| 4775 | /// template name such as \c std::vector. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4776 | TemplateName |
| 4777 | ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
| 4778 | bool TemplateKeyword, |
| 4779 | TemplateDecl *Template) const { |
Douglas Gregor | 0f0ea2a | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 4780 | assert(NNS && "Missing nested-name-specifier in qualified template name"); |
| 4781 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4782 | // FIXME: Canonicalization? |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4783 | llvm::FoldingSetNodeID ID; |
| 4784 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 4785 | |
| 4786 | void *InsertPos = 0; |
| 4787 | QualifiedTemplateName *QTN = |
| 4788 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4789 | if (!QTN) { |
| 4790 | QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); |
| 4791 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 4792 | } |
| 4793 | |
| 4794 | return TemplateName(QTN); |
| 4795 | } |
| 4796 | |
| 4797 | /// \brief Retrieve the template name that represents a dependent |
| 4798 | /// template name such as \c MetaFun::template apply. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4799 | TemplateName |
| 4800 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
| 4801 | const IdentifierInfo *Name) const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4802 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 4803 | "Nested name specifier must be dependent"); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4804 | |
| 4805 | llvm::FoldingSetNodeID ID; |
| 4806 | DependentTemplateName::Profile(ID, NNS, Name); |
| 4807 | |
| 4808 | void *InsertPos = 0; |
| 4809 | DependentTemplateName *QTN = |
| 4810 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4811 | |
| 4812 | if (QTN) |
| 4813 | return TemplateName(QTN); |
| 4814 | |
| 4815 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 4816 | if (CanonNNS == NNS) { |
| 4817 | QTN = new (*this,4) DependentTemplateName(NNS, Name); |
| 4818 | } else { |
| 4819 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
| 4820 | QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4821 | DependentTemplateName *CheckQTN = |
| 4822 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4823 | assert(!CheckQTN && "Dependent type name canonicalization broken"); |
| 4824 | (void)CheckQTN; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4825 | } |
| 4826 | |
| 4827 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 4828 | return TemplateName(QTN); |
| 4829 | } |
| 4830 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4831 | /// \brief Retrieve the template name that represents a dependent |
| 4832 | /// template name such as \c MetaFun::template operator+. |
| 4833 | TemplateName |
| 4834 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4835 | OverloadedOperatorKind Operator) const { |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4836 | assert((!NNS || NNS->isDependent()) && |
| 4837 | "Nested name specifier must be dependent"); |
| 4838 | |
| 4839 | llvm::FoldingSetNodeID ID; |
| 4840 | DependentTemplateName::Profile(ID, NNS, Operator); |
| 4841 | |
| 4842 | void *InsertPos = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4843 | DependentTemplateName *QTN |
| 4844 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4845 | |
| 4846 | if (QTN) |
| 4847 | return TemplateName(QTN); |
| 4848 | |
| 4849 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 4850 | if (CanonNNS == NNS) { |
| 4851 | QTN = new (*this,4) DependentTemplateName(NNS, Operator); |
| 4852 | } else { |
| 4853 | TemplateName Canon = getDependentTemplateName(CanonNNS, Operator); |
| 4854 | QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4855 | |
| 4856 | DependentTemplateName *CheckQTN |
| 4857 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4858 | assert(!CheckQTN && "Dependent template name canonicalization broken"); |
| 4859 | (void)CheckQTN; |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4860 | } |
| 4861 | |
| 4862 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 4863 | return TemplateName(QTN); |
| 4864 | } |
| 4865 | |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 4866 | TemplateName |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 4867 | ASTContext::getSubstTemplateTemplateParm(TemplateTemplateParmDecl *param, |
| 4868 | TemplateName replacement) const { |
| 4869 | llvm::FoldingSetNodeID ID; |
| 4870 | SubstTemplateTemplateParmStorage::Profile(ID, param, replacement); |
| 4871 | |
| 4872 | void *insertPos = 0; |
| 4873 | SubstTemplateTemplateParmStorage *subst |
| 4874 | = SubstTemplateTemplateParms.FindNodeOrInsertPos(ID, insertPos); |
| 4875 | |
| 4876 | if (!subst) { |
| 4877 | subst = new (*this) SubstTemplateTemplateParmStorage(param, replacement); |
| 4878 | SubstTemplateTemplateParms.InsertNode(subst, insertPos); |
| 4879 | } |
| 4880 | |
| 4881 | return TemplateName(subst); |
| 4882 | } |
| 4883 | |
| 4884 | TemplateName |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 4885 | ASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param, |
| 4886 | const TemplateArgument &ArgPack) const { |
| 4887 | ASTContext &Self = const_cast<ASTContext &>(*this); |
| 4888 | llvm::FoldingSetNodeID ID; |
| 4889 | SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack); |
| 4890 | |
| 4891 | void *InsertPos = 0; |
| 4892 | SubstTemplateTemplateParmPackStorage *Subst |
| 4893 | = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos); |
| 4894 | |
| 4895 | if (!Subst) { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 4896 | Subst = new (*this) SubstTemplateTemplateParmPackStorage(Param, |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 4897 | ArgPack.pack_size(), |
| 4898 | ArgPack.pack_begin()); |
| 4899 | SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos); |
| 4900 | } |
| 4901 | |
| 4902 | return TemplateName(Subst); |
| 4903 | } |
| 4904 | |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4905 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | d934112 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 4906 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 4907 | /// is actually a value of type @c TargetInfo::IntType. |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 4908 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4909 | switch (Type) { |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 4910 | case TargetInfo::NoInt: return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4911 | case TargetInfo::SignedShort: return ShortTy; |
| 4912 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 4913 | case TargetInfo::SignedInt: return IntTy; |
| 4914 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 4915 | case TargetInfo::SignedLong: return LongTy; |
| 4916 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 4917 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 4918 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 4919 | } |
| 4920 | |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4921 | llvm_unreachable("Unhandled TargetInfo::IntType value"); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4922 | } |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 4923 | |
| 4924 | //===----------------------------------------------------------------------===// |
| 4925 | // Type Predicates. |
| 4926 | //===----------------------------------------------------------------------===// |
| 4927 | |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4928 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 4929 | /// garbage collection attribute. |
| 4930 | /// |
John McCall | ae278a3 | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 4931 | Qualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const { |
Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 4932 | if (getLangOptions().getGC() == LangOptions::NonGC) |
John McCall | ae278a3 | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 4933 | return Qualifiers::GCNone; |
| 4934 | |
| 4935 | assert(getLangOptions().ObjC1); |
| 4936 | Qualifiers::GC GCAttrs = Ty.getObjCGCAttr(); |
| 4937 | |
| 4938 | // Default behaviour under objective-C's gc is for ObjC pointers |
| 4939 | // (or pointers to them) be treated as though they were declared |
| 4940 | // as __strong. |
| 4941 | if (GCAttrs == Qualifiers::GCNone) { |
| 4942 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
| 4943 | return Qualifiers::Strong; |
| 4944 | else if (Ty->isPointerType()) |
| 4945 | return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); |
| 4946 | } else { |
| 4947 | // It's not valid to set GC attributes on anything that isn't a |
| 4948 | // pointer. |
| 4949 | #ifndef NDEBUG |
| 4950 | QualType CT = Ty->getCanonicalTypeInternal(); |
| 4951 | while (const ArrayType *AT = dyn_cast<ArrayType>(CT)) |
| 4952 | CT = AT->getElementType(); |
| 4953 | assert(CT->isAnyPointerType() || CT->isBlockPointerType()); |
| 4954 | #endif |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4955 | } |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 4956 | return GCAttrs; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4957 | } |
| 4958 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4959 | //===----------------------------------------------------------------------===// |
| 4960 | // Type Compatibility Testing |
| 4961 | //===----------------------------------------------------------------------===// |
Chris Lattner | 770951b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 4962 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4963 | /// areCompatVectorTypes - Return true if the two specified vector types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4964 | /// compatible. |
| 4965 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 4966 | const VectorType *RHS) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 4967 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4968 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4969 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4970 | } |
| 4971 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 4972 | bool ASTContext::areCompatibleVectorTypes(QualType FirstVec, |
| 4973 | QualType SecondVec) { |
| 4974 | assert(FirstVec->isVectorType() && "FirstVec should be a vector type"); |
| 4975 | assert(SecondVec->isVectorType() && "SecondVec should be a vector type"); |
| 4976 | |
| 4977 | if (hasSameUnqualifiedType(FirstVec, SecondVec)) |
| 4978 | return true; |
| 4979 | |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 4980 | // Treat Neon vector types and most AltiVec vector types as if they are the |
| 4981 | // equivalent GCC vector types. |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 4982 | const VectorType *First = FirstVec->getAs<VectorType>(); |
| 4983 | const VectorType *Second = SecondVec->getAs<VectorType>(); |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 4984 | if (First->getNumElements() == Second->getNumElements() && |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 4985 | hasSameType(First->getElementType(), Second->getElementType()) && |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 4986 | First->getVectorKind() != VectorType::AltiVecPixel && |
| 4987 | First->getVectorKind() != VectorType::AltiVecBool && |
| 4988 | Second->getVectorKind() != VectorType::AltiVecPixel && |
| 4989 | Second->getVectorKind() != VectorType::AltiVecBool) |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 4990 | return true; |
| 4991 | |
| 4992 | return false; |
| 4993 | } |
| 4994 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4995 | //===----------------------------------------------------------------------===// |
| 4996 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 4997 | //===----------------------------------------------------------------------===// |
| 4998 | |
| 4999 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 5000 | /// inheritance hierarchy of 'rProto'. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5001 | bool |
| 5002 | ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 5003 | ObjCProtocolDecl *rProto) const { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5004 | if (lProto == rProto) |
| 5005 | return true; |
| 5006 | for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), |
| 5007 | E = rProto->protocol_end(); PI != E; ++PI) |
| 5008 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 5009 | return true; |
| 5010 | return false; |
| 5011 | } |
| 5012 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5013 | /// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...> |
| 5014 | /// return true if lhs's protocols conform to rhs's protocol; false |
| 5015 | /// otherwise. |
| 5016 | bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) { |
| 5017 | if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType()) |
| 5018 | return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false); |
| 5019 | return false; |
| 5020 | } |
| 5021 | |
Fariborz Jahanian | a8f8dac | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 5022 | /// ObjCQualifiedClassTypesAreCompatible - compare Class<p,...> and |
| 5023 | /// Class<p1, ...>. |
| 5024 | bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs, |
| 5025 | QualType rhs) { |
| 5026 | const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>(); |
| 5027 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
| 5028 | assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible"); |
| 5029 | |
| 5030 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5031 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5032 | bool match = false; |
| 5033 | ObjCProtocolDecl *lhsProto = *I; |
| 5034 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 5035 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 5036 | ObjCProtocolDecl *rhsProto = *J; |
| 5037 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) { |
| 5038 | match = true; |
| 5039 | break; |
| 5040 | } |
| 5041 | } |
| 5042 | if (!match) |
| 5043 | return false; |
| 5044 | } |
| 5045 | return true; |
| 5046 | } |
| 5047 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5048 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 5049 | /// ObjCQualifiedIDType. |
| 5050 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 5051 | bool compare) { |
| 5052 | // Allow id<P..> and an 'id' or void* type in all cases. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5053 | if (lhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5054 | lhs->isObjCIdType() || lhs->isObjCClassType()) |
| 5055 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5056 | else if (rhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5057 | rhs->isObjCIdType() || rhs->isObjCClassType()) |
| 5058 | return true; |
| 5059 | |
| 5060 | if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5061 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5062 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5063 | if (!rhsOPT) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5064 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5065 | if (rhsOPT->qual_empty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5066 | // If the RHS is a unqualified interface pointer "NSString*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5067 | // make sure we check the class hierarchy. |
| 5068 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 5069 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5070 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5071 | // when comparing an id<P> on lhs with a static type on rhs, |
| 5072 | // see if static class implements all of id's protocols, directly or |
| 5073 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 5074 | if (!rhsID->ClassImplementsProtocol(*I, true)) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5075 | return false; |
| 5076 | } |
| 5077 | } |
| 5078 | // If there are no qualifiers and no interface, we have an 'id'. |
| 5079 | return true; |
| 5080 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5081 | // Both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5082 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5083 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5084 | ObjCProtocolDecl *lhsProto = *I; |
| 5085 | bool match = false; |
| 5086 | |
| 5087 | // when comparing an id<P> on lhs with a static type on rhs, |
| 5088 | // see if static class implements all of id's protocols, directly or |
| 5089 | // through its super class and categories. |
| 5090 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 5091 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 5092 | ObjCProtocolDecl *rhsProto = *J; |
| 5093 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 5094 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 5095 | match = true; |
| 5096 | break; |
| 5097 | } |
| 5098 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5099 | // If the RHS is a qualified interface pointer "NSString<P>*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5100 | // make sure we check the class hierarchy. |
| 5101 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 5102 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5103 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5104 | // when comparing an id<P> on lhs with a static type on rhs, |
| 5105 | // see if static class implements all of id's protocols, directly or |
| 5106 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 5107 | if (rhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5108 | match = true; |
| 5109 | break; |
| 5110 | } |
| 5111 | } |
| 5112 | } |
| 5113 | if (!match) |
| 5114 | return false; |
| 5115 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5116 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5117 | return true; |
| 5118 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5119 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5120 | const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 5121 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 5122 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5123 | if (const ObjCObjectPointerType *lhsOPT = |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5124 | lhs->getAsObjCInterfacePointerType()) { |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5125 | // If both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5126 | for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(), |
| 5127 | E = lhsOPT->qual_end(); I != E; ++I) { |
| 5128 | ObjCProtocolDecl *lhsProto = *I; |
| 5129 | bool match = false; |
| 5130 | |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5131 | // when comparing an id<P> on rhs with a static type on lhs, |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5132 | // see if static class implements all of id's protocols, directly or |
| 5133 | // through its super class and categories. |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5134 | // First, lhs protocols in the qualifier list must be found, direct |
| 5135 | // or indirect in rhs's qualifier list or it is a mismatch. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5136 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 5137 | E = rhsQID->qual_end(); J != E; ++J) { |
| 5138 | ObjCProtocolDecl *rhsProto = *J; |
| 5139 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 5140 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 5141 | match = true; |
| 5142 | break; |
| 5143 | } |
| 5144 | } |
| 5145 | if (!match) |
| 5146 | return false; |
| 5147 | } |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5148 | |
| 5149 | // Static class's protocols, or its super class or category protocols |
| 5150 | // must be found, direct or indirect in rhs's qualifier list or it is a mismatch. |
| 5151 | if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { |
| 5152 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
| 5153 | CollectInheritedProtocols(lhsID, LHSInheritedProtocols); |
| 5154 | // This is rather dubious but matches gcc's behavior. If lhs has |
| 5155 | // no type qualifier and its class has no static protocol(s) |
| 5156 | // assume that it is mismatch. |
| 5157 | if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty()) |
| 5158 | return false; |
| 5159 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 5160 | LHSInheritedProtocols.begin(), |
| 5161 | E = LHSInheritedProtocols.end(); I != E; ++I) { |
| 5162 | bool match = false; |
| 5163 | ObjCProtocolDecl *lhsProto = (*I); |
| 5164 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 5165 | E = rhsQID->qual_end(); J != E; ++J) { |
| 5166 | ObjCProtocolDecl *rhsProto = *J; |
| 5167 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 5168 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 5169 | match = true; |
| 5170 | break; |
| 5171 | } |
| 5172 | } |
| 5173 | if (!match) |
| 5174 | return false; |
| 5175 | } |
| 5176 | } |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5177 | return true; |
| 5178 | } |
| 5179 | return false; |
| 5180 | } |
| 5181 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5182 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5183 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 5184 | /// protocol qualifiers on the LHS or RHS. |
| 5185 | /// |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5186 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 5187 | const ObjCObjectPointerType *RHSOPT) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5188 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 5189 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 5190 | |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 5191 | // If either type represents the built-in 'id' or 'Class' types, return true. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5192 | if (LHS->isObjCUnqualifiedIdOrClass() || |
| 5193 | RHS->isObjCUnqualifiedIdOrClass()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5194 | return true; |
| 5195 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5196 | if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5197 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 5198 | QualType(RHSOPT,0), |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5199 | false); |
Fariborz Jahanian | a8f8dac | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 5200 | |
| 5201 | if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass()) |
| 5202 | return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0), |
| 5203 | QualType(RHSOPT,0)); |
| 5204 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5205 | // If we have 2 user-defined types, fall into that path. |
| 5206 | if (LHS->getInterface() && RHS->getInterface()) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5207 | return canAssignObjCInterfaces(LHS, RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5208 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5209 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5210 | } |
| 5211 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5212 | /// canAssignObjCInterfacesInBlockPointer - This routine is specifically written |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 5213 | /// for providing type-safety for objective-c pointers used to pass/return |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5214 | /// arguments in block literals. When passed as arguments, passing 'A*' where |
| 5215 | /// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is |
| 5216 | /// not OK. For the return type, the opposite is not OK. |
| 5217 | bool ASTContext::canAssignObjCInterfacesInBlockPointer( |
| 5218 | const ObjCObjectPointerType *LHSOPT, |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5219 | const ObjCObjectPointerType *RHSOPT, |
| 5220 | bool BlockReturnType) { |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 5221 | if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5222 | return true; |
| 5223 | |
| 5224 | if (LHSOPT->isObjCBuiltinType()) { |
| 5225 | return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType(); |
| 5226 | } |
| 5227 | |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 5228 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5229 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 5230 | QualType(RHSOPT,0), |
| 5231 | false); |
| 5232 | |
| 5233 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 5234 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 5235 | if (LHS && RHS) { // We have 2 user-defined types. |
| 5236 | if (LHS != RHS) { |
| 5237 | if (LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5238 | return BlockReturnType; |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5239 | if (RHS->getDecl()->isSuperClassOf(LHS->getDecl())) |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5240 | return !BlockReturnType; |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5241 | } |
| 5242 | else |
| 5243 | return true; |
| 5244 | } |
| 5245 | return false; |
| 5246 | } |
| 5247 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5248 | /// getIntersectionOfProtocols - This routine finds the intersection of set |
| 5249 | /// of protocols inherited from two distinct objective-c pointer objects. |
| 5250 | /// It is used to build composite qualifier list of the composite type of |
| 5251 | /// the conditional expression involving two objective-c pointer objects. |
| 5252 | static |
| 5253 | void getIntersectionOfProtocols(ASTContext &Context, |
| 5254 | const ObjCObjectPointerType *LHSOPT, |
| 5255 | const ObjCObjectPointerType *RHSOPT, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5256 | SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5257 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5258 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 5259 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 5260 | assert(LHS->getInterface() && "LHS must have an interface base"); |
| 5261 | assert(RHS->getInterface() && "RHS must have an interface base"); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5262 | |
| 5263 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet; |
| 5264 | unsigned LHSNumProtocols = LHS->getNumProtocols(); |
| 5265 | if (LHSNumProtocols > 0) |
| 5266 | InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end()); |
| 5267 | else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 5268 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5269 | Context.CollectInheritedProtocols(LHS->getInterface(), |
| 5270 | LHSInheritedProtocols); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5271 | InheritedProtocolSet.insert(LHSInheritedProtocols.begin(), |
| 5272 | LHSInheritedProtocols.end()); |
| 5273 | } |
| 5274 | |
| 5275 | unsigned RHSNumProtocols = RHS->getNumProtocols(); |
| 5276 | if (RHSNumProtocols > 0) { |
Dan Gohman | cb421fa | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 5277 | ObjCProtocolDecl **RHSProtocols = |
| 5278 | const_cast<ObjCProtocolDecl **>(RHS->qual_begin()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5279 | for (unsigned i = 0; i < RHSNumProtocols; ++i) |
| 5280 | if (InheritedProtocolSet.count(RHSProtocols[i])) |
| 5281 | IntersectionOfProtocols.push_back(RHSProtocols[i]); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 5282 | } else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 5283 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5284 | Context.CollectInheritedProtocols(RHS->getInterface(), |
| 5285 | RHSInheritedProtocols); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 5286 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 5287 | RHSInheritedProtocols.begin(), |
| 5288 | E = RHSInheritedProtocols.end(); I != E; ++I) |
| 5289 | if (InheritedProtocolSet.count((*I))) |
| 5290 | IntersectionOfProtocols.push_back((*I)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5291 | } |
| 5292 | } |
| 5293 | |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 5294 | /// areCommonBaseCompatible - Returns common base class of the two classes if |
| 5295 | /// one found. Note that this is O'2 algorithm. But it will be called as the |
| 5296 | /// last type comparison in a ?-exp of ObjC pointer types before a |
| 5297 | /// warning is issued. So, its invokation is extremely rare. |
| 5298 | QualType ASTContext::areCommonBaseCompatible( |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5299 | const ObjCObjectPointerType *Lptr, |
| 5300 | const ObjCObjectPointerType *Rptr) { |
| 5301 | const ObjCObjectType *LHS = Lptr->getObjectType(); |
| 5302 | const ObjCObjectType *RHS = Rptr->getObjectType(); |
| 5303 | const ObjCInterfaceDecl* LDecl = LHS->getInterface(); |
| 5304 | const ObjCInterfaceDecl* RDecl = RHS->getInterface(); |
Fariborz Jahanian | 7c2bdcb | 2011-04-18 21:16:59 +0000 | [diff] [blame] | 5305 | if (!LDecl || !RDecl || (LDecl == RDecl)) |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 5306 | return QualType(); |
| 5307 | |
Fariborz Jahanian | 7c2bdcb | 2011-04-18 21:16:59 +0000 | [diff] [blame] | 5308 | do { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5309 | LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5310 | if (canAssignObjCInterfaces(LHS, RHS)) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5311 | SmallVector<ObjCProtocolDecl *, 8> Protocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5312 | getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols); |
| 5313 | |
| 5314 | QualType Result = QualType(LHS, 0); |
| 5315 | if (!Protocols.empty()) |
| 5316 | Result = getObjCObjectType(Result, Protocols.data(), Protocols.size()); |
| 5317 | Result = getObjCObjectPointerType(Result); |
| 5318 | return Result; |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5319 | } |
Fariborz Jahanian | 7c2bdcb | 2011-04-18 21:16:59 +0000 | [diff] [blame] | 5320 | } while ((LDecl = LDecl->getSuperClass())); |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 5321 | |
| 5322 | return QualType(); |
| 5323 | } |
| 5324 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5325 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS, |
| 5326 | const ObjCObjectType *RHS) { |
| 5327 | assert(LHS->getInterface() && "LHS is not an interface type"); |
| 5328 | assert(RHS->getInterface() && "RHS is not an interface type"); |
| 5329 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5330 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 5331 | // the LHS. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5332 | if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface())) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5333 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5334 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5335 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 5336 | // protocol qualified at all, then we are good. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 5337 | if (LHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5338 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5339 | |
Fariborz Jahanian | b7bc34a | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 5340 | // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, |
| 5341 | // more detailed analysis is required. |
| 5342 | if (RHS->getNumProtocols() == 0) { |
| 5343 | // OK, if LHS is a superclass of RHS *and* |
| 5344 | // this superclass is assignment compatible with LHS. |
| 5345 | // false otherwise. |
Fariborz Jahanian | 627788c | 2011-04-12 16:34:14 +0000 | [diff] [blame] | 5346 | bool IsSuperClass = |
| 5347 | LHS->getInterface()->isSuperClassOf(RHS->getInterface()); |
| 5348 | if (IsSuperClass) { |
Fariborz Jahanian | b7bc34a | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 5349 | // OK if conversion of LHS to SuperClass results in narrowing of types |
| 5350 | // ; i.e., SuperClass may implement at least one of the protocols |
| 5351 | // in LHS's protocol list. Example, SuperObj<P1> = lhs<P1,P2> is ok. |
| 5352 | // But not SuperObj<P1,P2,P3> = lhs<P1,P2>. |
| 5353 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> SuperClassInheritedProtocols; |
Fariborz Jahanian | 627788c | 2011-04-12 16:34:14 +0000 | [diff] [blame] | 5354 | CollectInheritedProtocols(RHS->getInterface(), SuperClassInheritedProtocols); |
Fariborz Jahanian | b7bc34a | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 5355 | // If super class has no protocols, it is not a match. |
| 5356 | if (SuperClassInheritedProtocols.empty()) |
| 5357 | return false; |
| 5358 | |
| 5359 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 5360 | LHSPE = LHS->qual_end(); |
| 5361 | LHSPI != LHSPE; LHSPI++) { |
| 5362 | bool SuperImplementsProtocol = false; |
| 5363 | ObjCProtocolDecl *LHSProto = (*LHSPI); |
| 5364 | |
| 5365 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 5366 | SuperClassInheritedProtocols.begin(), |
| 5367 | E = SuperClassInheritedProtocols.end(); I != E; ++I) { |
| 5368 | ObjCProtocolDecl *SuperClassProto = (*I); |
| 5369 | if (SuperClassProto->lookupProtocolNamed(LHSProto->getIdentifier())) { |
| 5370 | SuperImplementsProtocol = true; |
| 5371 | break; |
| 5372 | } |
| 5373 | } |
| 5374 | if (!SuperImplementsProtocol) |
| 5375 | return false; |
| 5376 | } |
| 5377 | return true; |
| 5378 | } |
| 5379 | return false; |
| 5380 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5381 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5382 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 5383 | LHSPE = LHS->qual_end(); |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 5384 | LHSPI != LHSPE; LHSPI++) { |
| 5385 | bool RHSImplementsProtocol = false; |
| 5386 | |
| 5387 | // If the RHS doesn't implement the protocol on the left, the types |
| 5388 | // are incompatible. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5389 | for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(), |
| 5390 | RHSPE = RHS->qual_end(); |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 5391 | RHSPI != RHSPE; RHSPI++) { |
| 5392 | if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) { |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 5393 | RHSImplementsProtocol = true; |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 5394 | break; |
| 5395 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 5396 | } |
| 5397 | // FIXME: For better diagnostics, consider passing back the protocol name. |
| 5398 | if (!RHSImplementsProtocol) |
| 5399 | return false; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5400 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 5401 | // The RHS implements all protocols listed on the LHS. |
| 5402 | return true; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5403 | } |
| 5404 | |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 5405 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 5406 | // get the "pointed to" types |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5407 | const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
| 5408 | const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5409 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5410 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 5411 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5412 | |
| 5413 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 5414 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 5415 | } |
| 5416 | |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 5417 | bool ASTContext::canBindObjCObjectType(QualType To, QualType From) { |
| 5418 | return canAssignObjCInterfaces( |
| 5419 | getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(), |
| 5420 | getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>()); |
| 5421 | } |
| 5422 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5423 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5424 | /// both shall have the identically qualified version of a compatible type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5425 | /// C99 6.2.7p1: Two types have compatible types if their types are the |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5426 | /// same. See 6.7.[2,3,5] for additional rules. |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5427 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS, |
| 5428 | bool CompareUnqualified) { |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 5429 | if (getLangOptions().CPlusPlus) |
| 5430 | return hasSameType(LHS, RHS); |
| 5431 | |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5432 | return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5433 | } |
| 5434 | |
Fariborz Jahanian | c286f38 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 5435 | bool ASTContext::propertyTypesAreCompatible(QualType LHS, QualType RHS) { |
Fariborz Jahanian | 8237839 | 2011-07-12 23:20:13 +0000 | [diff] [blame] | 5436 | return typesAreCompatible(LHS, RHS); |
Fariborz Jahanian | c286f38 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 5437 | } |
| 5438 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5439 | bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) { |
| 5440 | return !mergeTypes(LHS, RHS, true).isNull(); |
| 5441 | } |
| 5442 | |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 5443 | /// mergeTransparentUnionType - if T is a transparent union type and a member |
| 5444 | /// of T is compatible with SubType, return the merged type, else return |
| 5445 | /// QualType() |
| 5446 | QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType, |
| 5447 | bool OfBlockPointer, |
| 5448 | bool Unqualified) { |
| 5449 | if (const RecordType *UT = T->getAsUnionType()) { |
| 5450 | RecordDecl *UD = UT->getDecl(); |
| 5451 | if (UD->hasAttr<TransparentUnionAttr>()) { |
| 5452 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 5453 | itend = UD->field_end(); it != itend; ++it) { |
Peter Collingbourne | f91d757 | 2010-12-02 21:00:06 +0000 | [diff] [blame] | 5454 | QualType ET = it->getType().getUnqualifiedType(); |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 5455 | QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified); |
| 5456 | if (!MT.isNull()) |
| 5457 | return MT; |
| 5458 | } |
| 5459 | } |
| 5460 | } |
| 5461 | |
| 5462 | return QualType(); |
| 5463 | } |
| 5464 | |
| 5465 | /// mergeFunctionArgumentTypes - merge two types which appear as function |
| 5466 | /// argument types |
| 5467 | QualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs, |
| 5468 | bool OfBlockPointer, |
| 5469 | bool Unqualified) { |
| 5470 | // GNU extension: two types are compatible if they appear as a function |
| 5471 | // argument, one of the types is a transparent union type and the other |
| 5472 | // type is compatible with a union member |
| 5473 | QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer, |
| 5474 | Unqualified); |
| 5475 | if (!lmerge.isNull()) |
| 5476 | return lmerge; |
| 5477 | |
| 5478 | QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer, |
| 5479 | Unqualified); |
| 5480 | if (!rmerge.isNull()) |
| 5481 | return rmerge; |
| 5482 | |
| 5483 | return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified); |
| 5484 | } |
| 5485 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5486 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs, |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5487 | bool OfBlockPointer, |
| 5488 | bool Unqualified) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5489 | const FunctionType *lbase = lhs->getAs<FunctionType>(); |
| 5490 | const FunctionType *rbase = rhs->getAs<FunctionType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5491 | const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 5492 | const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5493 | bool allLTypes = true; |
| 5494 | bool allRTypes = true; |
| 5495 | |
| 5496 | // Check return type |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5497 | QualType retType; |
Fariborz Jahanian | d263fd1 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 5498 | if (OfBlockPointer) { |
| 5499 | QualType RHS = rbase->getResultType(); |
| 5500 | QualType LHS = lbase->getResultType(); |
| 5501 | bool UnqualifiedResult = Unqualified; |
| 5502 | if (!UnqualifiedResult) |
| 5503 | UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers()); |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5504 | retType = mergeTypes(LHS, RHS, true, UnqualifiedResult, true); |
Fariborz Jahanian | d263fd1 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 5505 | } |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5506 | else |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5507 | retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), false, |
| 5508 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5509 | if (retType.isNull()) return QualType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5510 | |
| 5511 | if (Unqualified) |
| 5512 | retType = retType.getUnqualifiedType(); |
| 5513 | |
| 5514 | CanQualType LRetType = getCanonicalType(lbase->getResultType()); |
| 5515 | CanQualType RRetType = getCanonicalType(rbase->getResultType()); |
| 5516 | if (Unqualified) { |
| 5517 | LRetType = LRetType.getUnqualifiedType(); |
| 5518 | RRetType = RRetType.getUnqualifiedType(); |
| 5519 | } |
| 5520 | |
| 5521 | if (getCanonicalType(retType) != LRetType) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5522 | allLTypes = false; |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5523 | if (getCanonicalType(retType) != RRetType) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5524 | allRTypes = false; |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5525 | |
Daniel Dunbar | 6a15c85 | 2010-04-28 16:20:58 +0000 | [diff] [blame] | 5526 | // FIXME: double check this |
| 5527 | // FIXME: should we error if lbase->getRegParmAttr() != 0 && |
| 5528 | // rbase->getRegParmAttr() != 0 && |
| 5529 | // lbase->getRegParmAttr() != rbase->getRegParmAttr()? |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 5530 | FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo(); |
| 5531 | FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo(); |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5532 | |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 5533 | // Compatible functions must have compatible calling conventions |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5534 | if (!isSameCallConv(lbaseInfo.getCC(), rbaseInfo.getCC())) |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 5535 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5536 | |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5537 | // Regparm is part of the calling convention. |
Eli Friedman | a49218e | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 5538 | if (lbaseInfo.getHasRegParm() != rbaseInfo.getHasRegParm()) |
| 5539 | return QualType(); |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5540 | if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm()) |
| 5541 | return QualType(); |
| 5542 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5543 | if (lbaseInfo.getProducesResult() != rbaseInfo.getProducesResult()) |
| 5544 | return QualType(); |
| 5545 | |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5546 | // It's noreturn if either type is. |
| 5547 | // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'. |
| 5548 | bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn(); |
| 5549 | if (NoReturn != lbaseInfo.getNoReturn()) |
| 5550 | allLTypes = false; |
| 5551 | if (NoReturn != rbaseInfo.getNoReturn()) |
| 5552 | allRTypes = false; |
| 5553 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5554 | FunctionType::ExtInfo einfo = lbaseInfo.withNoReturn(NoReturn); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5555 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5556 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 5557 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 5558 | "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5559 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 5560 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 5561 | |
| 5562 | // Compatible functions must have the same number of arguments |
| 5563 | if (lproto_nargs != rproto_nargs) |
| 5564 | return QualType(); |
| 5565 | |
| 5566 | // Variadic and non-variadic functions aren't compatible |
| 5567 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 5568 | return QualType(); |
| 5569 | |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 5570 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 5571 | return QualType(); |
| 5572 | |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 5573 | if (LangOpts.ObjCAutoRefCount && |
| 5574 | !FunctionTypesMatchOnNSConsumedAttrs(rproto, lproto)) |
| 5575 | return QualType(); |
| 5576 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5577 | // Check argument compatibility |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5578 | SmallVector<QualType, 10> types; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5579 | for (unsigned i = 0; i < lproto_nargs; i++) { |
| 5580 | QualType largtype = lproto->getArgType(i).getUnqualifiedType(); |
| 5581 | QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 5582 | QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype, |
| 5583 | OfBlockPointer, |
| 5584 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5585 | if (argtype.isNull()) return QualType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5586 | |
| 5587 | if (Unqualified) |
| 5588 | argtype = argtype.getUnqualifiedType(); |
| 5589 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5590 | types.push_back(argtype); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5591 | if (Unqualified) { |
| 5592 | largtype = largtype.getUnqualifiedType(); |
| 5593 | rargtype = rargtype.getUnqualifiedType(); |
| 5594 | } |
| 5595 | |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5596 | if (getCanonicalType(argtype) != getCanonicalType(largtype)) |
| 5597 | allLTypes = false; |
| 5598 | if (getCanonicalType(argtype) != getCanonicalType(rargtype)) |
| 5599 | allRTypes = false; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5600 | } |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 5601 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5602 | if (allLTypes) return lhs; |
| 5603 | if (allRTypes) return rhs; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5604 | |
| 5605 | FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo(); |
| 5606 | EPI.ExtInfo = einfo; |
| 5607 | return getFunctionType(retType, types.begin(), types.size(), EPI); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5608 | } |
| 5609 | |
| 5610 | if (lproto) allRTypes = false; |
| 5611 | if (rproto) allLTypes = false; |
| 5612 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5613 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5614 | if (proto) { |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 5615 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5616 | if (proto->isVariadic()) return QualType(); |
| 5617 | // Check that the types are compatible with the types that |
| 5618 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 5619 | // The only types actually affected are promotable integer |
| 5620 | // types and floats, which would be passed as a different |
| 5621 | // type depending on whether the prototype is visible. |
| 5622 | unsigned proto_nargs = proto->getNumArgs(); |
| 5623 | for (unsigned i = 0; i < proto_nargs; ++i) { |
| 5624 | QualType argTy = proto->getArgType(i); |
Douglas Gregor | b0f8eac | 2010-02-03 19:27:29 +0000 | [diff] [blame] | 5625 | |
| 5626 | // Look at the promotion type of enum types, since that is the type used |
| 5627 | // to pass enum values. |
| 5628 | if (const EnumType *Enum = argTy->getAs<EnumType>()) |
| 5629 | argTy = Enum->getDecl()->getPromotionType(); |
| 5630 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5631 | if (argTy->isPromotableIntegerType() || |
| 5632 | getCanonicalType(argTy).getUnqualifiedType() == FloatTy) |
| 5633 | return QualType(); |
| 5634 | } |
| 5635 | |
| 5636 | if (allLTypes) return lhs; |
| 5637 | if (allRTypes) return rhs; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5638 | |
| 5639 | FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo(); |
| 5640 | EPI.ExtInfo = einfo; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5641 | return getFunctionType(retType, proto->arg_type_begin(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5642 | proto->getNumArgs(), EPI); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5643 | } |
| 5644 | |
| 5645 | if (allLTypes) return lhs; |
| 5646 | if (allRTypes) return rhs; |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5647 | return getFunctionNoProtoType(retType, einfo); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5648 | } |
| 5649 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5650 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5651 | bool OfBlockPointer, |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5652 | bool Unqualified, bool BlockReturnType) { |
Bill Wendling | 43d6975 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 5653 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 5654 | // type is adjusted to "T" prior to any further analysis, the expression |
| 5655 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 5656 | // expression is an lvalue unless the reference is an rvalue reference and |
| 5657 | // the expression is a function call (possibly inside parentheses). |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 5658 | assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?"); |
| 5659 | assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?"); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5660 | |
| 5661 | if (Unqualified) { |
| 5662 | LHS = LHS.getUnqualifiedType(); |
| 5663 | RHS = RHS.getUnqualifiedType(); |
| 5664 | } |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 5665 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5666 | QualType LHSCan = getCanonicalType(LHS), |
| 5667 | RHSCan = getCanonicalType(RHS); |
| 5668 | |
| 5669 | // If two types are identical, they are compatible. |
| 5670 | if (LHSCan == RHSCan) |
| 5671 | return LHS; |
| 5672 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5673 | // If the qualifiers are different, the types aren't compatible... mostly. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 5674 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 5675 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5676 | if (LQuals != RQuals) { |
| 5677 | // If any of these qualifiers are different, we have a type |
| 5678 | // mismatch. |
| 5679 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5680 | LQuals.getAddressSpace() != RQuals.getAddressSpace() || |
| 5681 | LQuals.getObjCLifetime() != RQuals.getObjCLifetime()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5682 | return QualType(); |
| 5683 | |
| 5684 | // Exactly one GC qualifier difference is allowed: __strong is |
| 5685 | // okay if the other type has no GC qualifier but is an Objective |
| 5686 | // C object pointer (i.e. implicitly strong by default). We fix |
| 5687 | // this by pretending that the unqualified type was actually |
| 5688 | // qualified __strong. |
| 5689 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 5690 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 5691 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 5692 | |
| 5693 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 5694 | return QualType(); |
| 5695 | |
| 5696 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
| 5697 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); |
| 5698 | } |
| 5699 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
| 5700 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); |
| 5701 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5702 | return QualType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5703 | } |
| 5704 | |
| 5705 | // Okay, qualifiers are equal. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5706 | |
Eli Friedman | 852d63b | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 5707 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 5708 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5709 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5710 | // We want to consider the two function types to be the same for these |
| 5711 | // comparisons, just force one to the other. |
| 5712 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 5713 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 4c721d3 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 5714 | |
| 5715 | // Same as above for arrays |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 5716 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 5717 | LHSClass = Type::ConstantArray; |
| 5718 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 5719 | RHSClass = Type::ConstantArray; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5720 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5721 | // ObjCInterfaces are just specialized ObjCObjects. |
| 5722 | if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject; |
| 5723 | if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject; |
| 5724 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 5725 | // Canonicalize ExtVector -> Vector. |
| 5726 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 5727 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5728 | |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 5729 | // If the canonical type classes don't match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5730 | if (LHSClass != RHSClass) { |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5731 | // C99 6.7.2.2p4: Each enumerated type shall be compatible with char, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5732 | // a signed integer type, or an unsigned integer type. |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 5733 | // Compatibility is based on the underlying type, not the promotion |
| 5734 | // type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5735 | if (const EnumType* ETy = LHS->getAs<EnumType>()) { |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5736 | if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType()) |
| 5737 | return RHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 5738 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5739 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5740 | if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType()) |
| 5741 | return LHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 5742 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5743 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5744 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5745 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5746 | |
Steve Naroff | 4a74678 | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 5747 | // The canonical type classes match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5748 | switch (LHSClass) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5749 | #define TYPE(Class, Base) |
| 5750 | #define ABSTRACT_TYPE(Class, Base) |
John McCall | ad5e738 | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 5751 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5752 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 5753 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 5754 | #include "clang/AST/TypeNodes.def" |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5755 | llvm_unreachable("Non-canonical and dependent types shouldn't get here"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5756 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 5757 | case Type::LValueReference: |
| 5758 | case Type::RValueReference: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5759 | case Type::MemberPointer: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5760 | llvm_unreachable("C++ should never be in mergeTypes"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5761 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5762 | case Type::ObjCInterface: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5763 | case Type::IncompleteArray: |
| 5764 | case Type::VariableArray: |
| 5765 | case Type::FunctionProto: |
| 5766 | case Type::ExtVector: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5767 | llvm_unreachable("Types are eliminated above"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5768 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5769 | case Type::Pointer: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5770 | { |
| 5771 | // Merge two pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5772 | QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); |
| 5773 | QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5774 | if (Unqualified) { |
| 5775 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 5776 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 5777 | } |
| 5778 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false, |
| 5779 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5780 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 5781 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5782 | return LHS; |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 5783 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5784 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5785 | return getPointerType(ResultType); |
| 5786 | } |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 5787 | case Type::BlockPointer: |
| 5788 | { |
| 5789 | // Merge two block pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5790 | QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); |
| 5791 | QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5792 | if (Unqualified) { |
| 5793 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 5794 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 5795 | } |
| 5796 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer, |
| 5797 | Unqualified); |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 5798 | if (ResultType.isNull()) return QualType(); |
| 5799 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 5800 | return LHS; |
| 5801 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 5802 | return RHS; |
| 5803 | return getBlockPointerType(ResultType); |
| 5804 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5805 | case Type::ConstantArray: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5806 | { |
| 5807 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 5808 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 5809 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 5810 | return QualType(); |
| 5811 | |
| 5812 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 5813 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5814 | if (Unqualified) { |
| 5815 | LHSElem = LHSElem.getUnqualifiedType(); |
| 5816 | RHSElem = RHSElem.getUnqualifiedType(); |
| 5817 | } |
| 5818 | |
| 5819 | QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5820 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5821 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 5822 | return LHS; |
| 5823 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 5824 | return RHS; |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 5825 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 5826 | ArrayType::ArraySizeModifier(), 0); |
| 5827 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 5828 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5829 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 5830 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5831 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 5832 | return LHS; |
| 5833 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 5834 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5835 | if (LVAT) { |
| 5836 | // FIXME: This isn't correct! But tricky to implement because |
| 5837 | // the array's size has to be the size of LHS, but the type |
| 5838 | // has to be different. |
| 5839 | return LHS; |
| 5840 | } |
| 5841 | if (RVAT) { |
| 5842 | // FIXME: This isn't correct! But tricky to implement because |
| 5843 | // the array's size has to be the size of RHS, but the type |
| 5844 | // has to be different. |
| 5845 | return RHS; |
| 5846 | } |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 5847 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 5848 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 5849 | return getIncompleteArrayType(ResultType, |
| 5850 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5851 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5852 | case Type::FunctionNoProto: |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5853 | return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5854 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5855 | case Type::Enum: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5856 | return QualType(); |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5857 | case Type::Builtin: |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 5858 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5859 | return QualType(); |
Daniel Dunbar | 64cfdb7 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 5860 | case Type::Complex: |
| 5861 | // Distinct complex types are incompatible. |
| 5862 | return QualType(); |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 5863 | case Type::Vector: |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 5864 | // FIXME: The merged type should be an ExtVector! |
John McCall | 1c471f3 | 2010-03-12 23:14:13 +0000 | [diff] [blame] | 5865 | if (areCompatVectorTypes(LHSCan->getAs<VectorType>(), |
| 5866 | RHSCan->getAs<VectorType>())) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5867 | return LHS; |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5868 | return QualType(); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5869 | case Type::ObjCObject: { |
| 5870 | // Check if the types are assignment compatible. |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 5871 | // FIXME: This should be type compatibility, e.g. whether |
| 5872 | // "LHS x; RHS x;" at global scope is legal. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5873 | const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>(); |
| 5874 | const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>(); |
| 5875 | if (canAssignObjCInterfaces(LHSIface, RHSIface)) |
Steve Naroff | 5fd659d | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 5876 | return LHS; |
| 5877 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5878 | return QualType(); |
Cedric Venet | 61490e9 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 5879 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5880 | case Type::ObjCObjectPointer: { |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5881 | if (OfBlockPointer) { |
| 5882 | if (canAssignObjCInterfacesInBlockPointer( |
| 5883 | LHS->getAs<ObjCObjectPointerType>(), |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5884 | RHS->getAs<ObjCObjectPointerType>(), |
| 5885 | BlockReturnType)) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5886 | return LHS; |
| 5887 | return QualType(); |
| 5888 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5889 | if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(), |
| 5890 | RHS->getAs<ObjCObjectPointerType>())) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5891 | return LHS; |
| 5892 | |
Steve Naroff | bc76dd0 | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 5893 | return QualType(); |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5894 | } |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5895 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5896 | |
| 5897 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5898 | } |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 5899 | |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 5900 | bool ASTContext::FunctionTypesMatchOnNSConsumedAttrs( |
| 5901 | const FunctionProtoType *FromFunctionType, |
| 5902 | const FunctionProtoType *ToFunctionType) { |
| 5903 | if (FromFunctionType->hasAnyConsumedArgs() != |
| 5904 | ToFunctionType->hasAnyConsumedArgs()) |
| 5905 | return false; |
| 5906 | FunctionProtoType::ExtProtoInfo FromEPI = |
| 5907 | FromFunctionType->getExtProtoInfo(); |
| 5908 | FunctionProtoType::ExtProtoInfo ToEPI = |
| 5909 | ToFunctionType->getExtProtoInfo(); |
| 5910 | if (FromEPI.ConsumedArguments && ToEPI.ConsumedArguments) |
| 5911 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 5912 | ArgIdx != NumArgs; ++ArgIdx) { |
| 5913 | if (FromEPI.ConsumedArguments[ArgIdx] != |
| 5914 | ToEPI.ConsumedArguments[ArgIdx]) |
| 5915 | return false; |
| 5916 | } |
| 5917 | return true; |
| 5918 | } |
| 5919 | |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 5920 | /// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and |
| 5921 | /// 'RHS' attributes and returns the merged version; including for function |
| 5922 | /// return types. |
| 5923 | QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) { |
| 5924 | QualType LHSCan = getCanonicalType(LHS), |
| 5925 | RHSCan = getCanonicalType(RHS); |
| 5926 | // If two types are identical, they are compatible. |
| 5927 | if (LHSCan == RHSCan) |
| 5928 | return LHS; |
| 5929 | if (RHSCan->isFunctionType()) { |
| 5930 | if (!LHSCan->isFunctionType()) |
| 5931 | return QualType(); |
| 5932 | QualType OldReturnType = |
| 5933 | cast<FunctionType>(RHSCan.getTypePtr())->getResultType(); |
| 5934 | QualType NewReturnType = |
| 5935 | cast<FunctionType>(LHSCan.getTypePtr())->getResultType(); |
| 5936 | QualType ResReturnType = |
| 5937 | mergeObjCGCQualifiers(NewReturnType, OldReturnType); |
| 5938 | if (ResReturnType.isNull()) |
| 5939 | return QualType(); |
| 5940 | if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) { |
| 5941 | // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo(); |
| 5942 | // In either case, use OldReturnType to build the new function type. |
| 5943 | const FunctionType *F = LHS->getAs<FunctionType>(); |
| 5944 | if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5945 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 5946 | EPI.ExtInfo = getFunctionExtInfo(LHS); |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 5947 | QualType ResultType |
| 5948 | = getFunctionType(OldReturnType, FPT->arg_type_begin(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5949 | FPT->getNumArgs(), EPI); |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 5950 | return ResultType; |
| 5951 | } |
| 5952 | } |
| 5953 | return QualType(); |
| 5954 | } |
| 5955 | |
| 5956 | // If the qualifiers are different, the types can still be merged. |
| 5957 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 5958 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
| 5959 | if (LQuals != RQuals) { |
| 5960 | // If any of these qualifiers are different, we have a type mismatch. |
| 5961 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 5962 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 5963 | return QualType(); |
| 5964 | |
| 5965 | // Exactly one GC qualifier difference is allowed: __strong is |
| 5966 | // okay if the other type has no GC qualifier but is an Objective |
| 5967 | // C object pointer (i.e. implicitly strong by default). We fix |
| 5968 | // this by pretending that the unqualified type was actually |
| 5969 | // qualified __strong. |
| 5970 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 5971 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 5972 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 5973 | |
| 5974 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 5975 | return QualType(); |
| 5976 | |
| 5977 | if (GC_L == Qualifiers::Strong) |
| 5978 | return LHS; |
| 5979 | if (GC_R == Qualifiers::Strong) |
| 5980 | return RHS; |
| 5981 | return QualType(); |
| 5982 | } |
| 5983 | |
| 5984 | if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) { |
| 5985 | QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5986 | QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5987 | QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT); |
| 5988 | if (ResQT == LHSBaseQT) |
| 5989 | return LHS; |
| 5990 | if (ResQT == RHSBaseQT) |
| 5991 | return RHS; |
| 5992 | } |
| 5993 | return QualType(); |
| 5994 | } |
| 5995 | |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 5996 | //===----------------------------------------------------------------------===// |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5997 | // Integer Predicates |
| 5998 | //===----------------------------------------------------------------------===// |
Chris Lattner | 88054de | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 5999 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6000 | unsigned ASTContext::getIntWidth(QualType T) const { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 6001 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
Eli Friedman | 29a7f33 | 2009-12-10 22:29:29 +0000 | [diff] [blame] | 6002 | T = ET->getDecl()->getIntegerType(); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6003 | if (T->isBooleanType()) |
| 6004 | return 1; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 6005 | // For builtin types, just use the standard type sizing method |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6006 | return (unsigned)getTypeSize(T); |
| 6007 | } |
| 6008 | |
| 6009 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) { |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6010 | assert(T->hasSignedIntegerRepresentation() && "Unexpected type"); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6011 | |
| 6012 | // Turn <4 x signed int> -> <4 x unsigned int> |
| 6013 | if (const VectorType *VTy = T->getAs<VectorType>()) |
| 6014 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 6015 | VTy->getNumElements(), VTy->getVectorKind()); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6016 | |
| 6017 | // For enums, we return the unsigned version of the base type. |
| 6018 | if (const EnumType *ETy = T->getAs<EnumType>()) |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6019 | T = ETy->getDecl()->getIntegerType(); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6020 | |
| 6021 | const BuiltinType *BTy = T->getAs<BuiltinType>(); |
| 6022 | assert(BTy && "Unexpected signed integer type"); |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6023 | switch (BTy->getKind()) { |
| 6024 | case BuiltinType::Char_S: |
| 6025 | case BuiltinType::SChar: |
| 6026 | return UnsignedCharTy; |
| 6027 | case BuiltinType::Short: |
| 6028 | return UnsignedShortTy; |
| 6029 | case BuiltinType::Int: |
| 6030 | return UnsignedIntTy; |
| 6031 | case BuiltinType::Long: |
| 6032 | return UnsignedLongTy; |
| 6033 | case BuiltinType::LongLong: |
| 6034 | return UnsignedLongLongTy; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 6035 | case BuiltinType::Int128: |
| 6036 | return UnsignedInt128Ty; |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6037 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6038 | llvm_unreachable("Unexpected signed integer type"); |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6039 | } |
| 6040 | } |
| 6041 | |
Argyrios Kyrtzidis | 7b90340 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 6042 | ASTMutationListener::~ASTMutationListener() { } |
| 6043 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6044 | |
| 6045 | //===----------------------------------------------------------------------===// |
| 6046 | // Builtin Type Computation |
| 6047 | //===----------------------------------------------------------------------===// |
| 6048 | |
| 6049 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6050 | /// pointer over the consumed characters. This returns the resultant type. If |
| 6051 | /// AllowTypeModifiers is false then modifier like * are not parsed, just basic |
| 6052 | /// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of |
| 6053 | /// a vector of "i*". |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6054 | /// |
| 6055 | /// RequiresICE is filled in on return to indicate whether the value is required |
| 6056 | /// to be an Integer Constant Expression. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6057 | static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context, |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6058 | ASTContext::GetBuiltinTypeError &Error, |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6059 | bool &RequiresICE, |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6060 | bool AllowTypeModifiers) { |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6061 | // Modifiers. |
| 6062 | int HowLong = 0; |
| 6063 | bool Signed = false, Unsigned = false; |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6064 | RequiresICE = false; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6065 | |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6066 | // Read the prefixed modifiers first. |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6067 | bool Done = false; |
| 6068 | while (!Done) { |
| 6069 | switch (*Str++) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6070 | default: Done = true; --Str; break; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6071 | case 'I': |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6072 | RequiresICE = true; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6073 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6074 | case 'S': |
| 6075 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 6076 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 6077 | Signed = true; |
| 6078 | break; |
| 6079 | case 'U': |
| 6080 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
| 6081 | assert(!Unsigned && "Can't use 'S' modifier multiple times!"); |
| 6082 | Unsigned = true; |
| 6083 | break; |
| 6084 | case 'L': |
| 6085 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 6086 | ++HowLong; |
| 6087 | break; |
| 6088 | } |
| 6089 | } |
| 6090 | |
| 6091 | QualType Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6092 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6093 | // Read the base type. |
| 6094 | switch (*Str++) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6095 | default: llvm_unreachable("Unknown builtin type letter!"); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6096 | case 'v': |
| 6097 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 6098 | "Bad modifiers used with 'v'!"); |
| 6099 | Type = Context.VoidTy; |
| 6100 | break; |
| 6101 | case 'f': |
| 6102 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 6103 | "Bad modifiers used with 'f'!"); |
| 6104 | Type = Context.FloatTy; |
| 6105 | break; |
| 6106 | case 'd': |
| 6107 | assert(HowLong < 2 && !Signed && !Unsigned && |
| 6108 | "Bad modifiers used with 'd'!"); |
| 6109 | if (HowLong) |
| 6110 | Type = Context.LongDoubleTy; |
| 6111 | else |
| 6112 | Type = Context.DoubleTy; |
| 6113 | break; |
| 6114 | case 's': |
| 6115 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 6116 | if (Unsigned) |
| 6117 | Type = Context.UnsignedShortTy; |
| 6118 | else |
| 6119 | Type = Context.ShortTy; |
| 6120 | break; |
| 6121 | case 'i': |
| 6122 | if (HowLong == 3) |
| 6123 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 6124 | else if (HowLong == 2) |
| 6125 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 6126 | else if (HowLong == 1) |
| 6127 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 6128 | else |
| 6129 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 6130 | break; |
| 6131 | case 'c': |
| 6132 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 6133 | if (Signed) |
| 6134 | Type = Context.SignedCharTy; |
| 6135 | else if (Unsigned) |
| 6136 | Type = Context.UnsignedCharTy; |
| 6137 | else |
| 6138 | Type = Context.CharTy; |
| 6139 | break; |
| 6140 | case 'b': // boolean |
| 6141 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 6142 | Type = Context.BoolTy; |
| 6143 | break; |
| 6144 | case 'z': // size_t. |
| 6145 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 6146 | Type = Context.getSizeType(); |
| 6147 | break; |
| 6148 | case 'F': |
| 6149 | Type = Context.getCFConstantStringType(); |
| 6150 | break; |
Fariborz Jahanian | ba8bda0 | 2010-11-09 21:38:20 +0000 | [diff] [blame] | 6151 | case 'G': |
| 6152 | Type = Context.getObjCIdType(); |
| 6153 | break; |
| 6154 | case 'H': |
| 6155 | Type = Context.getObjCSelType(); |
| 6156 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6157 | case 'a': |
| 6158 | Type = Context.getBuiltinVaListType(); |
| 6159 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 6160 | break; |
| 6161 | case 'A': |
| 6162 | // This is a "reference" to a va_list; however, what exactly |
| 6163 | // this means depends on how va_list is defined. There are two |
| 6164 | // different kinds of va_list: ones passed by value, and ones |
| 6165 | // passed by reference. An example of a by-value va_list is |
| 6166 | // x86, where va_list is a char*. An example of by-ref va_list |
| 6167 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 6168 | // we want this argument to be a char*&; for x86-64, we want |
| 6169 | // it to be a __va_list_tag*. |
| 6170 | Type = Context.getBuiltinVaListType(); |
| 6171 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6172 | if (Type->isArrayType()) |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6173 | Type = Context.getArrayDecayedType(Type); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6174 | else |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6175 | Type = Context.getLValueReferenceType(Type); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6176 | break; |
| 6177 | case 'V': { |
| 6178 | char *End; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6179 | unsigned NumElements = strtoul(Str, &End, 10); |
| 6180 | assert(End != Str && "Missing vector size"); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6181 | Str = End; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6182 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6183 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, |
| 6184 | RequiresICE, false); |
| 6185 | assert(!RequiresICE && "Can't require vector ICE"); |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6186 | |
| 6187 | // TODO: No way to make AltiVec vectors in builtins yet. |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 6188 | Type = Context.getVectorType(ElementType, NumElements, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 6189 | VectorType::GenericVector); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6190 | break; |
| 6191 | } |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 6192 | case 'X': { |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6193 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
| 6194 | false); |
| 6195 | assert(!RequiresICE && "Can't require complex ICE"); |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 6196 | Type = Context.getComplexType(ElementType); |
| 6197 | break; |
Fariborz Jahanian | cc075e4 | 2011-08-23 23:33:09 +0000 | [diff] [blame] | 6198 | } |
| 6199 | case 'Y' : { |
| 6200 | Type = Context.getPointerDiffType(); |
| 6201 | break; |
| 6202 | } |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 6203 | case 'P': |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 6204 | Type = Context.getFILEType(); |
| 6205 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6206 | Error = ASTContext::GE_Missing_stdio; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6207 | return QualType(); |
| 6208 | } |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 6209 | break; |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 6210 | case 'J': |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6211 | if (Signed) |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 6212 | Type = Context.getsigjmp_bufType(); |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6213 | else |
| 6214 | Type = Context.getjmp_bufType(); |
| 6215 | |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 6216 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6217 | Error = ASTContext::GE_Missing_setjmp; |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 6218 | return QualType(); |
| 6219 | } |
| 6220 | break; |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 6221 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6222 | |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6223 | // If there are modifiers and if we're allowed to parse them, go for it. |
| 6224 | Done = !AllowTypeModifiers; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6225 | while (!Done) { |
John McCall | 187ab37 | 2010-03-12 04:21:28 +0000 | [diff] [blame] | 6226 | switch (char c = *Str++) { |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6227 | default: Done = true; --Str; break; |
| 6228 | case '*': |
| 6229 | case '&': { |
| 6230 | // Both pointers and references can have their pointee types |
| 6231 | // qualified with an address space. |
| 6232 | char *End; |
| 6233 | unsigned AddrSpace = strtoul(Str, &End, 10); |
| 6234 | if (End != Str && AddrSpace != 0) { |
| 6235 | Type = Context.getAddrSpaceQualType(Type, AddrSpace); |
| 6236 | Str = End; |
| 6237 | } |
| 6238 | if (c == '*') |
| 6239 | Type = Context.getPointerType(Type); |
| 6240 | else |
| 6241 | Type = Context.getLValueReferenceType(Type); |
| 6242 | break; |
| 6243 | } |
| 6244 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 6245 | case 'C': |
| 6246 | Type = Type.withConst(); |
| 6247 | break; |
| 6248 | case 'D': |
| 6249 | Type = Context.getVolatileType(Type); |
| 6250 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6251 | } |
| 6252 | } |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6253 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6254 | assert((!RequiresICE || Type->isIntegralOrEnumerationType()) && |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6255 | "Integer constant 'I' type must be an integer"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6256 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6257 | return Type; |
| 6258 | } |
| 6259 | |
| 6260 | /// GetBuiltinType - Return the type for the specified builtin. |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6261 | QualType ASTContext::GetBuiltinType(unsigned Id, |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6262 | GetBuiltinTypeError &Error, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6263 | unsigned *IntegerConstantArgs) const { |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6264 | const char *TypeStr = BuiltinInfo.GetTypeString(Id); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6265 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6266 | SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6267 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6268 | bool RequiresICE = false; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6269 | Error = GE_None; |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6270 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error, |
| 6271 | RequiresICE, true); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6272 | if (Error != GE_None) |
| 6273 | return QualType(); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6274 | |
| 6275 | assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE"); |
| 6276 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6277 | while (TypeStr[0] && TypeStr[0] != '.') { |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6278 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6279 | if (Error != GE_None) |
| 6280 | return QualType(); |
| 6281 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6282 | // If this argument is required to be an IntegerConstantExpression and the |
| 6283 | // caller cares, fill in the bitmask we return. |
| 6284 | if (RequiresICE && IntegerConstantArgs) |
| 6285 | *IntegerConstantArgs |= 1 << ArgTypes.size(); |
| 6286 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6287 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 6288 | if (Ty->isArrayType()) |
| 6289 | Ty = getArrayDecayedType(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6290 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6291 | ArgTypes.push_back(Ty); |
| 6292 | } |
| 6293 | |
| 6294 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 6295 | "'.' should only occur at end of builtin type list!"); |
| 6296 | |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 6297 | FunctionType::ExtInfo EI; |
| 6298 | if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true); |
| 6299 | |
| 6300 | bool Variadic = (TypeStr[0] == '.'); |
| 6301 | |
| 6302 | // We really shouldn't be making a no-proto type here, especially in C++. |
| 6303 | if (ArgTypes.empty() && Variadic) |
| 6304 | return getFunctionNoProtoType(ResType, EI); |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 6305 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6306 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 6307 | EPI.ExtInfo = EI; |
| 6308 | EPI.Variadic = Variadic; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6309 | |
| 6310 | return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), EPI); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6311 | } |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 6312 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6313 | GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) { |
| 6314 | GVALinkage External = GVA_StrongExternal; |
| 6315 | |
| 6316 | Linkage L = FD->getLinkage(); |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6317 | switch (L) { |
| 6318 | case NoLinkage: |
| 6319 | case InternalLinkage: |
| 6320 | case UniqueExternalLinkage: |
| 6321 | return GVA_Internal; |
| 6322 | |
| 6323 | case ExternalLinkage: |
| 6324 | switch (FD->getTemplateSpecializationKind()) { |
| 6325 | case TSK_Undeclared: |
| 6326 | case TSK_ExplicitSpecialization: |
| 6327 | External = GVA_StrongExternal; |
| 6328 | break; |
| 6329 | |
| 6330 | case TSK_ExplicitInstantiationDefinition: |
| 6331 | return GVA_ExplicitTemplateInstantiation; |
| 6332 | |
| 6333 | case TSK_ExplicitInstantiationDeclaration: |
| 6334 | case TSK_ImplicitInstantiation: |
| 6335 | External = GVA_TemplateInstantiation; |
| 6336 | break; |
| 6337 | } |
| 6338 | } |
| 6339 | |
| 6340 | if (!FD->isInlined()) |
| 6341 | return External; |
| 6342 | |
| 6343 | if (!getLangOptions().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) { |
| 6344 | // GNU or C99 inline semantics. Determine whether this symbol should be |
| 6345 | // externally visible. |
| 6346 | if (FD->isInlineDefinitionExternallyVisible()) |
| 6347 | return External; |
| 6348 | |
| 6349 | // C99 inline semantics, where the symbol is not externally visible. |
| 6350 | return GVA_C99Inline; |
| 6351 | } |
| 6352 | |
| 6353 | // C++0x [temp.explicit]p9: |
| 6354 | // [ Note: The intent is that an inline function that is the subject of |
| 6355 | // an explicit instantiation declaration will still be implicitly |
| 6356 | // instantiated when used so that the body can be considered for |
| 6357 | // inlining, but that no out-of-line copy of the inline function would be |
| 6358 | // generated in the translation unit. -- end note ] |
| 6359 | if (FD->getTemplateSpecializationKind() |
| 6360 | == TSK_ExplicitInstantiationDeclaration) |
| 6361 | return GVA_C99Inline; |
| 6362 | |
| 6363 | return GVA_CXXInline; |
| 6364 | } |
| 6365 | |
| 6366 | GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) { |
| 6367 | // If this is a static data member, compute the kind of template |
| 6368 | // specialization. Otherwise, this variable is not part of a |
| 6369 | // template. |
| 6370 | TemplateSpecializationKind TSK = TSK_Undeclared; |
| 6371 | if (VD->isStaticDataMember()) |
| 6372 | TSK = VD->getTemplateSpecializationKind(); |
| 6373 | |
| 6374 | Linkage L = VD->getLinkage(); |
| 6375 | if (L == ExternalLinkage && getLangOptions().CPlusPlus && |
| 6376 | VD->getType()->getLinkage() == UniqueExternalLinkage) |
| 6377 | L = UniqueExternalLinkage; |
| 6378 | |
| 6379 | switch (L) { |
| 6380 | case NoLinkage: |
| 6381 | case InternalLinkage: |
| 6382 | case UniqueExternalLinkage: |
| 6383 | return GVA_Internal; |
| 6384 | |
| 6385 | case ExternalLinkage: |
| 6386 | switch (TSK) { |
| 6387 | case TSK_Undeclared: |
| 6388 | case TSK_ExplicitSpecialization: |
| 6389 | return GVA_StrongExternal; |
| 6390 | |
| 6391 | case TSK_ExplicitInstantiationDeclaration: |
| 6392 | llvm_unreachable("Variable should not be instantiated"); |
| 6393 | // Fall through to treat this like any other instantiation. |
| 6394 | |
| 6395 | case TSK_ExplicitInstantiationDefinition: |
| 6396 | return GVA_ExplicitTemplateInstantiation; |
| 6397 | |
| 6398 | case TSK_ImplicitInstantiation: |
| 6399 | return GVA_TemplateInstantiation; |
| 6400 | } |
| 6401 | } |
| 6402 | |
| 6403 | return GVA_StrongExternal; |
| 6404 | } |
| 6405 | |
Argyrios Kyrtzidis | 4ac7c0b | 2010-07-29 20:08:05 +0000 | [diff] [blame] | 6406 | bool ASTContext::DeclMustBeEmitted(const Decl *D) { |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6407 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 6408 | if (!VD->isFileVarDecl()) |
| 6409 | return false; |
Argyrios Kyrtzidis | 144b38a | 2011-09-13 21:35:00 +0000 | [diff] [blame] | 6410 | } else if (!isa<FunctionDecl>(D)) |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6411 | return false; |
| 6412 | |
Argyrios Kyrtzidis | ab411c8 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 6413 | // Weak references don't produce any output by themselves. |
| 6414 | if (D->hasAttr<WeakRefAttr>()) |
| 6415 | return false; |
| 6416 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6417 | // Aliases and used decls are required. |
| 6418 | if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>()) |
| 6419 | return true; |
| 6420 | |
| 6421 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 6422 | // Forward declarations aren't required. |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 6423 | if (!FD->doesThisDeclarationHaveABody()) |
Nick Lewycky | dce67a7 | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 6424 | return FD->doesDeclarationForceExternallyVisibleDefinition(); |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6425 | |
| 6426 | // Constructors and destructors are required. |
| 6427 | if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>()) |
| 6428 | return true; |
| 6429 | |
| 6430 | // The key function for a class is required. |
| 6431 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 6432 | const CXXRecordDecl *RD = MD->getParent(); |
| 6433 | if (MD->isOutOfLine() && RD->isDynamicClass()) { |
| 6434 | const CXXMethodDecl *KeyFunc = getKeyFunction(RD); |
| 6435 | if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl()) |
| 6436 | return true; |
| 6437 | } |
| 6438 | } |
| 6439 | |
| 6440 | GVALinkage Linkage = GetGVALinkageForFunction(FD); |
| 6441 | |
| 6442 | // static, static inline, always_inline, and extern inline functions can |
| 6443 | // always be deferred. Normal inline functions can be deferred in C99/C++. |
| 6444 | // Implicit template instantiations can also be deferred in C++. |
| 6445 | if (Linkage == GVA_Internal || Linkage == GVA_C99Inline || |
| 6446 | Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) |
| 6447 | return false; |
| 6448 | return true; |
| 6449 | } |
Douglas Gregor | 94da158 | 2011-09-10 00:22:34 +0000 | [diff] [blame] | 6450 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6451 | const VarDecl *VD = cast<VarDecl>(D); |
| 6452 | assert(VD->isFileVarDecl() && "Expected file scoped var"); |
| 6453 | |
Argyrios Kyrtzidis | ab411c8 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 6454 | if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly) |
| 6455 | return false; |
| 6456 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6457 | // Structs that have non-trivial constructors or destructors are required. |
| 6458 | |
| 6459 | // FIXME: Handle references. |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 6460 | // FIXME: Be more selective about which constructors we care about. |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6461 | if (const RecordType *RT = VD->getType()->getAs<RecordType>()) { |
| 6462 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 6463 | if (RD->hasDefinition() && !(RD->hasTrivialDefaultConstructor() && |
| 6464 | RD->hasTrivialCopyConstructor() && |
| 6465 | RD->hasTrivialMoveConstructor() && |
| 6466 | RD->hasTrivialDestructor())) |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6467 | return true; |
| 6468 | } |
| 6469 | } |
| 6470 | |
| 6471 | GVALinkage L = GetGVALinkageForVariable(VD); |
| 6472 | if (L == GVA_Internal || L == GVA_TemplateInstantiation) { |
| 6473 | if (!(VD->getInit() && VD->getInit()->HasSideEffects(*this))) |
| 6474 | return false; |
| 6475 | } |
| 6476 | |
| 6477 | return true; |
| 6478 | } |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 6479 | |
Charles Davis | ee743f9 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 6480 | CallingConv ASTContext::getDefaultMethodCallConv() { |
| 6481 | // Pass through to the C++ ABI object |
| 6482 | return ABI->getDefaultMethodCallConv(); |
| 6483 | } |
| 6484 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6485 | bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const { |
Anders Carlsson | dae0cb5 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 6486 | // Pass through to the C++ ABI object |
| 6487 | return ABI->isNearlyEmpty(RD); |
| 6488 | } |
| 6489 | |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 6490 | MangleContext *ASTContext::createMangleContext() { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 6491 | switch (Target->getCXXABI()) { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 6492 | case CXXABI_ARM: |
| 6493 | case CXXABI_Itanium: |
| 6494 | return createItaniumMangleContext(*this, getDiagnostics()); |
| 6495 | case CXXABI_Microsoft: |
| 6496 | return createMicrosoftMangleContext(*this, getDiagnostics()); |
| 6497 | } |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6498 | llvm_unreachable("Unsupported ABI"); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 6499 | } |
| 6500 | |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 6501 | CXXABI::~CXXABI() {} |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 6502 | |
| 6503 | size_t ASTContext::getSideTableAllocatedMemory() const { |
Ted Kremenek | 0c8cd1a | 2011-07-27 18:41:12 +0000 | [diff] [blame] | 6504 | return ASTRecordLayouts.getMemorySize() |
| 6505 | + llvm::capacity_in_bytes(ObjCLayouts) |
| 6506 | + llvm::capacity_in_bytes(KeyFunctions) |
| 6507 | + llvm::capacity_in_bytes(ObjCImpls) |
| 6508 | + llvm::capacity_in_bytes(BlockVarCopyInits) |
| 6509 | + llvm::capacity_in_bytes(DeclAttrs) |
| 6510 | + llvm::capacity_in_bytes(InstantiatedFromStaticDataMember) |
| 6511 | + llvm::capacity_in_bytes(InstantiatedFromUsingDecl) |
| 6512 | + llvm::capacity_in_bytes(InstantiatedFromUsingShadowDecl) |
| 6513 | + llvm::capacity_in_bytes(InstantiatedFromUnnamedFieldDecl) |
| 6514 | + llvm::capacity_in_bytes(OverriddenMethods) |
| 6515 | + llvm::capacity_in_bytes(Types) |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 6516 | + llvm::capacity_in_bytes(VariableArrayTypes) |
Francois Pichet | 0d95f0d | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 6517 | + llvm::capacity_in_bytes(ClassScopeSpecializationPattern); |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 6518 | } |