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 { |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 53 | HalfRank, FloatRank, DoubleRank, LongDoubleRank |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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), |
Rafael Espindola | e2d4f4e | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 230 | jmp_bufDecl(0), sigjmp_bufDecl(0), ucontext_tDecl(0), |
| 231 | BlockDescriptorType(0), BlockDescriptorExtendedType(0), |
| 232 | cudaConfigureCallDecl(0), |
Douglas Gregor | e664977 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 233 | NullTypeSourceInfo(QualType()), |
| 234 | FirstLocalImport(), LastLocalImport(), |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 235 | SourceMgr(SM), LangOpts(LOpts), |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 236 | AddrSpaceMap(0), Target(t), PrintingPolicy(LOpts), |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 237 | Idents(idents), Selectors(sels), |
| 238 | BuiltinInfo(builtins), |
| 239 | DeclarationNames(*this), |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 240 | ExternalSource(0), Listener(0), |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 241 | LastSDM(0, 0), |
| 242 | UniqueBlockByRefTypeID(0) |
| 243 | { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 244 | if (size_reserve > 0) Types.reserve(size_reserve); |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 245 | TUDecl = TranslationUnitDecl::Create(*this); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 246 | |
| 247 | if (!DelayInitialization) { |
| 248 | assert(t && "No target supplied for ASTContext initialization"); |
| 249 | InitBuiltinTypes(*t); |
| 250 | } |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 253 | ASTContext::~ASTContext() { |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 254 | // Release the DenseMaps associated with DeclContext objects. |
| 255 | // FIXME: Is this the ideal solution? |
| 256 | ReleaseDeclContextMaps(); |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 257 | |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 258 | // Call all of the deallocation functions. |
| 259 | for (unsigned I = 0, N = Deallocations.size(); I != N; ++I) |
| 260 | Deallocations[I].first(Deallocations[I].second); |
Douglas Gregor | 0054531 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 261 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 262 | // Release all of the memory associated with overridden C++ methods. |
| 263 | for (llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::iterator |
| 264 | OM = OverriddenMethods.begin(), OMEnd = OverriddenMethods.end(); |
| 265 | OM != OMEnd; ++OM) |
| 266 | OM->second.Destroy(); |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 267 | |
Ted Kremenek | dcfcfbe | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 268 | // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 269 | // because they can contain DenseMaps. |
| 270 | for (llvm::DenseMap<const ObjCContainerDecl*, |
| 271 | const ASTRecordLayout*>::iterator |
| 272 | I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) |
| 273 | // Increment in loop to prevent using deallocated memory. |
| 274 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 275 | R->Destroy(*this); |
| 276 | |
Ted Kremenek | dcfcfbe | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 277 | for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator |
| 278 | I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) { |
| 279 | // Increment in loop to prevent using deallocated memory. |
| 280 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 281 | R->Destroy(*this); |
| 282 | } |
Douglas Gregor | 6320064 | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 283 | |
| 284 | for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(), |
| 285 | AEnd = DeclAttrs.end(); |
| 286 | A != AEnd; ++A) |
| 287 | A->second->~AttrVec(); |
| 288 | } |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 289 | |
Douglas Gregor | 0054531 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 290 | void ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) { |
| 291 | Deallocations.push_back(std::make_pair(Callback, Data)); |
| 292 | } |
| 293 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 294 | void |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 295 | ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) { |
| 296 | ExternalSource.reset(Source.take()); |
| 297 | } |
| 298 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 299 | void ASTContext::PrintStats() const { |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 300 | llvm::errs() << "\n*** AST Context Stats:\n"; |
| 301 | llvm::errs() << " " << Types.size() << " types total.\n"; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 302 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 303 | unsigned counts[] = { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 304 | #define TYPE(Name, Parent) 0, |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 305 | #define ABSTRACT_TYPE(Name, Parent) |
| 306 | #include "clang/AST/TypeNodes.def" |
| 307 | 0 // Extra |
| 308 | }; |
Douglas Gregor | c2ee10d | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 309 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 310 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 311 | Type *T = Types[i]; |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 312 | counts[(unsigned)T->getTypeClass()]++; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 315 | unsigned Idx = 0; |
| 316 | unsigned TotalBytes = 0; |
| 317 | #define TYPE(Name, Parent) \ |
| 318 | if (counts[Idx]) \ |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 319 | llvm::errs() << " " << counts[Idx] << " " << #Name \ |
| 320 | << " types\n"; \ |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 321 | TotalBytes += counts[Idx] * sizeof(Name##Type); \ |
| 322 | ++Idx; |
| 323 | #define ABSTRACT_TYPE(Name, Parent) |
| 324 | #include "clang/AST/TypeNodes.def" |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 325 | |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 326 | llvm::errs() << "Total bytes = " << TotalBytes << "\n"; |
| 327 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 328 | // Implicit special member functions. |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 329 | llvm::errs() << NumImplicitDefaultConstructorsDeclared << "/" |
| 330 | << NumImplicitDefaultConstructors |
| 331 | << " implicit default constructors created\n"; |
| 332 | llvm::errs() << NumImplicitCopyConstructorsDeclared << "/" |
| 333 | << NumImplicitCopyConstructors |
| 334 | << " implicit copy constructors created\n"; |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 335 | if (getLangOptions().CPlusPlus) |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 336 | llvm::errs() << NumImplicitMoveConstructorsDeclared << "/" |
| 337 | << NumImplicitMoveConstructors |
| 338 | << " implicit move constructors created\n"; |
| 339 | llvm::errs() << NumImplicitCopyAssignmentOperatorsDeclared << "/" |
| 340 | << NumImplicitCopyAssignmentOperators |
| 341 | << " implicit copy assignment operators created\n"; |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 342 | if (getLangOptions().CPlusPlus) |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 343 | llvm::errs() << NumImplicitMoveAssignmentOperatorsDeclared << "/" |
| 344 | << NumImplicitMoveAssignmentOperators |
| 345 | << " implicit move assignment operators created\n"; |
| 346 | llvm::errs() << NumImplicitDestructorsDeclared << "/" |
| 347 | << NumImplicitDestructors |
| 348 | << " implicit destructors created\n"; |
| 349 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 350 | if (ExternalSource.get()) { |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 351 | llvm::errs() << "\n"; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 352 | ExternalSource->PrintStats(); |
| 353 | } |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 354 | |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 355 | BumpAlloc.PrintStats(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Douglas Gregor | 772eeae | 2011-08-12 06:49:56 +0000 | [diff] [blame] | 358 | TypedefDecl *ASTContext::getInt128Decl() const { |
| 359 | if (!Int128Decl) { |
| 360 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(Int128Ty); |
| 361 | Int128Decl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 362 | getTranslationUnitDecl(), |
| 363 | SourceLocation(), |
| 364 | SourceLocation(), |
| 365 | &Idents.get("__int128_t"), |
| 366 | TInfo); |
| 367 | } |
| 368 | |
| 369 | return Int128Decl; |
| 370 | } |
| 371 | |
| 372 | TypedefDecl *ASTContext::getUInt128Decl() const { |
| 373 | if (!UInt128Decl) { |
| 374 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(UnsignedInt128Ty); |
| 375 | UInt128Decl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 376 | getTranslationUnitDecl(), |
| 377 | SourceLocation(), |
| 378 | SourceLocation(), |
| 379 | &Idents.get("__uint128_t"), |
| 380 | TInfo); |
| 381 | } |
| 382 | |
| 383 | return UInt128Decl; |
| 384 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 385 | |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 386 | void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 387 | BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 388 | R = CanQualType::CreateUnsafe(QualType(Ty, 0)); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 389 | Types.push_back(Ty); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 392 | void ASTContext::InitBuiltinTypes(const TargetInfo &Target) { |
| 393 | assert((!this->Target || this->Target == &Target) && |
| 394 | "Incorrect target reinitialization"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 395 | assert(VoidTy.isNull() && "Context reinitialized?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 396 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 397 | this->Target = &Target; |
| 398 | |
| 399 | ABI.reset(createCXXABI(Target)); |
| 400 | AddrSpaceMap = getAddressSpaceMap(Target, LangOpts); |
| 401 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 402 | // C99 6.2.5p19. |
| 403 | InitBuiltinType(VoidTy, BuiltinType::Void); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 404 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 405 | // C99 6.2.5p2. |
| 406 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
| 407 | // C99 6.2.5p3. |
Eli Friedman | 15b9176 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 408 | if (LangOpts.CharIsSigned) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 409 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 410 | else |
| 411 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
| 412 | // C99 6.2.5p4. |
| 413 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 414 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 415 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 416 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 417 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 418 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 419 | // C99 6.2.5p6. |
| 420 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 421 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 422 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 423 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 424 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 425 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 426 | // C99 6.2.5p10. |
| 427 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 428 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 429 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 430 | |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 431 | // GNU extension, 128-bit integers. |
| 432 | InitBuiltinType(Int128Ty, BuiltinType::Int128); |
| 433 | InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128); |
| 434 | |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 435 | if (LangOpts.CPlusPlus) { // C++ 3.9.1p5 |
Eli Friedman | d3d77cd | 2011-04-30 19:24:24 +0000 | [diff] [blame] | 436 | if (TargetInfo::isTypeSigned(Target.getWCharType())) |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 437 | InitBuiltinType(WCharTy, BuiltinType::WChar_S); |
| 438 | else // -fshort-wchar makes wchar_t be unsigned. |
| 439 | InitBuiltinType(WCharTy, BuiltinType::WChar_U); |
| 440 | } else // C99 |
Chris Lattner | 3a25032 | 2009-02-26 23:43:47 +0000 | [diff] [blame] | 441 | WCharTy = getFromTargetType(Target.getWCharType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 442 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 443 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 444 | InitBuiltinType(Char16Ty, BuiltinType::Char16); |
| 445 | else // C99 |
| 446 | Char16Ty = getFromTargetType(Target.getChar16Type()); |
| 447 | |
| 448 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 449 | InitBuiltinType(Char32Ty, BuiltinType::Char32); |
| 450 | else // C99 |
| 451 | Char32Ty = getFromTargetType(Target.getChar32Type()); |
| 452 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 453 | // Placeholder type for type-dependent expressions whose type is |
| 454 | // completely unknown. No code should ever check a type against |
| 455 | // DependentTy and users should never see it; however, it is here to |
| 456 | // help diagnose failures to properly check for type-dependent |
| 457 | // expressions. |
| 458 | InitBuiltinType(DependentTy, BuiltinType::Dependent); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 459 | |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 460 | // Placeholder type for functions. |
| 461 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
| 462 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 463 | // Placeholder type for bound members. |
| 464 | InitBuiltinType(BoundMemberTy, BuiltinType::BoundMember); |
| 465 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 466 | // Placeholder type for pseudo-objects. |
| 467 | InitBuiltinType(PseudoObjectTy, BuiltinType::PseudoObject); |
| 468 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 469 | // "any" type; useful for debugger-like clients. |
| 470 | InitBuiltinType(UnknownAnyTy, BuiltinType::UnknownAny); |
| 471 | |
John McCall | 0ddaeb9 | 2011-10-17 18:09:15 +0000 | [diff] [blame] | 472 | // Placeholder type for unbridged ARC casts. |
| 473 | InitBuiltinType(ARCUnbridgedCastTy, BuiltinType::ARCUnbridgedCast); |
| 474 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 475 | // C99 6.2.5p11. |
| 476 | FloatComplexTy = getComplexType(FloatTy); |
| 477 | DoubleComplexTy = getComplexType(DoubleTy); |
| 478 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 479 | |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 480 | BuiltinVaListType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 481 | |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 482 | // Builtin types for 'id', 'Class', and 'SEL'. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 483 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); |
| 484 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 485 | InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 486 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 487 | ObjCConstantStringType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 488 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 489 | // void * type |
| 490 | VoidPtrTy = getPointerType(VoidTy); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 491 | |
| 492 | // nullptr type (C++0x 2.14.7) |
| 493 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 494 | |
| 495 | // half type (OpenCL 6.1.1.1) / ARM NEON __fp16 |
| 496 | InitBuiltinType(HalfTy, BuiltinType::Half); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 497 | } |
| 498 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 499 | DiagnosticsEngine &ASTContext::getDiagnostics() const { |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 500 | return SourceMgr.getDiagnostics(); |
| 501 | } |
| 502 | |
Douglas Gregor | 6320064 | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 503 | AttrVec& ASTContext::getDeclAttrs(const Decl *D) { |
| 504 | AttrVec *&Result = DeclAttrs[D]; |
| 505 | if (!Result) { |
| 506 | void *Mem = Allocate(sizeof(AttrVec)); |
| 507 | Result = new (Mem) AttrVec; |
| 508 | } |
| 509 | |
| 510 | return *Result; |
| 511 | } |
| 512 | |
| 513 | /// \brief Erase the attributes corresponding to the given declaration. |
| 514 | void ASTContext::eraseDeclAttrs(const Decl *D) { |
| 515 | llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D); |
| 516 | if (Pos != DeclAttrs.end()) { |
| 517 | Pos->second->~AttrVec(); |
| 518 | DeclAttrs.erase(Pos); |
| 519 | } |
| 520 | } |
| 521 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 522 | MemberSpecializationInfo * |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 523 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 524 | assert(Var->isStaticDataMember() && "Not a static data member"); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 525 | llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 526 | = InstantiatedFromStaticDataMember.find(Var); |
| 527 | if (Pos == InstantiatedFromStaticDataMember.end()) |
| 528 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 529 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 530 | return Pos->second; |
| 531 | } |
| 532 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 533 | void |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 534 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, |
Argyrios Kyrtzidis | 9421adc | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 535 | TemplateSpecializationKind TSK, |
| 536 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 537 | assert(Inst->isStaticDataMember() && "Not a static data member"); |
| 538 | assert(Tmpl->isStaticDataMember() && "Not a static data member"); |
| 539 | assert(!InstantiatedFromStaticDataMember[Inst] && |
| 540 | "Already noted what static data member was instantiated from"); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 541 | InstantiatedFromStaticDataMember[Inst] |
Argyrios Kyrtzidis | 9421adc | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 542 | = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 545 | FunctionDecl *ASTContext::getClassScopeSpecializationPattern( |
| 546 | const FunctionDecl *FD){ |
| 547 | assert(FD && "Specialization is 0"); |
| 548 | llvm::DenseMap<const FunctionDecl*, FunctionDecl *>::const_iterator Pos |
Francois Pichet | 0d95f0d | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 549 | = ClassScopeSpecializationPattern.find(FD); |
| 550 | if (Pos == ClassScopeSpecializationPattern.end()) |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 551 | return 0; |
| 552 | |
| 553 | return Pos->second; |
| 554 | } |
| 555 | |
| 556 | void ASTContext::setClassScopeSpecializationPattern(FunctionDecl *FD, |
| 557 | FunctionDecl *Pattern) { |
| 558 | assert(FD && "Specialization is 0"); |
| 559 | assert(Pattern && "Class scope specialization pattern is 0"); |
Francois Pichet | 0d95f0d | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 560 | ClassScopeSpecializationPattern[FD] = Pattern; |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 561 | } |
| 562 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 563 | NamedDecl * |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 564 | ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 565 | llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 566 | = InstantiatedFromUsingDecl.find(UUD); |
| 567 | if (Pos == InstantiatedFromUsingDecl.end()) |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 568 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 569 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 570 | return Pos->second; |
| 571 | } |
| 572 | |
| 573 | void |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 574 | ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) { |
| 575 | assert((isa<UsingDecl>(Pattern) || |
| 576 | isa<UnresolvedUsingValueDecl>(Pattern) || |
| 577 | isa<UnresolvedUsingTypenameDecl>(Pattern)) && |
| 578 | "pattern decl is not a using decl"); |
| 579 | assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists"); |
| 580 | InstantiatedFromUsingDecl[Inst] = Pattern; |
| 581 | } |
| 582 | |
| 583 | UsingShadowDecl * |
| 584 | ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) { |
| 585 | llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos |
| 586 | = InstantiatedFromUsingShadowDecl.find(Inst); |
| 587 | if (Pos == InstantiatedFromUsingShadowDecl.end()) |
| 588 | return 0; |
| 589 | |
| 590 | return Pos->second; |
| 591 | } |
| 592 | |
| 593 | void |
| 594 | ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, |
| 595 | UsingShadowDecl *Pattern) { |
| 596 | assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists"); |
| 597 | InstantiatedFromUsingShadowDecl[Inst] = Pattern; |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 600 | FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { |
| 601 | llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos |
| 602 | = InstantiatedFromUnnamedFieldDecl.find(Field); |
| 603 | if (Pos == InstantiatedFromUnnamedFieldDecl.end()) |
| 604 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 605 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 606 | return Pos->second; |
| 607 | } |
| 608 | |
| 609 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, |
| 610 | FieldDecl *Tmpl) { |
| 611 | assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed"); |
| 612 | assert(!Tmpl->getDeclName() && "Template field decl is not unnamed"); |
| 613 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] && |
| 614 | "Already noted what unnamed field was instantiated from"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 615 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 616 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; |
| 617 | } |
| 618 | |
Fariborz Jahanian | 14d56ef | 2011-04-27 17:14:21 +0000 | [diff] [blame] | 619 | bool ASTContext::ZeroBitfieldFollowsNonBitfield(const FieldDecl *FD, |
| 620 | const FieldDecl *LastFD) const { |
| 621 | return (FD->isBitField() && LastFD && !LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 622 | FD->getBitWidthValue(*this) == 0); |
Fariborz Jahanian | 14d56ef | 2011-04-27 17:14:21 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Fariborz Jahanian | 340fa24 | 2011-05-02 17:20:56 +0000 | [diff] [blame] | 625 | bool ASTContext::ZeroBitfieldFollowsBitfield(const FieldDecl *FD, |
| 626 | const FieldDecl *LastFD) const { |
| 627 | return (FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 628 | FD->getBitWidthValue(*this) == 0 && |
| 629 | LastFD->getBitWidthValue(*this) != 0); |
Fariborz Jahanian | 340fa24 | 2011-05-02 17:20:56 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Fariborz Jahanian | 9b3acaa | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 632 | bool ASTContext::BitfieldFollowsBitfield(const FieldDecl *FD, |
| 633 | const FieldDecl *LastFD) const { |
| 634 | return (FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 635 | FD->getBitWidthValue(*this) && |
| 636 | LastFD->getBitWidthValue(*this)); |
Fariborz Jahanian | 9b3acaa | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Chad Rosier | dd7fddb | 2011-08-04 23:34:15 +0000 | [diff] [blame] | 639 | bool ASTContext::NonBitfieldFollowsBitfield(const FieldDecl *FD, |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 640 | const FieldDecl *LastFD) const { |
| 641 | return (!FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 642 | LastFD->getBitWidthValue(*this)); |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Chad Rosier | dd7fddb | 2011-08-04 23:34:15 +0000 | [diff] [blame] | 645 | bool ASTContext::BitfieldFollowsNonBitfield(const FieldDecl *FD, |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 646 | const FieldDecl *LastFD) const { |
| 647 | return (FD->isBitField() && LastFD && !LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 648 | FD->getBitWidthValue(*this)); |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 651 | ASTContext::overridden_cxx_method_iterator |
| 652 | ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const { |
| 653 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 654 | = OverriddenMethods.find(Method); |
| 655 | if (Pos == OverriddenMethods.end()) |
| 656 | return 0; |
| 657 | |
| 658 | return Pos->second.begin(); |
| 659 | } |
| 660 | |
| 661 | ASTContext::overridden_cxx_method_iterator |
| 662 | ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const { |
| 663 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 664 | = OverriddenMethods.find(Method); |
| 665 | if (Pos == OverriddenMethods.end()) |
| 666 | return 0; |
| 667 | |
| 668 | return Pos->second.end(); |
| 669 | } |
| 670 | |
Argyrios Kyrtzidis | c91e9f4 | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 671 | unsigned |
| 672 | ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const { |
| 673 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 674 | = OverriddenMethods.find(Method); |
| 675 | if (Pos == OverriddenMethods.end()) |
| 676 | return 0; |
| 677 | |
| 678 | return Pos->second.size(); |
| 679 | } |
| 680 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 681 | void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method, |
| 682 | const CXXMethodDecl *Overridden) { |
| 683 | OverriddenMethods[Method].push_back(Overridden); |
| 684 | } |
| 685 | |
Douglas Gregor | e664977 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 686 | void ASTContext::addedLocalImportDecl(ImportDecl *Import) { |
| 687 | assert(!Import->NextLocalImport && "Import declaration already in the chain"); |
| 688 | assert(!Import->isFromASTFile() && "Non-local import declaration"); |
| 689 | if (!FirstLocalImport) { |
| 690 | FirstLocalImport = Import; |
| 691 | LastLocalImport = Import; |
| 692 | return; |
| 693 | } |
| 694 | |
| 695 | LastLocalImport->NextLocalImport = Import; |
| 696 | LastLocalImport = Import; |
| 697 | } |
| 698 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 699 | //===----------------------------------------------------------------------===// |
| 700 | // Type Sizing and Analysis |
| 701 | //===----------------------------------------------------------------------===// |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 702 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 703 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 704 | /// scalar floating point type. |
| 705 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 706 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 707 | assert(BT && "Not a floating point type!"); |
| 708 | switch (BT->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 709 | default: llvm_unreachable("Not a floating point type!"); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 710 | case BuiltinType::Half: return Target->getHalfFormat(); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 711 | case BuiltinType::Float: return Target->getFloatFormat(); |
| 712 | case BuiltinType::Double: return Target->getDoubleFormat(); |
| 713 | case BuiltinType::LongDouble: return Target->getLongDoubleFormat(); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 714 | } |
| 715 | } |
| 716 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 717 | /// getDeclAlign - Return a conservative estimate of the alignment of the |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 718 | /// specified decl. Note that bitfields do not have a valid alignment, so |
| 719 | /// this method will assert on them. |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 720 | /// If @p RefAsPointee, references are treated like their underlying type |
| 721 | /// (for alignof), else they're treated like pointers (for CodeGen). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 722 | CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) const { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 723 | unsigned Align = Target->getCharWidth(); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 724 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 725 | bool UseAlignAttrOnly = false; |
| 726 | if (unsigned AlignFromAttr = D->getMaxAlignment()) { |
| 727 | Align = AlignFromAttr; |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 728 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 729 | // __attribute__((aligned)) can increase or decrease alignment |
| 730 | // *except* on a struct or struct member, where it only increases |
| 731 | // alignment unless 'packed' is also specified. |
| 732 | // |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 733 | // It is an error for alignas to decrease alignment, so we can |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 734 | // ignore that possibility; Sema should diagnose it. |
| 735 | if (isa<FieldDecl>(D)) { |
| 736 | UseAlignAttrOnly = D->hasAttr<PackedAttr>() || |
| 737 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
| 738 | } else { |
| 739 | UseAlignAttrOnly = true; |
| 740 | } |
| 741 | } |
Fariborz Jahanian | 78a7d7d | 2011-05-05 21:19:14 +0000 | [diff] [blame] | 742 | else if (isa<FieldDecl>(D)) |
| 743 | UseAlignAttrOnly = |
| 744 | D->hasAttr<PackedAttr>() || |
| 745 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 746 | |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 747 | // If we're using the align attribute only, just ignore everything |
| 748 | // else about the declaration and its type. |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 749 | if (UseAlignAttrOnly) { |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 750 | // do nothing |
| 751 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 752 | } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 753 | QualType T = VD->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 754 | if (const ReferenceType* RT = T->getAs<ReferenceType>()) { |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 755 | if (RefAsPointee) |
| 756 | T = RT->getPointeeType(); |
| 757 | else |
| 758 | T = getPointerType(RT->getPointeeType()); |
| 759 | } |
| 760 | if (!T->isIncompleteType() && !T->isFunctionType()) { |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 761 | // Adjust alignments of declarations with array type by the |
| 762 | // large-array alignment on the target. |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 763 | unsigned MinWidth = Target->getLargeArrayMinWidth(); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 764 | const ArrayType *arrayType; |
| 765 | if (MinWidth && (arrayType = getAsArrayType(T))) { |
| 766 | if (isa<VariableArrayType>(arrayType)) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 767 | Align = std::max(Align, Target->getLargeArrayAlign()); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 768 | else if (isa<ConstantArrayType>(arrayType) && |
| 769 | MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType))) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 770 | Align = std::max(Align, Target->getLargeArrayAlign()); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 771 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 772 | // Walk through any array types while we're at it. |
| 773 | T = getBaseElementType(arrayType); |
| 774 | } |
Chad Rosier | 9f1210c | 2011-07-26 07:03:04 +0000 | [diff] [blame] | 775 | Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 776 | } |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 777 | |
| 778 | // Fields can be subject to extra alignment constraints, like if |
| 779 | // the field is packed, the struct is packed, or the struct has a |
| 780 | // a max-field-alignment constraint (#pragma pack). So calculate |
| 781 | // the actual alignment of the field within the struct, and then |
| 782 | // (as we're expected to) constrain that by the alignment of the type. |
| 783 | if (const FieldDecl *field = dyn_cast<FieldDecl>(VD)) { |
| 784 | // So calculate the alignment of the field. |
| 785 | const ASTRecordLayout &layout = getASTRecordLayout(field->getParent()); |
| 786 | |
| 787 | // Start with the record's overall alignment. |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 788 | unsigned fieldAlign = toBits(layout.getAlignment()); |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 789 | |
| 790 | // Use the GCD of that and the offset within the record. |
| 791 | uint64_t offset = layout.getFieldOffset(field->getFieldIndex()); |
| 792 | if (offset > 0) { |
| 793 | // Alignment is always a power of 2, so the GCD will be a power of 2, |
| 794 | // which means we get to do this crazy thing instead of Euclid's. |
| 795 | uint64_t lowBitOfOffset = offset & (~offset + 1); |
| 796 | if (lowBitOfOffset < fieldAlign) |
| 797 | fieldAlign = static_cast<unsigned>(lowBitOfOffset); |
| 798 | } |
| 799 | |
| 800 | Align = std::min(Align, fieldAlign); |
Charles Davis | 05f6247 | 2010-02-23 04:52:00 +0000 | [diff] [blame] | 801 | } |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 802 | } |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 803 | |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 804 | return toCharUnitsFromBits(Align); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 805 | } |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 806 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 807 | std::pair<CharUnits, CharUnits> |
Ken Dyck | bee5a79 | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 808 | ASTContext::getTypeInfoInChars(const Type *T) const { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 809 | std::pair<uint64_t, unsigned> Info = getTypeInfo(T); |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 810 | return std::make_pair(toCharUnitsFromBits(Info.first), |
| 811 | toCharUnitsFromBits(Info.second)); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | std::pair<CharUnits, CharUnits> |
Ken Dyck | bee5a79 | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 815 | ASTContext::getTypeInfoInChars(QualType T) const { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 816 | return getTypeInfoInChars(T.getTypePtr()); |
| 817 | } |
| 818 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 819 | /// getTypeSize - Return the size of the specified type, in bits. This method |
| 820 | /// does not work on incomplete types. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 821 | /// |
| 822 | /// FIXME: Pointers into different addr spaces could have different sizes and |
| 823 | /// alignment requirements: getPointerInfo should take an AddrSpace, this |
| 824 | /// should take a QualType, &c. |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 825 | std::pair<uint64_t, unsigned> |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 826 | ASTContext::getTypeInfo(const Type *T) const { |
Mike Stump | 5e30100 | 2009-02-27 18:32:39 +0000 | [diff] [blame] | 827 | uint64_t Width=0; |
| 828 | unsigned Align=8; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 829 | switch (T->getTypeClass()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 830 | #define TYPE(Class, Base) |
| 831 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 832 | #define NON_CANONICAL_TYPE(Class, Base) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 833 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 834 | #include "clang/AST/TypeNodes.def" |
John McCall | d3d49bb | 2011-06-28 16:49:23 +0000 | [diff] [blame] | 835 | llvm_unreachable("Should not see dependent types"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 836 | break; |
| 837 | |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 838 | case Type::FunctionNoProto: |
| 839 | case Type::FunctionProto: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 840 | // GCC extension: alignof(function) = 32 bits |
| 841 | Width = 0; |
| 842 | Align = 32; |
| 843 | break; |
| 844 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 845 | case Type::IncompleteArray: |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 846 | case Type::VariableArray: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 847 | Width = 0; |
| 848 | Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); |
| 849 | break; |
| 850 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 851 | case Type::ConstantArray: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 852 | const ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 853 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 854 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); |
Abramo Bagnara | fea966a | 2011-12-13 11:23:52 +0000 | [diff] [blame] | 855 | uint64_t Size = CAT->getSize().getZExtValue(); |
| 856 | assert((Size == 0 || EltInfo.first <= (uint64_t)(-1)/Size) && "Overflow in array type bit size evaluation"); |
| 857 | Width = EltInfo.first*Size; |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 858 | Align = EltInfo.second; |
Argyrios Kyrtzidis | cd88b41 | 2011-04-26 21:05:39 +0000 | [diff] [blame] | 859 | Width = llvm::RoundUpToAlignment(Width, Align); |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 860 | break; |
Christopher Lamb | 5c09a02 | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 861 | } |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 862 | case Type::ExtVector: |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 863 | case Type::Vector: { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 864 | const VectorType *VT = cast<VectorType>(T); |
| 865 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType()); |
| 866 | Width = EltInfo.first*VT->getNumElements(); |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 867 | Align = Width; |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 868 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 869 | // This happens for non-power-of-2 length vectors. |
Dan Gohman | 8eefcd3 | 2010-04-21 23:32:43 +0000 | [diff] [blame] | 870 | if (Align & (Align-1)) { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 871 | Align = llvm::NextPowerOf2(Align); |
| 872 | Width = llvm::RoundUpToAlignment(Width, Align); |
| 873 | } |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 874 | break; |
| 875 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 876 | |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 877 | case Type::Builtin: |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 878 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 879 | default: llvm_unreachable("Unknown builtin type!"); |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 880 | case BuiltinType::Void: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 881 | // GCC extension: alignof(void) = 8 bits. |
| 882 | Width = 0; |
| 883 | Align = 8; |
| 884 | break; |
| 885 | |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 886 | case BuiltinType::Bool: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 887 | Width = Target->getBoolWidth(); |
| 888 | Align = Target->getBoolAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 889 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 890 | case BuiltinType::Char_S: |
| 891 | case BuiltinType::Char_U: |
| 892 | case BuiltinType::UChar: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 893 | case BuiltinType::SChar: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 894 | Width = Target->getCharWidth(); |
| 895 | Align = Target->getCharAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 896 | break; |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 897 | case BuiltinType::WChar_S: |
| 898 | case BuiltinType::WChar_U: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 899 | Width = Target->getWCharWidth(); |
| 900 | Align = Target->getWCharAlign(); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 901 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 902 | case BuiltinType::Char16: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 903 | Width = Target->getChar16Width(); |
| 904 | Align = Target->getChar16Align(); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 905 | break; |
| 906 | case BuiltinType::Char32: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 907 | Width = Target->getChar32Width(); |
| 908 | Align = Target->getChar32Align(); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 909 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 910 | case BuiltinType::UShort: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 911 | case BuiltinType::Short: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 912 | Width = Target->getShortWidth(); |
| 913 | Align = Target->getShortAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 914 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 915 | case BuiltinType::UInt: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 916 | case BuiltinType::Int: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 917 | Width = Target->getIntWidth(); |
| 918 | Align = Target->getIntAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 919 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 920 | case BuiltinType::ULong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 921 | case BuiltinType::Long: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 922 | Width = Target->getLongWidth(); |
| 923 | Align = Target->getLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 924 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 925 | case BuiltinType::ULongLong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 926 | case BuiltinType::LongLong: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 927 | Width = Target->getLongLongWidth(); |
| 928 | Align = Target->getLongLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 929 | break; |
Chris Lattner | ec16cb9 | 2009-04-30 02:55:13 +0000 | [diff] [blame] | 930 | case BuiltinType::Int128: |
| 931 | case BuiltinType::UInt128: |
| 932 | Width = 128; |
| 933 | Align = 128; // int128_t is 128-bit aligned on all targets. |
| 934 | break; |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 935 | case BuiltinType::Half: |
| 936 | Width = Target->getHalfWidth(); |
| 937 | Align = Target->getHalfAlign(); |
| 938 | break; |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 939 | case BuiltinType::Float: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 940 | Width = Target->getFloatWidth(); |
| 941 | Align = Target->getFloatAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 942 | break; |
| 943 | case BuiltinType::Double: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 944 | Width = Target->getDoubleWidth(); |
| 945 | Align = Target->getDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 946 | break; |
| 947 | case BuiltinType::LongDouble: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 948 | Width = Target->getLongDoubleWidth(); |
| 949 | Align = Target->getLongDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 950 | break; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 951 | case BuiltinType::NullPtr: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 952 | Width = Target->getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) |
| 953 | Align = Target->getPointerAlign(0); // == sizeof(void*) |
Sebastian Redl | 1590d9c | 2009-05-27 19:34:06 +0000 | [diff] [blame] | 954 | break; |
Fariborz Jahanian | e04f5fc | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 955 | case BuiltinType::ObjCId: |
| 956 | case BuiltinType::ObjCClass: |
| 957 | case BuiltinType::ObjCSel: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 958 | Width = Target->getPointerWidth(0); |
| 959 | Align = Target->getPointerAlign(0); |
Fariborz Jahanian | e04f5fc | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 960 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 961 | } |
Chris Lattner | bfef6d7 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 962 | break; |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 963 | case Type::ObjCObjectPointer: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 964 | Width = Target->getPointerWidth(0); |
| 965 | Align = Target->getPointerAlign(0); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 966 | break; |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 967 | case Type::BlockPointer: { |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 968 | unsigned AS = getTargetAddressSpace( |
| 969 | cast<BlockPointerType>(T)->getPointeeType()); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 970 | Width = Target->getPointerWidth(AS); |
| 971 | Align = Target->getPointerAlign(AS); |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 972 | break; |
| 973 | } |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 974 | case Type::LValueReference: |
| 975 | case Type::RValueReference: { |
| 976 | // alignof and sizeof should never enter this code path here, so we go |
| 977 | // the pointer route. |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 978 | unsigned AS = getTargetAddressSpace( |
| 979 | cast<ReferenceType>(T)->getPointeeType()); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 980 | Width = Target->getPointerWidth(AS); |
| 981 | Align = Target->getPointerAlign(AS); |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 982 | break; |
| 983 | } |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 984 | case Type::Pointer: { |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 985 | unsigned AS = getTargetAddressSpace(cast<PointerType>(T)->getPointeeType()); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 986 | Width = Target->getPointerWidth(AS); |
| 987 | Align = Target->getPointerAlign(AS); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 988 | break; |
| 989 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 990 | case Type::MemberPointer: { |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 991 | const MemberPointerType *MPT = cast<MemberPointerType>(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 992 | std::pair<uint64_t, unsigned> PtrDiffInfo = |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 993 | getTypeInfo(getPointerDiffType()); |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 994 | Width = PtrDiffInfo.first * ABI->getMemberPointerSize(MPT); |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 995 | Align = PtrDiffInfo.second; |
| 996 | break; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 997 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 998 | case Type::Complex: { |
| 999 | // Complex types have the same alignment as their elements, but twice the |
| 1000 | // size. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1001 | std::pair<uint64_t, unsigned> EltInfo = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1002 | getTypeInfo(cast<ComplexType>(T)->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1003 | Width = EltInfo.first*2; |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1004 | Align = EltInfo.second; |
| 1005 | break; |
| 1006 | } |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1007 | case Type::ObjCObject: |
| 1008 | return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr()); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1009 | case Type::ObjCInterface: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1010 | const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1011 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1012 | Width = toBits(Layout.getSize()); |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 1013 | Align = toBits(Layout.getAlignment()); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1014 | break; |
| 1015 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1016 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1017 | case Type::Enum: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1018 | const TagType *TT = cast<TagType>(T); |
| 1019 | |
| 1020 | if (TT->getDecl()->isInvalidDecl()) { |
Douglas Gregor | 22ce41d | 2011-04-20 17:29:44 +0000 | [diff] [blame] | 1021 | Width = 8; |
| 1022 | Align = 8; |
Chris Lattner | 8389eab | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 1023 | break; |
| 1024 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1025 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1026 | if (const EnumType *ET = dyn_cast<EnumType>(TT)) |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1027 | return getTypeInfo(ET->getDecl()->getIntegerType()); |
| 1028 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1029 | const RecordType *RT = cast<RecordType>(TT); |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1030 | const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1031 | Width = toBits(Layout.getSize()); |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 1032 | Align = toBits(Layout.getAlignment()); |
Chris Lattner | dc0d73e | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 1033 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1034 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1035 | |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1036 | case Type::SubstTemplateTypeParm: |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1037 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> |
| 1038 | getReplacementType().getTypePtr()); |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1039 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1040 | case Type::Auto: { |
| 1041 | const AutoType *A = cast<AutoType>(T); |
| 1042 | assert(A->isDeduced() && "Cannot request the size of a dependent type"); |
Matt Beaumont-Gay | dc856af | 2011-02-22 20:00:16 +0000 | [diff] [blame] | 1043 | return getTypeInfo(A->getDeducedType().getTypePtr()); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1044 | } |
| 1045 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1046 | case Type::Paren: |
| 1047 | return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr()); |
| 1048 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1049 | case Type::Typedef: { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1050 | const TypedefNameDecl *Typedef = cast<TypedefType>(T)->getDecl(); |
Douglas Gregor | df1367a | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 1051 | std::pair<uint64_t, unsigned> Info |
| 1052 | = getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); |
Chris Lattner | c1de52d | 2011-02-19 22:55:41 +0000 | [diff] [blame] | 1053 | // If the typedef has an aligned attribute on it, it overrides any computed |
| 1054 | // alignment we have. This violates the GCC documentation (which says that |
| 1055 | // attribute(aligned) can only round up) but matches its implementation. |
| 1056 | if (unsigned AttrAlign = Typedef->getMaxAlignment()) |
| 1057 | Align = AttrAlign; |
| 1058 | else |
| 1059 | Align = Info.second; |
Douglas Gregor | df1367a | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 1060 | Width = Info.first; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1061 | break; |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1062 | } |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1063 | |
| 1064 | case Type::TypeOfExpr: |
| 1065 | return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType() |
| 1066 | .getTypePtr()); |
| 1067 | |
| 1068 | case Type::TypeOf: |
| 1069 | return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr()); |
| 1070 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 1071 | case Type::Decltype: |
| 1072 | return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType() |
| 1073 | .getTypePtr()); |
| 1074 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1075 | case Type::UnaryTransform: |
| 1076 | return getTypeInfo(cast<UnaryTransformType>(T)->getUnderlyingType()); |
| 1077 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1078 | case Type::Elaborated: |
| 1079 | return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1080 | |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 1081 | case Type::Attributed: |
| 1082 | return getTypeInfo( |
| 1083 | cast<AttributedType>(T)->getEquivalentType().getTypePtr()); |
| 1084 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1085 | case Type::TemplateSpecialization: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1086 | assert(getCanonicalType(T) != T && |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1087 | "Cannot request the size of a dependent type"); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1088 | const TemplateSpecializationType *TST = cast<TemplateSpecializationType>(T); |
| 1089 | // A type alias template specialization may refer to a typedef with the |
| 1090 | // aligned attribute on it. |
| 1091 | if (TST->isTypeAlias()) |
| 1092 | return getTypeInfo(TST->getAliasedType().getTypePtr()); |
| 1093 | else |
| 1094 | return getTypeInfo(getCanonicalType(T)); |
| 1095 | } |
| 1096 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1097 | case Type::Atomic: { |
Eli Friedman | 2be4607 | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 1098 | std::pair<uint64_t, unsigned> Info |
| 1099 | = getTypeInfo(cast<AtomicType>(T)->getValueType()); |
| 1100 | Width = Info.first; |
| 1101 | Align = Info.second; |
| 1102 | if (Width != 0 && Width <= Target->getMaxAtomicPromoteWidth() && |
| 1103 | llvm::isPowerOf2_64(Width)) { |
| 1104 | // We can potentially perform lock-free atomic operations for this |
| 1105 | // type; promote the alignment appropriately. |
| 1106 | // FIXME: We could potentially promote the width here as well... |
| 1107 | // is that worthwhile? (Non-struct atomic types generally have |
| 1108 | // power-of-two size anyway, but structs might not. Requires a bit |
| 1109 | // of implementation work to make sure we zero out the extra bits.) |
| 1110 | Align = static_cast<unsigned>(Width); |
| 1111 | } |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1114 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1115 | |
Eli Friedman | 2be4607 | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 1116 | assert(llvm::isPowerOf2_32(Align) && "Alignment must be power of 2"); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1117 | return std::make_pair(Width, Align); |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1120 | /// toCharUnitsFromBits - Convert a size in bits to a size in characters. |
| 1121 | CharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const { |
| 1122 | return CharUnits::fromQuantity(BitSize / getCharWidth()); |
| 1123 | } |
| 1124 | |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1125 | /// toBits - Convert a size in characters to a size in characters. |
| 1126 | int64_t ASTContext::toBits(CharUnits CharSize) const { |
| 1127 | return CharSize.getQuantity() * getCharWidth(); |
| 1128 | } |
| 1129 | |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1130 | /// getTypeSizeInChars - Return the size of the specified type, in characters. |
| 1131 | /// This method does not work on incomplete types. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1132 | CharUnits ASTContext::getTypeSizeInChars(QualType T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1133 | return toCharUnitsFromBits(getTypeSize(T)); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1134 | } |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1135 | CharUnits ASTContext::getTypeSizeInChars(const Type *T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1136 | return toCharUnitsFromBits(getTypeSize(T)); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
Ken Dyck | 16e20cc | 2010-01-26 17:25:18 +0000 | [diff] [blame] | 1139 | /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1140 | /// characters. This method does not work on incomplete types. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1141 | CharUnits ASTContext::getTypeAlignInChars(QualType T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1142 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1143 | } |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1144 | CharUnits ASTContext::getTypeAlignInChars(const Type *T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1145 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1148 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 1149 | /// type for the current target in bits. This can be different than the ABI |
| 1150 | /// alignment in cases where it is beneficial for performance to overalign |
| 1151 | /// a data type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1152 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) const { |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1153 | unsigned ABIAlign = getTypeAlign(T); |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1154 | |
| 1155 | // Double and long long should be naturally aligned if possible. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1156 | if (const ComplexType* CT = T->getAs<ComplexType>()) |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1157 | T = CT->getElementType().getTypePtr(); |
| 1158 | if (T->isSpecificBuiltinType(BuiltinType::Double) || |
| 1159 | T->isSpecificBuiltinType(BuiltinType::LongLong)) |
| 1160 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); |
| 1161 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1162 | return ABIAlign; |
| 1163 | } |
| 1164 | |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1165 | /// DeepCollectObjCIvars - |
| 1166 | /// This routine first collects all declared, but not synthesized, ivars in |
| 1167 | /// super class and then collects all ivars, including those synthesized for |
| 1168 | /// current class. This routine is used for implementation of current class |
| 1169 | /// when all ivars, declared and synthesized are known. |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 1170 | /// |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1171 | void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 1172 | bool leafClass, |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 1173 | SmallVectorImpl<const ObjCIvarDecl*> &Ivars) const { |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1174 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
| 1175 | DeepCollectObjCIvars(SuperClass, false, Ivars); |
| 1176 | if (!leafClass) { |
| 1177 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 1178 | E = OI->ivar_end(); I != E; ++I) |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 1179 | Ivars.push_back(*I); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 1180 | } else { |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 1181 | ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI); |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 1182 | for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv; |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 1183 | Iv= Iv->getNextIvar()) |
| 1184 | Ivars.push_back(Iv); |
| 1185 | } |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1188 | /// CollectInheritedProtocols - Collect all protocols in current class and |
| 1189 | /// those inherited by it. |
| 1190 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 1191 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1192 | if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1193 | // We can use protocol_iterator here instead of |
| 1194 | // all_referenced_protocol_iterator since we are walking all categories. |
| 1195 | for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(), |
| 1196 | PE = OI->all_referenced_protocol_end(); P != PE; ++P) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1197 | ObjCProtocolDecl *Proto = (*P); |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1198 | Protocols.insert(Proto->getCanonicalDecl()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1199 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 1200 | PE = Proto->protocol_end(); P != PE; ++P) { |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1201 | Protocols.insert((*P)->getCanonicalDecl()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1202 | CollectInheritedProtocols(*P, Protocols); |
| 1203 | } |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 1204 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1205 | |
| 1206 | // Categories of this Interface. |
| 1207 | for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList(); |
| 1208 | CDeclChain; CDeclChain = CDeclChain->getNextClassCategory()) |
| 1209 | CollectInheritedProtocols(CDeclChain, Protocols); |
| 1210 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) |
| 1211 | while (SD) { |
| 1212 | CollectInheritedProtocols(SD, Protocols); |
| 1213 | SD = SD->getSuperClass(); |
| 1214 | } |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1215 | } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1216 | for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(), |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1217 | PE = OC->protocol_end(); P != PE; ++P) { |
| 1218 | ObjCProtocolDecl *Proto = (*P); |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1219 | Protocols.insert(Proto->getCanonicalDecl()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1220 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 1221 | PE = Proto->protocol_end(); P != PE; ++P) |
| 1222 | CollectInheritedProtocols(*P, Protocols); |
| 1223 | } |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1224 | } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1225 | for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(), |
| 1226 | PE = OP->protocol_end(); P != PE; ++P) { |
| 1227 | ObjCProtocolDecl *Proto = (*P); |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1228 | Protocols.insert(Proto->getCanonicalDecl()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1229 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 1230 | PE = Proto->protocol_end(); P != PE; ++P) |
| 1231 | CollectInheritedProtocols(*P, Protocols); |
| 1232 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1233 | } |
| 1234 | } |
| 1235 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1236 | unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const { |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1237 | unsigned count = 0; |
| 1238 | // Count ivars declared in class extension. |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 1239 | for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl; |
| 1240 | CDecl = CDecl->getNextClassExtension()) |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1241 | count += CDecl->ivar_size(); |
| 1242 | |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1243 | // Count ivar defined in this class's implementation. This |
| 1244 | // includes synthesized ivars. |
| 1245 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1246 | count += ImplDecl->ivar_size(); |
| 1247 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 1248 | return count; |
| 1249 | } |
| 1250 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1251 | /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. |
| 1252 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
| 1253 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1254 | I = ObjCImpls.find(D); |
| 1255 | if (I != ObjCImpls.end()) |
| 1256 | return cast<ObjCImplementationDecl>(I->second); |
| 1257 | return 0; |
| 1258 | } |
| 1259 | /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. |
| 1260 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
| 1261 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1262 | I = ObjCImpls.find(D); |
| 1263 | if (I != ObjCImpls.end()) |
| 1264 | return cast<ObjCCategoryImplDecl>(I->second); |
| 1265 | return 0; |
| 1266 | } |
| 1267 | |
| 1268 | /// \brief Set the implementation of ObjCInterfaceDecl. |
| 1269 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
| 1270 | ObjCImplementationDecl *ImplD) { |
| 1271 | assert(IFaceD && ImplD && "Passed null params"); |
| 1272 | ObjCImpls[IFaceD] = ImplD; |
| 1273 | } |
| 1274 | /// \brief Set the implementation of ObjCCategoryDecl. |
| 1275 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
| 1276 | ObjCCategoryImplDecl *ImplD) { |
| 1277 | assert(CatD && ImplD && "Passed null params"); |
| 1278 | ObjCImpls[CatD] = ImplD; |
| 1279 | } |
| 1280 | |
Argyrios Kyrtzidis | 87ec9c2 | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 1281 | ObjCInterfaceDecl *ASTContext::getObjContainingInterface(NamedDecl *ND) const { |
| 1282 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ND->getDeclContext())) |
| 1283 | return ID; |
| 1284 | if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(ND->getDeclContext())) |
| 1285 | return CD->getClassInterface(); |
| 1286 | if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(ND->getDeclContext())) |
| 1287 | return IMD->getClassInterface(); |
| 1288 | |
| 1289 | return 0; |
| 1290 | } |
| 1291 | |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1292 | /// \brief Get the copy initialization expression of VarDecl,or NULL if |
| 1293 | /// none exists. |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1294 | Expr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) { |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1295 | assert(VD && "Passed null params"); |
| 1296 | assert(VD->hasAttr<BlocksAttr>() && |
| 1297 | "getBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1298 | llvm::DenseMap<const VarDecl*, Expr*>::iterator |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1299 | I = BlockVarCopyInits.find(VD); |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1300 | return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0; |
| 1301 | } |
| 1302 | |
| 1303 | /// \brief Set the copy inialization expression of a block var decl. |
| 1304 | void ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) { |
| 1305 | assert(VD && Init && "Passed null params"); |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1306 | assert(VD->hasAttr<BlocksAttr>() && |
| 1307 | "setBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1308 | BlockVarCopyInits[VD] = Init; |
| 1309 | } |
| 1310 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1311 | /// \brief Allocate an uninitialized TypeSourceInfo. |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1312 | /// |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1313 | /// The caller should initialize the memory held by TypeSourceInfo using |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1314 | /// the TypeLoc wrappers. |
| 1315 | /// |
| 1316 | /// \param T the type that will be the basis for type source info. This type |
| 1317 | /// should refer to how the declarator was written in source code, not to |
| 1318 | /// what type semantic analysis resolved the declarator to. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1319 | TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1320 | unsigned DataSize) const { |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1321 | if (!DataSize) |
| 1322 | DataSize = TypeLoc::getFullDataSizeForType(T); |
| 1323 | else |
| 1324 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1325 | "incorrect data size provided to CreateTypeSourceInfo!"); |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1326 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1327 | TypeSourceInfo *TInfo = |
| 1328 | (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8); |
| 1329 | new (TInfo) TypeSourceInfo(T); |
| 1330 | return TInfo; |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1333 | TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1334 | SourceLocation L) const { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1335 | TypeSourceInfo *DI = CreateTypeSourceInfo(T); |
Douglas Gregor | c21c7e9 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 1336 | DI->getTypeLoc().initialize(const_cast<ASTContext &>(*this), L); |
John McCall | a4eb74d | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 1337 | return DI; |
| 1338 | } |
| 1339 | |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1340 | const ASTRecordLayout & |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1341 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const { |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1342 | return getObjCLayout(D, 0); |
| 1343 | } |
| 1344 | |
| 1345 | const ASTRecordLayout & |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1346 | ASTContext::getASTObjCImplementationLayout( |
| 1347 | const ObjCImplementationDecl *D) const { |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1348 | return getObjCLayout(D->getClassInterface(), D); |
| 1349 | } |
| 1350 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1351 | //===----------------------------------------------------------------------===// |
| 1352 | // Type creation/memoization methods |
| 1353 | //===----------------------------------------------------------------------===// |
| 1354 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1355 | QualType |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1356 | ASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const { |
| 1357 | unsigned fastQuals = quals.getFastQualifiers(); |
| 1358 | quals.removeFastQualifiers(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1359 | |
| 1360 | // Check if we've already instantiated this type. |
| 1361 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1362 | ExtQuals::Profile(ID, baseType, quals); |
| 1363 | void *insertPos = 0; |
| 1364 | if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, insertPos)) { |
| 1365 | assert(eq->getQualifiers() == quals); |
| 1366 | return QualType(eq, fastQuals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1369 | // If the base type is not canonical, make the appropriate canonical type. |
| 1370 | QualType canon; |
| 1371 | if (!baseType->isCanonicalUnqualified()) { |
| 1372 | SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split(); |
| 1373 | canonSplit.second.addConsistentQualifiers(quals); |
| 1374 | canon = getExtQualType(canonSplit.first, canonSplit.second); |
| 1375 | |
| 1376 | // Re-find the insert position. |
| 1377 | (void) ExtQualNodes.FindNodeOrInsertPos(ID, insertPos); |
| 1378 | } |
| 1379 | |
| 1380 | ExtQuals *eq = new (*this, TypeAlignment) ExtQuals(baseType, canon, quals); |
| 1381 | ExtQualNodes.InsertNode(eq, insertPos); |
| 1382 | return QualType(eq, fastQuals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1385 | QualType |
| 1386 | ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) const { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1387 | QualType CanT = getCanonicalType(T); |
| 1388 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1389 | return T; |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1390 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1391 | // If we are composing extended qualifiers together, merge together |
| 1392 | // into one ExtQuals node. |
| 1393 | QualifierCollector Quals; |
| 1394 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1395 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1396 | // If this type already has an address space specified, it cannot get |
| 1397 | // another one. |
| 1398 | assert(!Quals.hasAddressSpace() && |
| 1399 | "Type cannot be in multiple addr spaces!"); |
| 1400 | Quals.addAddressSpace(AddressSpace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1401 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1402 | return getExtQualType(TypeNode, Quals); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1405 | QualType ASTContext::getObjCGCQualType(QualType T, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1406 | Qualifiers::GC GCAttr) const { |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1407 | QualType CanT = getCanonicalType(T); |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1408 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1409 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1410 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1411 | if (const PointerType *ptr = T->getAs<PointerType>()) { |
| 1412 | QualType Pointee = ptr->getPointeeType(); |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 1413 | if (Pointee->isAnyPointerType()) { |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1414 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 1415 | return getPointerType(ResultType); |
| 1416 | } |
| 1417 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1418 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1419 | // If we are composing extended qualifiers together, merge together |
| 1420 | // into one ExtQuals node. |
| 1421 | QualifierCollector Quals; |
| 1422 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1423 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1424 | // If this type already has an ObjCGC specified, it cannot get |
| 1425 | // another one. |
| 1426 | assert(!Quals.hasObjCGCAttr() && |
| 1427 | "Type cannot have multiple ObjCGCs!"); |
| 1428 | Quals.addObjCGCAttr(GCAttr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1429 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1430 | return getExtQualType(TypeNode, Quals); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1431 | } |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1432 | |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 1433 | const FunctionType *ASTContext::adjustFunctionType(const FunctionType *T, |
| 1434 | FunctionType::ExtInfo Info) { |
| 1435 | if (T->getExtInfo() == Info) |
| 1436 | return T; |
| 1437 | |
| 1438 | QualType Result; |
| 1439 | if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) { |
| 1440 | Result = getFunctionNoProtoType(FNPT->getResultType(), Info); |
| 1441 | } else { |
| 1442 | const FunctionProtoType *FPT = cast<FunctionProtoType>(T); |
| 1443 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 1444 | EPI.ExtInfo = Info; |
| 1445 | Result = getFunctionType(FPT->getResultType(), FPT->arg_type_begin(), |
| 1446 | FPT->getNumArgs(), EPI); |
| 1447 | } |
| 1448 | |
| 1449 | return cast<FunctionType>(Result.getTypePtr()); |
| 1450 | } |
| 1451 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1452 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 1453 | /// number with the specified element type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1454 | QualType ASTContext::getComplexType(QualType T) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1455 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1456 | // structure. |
| 1457 | llvm::FoldingSetNodeID ID; |
| 1458 | ComplexType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1459 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1460 | void *InsertPos = 0; |
| 1461 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1462 | return QualType(CT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1463 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1464 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1465 | // so fill in the canonical type field. |
| 1466 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1467 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1468 | Canonical = getComplexType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1469 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1470 | // Get the new insert position for the node we care about. |
| 1471 | ComplexType *NewIP = ComplexTypes.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; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1473 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1474 | ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1475 | Types.push_back(New); |
| 1476 | ComplexTypes.InsertNode(New, InsertPos); |
| 1477 | return QualType(New, 0); |
| 1478 | } |
| 1479 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1480 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 1481 | /// the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1482 | QualType ASTContext::getPointerType(QualType T) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1483 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1484 | // structure. |
| 1485 | llvm::FoldingSetNodeID ID; |
| 1486 | PointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1487 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1488 | void *InsertPos = 0; |
| 1489 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1490 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1491 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1492 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1493 | // so fill in the canonical type field. |
| 1494 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1495 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1496 | Canonical = getPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1497 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1498 | // Get the new insert position for the node we care about. |
| 1499 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1500 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1501 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1502 | PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1503 | Types.push_back(New); |
| 1504 | PointerTypes.InsertNode(New, InsertPos); |
| 1505 | return QualType(New, 0); |
| 1506 | } |
| 1507 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1508 | /// getBlockPointerType - Return the uniqued reference to the type for |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1509 | /// a pointer to the specified block. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1510 | QualType ASTContext::getBlockPointerType(QualType T) const { |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1511 | assert(T->isFunctionType() && "block of function types only"); |
| 1512 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1513 | // structure. |
| 1514 | llvm::FoldingSetNodeID ID; |
| 1515 | BlockPointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1516 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1517 | void *InsertPos = 0; |
| 1518 | if (BlockPointerType *PT = |
| 1519 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1520 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1521 | |
| 1522 | // 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] | 1523 | // type either so fill in the canonical type field. |
| 1524 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1525 | if (!T.isCanonical()) { |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1526 | Canonical = getBlockPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1527 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1528 | // Get the new insert position for the node we care about. |
| 1529 | BlockPointerType *NewIP = |
| 1530 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1531 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1532 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1533 | BlockPointerType *New |
| 1534 | = new (*this, TypeAlignment) BlockPointerType(T, Canonical); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1535 | Types.push_back(New); |
| 1536 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 1537 | return QualType(New, 0); |
| 1538 | } |
| 1539 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1540 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 1541 | /// lvalue reference to the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1542 | QualType |
| 1543 | ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const { |
Douglas Gregor | 9625e44 | 2011-05-21 22:16:50 +0000 | [diff] [blame] | 1544 | assert(getCanonicalType(T) != OverloadTy && |
| 1545 | "Unresolved overloaded function type"); |
| 1546 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1547 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1548 | // structure. |
| 1549 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1550 | ReferenceType::Profile(ID, T, SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1551 | |
| 1552 | void *InsertPos = 0; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1553 | if (LValueReferenceType *RT = |
| 1554 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1555 | return QualType(RT, 0); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1556 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1557 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1558 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1559 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1560 | // either, so fill in the canonical type field. |
| 1561 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1562 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
| 1563 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1564 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1565 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1566 | // Get the new insert position for the node we care about. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1567 | LValueReferenceType *NewIP = |
| 1568 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1569 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1572 | LValueReferenceType *New |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1573 | = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, |
| 1574 | SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1575 | Types.push_back(New); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1576 | LValueReferenceTypes.InsertNode(New, InsertPos); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1577 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1578 | return QualType(New, 0); |
| 1579 | } |
| 1580 | |
| 1581 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 1582 | /// rvalue reference to the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1583 | QualType ASTContext::getRValueReferenceType(QualType T) const { |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1584 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1585 | // structure. |
| 1586 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1587 | ReferenceType::Profile(ID, T, false); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1588 | |
| 1589 | void *InsertPos = 0; |
| 1590 | if (RValueReferenceType *RT = |
| 1591 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1592 | return QualType(RT, 0); |
| 1593 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1594 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1595 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1596 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1597 | // either, so fill in the canonical type field. |
| 1598 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1599 | if (InnerRef || !T.isCanonical()) { |
| 1600 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1601 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1602 | |
| 1603 | // Get the new insert position for the node we care about. |
| 1604 | RValueReferenceType *NewIP = |
| 1605 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1606 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1607 | } |
| 1608 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1609 | RValueReferenceType *New |
| 1610 | = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1611 | Types.push_back(New); |
| 1612 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1613 | return QualType(New, 0); |
| 1614 | } |
| 1615 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1616 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 1617 | /// member pointer to the specified type, in the specified class. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1618 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1619 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1620 | // structure. |
| 1621 | llvm::FoldingSetNodeID ID; |
| 1622 | MemberPointerType::Profile(ID, T, Cls); |
| 1623 | |
| 1624 | void *InsertPos = 0; |
| 1625 | if (MemberPointerType *PT = |
| 1626 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1627 | return QualType(PT, 0); |
| 1628 | |
| 1629 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 1630 | // type either, so fill in the canonical type field. |
| 1631 | QualType Canonical; |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1632 | if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1633 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 1634 | |
| 1635 | // Get the new insert position for the node we care about. |
| 1636 | MemberPointerType *NewIP = |
| 1637 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1638 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1639 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1640 | MemberPointerType *New |
| 1641 | = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1642 | Types.push_back(New); |
| 1643 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 1644 | return QualType(New, 0); |
| 1645 | } |
| 1646 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1647 | /// getConstantArrayType - Return the unique reference to the type for an |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1648 | /// array of the specified element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1649 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1650 | const llvm::APInt &ArySizeIn, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1651 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1652 | unsigned IndexTypeQuals) const { |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1653 | assert((EltTy->isDependentType() || |
| 1654 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && |
Eli Friedman | 587cbdf | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1655 | "Constant array of VLAs is illegal!"); |
| 1656 | |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1657 | // Convert the array size into a canonical width matching the pointer size for |
| 1658 | // the target. |
| 1659 | llvm::APInt ArySize(ArySizeIn); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1660 | ArySize = |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1661 | ArySize.zextOrTrunc(Target->getPointerWidth(getTargetAddressSpace(EltTy))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1662 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1663 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1664 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, IndexTypeQuals); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1665 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1666 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1667 | if (ConstantArrayType *ATP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1668 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1669 | return QualType(ATP, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1670 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1671 | // If the element type isn't canonical or has qualifiers, this won't |
| 1672 | // be a canonical type either, so fill in the canonical type field. |
| 1673 | QualType Canon; |
| 1674 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 1675 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
| 1676 | Canon = getConstantArrayType(QualType(canonSplit.first, 0), ArySize, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1677 | ASM, IndexTypeQuals); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1678 | Canon = getQualifiedType(Canon, canonSplit.second); |
| 1679 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1680 | // Get the new insert position for the node we care about. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1681 | ConstantArrayType *NewIP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1682 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1683 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1684 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1685 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1686 | ConstantArrayType *New = new(*this,TypeAlignment) |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1687 | ConstantArrayType(EltTy, Canon, ArySize, ASM, IndexTypeQuals); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1688 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1689 | Types.push_back(New); |
| 1690 | return QualType(New, 0); |
| 1691 | } |
| 1692 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1693 | /// getVariableArrayDecayedType - Turns the given type, which may be |
| 1694 | /// variably-modified, into the corresponding type with all the known |
| 1695 | /// sizes replaced with [*]. |
| 1696 | QualType ASTContext::getVariableArrayDecayedType(QualType type) const { |
| 1697 | // Vastly most common case. |
| 1698 | if (!type->isVariablyModifiedType()) return type; |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1699 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1700 | QualType result; |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1701 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1702 | SplitQualType split = type.getSplitDesugaredType(); |
| 1703 | const Type *ty = split.first; |
| 1704 | switch (ty->getTypeClass()) { |
| 1705 | #define TYPE(Class, Base) |
| 1706 | #define ABSTRACT_TYPE(Class, Base) |
| 1707 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 1708 | #include "clang/AST/TypeNodes.def" |
| 1709 | llvm_unreachable("didn't desugar past all non-canonical types?"); |
| 1710 | |
| 1711 | // These types should never be variably-modified. |
| 1712 | case Type::Builtin: |
| 1713 | case Type::Complex: |
| 1714 | case Type::Vector: |
| 1715 | case Type::ExtVector: |
| 1716 | case Type::DependentSizedExtVector: |
| 1717 | case Type::ObjCObject: |
| 1718 | case Type::ObjCInterface: |
| 1719 | case Type::ObjCObjectPointer: |
| 1720 | case Type::Record: |
| 1721 | case Type::Enum: |
| 1722 | case Type::UnresolvedUsing: |
| 1723 | case Type::TypeOfExpr: |
| 1724 | case Type::TypeOf: |
| 1725 | case Type::Decltype: |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1726 | case Type::UnaryTransform: |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1727 | case Type::DependentName: |
| 1728 | case Type::InjectedClassName: |
| 1729 | case Type::TemplateSpecialization: |
| 1730 | case Type::DependentTemplateSpecialization: |
| 1731 | case Type::TemplateTypeParm: |
| 1732 | case Type::SubstTemplateTypeParmPack: |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1733 | case Type::Auto: |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1734 | case Type::PackExpansion: |
| 1735 | llvm_unreachable("type should never be variably-modified"); |
| 1736 | |
| 1737 | // These types can be variably-modified but should never need to |
| 1738 | // further decay. |
| 1739 | case Type::FunctionNoProto: |
| 1740 | case Type::FunctionProto: |
| 1741 | case Type::BlockPointer: |
| 1742 | case Type::MemberPointer: |
| 1743 | return type; |
| 1744 | |
| 1745 | // These types can be variably-modified. All these modifications |
| 1746 | // preserve structure except as noted by comments. |
| 1747 | // TODO: if we ever care about optimizing VLAs, there are no-op |
| 1748 | // optimizations available here. |
| 1749 | case Type::Pointer: |
| 1750 | result = getPointerType(getVariableArrayDecayedType( |
| 1751 | cast<PointerType>(ty)->getPointeeType())); |
| 1752 | break; |
| 1753 | |
| 1754 | case Type::LValueReference: { |
| 1755 | const LValueReferenceType *lv = cast<LValueReferenceType>(ty); |
| 1756 | result = getLValueReferenceType( |
| 1757 | getVariableArrayDecayedType(lv->getPointeeType()), |
| 1758 | lv->isSpelledAsLValue()); |
| 1759 | break; |
| 1760 | } |
| 1761 | |
| 1762 | case Type::RValueReference: { |
| 1763 | const RValueReferenceType *lv = cast<RValueReferenceType>(ty); |
| 1764 | result = getRValueReferenceType( |
| 1765 | getVariableArrayDecayedType(lv->getPointeeType())); |
| 1766 | break; |
| 1767 | } |
| 1768 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1769 | case Type::Atomic: { |
| 1770 | const AtomicType *at = cast<AtomicType>(ty); |
| 1771 | result = getAtomicType(getVariableArrayDecayedType(at->getValueType())); |
| 1772 | break; |
| 1773 | } |
| 1774 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1775 | case Type::ConstantArray: { |
| 1776 | const ConstantArrayType *cat = cast<ConstantArrayType>(ty); |
| 1777 | result = getConstantArrayType( |
| 1778 | getVariableArrayDecayedType(cat->getElementType()), |
| 1779 | cat->getSize(), |
| 1780 | cat->getSizeModifier(), |
| 1781 | cat->getIndexTypeCVRQualifiers()); |
| 1782 | break; |
| 1783 | } |
| 1784 | |
| 1785 | case Type::DependentSizedArray: { |
| 1786 | const DependentSizedArrayType *dat = cast<DependentSizedArrayType>(ty); |
| 1787 | result = getDependentSizedArrayType( |
| 1788 | getVariableArrayDecayedType(dat->getElementType()), |
| 1789 | dat->getSizeExpr(), |
| 1790 | dat->getSizeModifier(), |
| 1791 | dat->getIndexTypeCVRQualifiers(), |
| 1792 | dat->getBracketsRange()); |
| 1793 | break; |
| 1794 | } |
| 1795 | |
| 1796 | // Turn incomplete types into [*] types. |
| 1797 | case Type::IncompleteArray: { |
| 1798 | const IncompleteArrayType *iat = cast<IncompleteArrayType>(ty); |
| 1799 | result = getVariableArrayType( |
| 1800 | getVariableArrayDecayedType(iat->getElementType()), |
| 1801 | /*size*/ 0, |
| 1802 | ArrayType::Normal, |
| 1803 | iat->getIndexTypeCVRQualifiers(), |
| 1804 | SourceRange()); |
| 1805 | break; |
| 1806 | } |
| 1807 | |
| 1808 | // Turn VLA types into [*] types. |
| 1809 | case Type::VariableArray: { |
| 1810 | const VariableArrayType *vat = cast<VariableArrayType>(ty); |
| 1811 | result = getVariableArrayType( |
| 1812 | getVariableArrayDecayedType(vat->getElementType()), |
| 1813 | /*size*/ 0, |
| 1814 | ArrayType::Star, |
| 1815 | vat->getIndexTypeCVRQualifiers(), |
| 1816 | vat->getBracketsRange()); |
| 1817 | break; |
| 1818 | } |
| 1819 | } |
| 1820 | |
| 1821 | // Apply the top-level qualifiers from the original. |
| 1822 | return getQualifiedType(result, split.second); |
| 1823 | } |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1824 | |
Steve Naroff | bdbf7b0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 1825 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 1826 | /// variable array of the specified element type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1827 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 1828 | Expr *NumElts, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1829 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1830 | unsigned IndexTypeQuals, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1831 | SourceRange Brackets) const { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1832 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 1833 | // that have an expression provided for their size. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1834 | QualType Canon; |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 1835 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1836 | // Be sure to pull qualifiers off the element type. |
| 1837 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 1838 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
| 1839 | Canon = getVariableArrayType(QualType(canonSplit.first, 0), NumElts, ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1840 | IndexTypeQuals, Brackets); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1841 | Canon = getQualifiedType(Canon, canonSplit.second); |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 1842 | } |
| 1843 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1844 | VariableArrayType *New = new(*this, TypeAlignment) |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1845 | VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1846 | |
| 1847 | VariableArrayTypes.push_back(New); |
| 1848 | Types.push_back(New); |
| 1849 | return QualType(New, 0); |
| 1850 | } |
| 1851 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1852 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 1853 | /// the type for a dependently-sized array of the specified element |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1854 | /// type. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1855 | QualType ASTContext::getDependentSizedArrayType(QualType elementType, |
| 1856 | Expr *numElements, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1857 | ArrayType::ArraySizeModifier ASM, |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1858 | unsigned elementTypeQuals, |
| 1859 | SourceRange brackets) const { |
| 1860 | assert((!numElements || numElements->isTypeDependent() || |
| 1861 | numElements->isValueDependent()) && |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1862 | "Size must be type- or value-dependent!"); |
| 1863 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1864 | // Dependently-sized array types that do not have a specified number |
| 1865 | // of elements will have their sizes deduced from a dependent |
| 1866 | // initializer. We do no canonicalization here at all, which is okay |
| 1867 | // because they can't be used in most locations. |
| 1868 | if (!numElements) { |
| 1869 | DependentSizedArrayType *newType |
| 1870 | = new (*this, TypeAlignment) |
| 1871 | DependentSizedArrayType(*this, elementType, QualType(), |
| 1872 | numElements, ASM, elementTypeQuals, |
| 1873 | brackets); |
| 1874 | Types.push_back(newType); |
| 1875 | return QualType(newType, 0); |
| 1876 | } |
| 1877 | |
| 1878 | // Otherwise, we actually build a new type every time, but we |
| 1879 | // also build a canonical type. |
| 1880 | |
| 1881 | SplitQualType canonElementType = getCanonicalType(elementType).split(); |
| 1882 | |
| 1883 | void *insertPos = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1884 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1885 | DependentSizedArrayType::Profile(ID, *this, |
| 1886 | QualType(canonElementType.first, 0), |
| 1887 | ASM, elementTypeQuals, numElements); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1888 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1889 | // Look for an existing type with these properties. |
| 1890 | DependentSizedArrayType *canonTy = |
| 1891 | DependentSizedArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1892 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1893 | // If we don't have one, build one. |
| 1894 | if (!canonTy) { |
| 1895 | canonTy = new (*this, TypeAlignment) |
| 1896 | DependentSizedArrayType(*this, QualType(canonElementType.first, 0), |
| 1897 | QualType(), numElements, ASM, elementTypeQuals, |
| 1898 | brackets); |
| 1899 | DependentSizedArrayTypes.InsertNode(canonTy, insertPos); |
| 1900 | Types.push_back(canonTy); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1901 | } |
| 1902 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1903 | // Apply qualifiers from the element type to the array. |
| 1904 | QualType canon = getQualifiedType(QualType(canonTy,0), |
| 1905 | canonElementType.second); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1906 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1907 | // If we didn't need extra canonicalization for the element type, |
| 1908 | // then just use that as our result. |
| 1909 | if (QualType(canonElementType.first, 0) == elementType) |
| 1910 | return canon; |
| 1911 | |
| 1912 | // Otherwise, we need to build a type which follows the spelling |
| 1913 | // of the element type. |
| 1914 | DependentSizedArrayType *sugaredType |
| 1915 | = new (*this, TypeAlignment) |
| 1916 | DependentSizedArrayType(*this, elementType, canon, numElements, |
| 1917 | ASM, elementTypeQuals, brackets); |
| 1918 | Types.push_back(sugaredType); |
| 1919 | return QualType(sugaredType, 0); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1920 | } |
| 1921 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1922 | QualType ASTContext::getIncompleteArrayType(QualType elementType, |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1923 | ArrayType::ArraySizeModifier ASM, |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1924 | unsigned elementTypeQuals) const { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1925 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1926 | IncompleteArrayType::Profile(ID, elementType, ASM, elementTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1927 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1928 | void *insertPos = 0; |
| 1929 | if (IncompleteArrayType *iat = |
| 1930 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos)) |
| 1931 | return QualType(iat, 0); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1932 | |
| 1933 | // 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] | 1934 | // either, so fill in the canonical type field. We also have to pull |
| 1935 | // qualifiers off the element type. |
| 1936 | QualType canon; |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1937 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1938 | if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) { |
| 1939 | SplitQualType canonSplit = getCanonicalType(elementType).split(); |
| 1940 | canon = getIncompleteArrayType(QualType(canonSplit.first, 0), |
| 1941 | ASM, elementTypeQuals); |
| 1942 | canon = getQualifiedType(canon, canonSplit.second); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1943 | |
| 1944 | // Get the new insert position for the node we care about. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1945 | IncompleteArrayType *existing = |
| 1946 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
| 1947 | assert(!existing && "Shouldn't be in the map!"); (void) existing; |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1948 | } |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1949 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1950 | IncompleteArrayType *newType = new (*this, TypeAlignment) |
| 1951 | IncompleteArrayType(elementType, canon, ASM, elementTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1952 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1953 | IncompleteArrayTypes.InsertNode(newType, insertPos); |
| 1954 | Types.push_back(newType); |
| 1955 | return QualType(newType, 0); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1956 | } |
| 1957 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1958 | /// getVectorType - Return the unique reference to a vector type of |
| 1959 | /// 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] | 1960 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1961 | VectorType::VectorKind VecKind) const { |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1962 | assert(vecType->isBuiltinType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1963 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1964 | // Check if we've already instantiated a vector of this type. |
| 1965 | llvm::FoldingSetNodeID ID; |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1966 | VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind); |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1967 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1968 | void *InsertPos = 0; |
| 1969 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1970 | return QualType(VTP, 0); |
| 1971 | |
| 1972 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1973 | // so fill in the canonical type field. |
| 1974 | QualType Canonical; |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1975 | if (!vecType.isCanonical()) { |
Bob Wilson | 231da7e | 2010-11-16 00:32:20 +0000 | [diff] [blame] | 1976 | Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1977 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1978 | // Get the new insert position for the node we care about. |
| 1979 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1980 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1981 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1982 | VectorType *New = new (*this, TypeAlignment) |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1983 | VectorType(vecType, NumElts, Canonical, VecKind); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1984 | VectorTypes.InsertNode(New, InsertPos); |
| 1985 | Types.push_back(New); |
| 1986 | return QualType(New, 0); |
| 1987 | } |
| 1988 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1989 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1990 | /// 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] | 1991 | QualType |
| 1992 | ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) const { |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1993 | assert(vecType->isBuiltinType() || vecType->isDependentType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1994 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1995 | // Check if we've already instantiated a vector of this type. |
| 1996 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1997 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1998 | VectorType::GenericVector); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1999 | void *InsertPos = 0; |
| 2000 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2001 | return QualType(VTP, 0); |
| 2002 | |
| 2003 | // If the element type isn't canonical, this won't be a canonical type either, |
| 2004 | // so fill in the canonical type field. |
| 2005 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2006 | if (!vecType.isCanonical()) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2007 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2008 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2009 | // Get the new insert position for the node we care about. |
| 2010 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2011 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2012 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2013 | ExtVectorType *New = new (*this, TypeAlignment) |
| 2014 | ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2015 | VectorTypes.InsertNode(New, InsertPos); |
| 2016 | Types.push_back(New); |
| 2017 | return QualType(New, 0); |
| 2018 | } |
| 2019 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2020 | QualType |
| 2021 | ASTContext::getDependentSizedExtVectorType(QualType vecType, |
| 2022 | Expr *SizeExpr, |
| 2023 | SourceLocation AttrLoc) const { |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2024 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2025 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2026 | SizeExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2027 | |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2028 | void *InsertPos = 0; |
| 2029 | DependentSizedExtVectorType *Canon |
| 2030 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2031 | DependentSizedExtVectorType *New; |
| 2032 | if (Canon) { |
| 2033 | // We already have a canonical version of this array type; use it as |
| 2034 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2035 | New = new (*this, TypeAlignment) |
| 2036 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), |
| 2037 | SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2038 | } else { |
| 2039 | QualType CanonVecTy = getCanonicalType(vecType); |
| 2040 | if (CanonVecTy == vecType) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2041 | New = new (*this, TypeAlignment) |
| 2042 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, |
| 2043 | AttrLoc); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2044 | |
| 2045 | DependentSizedExtVectorType *CanonCheck |
| 2046 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2047 | assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken"); |
| 2048 | (void)CanonCheck; |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2049 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); |
| 2050 | } else { |
| 2051 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 2052 | SourceLocation()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2053 | New = new (*this, TypeAlignment) |
| 2054 | DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2055 | } |
| 2056 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2057 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 2058 | Types.push_back(New); |
| 2059 | return QualType(New, 0); |
| 2060 | } |
| 2061 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2062 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2063 | /// |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2064 | QualType |
| 2065 | ASTContext::getFunctionNoProtoType(QualType ResultTy, |
| 2066 | const FunctionType::ExtInfo &Info) const { |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2067 | const CallingConv DefaultCC = Info.getCC(); |
| 2068 | const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ? |
| 2069 | CC_X86StdCall : DefaultCC; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2070 | // Unique functions, to guarantee there is only one function of a particular |
| 2071 | // structure. |
| 2072 | llvm::FoldingSetNodeID ID; |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2073 | FunctionNoProtoType::Profile(ID, ResultTy, Info); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2074 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2075 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2076 | if (FunctionNoProtoType *FT = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2077 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2078 | return QualType(FT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2079 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2080 | QualType Canonical; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 2081 | if (!ResultTy.isCanonical() || |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2082 | getCanonicalCallConv(CallConv) != CallConv) { |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2083 | Canonical = |
| 2084 | getFunctionNoProtoType(getCanonicalType(ResultTy), |
| 2085 | Info.withCallingConv(getCanonicalCallConv(CallConv))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2086 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2087 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2088 | FunctionNoProtoType *NewIP = |
| 2089 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2090 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2091 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2092 | |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2093 | FunctionProtoType::ExtInfo newInfo = Info.withCallingConv(CallConv); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2094 | FunctionNoProtoType *New = new (*this, TypeAlignment) |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2095 | FunctionNoProtoType(ResultTy, Canonical, newInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2096 | Types.push_back(New); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2097 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2098 | return QualType(New, 0); |
| 2099 | } |
| 2100 | |
| 2101 | /// getFunctionType - Return a normal function type with a typed argument |
| 2102 | /// list. isVariadic indicates whether the argument list includes '...'. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2103 | QualType |
| 2104 | ASTContext::getFunctionType(QualType ResultTy, |
| 2105 | const QualType *ArgArray, unsigned NumArgs, |
| 2106 | const FunctionProtoType::ExtProtoInfo &EPI) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2107 | // Unique functions, to guarantee there is only one function of a particular |
| 2108 | // structure. |
| 2109 | llvm::FoldingSetNodeID ID; |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2110 | FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, EPI, *this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2111 | |
| 2112 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2113 | if (FunctionProtoType *FTP = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2114 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2115 | return QualType(FTP, 0); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2116 | |
| 2117 | // Determine whether the type being created is already canonical or not. |
Sebastian Redl | 8b5b409 | 2011-03-06 10:52:04 +0000 | [diff] [blame] | 2118 | bool isCanonical= EPI.ExceptionSpecType == EST_None && ResultTy.isCanonical(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2119 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2120 | if (!ArgArray[i].isCanonicalAsParam()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2121 | isCanonical = false; |
| 2122 | |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2123 | const CallingConv DefaultCC = EPI.ExtInfo.getCC(); |
| 2124 | const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ? |
| 2125 | CC_X86StdCall : DefaultCC; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2126 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2127 | // If this type isn't canonical, get the canonical version of it. |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2128 | // The exception spec is not part of the canonical type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2129 | QualType Canonical; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2130 | if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2131 | SmallVector<QualType, 16> CanonicalArgs; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2132 | CanonicalArgs.reserve(NumArgs); |
| 2133 | for (unsigned i = 0; i != NumArgs; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2134 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2135 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2136 | FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI; |
Sebastian Redl | 8b5b409 | 2011-03-06 10:52:04 +0000 | [diff] [blame] | 2137 | CanonicalEPI.ExceptionSpecType = EST_None; |
| 2138 | CanonicalEPI.NumExceptions = 0; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2139 | CanonicalEPI.ExtInfo |
| 2140 | = CanonicalEPI.ExtInfo.withCallingConv(getCanonicalCallConv(CallConv)); |
| 2141 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2142 | Canonical = getFunctionType(getCanonicalType(ResultTy), |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2143 | CanonicalArgs.data(), NumArgs, |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2144 | CanonicalEPI); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2145 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2146 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2147 | FunctionProtoType *NewIP = |
| 2148 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2149 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2150 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2151 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2152 | // FunctionProtoType objects are allocated with extra bytes after |
| 2153 | // them for three variable size arrays at the end: |
| 2154 | // - parameter types |
| 2155 | // - exception types |
| 2156 | // - consumed-arguments flags |
| 2157 | // Instead of the exception types, there could be a noexcept |
| 2158 | // expression. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2159 | size_t Size = sizeof(FunctionProtoType) + |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2160 | NumArgs * sizeof(QualType); |
| 2161 | if (EPI.ExceptionSpecType == EST_Dynamic) |
| 2162 | Size += EPI.NumExceptions * sizeof(QualType); |
| 2163 | else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) { |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2164 | Size += sizeof(Expr*); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2165 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2166 | if (EPI.ConsumedArguments) |
| 2167 | Size += NumArgs * sizeof(bool); |
| 2168 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2169 | FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment); |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2170 | FunctionProtoType::ExtProtoInfo newEPI = EPI; |
| 2171 | newEPI.ExtInfo = EPI.ExtInfo.withCallingConv(CallConv); |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2172 | new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, Canonical, newEPI); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2173 | Types.push_back(FTP); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2174 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2175 | return QualType(FTP, 0); |
| 2176 | } |
| 2177 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2178 | #ifndef NDEBUG |
| 2179 | static bool NeedsInjectedClassNameType(const RecordDecl *D) { |
| 2180 | if (!isa<CXXRecordDecl>(D)) return false; |
| 2181 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 2182 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) |
| 2183 | return true; |
| 2184 | if (RD->getDescribedClassTemplate() && |
| 2185 | !isa<ClassTemplateSpecializationDecl>(RD)) |
| 2186 | return true; |
| 2187 | return false; |
| 2188 | } |
| 2189 | #endif |
| 2190 | |
| 2191 | /// getInjectedClassNameType - Return the unique reference to the |
| 2192 | /// injected class name type for the specified templated declaration. |
| 2193 | QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2194 | QualType TST) const { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2195 | assert(NeedsInjectedClassNameType(Decl)); |
| 2196 | if (Decl->TypeForDecl) { |
| 2197 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
Argyrios Kyrtzidis | 37ffed3 | 2010-07-02 11:55:32 +0000 | [diff] [blame] | 2198 | } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDeclaration()) { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2199 | assert(PrevDecl->TypeForDecl && "previous declaration has no type"); |
| 2200 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 2201 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
| 2202 | } else { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2203 | Type *newType = |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2204 | new (*this, TypeAlignment) InjectedClassNameType(Decl, TST); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2205 | Decl->TypeForDecl = newType; |
| 2206 | Types.push_back(newType); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2207 | } |
| 2208 | return QualType(Decl->TypeForDecl, 0); |
| 2209 | } |
| 2210 | |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2211 | /// getTypeDeclType - Return the unique reference to the type for the |
| 2212 | /// specified type declaration. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2213 | QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const { |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 2214 | assert(Decl && "Passed null for Decl param"); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2215 | assert(!Decl->TypeForDecl && "TypeForDecl present in slow case"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2216 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2217 | if (const TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2218 | return getTypedefType(Typedef); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2219 | |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2220 | assert(!isa<TemplateTypeParmDecl>(Decl) && |
| 2221 | "Template type parameter types are always available."); |
| 2222 | |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2223 | if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2224 | assert(!Record->getPreviousDeclaration() && |
| 2225 | "struct/union has previous declaration"); |
| 2226 | assert(!NeedsInjectedClassNameType(Record)); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2227 | return getRecordType(Record); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2228 | } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2229 | assert(!Enum->getPreviousDeclaration() && |
| 2230 | "enum has previous declaration"); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2231 | return getEnumType(Enum); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2232 | } else if (const UnresolvedUsingTypenameDecl *Using = |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2233 | dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2234 | Type *newType = new (*this, TypeAlignment) UnresolvedUsingType(Using); |
| 2235 | Decl->TypeForDecl = newType; |
| 2236 | Types.push_back(newType); |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 2237 | } else |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2238 | llvm_unreachable("TypeDecl without a type?"); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 2239 | |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 2240 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2241 | } |
| 2242 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2243 | /// getTypedefType - Return the unique reference to the type for the |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2244 | /// specified typedef name decl. |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2245 | QualType |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2246 | ASTContext::getTypedefType(const TypedefNameDecl *Decl, |
| 2247 | QualType Canonical) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2248 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2249 | |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2250 | if (Canonical.isNull()) |
| 2251 | Canonical = getCanonicalType(Decl->getUnderlyingType()); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2252 | TypedefType *newType = new(*this, TypeAlignment) |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2253 | TypedefType(Type::Typedef, Decl, Canonical); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2254 | Decl->TypeForDecl = newType; |
| 2255 | Types.push_back(newType); |
| 2256 | return QualType(newType, 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2257 | } |
| 2258 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2259 | QualType ASTContext::getRecordType(const RecordDecl *Decl) const { |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2260 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 2261 | |
| 2262 | if (const RecordDecl *PrevDecl = Decl->getPreviousDeclaration()) |
| 2263 | if (PrevDecl->TypeForDecl) |
| 2264 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 2265 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2266 | RecordType *newType = new (*this, TypeAlignment) RecordType(Decl); |
| 2267 | Decl->TypeForDecl = newType; |
| 2268 | Types.push_back(newType); |
| 2269 | return QualType(newType, 0); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2270 | } |
| 2271 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2272 | QualType ASTContext::getEnumType(const EnumDecl *Decl) const { |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2273 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 2274 | |
| 2275 | if (const EnumDecl *PrevDecl = Decl->getPreviousDeclaration()) |
| 2276 | if (PrevDecl->TypeForDecl) |
| 2277 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 2278 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2279 | EnumType *newType = new (*this, TypeAlignment) EnumType(Decl); |
| 2280 | Decl->TypeForDecl = newType; |
| 2281 | Types.push_back(newType); |
| 2282 | return QualType(newType, 0); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2283 | } |
| 2284 | |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 2285 | QualType ASTContext::getAttributedType(AttributedType::Kind attrKind, |
| 2286 | QualType modifiedType, |
| 2287 | QualType equivalentType) { |
| 2288 | llvm::FoldingSetNodeID id; |
| 2289 | AttributedType::Profile(id, attrKind, modifiedType, equivalentType); |
| 2290 | |
| 2291 | void *insertPos = 0; |
| 2292 | AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos); |
| 2293 | if (type) return QualType(type, 0); |
| 2294 | |
| 2295 | QualType canon = getCanonicalType(equivalentType); |
| 2296 | type = new (*this, TypeAlignment) |
| 2297 | AttributedType(canon, attrKind, modifiedType, equivalentType); |
| 2298 | |
| 2299 | Types.push_back(type); |
| 2300 | AttributedTypes.InsertNode(type, insertPos); |
| 2301 | |
| 2302 | return QualType(type, 0); |
| 2303 | } |
| 2304 | |
| 2305 | |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2306 | /// \brief Retrieve a substitution-result type. |
| 2307 | QualType |
| 2308 | ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2309 | QualType Replacement) const { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2310 | assert(Replacement.isCanonical() |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2311 | && "replacement types must always be canonical"); |
| 2312 | |
| 2313 | llvm::FoldingSetNodeID ID; |
| 2314 | SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); |
| 2315 | void *InsertPos = 0; |
| 2316 | SubstTemplateTypeParmType *SubstParm |
| 2317 | = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2318 | |
| 2319 | if (!SubstParm) { |
| 2320 | SubstParm = new (*this, TypeAlignment) |
| 2321 | SubstTemplateTypeParmType(Parm, Replacement); |
| 2322 | Types.push_back(SubstParm); |
| 2323 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 2324 | } |
| 2325 | |
| 2326 | return QualType(SubstParm, 0); |
| 2327 | } |
| 2328 | |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 2329 | /// \brief Retrieve a |
| 2330 | QualType ASTContext::getSubstTemplateTypeParmPackType( |
| 2331 | const TemplateTypeParmType *Parm, |
| 2332 | const TemplateArgument &ArgPack) { |
| 2333 | #ifndef NDEBUG |
| 2334 | for (TemplateArgument::pack_iterator P = ArgPack.pack_begin(), |
| 2335 | PEnd = ArgPack.pack_end(); |
| 2336 | P != PEnd; ++P) { |
| 2337 | assert(P->getKind() == TemplateArgument::Type &&"Pack contains a non-type"); |
| 2338 | assert(P->getAsType().isCanonical() && "Pack contains non-canonical type"); |
| 2339 | } |
| 2340 | #endif |
| 2341 | |
| 2342 | llvm::FoldingSetNodeID ID; |
| 2343 | SubstTemplateTypeParmPackType::Profile(ID, Parm, ArgPack); |
| 2344 | void *InsertPos = 0; |
| 2345 | if (SubstTemplateTypeParmPackType *SubstParm |
| 2346 | = SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2347 | return QualType(SubstParm, 0); |
| 2348 | |
| 2349 | QualType Canon; |
| 2350 | if (!Parm->isCanonicalUnqualified()) { |
| 2351 | Canon = getCanonicalType(QualType(Parm, 0)); |
| 2352 | Canon = getSubstTemplateTypeParmPackType(cast<TemplateTypeParmType>(Canon), |
| 2353 | ArgPack); |
| 2354 | SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2355 | } |
| 2356 | |
| 2357 | SubstTemplateTypeParmPackType *SubstParm |
| 2358 | = new (*this, TypeAlignment) SubstTemplateTypeParmPackType(Parm, Canon, |
| 2359 | ArgPack); |
| 2360 | Types.push_back(SubstParm); |
| 2361 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 2362 | return QualType(SubstParm, 0); |
| 2363 | } |
| 2364 | |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2365 | /// \brief Retrieve the template type parameter type for a template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2366 | /// parameter or parameter pack with the given depth, index, and (optionally) |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2367 | /// name. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2368 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2369 | bool ParameterPack, |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2370 | TemplateTypeParmDecl *TTPDecl) const { |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2371 | llvm::FoldingSetNodeID ID; |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2372 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, TTPDecl); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2373 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2374 | TemplateTypeParmType *TypeParm |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2375 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2376 | |
| 2377 | if (TypeParm) |
| 2378 | return QualType(TypeParm, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2379 | |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2380 | if (TTPDecl) { |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2381 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2382 | TypeParm = new (*this, TypeAlignment) TemplateTypeParmType(TTPDecl, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2383 | |
| 2384 | TemplateTypeParmType *TypeCheck |
| 2385 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2386 | assert(!TypeCheck && "Template type parameter canonical type broken"); |
| 2387 | (void)TypeCheck; |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2388 | } else |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2389 | TypeParm = new (*this, TypeAlignment) |
| 2390 | TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2391 | |
| 2392 | Types.push_back(TypeParm); |
| 2393 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 2394 | |
| 2395 | return QualType(TypeParm, 0); |
| 2396 | } |
| 2397 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2398 | TypeSourceInfo * |
| 2399 | ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name, |
| 2400 | SourceLocation NameLoc, |
| 2401 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2402 | QualType Underlying) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2403 | assert(!Name.getAsDependentTemplateName() && |
| 2404 | "No dependent template names here!"); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2405 | QualType TST = getTemplateSpecializationType(Name, Args, Underlying); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2406 | |
| 2407 | TypeSourceInfo *DI = CreateTypeSourceInfo(TST); |
| 2408 | TemplateSpecializationTypeLoc TL |
| 2409 | = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc()); |
| 2410 | TL.setTemplateNameLoc(NameLoc); |
| 2411 | TL.setLAngleLoc(Args.getLAngleLoc()); |
| 2412 | TL.setRAngleLoc(Args.getRAngleLoc()); |
| 2413 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 2414 | TL.setArgLocInfo(i, Args[i].getLocInfo()); |
| 2415 | return DI; |
| 2416 | } |
| 2417 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2418 | QualType |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2419 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2420 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2421 | QualType Underlying) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2422 | assert(!Template.getAsDependentTemplateName() && |
| 2423 | "No dependent template names here!"); |
| 2424 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2425 | unsigned NumArgs = Args.size(); |
| 2426 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2427 | SmallVector<TemplateArgument, 4> ArgVec; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2428 | ArgVec.reserve(NumArgs); |
| 2429 | for (unsigned i = 0; i != NumArgs; ++i) |
| 2430 | ArgVec.push_back(Args[i].getArgument()); |
| 2431 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2432 | return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2433 | Underlying); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2434 | } |
| 2435 | |
| 2436 | QualType |
| 2437 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2438 | const TemplateArgument *Args, |
| 2439 | unsigned NumArgs, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2440 | QualType Underlying) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2441 | assert(!Template.getAsDependentTemplateName() && |
| 2442 | "No dependent template names here!"); |
Douglas Gregor | 0f0ea2a | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 2443 | // Look through qualified template names. |
| 2444 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 2445 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2446 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2447 | bool isTypeAlias = |
| 2448 | Template.getAsTemplateDecl() && |
| 2449 | isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl()); |
| 2450 | |
| 2451 | QualType CanonType; |
| 2452 | if (!Underlying.isNull()) |
| 2453 | CanonType = getCanonicalType(Underlying); |
| 2454 | else { |
| 2455 | assert(!isTypeAlias && |
| 2456 | "Underlying type for template alias must be computed by caller"); |
| 2457 | CanonType = getCanonicalTemplateSpecializationType(Template, Args, |
| 2458 | NumArgs); |
| 2459 | } |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 2460 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2461 | // Allocate the (non-canonical) template specialization type, but don't |
| 2462 | // try to unique it: these types typically have location information that |
| 2463 | // we don't unique and don't want to lose. |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2464 | void *Mem = Allocate(sizeof(TemplateSpecializationType) + |
| 2465 | sizeof(TemplateArgument) * NumArgs + |
| 2466 | (isTypeAlias ? sizeof(QualType) : 0), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2467 | TypeAlignment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2468 | TemplateSpecializationType *Spec |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 2469 | = new (Mem) TemplateSpecializationType(Template, |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2470 | Args, NumArgs, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2471 | CanonType, |
| 2472 | isTypeAlias ? Underlying : QualType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2473 | |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2474 | Types.push_back(Spec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2475 | return QualType(Spec, 0); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2476 | } |
| 2477 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2478 | QualType |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2479 | ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template, |
| 2480 | const TemplateArgument *Args, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2481 | unsigned NumArgs) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2482 | assert(!Template.getAsDependentTemplateName() && |
| 2483 | "No dependent template names here!"); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2484 | assert((!Template.getAsTemplateDecl() || |
| 2485 | !isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) && |
| 2486 | "Underlying type for template alias must be computed by caller"); |
| 2487 | |
Douglas Gregor | 0f0ea2a | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 2488 | // Look through qualified template names. |
| 2489 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 2490 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2491 | |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2492 | // Build the canonical template specialization type. |
| 2493 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2494 | SmallVector<TemplateArgument, 4> CanonArgs; |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2495 | CanonArgs.reserve(NumArgs); |
| 2496 | for (unsigned I = 0; I != NumArgs; ++I) |
| 2497 | CanonArgs.push_back(getCanonicalTemplateArgument(Args[I])); |
| 2498 | |
| 2499 | // Determine whether this canonical template specialization type already |
| 2500 | // exists. |
| 2501 | llvm::FoldingSetNodeID ID; |
| 2502 | TemplateSpecializationType::Profile(ID, CanonTemplate, |
| 2503 | CanonArgs.data(), NumArgs, *this); |
| 2504 | |
| 2505 | void *InsertPos = 0; |
| 2506 | TemplateSpecializationType *Spec |
| 2507 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2508 | |
| 2509 | if (!Spec) { |
| 2510 | // Allocate a new canonical template specialization type. |
| 2511 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
| 2512 | sizeof(TemplateArgument) * NumArgs), |
| 2513 | TypeAlignment); |
| 2514 | Spec = new (Mem) TemplateSpecializationType(CanonTemplate, |
| 2515 | CanonArgs.data(), NumArgs, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2516 | QualType(), QualType()); |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2517 | Types.push_back(Spec); |
| 2518 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
| 2519 | } |
| 2520 | |
| 2521 | assert(Spec->isDependentType() && |
| 2522 | "Non-dependent template-id type must have a canonical type"); |
| 2523 | return QualType(Spec, 0); |
| 2524 | } |
| 2525 | |
| 2526 | QualType |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2527 | ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword, |
| 2528 | NestedNameSpecifier *NNS, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2529 | QualType NamedType) const { |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2530 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2531 | ElaboratedType::Profile(ID, Keyword, NNS, NamedType); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2532 | |
| 2533 | void *InsertPos = 0; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2534 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2535 | if (T) |
| 2536 | return QualType(T, 0); |
| 2537 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2538 | QualType Canon = NamedType; |
| 2539 | if (!Canon.isCanonical()) { |
| 2540 | Canon = getCanonicalType(NamedType); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2541 | ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2542 | assert(!CheckT && "Elaborated canonical type broken"); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2543 | (void)CheckT; |
| 2544 | } |
| 2545 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2546 | T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2547 | Types.push_back(T); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2548 | ElaboratedTypes.InsertNode(T, InsertPos); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2549 | return QualType(T, 0); |
| 2550 | } |
| 2551 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 2552 | QualType |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2553 | ASTContext::getParenType(QualType InnerType) const { |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 2554 | llvm::FoldingSetNodeID ID; |
| 2555 | ParenType::Profile(ID, InnerType); |
| 2556 | |
| 2557 | void *InsertPos = 0; |
| 2558 | ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2559 | if (T) |
| 2560 | return QualType(T, 0); |
| 2561 | |
| 2562 | QualType Canon = InnerType; |
| 2563 | if (!Canon.isCanonical()) { |
| 2564 | Canon = getCanonicalType(InnerType); |
| 2565 | ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2566 | assert(!CheckT && "Paren canonical type broken"); |
| 2567 | (void)CheckT; |
| 2568 | } |
| 2569 | |
| 2570 | T = new (*this) ParenType(InnerType, Canon); |
| 2571 | Types.push_back(T); |
| 2572 | ParenTypes.InsertNode(T, InsertPos); |
| 2573 | return QualType(T, 0); |
| 2574 | } |
| 2575 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2576 | QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, |
| 2577 | NestedNameSpecifier *NNS, |
| 2578 | const IdentifierInfo *Name, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2579 | QualType Canon) const { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2580 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 2581 | |
| 2582 | if (Canon.isNull()) { |
| 2583 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2584 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 2585 | if (Keyword == ETK_None) |
| 2586 | CanonKeyword = ETK_Typename; |
| 2587 | |
| 2588 | if (CanonNNS != NNS || CanonKeyword != Keyword) |
| 2589 | Canon = getDependentNameType(CanonKeyword, CanonNNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2590 | } |
| 2591 | |
| 2592 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2593 | DependentNameType::Profile(ID, Keyword, NNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2594 | |
| 2595 | void *InsertPos = 0; |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2596 | DependentNameType *T |
| 2597 | = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2598 | if (T) |
| 2599 | return QualType(T, 0); |
| 2600 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2601 | T = new (*this) DependentNameType(Keyword, NNS, Name, Canon); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2602 | Types.push_back(T); |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2603 | DependentNameTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2604 | return QualType(T, 0); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2605 | } |
| 2606 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2607 | QualType |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2608 | ASTContext::getDependentTemplateSpecializationType( |
| 2609 | ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2610 | NestedNameSpecifier *NNS, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2611 | const IdentifierInfo *Name, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2612 | const TemplateArgumentListInfo &Args) const { |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2613 | // TODO: avoid this copy |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2614 | SmallVector<TemplateArgument, 16> ArgCopy; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2615 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
| 2616 | ArgCopy.push_back(Args[I].getArgument()); |
| 2617 | return getDependentTemplateSpecializationType(Keyword, NNS, Name, |
| 2618 | ArgCopy.size(), |
| 2619 | ArgCopy.data()); |
| 2620 | } |
| 2621 | |
| 2622 | QualType |
| 2623 | ASTContext::getDependentTemplateSpecializationType( |
| 2624 | ElaboratedTypeKeyword Keyword, |
| 2625 | NestedNameSpecifier *NNS, |
| 2626 | const IdentifierInfo *Name, |
| 2627 | unsigned NumArgs, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2628 | const TemplateArgument *Args) const { |
Douglas Gregor | aa2187d | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 2629 | assert((!NNS || NNS->isDependent()) && |
| 2630 | "nested-name-specifier must be dependent"); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2631 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2632 | llvm::FoldingSetNodeID ID; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2633 | DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS, |
| 2634 | Name, NumArgs, Args); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2635 | |
| 2636 | void *InsertPos = 0; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2637 | DependentTemplateSpecializationType *T |
| 2638 | = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2639 | if (T) |
| 2640 | return QualType(T, 0); |
| 2641 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2642 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2643 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2644 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 2645 | if (Keyword == ETK_None) CanonKeyword = ETK_Typename; |
| 2646 | |
| 2647 | bool AnyNonCanonArgs = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2648 | SmallVector<TemplateArgument, 16> CanonArgs(NumArgs); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2649 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 2650 | CanonArgs[I] = getCanonicalTemplateArgument(Args[I]); |
| 2651 | if (!CanonArgs[I].structurallyEquals(Args[I])) |
| 2652 | AnyNonCanonArgs = true; |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2653 | } |
| 2654 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2655 | QualType Canon; |
| 2656 | if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) { |
| 2657 | Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS, |
| 2658 | Name, NumArgs, |
| 2659 | CanonArgs.data()); |
| 2660 | |
| 2661 | // Find the insert position again. |
| 2662 | DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2663 | } |
| 2664 | |
| 2665 | void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) + |
| 2666 | sizeof(TemplateArgument) * NumArgs), |
| 2667 | TypeAlignment); |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 2668 | T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2669 | Name, NumArgs, Args, Canon); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2670 | Types.push_back(T); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2671 | DependentTemplateSpecializationTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2672 | return QualType(T, 0); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2673 | } |
| 2674 | |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2675 | QualType ASTContext::getPackExpansionType(QualType Pattern, |
| 2676 | llvm::Optional<unsigned> NumExpansions) { |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2677 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2678 | PackExpansionType::Profile(ID, Pattern, NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2679 | |
| 2680 | assert(Pattern->containsUnexpandedParameterPack() && |
| 2681 | "Pack expansions must expand one or more parameter packs"); |
| 2682 | void *InsertPos = 0; |
| 2683 | PackExpansionType *T |
| 2684 | = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2685 | if (T) |
| 2686 | return QualType(T, 0); |
| 2687 | |
| 2688 | QualType Canon; |
| 2689 | if (!Pattern.isCanonical()) { |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2690 | Canon = getPackExpansionType(getCanonicalType(Pattern), NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2691 | |
| 2692 | // Find the insert position again. |
| 2693 | PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2694 | } |
| 2695 | |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2696 | T = new (*this) PackExpansionType(Pattern, Canon, NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2697 | Types.push_back(T); |
| 2698 | PackExpansionTypes.InsertNode(T, InsertPos); |
| 2699 | return QualType(T, 0); |
| 2700 | } |
| 2701 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2702 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 2703 | /// alphabetically. |
| 2704 | static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, |
| 2705 | const ObjCProtocolDecl *RHS) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2706 | return LHS->getDeclName() < RHS->getDeclName(); |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2707 | } |
| 2708 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2709 | static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols, |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2710 | unsigned NumProtocols) { |
| 2711 | if (NumProtocols == 0) return true; |
| 2712 | |
Douglas Gregor | 61cc296 | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 2713 | if (Protocols[0]->getCanonicalDecl() != Protocols[0]) |
| 2714 | return false; |
| 2715 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2716 | for (unsigned i = 1; i != NumProtocols; ++i) |
Douglas Gregor | 61cc296 | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 2717 | if (!CmpProtocolNames(Protocols[i-1], Protocols[i]) || |
| 2718 | Protocols[i]->getCanonicalDecl() != Protocols[i]) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2719 | return false; |
| 2720 | return true; |
| 2721 | } |
| 2722 | |
| 2723 | static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols, |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2724 | unsigned &NumProtocols) { |
| 2725 | ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2726 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2727 | // Sort protocols, keyed by name. |
| 2728 | std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); |
| 2729 | |
Douglas Gregor | 61cc296 | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 2730 | // Canonicalize. |
| 2731 | for (unsigned I = 0, N = NumProtocols; I != N; ++I) |
| 2732 | Protocols[I] = Protocols[I]->getCanonicalDecl(); |
| 2733 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2734 | // Remove duplicates. |
| 2735 | ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); |
| 2736 | NumProtocols = ProtocolsEnd-Protocols; |
| 2737 | } |
| 2738 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2739 | QualType ASTContext::getObjCObjectType(QualType BaseType, |
| 2740 | ObjCProtocolDecl * const *Protocols, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2741 | unsigned NumProtocols) const { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2742 | // If the base type is an interface and there aren't any protocols |
| 2743 | // to add, then the interface type will do just fine. |
| 2744 | if (!NumProtocols && isa<ObjCInterfaceType>(BaseType)) |
| 2745 | return BaseType; |
| 2746 | |
| 2747 | // Look in the folding set for an existing type. |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2748 | llvm::FoldingSetNodeID ID; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2749 | ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2750 | void *InsertPos = 0; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2751 | if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2752 | return QualType(QT, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2753 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2754 | // Build the canonical type, which has the canonical base type and |
| 2755 | // a sorted-and-uniqued list of protocols. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2756 | QualType Canonical; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2757 | bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols); |
| 2758 | if (!ProtocolsSorted || !BaseType.isCanonical()) { |
| 2759 | if (!ProtocolsSorted) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2760 | SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols, |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 2761 | Protocols + NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2762 | unsigned UniqueCount = NumProtocols; |
| 2763 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2764 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2765 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 2766 | &Sorted[0], UniqueCount); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2767 | } else { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2768 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 2769 | Protocols, NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2770 | } |
| 2771 | |
| 2772 | // Regenerate InsertPos. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2773 | ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2774 | } |
| 2775 | |
| 2776 | unsigned Size = sizeof(ObjCObjectTypeImpl); |
| 2777 | Size += NumProtocols * sizeof(ObjCProtocolDecl *); |
| 2778 | void *Mem = Allocate(Size, TypeAlignment); |
| 2779 | ObjCObjectTypeImpl *T = |
| 2780 | new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols); |
| 2781 | |
| 2782 | Types.push_back(T); |
| 2783 | ObjCObjectTypes.InsertNode(T, InsertPos); |
| 2784 | return QualType(T, 0); |
| 2785 | } |
| 2786 | |
| 2787 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 2788 | /// the given object type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2789 | QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2790 | llvm::FoldingSetNodeID ID; |
| 2791 | ObjCObjectPointerType::Profile(ID, ObjectT); |
| 2792 | |
| 2793 | void *InsertPos = 0; |
| 2794 | if (ObjCObjectPointerType *QT = |
| 2795 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2796 | return QualType(QT, 0); |
| 2797 | |
| 2798 | // Find the canonical object type. |
| 2799 | QualType Canonical; |
| 2800 | if (!ObjectT.isCanonical()) { |
| 2801 | Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT)); |
| 2802 | |
| 2803 | // Regenerate InsertPos. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2804 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2805 | } |
| 2806 | |
Douglas Gregor | fd6a088 | 2010-02-08 22:59:26 +0000 | [diff] [blame] | 2807 | // No match. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2808 | void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment); |
| 2809 | ObjCObjectPointerType *QType = |
| 2810 | new (Mem) ObjCObjectPointerType(Canonical, ObjectT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2811 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2812 | Types.push_back(QType); |
| 2813 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2814 | return QualType(QType, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2815 | } |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2816 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2817 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 2818 | /// specified ObjC interface decl. The list of protocols is optional. |
Douglas Gregor | 0af5501 | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 2819 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl, |
| 2820 | ObjCInterfaceDecl *PrevDecl) const { |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2821 | if (Decl->TypeForDecl) |
| 2822 | return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2823 | |
Douglas Gregor | 0af5501 | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 2824 | if (PrevDecl) { |
| 2825 | assert(PrevDecl->TypeForDecl && "previous decl has no TypeForDecl"); |
| 2826 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 2827 | return QualType(PrevDecl->TypeForDecl, 0); |
| 2828 | } |
| 2829 | |
Douglas Gregor | 8d2dbbf | 2011-12-16 16:34:57 +0000 | [diff] [blame] | 2830 | // Prefer the definition, if there is one. |
| 2831 | if (const ObjCInterfaceDecl *Def = Decl->getDefinition()) |
| 2832 | Decl = Def; |
| 2833 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2834 | void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment); |
| 2835 | ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl); |
| 2836 | Decl->TypeForDecl = T; |
| 2837 | Types.push_back(T); |
| 2838 | return QualType(T, 0); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2839 | } |
| 2840 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2841 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 2842 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2843 | /// multiple declarations that refer to "typeof(x)" all contain different |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2844 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2845 | /// on canonical type's (which are always unique). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2846 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) const { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2847 | TypeOfExprType *toe; |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2848 | if (tofExpr->isTypeDependent()) { |
| 2849 | llvm::FoldingSetNodeID ID; |
| 2850 | DependentTypeOfExprType::Profile(ID, *this, tofExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2851 | |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2852 | void *InsertPos = 0; |
| 2853 | DependentTypeOfExprType *Canon |
| 2854 | = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2855 | if (Canon) { |
| 2856 | // We already have a "canonical" version of an identical, dependent |
| 2857 | // typeof(expr) type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2858 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2859 | QualType((TypeOfExprType*)Canon, 0)); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 2860 | } else { |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +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 |
| 2863 | = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2864 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); |
| 2865 | toe = Canon; |
| 2866 | } |
| 2867 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2868 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2869 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2870 | } |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2871 | Types.push_back(toe); |
| 2872 | return QualType(toe, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2873 | } |
| 2874 | |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2875 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 2876 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 2877 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2878 | /// an issue. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2879 | /// on canonical type's (which are always unique). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2880 | QualType ASTContext::getTypeOfType(QualType tofType) const { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2881 | QualType Canonical = getCanonicalType(tofType); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2882 | TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2883 | Types.push_back(tot); |
| 2884 | return QualType(tot, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2885 | } |
| 2886 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2887 | /// getDecltypeForExpr - Given an expr, will return the decltype for that |
| 2888 | /// expression, according to the rules in C++0x [dcl.type.simple]p4 |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2889 | static QualType getDecltypeForExpr(const Expr *e, const ASTContext &Context) { |
Anders Carlsson | a07c33e | 2009-06-25 15:00:34 +0000 | [diff] [blame] | 2890 | if (e->isTypeDependent()) |
| 2891 | return Context.DependentTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2892 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2893 | // If e is an id expression or a class member access, decltype(e) is defined |
| 2894 | // as the type of the entity named by e. |
| 2895 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) { |
| 2896 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) |
| 2897 | return VD->getType(); |
| 2898 | } |
| 2899 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) { |
| 2900 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) |
| 2901 | return FD->getType(); |
| 2902 | } |
| 2903 | // If e is a function call or an invocation of an overloaded operator, |
| 2904 | // (parentheses around e are ignored), decltype(e) is defined as the |
| 2905 | // return type of that function. |
| 2906 | if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens())) |
| 2907 | return CE->getCallReturnType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2908 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2909 | QualType T = e->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2910 | |
| 2911 | // 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] | 2912 | // defined as T&, otherwise decltype(e) is defined as T. |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 2913 | if (e->isLValue()) |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2914 | T = Context.getLValueReferenceType(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2915 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2916 | return T; |
| 2917 | } |
| 2918 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2919 | /// getDecltypeType - Unlike many "get<Type>" functions, we don't unique |
| 2920 | /// DecltypeType AST's. The only motivation to unique these nodes would be |
| 2921 | /// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2922 | /// an issue. This doesn't effect the type checker, since it operates |
David Blaikie | 39e0203 | 2011-11-06 22:28:03 +0000 | [diff] [blame] | 2923 | /// on canonical types (which are always unique). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2924 | QualType ASTContext::getDecltypeType(Expr *e) const { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2925 | DecltypeType *dt; |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2926 | |
| 2927 | // C++0x [temp.type]p2: |
| 2928 | // If an expression e involves a template parameter, decltype(e) denotes a |
| 2929 | // unique dependent type. Two such decltype-specifiers refer to the same |
| 2930 | // type only if their expressions are equivalent (14.5.6.1). |
| 2931 | if (e->isInstantiationDependent()) { |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2932 | llvm::FoldingSetNodeID ID; |
| 2933 | DependentDecltypeType::Profile(ID, *this, e); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2934 | |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2935 | void *InsertPos = 0; |
| 2936 | DependentDecltypeType *Canon |
| 2937 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2938 | if (Canon) { |
| 2939 | // We already have a "canonical" version of an equivalent, dependent |
| 2940 | // decltype type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2941 | dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy, |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2942 | QualType((DecltypeType*)Canon, 0)); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 2943 | } else { |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2944 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2945 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2946 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); |
| 2947 | dt = Canon; |
| 2948 | } |
| 2949 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2950 | QualType T = getDecltypeForExpr(e, *this); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2951 | dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T)); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2952 | } |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2953 | Types.push_back(dt); |
| 2954 | return QualType(dt, 0); |
| 2955 | } |
| 2956 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 2957 | /// getUnaryTransformationType - We don't unique these, since the memory |
| 2958 | /// savings are minimal and these are rare. |
| 2959 | QualType ASTContext::getUnaryTransformType(QualType BaseType, |
| 2960 | QualType UnderlyingType, |
| 2961 | UnaryTransformType::UTTKind Kind) |
| 2962 | const { |
| 2963 | UnaryTransformType *Ty = |
Douglas Gregor | 69d9775 | 2011-05-25 17:51:54 +0000 | [diff] [blame] | 2964 | new (*this, TypeAlignment) UnaryTransformType (BaseType, UnderlyingType, |
| 2965 | Kind, |
| 2966 | UnderlyingType->isDependentType() ? |
| 2967 | QualType() : UnderlyingType); |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 2968 | Types.push_back(Ty); |
| 2969 | return QualType(Ty, 0); |
| 2970 | } |
| 2971 | |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 2972 | /// getAutoType - We only unique auto types after they've been deduced. |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2973 | QualType ASTContext::getAutoType(QualType DeducedType) const { |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 2974 | void *InsertPos = 0; |
| 2975 | if (!DeducedType.isNull()) { |
| 2976 | // Look in the folding set for an existing type. |
| 2977 | llvm::FoldingSetNodeID ID; |
| 2978 | AutoType::Profile(ID, DeducedType); |
| 2979 | if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2980 | return QualType(AT, 0); |
| 2981 | } |
| 2982 | |
| 2983 | AutoType *AT = new (*this, TypeAlignment) AutoType(DeducedType); |
| 2984 | Types.push_back(AT); |
| 2985 | if (InsertPos) |
| 2986 | AutoTypes.InsertNode(AT, InsertPos); |
| 2987 | return QualType(AT, 0); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2988 | } |
| 2989 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 2990 | /// getAtomicType - Return the uniqued reference to the atomic type for |
| 2991 | /// the given value type. |
| 2992 | QualType ASTContext::getAtomicType(QualType T) const { |
| 2993 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2994 | // structure. |
| 2995 | llvm::FoldingSetNodeID ID; |
| 2996 | AtomicType::Profile(ID, T); |
| 2997 | |
| 2998 | void *InsertPos = 0; |
| 2999 | if (AtomicType *AT = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3000 | return QualType(AT, 0); |
| 3001 | |
| 3002 | // If the atomic value type isn't canonical, this won't be a canonical type |
| 3003 | // either, so fill in the canonical type field. |
| 3004 | QualType Canonical; |
| 3005 | if (!T.isCanonical()) { |
| 3006 | Canonical = getAtomicType(getCanonicalType(T)); |
| 3007 | |
| 3008 | // Get the new insert position for the node we care about. |
| 3009 | AtomicType *NewIP = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3010 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
| 3011 | } |
| 3012 | AtomicType *New = new (*this, TypeAlignment) AtomicType(T, Canonical); |
| 3013 | Types.push_back(New); |
| 3014 | AtomicTypes.InsertNode(New, InsertPos); |
| 3015 | return QualType(New, 0); |
| 3016 | } |
| 3017 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3018 | /// getAutoDeductType - Get type pattern for deducing against 'auto'. |
| 3019 | QualType ASTContext::getAutoDeductType() const { |
| 3020 | if (AutoDeductTy.isNull()) |
| 3021 | AutoDeductTy = getAutoType(QualType()); |
| 3022 | assert(!AutoDeductTy.isNull() && "can't build 'auto' pattern"); |
| 3023 | return AutoDeductTy; |
| 3024 | } |
| 3025 | |
| 3026 | /// getAutoRRefDeductType - Get type pattern for deducing against 'auto &&'. |
| 3027 | QualType ASTContext::getAutoRRefDeductType() const { |
| 3028 | if (AutoRRefDeductTy.isNull()) |
| 3029 | AutoRRefDeductTy = getRValueReferenceType(getAutoDeductType()); |
| 3030 | assert(!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern"); |
| 3031 | return AutoRRefDeductTy; |
| 3032 | } |
| 3033 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3034 | /// getTagDeclType - Return the unique reference to the type for the |
| 3035 | /// specified TagDecl (struct/union/class/enum) decl. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3036 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) const { |
Ted Kremenek | d778f88 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 3037 | assert (Decl); |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 3038 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 3039 | // away const? mutable? |
| 3040 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3041 | } |
| 3042 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3043 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 3044 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 3045 | /// needs to agree with the definition in <stddef.h>. |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 3046 | CanQualType ASTContext::getSizeType() const { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3047 | return getFromTargetType(Target->getSizeType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3048 | } |
| 3049 | |
Hans Wennborg | 29e97cb | 2011-10-27 08:29:09 +0000 | [diff] [blame] | 3050 | /// getIntMaxType - Return the unique type for "intmax_t" (C99 7.18.1.5). |
| 3051 | CanQualType ASTContext::getIntMaxType() const { |
| 3052 | return getFromTargetType(Target->getIntMaxType()); |
| 3053 | } |
| 3054 | |
| 3055 | /// getUIntMaxType - Return the unique type for "uintmax_t" (C99 7.18.1.5). |
| 3056 | CanQualType ASTContext::getUIntMaxType() const { |
| 3057 | return getFromTargetType(Target->getUIntMaxType()); |
| 3058 | } |
| 3059 | |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3060 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 3061 | /// Used when in C++, as a GCC extension. |
| 3062 | QualType ASTContext::getSignedWCharType() const { |
| 3063 | // FIXME: derive from "Target" ? |
| 3064 | return WCharTy; |
| 3065 | } |
| 3066 | |
| 3067 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 3068 | /// Used when in C++, as a GCC extension. |
| 3069 | QualType ASTContext::getUnsignedWCharType() const { |
| 3070 | // FIXME: derive from "Target" ? |
| 3071 | return UnsignedIntTy; |
| 3072 | } |
| 3073 | |
Hans Wennborg | 29e97cb | 2011-10-27 08:29:09 +0000 | [diff] [blame] | 3074 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17) |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 3075 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 3076 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3077 | return getFromTargetType(Target->getPtrDiffType(0)); |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 3078 | } |
| 3079 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3080 | //===----------------------------------------------------------------------===// |
| 3081 | // Type Operators |
| 3082 | //===----------------------------------------------------------------------===// |
| 3083 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3084 | CanQualType ASTContext::getCanonicalParamType(QualType T) const { |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3085 | // Push qualifiers into arrays, and then discard any remaining |
| 3086 | // qualifiers. |
| 3087 | T = getCanonicalType(T); |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3088 | T = getVariableArrayDecayedType(T); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3089 | const Type *Ty = T.getTypePtr(); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3090 | QualType Result; |
| 3091 | if (isa<ArrayType>(Ty)) { |
| 3092 | Result = getArrayDecayedType(QualType(Ty,0)); |
| 3093 | } else if (isa<FunctionType>(Ty)) { |
| 3094 | Result = getPointerType(QualType(Ty, 0)); |
| 3095 | } else { |
| 3096 | Result = QualType(Ty, 0); |
| 3097 | } |
| 3098 | |
| 3099 | return CanQualType::CreateUnsafe(Result); |
| 3100 | } |
| 3101 | |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3102 | QualType ASTContext::getUnqualifiedArrayType(QualType type, |
| 3103 | Qualifiers &quals) { |
| 3104 | SplitQualType splitType = type.getSplitUnqualifiedType(); |
| 3105 | |
| 3106 | // FIXME: getSplitUnqualifiedType() actually walks all the way to |
| 3107 | // the unqualified desugared type and then drops it on the floor. |
| 3108 | // We then have to strip that sugar back off with |
| 3109 | // getUnqualifiedDesugaredType(), which is silly. |
| 3110 | const ArrayType *AT = |
| 3111 | dyn_cast<ArrayType>(splitType.first->getUnqualifiedDesugaredType()); |
| 3112 | |
| 3113 | // 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] | 3114 | if (!AT) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3115 | quals = splitType.second; |
| 3116 | return QualType(splitType.first, 0); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3117 | } |
| 3118 | |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3119 | // Otherwise, recurse on the array's element type. |
| 3120 | QualType elementType = AT->getElementType(); |
| 3121 | QualType unqualElementType = getUnqualifiedArrayType(elementType, quals); |
| 3122 | |
| 3123 | // If that didn't change the element type, AT has no qualifiers, so we |
| 3124 | // can just use the results in splitType. |
| 3125 | if (elementType == unqualElementType) { |
| 3126 | assert(quals.empty()); // from the recursive call |
| 3127 | quals = splitType.second; |
| 3128 | return QualType(splitType.first, 0); |
| 3129 | } |
| 3130 | |
| 3131 | // Otherwise, add in the qualifiers from the outermost type, then |
| 3132 | // build the type back up. |
| 3133 | quals.addConsistentQualifiers(splitType.second); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3134 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3135 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3136 | return getConstantArrayType(unqualElementType, CAT->getSize(), |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3137 | CAT->getSizeModifier(), 0); |
| 3138 | } |
| 3139 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3140 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3141 | return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3142 | } |
| 3143 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3144 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3145 | return getVariableArrayType(unqualElementType, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3146 | VAT->getSizeExpr(), |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3147 | VAT->getSizeModifier(), |
| 3148 | VAT->getIndexTypeCVRQualifiers(), |
| 3149 | VAT->getBracketsRange()); |
| 3150 | } |
| 3151 | |
| 3152 | const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT); |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3153 | return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(), |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3154 | DSAT->getSizeModifier(), 0, |
| 3155 | SourceRange()); |
| 3156 | } |
| 3157 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3158 | /// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that |
| 3159 | /// may be similar (C++ 4.4), replaces T1 and T2 with the type that |
| 3160 | /// they point to and return true. If T1 and T2 aren't pointer types |
| 3161 | /// or pointer-to-member types, or if they are not similar at this |
| 3162 | /// level, returns false and leaves T1 and T2 unchanged. Top-level |
| 3163 | /// qualifiers on T1 and T2 are ignored. This function will typically |
| 3164 | /// be called in a loop that successively "unwraps" pointer and |
| 3165 | /// pointer-to-member types to compare them at each level. |
| 3166 | bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) { |
| 3167 | const PointerType *T1PtrType = T1->getAs<PointerType>(), |
| 3168 | *T2PtrType = T2->getAs<PointerType>(); |
| 3169 | if (T1PtrType && T2PtrType) { |
| 3170 | T1 = T1PtrType->getPointeeType(); |
| 3171 | T2 = T2PtrType->getPointeeType(); |
| 3172 | return true; |
| 3173 | } |
| 3174 | |
| 3175 | const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(), |
| 3176 | *T2MPType = T2->getAs<MemberPointerType>(); |
| 3177 | if (T1MPType && T2MPType && |
| 3178 | hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0), |
| 3179 | QualType(T2MPType->getClass(), 0))) { |
| 3180 | T1 = T1MPType->getPointeeType(); |
| 3181 | T2 = T2MPType->getPointeeType(); |
| 3182 | return true; |
| 3183 | } |
| 3184 | |
| 3185 | if (getLangOptions().ObjC1) { |
| 3186 | const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(), |
| 3187 | *T2OPType = T2->getAs<ObjCObjectPointerType>(); |
| 3188 | if (T1OPType && T2OPType) { |
| 3189 | T1 = T1OPType->getPointeeType(); |
| 3190 | T2 = T2OPType->getPointeeType(); |
| 3191 | return true; |
| 3192 | } |
| 3193 | } |
| 3194 | |
| 3195 | // FIXME: Block pointers, too? |
| 3196 | |
| 3197 | return false; |
| 3198 | } |
| 3199 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3200 | DeclarationNameInfo |
| 3201 | ASTContext::getNameForTemplate(TemplateName Name, |
| 3202 | SourceLocation NameLoc) const { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3203 | switch (Name.getKind()) { |
| 3204 | case TemplateName::QualifiedTemplate: |
| 3205 | case TemplateName::Template: |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3206 | // DNInfo work in progress: CHECKME: what about DNLoc? |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3207 | return DeclarationNameInfo(Name.getAsTemplateDecl()->getDeclName(), |
| 3208 | NameLoc); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3209 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3210 | case TemplateName::OverloadedTemplate: { |
| 3211 | OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate(); |
| 3212 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 3213 | return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc); |
| 3214 | } |
| 3215 | |
| 3216 | case TemplateName::DependentTemplate: { |
| 3217 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3218 | DeclarationName DName; |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3219 | if (DTN->isIdentifier()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3220 | DName = DeclarationNames.getIdentifier(DTN->getIdentifier()); |
| 3221 | return DeclarationNameInfo(DName, NameLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3222 | } else { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3223 | DName = DeclarationNames.getCXXOperatorName(DTN->getOperator()); |
| 3224 | // DNInfo work in progress: FIXME: source locations? |
| 3225 | DeclarationNameLoc DNLoc; |
| 3226 | DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding(); |
| 3227 | DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding(); |
| 3228 | return DeclarationNameInfo(DName, NameLoc, DNLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3229 | } |
| 3230 | } |
| 3231 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3232 | case TemplateName::SubstTemplateTemplateParm: { |
| 3233 | SubstTemplateTemplateParmStorage *subst |
| 3234 | = Name.getAsSubstTemplateTemplateParm(); |
| 3235 | return DeclarationNameInfo(subst->getParameter()->getDeclName(), |
| 3236 | NameLoc); |
| 3237 | } |
| 3238 | |
| 3239 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 3240 | SubstTemplateTemplateParmPackStorage *subst |
| 3241 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 3242 | return DeclarationNameInfo(subst->getParameterPack()->getDeclName(), |
| 3243 | NameLoc); |
| 3244 | } |
| 3245 | } |
| 3246 | |
| 3247 | llvm_unreachable("bad template name kind!"); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3248 | } |
| 3249 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3250 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3251 | switch (Name.getKind()) { |
| 3252 | case TemplateName::QualifiedTemplate: |
| 3253 | case TemplateName::Template: { |
| 3254 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3255 | if (TemplateTemplateParmDecl *TTP |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3256 | = dyn_cast<TemplateTemplateParmDecl>(Template)) |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3257 | Template = getCanonicalTemplateTemplateParmDecl(TTP); |
| 3258 | |
| 3259 | // The canonical template name is the canonical template declaration. |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 3260 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3261 | } |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 3262 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3263 | case TemplateName::OverloadedTemplate: |
| 3264 | llvm_unreachable("cannot canonicalize overloaded template"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3265 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3266 | case TemplateName::DependentTemplate: { |
| 3267 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 3268 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 3269 | return DTN->CanonicalTemplateName; |
| 3270 | } |
| 3271 | |
| 3272 | case TemplateName::SubstTemplateTemplateParm: { |
| 3273 | SubstTemplateTemplateParmStorage *subst |
| 3274 | = Name.getAsSubstTemplateTemplateParm(); |
| 3275 | return getCanonicalTemplateName(subst->getReplacement()); |
| 3276 | } |
| 3277 | |
| 3278 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 3279 | SubstTemplateTemplateParmPackStorage *subst |
| 3280 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 3281 | TemplateTemplateParmDecl *canonParameter |
| 3282 | = getCanonicalTemplateTemplateParmDecl(subst->getParameterPack()); |
| 3283 | TemplateArgument canonArgPack |
| 3284 | = getCanonicalTemplateArgument(subst->getArgumentPack()); |
| 3285 | return getSubstTemplateTemplateParmPack(canonParameter, canonArgPack); |
| 3286 | } |
| 3287 | } |
| 3288 | |
| 3289 | llvm_unreachable("bad template name!"); |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 3290 | } |
| 3291 | |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 3292 | bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) { |
| 3293 | X = getCanonicalTemplateName(X); |
| 3294 | Y = getCanonicalTemplateName(Y); |
| 3295 | return X.getAsVoidPointer() == Y.getAsVoidPointer(); |
| 3296 | } |
| 3297 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3298 | TemplateArgument |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3299 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const { |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3300 | switch (Arg.getKind()) { |
| 3301 | case TemplateArgument::Null: |
| 3302 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3303 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3304 | case TemplateArgument::Expression: |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3305 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3306 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3307 | case TemplateArgument::Declaration: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3308 | return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3309 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3310 | case TemplateArgument::Template: |
| 3311 | return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate())); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3312 | |
| 3313 | case TemplateArgument::TemplateExpansion: |
| 3314 | return TemplateArgument(getCanonicalTemplateName( |
| 3315 | Arg.getAsTemplateOrTemplatePattern()), |
Douglas Gregor | 2be29f4 | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 3316 | Arg.getNumTemplateExpansions()); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3317 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3318 | case TemplateArgument::Integral: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3319 | return TemplateArgument(*Arg.getAsIntegral(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3320 | getCanonicalType(Arg.getIntegralType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3321 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3322 | case TemplateArgument::Type: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3323 | return TemplateArgument(getCanonicalType(Arg.getAsType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3324 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3325 | case TemplateArgument::Pack: { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 3326 | if (Arg.pack_size() == 0) |
| 3327 | return Arg; |
| 3328 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3329 | TemplateArgument *CanonArgs |
| 3330 | = new (*this) TemplateArgument[Arg.pack_size()]; |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3331 | unsigned Idx = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3332 | for (TemplateArgument::pack_iterator A = Arg.pack_begin(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3333 | AEnd = Arg.pack_end(); |
| 3334 | A != AEnd; (void)++A, ++Idx) |
| 3335 | CanonArgs[Idx] = getCanonicalTemplateArgument(*A); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3336 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3337 | return TemplateArgument(CanonArgs, Arg.pack_size()); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3338 | } |
| 3339 | } |
| 3340 | |
| 3341 | // Silence GCC warning |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3342 | llvm_unreachable("Unhandled template argument kind"); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3343 | } |
| 3344 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3345 | NestedNameSpecifier * |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3346 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3347 | if (!NNS) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3348 | return 0; |
| 3349 | |
| 3350 | switch (NNS->getKind()) { |
| 3351 | case NestedNameSpecifier::Identifier: |
| 3352 | // Canonicalize the prefix but keep the identifier the same. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3353 | return NestedNameSpecifier::Create(*this, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3354 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 3355 | NNS->getAsIdentifier()); |
| 3356 | |
| 3357 | case NestedNameSpecifier::Namespace: |
| 3358 | // A namespace is canonical; build a nested-name-specifier with |
| 3359 | // this namespace and no prefix. |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 3360 | return NestedNameSpecifier::Create(*this, 0, |
| 3361 | NNS->getAsNamespace()->getOriginalNamespace()); |
| 3362 | |
| 3363 | case NestedNameSpecifier::NamespaceAlias: |
| 3364 | // A namespace is canonical; build a nested-name-specifier with |
| 3365 | // this namespace and no prefix. |
| 3366 | return NestedNameSpecifier::Create(*this, 0, |
| 3367 | NNS->getAsNamespaceAlias()->getNamespace() |
| 3368 | ->getOriginalNamespace()); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3369 | |
| 3370 | case NestedNameSpecifier::TypeSpec: |
| 3371 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 3372 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3373 | |
| 3374 | // If we have some kind of dependent-named type (e.g., "typename T::type"), |
| 3375 | // break it apart into its prefix and identifier, then reconsititute those |
| 3376 | // as the canonical nested-name-specifier. This is required to canonicalize |
| 3377 | // a dependent nested-name-specifier involving typedefs of dependent-name |
| 3378 | // types, e.g., |
| 3379 | // typedef typename T::type T1; |
| 3380 | // typedef typename T1::type T2; |
| 3381 | if (const DependentNameType *DNT = T->getAs<DependentNameType>()) { |
| 3382 | NestedNameSpecifier *Prefix |
| 3383 | = getCanonicalNestedNameSpecifier(DNT->getQualifier()); |
| 3384 | return NestedNameSpecifier::Create(*this, Prefix, |
| 3385 | const_cast<IdentifierInfo *>(DNT->getIdentifier())); |
| 3386 | } |
| 3387 | |
Douglas Gregor | 643f843 | 2010-11-04 00:14:23 +0000 | [diff] [blame] | 3388 | // Do the same thing as above, but with dependent-named specializations. |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3389 | if (const DependentTemplateSpecializationType *DTST |
| 3390 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 3391 | NestedNameSpecifier *Prefix |
| 3392 | = getCanonicalNestedNameSpecifier(DTST->getQualifier()); |
Douglas Gregor | aa2187d | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 3393 | |
| 3394 | T = getDependentTemplateSpecializationType(DTST->getKeyword(), |
| 3395 | Prefix, DTST->getIdentifier(), |
| 3396 | DTST->getNumArgs(), |
| 3397 | DTST->getArgs()); |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3398 | T = getCanonicalType(T); |
| 3399 | } |
| 3400 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3401 | return NestedNameSpecifier::Create(*this, 0, false, |
| 3402 | const_cast<Type*>(T.getTypePtr())); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3403 | } |
| 3404 | |
| 3405 | case NestedNameSpecifier::Global: |
| 3406 | // The global specifier is canonical and unique. |
| 3407 | return NNS; |
| 3408 | } |
| 3409 | |
| 3410 | // Required to silence a GCC warning |
| 3411 | return 0; |
| 3412 | } |
| 3413 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3414 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3415 | const ArrayType *ASTContext::getAsArrayType(QualType T) const { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3416 | // Handle the non-qualified case efficiently. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3417 | if (!T.hasLocalQualifiers()) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3418 | // Handle the common positive case fast. |
| 3419 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 3420 | return AT; |
| 3421 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3422 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3423 | // Handle the common negative case fast. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3424 | if (!isa<ArrayType>(T.getCanonicalType())) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3425 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3426 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3427 | // Apply any qualifiers from the array type to the element type. This |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3428 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 3429 | // 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] | 3430 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3431 | // If we get here, we either have type qualifiers on the type, or we have |
| 3432 | // 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] | 3433 | // we must propagate them down into the element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3434 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3435 | SplitQualType split = T.getSplitDesugaredType(); |
| 3436 | Qualifiers qs = split.second; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3437 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3438 | // If we have a simple case, just return now. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3439 | const ArrayType *ATy = dyn_cast<ArrayType>(split.first); |
| 3440 | if (ATy == 0 || qs.empty()) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3441 | return ATy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3442 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3443 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 3444 | // qualifiers into the array element type and return a new array type. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3445 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3446 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3447 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 3448 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 3449 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3450 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3451 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 3452 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 3453 | IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3454 | IAT->getIndexTypeCVRQualifiers())); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3455 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3456 | if (const DependentSizedArrayType *DSAT |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3457 | = dyn_cast<DependentSizedArrayType>(ATy)) |
| 3458 | return cast<ArrayType>( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3459 | getDependentSizedArrayType(NewEltTy, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3460 | DSAT->getSizeExpr(), |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3461 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3462 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3463 | DSAT->getBracketsRange())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3464 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3465 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3466 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3467 | VAT->getSizeExpr(), |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3468 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3469 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3470 | VAT->getBracketsRange())); |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 3471 | } |
| 3472 | |
Douglas Gregor | 79e6bd3 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 3473 | QualType ASTContext::getAdjustedParameterType(QualType T) { |
| 3474 | // C99 6.7.5.3p7: |
| 3475 | // A declaration of a parameter as "array of type" shall be |
| 3476 | // adjusted to "qualified pointer to type", where the type |
| 3477 | // qualifiers (if any) are those specified within the [ and ] of |
| 3478 | // the array type derivation. |
| 3479 | if (T->isArrayType()) |
| 3480 | return getArrayDecayedType(T); |
| 3481 | |
| 3482 | // C99 6.7.5.3p8: |
| 3483 | // A declaration of a parameter as "function returning type" |
| 3484 | // shall be adjusted to "pointer to function returning type", as |
| 3485 | // in 6.3.2.1. |
| 3486 | if (T->isFunctionType()) |
| 3487 | return getPointerType(T); |
| 3488 | |
| 3489 | return T; |
| 3490 | } |
| 3491 | |
| 3492 | QualType ASTContext::getSignatureParameterType(QualType T) { |
| 3493 | T = getVariableArrayDecayedType(T); |
| 3494 | T = getAdjustedParameterType(T); |
| 3495 | return T.getUnqualifiedType(); |
| 3496 | } |
| 3497 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3498 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 3499 | /// specified array type to a pointer. This operation is non-trivial when |
| 3500 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 3501 | /// this returns a pointer to a properly qualified element of the array. |
| 3502 | /// |
| 3503 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3504 | QualType ASTContext::getArrayDecayedType(QualType Ty) const { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3505 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 3506 | // typedefs in the element type of the array. This also handles propagation |
| 3507 | // of type qualifiers from the array type into the element type if present |
| 3508 | // (C99 6.7.3p8). |
| 3509 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 3510 | assert(PrettyArrayType && "Not an array type!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3511 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3512 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3513 | |
| 3514 | // int x[restrict 4] -> int *restrict |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3515 | return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3516 | } |
| 3517 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3518 | QualType ASTContext::getBaseElementType(const ArrayType *array) const { |
| 3519 | return getBaseElementType(array->getElementType()); |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 3520 | } |
| 3521 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3522 | QualType ASTContext::getBaseElementType(QualType type) const { |
| 3523 | Qualifiers qs; |
| 3524 | while (true) { |
| 3525 | SplitQualType split = type.getSplitDesugaredType(); |
| 3526 | const ArrayType *array = split.first->getAsArrayTypeUnsafe(); |
| 3527 | if (!array) break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3528 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3529 | type = array->getElementType(); |
| 3530 | qs.addConsistentQualifiers(split.second); |
| 3531 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3532 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3533 | return getQualifiedType(type, qs); |
Anders Carlsson | 6183a99 | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 3534 | } |
| 3535 | |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 3536 | /// getConstantArrayElementCount - Returns number of constant array elements. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3537 | uint64_t |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 3538 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
| 3539 | uint64_t ElementCount = 1; |
| 3540 | do { |
| 3541 | ElementCount *= CA->getSize().getZExtValue(); |
| 3542 | CA = dyn_cast<ConstantArrayType>(CA->getElementType()); |
| 3543 | } while (CA); |
| 3544 | return ElementCount; |
| 3545 | } |
| 3546 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3547 | /// getFloatingRank - Return a relative rank for floating point types. |
| 3548 | /// 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] | 3549 | static FloatingRank getFloatingRank(QualType T) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3550 | if (const ComplexType *CT = T->getAs<ComplexType>()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3551 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3552 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3553 | assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"); |
| 3554 | switch (T->getAs<BuiltinType>()->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3555 | default: llvm_unreachable("getFloatingRank(): not a floating type"); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3556 | case BuiltinType::Half: return HalfRank; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3557 | case BuiltinType::Float: return FloatRank; |
| 3558 | case BuiltinType::Double: return DoubleRank; |
| 3559 | case BuiltinType::LongDouble: return LongDoubleRank; |
| 3560 | } |
| 3561 | } |
| 3562 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3563 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 3564 | /// point or a complex type (based on typeDomain/typeSize). |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 3565 | /// 'typeDomain' is a real floating point or complex type. |
| 3566 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 3567 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 3568 | QualType Domain) const { |
| 3569 | FloatingRank EltRank = getFloatingRank(Size); |
| 3570 | if (Domain->isComplexType()) { |
| 3571 | switch (EltRank) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3572 | default: llvm_unreachable("getFloatingRank(): illegal value for rank"); |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 3573 | case FloatRank: return FloatComplexTy; |
| 3574 | case DoubleRank: return DoubleComplexTy; |
| 3575 | case LongDoubleRank: return LongDoubleComplexTy; |
| 3576 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3577 | } |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 3578 | |
| 3579 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 3580 | switch (EltRank) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3581 | default: llvm_unreachable("getFloatingRank(): illegal value for rank"); |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 3582 | case FloatRank: return FloatTy; |
| 3583 | case DoubleRank: return DoubleTy; |
| 3584 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 3585 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3586 | } |
| 3587 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3588 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 3589 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 3590 | /// '_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] | 3591 | /// LHS < RHS, return -1. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3592 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const { |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3593 | FloatingRank LHSR = getFloatingRank(LHS); |
| 3594 | FloatingRank RHSR = getFloatingRank(RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3595 | |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3596 | if (LHSR == RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 3597 | return 0; |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3598 | if (LHSR > RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 3599 | return 1; |
| 3600 | return -1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3601 | } |
| 3602 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3603 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 3604 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 3605 | /// or if it is not canonicalized. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3606 | unsigned ASTContext::getIntegerRank(const Type *T) const { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 3607 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3608 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3609 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3610 | default: llvm_unreachable("getIntegerRank(): not a built-in integer"); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3611 | case BuiltinType::Bool: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3612 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3613 | case BuiltinType::Char_S: |
| 3614 | case BuiltinType::Char_U: |
| 3615 | case BuiltinType::SChar: |
| 3616 | case BuiltinType::UChar: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3617 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3618 | case BuiltinType::Short: |
| 3619 | case BuiltinType::UShort: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3620 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3621 | case BuiltinType::Int: |
| 3622 | case BuiltinType::UInt: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3623 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3624 | case BuiltinType::Long: |
| 3625 | case BuiltinType::ULong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3626 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3627 | case BuiltinType::LongLong: |
| 3628 | case BuiltinType::ULongLong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3629 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 3630 | case BuiltinType::Int128: |
| 3631 | case BuiltinType::UInt128: |
| 3632 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3633 | } |
| 3634 | } |
| 3635 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3636 | /// \brief Whether this is a promotable bitfield reference according |
| 3637 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
| 3638 | /// |
| 3639 | /// \returns the type this bit-field will promote to, or NULL if no |
| 3640 | /// promotion occurs. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3641 | QualType ASTContext::isPromotableBitField(Expr *E) const { |
Douglas Gregor | ceafbde | 2010-05-24 20:13:53 +0000 | [diff] [blame] | 3642 | if (E->isTypeDependent() || E->isValueDependent()) |
| 3643 | return QualType(); |
| 3644 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3645 | FieldDecl *Field = E->getBitField(); |
| 3646 | if (!Field) |
| 3647 | return QualType(); |
| 3648 | |
| 3649 | QualType FT = Field->getType(); |
| 3650 | |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3651 | uint64_t BitWidth = Field->getBitWidthValue(*this); |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3652 | uint64_t IntSize = getTypeSize(IntTy); |
| 3653 | // GCC extension compatibility: if the bit-field size is less than or equal |
| 3654 | // to the size of int, it gets promoted no matter what its type is. |
| 3655 | // For instance, unsigned long bf : 4 gets promoted to signed int. |
| 3656 | if (BitWidth < IntSize) |
| 3657 | return IntTy; |
| 3658 | |
| 3659 | if (BitWidth == IntSize) |
| 3660 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
| 3661 | |
| 3662 | // Types bigger than int are not subject to promotions, and therefore act |
| 3663 | // like the base type. |
| 3664 | // FIXME: This doesn't quite match what gcc does, but what gcc does here |
| 3665 | // is ridiculous. |
| 3666 | return QualType(); |
| 3667 | } |
| 3668 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3669 | /// getPromotedIntegerType - Returns the type that Promotable will |
| 3670 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
| 3671 | /// integer type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3672 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) const { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3673 | assert(!Promotable.isNull()); |
| 3674 | assert(Promotable->isPromotableIntegerType()); |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 3675 | if (const EnumType *ET = Promotable->getAs<EnumType>()) |
| 3676 | return ET->getDecl()->getPromotionType(); |
Eli Friedman | 68a2dc4 | 2011-10-26 07:22:48 +0000 | [diff] [blame] | 3677 | |
| 3678 | if (const BuiltinType *BT = Promotable->getAs<BuiltinType>()) { |
| 3679 | // C++ [conv.prom]: A prvalue of type char16_t, char32_t, or wchar_t |
| 3680 | // (3.9.1) can be converted to a prvalue of the first of the following |
| 3681 | // types that can represent all the values of its underlying type: |
| 3682 | // int, unsigned int, long int, unsigned long int, long long int, or |
| 3683 | // unsigned long long int [...] |
| 3684 | // FIXME: Is there some better way to compute this? |
| 3685 | if (BT->getKind() == BuiltinType::WChar_S || |
| 3686 | BT->getKind() == BuiltinType::WChar_U || |
| 3687 | BT->getKind() == BuiltinType::Char16 || |
| 3688 | BT->getKind() == BuiltinType::Char32) { |
| 3689 | bool FromIsSigned = BT->getKind() == BuiltinType::WChar_S; |
| 3690 | uint64_t FromSize = getTypeSize(BT); |
| 3691 | QualType PromoteTypes[] = { IntTy, UnsignedIntTy, LongTy, UnsignedLongTy, |
| 3692 | LongLongTy, UnsignedLongLongTy }; |
| 3693 | for (size_t Idx = 0; Idx < llvm::array_lengthof(PromoteTypes); ++Idx) { |
| 3694 | uint64_t ToSize = getTypeSize(PromoteTypes[Idx]); |
| 3695 | if (FromSize < ToSize || |
| 3696 | (FromSize == ToSize && |
| 3697 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) |
| 3698 | return PromoteTypes[Idx]; |
| 3699 | } |
| 3700 | llvm_unreachable("char type should fit into long long"); |
| 3701 | } |
| 3702 | } |
| 3703 | |
| 3704 | // At this point, we should have a signed or unsigned integer type. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3705 | if (Promotable->isSignedIntegerType()) |
| 3706 | return IntTy; |
| 3707 | uint64_t PromotableSize = getTypeSize(Promotable); |
| 3708 | uint64_t IntSize = getTypeSize(IntTy); |
| 3709 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
| 3710 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
| 3711 | } |
| 3712 | |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 3713 | /// \brief Recurses in pointer/array types until it finds an objc retainable |
| 3714 | /// type and returns its ownership. |
| 3715 | Qualifiers::ObjCLifetime ASTContext::getInnerObjCOwnership(QualType T) const { |
| 3716 | while (!T.isNull()) { |
| 3717 | if (T.getObjCLifetime() != Qualifiers::OCL_None) |
| 3718 | return T.getObjCLifetime(); |
| 3719 | if (T->isArrayType()) |
| 3720 | T = getBaseElementType(T); |
| 3721 | else if (const PointerType *PT = T->getAs<PointerType>()) |
| 3722 | T = PT->getPointeeType(); |
| 3723 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
Argyrios Kyrtzidis | 28445f0 | 2011-07-01 23:01:46 +0000 | [diff] [blame] | 3724 | T = RT->getPointeeType(); |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 3725 | else |
| 3726 | break; |
| 3727 | } |
| 3728 | |
| 3729 | return Qualifiers::OCL_None; |
| 3730 | } |
| 3731 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3732 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3733 | /// 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] | 3734 | /// LHS < RHS, return -1. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3735 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3736 | const Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 3737 | const Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3738 | if (LHSC == RHSC) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3739 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3740 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 3741 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3742 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3743 | unsigned LHSRank = getIntegerRank(LHSC); |
| 3744 | unsigned RHSRank = getIntegerRank(RHSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3745 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3746 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 3747 | if (LHSRank == RHSRank) return 0; |
| 3748 | return LHSRank > RHSRank ? 1 : -1; |
| 3749 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3750 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3751 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 3752 | if (LHSUnsigned) { |
| 3753 | // If the unsigned [LHS] type is larger, return it. |
| 3754 | if (LHSRank >= RHSRank) |
| 3755 | return 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3756 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3757 | // If the signed type can represent all values of the unsigned type, it |
| 3758 | // 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] | 3759 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3760 | return -1; |
| 3761 | } |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3762 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3763 | // If the unsigned [RHS] type is larger, return it. |
| 3764 | if (RHSRank >= LHSRank) |
| 3765 | return -1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3766 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3767 | // If the signed type can represent all values of the unsigned type, it |
| 3768 | // 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] | 3769 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3770 | return 1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3771 | } |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3772 | |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3773 | static RecordDecl * |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3774 | CreateRecordDecl(const ASTContext &Ctx, RecordDecl::TagKind TK, |
| 3775 | DeclContext *DC, IdentifierInfo *Id) { |
| 3776 | SourceLocation Loc; |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3777 | if (Ctx.getLangOptions().CPlusPlus) |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3778 | return CXXRecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3779 | else |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3780 | return RecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3781 | } |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3782 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3783 | // getCFConstantStringType - Return the type used for constant CFStrings. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3784 | QualType ASTContext::getCFConstantStringType() const { |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3785 | if (!CFConstantStringTypeDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3786 | CFConstantStringTypeDecl = |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3787 | CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3788 | &Idents.get("NSConstantString")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3789 | CFConstantStringTypeDecl->startDefinition(); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3790 | |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 3791 | QualType FieldTypes[4]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3792 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3793 | // const int *isa; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3794 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 3795 | // int flags; |
| 3796 | FieldTypes[1] = IntTy; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3797 | // const char *str; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3798 | FieldTypes[2] = getPointerType(CharTy.withConst()); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3799 | // long length; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3800 | FieldTypes[3] = LongTy; |
| 3801 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3802 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3803 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3804 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3805 | SourceLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3806 | SourceLocation(), 0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3807 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3808 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3809 | /*Mutable=*/false, |
| 3810 | /*HasInit=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3811 | Field->setAccess(AS_public); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3812 | CFConstantStringTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3813 | } |
| 3814 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3815 | CFConstantStringTypeDecl->completeDefinition(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3816 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3817 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3818 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 8467583 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 3819 | } |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 3820 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3821 | void ASTContext::setCFConstantStringType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3822 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3823 | assert(Rec && "Invalid CFConstantStringType"); |
| 3824 | CFConstantStringTypeDecl = Rec->getDecl(); |
| 3825 | } |
| 3826 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3827 | QualType ASTContext::getBlockDescriptorType() const { |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3828 | if (BlockDescriptorType) |
| 3829 | return getTagDeclType(BlockDescriptorType); |
| 3830 | |
| 3831 | RecordDecl *T; |
| 3832 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3833 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3834 | &Idents.get("__block_descriptor")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3835 | T->startDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3836 | |
| 3837 | QualType FieldTypes[] = { |
| 3838 | UnsignedLongTy, |
| 3839 | UnsignedLongTy, |
| 3840 | }; |
| 3841 | |
| 3842 | const char *FieldNames[] = { |
| 3843 | "reserved", |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3844 | "Size" |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3845 | }; |
| 3846 | |
| 3847 | for (size_t i = 0; i < 2; ++i) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3848 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3849 | SourceLocation(), |
| 3850 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3851 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3852 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3853 | /*Mutable=*/false, |
| 3854 | /*HasInit=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3855 | Field->setAccess(AS_public); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3856 | T->addDecl(Field); |
| 3857 | } |
| 3858 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3859 | T->completeDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3860 | |
| 3861 | BlockDescriptorType = T; |
| 3862 | |
| 3863 | return getTagDeclType(BlockDescriptorType); |
| 3864 | } |
| 3865 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3866 | QualType ASTContext::getBlockDescriptorExtendedType() const { |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3867 | if (BlockDescriptorExtendedType) |
| 3868 | return getTagDeclType(BlockDescriptorExtendedType); |
| 3869 | |
| 3870 | RecordDecl *T; |
| 3871 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3872 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3873 | &Idents.get("__block_descriptor_withcopydispose")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3874 | T->startDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3875 | |
| 3876 | QualType FieldTypes[] = { |
| 3877 | UnsignedLongTy, |
| 3878 | UnsignedLongTy, |
| 3879 | getPointerType(VoidPtrTy), |
| 3880 | getPointerType(VoidPtrTy) |
| 3881 | }; |
| 3882 | |
| 3883 | const char *FieldNames[] = { |
| 3884 | "reserved", |
| 3885 | "Size", |
| 3886 | "CopyFuncPtr", |
| 3887 | "DestroyFuncPtr" |
| 3888 | }; |
| 3889 | |
| 3890 | for (size_t i = 0; i < 4; ++i) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3891 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3892 | SourceLocation(), |
| 3893 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3894 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3895 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3896 | /*Mutable=*/false, |
| 3897 | /*HasInit=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3898 | Field->setAccess(AS_public); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3899 | T->addDecl(Field); |
| 3900 | } |
| 3901 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3902 | T->completeDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3903 | |
| 3904 | BlockDescriptorExtendedType = T; |
| 3905 | |
| 3906 | return getTagDeclType(BlockDescriptorExtendedType); |
| 3907 | } |
| 3908 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3909 | bool ASTContext::BlockRequiresCopying(QualType Ty) const { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3910 | if (Ty->isObjCRetainableType()) |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3911 | return true; |
Fariborz Jahanian | e38be61 | 2010-11-17 00:21:28 +0000 | [diff] [blame] | 3912 | if (getLangOptions().CPlusPlus) { |
| 3913 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3914 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 3915 | return RD->hasConstCopyConstructor(); |
Fariborz Jahanian | e38be61 | 2010-11-17 00:21:28 +0000 | [diff] [blame] | 3916 | |
| 3917 | } |
| 3918 | } |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3919 | return false; |
| 3920 | } |
| 3921 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3922 | QualType |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3923 | ASTContext::BuildByRefType(StringRef DeclName, QualType Ty) const { |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3924 | // type = struct __Block_byref_1_X { |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3925 | // void *__isa; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3926 | // struct __Block_byref_1_X *__forwarding; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3927 | // unsigned int __flags; |
| 3928 | // unsigned int __size; |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 3929 | // void *__copy_helper; // as needed |
| 3930 | // void *__destroy_help // as needed |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3931 | // int X; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3932 | // } * |
| 3933 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3934 | bool HasCopyAndDispose = BlockRequiresCopying(Ty); |
| 3935 | |
| 3936 | // FIXME: Move up |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 3937 | llvm::SmallString<36> Name; |
| 3938 | llvm::raw_svector_ostream(Name) << "__Block_byref_" << |
| 3939 | ++UniqueBlockByRefTypeID << '_' << DeclName; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3940 | RecordDecl *T; |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3941 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, &Idents.get(Name.str())); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3942 | T->startDefinition(); |
| 3943 | QualType Int32Ty = IntTy; |
| 3944 | assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported"); |
| 3945 | QualType FieldTypes[] = { |
| 3946 | getPointerType(VoidPtrTy), |
| 3947 | getPointerType(getTagDeclType(T)), |
| 3948 | Int32Ty, |
| 3949 | Int32Ty, |
| 3950 | getPointerType(VoidPtrTy), |
| 3951 | getPointerType(VoidPtrTy), |
| 3952 | Ty |
| 3953 | }; |
| 3954 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3955 | StringRef FieldNames[] = { |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3956 | "__isa", |
| 3957 | "__forwarding", |
| 3958 | "__flags", |
| 3959 | "__size", |
| 3960 | "__copy_helper", |
| 3961 | "__destroy_helper", |
| 3962 | DeclName, |
| 3963 | }; |
| 3964 | |
| 3965 | for (size_t i = 0; i < 7; ++i) { |
| 3966 | if (!HasCopyAndDispose && i >=4 && i <= 5) |
| 3967 | continue; |
| 3968 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3969 | SourceLocation(), |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3970 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3971 | FieldTypes[i], /*TInfo=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3972 | /*BitWidth=*/0, /*Mutable=*/false, |
| 3973 | /*HasInit=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3974 | Field->setAccess(AS_public); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3975 | T->addDecl(Field); |
| 3976 | } |
| 3977 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3978 | T->completeDefinition(); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3979 | |
| 3980 | return getPointerType(getTagDeclType(T)); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3981 | } |
| 3982 | |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 3983 | TypedefDecl *ASTContext::getObjCInstanceTypeDecl() { |
| 3984 | if (!ObjCInstanceTypeDecl) |
| 3985 | ObjCInstanceTypeDecl = TypedefDecl::Create(*this, |
| 3986 | getTranslationUnitDecl(), |
| 3987 | SourceLocation(), |
| 3988 | SourceLocation(), |
| 3989 | &Idents.get("instancetype"), |
| 3990 | getTrivialTypeSourceInfo(getObjCIdType())); |
| 3991 | return ObjCInstanceTypeDecl; |
| 3992 | } |
| 3993 | |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3994 | // This returns true if a type has been typedefed to BOOL: |
| 3995 | // typedef <type> BOOL; |
Chris Lattner | 2d99833 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 3996 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3997 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | bb49c3e | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 3998 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 3999 | return II->isStr("BOOL"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4000 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4001 | return false; |
| 4002 | } |
| 4003 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4004 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4005 | /// purpose. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4006 | CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const { |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4007 | if (!type->isIncompleteArrayType() && type->isIncompleteType()) |
| 4008 | return CharUnits::Zero(); |
| 4009 | |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4010 | CharUnits sz = getTypeSizeInChars(type); |
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 | // 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] | 4013 | if (sz.isPositive() && type->isIntegralOrEnumerationType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4014 | sz = std::max(sz, getTypeSizeInChars(IntTy)); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4015 | // Treat arrays as pointers, since that's how they're passed in. |
| 4016 | else if (type->isArrayType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4017 | sz = getTypeSizeInChars(VoidPtrTy); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4018 | return sz; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4019 | } |
| 4020 | |
| 4021 | static inline |
| 4022 | std::string charUnitsToString(const CharUnits &CU) { |
| 4023 | return llvm::itostr(CU.getQuantity()); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4024 | } |
| 4025 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4026 | /// getObjCEncodingForBlock - Return the encoded type for this block |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4027 | /// declaration. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4028 | std::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const { |
| 4029 | std::string S; |
| 4030 | |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4031 | const BlockDecl *Decl = Expr->getBlockDecl(); |
| 4032 | QualType BlockTy = |
| 4033 | Expr->getType()->getAs<BlockPointerType>()->getPointeeType(); |
| 4034 | // Encode result type. |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 4035 | getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4036 | // Compute size of all parameters. |
| 4037 | // Start with computing size of a pointer in number of bytes. |
| 4038 | // FIXME: There might(should) be a better way of doing this computation! |
| 4039 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4040 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
| 4041 | CharUnits ParmOffset = PtrSize; |
Fariborz Jahanian | 6f46c26 | 2010-04-08 18:06:22 +0000 | [diff] [blame] | 4042 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4043 | E = Decl->param_end(); PI != E; ++PI) { |
| 4044 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4045 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4046 | assert (sz.isPositive() && "BlockExpr - Incomplete param type"); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4047 | ParmOffset += sz; |
| 4048 | } |
| 4049 | // Size of the argument frame |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4050 | S += charUnitsToString(ParmOffset); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4051 | // Block pointer and offset. |
| 4052 | S += "@?0"; |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4053 | |
| 4054 | // Argument types. |
| 4055 | ParmOffset = PtrSize; |
| 4056 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E = |
| 4057 | Decl->param_end(); PI != E; ++PI) { |
| 4058 | ParmVarDecl *PVDecl = *PI; |
| 4059 | QualType PType = PVDecl->getOriginalType(); |
| 4060 | if (const ArrayType *AT = |
| 4061 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4062 | // Use array's original type only if it has known number of |
| 4063 | // elements. |
| 4064 | if (!isa<ConstantArrayType>(AT)) |
| 4065 | PType = PVDecl->getType(); |
| 4066 | } else if (PType->isFunctionType()) |
| 4067 | PType = PVDecl->getType(); |
| 4068 | getObjCEncodingForType(PType, S); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4069 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4070 | ParmOffset += getObjCEncodingTypeSize(PType); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4071 | } |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4072 | |
| 4073 | return S; |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4074 | } |
| 4075 | |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4076 | bool ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl, |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4077 | std::string& S) { |
| 4078 | // Encode result type. |
| 4079 | getObjCEncodingForType(Decl->getResultType(), S); |
| 4080 | CharUnits ParmOffset; |
| 4081 | // Compute size of all parameters. |
| 4082 | for (FunctionDecl::param_const_iterator PI = Decl->param_begin(), |
| 4083 | E = Decl->param_end(); PI != E; ++PI) { |
| 4084 | QualType PType = (*PI)->getType(); |
| 4085 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4086 | if (sz.isZero()) |
| 4087 | return true; |
| 4088 | |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4089 | assert (sz.isPositive() && |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4090 | "getObjCEncodingForFunctionDecl - Incomplete param type"); |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4091 | ParmOffset += sz; |
| 4092 | } |
| 4093 | S += charUnitsToString(ParmOffset); |
| 4094 | ParmOffset = CharUnits::Zero(); |
| 4095 | |
| 4096 | // Argument types. |
| 4097 | for (FunctionDecl::param_const_iterator PI = Decl->param_begin(), |
| 4098 | E = Decl->param_end(); PI != E; ++PI) { |
| 4099 | ParmVarDecl *PVDecl = *PI; |
| 4100 | QualType PType = PVDecl->getOriginalType(); |
| 4101 | if (const ArrayType *AT = |
| 4102 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4103 | // Use array's original type only if it has known number of |
| 4104 | // elements. |
| 4105 | if (!isa<ConstantArrayType>(AT)) |
| 4106 | PType = PVDecl->getType(); |
| 4107 | } else if (PType->isFunctionType()) |
| 4108 | PType = PVDecl->getType(); |
| 4109 | getObjCEncodingForType(PType, S); |
| 4110 | S += charUnitsToString(ParmOffset); |
| 4111 | ParmOffset += getObjCEncodingTypeSize(PType); |
| 4112 | } |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4113 | |
| 4114 | return false; |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4115 | } |
| 4116 | |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4117 | /// getObjCEncodingForMethodParameter - Return the encoded type for a single |
| 4118 | /// method parameter or return type. If Extended, include class names and |
| 4119 | /// block object types. |
| 4120 | void ASTContext::getObjCEncodingForMethodParameter(Decl::ObjCDeclQualifier QT, |
| 4121 | QualType T, std::string& S, |
| 4122 | bool Extended) const { |
| 4123 | // Encode type qualifer, 'in', 'inout', etc. for the parameter. |
| 4124 | getObjCEncodingForTypeQualifier(QT, S); |
| 4125 | // Encode parameter type. |
| 4126 | getObjCEncodingForTypeImpl(T, S, true, true, 0, |
| 4127 | true /*OutermostType*/, |
| 4128 | false /*EncodingProperty*/, |
| 4129 | false /*StructField*/, |
| 4130 | Extended /*EncodeBlockParameters*/, |
| 4131 | Extended /*EncodeClassNames*/); |
| 4132 | } |
| 4133 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4134 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4135 | /// declaration. |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4136 | bool ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4137 | std::string& S, |
| 4138 | bool Extended) const { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4139 | // FIXME: This is not very efficient. |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4140 | // Encode return type. |
| 4141 | getObjCEncodingForMethodParameter(Decl->getObjCDeclQualifier(), |
| 4142 | Decl->getResultType(), S, Extended); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4143 | // Compute size of all parameters. |
| 4144 | // Start with computing size of a pointer in number of bytes. |
| 4145 | // FIXME: There might(should) be a better way of doing this computation! |
| 4146 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4147 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4148 | // The first two arguments (self and _cmd) are pointers; account for |
| 4149 | // their size. |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4150 | CharUnits ParmOffset = 2 * PtrSize; |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4151 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 4152 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 4153 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4154 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4155 | if (sz.isZero()) |
| 4156 | return true; |
| 4157 | |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4158 | assert (sz.isPositive() && |
| 4159 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4160 | ParmOffset += sz; |
| 4161 | } |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4162 | S += charUnitsToString(ParmOffset); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4163 | S += "@0:"; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4164 | S += charUnitsToString(PtrSize); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4165 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4166 | // Argument types. |
| 4167 | ParmOffset = 2 * PtrSize; |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4168 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 4169 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4170 | const ParmVarDecl *PVDecl = *PI; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4171 | QualType PType = PVDecl->getOriginalType(); |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 4172 | if (const ArrayType *AT = |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 4173 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4174 | // Use array's original type only if it has known number of |
| 4175 | // elements. |
Steve Naroff | bb3fde3 | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 4176 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 4177 | PType = PVDecl->getType(); |
| 4178 | } else if (PType->isFunctionType()) |
| 4179 | PType = PVDecl->getType(); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4180 | getObjCEncodingForMethodParameter(PVDecl->getObjCDeclQualifier(), |
| 4181 | PType, S, Extended); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4182 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4183 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4184 | } |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4185 | |
| 4186 | return false; |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4187 | } |
| 4188 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4189 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4190 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4191 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 4192 | /// NULL when getting encodings for protocol properties. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4193 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 4194 | /// attributes readonly and bycopy are encoded as single characters. The |
| 4195 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 4196 | /// encoded as single characters, followed by an identifier. Property types |
| 4197 | /// are also encoded as a parametrized attribute. The characters used to encode |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4198 | /// these attributes are defined by the following enumeration: |
| 4199 | /// @code |
| 4200 | /// enum PropertyAttributes { |
| 4201 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 4202 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 4203 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 4204 | /// kPropertyDynamic = 'D', // property is dynamic |
| 4205 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 4206 | /// kPropertySetter = 'S', // followed by setter selector name |
| 4207 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
| 4208 | /// kPropertyType = 't' // followed by old-style type encoding. |
| 4209 | /// kPropertyWeak = 'W' // 'weak' property |
| 4210 | /// kPropertyStrong = 'P' // property GC'able |
| 4211 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 4212 | /// }; |
| 4213 | /// @endcode |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4214 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4215 | const Decl *Container, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4216 | std::string& S) const { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4217 | // Collect information from the property implementation decl(s). |
| 4218 | bool Dynamic = false; |
| 4219 | ObjCPropertyImplDecl *SynthesizePID = 0; |
| 4220 | |
| 4221 | // FIXME: Duplicated code due to poor abstraction. |
| 4222 | if (Container) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4223 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4224 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
| 4225 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4226 | i = CID->propimpl_begin(), e = CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4227 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4228 | ObjCPropertyImplDecl *PID = *i; |
| 4229 | if (PID->getPropertyDecl() == PD) { |
| 4230 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 4231 | Dynamic = true; |
| 4232 | } else { |
| 4233 | SynthesizePID = PID; |
| 4234 | } |
| 4235 | } |
| 4236 | } |
| 4237 | } else { |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4238 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4239 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4240 | i = OID->propimpl_begin(), e = OID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4241 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4242 | ObjCPropertyImplDecl *PID = *i; |
| 4243 | if (PID->getPropertyDecl() == PD) { |
| 4244 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 4245 | Dynamic = true; |
| 4246 | } else { |
| 4247 | SynthesizePID = PID; |
| 4248 | } |
| 4249 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4250 | } |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4251 | } |
| 4252 | } |
| 4253 | |
| 4254 | // FIXME: This is not very efficient. |
| 4255 | S = "T"; |
| 4256 | |
| 4257 | // Encode result type. |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4258 | // GCC has some special rules regarding encoding of properties which |
| 4259 | // closely resembles encoding of ivars. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4260 | getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4261 | true /* outermost type */, |
| 4262 | true /* encoding for property */); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4263 | |
| 4264 | if (PD->isReadOnly()) { |
| 4265 | S += ",R"; |
| 4266 | } else { |
| 4267 | switch (PD->getSetterKind()) { |
| 4268 | case ObjCPropertyDecl::Assign: break; |
| 4269 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4270 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
Fariborz Jahanian | 3a02b44 | 2011-08-12 20:47:08 +0000 | [diff] [blame] | 4271 | case ObjCPropertyDecl::Weak: S += ",W"; break; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4272 | } |
| 4273 | } |
| 4274 | |
| 4275 | // It really isn't clear at all what this means, since properties |
| 4276 | // are "dynamic by default". |
| 4277 | if (Dynamic) |
| 4278 | S += ",D"; |
| 4279 | |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4280 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 4281 | S += ",N"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4282 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4283 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 4284 | S += ",G"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4285 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4286 | } |
| 4287 | |
| 4288 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 4289 | S += ",S"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4290 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4291 | } |
| 4292 | |
| 4293 | if (SynthesizePID) { |
| 4294 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 4295 | S += ",V"; |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 4296 | S += OID->getNameAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4297 | } |
| 4298 | |
| 4299 | // FIXME: OBJCGC: weak & strong |
| 4300 | } |
| 4301 | |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4302 | /// getLegacyIntegralTypeEncoding - |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4303 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
| 4304 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4305 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
| 4306 | /// |
| 4307 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 4308 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4309 | if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) { |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4310 | if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4311 | PointeeTy = UnsignedIntTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4312 | else |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4313 | if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4314 | PointeeTy = IntTy; |
| 4315 | } |
| 4316 | } |
| 4317 | } |
| 4318 | |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4319 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4320 | const FieldDecl *Field) const { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4321 | // We follow the behavior of gcc, expanding structures which are |
| 4322 | // directly pointed to, and expanding embedded structures. Note that |
| 4323 | // these rules are sufficient to prevent recursive encoding of the |
| 4324 | // same type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4325 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
Fariborz Jahanian | 5b8c7d9 | 2008-12-22 23:22:27 +0000 | [diff] [blame] | 4326 | true /* outermost type */); |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4327 | } |
| 4328 | |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4329 | static char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) { |
| 4330 | switch (T->getAs<BuiltinType>()->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4331 | default: llvm_unreachable("Unhandled builtin type kind"); |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4332 | case BuiltinType::Void: return 'v'; |
| 4333 | case BuiltinType::Bool: return 'B'; |
| 4334 | case BuiltinType::Char_U: |
| 4335 | case BuiltinType::UChar: return 'C'; |
| 4336 | case BuiltinType::UShort: return 'S'; |
| 4337 | case BuiltinType::UInt: return 'I'; |
| 4338 | case BuiltinType::ULong: |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4339 | return C->getIntWidth(T) == 32 ? 'L' : 'Q'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4340 | case BuiltinType::UInt128: return 'T'; |
| 4341 | case BuiltinType::ULongLong: return 'Q'; |
| 4342 | case BuiltinType::Char_S: |
| 4343 | case BuiltinType::SChar: return 'c'; |
| 4344 | case BuiltinType::Short: return 's'; |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 4345 | case BuiltinType::WChar_S: |
| 4346 | case BuiltinType::WChar_U: |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4347 | case BuiltinType::Int: return 'i'; |
| 4348 | case BuiltinType::Long: |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4349 | return C->getIntWidth(T) == 32 ? 'l' : 'q'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4350 | case BuiltinType::LongLong: return 'q'; |
| 4351 | case BuiltinType::Int128: return 't'; |
| 4352 | case BuiltinType::Float: return 'f'; |
| 4353 | case BuiltinType::Double: return 'd'; |
Daniel Dunbar | 3a0be84 | 2010-10-11 21:13:48 +0000 | [diff] [blame] | 4354 | case BuiltinType::LongDouble: return 'D'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4355 | } |
| 4356 | } |
| 4357 | |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4358 | static char ObjCEncodingForEnumType(const ASTContext *C, const EnumType *ET) { |
| 4359 | EnumDecl *Enum = ET->getDecl(); |
| 4360 | |
| 4361 | // The encoding of an non-fixed enum type is always 'i', regardless of size. |
| 4362 | if (!Enum->isFixed()) |
| 4363 | return 'i'; |
| 4364 | |
| 4365 | // The encoding of a fixed enum type matches its fixed underlying type. |
| 4366 | return ObjCEncodingForPrimitiveKind(C, Enum->getIntegerType()); |
| 4367 | } |
| 4368 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4369 | static void EncodeBitField(const ASTContext *Ctx, std::string& S, |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4370 | QualType T, const FieldDecl *FD) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4371 | assert(FD->isBitField() && "not a bitfield - getObjCEncodingForTypeImpl"); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4372 | S += 'b'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4373 | // The NeXT runtime encodes bit fields as b followed by the number of bits. |
| 4374 | // The GNU runtime requires more information; bitfields are encoded as b, |
| 4375 | // then the offset (in bits) of the first element, then the type of the |
| 4376 | // bitfield, then the size in bits. For example, in this structure: |
| 4377 | // |
| 4378 | // struct |
| 4379 | // { |
| 4380 | // int integer; |
| 4381 | // int flags:2; |
| 4382 | // }; |
| 4383 | // On a 32-bit system, the encoding for flags would be b2 for the NeXT |
| 4384 | // runtime, but b32i2 for the GNU runtime. The reason for this extra |
| 4385 | // information is not especially sensible, but we're stuck with it for |
| 4386 | // compatibility with GCC, although providing it breaks anything that |
| 4387 | // actually uses runtime introspection and wants to work on both runtimes... |
| 4388 | if (!Ctx->getLangOptions().NeXTRuntime) { |
| 4389 | const RecordDecl *RD = FD->getParent(); |
| 4390 | const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD); |
Eli Friedman | 8290574 | 2011-07-07 01:54:01 +0000 | [diff] [blame] | 4391 | S += llvm::utostr(RL.getFieldOffset(FD->getFieldIndex())); |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4392 | if (const EnumType *ET = T->getAs<EnumType>()) |
| 4393 | S += ObjCEncodingForEnumType(Ctx, ET); |
David Chisnall | c7ff82c | 2010-12-26 20:12:30 +0000 | [diff] [blame] | 4394 | else |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4395 | S += ObjCEncodingForPrimitiveKind(Ctx, T); |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4396 | } |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4397 | S += llvm::utostr(FD->getBitWidthValue(*Ctx)); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4398 | } |
| 4399 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 4400 | // FIXME: Use SmallString for accumulating string. |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4401 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 4402 | bool ExpandPointedToStructures, |
| 4403 | bool ExpandStructures, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 4404 | const FieldDecl *FD, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4405 | bool OutermostType, |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4406 | bool EncodingProperty, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4407 | bool StructField, |
| 4408 | bool EncodeBlockParameters, |
| 4409 | bool EncodeClassNames) const { |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4410 | if (T->getAs<BuiltinType>()) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4411 | if (FD && FD->isBitField()) |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4412 | return EncodeBitField(this, S, T, FD); |
| 4413 | S += ObjCEncodingForPrimitiveKind(this, T); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4414 | return; |
| 4415 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4416 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4417 | if (const ComplexType *CT = T->getAs<ComplexType>()) { |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 4418 | S += 'j'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4419 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false, |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 4420 | false); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4421 | return; |
| 4422 | } |
Fariborz Jahanian | 60bce3e | 2009-11-23 20:40:50 +0000 | [diff] [blame] | 4423 | |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4424 | // encoding for pointer or r3eference types. |
| 4425 | QualType PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4426 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Fariborz Jahanian | 8d2c0a9 | 2009-11-30 18:43:52 +0000 | [diff] [blame] | 4427 | if (PT->isObjCSelType()) { |
| 4428 | S += ':'; |
| 4429 | return; |
| 4430 | } |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4431 | PointeeTy = PT->getPointeeType(); |
| 4432 | } |
| 4433 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 4434 | PointeeTy = RT->getPointeeType(); |
| 4435 | if (!PointeeTy.isNull()) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4436 | bool isReadOnly = false; |
| 4437 | // For historical/compatibility reasons, the read-only qualifier of the |
| 4438 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 4439 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4440 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 4441 | if (isa<TypedefType>(T.getTypePtr())) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4442 | if (OutermostType && T.isConstQualified()) { |
| 4443 | isReadOnly = true; |
| 4444 | S += 'r'; |
| 4445 | } |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 4446 | } else if (OutermostType) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4447 | QualType P = PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4448 | while (P->getAs<PointerType>()) |
| 4449 | P = P->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4450 | if (P.isConstQualified()) { |
| 4451 | isReadOnly = true; |
| 4452 | S += 'r'; |
| 4453 | } |
| 4454 | } |
| 4455 | if (isReadOnly) { |
| 4456 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 4457 | // combinations need to be rearranged. |
| 4458 | // Rewrite "in const" from "nr" to "rn" |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4459 | if (StringRef(S).endswith("nr")) |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 4460 | S.replace(S.end()-2, S.end(), "rn"); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4461 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4462 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4463 | if (PointeeTy->isCharType()) { |
| 4464 | // char pointer types should be encoded as '*' unless it is a |
| 4465 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 4466 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4467 | S += '*'; |
| 4468 | return; |
| 4469 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4470 | } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) { |
Steve Naroff | 9533a7f | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 4471 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 4472 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 4473 | S += '#'; |
| 4474 | return; |
| 4475 | } |
| 4476 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 4477 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 4478 | S += '@'; |
| 4479 | return; |
| 4480 | } |
| 4481 | // fall through... |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4482 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4483 | S += '^'; |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4484 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 4485 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4486 | getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4487 | NULL); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4488 | return; |
| 4489 | } |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4490 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4491 | if (const ArrayType *AT = |
| 4492 | // Ignore type qualifiers etc. |
| 4493 | dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) { |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4494 | if (isa<IncompleteArrayType>(AT) && !StructField) { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4495 | // Incomplete arrays are encoded as a pointer to the array element. |
| 4496 | S += '^'; |
| 4497 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4498 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4499 | false, ExpandStructures, FD); |
| 4500 | } else { |
| 4501 | S += '['; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4502 | |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4503 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
| 4504 | if (getTypeSize(CAT->getElementType()) == 0) |
| 4505 | S += '0'; |
| 4506 | else |
| 4507 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 4508 | } else { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4509 | //Variable length arrays are encoded as a regular array with 0 elements. |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4510 | assert((isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) && |
| 4511 | "Unknown array type!"); |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4512 | S += '0'; |
| 4513 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4514 | |
| 4515 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4516 | false, ExpandStructures, FD); |
| 4517 | S += ']'; |
| 4518 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4519 | return; |
| 4520 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4521 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4522 | if (T->getAs<FunctionType>()) { |
Anders Carlsson | c0a87b7 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 4523 | S += '?'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4524 | return; |
| 4525 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4526 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4527 | if (const RecordType *RTy = T->getAs<RecordType>()) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4528 | RecordDecl *RDecl = RTy->getDecl(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4529 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 4530 | // Anonymous structures print as '?' |
| 4531 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 4532 | S += II->getName(); |
Fariborz Jahanian | 6fb9439 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 4533 | if (ClassTemplateSpecializationDecl *Spec |
| 4534 | = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) { |
| 4535 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 4536 | std::string TemplateArgsStr |
| 4537 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4538 | TemplateArgs.data(), |
| 4539 | TemplateArgs.size(), |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 4540 | (*this).getPrintingPolicy()); |
Fariborz Jahanian | 6fb9439 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 4541 | |
| 4542 | S += TemplateArgsStr; |
| 4543 | } |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 4544 | } else { |
| 4545 | S += '?'; |
| 4546 | } |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 4547 | if (ExpandStructures) { |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4548 | S += '='; |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4549 | if (!RDecl->isUnion()) { |
| 4550 | getObjCEncodingForStructureImpl(RDecl, S, FD); |
| 4551 | } else { |
| 4552 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 4553 | FieldEnd = RDecl->field_end(); |
| 4554 | Field != FieldEnd; ++Field) { |
| 4555 | if (FD) { |
| 4556 | S += '"'; |
| 4557 | S += Field->getNameAsString(); |
| 4558 | S += '"'; |
| 4559 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4560 | |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4561 | // Special case bit-fields. |
| 4562 | if (Field->isBitField()) { |
| 4563 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
| 4564 | (*Field)); |
| 4565 | } else { |
| 4566 | QualType qt = Field->getType(); |
| 4567 | getLegacyIntegralTypeEncoding(qt); |
| 4568 | getObjCEncodingForTypeImpl(qt, S, false, true, |
| 4569 | FD, /*OutermostType*/false, |
| 4570 | /*EncodingProperty*/false, |
| 4571 | /*StructField*/true); |
| 4572 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4573 | } |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4574 | } |
Fariborz Jahanian | 6de88a8 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 4575 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4576 | S += RDecl->isUnion() ? ')' : '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4577 | return; |
| 4578 | } |
Fariborz Jahanian | e6012c7 | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 4579 | |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4580 | if (const EnumType *ET = T->getAs<EnumType>()) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4581 | if (FD && FD->isBitField()) |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4582 | EncodeBitField(this, S, T, FD); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4583 | else |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4584 | S += ObjCEncodingForEnumType(this, ET); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4585 | return; |
| 4586 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4587 | |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4588 | if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) { |
Steve Naroff | 21a98b1 | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 4589 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4590 | if (EncodeBlockParameters) { |
| 4591 | const FunctionType *FT = BT->getPointeeType()->getAs<FunctionType>(); |
| 4592 | |
| 4593 | S += '<'; |
| 4594 | // Block return type |
| 4595 | getObjCEncodingForTypeImpl(FT->getResultType(), S, |
| 4596 | ExpandPointedToStructures, ExpandStructures, |
| 4597 | FD, |
| 4598 | false /* OutermostType */, |
| 4599 | EncodingProperty, |
| 4600 | false /* StructField */, |
| 4601 | EncodeBlockParameters, |
| 4602 | EncodeClassNames); |
| 4603 | // Block self |
| 4604 | S += "@?"; |
| 4605 | // Block parameters |
| 4606 | if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) { |
| 4607 | for (FunctionProtoType::arg_type_iterator I = FPT->arg_type_begin(), |
| 4608 | E = FPT->arg_type_end(); I && (I != E); ++I) { |
| 4609 | getObjCEncodingForTypeImpl(*I, S, |
| 4610 | ExpandPointedToStructures, |
| 4611 | ExpandStructures, |
| 4612 | FD, |
| 4613 | false /* OutermostType */, |
| 4614 | EncodingProperty, |
| 4615 | false /* StructField */, |
| 4616 | EncodeBlockParameters, |
| 4617 | EncodeClassNames); |
| 4618 | } |
| 4619 | } |
| 4620 | S += '>'; |
| 4621 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4622 | return; |
| 4623 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4624 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4625 | // Ignore protocol qualifiers when mangling at this level. |
| 4626 | if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>()) |
| 4627 | T = OT->getBaseType(); |
| 4628 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4629 | if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4630 | // @encode(class_name) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4631 | ObjCInterfaceDecl *OI = OIT->getDecl(); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4632 | S += '{'; |
| 4633 | const IdentifierInfo *II = OI->getIdentifier(); |
| 4634 | S += II->getName(); |
| 4635 | S += '='; |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4636 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4637 | DeepCollectObjCIvars(OI, true, Ivars); |
| 4638 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) { |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4639 | const FieldDecl *Field = cast<FieldDecl>(Ivars[i]); |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4640 | if (Field->isBitField()) |
| 4641 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4642 | else |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4643 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4644 | } |
| 4645 | S += '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4646 | return; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4647 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4648 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4649 | if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4650 | if (OPT->isObjCIdType()) { |
| 4651 | S += '@'; |
| 4652 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4653 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4654 | |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4655 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
| 4656 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
| 4657 | // Since this is a binary compatibility issue, need to consult with runtime |
| 4658 | // folks. Fortunately, this is a *very* obsure construct. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4659 | S += '#'; |
| 4660 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4661 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4662 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4663 | if (OPT->isObjCQualifiedIdType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4664 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4665 | ExpandPointedToStructures, |
| 4666 | ExpandStructures, FD); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4667 | if (FD || EncodingProperty || EncodeClassNames) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4668 | // Note that we do extended encoding of protocol qualifer list |
| 4669 | // Only when doing ivar or property encoding. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4670 | S += '"'; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4671 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 4672 | E = OPT->qual_end(); I != E; ++I) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4673 | S += '<'; |
| 4674 | S += (*I)->getNameAsString(); |
| 4675 | S += '>'; |
| 4676 | } |
| 4677 | S += '"'; |
| 4678 | } |
| 4679 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4680 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4681 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4682 | QualType PointeeTy = OPT->getPointeeType(); |
| 4683 | if (!EncodingProperty && |
| 4684 | isa<TypedefType>(PointeeTy.getTypePtr())) { |
| 4685 | // Another historical/compatibility reason. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4686 | // We encode the underlying type which comes out as |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4687 | // {...}; |
| 4688 | S += '^'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4689 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 4690 | false, ExpandPointedToStructures, |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4691 | NULL); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4692 | return; |
| 4693 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4694 | |
| 4695 | S += '@'; |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4696 | if (OPT->getInterfaceDecl() && |
| 4697 | (FD || EncodingProperty || EncodeClassNames)) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4698 | S += '"'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4699 | S += OPT->getInterfaceDecl()->getIdentifier()->getName(); |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4700 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 4701 | E = OPT->qual_end(); I != E; ++I) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4702 | S += '<'; |
| 4703 | S += (*I)->getNameAsString(); |
| 4704 | S += '>'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4705 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4706 | S += '"'; |
| 4707 | } |
| 4708 | return; |
| 4709 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4710 | |
John McCall | 532ec7b | 2010-05-17 23:56:34 +0000 | [diff] [blame] | 4711 | // gcc just blithely ignores member pointers. |
| 4712 | // TODO: maybe there should be a mangling for these |
| 4713 | if (T->getAs<MemberPointerType>()) |
| 4714 | return; |
Fariborz Jahanian | e6012c7 | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 4715 | |
| 4716 | if (T->isVectorType()) { |
| 4717 | // This matches gcc's encoding, even though technically it is |
| 4718 | // insufficient. |
| 4719 | // FIXME. We should do a better job than gcc. |
| 4720 | return; |
| 4721 | } |
| 4722 | |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4723 | llvm_unreachable("@encode for type not implemented!"); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4724 | } |
| 4725 | |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4726 | void ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl, |
| 4727 | std::string &S, |
| 4728 | const FieldDecl *FD, |
| 4729 | bool includeVBases) const { |
| 4730 | assert(RDecl && "Expected non-null RecordDecl"); |
| 4731 | assert(!RDecl->isUnion() && "Should not be called for unions"); |
| 4732 | if (!RDecl->getDefinition()) |
| 4733 | return; |
| 4734 | |
| 4735 | CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(RDecl); |
| 4736 | std::multimap<uint64_t, NamedDecl *> FieldOrBaseOffsets; |
| 4737 | const ASTRecordLayout &layout = getASTRecordLayout(RDecl); |
| 4738 | |
| 4739 | if (CXXRec) { |
| 4740 | for (CXXRecordDecl::base_class_iterator |
| 4741 | BI = CXXRec->bases_begin(), |
| 4742 | BE = CXXRec->bases_end(); BI != BE; ++BI) { |
| 4743 | if (!BI->isVirtual()) { |
| 4744 | CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 829f200 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 4745 | if (base->isEmpty()) |
| 4746 | continue; |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4747 | uint64_t offs = layout.getBaseClassOffsetInBits(base); |
| 4748 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 4749 | std::make_pair(offs, base)); |
| 4750 | } |
| 4751 | } |
| 4752 | } |
| 4753 | |
| 4754 | unsigned i = 0; |
| 4755 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 4756 | FieldEnd = RDecl->field_end(); |
| 4757 | Field != FieldEnd; ++Field, ++i) { |
| 4758 | uint64_t offs = layout.getFieldOffset(i); |
| 4759 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 4760 | std::make_pair(offs, *Field)); |
| 4761 | } |
| 4762 | |
| 4763 | if (CXXRec && includeVBases) { |
| 4764 | for (CXXRecordDecl::base_class_iterator |
| 4765 | BI = CXXRec->vbases_begin(), |
| 4766 | BE = CXXRec->vbases_end(); BI != BE; ++BI) { |
| 4767 | CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 829f200 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 4768 | if (base->isEmpty()) |
| 4769 | continue; |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4770 | uint64_t offs = layout.getVBaseClassOffsetInBits(base); |
Argyrios Kyrtzidis | 19aa860 | 2011-09-26 18:14:24 +0000 | [diff] [blame] | 4771 | if (FieldOrBaseOffsets.find(offs) == FieldOrBaseOffsets.end()) |
| 4772 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.end(), |
| 4773 | std::make_pair(offs, base)); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4774 | } |
| 4775 | } |
| 4776 | |
| 4777 | CharUnits size; |
| 4778 | if (CXXRec) { |
| 4779 | size = includeVBases ? layout.getSize() : layout.getNonVirtualSize(); |
| 4780 | } else { |
| 4781 | size = layout.getSize(); |
| 4782 | } |
| 4783 | |
| 4784 | uint64_t CurOffs = 0; |
| 4785 | std::multimap<uint64_t, NamedDecl *>::iterator |
| 4786 | CurLayObj = FieldOrBaseOffsets.begin(); |
| 4787 | |
Argyrios Kyrtzidis | cb8061e | 2011-08-22 16:03:14 +0000 | [diff] [blame] | 4788 | if ((CurLayObj != FieldOrBaseOffsets.end() && CurLayObj->first != 0) || |
| 4789 | (CurLayObj == FieldOrBaseOffsets.end() && |
| 4790 | CXXRec && CXXRec->isDynamicClass())) { |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4791 | assert(CXXRec && CXXRec->isDynamicClass() && |
| 4792 | "Offset 0 was empty but no VTable ?"); |
| 4793 | if (FD) { |
| 4794 | S += "\"_vptr$"; |
| 4795 | std::string recname = CXXRec->getNameAsString(); |
| 4796 | if (recname.empty()) recname = "?"; |
| 4797 | S += recname; |
| 4798 | S += '"'; |
| 4799 | } |
| 4800 | S += "^^?"; |
| 4801 | CurOffs += getTypeSize(VoidPtrTy); |
| 4802 | } |
| 4803 | |
| 4804 | if (!RDecl->hasFlexibleArrayMember()) { |
| 4805 | // Mark the end of the structure. |
| 4806 | uint64_t offs = toBits(size); |
| 4807 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 4808 | std::make_pair(offs, (NamedDecl*)0)); |
| 4809 | } |
| 4810 | |
| 4811 | for (; CurLayObj != FieldOrBaseOffsets.end(); ++CurLayObj) { |
| 4812 | assert(CurOffs <= CurLayObj->first); |
| 4813 | |
| 4814 | if (CurOffs < CurLayObj->first) { |
| 4815 | uint64_t padding = CurLayObj->first - CurOffs; |
| 4816 | // FIXME: There doesn't seem to be a way to indicate in the encoding that |
| 4817 | // packing/alignment of members is different that normal, in which case |
| 4818 | // the encoding will be out-of-sync with the real layout. |
| 4819 | // If the runtime switches to just consider the size of types without |
| 4820 | // taking into account alignment, we could make padding explicit in the |
| 4821 | // encoding (e.g. using arrays of chars). The encoding strings would be |
| 4822 | // longer then though. |
| 4823 | CurOffs += padding; |
| 4824 | } |
| 4825 | |
| 4826 | NamedDecl *dcl = CurLayObj->second; |
| 4827 | if (dcl == 0) |
| 4828 | break; // reached end of structure. |
| 4829 | |
| 4830 | if (CXXRecordDecl *base = dyn_cast<CXXRecordDecl>(dcl)) { |
| 4831 | // We expand the bases without their virtual bases since those are going |
| 4832 | // in the initial structure. Note that this differs from gcc which |
| 4833 | // expands virtual bases each time one is encountered in the hierarchy, |
| 4834 | // making the encoding type bigger than it really is. |
| 4835 | getObjCEncodingForStructureImpl(base, S, FD, /*includeVBases*/false); |
Argyrios Kyrtzidis | 829f200 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 4836 | assert(!base->isEmpty()); |
| 4837 | CurOffs += toBits(getASTRecordLayout(base).getNonVirtualSize()); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4838 | } else { |
| 4839 | FieldDecl *field = cast<FieldDecl>(dcl); |
| 4840 | if (FD) { |
| 4841 | S += '"'; |
| 4842 | S += field->getNameAsString(); |
| 4843 | S += '"'; |
| 4844 | } |
| 4845 | |
| 4846 | if (field->isBitField()) { |
| 4847 | EncodeBitField(this, S, field->getType(), field); |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4848 | CurOffs += field->getBitWidthValue(*this); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4849 | } else { |
| 4850 | QualType qt = field->getType(); |
| 4851 | getLegacyIntegralTypeEncoding(qt); |
| 4852 | getObjCEncodingForTypeImpl(qt, S, false, true, FD, |
| 4853 | /*OutermostType*/false, |
| 4854 | /*EncodingProperty*/false, |
| 4855 | /*StructField*/true); |
| 4856 | CurOffs += getTypeSize(field->getType()); |
| 4857 | } |
| 4858 | } |
| 4859 | } |
| 4860 | } |
| 4861 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4862 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 4863 | std::string& S) const { |
| 4864 | if (QT & Decl::OBJC_TQ_In) |
| 4865 | S += 'n'; |
| 4866 | if (QT & Decl::OBJC_TQ_Inout) |
| 4867 | S += 'N'; |
| 4868 | if (QT & Decl::OBJC_TQ_Out) |
| 4869 | S += 'o'; |
| 4870 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 4871 | S += 'O'; |
| 4872 | if (QT & Decl::OBJC_TQ_Byref) |
| 4873 | S += 'R'; |
| 4874 | if (QT & Decl::OBJC_TQ_Oneway) |
| 4875 | S += 'V'; |
| 4876 | } |
| 4877 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4878 | void ASTContext::setBuiltinVaListType(QualType T) { |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 4879 | assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4880 | |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 4881 | BuiltinVaListType = T; |
| 4882 | } |
| 4883 | |
Douglas Gregor | 4dfd02a | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 4884 | TypedefDecl *ASTContext::getObjCIdDecl() const { |
| 4885 | if (!ObjCIdDecl) { |
| 4886 | QualType T = getObjCObjectType(ObjCBuiltinIdTy, 0, 0); |
| 4887 | T = getObjCObjectPointerType(T); |
| 4888 | TypeSourceInfo *IdInfo = getTrivialTypeSourceInfo(T); |
| 4889 | ObjCIdDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 4890 | getTranslationUnitDecl(), |
| 4891 | SourceLocation(), SourceLocation(), |
| 4892 | &Idents.get("id"), IdInfo); |
| 4893 | } |
| 4894 | |
| 4895 | return ObjCIdDecl; |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 4896 | } |
| 4897 | |
Douglas Gregor | 7a27ea5 | 2011-08-12 06:17:30 +0000 | [diff] [blame] | 4898 | TypedefDecl *ASTContext::getObjCSelDecl() const { |
| 4899 | if (!ObjCSelDecl) { |
| 4900 | QualType SelT = getPointerType(ObjCBuiltinSelTy); |
| 4901 | TypeSourceInfo *SelInfo = getTrivialTypeSourceInfo(SelT); |
| 4902 | ObjCSelDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 4903 | getTranslationUnitDecl(), |
| 4904 | SourceLocation(), SourceLocation(), |
| 4905 | &Idents.get("SEL"), SelInfo); |
| 4906 | } |
| 4907 | return ObjCSelDecl; |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 4908 | } |
| 4909 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4910 | void ASTContext::setObjCProtoType(QualType QT) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4911 | ObjCProtoType = QT; |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 4912 | } |
| 4913 | |
Douglas Gregor | 79d6726 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 4914 | TypedefDecl *ASTContext::getObjCClassDecl() const { |
| 4915 | if (!ObjCClassDecl) { |
| 4916 | QualType T = getObjCObjectType(ObjCBuiltinClassTy, 0, 0); |
| 4917 | T = getObjCObjectPointerType(T); |
| 4918 | TypeSourceInfo *ClassInfo = getTrivialTypeSourceInfo(T); |
| 4919 | ObjCClassDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 4920 | getTranslationUnitDecl(), |
| 4921 | SourceLocation(), SourceLocation(), |
| 4922 | &Idents.get("Class"), ClassInfo); |
| 4923 | } |
| 4924 | |
| 4925 | return ObjCClassDecl; |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 4926 | } |
| 4927 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4928 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4929 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 4930 | "'NSConstantString' type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4931 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4932 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 4933 | } |
| 4934 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4935 | /// \brief Retrieve the template name that corresponds to a non-empty |
| 4936 | /// lookup. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4937 | TemplateName |
| 4938 | ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin, |
| 4939 | UnresolvedSetIterator End) const { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4940 | unsigned size = End - Begin; |
| 4941 | assert(size > 1 && "set is not overloaded!"); |
| 4942 | |
| 4943 | void *memory = Allocate(sizeof(OverloadedTemplateStorage) + |
| 4944 | size * sizeof(FunctionTemplateDecl*)); |
| 4945 | OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size); |
| 4946 | |
| 4947 | NamedDecl **Storage = OT->getStorage(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4948 | for (UnresolvedSetIterator I = Begin; I != End; ++I) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4949 | NamedDecl *D = *I; |
| 4950 | assert(isa<FunctionTemplateDecl>(D) || |
| 4951 | (isa<UsingShadowDecl>(D) && |
| 4952 | isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))); |
| 4953 | *Storage++ = D; |
| 4954 | } |
| 4955 | |
| 4956 | return TemplateName(OT); |
| 4957 | } |
| 4958 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4959 | /// \brief Retrieve the template name that represents a qualified |
| 4960 | /// template name such as \c std::vector. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4961 | TemplateName |
| 4962 | ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
| 4963 | bool TemplateKeyword, |
| 4964 | TemplateDecl *Template) const { |
Douglas Gregor | 0f0ea2a | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 4965 | assert(NNS && "Missing nested-name-specifier in qualified template name"); |
| 4966 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4967 | // FIXME: Canonicalization? |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4968 | llvm::FoldingSetNodeID ID; |
| 4969 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 4970 | |
| 4971 | void *InsertPos = 0; |
| 4972 | QualifiedTemplateName *QTN = |
| 4973 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4974 | if (!QTN) { |
| 4975 | QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); |
| 4976 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 4977 | } |
| 4978 | |
| 4979 | return TemplateName(QTN); |
| 4980 | } |
| 4981 | |
| 4982 | /// \brief Retrieve the template name that represents a dependent |
| 4983 | /// template name such as \c MetaFun::template apply. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4984 | TemplateName |
| 4985 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
| 4986 | const IdentifierInfo *Name) const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4987 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 4988 | "Nested name specifier must be dependent"); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4989 | |
| 4990 | llvm::FoldingSetNodeID ID; |
| 4991 | DependentTemplateName::Profile(ID, NNS, Name); |
| 4992 | |
| 4993 | void *InsertPos = 0; |
| 4994 | DependentTemplateName *QTN = |
| 4995 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4996 | |
| 4997 | if (QTN) |
| 4998 | return TemplateName(QTN); |
| 4999 | |
| 5000 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 5001 | if (CanonNNS == NNS) { |
| 5002 | QTN = new (*this,4) DependentTemplateName(NNS, Name); |
| 5003 | } else { |
| 5004 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
| 5005 | QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 5006 | DependentTemplateName *CheckQTN = |
| 5007 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 5008 | assert(!CheckQTN && "Dependent type name canonicalization broken"); |
| 5009 | (void)CheckQTN; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 5010 | } |
| 5011 | |
| 5012 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 5013 | return TemplateName(QTN); |
| 5014 | } |
| 5015 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 5016 | /// \brief Retrieve the template name that represents a dependent |
| 5017 | /// template name such as \c MetaFun::template operator+. |
| 5018 | TemplateName |
| 5019 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5020 | OverloadedOperatorKind Operator) const { |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 5021 | assert((!NNS || NNS->isDependent()) && |
| 5022 | "Nested name specifier must be dependent"); |
| 5023 | |
| 5024 | llvm::FoldingSetNodeID ID; |
| 5025 | DependentTemplateName::Profile(ID, NNS, Operator); |
| 5026 | |
| 5027 | void *InsertPos = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 5028 | DependentTemplateName *QTN |
| 5029 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 5030 | |
| 5031 | if (QTN) |
| 5032 | return TemplateName(QTN); |
| 5033 | |
| 5034 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 5035 | if (CanonNNS == NNS) { |
| 5036 | QTN = new (*this,4) DependentTemplateName(NNS, Operator); |
| 5037 | } else { |
| 5038 | TemplateName Canon = getDependentTemplateName(CanonNNS, Operator); |
| 5039 | QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 5040 | |
| 5041 | DependentTemplateName *CheckQTN |
| 5042 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 5043 | assert(!CheckQTN && "Dependent template name canonicalization broken"); |
| 5044 | (void)CheckQTN; |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 5045 | } |
| 5046 | |
| 5047 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 5048 | return TemplateName(QTN); |
| 5049 | } |
| 5050 | |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 5051 | TemplateName |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5052 | ASTContext::getSubstTemplateTemplateParm(TemplateTemplateParmDecl *param, |
| 5053 | TemplateName replacement) const { |
| 5054 | llvm::FoldingSetNodeID ID; |
| 5055 | SubstTemplateTemplateParmStorage::Profile(ID, param, replacement); |
| 5056 | |
| 5057 | void *insertPos = 0; |
| 5058 | SubstTemplateTemplateParmStorage *subst |
| 5059 | = SubstTemplateTemplateParms.FindNodeOrInsertPos(ID, insertPos); |
| 5060 | |
| 5061 | if (!subst) { |
| 5062 | subst = new (*this) SubstTemplateTemplateParmStorage(param, replacement); |
| 5063 | SubstTemplateTemplateParms.InsertNode(subst, insertPos); |
| 5064 | } |
| 5065 | |
| 5066 | return TemplateName(subst); |
| 5067 | } |
| 5068 | |
| 5069 | TemplateName |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 5070 | ASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param, |
| 5071 | const TemplateArgument &ArgPack) const { |
| 5072 | ASTContext &Self = const_cast<ASTContext &>(*this); |
| 5073 | llvm::FoldingSetNodeID ID; |
| 5074 | SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack); |
| 5075 | |
| 5076 | void *InsertPos = 0; |
| 5077 | SubstTemplateTemplateParmPackStorage *Subst |
| 5078 | = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos); |
| 5079 | |
| 5080 | if (!Subst) { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5081 | Subst = new (*this) SubstTemplateTemplateParmPackStorage(Param, |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 5082 | ArgPack.pack_size(), |
| 5083 | ArgPack.pack_begin()); |
| 5084 | SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos); |
| 5085 | } |
| 5086 | |
| 5087 | return TemplateName(Subst); |
| 5088 | } |
| 5089 | |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 5090 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | d934112 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 5091 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 5092 | /// is actually a value of type @c TargetInfo::IntType. |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 5093 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 5094 | switch (Type) { |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 5095 | case TargetInfo::NoInt: return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 5096 | case TargetInfo::SignedShort: return ShortTy; |
| 5097 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 5098 | case TargetInfo::SignedInt: return IntTy; |
| 5099 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 5100 | case TargetInfo::SignedLong: return LongTy; |
| 5101 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 5102 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 5103 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 5104 | } |
| 5105 | |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5106 | llvm_unreachable("Unhandled TargetInfo::IntType value"); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 5107 | } |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 5108 | |
| 5109 | //===----------------------------------------------------------------------===// |
| 5110 | // Type Predicates. |
| 5111 | //===----------------------------------------------------------------------===// |
| 5112 | |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 5113 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 5114 | /// garbage collection attribute. |
| 5115 | /// |
John McCall | ae278a3 | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 5116 | Qualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const { |
Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 5117 | if (getLangOptions().getGC() == LangOptions::NonGC) |
John McCall | ae278a3 | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 5118 | return Qualifiers::GCNone; |
| 5119 | |
| 5120 | assert(getLangOptions().ObjC1); |
| 5121 | Qualifiers::GC GCAttrs = Ty.getObjCGCAttr(); |
| 5122 | |
| 5123 | // Default behaviour under objective-C's gc is for ObjC pointers |
| 5124 | // (or pointers to them) be treated as though they were declared |
| 5125 | // as __strong. |
| 5126 | if (GCAttrs == Qualifiers::GCNone) { |
| 5127 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
| 5128 | return Qualifiers::Strong; |
| 5129 | else if (Ty->isPointerType()) |
| 5130 | return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); |
| 5131 | } else { |
| 5132 | // It's not valid to set GC attributes on anything that isn't a |
| 5133 | // pointer. |
| 5134 | #ifndef NDEBUG |
| 5135 | QualType CT = Ty->getCanonicalTypeInternal(); |
| 5136 | while (const ArrayType *AT = dyn_cast<ArrayType>(CT)) |
| 5137 | CT = AT->getElementType(); |
| 5138 | assert(CT->isAnyPointerType() || CT->isBlockPointerType()); |
| 5139 | #endif |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 5140 | } |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 5141 | return GCAttrs; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 5142 | } |
| 5143 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5144 | //===----------------------------------------------------------------------===// |
| 5145 | // Type Compatibility Testing |
| 5146 | //===----------------------------------------------------------------------===// |
Chris Lattner | 770951b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 5147 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5148 | /// areCompatVectorTypes - Return true if the two specified vector types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5149 | /// compatible. |
| 5150 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 5151 | const VectorType *RHS) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 5152 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5153 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5154 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5155 | } |
| 5156 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5157 | bool ASTContext::areCompatibleVectorTypes(QualType FirstVec, |
| 5158 | QualType SecondVec) { |
| 5159 | assert(FirstVec->isVectorType() && "FirstVec should be a vector type"); |
| 5160 | assert(SecondVec->isVectorType() && "SecondVec should be a vector type"); |
| 5161 | |
| 5162 | if (hasSameUnqualifiedType(FirstVec, SecondVec)) |
| 5163 | return true; |
| 5164 | |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 5165 | // Treat Neon vector types and most AltiVec vector types as if they are the |
| 5166 | // equivalent GCC vector types. |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5167 | const VectorType *First = FirstVec->getAs<VectorType>(); |
| 5168 | const VectorType *Second = SecondVec->getAs<VectorType>(); |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 5169 | if (First->getNumElements() == Second->getNumElements() && |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5170 | hasSameType(First->getElementType(), Second->getElementType()) && |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 5171 | First->getVectorKind() != VectorType::AltiVecPixel && |
| 5172 | First->getVectorKind() != VectorType::AltiVecBool && |
| 5173 | Second->getVectorKind() != VectorType::AltiVecPixel && |
| 5174 | Second->getVectorKind() != VectorType::AltiVecBool) |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5175 | return true; |
| 5176 | |
| 5177 | return false; |
| 5178 | } |
| 5179 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5180 | //===----------------------------------------------------------------------===// |
| 5181 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 5182 | //===----------------------------------------------------------------------===// |
| 5183 | |
| 5184 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 5185 | /// inheritance hierarchy of 'rProto'. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5186 | bool |
| 5187 | ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 5188 | ObjCProtocolDecl *rProto) const { |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 5189 | if (declaresSameEntity(lProto, rProto)) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5190 | return true; |
| 5191 | for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), |
| 5192 | E = rProto->protocol_end(); PI != E; ++PI) |
| 5193 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 5194 | return true; |
| 5195 | return false; |
| 5196 | } |
| 5197 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5198 | /// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...> |
| 5199 | /// return true if lhs's protocols conform to rhs's protocol; false |
| 5200 | /// otherwise. |
| 5201 | bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) { |
| 5202 | if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType()) |
| 5203 | return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false); |
| 5204 | return false; |
| 5205 | } |
| 5206 | |
Fariborz Jahanian | a8f8dac | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 5207 | /// ObjCQualifiedClassTypesAreCompatible - compare Class<p,...> and |
| 5208 | /// Class<p1, ...>. |
| 5209 | bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs, |
| 5210 | QualType rhs) { |
| 5211 | const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>(); |
| 5212 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
| 5213 | assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible"); |
| 5214 | |
| 5215 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5216 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5217 | bool match = false; |
| 5218 | ObjCProtocolDecl *lhsProto = *I; |
| 5219 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 5220 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 5221 | ObjCProtocolDecl *rhsProto = *J; |
| 5222 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) { |
| 5223 | match = true; |
| 5224 | break; |
| 5225 | } |
| 5226 | } |
| 5227 | if (!match) |
| 5228 | return false; |
| 5229 | } |
| 5230 | return true; |
| 5231 | } |
| 5232 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5233 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 5234 | /// ObjCQualifiedIDType. |
| 5235 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 5236 | bool compare) { |
| 5237 | // Allow id<P..> and an 'id' or void* type in all cases. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5238 | if (lhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5239 | lhs->isObjCIdType() || lhs->isObjCClassType()) |
| 5240 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5241 | else if (rhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5242 | rhs->isObjCIdType() || rhs->isObjCClassType()) |
| 5243 | return true; |
| 5244 | |
| 5245 | if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5246 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5247 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5248 | if (!rhsOPT) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5249 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5250 | if (rhsOPT->qual_empty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5251 | // If the RHS is a unqualified interface pointer "NSString*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5252 | // make sure we check the class hierarchy. |
| 5253 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 5254 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5255 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5256 | // when comparing an id<P> on lhs with a static type on rhs, |
| 5257 | // see if static class implements all of id's protocols, directly or |
| 5258 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 5259 | if (!rhsID->ClassImplementsProtocol(*I, true)) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5260 | return false; |
| 5261 | } |
| 5262 | } |
| 5263 | // If there are no qualifiers and no interface, we have an 'id'. |
| 5264 | return true; |
| 5265 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5266 | // Both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5267 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5268 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5269 | ObjCProtocolDecl *lhsProto = *I; |
| 5270 | bool match = false; |
| 5271 | |
| 5272 | // when comparing an id<P> on lhs with a static type on rhs, |
| 5273 | // see if static class implements all of id's protocols, directly or |
| 5274 | // through its super class and categories. |
| 5275 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 5276 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 5277 | ObjCProtocolDecl *rhsProto = *J; |
| 5278 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 5279 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 5280 | match = true; |
| 5281 | break; |
| 5282 | } |
| 5283 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5284 | // If the RHS is a qualified interface pointer "NSString<P>*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5285 | // make sure we check the class hierarchy. |
| 5286 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 5287 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5288 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5289 | // when comparing an id<P> on lhs with a static type on rhs, |
| 5290 | // see if static class implements all of id's protocols, directly or |
| 5291 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 5292 | if (rhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5293 | match = true; |
| 5294 | break; |
| 5295 | } |
| 5296 | } |
| 5297 | } |
| 5298 | if (!match) |
| 5299 | return false; |
| 5300 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5301 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5302 | return true; |
| 5303 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5304 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5305 | const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 5306 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 5307 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5308 | if (const ObjCObjectPointerType *lhsOPT = |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5309 | lhs->getAsObjCInterfacePointerType()) { |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5310 | // If both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5311 | for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(), |
| 5312 | E = lhsOPT->qual_end(); I != E; ++I) { |
| 5313 | ObjCProtocolDecl *lhsProto = *I; |
| 5314 | bool match = false; |
| 5315 | |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5316 | // 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] | 5317 | // see if static class implements all of id's protocols, directly or |
| 5318 | // through its super class and categories. |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5319 | // First, lhs protocols in the qualifier list must be found, direct |
| 5320 | // or indirect in rhs's qualifier list or it is a mismatch. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5321 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 5322 | E = rhsQID->qual_end(); J != E; ++J) { |
| 5323 | ObjCProtocolDecl *rhsProto = *J; |
| 5324 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 5325 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 5326 | match = true; |
| 5327 | break; |
| 5328 | } |
| 5329 | } |
| 5330 | if (!match) |
| 5331 | return false; |
| 5332 | } |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5333 | |
| 5334 | // Static class's protocols, or its super class or category protocols |
| 5335 | // must be found, direct or indirect in rhs's qualifier list or it is a mismatch. |
| 5336 | if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { |
| 5337 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
| 5338 | CollectInheritedProtocols(lhsID, LHSInheritedProtocols); |
| 5339 | // This is rather dubious but matches gcc's behavior. If lhs has |
| 5340 | // no type qualifier and its class has no static protocol(s) |
| 5341 | // assume that it is mismatch. |
| 5342 | if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty()) |
| 5343 | return false; |
| 5344 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 5345 | LHSInheritedProtocols.begin(), |
| 5346 | E = LHSInheritedProtocols.end(); I != E; ++I) { |
| 5347 | bool match = false; |
| 5348 | ObjCProtocolDecl *lhsProto = (*I); |
| 5349 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 5350 | E = rhsQID->qual_end(); J != E; ++J) { |
| 5351 | ObjCProtocolDecl *rhsProto = *J; |
| 5352 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 5353 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 5354 | match = true; |
| 5355 | break; |
| 5356 | } |
| 5357 | } |
| 5358 | if (!match) |
| 5359 | return false; |
| 5360 | } |
| 5361 | } |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5362 | return true; |
| 5363 | } |
| 5364 | return false; |
| 5365 | } |
| 5366 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5367 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5368 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 5369 | /// protocol qualifiers on the LHS or RHS. |
| 5370 | /// |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5371 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 5372 | const ObjCObjectPointerType *RHSOPT) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5373 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 5374 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 5375 | |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 5376 | // 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] | 5377 | if (LHS->isObjCUnqualifiedIdOrClass() || |
| 5378 | RHS->isObjCUnqualifiedIdOrClass()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5379 | return true; |
| 5380 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5381 | if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5382 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 5383 | QualType(RHSOPT,0), |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5384 | false); |
Fariborz Jahanian | a8f8dac | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 5385 | |
| 5386 | if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass()) |
| 5387 | return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0), |
| 5388 | QualType(RHSOPT,0)); |
| 5389 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5390 | // If we have 2 user-defined types, fall into that path. |
| 5391 | if (LHS->getInterface() && RHS->getInterface()) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5392 | return canAssignObjCInterfaces(LHS, RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5393 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5394 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5395 | } |
| 5396 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5397 | /// canAssignObjCInterfacesInBlockPointer - This routine is specifically written |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 5398 | /// for providing type-safety for objective-c pointers used to pass/return |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5399 | /// arguments in block literals. When passed as arguments, passing 'A*' where |
| 5400 | /// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is |
| 5401 | /// not OK. For the return type, the opposite is not OK. |
| 5402 | bool ASTContext::canAssignObjCInterfacesInBlockPointer( |
| 5403 | const ObjCObjectPointerType *LHSOPT, |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5404 | const ObjCObjectPointerType *RHSOPT, |
| 5405 | bool BlockReturnType) { |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 5406 | if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5407 | return true; |
| 5408 | |
| 5409 | if (LHSOPT->isObjCBuiltinType()) { |
| 5410 | return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType(); |
| 5411 | } |
| 5412 | |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 5413 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5414 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 5415 | QualType(RHSOPT,0), |
| 5416 | false); |
| 5417 | |
| 5418 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 5419 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 5420 | if (LHS && RHS) { // We have 2 user-defined types. |
| 5421 | if (LHS != RHS) { |
| 5422 | if (LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5423 | return BlockReturnType; |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5424 | if (RHS->getDecl()->isSuperClassOf(LHS->getDecl())) |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5425 | return !BlockReturnType; |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5426 | } |
| 5427 | else |
| 5428 | return true; |
| 5429 | } |
| 5430 | return false; |
| 5431 | } |
| 5432 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5433 | /// getIntersectionOfProtocols - This routine finds the intersection of set |
| 5434 | /// of protocols inherited from two distinct objective-c pointer objects. |
| 5435 | /// It is used to build composite qualifier list of the composite type of |
| 5436 | /// the conditional expression involving two objective-c pointer objects. |
| 5437 | static |
| 5438 | void getIntersectionOfProtocols(ASTContext &Context, |
| 5439 | const ObjCObjectPointerType *LHSOPT, |
| 5440 | const ObjCObjectPointerType *RHSOPT, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5441 | SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5442 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5443 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 5444 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 5445 | assert(LHS->getInterface() && "LHS must have an interface base"); |
| 5446 | assert(RHS->getInterface() && "RHS must have an interface base"); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5447 | |
| 5448 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet; |
| 5449 | unsigned LHSNumProtocols = LHS->getNumProtocols(); |
| 5450 | if (LHSNumProtocols > 0) |
| 5451 | InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end()); |
| 5452 | else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 5453 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5454 | Context.CollectInheritedProtocols(LHS->getInterface(), |
| 5455 | LHSInheritedProtocols); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5456 | InheritedProtocolSet.insert(LHSInheritedProtocols.begin(), |
| 5457 | LHSInheritedProtocols.end()); |
| 5458 | } |
| 5459 | |
| 5460 | unsigned RHSNumProtocols = RHS->getNumProtocols(); |
| 5461 | if (RHSNumProtocols > 0) { |
Dan Gohman | cb421fa | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 5462 | ObjCProtocolDecl **RHSProtocols = |
| 5463 | const_cast<ObjCProtocolDecl **>(RHS->qual_begin()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5464 | for (unsigned i = 0; i < RHSNumProtocols; ++i) |
| 5465 | if (InheritedProtocolSet.count(RHSProtocols[i])) |
| 5466 | IntersectionOfProtocols.push_back(RHSProtocols[i]); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 5467 | } else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 5468 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5469 | Context.CollectInheritedProtocols(RHS->getInterface(), |
| 5470 | RHSInheritedProtocols); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 5471 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 5472 | RHSInheritedProtocols.begin(), |
| 5473 | E = RHSInheritedProtocols.end(); I != E; ++I) |
| 5474 | if (InheritedProtocolSet.count((*I))) |
| 5475 | IntersectionOfProtocols.push_back((*I)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5476 | } |
| 5477 | } |
| 5478 | |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 5479 | /// areCommonBaseCompatible - Returns common base class of the two classes if |
| 5480 | /// one found. Note that this is O'2 algorithm. But it will be called as the |
| 5481 | /// last type comparison in a ?-exp of ObjC pointer types before a |
| 5482 | /// warning is issued. So, its invokation is extremely rare. |
| 5483 | QualType ASTContext::areCommonBaseCompatible( |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5484 | const ObjCObjectPointerType *Lptr, |
| 5485 | const ObjCObjectPointerType *Rptr) { |
| 5486 | const ObjCObjectType *LHS = Lptr->getObjectType(); |
| 5487 | const ObjCObjectType *RHS = Rptr->getObjectType(); |
| 5488 | const ObjCInterfaceDecl* LDecl = LHS->getInterface(); |
| 5489 | const ObjCInterfaceDecl* RDecl = RHS->getInterface(); |
Douglas Gregor | 60ef308 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 5490 | if (!LDecl || !RDecl || (declaresSameEntity(LDecl, RDecl))) |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 5491 | return QualType(); |
| 5492 | |
Fariborz Jahanian | 7c2bdcb | 2011-04-18 21:16:59 +0000 | [diff] [blame] | 5493 | do { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5494 | LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5495 | if (canAssignObjCInterfaces(LHS, RHS)) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5496 | SmallVector<ObjCProtocolDecl *, 8> Protocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5497 | getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols); |
| 5498 | |
| 5499 | QualType Result = QualType(LHS, 0); |
| 5500 | if (!Protocols.empty()) |
| 5501 | Result = getObjCObjectType(Result, Protocols.data(), Protocols.size()); |
| 5502 | Result = getObjCObjectPointerType(Result); |
| 5503 | return Result; |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5504 | } |
Fariborz Jahanian | 7c2bdcb | 2011-04-18 21:16:59 +0000 | [diff] [blame] | 5505 | } while ((LDecl = LDecl->getSuperClass())); |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 5506 | |
| 5507 | return QualType(); |
| 5508 | } |
| 5509 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5510 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS, |
| 5511 | const ObjCObjectType *RHS) { |
| 5512 | assert(LHS->getInterface() && "LHS is not an interface type"); |
| 5513 | assert(RHS->getInterface() && "RHS is not an interface type"); |
| 5514 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5515 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 5516 | // the LHS. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5517 | if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface())) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5518 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5519 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5520 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 5521 | // protocol qualified at all, then we are good. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 5522 | if (LHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5523 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5524 | |
Fariborz Jahanian | b7bc34a | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 5525 | // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, |
| 5526 | // more detailed analysis is required. |
| 5527 | if (RHS->getNumProtocols() == 0) { |
| 5528 | // OK, if LHS is a superclass of RHS *and* |
| 5529 | // this superclass is assignment compatible with LHS. |
| 5530 | // false otherwise. |
Fariborz Jahanian | 627788c | 2011-04-12 16:34:14 +0000 | [diff] [blame] | 5531 | bool IsSuperClass = |
| 5532 | LHS->getInterface()->isSuperClassOf(RHS->getInterface()); |
| 5533 | if (IsSuperClass) { |
Fariborz Jahanian | b7bc34a | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 5534 | // OK if conversion of LHS to SuperClass results in narrowing of types |
| 5535 | // ; i.e., SuperClass may implement at least one of the protocols |
| 5536 | // in LHS's protocol list. Example, SuperObj<P1> = lhs<P1,P2> is ok. |
| 5537 | // But not SuperObj<P1,P2,P3> = lhs<P1,P2>. |
| 5538 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> SuperClassInheritedProtocols; |
Fariborz Jahanian | 627788c | 2011-04-12 16:34:14 +0000 | [diff] [blame] | 5539 | CollectInheritedProtocols(RHS->getInterface(), SuperClassInheritedProtocols); |
Fariborz Jahanian | b7bc34a | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 5540 | // If super class has no protocols, it is not a match. |
| 5541 | if (SuperClassInheritedProtocols.empty()) |
| 5542 | return false; |
| 5543 | |
| 5544 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 5545 | LHSPE = LHS->qual_end(); |
| 5546 | LHSPI != LHSPE; LHSPI++) { |
| 5547 | bool SuperImplementsProtocol = false; |
| 5548 | ObjCProtocolDecl *LHSProto = (*LHSPI); |
| 5549 | |
| 5550 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 5551 | SuperClassInheritedProtocols.begin(), |
| 5552 | E = SuperClassInheritedProtocols.end(); I != E; ++I) { |
| 5553 | ObjCProtocolDecl *SuperClassProto = (*I); |
| 5554 | if (SuperClassProto->lookupProtocolNamed(LHSProto->getIdentifier())) { |
| 5555 | SuperImplementsProtocol = true; |
| 5556 | break; |
| 5557 | } |
| 5558 | } |
| 5559 | if (!SuperImplementsProtocol) |
| 5560 | return false; |
| 5561 | } |
| 5562 | return true; |
| 5563 | } |
| 5564 | return false; |
| 5565 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5566 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5567 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 5568 | LHSPE = LHS->qual_end(); |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 5569 | LHSPI != LHSPE; LHSPI++) { |
| 5570 | bool RHSImplementsProtocol = false; |
| 5571 | |
| 5572 | // If the RHS doesn't implement the protocol on the left, the types |
| 5573 | // are incompatible. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5574 | for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(), |
| 5575 | RHSPE = RHS->qual_end(); |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 5576 | RHSPI != RHSPE; RHSPI++) { |
| 5577 | if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) { |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 5578 | RHSImplementsProtocol = true; |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 5579 | break; |
| 5580 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 5581 | } |
| 5582 | // FIXME: For better diagnostics, consider passing back the protocol name. |
| 5583 | if (!RHSImplementsProtocol) |
| 5584 | return false; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5585 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 5586 | // The RHS implements all protocols listed on the LHS. |
| 5587 | return true; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5588 | } |
| 5589 | |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 5590 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 5591 | // get the "pointed to" types |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5592 | const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
| 5593 | const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5594 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5595 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 5596 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5597 | |
| 5598 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 5599 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 5600 | } |
| 5601 | |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 5602 | bool ASTContext::canBindObjCObjectType(QualType To, QualType From) { |
| 5603 | return canAssignObjCInterfaces( |
| 5604 | getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(), |
| 5605 | getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>()); |
| 5606 | } |
| 5607 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5608 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5609 | /// both shall have the identically qualified version of a compatible type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5610 | /// 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] | 5611 | /// same. See 6.7.[2,3,5] for additional rules. |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5612 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS, |
| 5613 | bool CompareUnqualified) { |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 5614 | if (getLangOptions().CPlusPlus) |
| 5615 | return hasSameType(LHS, RHS); |
| 5616 | |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5617 | return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5618 | } |
| 5619 | |
Fariborz Jahanian | c286f38 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 5620 | bool ASTContext::propertyTypesAreCompatible(QualType LHS, QualType RHS) { |
Fariborz Jahanian | 8237839 | 2011-07-12 23:20:13 +0000 | [diff] [blame] | 5621 | return typesAreCompatible(LHS, RHS); |
Fariborz Jahanian | c286f38 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 5622 | } |
| 5623 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5624 | bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) { |
| 5625 | return !mergeTypes(LHS, RHS, true).isNull(); |
| 5626 | } |
| 5627 | |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 5628 | /// mergeTransparentUnionType - if T is a transparent union type and a member |
| 5629 | /// of T is compatible with SubType, return the merged type, else return |
| 5630 | /// QualType() |
| 5631 | QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType, |
| 5632 | bool OfBlockPointer, |
| 5633 | bool Unqualified) { |
| 5634 | if (const RecordType *UT = T->getAsUnionType()) { |
| 5635 | RecordDecl *UD = UT->getDecl(); |
| 5636 | if (UD->hasAttr<TransparentUnionAttr>()) { |
| 5637 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 5638 | itend = UD->field_end(); it != itend; ++it) { |
Peter Collingbourne | f91d757 | 2010-12-02 21:00:06 +0000 | [diff] [blame] | 5639 | QualType ET = it->getType().getUnqualifiedType(); |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 5640 | QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified); |
| 5641 | if (!MT.isNull()) |
| 5642 | return MT; |
| 5643 | } |
| 5644 | } |
| 5645 | } |
| 5646 | |
| 5647 | return QualType(); |
| 5648 | } |
| 5649 | |
| 5650 | /// mergeFunctionArgumentTypes - merge two types which appear as function |
| 5651 | /// argument types |
| 5652 | QualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs, |
| 5653 | bool OfBlockPointer, |
| 5654 | bool Unqualified) { |
| 5655 | // GNU extension: two types are compatible if they appear as a function |
| 5656 | // argument, one of the types is a transparent union type and the other |
| 5657 | // type is compatible with a union member |
| 5658 | QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer, |
| 5659 | Unqualified); |
| 5660 | if (!lmerge.isNull()) |
| 5661 | return lmerge; |
| 5662 | |
| 5663 | QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer, |
| 5664 | Unqualified); |
| 5665 | if (!rmerge.isNull()) |
| 5666 | return rmerge; |
| 5667 | |
| 5668 | return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified); |
| 5669 | } |
| 5670 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5671 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs, |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5672 | bool OfBlockPointer, |
| 5673 | bool Unqualified) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5674 | const FunctionType *lbase = lhs->getAs<FunctionType>(); |
| 5675 | const FunctionType *rbase = rhs->getAs<FunctionType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5676 | const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 5677 | const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5678 | bool allLTypes = true; |
| 5679 | bool allRTypes = true; |
| 5680 | |
| 5681 | // Check return type |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5682 | QualType retType; |
Fariborz Jahanian | d263fd1 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 5683 | if (OfBlockPointer) { |
| 5684 | QualType RHS = rbase->getResultType(); |
| 5685 | QualType LHS = lbase->getResultType(); |
| 5686 | bool UnqualifiedResult = Unqualified; |
| 5687 | if (!UnqualifiedResult) |
| 5688 | UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers()); |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5689 | retType = mergeTypes(LHS, RHS, true, UnqualifiedResult, true); |
Fariborz Jahanian | d263fd1 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 5690 | } |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5691 | else |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5692 | retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), false, |
| 5693 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5694 | if (retType.isNull()) return QualType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5695 | |
| 5696 | if (Unqualified) |
| 5697 | retType = retType.getUnqualifiedType(); |
| 5698 | |
| 5699 | CanQualType LRetType = getCanonicalType(lbase->getResultType()); |
| 5700 | CanQualType RRetType = getCanonicalType(rbase->getResultType()); |
| 5701 | if (Unqualified) { |
| 5702 | LRetType = LRetType.getUnqualifiedType(); |
| 5703 | RRetType = RRetType.getUnqualifiedType(); |
| 5704 | } |
| 5705 | |
| 5706 | if (getCanonicalType(retType) != LRetType) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5707 | allLTypes = false; |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5708 | if (getCanonicalType(retType) != RRetType) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5709 | allRTypes = false; |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5710 | |
Daniel Dunbar | 6a15c85 | 2010-04-28 16:20:58 +0000 | [diff] [blame] | 5711 | // FIXME: double check this |
| 5712 | // FIXME: should we error if lbase->getRegParmAttr() != 0 && |
| 5713 | // rbase->getRegParmAttr() != 0 && |
| 5714 | // lbase->getRegParmAttr() != rbase->getRegParmAttr()? |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 5715 | FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo(); |
| 5716 | FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo(); |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5717 | |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 5718 | // Compatible functions must have compatible calling conventions |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5719 | if (!isSameCallConv(lbaseInfo.getCC(), rbaseInfo.getCC())) |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 5720 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5721 | |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5722 | // Regparm is part of the calling convention. |
Eli Friedman | a49218e | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 5723 | if (lbaseInfo.getHasRegParm() != rbaseInfo.getHasRegParm()) |
| 5724 | return QualType(); |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5725 | if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm()) |
| 5726 | return QualType(); |
| 5727 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5728 | if (lbaseInfo.getProducesResult() != rbaseInfo.getProducesResult()) |
| 5729 | return QualType(); |
| 5730 | |
Fariborz Jahanian | 53c8167 | 2011-10-05 00:05:34 +0000 | [diff] [blame] | 5731 | // functypes which return are preferred over those that do not. |
| 5732 | if (lbaseInfo.getNoReturn() && !rbaseInfo.getNoReturn()) |
| 5733 | allLTypes = false; |
| 5734 | else if (!lbaseInfo.getNoReturn() && rbaseInfo.getNoReturn()) |
| 5735 | allRTypes = false; |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5736 | // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'. |
| 5737 | bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn(); |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5738 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5739 | FunctionType::ExtInfo einfo = lbaseInfo.withNoReturn(NoReturn); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5740 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5741 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 5742 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 5743 | "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5744 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 5745 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 5746 | |
| 5747 | // Compatible functions must have the same number of arguments |
| 5748 | if (lproto_nargs != rproto_nargs) |
| 5749 | return QualType(); |
| 5750 | |
| 5751 | // Variadic and non-variadic functions aren't compatible |
| 5752 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 5753 | return QualType(); |
| 5754 | |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 5755 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 5756 | return QualType(); |
| 5757 | |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 5758 | if (LangOpts.ObjCAutoRefCount && |
| 5759 | !FunctionTypesMatchOnNSConsumedAttrs(rproto, lproto)) |
| 5760 | return QualType(); |
| 5761 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5762 | // Check argument compatibility |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5763 | SmallVector<QualType, 10> types; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5764 | for (unsigned i = 0; i < lproto_nargs; i++) { |
| 5765 | QualType largtype = lproto->getArgType(i).getUnqualifiedType(); |
| 5766 | QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 5767 | QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype, |
| 5768 | OfBlockPointer, |
| 5769 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5770 | if (argtype.isNull()) return QualType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5771 | |
| 5772 | if (Unqualified) |
| 5773 | argtype = argtype.getUnqualifiedType(); |
| 5774 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5775 | types.push_back(argtype); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5776 | if (Unqualified) { |
| 5777 | largtype = largtype.getUnqualifiedType(); |
| 5778 | rargtype = rargtype.getUnqualifiedType(); |
| 5779 | } |
| 5780 | |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5781 | if (getCanonicalType(argtype) != getCanonicalType(largtype)) |
| 5782 | allLTypes = false; |
| 5783 | if (getCanonicalType(argtype) != getCanonicalType(rargtype)) |
| 5784 | allRTypes = false; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5785 | } |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 5786 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5787 | if (allLTypes) return lhs; |
| 5788 | if (allRTypes) return rhs; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5789 | |
| 5790 | FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo(); |
| 5791 | EPI.ExtInfo = einfo; |
| 5792 | return getFunctionType(retType, types.begin(), types.size(), EPI); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5793 | } |
| 5794 | |
| 5795 | if (lproto) allRTypes = false; |
| 5796 | if (rproto) allLTypes = false; |
| 5797 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5798 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5799 | if (proto) { |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 5800 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5801 | if (proto->isVariadic()) return QualType(); |
| 5802 | // Check that the types are compatible with the types that |
| 5803 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 5804 | // The only types actually affected are promotable integer |
| 5805 | // types and floats, which would be passed as a different |
| 5806 | // type depending on whether the prototype is visible. |
| 5807 | unsigned proto_nargs = proto->getNumArgs(); |
| 5808 | for (unsigned i = 0; i < proto_nargs; ++i) { |
| 5809 | QualType argTy = proto->getArgType(i); |
Douglas Gregor | b0f8eac | 2010-02-03 19:27:29 +0000 | [diff] [blame] | 5810 | |
| 5811 | // Look at the promotion type of enum types, since that is the type used |
| 5812 | // to pass enum values. |
| 5813 | if (const EnumType *Enum = argTy->getAs<EnumType>()) |
| 5814 | argTy = Enum->getDecl()->getPromotionType(); |
| 5815 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5816 | if (argTy->isPromotableIntegerType() || |
| 5817 | getCanonicalType(argTy).getUnqualifiedType() == FloatTy) |
| 5818 | return QualType(); |
| 5819 | } |
| 5820 | |
| 5821 | if (allLTypes) return lhs; |
| 5822 | if (allRTypes) return rhs; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5823 | |
| 5824 | FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo(); |
| 5825 | EPI.ExtInfo = einfo; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5826 | return getFunctionType(retType, proto->arg_type_begin(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5827 | proto->getNumArgs(), EPI); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5828 | } |
| 5829 | |
| 5830 | if (allLTypes) return lhs; |
| 5831 | if (allRTypes) return rhs; |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5832 | return getFunctionNoProtoType(retType, einfo); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5833 | } |
| 5834 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5835 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5836 | bool OfBlockPointer, |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5837 | bool Unqualified, bool BlockReturnType) { |
Bill Wendling | 43d6975 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 5838 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 5839 | // type is adjusted to "T" prior to any further analysis, the expression |
| 5840 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 5841 | // expression is an lvalue unless the reference is an rvalue reference and |
| 5842 | // the expression is a function call (possibly inside parentheses). |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 5843 | assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?"); |
| 5844 | assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?"); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5845 | |
| 5846 | if (Unqualified) { |
| 5847 | LHS = LHS.getUnqualifiedType(); |
| 5848 | RHS = RHS.getUnqualifiedType(); |
| 5849 | } |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 5850 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5851 | QualType LHSCan = getCanonicalType(LHS), |
| 5852 | RHSCan = getCanonicalType(RHS); |
| 5853 | |
| 5854 | // If two types are identical, they are compatible. |
| 5855 | if (LHSCan == RHSCan) |
| 5856 | return LHS; |
| 5857 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5858 | // If the qualifiers are different, the types aren't compatible... mostly. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 5859 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 5860 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5861 | if (LQuals != RQuals) { |
| 5862 | // If any of these qualifiers are different, we have a type |
| 5863 | // mismatch. |
| 5864 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5865 | LQuals.getAddressSpace() != RQuals.getAddressSpace() || |
| 5866 | LQuals.getObjCLifetime() != RQuals.getObjCLifetime()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5867 | return QualType(); |
| 5868 | |
| 5869 | // Exactly one GC qualifier difference is allowed: __strong is |
| 5870 | // okay if the other type has no GC qualifier but is an Objective |
| 5871 | // C object pointer (i.e. implicitly strong by default). We fix |
| 5872 | // this by pretending that the unqualified type was actually |
| 5873 | // qualified __strong. |
| 5874 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 5875 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 5876 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 5877 | |
| 5878 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 5879 | return QualType(); |
| 5880 | |
| 5881 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
| 5882 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); |
| 5883 | } |
| 5884 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
| 5885 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); |
| 5886 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5887 | return QualType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5888 | } |
| 5889 | |
| 5890 | // Okay, qualifiers are equal. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5891 | |
Eli Friedman | 852d63b | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 5892 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 5893 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5894 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5895 | // We want to consider the two function types to be the same for these |
| 5896 | // comparisons, just force one to the other. |
| 5897 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 5898 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 4c721d3 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 5899 | |
| 5900 | // Same as above for arrays |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 5901 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 5902 | LHSClass = Type::ConstantArray; |
| 5903 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 5904 | RHSClass = Type::ConstantArray; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5905 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5906 | // ObjCInterfaces are just specialized ObjCObjects. |
| 5907 | if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject; |
| 5908 | if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject; |
| 5909 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 5910 | // Canonicalize ExtVector -> Vector. |
| 5911 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 5912 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5913 | |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 5914 | // If the canonical type classes don't match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5915 | if (LHSClass != RHSClass) { |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5916 | // 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] | 5917 | // a signed integer type, or an unsigned integer type. |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 5918 | // Compatibility is based on the underlying type, not the promotion |
| 5919 | // type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5920 | if (const EnumType* ETy = LHS->getAs<EnumType>()) { |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5921 | if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType()) |
| 5922 | return RHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 5923 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5924 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5925 | if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType()) |
| 5926 | return LHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 5927 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5928 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5929 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5930 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5931 | |
Steve Naroff | 4a74678 | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 5932 | // The canonical type classes match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5933 | switch (LHSClass) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5934 | #define TYPE(Class, Base) |
| 5935 | #define ABSTRACT_TYPE(Class, Base) |
John McCall | ad5e738 | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 5936 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5937 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 5938 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 5939 | #include "clang/AST/TypeNodes.def" |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5940 | llvm_unreachable("Non-canonical and dependent types shouldn't get here"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5941 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 5942 | case Type::LValueReference: |
| 5943 | case Type::RValueReference: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5944 | case Type::MemberPointer: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5945 | llvm_unreachable("C++ should never be in mergeTypes"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5946 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5947 | case Type::ObjCInterface: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5948 | case Type::IncompleteArray: |
| 5949 | case Type::VariableArray: |
| 5950 | case Type::FunctionProto: |
| 5951 | case Type::ExtVector: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5952 | llvm_unreachable("Types are eliminated above"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5953 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5954 | case Type::Pointer: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5955 | { |
| 5956 | // Merge two pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5957 | QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); |
| 5958 | QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5959 | if (Unqualified) { |
| 5960 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 5961 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 5962 | } |
| 5963 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false, |
| 5964 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5965 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 5966 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5967 | return LHS; |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 5968 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5969 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5970 | return getPointerType(ResultType); |
| 5971 | } |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 5972 | case Type::BlockPointer: |
| 5973 | { |
| 5974 | // Merge two block pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5975 | QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); |
| 5976 | QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5977 | if (Unqualified) { |
| 5978 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 5979 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 5980 | } |
| 5981 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer, |
| 5982 | Unqualified); |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 5983 | if (ResultType.isNull()) return QualType(); |
| 5984 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 5985 | return LHS; |
| 5986 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 5987 | return RHS; |
| 5988 | return getBlockPointerType(ResultType); |
| 5989 | } |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5990 | case Type::Atomic: |
| 5991 | { |
| 5992 | // Merge two pointer types, while trying to preserve typedef info |
| 5993 | QualType LHSValue = LHS->getAs<AtomicType>()->getValueType(); |
| 5994 | QualType RHSValue = RHS->getAs<AtomicType>()->getValueType(); |
| 5995 | if (Unqualified) { |
| 5996 | LHSValue = LHSValue.getUnqualifiedType(); |
| 5997 | RHSValue = RHSValue.getUnqualifiedType(); |
| 5998 | } |
| 5999 | QualType ResultType = mergeTypes(LHSValue, RHSValue, false, |
| 6000 | Unqualified); |
| 6001 | if (ResultType.isNull()) return QualType(); |
| 6002 | if (getCanonicalType(LHSValue) == getCanonicalType(ResultType)) |
| 6003 | return LHS; |
| 6004 | if (getCanonicalType(RHSValue) == getCanonicalType(ResultType)) |
| 6005 | return RHS; |
| 6006 | return getAtomicType(ResultType); |
| 6007 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6008 | case Type::ConstantArray: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6009 | { |
| 6010 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 6011 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 6012 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 6013 | return QualType(); |
| 6014 | |
| 6015 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 6016 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6017 | if (Unqualified) { |
| 6018 | LHSElem = LHSElem.getUnqualifiedType(); |
| 6019 | RHSElem = RHSElem.getUnqualifiedType(); |
| 6020 | } |
| 6021 | |
| 6022 | QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6023 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6024 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 6025 | return LHS; |
| 6026 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 6027 | return RHS; |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 6028 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 6029 | ArrayType::ArraySizeModifier(), 0); |
| 6030 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 6031 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6032 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 6033 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6034 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 6035 | return LHS; |
| 6036 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 6037 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6038 | if (LVAT) { |
| 6039 | // FIXME: This isn't correct! But tricky to implement because |
| 6040 | // the array's size has to be the size of LHS, but the type |
| 6041 | // has to be different. |
| 6042 | return LHS; |
| 6043 | } |
| 6044 | if (RVAT) { |
| 6045 | // FIXME: This isn't correct! But tricky to implement because |
| 6046 | // the array's size has to be the size of RHS, but the type |
| 6047 | // has to be different. |
| 6048 | return RHS; |
| 6049 | } |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 6050 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 6051 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 6052 | return getIncompleteArrayType(ResultType, |
| 6053 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6054 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6055 | case Type::FunctionNoProto: |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6056 | return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6057 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6058 | case Type::Enum: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6059 | return QualType(); |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6060 | case Type::Builtin: |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 6061 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6062 | return QualType(); |
Daniel Dunbar | 64cfdb7 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 6063 | case Type::Complex: |
| 6064 | // Distinct complex types are incompatible. |
| 6065 | return QualType(); |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 6066 | case Type::Vector: |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 6067 | // FIXME: The merged type should be an ExtVector! |
John McCall | 1c471f3 | 2010-03-12 23:14:13 +0000 | [diff] [blame] | 6068 | if (areCompatVectorTypes(LHSCan->getAs<VectorType>(), |
| 6069 | RHSCan->getAs<VectorType>())) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6070 | return LHS; |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6071 | return QualType(); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6072 | case Type::ObjCObject: { |
| 6073 | // Check if the types are assignment compatible. |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 6074 | // FIXME: This should be type compatibility, e.g. whether |
| 6075 | // "LHS x; RHS x;" at global scope is legal. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6076 | const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>(); |
| 6077 | const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>(); |
| 6078 | if (canAssignObjCInterfaces(LHSIface, RHSIface)) |
Steve Naroff | 5fd659d | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 6079 | return LHS; |
| 6080 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6081 | return QualType(); |
Cedric Venet | 61490e9 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 6082 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6083 | case Type::ObjCObjectPointer: { |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6084 | if (OfBlockPointer) { |
| 6085 | if (canAssignObjCInterfacesInBlockPointer( |
| 6086 | LHS->getAs<ObjCObjectPointerType>(), |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6087 | RHS->getAs<ObjCObjectPointerType>(), |
| 6088 | BlockReturnType)) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6089 | return LHS; |
| 6090 | return QualType(); |
| 6091 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6092 | if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(), |
| 6093 | RHS->getAs<ObjCObjectPointerType>())) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6094 | return LHS; |
| 6095 | |
Steve Naroff | bc76dd0 | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 6096 | return QualType(); |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6097 | } |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 6098 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6099 | |
| 6100 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 6101 | } |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 6102 | |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 6103 | bool ASTContext::FunctionTypesMatchOnNSConsumedAttrs( |
| 6104 | const FunctionProtoType *FromFunctionType, |
| 6105 | const FunctionProtoType *ToFunctionType) { |
| 6106 | if (FromFunctionType->hasAnyConsumedArgs() != |
| 6107 | ToFunctionType->hasAnyConsumedArgs()) |
| 6108 | return false; |
| 6109 | FunctionProtoType::ExtProtoInfo FromEPI = |
| 6110 | FromFunctionType->getExtProtoInfo(); |
| 6111 | FunctionProtoType::ExtProtoInfo ToEPI = |
| 6112 | ToFunctionType->getExtProtoInfo(); |
| 6113 | if (FromEPI.ConsumedArguments && ToEPI.ConsumedArguments) |
| 6114 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 6115 | ArgIdx != NumArgs; ++ArgIdx) { |
| 6116 | if (FromEPI.ConsumedArguments[ArgIdx] != |
| 6117 | ToEPI.ConsumedArguments[ArgIdx]) |
| 6118 | return false; |
| 6119 | } |
| 6120 | return true; |
| 6121 | } |
| 6122 | |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 6123 | /// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and |
| 6124 | /// 'RHS' attributes and returns the merged version; including for function |
| 6125 | /// return types. |
| 6126 | QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) { |
| 6127 | QualType LHSCan = getCanonicalType(LHS), |
| 6128 | RHSCan = getCanonicalType(RHS); |
| 6129 | // If two types are identical, they are compatible. |
| 6130 | if (LHSCan == RHSCan) |
| 6131 | return LHS; |
| 6132 | if (RHSCan->isFunctionType()) { |
| 6133 | if (!LHSCan->isFunctionType()) |
| 6134 | return QualType(); |
| 6135 | QualType OldReturnType = |
| 6136 | cast<FunctionType>(RHSCan.getTypePtr())->getResultType(); |
| 6137 | QualType NewReturnType = |
| 6138 | cast<FunctionType>(LHSCan.getTypePtr())->getResultType(); |
| 6139 | QualType ResReturnType = |
| 6140 | mergeObjCGCQualifiers(NewReturnType, OldReturnType); |
| 6141 | if (ResReturnType.isNull()) |
| 6142 | return QualType(); |
| 6143 | if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) { |
| 6144 | // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo(); |
| 6145 | // In either case, use OldReturnType to build the new function type. |
| 6146 | const FunctionType *F = LHS->getAs<FunctionType>(); |
| 6147 | if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6148 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 6149 | EPI.ExtInfo = getFunctionExtInfo(LHS); |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 6150 | QualType ResultType |
| 6151 | = getFunctionType(OldReturnType, FPT->arg_type_begin(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6152 | FPT->getNumArgs(), EPI); |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 6153 | return ResultType; |
| 6154 | } |
| 6155 | } |
| 6156 | return QualType(); |
| 6157 | } |
| 6158 | |
| 6159 | // If the qualifiers are different, the types can still be merged. |
| 6160 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 6161 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
| 6162 | if (LQuals != RQuals) { |
| 6163 | // If any of these qualifiers are different, we have a type mismatch. |
| 6164 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 6165 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 6166 | return QualType(); |
| 6167 | |
| 6168 | // Exactly one GC qualifier difference is allowed: __strong is |
| 6169 | // okay if the other type has no GC qualifier but is an Objective |
| 6170 | // C object pointer (i.e. implicitly strong by default). We fix |
| 6171 | // this by pretending that the unqualified type was actually |
| 6172 | // qualified __strong. |
| 6173 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 6174 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 6175 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 6176 | |
| 6177 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 6178 | return QualType(); |
| 6179 | |
| 6180 | if (GC_L == Qualifiers::Strong) |
| 6181 | return LHS; |
| 6182 | if (GC_R == Qualifiers::Strong) |
| 6183 | return RHS; |
| 6184 | return QualType(); |
| 6185 | } |
| 6186 | |
| 6187 | if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) { |
| 6188 | QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 6189 | QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 6190 | QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT); |
| 6191 | if (ResQT == LHSBaseQT) |
| 6192 | return LHS; |
| 6193 | if (ResQT == RHSBaseQT) |
| 6194 | return RHS; |
| 6195 | } |
| 6196 | return QualType(); |
| 6197 | } |
| 6198 | |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 6199 | //===----------------------------------------------------------------------===// |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6200 | // Integer Predicates |
| 6201 | //===----------------------------------------------------------------------===// |
Chris Lattner | 88054de | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 6202 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6203 | unsigned ASTContext::getIntWidth(QualType T) const { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 6204 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
Eli Friedman | 29a7f33 | 2009-12-10 22:29:29 +0000 | [diff] [blame] | 6205 | T = ET->getDecl()->getIntegerType(); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6206 | if (T->isBooleanType()) |
| 6207 | return 1; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 6208 | // For builtin types, just use the standard type sizing method |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6209 | return (unsigned)getTypeSize(T); |
| 6210 | } |
| 6211 | |
| 6212 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) { |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6213 | assert(T->hasSignedIntegerRepresentation() && "Unexpected type"); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6214 | |
| 6215 | // Turn <4 x signed int> -> <4 x unsigned int> |
| 6216 | if (const VectorType *VTy = T->getAs<VectorType>()) |
| 6217 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 6218 | VTy->getNumElements(), VTy->getVectorKind()); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6219 | |
| 6220 | // For enums, we return the unsigned version of the base type. |
| 6221 | if (const EnumType *ETy = T->getAs<EnumType>()) |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6222 | T = ETy->getDecl()->getIntegerType(); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6223 | |
| 6224 | const BuiltinType *BTy = T->getAs<BuiltinType>(); |
| 6225 | assert(BTy && "Unexpected signed integer type"); |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6226 | switch (BTy->getKind()) { |
| 6227 | case BuiltinType::Char_S: |
| 6228 | case BuiltinType::SChar: |
| 6229 | return UnsignedCharTy; |
| 6230 | case BuiltinType::Short: |
| 6231 | return UnsignedShortTy; |
| 6232 | case BuiltinType::Int: |
| 6233 | return UnsignedIntTy; |
| 6234 | case BuiltinType::Long: |
| 6235 | return UnsignedLongTy; |
| 6236 | case BuiltinType::LongLong: |
| 6237 | return UnsignedLongLongTy; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 6238 | case BuiltinType::Int128: |
| 6239 | return UnsignedInt128Ty; |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6240 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6241 | llvm_unreachable("Unexpected signed integer type"); |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6242 | } |
| 6243 | } |
| 6244 | |
Argyrios Kyrtzidis | 7b90340 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 6245 | ASTMutationListener::~ASTMutationListener() { } |
| 6246 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6247 | |
| 6248 | //===----------------------------------------------------------------------===// |
| 6249 | // Builtin Type Computation |
| 6250 | //===----------------------------------------------------------------------===// |
| 6251 | |
| 6252 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6253 | /// pointer over the consumed characters. This returns the resultant type. If |
| 6254 | /// AllowTypeModifiers is false then modifier like * are not parsed, just basic |
| 6255 | /// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of |
| 6256 | /// a vector of "i*". |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6257 | /// |
| 6258 | /// RequiresICE is filled in on return to indicate whether the value is required |
| 6259 | /// to be an Integer Constant Expression. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6260 | static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context, |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6261 | ASTContext::GetBuiltinTypeError &Error, |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6262 | bool &RequiresICE, |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6263 | bool AllowTypeModifiers) { |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6264 | // Modifiers. |
| 6265 | int HowLong = 0; |
| 6266 | bool Signed = false, Unsigned = false; |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6267 | RequiresICE = false; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6268 | |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6269 | // Read the prefixed modifiers first. |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6270 | bool Done = false; |
| 6271 | while (!Done) { |
| 6272 | switch (*Str++) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6273 | default: Done = true; --Str; break; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6274 | case 'I': |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6275 | RequiresICE = true; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6276 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6277 | case 'S': |
| 6278 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 6279 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 6280 | Signed = true; |
| 6281 | break; |
| 6282 | case 'U': |
| 6283 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
| 6284 | assert(!Unsigned && "Can't use 'S' modifier multiple times!"); |
| 6285 | Unsigned = true; |
| 6286 | break; |
| 6287 | case 'L': |
| 6288 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 6289 | ++HowLong; |
| 6290 | break; |
| 6291 | } |
| 6292 | } |
| 6293 | |
| 6294 | QualType Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6295 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6296 | // Read the base type. |
| 6297 | switch (*Str++) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6298 | default: llvm_unreachable("Unknown builtin type letter!"); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6299 | case 'v': |
| 6300 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 6301 | "Bad modifiers used with 'v'!"); |
| 6302 | Type = Context.VoidTy; |
| 6303 | break; |
| 6304 | case 'f': |
| 6305 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 6306 | "Bad modifiers used with 'f'!"); |
| 6307 | Type = Context.FloatTy; |
| 6308 | break; |
| 6309 | case 'd': |
| 6310 | assert(HowLong < 2 && !Signed && !Unsigned && |
| 6311 | "Bad modifiers used with 'd'!"); |
| 6312 | if (HowLong) |
| 6313 | Type = Context.LongDoubleTy; |
| 6314 | else |
| 6315 | Type = Context.DoubleTy; |
| 6316 | break; |
| 6317 | case 's': |
| 6318 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 6319 | if (Unsigned) |
| 6320 | Type = Context.UnsignedShortTy; |
| 6321 | else |
| 6322 | Type = Context.ShortTy; |
| 6323 | break; |
| 6324 | case 'i': |
| 6325 | if (HowLong == 3) |
| 6326 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 6327 | else if (HowLong == 2) |
| 6328 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 6329 | else if (HowLong == 1) |
| 6330 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 6331 | else |
| 6332 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 6333 | break; |
| 6334 | case 'c': |
| 6335 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 6336 | if (Signed) |
| 6337 | Type = Context.SignedCharTy; |
| 6338 | else if (Unsigned) |
| 6339 | Type = Context.UnsignedCharTy; |
| 6340 | else |
| 6341 | Type = Context.CharTy; |
| 6342 | break; |
| 6343 | case 'b': // boolean |
| 6344 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 6345 | Type = Context.BoolTy; |
| 6346 | break; |
| 6347 | case 'z': // size_t. |
| 6348 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 6349 | Type = Context.getSizeType(); |
| 6350 | break; |
| 6351 | case 'F': |
| 6352 | Type = Context.getCFConstantStringType(); |
| 6353 | break; |
Fariborz Jahanian | ba8bda0 | 2010-11-09 21:38:20 +0000 | [diff] [blame] | 6354 | case 'G': |
| 6355 | Type = Context.getObjCIdType(); |
| 6356 | break; |
| 6357 | case 'H': |
| 6358 | Type = Context.getObjCSelType(); |
| 6359 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6360 | case 'a': |
| 6361 | Type = Context.getBuiltinVaListType(); |
| 6362 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 6363 | break; |
| 6364 | case 'A': |
| 6365 | // This is a "reference" to a va_list; however, what exactly |
| 6366 | // this means depends on how va_list is defined. There are two |
| 6367 | // different kinds of va_list: ones passed by value, and ones |
| 6368 | // passed by reference. An example of a by-value va_list is |
| 6369 | // x86, where va_list is a char*. An example of by-ref va_list |
| 6370 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 6371 | // we want this argument to be a char*&; for x86-64, we want |
| 6372 | // it to be a __va_list_tag*. |
| 6373 | Type = Context.getBuiltinVaListType(); |
| 6374 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6375 | if (Type->isArrayType()) |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6376 | Type = Context.getArrayDecayedType(Type); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6377 | else |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6378 | Type = Context.getLValueReferenceType(Type); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6379 | break; |
| 6380 | case 'V': { |
| 6381 | char *End; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6382 | unsigned NumElements = strtoul(Str, &End, 10); |
| 6383 | assert(End != Str && "Missing vector size"); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6384 | Str = End; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6385 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6386 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, |
| 6387 | RequiresICE, false); |
| 6388 | assert(!RequiresICE && "Can't require vector ICE"); |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6389 | |
| 6390 | // TODO: No way to make AltiVec vectors in builtins yet. |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 6391 | Type = Context.getVectorType(ElementType, NumElements, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 6392 | VectorType::GenericVector); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6393 | break; |
| 6394 | } |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 6395 | case 'X': { |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6396 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
| 6397 | false); |
| 6398 | assert(!RequiresICE && "Can't require complex ICE"); |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 6399 | Type = Context.getComplexType(ElementType); |
| 6400 | break; |
Fariborz Jahanian | cc075e4 | 2011-08-23 23:33:09 +0000 | [diff] [blame] | 6401 | } |
| 6402 | case 'Y' : { |
| 6403 | Type = Context.getPointerDiffType(); |
| 6404 | break; |
| 6405 | } |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 6406 | case 'P': |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 6407 | Type = Context.getFILEType(); |
| 6408 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6409 | Error = ASTContext::GE_Missing_stdio; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6410 | return QualType(); |
| 6411 | } |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 6412 | break; |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 6413 | case 'J': |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6414 | if (Signed) |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 6415 | Type = Context.getsigjmp_bufType(); |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6416 | else |
| 6417 | Type = Context.getjmp_bufType(); |
| 6418 | |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 6419 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6420 | Error = ASTContext::GE_Missing_setjmp; |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 6421 | return QualType(); |
| 6422 | } |
| 6423 | break; |
Rafael Espindola | e2d4f4e | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 6424 | case 'K': |
| 6425 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'K'!"); |
| 6426 | Type = Context.getucontext_tType(); |
| 6427 | |
| 6428 | if (Type.isNull()) { |
| 6429 | Error = ASTContext::GE_Missing_ucontext; |
| 6430 | return QualType(); |
| 6431 | } |
| 6432 | break; |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 6433 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6434 | |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6435 | // If there are modifiers and if we're allowed to parse them, go for it. |
| 6436 | Done = !AllowTypeModifiers; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6437 | while (!Done) { |
John McCall | 187ab37 | 2010-03-12 04:21:28 +0000 | [diff] [blame] | 6438 | switch (char c = *Str++) { |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6439 | default: Done = true; --Str; break; |
| 6440 | case '*': |
| 6441 | case '&': { |
| 6442 | // Both pointers and references can have their pointee types |
| 6443 | // qualified with an address space. |
| 6444 | char *End; |
| 6445 | unsigned AddrSpace = strtoul(Str, &End, 10); |
| 6446 | if (End != Str && AddrSpace != 0) { |
| 6447 | Type = Context.getAddrSpaceQualType(Type, AddrSpace); |
| 6448 | Str = End; |
| 6449 | } |
| 6450 | if (c == '*') |
| 6451 | Type = Context.getPointerType(Type); |
| 6452 | else |
| 6453 | Type = Context.getLValueReferenceType(Type); |
| 6454 | break; |
| 6455 | } |
| 6456 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 6457 | case 'C': |
| 6458 | Type = Type.withConst(); |
| 6459 | break; |
| 6460 | case 'D': |
| 6461 | Type = Context.getVolatileType(Type); |
| 6462 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6463 | } |
| 6464 | } |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6465 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6466 | assert((!RequiresICE || Type->isIntegralOrEnumerationType()) && |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6467 | "Integer constant 'I' type must be an integer"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6468 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6469 | return Type; |
| 6470 | } |
| 6471 | |
| 6472 | /// GetBuiltinType - Return the type for the specified builtin. |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6473 | QualType ASTContext::GetBuiltinType(unsigned Id, |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6474 | GetBuiltinTypeError &Error, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6475 | unsigned *IntegerConstantArgs) const { |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6476 | const char *TypeStr = BuiltinInfo.GetTypeString(Id); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6477 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6478 | SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6479 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6480 | bool RequiresICE = false; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6481 | Error = GE_None; |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6482 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error, |
| 6483 | RequiresICE, true); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6484 | if (Error != GE_None) |
| 6485 | return QualType(); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6486 | |
| 6487 | assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE"); |
| 6488 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6489 | while (TypeStr[0] && TypeStr[0] != '.') { |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6490 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6491 | if (Error != GE_None) |
| 6492 | return QualType(); |
| 6493 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6494 | // If this argument is required to be an IntegerConstantExpression and the |
| 6495 | // caller cares, fill in the bitmask we return. |
| 6496 | if (RequiresICE && IntegerConstantArgs) |
| 6497 | *IntegerConstantArgs |= 1 << ArgTypes.size(); |
| 6498 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6499 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 6500 | if (Ty->isArrayType()) |
| 6501 | Ty = getArrayDecayedType(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6502 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6503 | ArgTypes.push_back(Ty); |
| 6504 | } |
| 6505 | |
| 6506 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 6507 | "'.' should only occur at end of builtin type list!"); |
| 6508 | |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 6509 | FunctionType::ExtInfo EI; |
| 6510 | if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true); |
| 6511 | |
| 6512 | bool Variadic = (TypeStr[0] == '.'); |
| 6513 | |
| 6514 | // We really shouldn't be making a no-proto type here, especially in C++. |
| 6515 | if (ArgTypes.empty() && Variadic) |
| 6516 | return getFunctionNoProtoType(ResType, EI); |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 6517 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6518 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 6519 | EPI.ExtInfo = EI; |
| 6520 | EPI.Variadic = Variadic; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6521 | |
| 6522 | return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), EPI); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6523 | } |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 6524 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6525 | GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) { |
| 6526 | GVALinkage External = GVA_StrongExternal; |
| 6527 | |
| 6528 | Linkage L = FD->getLinkage(); |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6529 | switch (L) { |
| 6530 | case NoLinkage: |
| 6531 | case InternalLinkage: |
| 6532 | case UniqueExternalLinkage: |
| 6533 | return GVA_Internal; |
| 6534 | |
| 6535 | case ExternalLinkage: |
| 6536 | switch (FD->getTemplateSpecializationKind()) { |
| 6537 | case TSK_Undeclared: |
| 6538 | case TSK_ExplicitSpecialization: |
| 6539 | External = GVA_StrongExternal; |
| 6540 | break; |
| 6541 | |
| 6542 | case TSK_ExplicitInstantiationDefinition: |
| 6543 | return GVA_ExplicitTemplateInstantiation; |
| 6544 | |
| 6545 | case TSK_ExplicitInstantiationDeclaration: |
| 6546 | case TSK_ImplicitInstantiation: |
| 6547 | External = GVA_TemplateInstantiation; |
| 6548 | break; |
| 6549 | } |
| 6550 | } |
| 6551 | |
| 6552 | if (!FD->isInlined()) |
| 6553 | return External; |
| 6554 | |
| 6555 | if (!getLangOptions().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) { |
| 6556 | // GNU or C99 inline semantics. Determine whether this symbol should be |
| 6557 | // externally visible. |
| 6558 | if (FD->isInlineDefinitionExternallyVisible()) |
| 6559 | return External; |
| 6560 | |
| 6561 | // C99 inline semantics, where the symbol is not externally visible. |
| 6562 | return GVA_C99Inline; |
| 6563 | } |
| 6564 | |
| 6565 | // C++0x [temp.explicit]p9: |
| 6566 | // [ Note: The intent is that an inline function that is the subject of |
| 6567 | // an explicit instantiation declaration will still be implicitly |
| 6568 | // instantiated when used so that the body can be considered for |
| 6569 | // inlining, but that no out-of-line copy of the inline function would be |
| 6570 | // generated in the translation unit. -- end note ] |
| 6571 | if (FD->getTemplateSpecializationKind() |
| 6572 | == TSK_ExplicitInstantiationDeclaration) |
| 6573 | return GVA_C99Inline; |
| 6574 | |
| 6575 | return GVA_CXXInline; |
| 6576 | } |
| 6577 | |
| 6578 | GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) { |
| 6579 | // If this is a static data member, compute the kind of template |
| 6580 | // specialization. Otherwise, this variable is not part of a |
| 6581 | // template. |
| 6582 | TemplateSpecializationKind TSK = TSK_Undeclared; |
| 6583 | if (VD->isStaticDataMember()) |
| 6584 | TSK = VD->getTemplateSpecializationKind(); |
| 6585 | |
| 6586 | Linkage L = VD->getLinkage(); |
| 6587 | if (L == ExternalLinkage && getLangOptions().CPlusPlus && |
| 6588 | VD->getType()->getLinkage() == UniqueExternalLinkage) |
| 6589 | L = UniqueExternalLinkage; |
| 6590 | |
| 6591 | switch (L) { |
| 6592 | case NoLinkage: |
| 6593 | case InternalLinkage: |
| 6594 | case UniqueExternalLinkage: |
| 6595 | return GVA_Internal; |
| 6596 | |
| 6597 | case ExternalLinkage: |
| 6598 | switch (TSK) { |
| 6599 | case TSK_Undeclared: |
| 6600 | case TSK_ExplicitSpecialization: |
| 6601 | return GVA_StrongExternal; |
| 6602 | |
| 6603 | case TSK_ExplicitInstantiationDeclaration: |
| 6604 | llvm_unreachable("Variable should not be instantiated"); |
| 6605 | // Fall through to treat this like any other instantiation. |
| 6606 | |
| 6607 | case TSK_ExplicitInstantiationDefinition: |
| 6608 | return GVA_ExplicitTemplateInstantiation; |
| 6609 | |
| 6610 | case TSK_ImplicitInstantiation: |
| 6611 | return GVA_TemplateInstantiation; |
| 6612 | } |
| 6613 | } |
| 6614 | |
| 6615 | return GVA_StrongExternal; |
| 6616 | } |
| 6617 | |
Argyrios Kyrtzidis | 4ac7c0b | 2010-07-29 20:08:05 +0000 | [diff] [blame] | 6618 | bool ASTContext::DeclMustBeEmitted(const Decl *D) { |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6619 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 6620 | if (!VD->isFileVarDecl()) |
| 6621 | return false; |
Argyrios Kyrtzidis | 144b38a | 2011-09-13 21:35:00 +0000 | [diff] [blame] | 6622 | } else if (!isa<FunctionDecl>(D)) |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6623 | return false; |
| 6624 | |
Argyrios Kyrtzidis | ab411c8 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 6625 | // Weak references don't produce any output by themselves. |
| 6626 | if (D->hasAttr<WeakRefAttr>()) |
| 6627 | return false; |
| 6628 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6629 | // Aliases and used decls are required. |
| 6630 | if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>()) |
| 6631 | return true; |
| 6632 | |
| 6633 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 6634 | // Forward declarations aren't required. |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 6635 | if (!FD->doesThisDeclarationHaveABody()) |
Nick Lewycky | dce67a7 | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 6636 | return FD->doesDeclarationForceExternallyVisibleDefinition(); |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6637 | |
| 6638 | // Constructors and destructors are required. |
| 6639 | if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>()) |
| 6640 | return true; |
| 6641 | |
| 6642 | // The key function for a class is required. |
| 6643 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 6644 | const CXXRecordDecl *RD = MD->getParent(); |
| 6645 | if (MD->isOutOfLine() && RD->isDynamicClass()) { |
| 6646 | const CXXMethodDecl *KeyFunc = getKeyFunction(RD); |
| 6647 | if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl()) |
| 6648 | return true; |
| 6649 | } |
| 6650 | } |
| 6651 | |
| 6652 | GVALinkage Linkage = GetGVALinkageForFunction(FD); |
| 6653 | |
| 6654 | // static, static inline, always_inline, and extern inline functions can |
| 6655 | // always be deferred. Normal inline functions can be deferred in C99/C++. |
| 6656 | // Implicit template instantiations can also be deferred in C++. |
| 6657 | if (Linkage == GVA_Internal || Linkage == GVA_C99Inline || |
| 6658 | Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) |
| 6659 | return false; |
| 6660 | return true; |
| 6661 | } |
Douglas Gregor | 94da158 | 2011-09-10 00:22:34 +0000 | [diff] [blame] | 6662 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6663 | const VarDecl *VD = cast<VarDecl>(D); |
| 6664 | assert(VD->isFileVarDecl() && "Expected file scoped var"); |
| 6665 | |
Argyrios Kyrtzidis | ab411c8 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 6666 | if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly) |
| 6667 | return false; |
| 6668 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6669 | // Structs that have non-trivial constructors or destructors are required. |
| 6670 | |
| 6671 | // FIXME: Handle references. |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 6672 | // FIXME: Be more selective about which constructors we care about. |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6673 | if (const RecordType *RT = VD->getType()->getAs<RecordType>()) { |
| 6674 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 6675 | if (RD->hasDefinition() && !(RD->hasTrivialDefaultConstructor() && |
| 6676 | RD->hasTrivialCopyConstructor() && |
| 6677 | RD->hasTrivialMoveConstructor() && |
| 6678 | RD->hasTrivialDestructor())) |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6679 | return true; |
| 6680 | } |
| 6681 | } |
| 6682 | |
| 6683 | GVALinkage L = GetGVALinkageForVariable(VD); |
| 6684 | if (L == GVA_Internal || L == GVA_TemplateInstantiation) { |
| 6685 | if (!(VD->getInit() && VD->getInit()->HasSideEffects(*this))) |
| 6686 | return false; |
| 6687 | } |
| 6688 | |
| 6689 | return true; |
| 6690 | } |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 6691 | |
Charles Davis | ee743f9 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 6692 | CallingConv ASTContext::getDefaultMethodCallConv() { |
| 6693 | // Pass through to the C++ ABI object |
| 6694 | return ABI->getDefaultMethodCallConv(); |
| 6695 | } |
| 6696 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6697 | bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const { |
Anders Carlsson | dae0cb5 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 6698 | // Pass through to the C++ ABI object |
| 6699 | return ABI->isNearlyEmpty(RD); |
| 6700 | } |
| 6701 | |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 6702 | MangleContext *ASTContext::createMangleContext() { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 6703 | switch (Target->getCXXABI()) { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 6704 | case CXXABI_ARM: |
| 6705 | case CXXABI_Itanium: |
| 6706 | return createItaniumMangleContext(*this, getDiagnostics()); |
| 6707 | case CXXABI_Microsoft: |
| 6708 | return createMicrosoftMangleContext(*this, getDiagnostics()); |
| 6709 | } |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6710 | llvm_unreachable("Unsupported ABI"); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 6711 | } |
| 6712 | |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 6713 | CXXABI::~CXXABI() {} |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 6714 | |
| 6715 | size_t ASTContext::getSideTableAllocatedMemory() const { |
Ted Kremenek | 0c8cd1a | 2011-07-27 18:41:12 +0000 | [diff] [blame] | 6716 | return ASTRecordLayouts.getMemorySize() |
| 6717 | + llvm::capacity_in_bytes(ObjCLayouts) |
| 6718 | + llvm::capacity_in_bytes(KeyFunctions) |
| 6719 | + llvm::capacity_in_bytes(ObjCImpls) |
| 6720 | + llvm::capacity_in_bytes(BlockVarCopyInits) |
| 6721 | + llvm::capacity_in_bytes(DeclAttrs) |
| 6722 | + llvm::capacity_in_bytes(InstantiatedFromStaticDataMember) |
| 6723 | + llvm::capacity_in_bytes(InstantiatedFromUsingDecl) |
| 6724 | + llvm::capacity_in_bytes(InstantiatedFromUsingShadowDecl) |
| 6725 | + llvm::capacity_in_bytes(InstantiatedFromUnnamedFieldDecl) |
| 6726 | + llvm::capacity_in_bytes(OverriddenMethods) |
| 6727 | + llvm::capacity_in_bytes(Types) |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 6728 | + llvm::capacity_in_bytes(VariableArrayTypes) |
Francois Pichet | 0d95f0d | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 6729 | + llvm::capacity_in_bytes(ClassScopeSpecializationPattern); |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 6730 | } |
Ted Kremenek | d211cb7 | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 6731 | |
| 6732 | void ASTContext::setParameterIndex(const ParmVarDecl *D, unsigned int index) { |
| 6733 | ParamIndices[D] = index; |
| 6734 | } |
| 6735 | |
| 6736 | unsigned ASTContext::getParameterIndex(const ParmVarDecl *D) const { |
| 6737 | ParameterIndexTable::const_iterator I = ParamIndices.find(D); |
| 6738 | assert(I != ParamIndices.end() && |
| 6739 | "ParmIndices lacks entry set by ParmVarDecl"); |
| 6740 | return I->second; |
| 6741 | } |