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()); |
Eli Friedman | 9e9c454 | 2012-03-07 01:09:33 +0000 | [diff] [blame] | 77 | ID.AddPointer(NTTP->getType().getCanonicalType().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()); |
Eli Friedman | 9e9c454 | 2012-03-07 01:09:33 +0000 | [diff] [blame] | 81 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) { |
| 82 | QualType T = NTTP->getExpansionType(I); |
| 83 | ID.AddPointer(T.getCanonicalType().getAsOpaquePtr()); |
| 84 | } |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 85 | } else |
| 86 | ID.AddBoolean(false); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 87 | continue; |
| 88 | } |
| 89 | |
| 90 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); |
| 91 | ID.AddInteger(2); |
| 92 | Profile(ID, TTP); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | TemplateTemplateParmDecl * |
| 97 | ASTContext::getCanonicalTemplateTemplateParmDecl( |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 98 | TemplateTemplateParmDecl *TTP) const { |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 99 | // Check if we already have a canonical template template parameter. |
| 100 | llvm::FoldingSetNodeID ID; |
| 101 | CanonicalTemplateTemplateParm::Profile(ID, TTP); |
| 102 | void *InsertPos = 0; |
| 103 | CanonicalTemplateTemplateParm *Canonical |
| 104 | = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
| 105 | if (Canonical) |
| 106 | return Canonical->getParam(); |
| 107 | |
| 108 | // Build a canonical template parameter list. |
| 109 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 110 | SmallVector<NamedDecl *, 4> CanonParams; |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 111 | CanonParams.reserve(Params->size()); |
| 112 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 113 | PEnd = Params->end(); |
| 114 | P != PEnd; ++P) { |
| 115 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) |
| 116 | CanonParams.push_back( |
| 117 | TemplateTypeParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 118 | SourceLocation(), |
| 119 | SourceLocation(), |
| 120 | TTP->getDepth(), |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 121 | TTP->getIndex(), 0, false, |
| 122 | TTP->isParameterPack())); |
| 123 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 124 | = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 125 | QualType T = getCanonicalType(NTTP->getType()); |
| 126 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T); |
| 127 | NonTypeTemplateParmDecl *Param; |
| 128 | if (NTTP->isExpandedParameterPack()) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 129 | SmallVector<QualType, 2> ExpandedTypes; |
| 130 | SmallVector<TypeSourceInfo *, 2> ExpandedTInfos; |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 131 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) { |
| 132 | ExpandedTypes.push_back(getCanonicalType(NTTP->getExpansionType(I))); |
| 133 | ExpandedTInfos.push_back( |
| 134 | getTrivialTypeSourceInfo(ExpandedTypes.back())); |
| 135 | } |
| 136 | |
| 137 | Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 138 | SourceLocation(), |
| 139 | SourceLocation(), |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 140 | NTTP->getDepth(), |
| 141 | NTTP->getPosition(), 0, |
| 142 | T, |
| 143 | TInfo, |
| 144 | ExpandedTypes.data(), |
| 145 | ExpandedTypes.size(), |
| 146 | ExpandedTInfos.data()); |
| 147 | } else { |
| 148 | Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 149 | SourceLocation(), |
| 150 | SourceLocation(), |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 151 | NTTP->getDepth(), |
| 152 | NTTP->getPosition(), 0, |
| 153 | T, |
| 154 | NTTP->isParameterPack(), |
| 155 | TInfo); |
| 156 | } |
| 157 | CanonParams.push_back(Param); |
| 158 | |
| 159 | } else |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 160 | CanonParams.push_back(getCanonicalTemplateTemplateParmDecl( |
| 161 | cast<TemplateTemplateParmDecl>(*P))); |
| 162 | } |
| 163 | |
| 164 | TemplateTemplateParmDecl *CanonTTP |
| 165 | = TemplateTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
| 166 | SourceLocation(), TTP->getDepth(), |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 167 | TTP->getPosition(), |
| 168 | TTP->isParameterPack(), |
| 169 | 0, |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 170 | TemplateParameterList::Create(*this, SourceLocation(), |
| 171 | SourceLocation(), |
| 172 | CanonParams.data(), |
| 173 | CanonParams.size(), |
| 174 | SourceLocation())); |
| 175 | |
| 176 | // Get the new insert position for the node we care about. |
| 177 | Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
| 178 | assert(Canonical == 0 && "Shouldn't be in the map!"); |
| 179 | (void)Canonical; |
| 180 | |
| 181 | // Create the canonical template template parameter entry. |
| 182 | Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP); |
| 183 | CanonTemplateTemplateParms.InsertNode(Canonical, InsertPos); |
| 184 | return CanonTTP; |
| 185 | } |
| 186 | |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 187 | CXXABI *ASTContext::createCXXABI(const TargetInfo &T) { |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 188 | if (!LangOpts.CPlusPlus) return 0; |
| 189 | |
Charles Davis | 20cf717 | 2010-08-19 02:18:14 +0000 | [diff] [blame] | 190 | switch (T.getCXXABI()) { |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 191 | case CXXABI_ARM: |
| 192 | return CreateARMCXXABI(*this); |
| 193 | case CXXABI_Itanium: |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 194 | return CreateItaniumCXXABI(*this); |
Charles Davis | 20cf717 | 2010-08-19 02:18:14 +0000 | [diff] [blame] | 195 | case CXXABI_Microsoft: |
| 196 | return CreateMicrosoftCXXABI(*this); |
| 197 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 198 | llvm_unreachable("Invalid CXXABI type!"); |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 201 | static const LangAS::Map *getAddressSpaceMap(const TargetInfo &T, |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 202 | const LangOptions &LOpts) { |
| 203 | if (LOpts.FakeAddressSpaceMap) { |
| 204 | // The fake address space map must have a distinct entry for each |
| 205 | // language-specific address space. |
| 206 | static const unsigned FakeAddrSpaceMap[] = { |
| 207 | 1, // opencl_global |
| 208 | 2, // opencl_local |
| 209 | 3 // opencl_constant |
| 210 | }; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 211 | return &FakeAddrSpaceMap; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 212 | } else { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 213 | return &T.getAddressSpaceMap(); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
Douglas Gregor | 3e3cd93 | 2011-09-01 20:23:19 +0000 | [diff] [blame] | 217 | ASTContext::ASTContext(LangOptions& LOpts, SourceManager &SM, |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 218 | const TargetInfo *t, |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 219 | IdentifierTable &idents, SelectorTable &sels, |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 220 | Builtin::Context &builtins, |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 221 | unsigned size_reserve, |
| 222 | bool DelayInitialization) |
| 223 | : FunctionProtoTypes(this_()), |
| 224 | TemplateSpecializationTypes(this_()), |
| 225 | DependentTemplateSpecializationTypes(this_()), |
| 226 | SubstTemplateTemplateParmPacks(this_()), |
| 227 | GlobalNestedNameSpecifier(0), |
| 228 | Int128Decl(0), UInt128Decl(0), |
Douglas Gregor | a6ea10e | 2012-01-17 18:09:05 +0000 | [diff] [blame] | 229 | ObjCIdDecl(0), ObjCSelDecl(0), ObjCClassDecl(0), ObjCProtocolClassDecl(0), |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 230 | CFConstantStringTypeDecl(0), ObjCInstanceTypeDecl(0), |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 231 | FILEDecl(0), |
Rafael Espindola | e2d4f4e | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 232 | jmp_bufDecl(0), sigjmp_bufDecl(0), ucontext_tDecl(0), |
| 233 | BlockDescriptorType(0), BlockDescriptorExtendedType(0), |
| 234 | cudaConfigureCallDecl(0), |
Douglas Gregor | e664977 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 235 | NullTypeSourceInfo(QualType()), |
| 236 | FirstLocalImport(), LastLocalImport(), |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 237 | SourceMgr(SM), LangOpts(LOpts), |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 238 | AddrSpaceMap(0), Target(t), PrintingPolicy(LOpts), |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 239 | Idents(idents), Selectors(sels), |
| 240 | BuiltinInfo(builtins), |
| 241 | DeclarationNames(*this), |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 242 | ExternalSource(0), Listener(0), |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 243 | LastSDM(0, 0), |
| 244 | UniqueBlockByRefTypeID(0) |
| 245 | { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 246 | if (size_reserve > 0) Types.reserve(size_reserve); |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 247 | TUDecl = TranslationUnitDecl::Create(*this); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 248 | |
| 249 | if (!DelayInitialization) { |
| 250 | assert(t && "No target supplied for ASTContext initialization"); |
| 251 | InitBuiltinTypes(*t); |
| 252 | } |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 255 | ASTContext::~ASTContext() { |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 256 | // Release the DenseMaps associated with DeclContext objects. |
| 257 | // FIXME: Is this the ideal solution? |
| 258 | ReleaseDeclContextMaps(); |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 259 | |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 260 | // Call all of the deallocation functions. |
| 261 | for (unsigned I = 0, N = Deallocations.size(); I != N; ++I) |
| 262 | Deallocations[I].first(Deallocations[I].second); |
Douglas Gregor | 0054531 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 263 | |
Ted Kremenek | dcfcfbe | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 264 | // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 265 | // because they can contain DenseMaps. |
| 266 | for (llvm::DenseMap<const ObjCContainerDecl*, |
| 267 | const ASTRecordLayout*>::iterator |
| 268 | I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) |
| 269 | // Increment in loop to prevent using deallocated memory. |
| 270 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 271 | R->Destroy(*this); |
| 272 | |
Ted Kremenek | dcfcfbe | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 273 | for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator |
| 274 | I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) { |
| 275 | // Increment in loop to prevent using deallocated memory. |
| 276 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 277 | R->Destroy(*this); |
| 278 | } |
Douglas Gregor | 6320064 | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 279 | |
| 280 | for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(), |
| 281 | AEnd = DeclAttrs.end(); |
| 282 | A != AEnd; ++A) |
| 283 | A->second->~AttrVec(); |
| 284 | } |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 285 | |
Douglas Gregor | 0054531 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 286 | void ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) { |
| 287 | Deallocations.push_back(std::make_pair(Callback, Data)); |
| 288 | } |
| 289 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 290 | void |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 291 | ASTContext::setExternalSource(OwningPtr<ExternalASTSource> &Source) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 292 | ExternalSource.reset(Source.take()); |
| 293 | } |
| 294 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 295 | void ASTContext::PrintStats() const { |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 296 | llvm::errs() << "\n*** AST Context Stats:\n"; |
| 297 | llvm::errs() << " " << Types.size() << " types total.\n"; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 298 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 299 | unsigned counts[] = { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 300 | #define TYPE(Name, Parent) 0, |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 301 | #define ABSTRACT_TYPE(Name, Parent) |
| 302 | #include "clang/AST/TypeNodes.def" |
| 303 | 0 // Extra |
| 304 | }; |
Douglas Gregor | c2ee10d | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 305 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 306 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 307 | Type *T = Types[i]; |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 308 | counts[(unsigned)T->getTypeClass()]++; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 311 | unsigned Idx = 0; |
| 312 | unsigned TotalBytes = 0; |
| 313 | #define TYPE(Name, Parent) \ |
| 314 | if (counts[Idx]) \ |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 315 | llvm::errs() << " " << counts[Idx] << " " << #Name \ |
| 316 | << " types\n"; \ |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 317 | TotalBytes += counts[Idx] * sizeof(Name##Type); \ |
| 318 | ++Idx; |
| 319 | #define ABSTRACT_TYPE(Name, Parent) |
| 320 | #include "clang/AST/TypeNodes.def" |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 321 | |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 322 | llvm::errs() << "Total bytes = " << TotalBytes << "\n"; |
| 323 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 324 | // Implicit special member functions. |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 325 | llvm::errs() << NumImplicitDefaultConstructorsDeclared << "/" |
| 326 | << NumImplicitDefaultConstructors |
| 327 | << " implicit default constructors created\n"; |
| 328 | llvm::errs() << NumImplicitCopyConstructorsDeclared << "/" |
| 329 | << NumImplicitCopyConstructors |
| 330 | << " implicit copy constructors created\n"; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 331 | if (getLangOpts().CPlusPlus) |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 332 | llvm::errs() << NumImplicitMoveConstructorsDeclared << "/" |
| 333 | << NumImplicitMoveConstructors |
| 334 | << " implicit move constructors created\n"; |
| 335 | llvm::errs() << NumImplicitCopyAssignmentOperatorsDeclared << "/" |
| 336 | << NumImplicitCopyAssignmentOperators |
| 337 | << " implicit copy assignment operators created\n"; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 338 | if (getLangOpts().CPlusPlus) |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 339 | llvm::errs() << NumImplicitMoveAssignmentOperatorsDeclared << "/" |
| 340 | << NumImplicitMoveAssignmentOperators |
| 341 | << " implicit move assignment operators created\n"; |
| 342 | llvm::errs() << NumImplicitDestructorsDeclared << "/" |
| 343 | << NumImplicitDestructors |
| 344 | << " implicit destructors created\n"; |
| 345 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 346 | if (ExternalSource.get()) { |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 347 | llvm::errs() << "\n"; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 348 | ExternalSource->PrintStats(); |
| 349 | } |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 350 | |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 351 | BumpAlloc.PrintStats(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Douglas Gregor | 772eeae | 2011-08-12 06:49:56 +0000 | [diff] [blame] | 354 | TypedefDecl *ASTContext::getInt128Decl() const { |
| 355 | if (!Int128Decl) { |
| 356 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(Int128Ty); |
| 357 | Int128Decl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 358 | getTranslationUnitDecl(), |
| 359 | SourceLocation(), |
| 360 | SourceLocation(), |
| 361 | &Idents.get("__int128_t"), |
| 362 | TInfo); |
| 363 | } |
| 364 | |
| 365 | return Int128Decl; |
| 366 | } |
| 367 | |
| 368 | TypedefDecl *ASTContext::getUInt128Decl() const { |
| 369 | if (!UInt128Decl) { |
| 370 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(UnsignedInt128Ty); |
| 371 | UInt128Decl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 372 | getTranslationUnitDecl(), |
| 373 | SourceLocation(), |
| 374 | SourceLocation(), |
| 375 | &Idents.get("__uint128_t"), |
| 376 | TInfo); |
| 377 | } |
| 378 | |
| 379 | return UInt128Decl; |
| 380 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 381 | |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 382 | void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 383 | BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 384 | R = CanQualType::CreateUnsafe(QualType(Ty, 0)); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 385 | Types.push_back(Ty); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 388 | void ASTContext::InitBuiltinTypes(const TargetInfo &Target) { |
| 389 | assert((!this->Target || this->Target == &Target) && |
| 390 | "Incorrect target reinitialization"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 391 | assert(VoidTy.isNull() && "Context reinitialized?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 392 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 393 | this->Target = &Target; |
| 394 | |
| 395 | ABI.reset(createCXXABI(Target)); |
| 396 | AddrSpaceMap = getAddressSpaceMap(Target, LangOpts); |
| 397 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 398 | // C99 6.2.5p19. |
| 399 | InitBuiltinType(VoidTy, BuiltinType::Void); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 400 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 401 | // C99 6.2.5p2. |
| 402 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
| 403 | // C99 6.2.5p3. |
Eli Friedman | 15b9176 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 404 | if (LangOpts.CharIsSigned) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 405 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 406 | else |
| 407 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
| 408 | // C99 6.2.5p4. |
| 409 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 410 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 411 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 412 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 413 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 414 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 415 | // C99 6.2.5p6. |
| 416 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 417 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 418 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 419 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 420 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 421 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 422 | // C99 6.2.5p10. |
| 423 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 424 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 425 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 426 | |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 427 | // GNU extension, 128-bit integers. |
| 428 | InitBuiltinType(Int128Ty, BuiltinType::Int128); |
| 429 | InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128); |
| 430 | |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 431 | if (LangOpts.CPlusPlus) { // C++ 3.9.1p5 |
Eli Friedman | d3d77cd | 2011-04-30 19:24:24 +0000 | [diff] [blame] | 432 | if (TargetInfo::isTypeSigned(Target.getWCharType())) |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 433 | InitBuiltinType(WCharTy, BuiltinType::WChar_S); |
| 434 | else // -fshort-wchar makes wchar_t be unsigned. |
| 435 | InitBuiltinType(WCharTy, BuiltinType::WChar_U); |
| 436 | } else // C99 |
Chris Lattner | 3a25032 | 2009-02-26 23:43:47 +0000 | [diff] [blame] | 437 | WCharTy = getFromTargetType(Target.getWCharType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 438 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 439 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 440 | InitBuiltinType(Char16Ty, BuiltinType::Char16); |
| 441 | else // C99 |
| 442 | Char16Ty = getFromTargetType(Target.getChar16Type()); |
| 443 | |
| 444 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 445 | InitBuiltinType(Char32Ty, BuiltinType::Char32); |
| 446 | else // C99 |
| 447 | Char32Ty = getFromTargetType(Target.getChar32Type()); |
| 448 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 449 | // Placeholder type for type-dependent expressions whose type is |
| 450 | // completely unknown. No code should ever check a type against |
| 451 | // DependentTy and users should never see it; however, it is here to |
| 452 | // help diagnose failures to properly check for type-dependent |
| 453 | // expressions. |
| 454 | InitBuiltinType(DependentTy, BuiltinType::Dependent); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 455 | |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 456 | // Placeholder type for functions. |
| 457 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
| 458 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 459 | // Placeholder type for bound members. |
| 460 | InitBuiltinType(BoundMemberTy, BuiltinType::BoundMember); |
| 461 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 462 | // Placeholder type for pseudo-objects. |
| 463 | InitBuiltinType(PseudoObjectTy, BuiltinType::PseudoObject); |
| 464 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 465 | // "any" type; useful for debugger-like clients. |
| 466 | InitBuiltinType(UnknownAnyTy, BuiltinType::UnknownAny); |
| 467 | |
John McCall | 0ddaeb9 | 2011-10-17 18:09:15 +0000 | [diff] [blame] | 468 | // Placeholder type for unbridged ARC casts. |
| 469 | InitBuiltinType(ARCUnbridgedCastTy, BuiltinType::ARCUnbridgedCast); |
| 470 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 471 | // C99 6.2.5p11. |
| 472 | FloatComplexTy = getComplexType(FloatTy); |
| 473 | DoubleComplexTy = getComplexType(DoubleTy); |
| 474 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 475 | |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 476 | BuiltinVaListType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 477 | |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 478 | // Builtin types for 'id', 'Class', and 'SEL'. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 479 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); |
| 480 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 481 | InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 482 | |
| 483 | // Builtin type for __objc_yes and __objc_no |
| 484 | ObjCBuiltinBoolTy = SignedCharTy; |
| 485 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 486 | ObjCConstantStringType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 487 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 488 | // void * type |
| 489 | VoidPtrTy = getPointerType(VoidTy); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 490 | |
| 491 | // nullptr type (C++0x 2.14.7) |
| 492 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 493 | |
| 494 | // half type (OpenCL 6.1.1.1) / ARM NEON __fp16 |
| 495 | InitBuiltinType(HalfTy, BuiltinType::Half); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 496 | } |
| 497 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 498 | DiagnosticsEngine &ASTContext::getDiagnostics() const { |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 499 | return SourceMgr.getDiagnostics(); |
| 500 | } |
| 501 | |
Douglas Gregor | 6320064 | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 502 | AttrVec& ASTContext::getDeclAttrs(const Decl *D) { |
| 503 | AttrVec *&Result = DeclAttrs[D]; |
| 504 | if (!Result) { |
| 505 | void *Mem = Allocate(sizeof(AttrVec)); |
| 506 | Result = new (Mem) AttrVec; |
| 507 | } |
| 508 | |
| 509 | return *Result; |
| 510 | } |
| 511 | |
| 512 | /// \brief Erase the attributes corresponding to the given declaration. |
| 513 | void ASTContext::eraseDeclAttrs(const Decl *D) { |
| 514 | llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D); |
| 515 | if (Pos != DeclAttrs.end()) { |
| 516 | Pos->second->~AttrVec(); |
| 517 | DeclAttrs.erase(Pos); |
| 518 | } |
| 519 | } |
| 520 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 521 | MemberSpecializationInfo * |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 522 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 523 | assert(Var->isStaticDataMember() && "Not a static data member"); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 524 | llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 525 | = InstantiatedFromStaticDataMember.find(Var); |
| 526 | if (Pos == InstantiatedFromStaticDataMember.end()) |
| 527 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 528 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 529 | return Pos->second; |
| 530 | } |
| 531 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 532 | void |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 533 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, |
Argyrios Kyrtzidis | 9421adc | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 534 | TemplateSpecializationKind TSK, |
| 535 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 536 | assert(Inst->isStaticDataMember() && "Not a static data member"); |
| 537 | assert(Tmpl->isStaticDataMember() && "Not a static data member"); |
| 538 | assert(!InstantiatedFromStaticDataMember[Inst] && |
| 539 | "Already noted what static data member was instantiated from"); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 540 | InstantiatedFromStaticDataMember[Inst] |
Argyrios Kyrtzidis | 9421adc | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 541 | = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 544 | FunctionDecl *ASTContext::getClassScopeSpecializationPattern( |
| 545 | const FunctionDecl *FD){ |
| 546 | assert(FD && "Specialization is 0"); |
| 547 | llvm::DenseMap<const FunctionDecl*, FunctionDecl *>::const_iterator Pos |
Francois Pichet | 0d95f0d | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 548 | = ClassScopeSpecializationPattern.find(FD); |
| 549 | if (Pos == ClassScopeSpecializationPattern.end()) |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 550 | return 0; |
| 551 | |
| 552 | return Pos->second; |
| 553 | } |
| 554 | |
| 555 | void ASTContext::setClassScopeSpecializationPattern(FunctionDecl *FD, |
| 556 | FunctionDecl *Pattern) { |
| 557 | assert(FD && "Specialization is 0"); |
| 558 | assert(Pattern && "Class scope specialization pattern is 0"); |
Francois Pichet | 0d95f0d | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 559 | ClassScopeSpecializationPattern[FD] = Pattern; |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 560 | } |
| 561 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 562 | NamedDecl * |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 563 | ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 564 | llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 565 | = InstantiatedFromUsingDecl.find(UUD); |
| 566 | if (Pos == InstantiatedFromUsingDecl.end()) |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 567 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 568 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 569 | return Pos->second; |
| 570 | } |
| 571 | |
| 572 | void |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 573 | ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) { |
| 574 | assert((isa<UsingDecl>(Pattern) || |
| 575 | isa<UnresolvedUsingValueDecl>(Pattern) || |
| 576 | isa<UnresolvedUsingTypenameDecl>(Pattern)) && |
| 577 | "pattern decl is not a using decl"); |
| 578 | assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists"); |
| 579 | InstantiatedFromUsingDecl[Inst] = Pattern; |
| 580 | } |
| 581 | |
| 582 | UsingShadowDecl * |
| 583 | ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) { |
| 584 | llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos |
| 585 | = InstantiatedFromUsingShadowDecl.find(Inst); |
| 586 | if (Pos == InstantiatedFromUsingShadowDecl.end()) |
| 587 | return 0; |
| 588 | |
| 589 | return Pos->second; |
| 590 | } |
| 591 | |
| 592 | void |
| 593 | ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, |
| 594 | UsingShadowDecl *Pattern) { |
| 595 | assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists"); |
| 596 | InstantiatedFromUsingShadowDecl[Inst] = Pattern; |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 599 | FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { |
| 600 | llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos |
| 601 | = InstantiatedFromUnnamedFieldDecl.find(Field); |
| 602 | if (Pos == InstantiatedFromUnnamedFieldDecl.end()) |
| 603 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 604 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 605 | return Pos->second; |
| 606 | } |
| 607 | |
| 608 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, |
| 609 | FieldDecl *Tmpl) { |
| 610 | assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed"); |
| 611 | assert(!Tmpl->getDeclName() && "Template field decl is not unnamed"); |
| 612 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] && |
| 613 | "Already noted what unnamed field was instantiated from"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 614 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 615 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; |
| 616 | } |
| 617 | |
Fariborz Jahanian | 14d56ef | 2011-04-27 17:14:21 +0000 | [diff] [blame] | 618 | bool ASTContext::ZeroBitfieldFollowsNonBitfield(const FieldDecl *FD, |
| 619 | const FieldDecl *LastFD) const { |
| 620 | return (FD->isBitField() && LastFD && !LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 621 | FD->getBitWidthValue(*this) == 0); |
Fariborz Jahanian | 14d56ef | 2011-04-27 17:14:21 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Fariborz Jahanian | 340fa24 | 2011-05-02 17:20:56 +0000 | [diff] [blame] | 624 | bool ASTContext::ZeroBitfieldFollowsBitfield(const FieldDecl *FD, |
| 625 | const FieldDecl *LastFD) const { |
| 626 | return (FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 627 | FD->getBitWidthValue(*this) == 0 && |
| 628 | LastFD->getBitWidthValue(*this) != 0); |
Fariborz Jahanian | 340fa24 | 2011-05-02 17:20:56 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Fariborz Jahanian | 9b3acaa | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 631 | bool ASTContext::BitfieldFollowsBitfield(const FieldDecl *FD, |
| 632 | const FieldDecl *LastFD) const { |
| 633 | return (FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 634 | FD->getBitWidthValue(*this) && |
| 635 | LastFD->getBitWidthValue(*this)); |
Fariborz Jahanian | 9b3acaa | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Chad Rosier | dd7fddb | 2011-08-04 23:34:15 +0000 | [diff] [blame] | 638 | bool ASTContext::NonBitfieldFollowsBitfield(const FieldDecl *FD, |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 639 | const FieldDecl *LastFD) const { |
| 640 | return (!FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 641 | LastFD->getBitWidthValue(*this)); |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 642 | } |
| 643 | |
Chad Rosier | dd7fddb | 2011-08-04 23:34:15 +0000 | [diff] [blame] | 644 | bool ASTContext::BitfieldFollowsNonBitfield(const FieldDecl *FD, |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 645 | const FieldDecl *LastFD) const { |
| 646 | return (FD->isBitField() && LastFD && !LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 647 | FD->getBitWidthValue(*this)); |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 648 | } |
| 649 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 650 | ASTContext::overridden_cxx_method_iterator |
| 651 | ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const { |
| 652 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 653 | = OverriddenMethods.find(Method); |
| 654 | if (Pos == OverriddenMethods.end()) |
| 655 | return 0; |
| 656 | |
| 657 | return Pos->second.begin(); |
| 658 | } |
| 659 | |
| 660 | ASTContext::overridden_cxx_method_iterator |
| 661 | ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const { |
| 662 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 663 | = OverriddenMethods.find(Method); |
| 664 | if (Pos == OverriddenMethods.end()) |
| 665 | return 0; |
| 666 | |
| 667 | return Pos->second.end(); |
| 668 | } |
| 669 | |
Argyrios Kyrtzidis | c91e9f4 | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 670 | unsigned |
| 671 | ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const { |
| 672 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 673 | = OverriddenMethods.find(Method); |
| 674 | if (Pos == OverriddenMethods.end()) |
| 675 | return 0; |
| 676 | |
| 677 | return Pos->second.size(); |
| 678 | } |
| 679 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 680 | void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method, |
| 681 | const CXXMethodDecl *Overridden) { |
| 682 | OverriddenMethods[Method].push_back(Overridden); |
| 683 | } |
| 684 | |
Douglas Gregor | e664977 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 685 | void ASTContext::addedLocalImportDecl(ImportDecl *Import) { |
| 686 | assert(!Import->NextLocalImport && "Import declaration already in the chain"); |
| 687 | assert(!Import->isFromASTFile() && "Non-local import declaration"); |
| 688 | if (!FirstLocalImport) { |
| 689 | FirstLocalImport = Import; |
| 690 | LastLocalImport = Import; |
| 691 | return; |
| 692 | } |
| 693 | |
| 694 | LastLocalImport->NextLocalImport = Import; |
| 695 | LastLocalImport = Import; |
| 696 | } |
| 697 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 698 | //===----------------------------------------------------------------------===// |
| 699 | // Type Sizing and Analysis |
| 700 | //===----------------------------------------------------------------------===// |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 701 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 702 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 703 | /// scalar floating point type. |
| 704 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 705 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 706 | assert(BT && "Not a floating point type!"); |
| 707 | switch (BT->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 708 | default: llvm_unreachable("Not a floating point type!"); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 709 | case BuiltinType::Half: return Target->getHalfFormat(); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 710 | case BuiltinType::Float: return Target->getFloatFormat(); |
| 711 | case BuiltinType::Double: return Target->getDoubleFormat(); |
| 712 | case BuiltinType::LongDouble: return Target->getLongDoubleFormat(); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 713 | } |
| 714 | } |
| 715 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 716 | /// getDeclAlign - Return a conservative estimate of the alignment of the |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 717 | /// specified decl. Note that bitfields do not have a valid alignment, so |
| 718 | /// this method will assert on them. |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 719 | /// If @p RefAsPointee, references are treated like their underlying type |
| 720 | /// (for alignof), else they're treated like pointers (for CodeGen). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 721 | CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) const { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 722 | unsigned Align = Target->getCharWidth(); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 723 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 724 | bool UseAlignAttrOnly = false; |
| 725 | if (unsigned AlignFromAttr = D->getMaxAlignment()) { |
| 726 | Align = AlignFromAttr; |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 727 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 728 | // __attribute__((aligned)) can increase or decrease alignment |
| 729 | // *except* on a struct or struct member, where it only increases |
| 730 | // alignment unless 'packed' is also specified. |
| 731 | // |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 732 | // It is an error for alignas to decrease alignment, so we can |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 733 | // ignore that possibility; Sema should diagnose it. |
| 734 | if (isa<FieldDecl>(D)) { |
| 735 | UseAlignAttrOnly = D->hasAttr<PackedAttr>() || |
| 736 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
| 737 | } else { |
| 738 | UseAlignAttrOnly = true; |
| 739 | } |
| 740 | } |
Fariborz Jahanian | 78a7d7d | 2011-05-05 21:19:14 +0000 | [diff] [blame] | 741 | else if (isa<FieldDecl>(D)) |
| 742 | UseAlignAttrOnly = |
| 743 | D->hasAttr<PackedAttr>() || |
| 744 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 745 | |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 746 | // If we're using the align attribute only, just ignore everything |
| 747 | // else about the declaration and its type. |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 748 | if (UseAlignAttrOnly) { |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 749 | // do nothing |
| 750 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 751 | } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 752 | QualType T = VD->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 753 | if (const ReferenceType* RT = T->getAs<ReferenceType>()) { |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 754 | if (RefAsPointee) |
| 755 | T = RT->getPointeeType(); |
| 756 | else |
| 757 | T = getPointerType(RT->getPointeeType()); |
| 758 | } |
| 759 | if (!T->isIncompleteType() && !T->isFunctionType()) { |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 760 | // Adjust alignments of declarations with array type by the |
| 761 | // large-array alignment on the target. |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 762 | unsigned MinWidth = Target->getLargeArrayMinWidth(); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 763 | const ArrayType *arrayType; |
| 764 | if (MinWidth && (arrayType = getAsArrayType(T))) { |
| 765 | if (isa<VariableArrayType>(arrayType)) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 766 | Align = std::max(Align, Target->getLargeArrayAlign()); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 767 | else if (isa<ConstantArrayType>(arrayType) && |
| 768 | MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType))) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 769 | Align = std::max(Align, Target->getLargeArrayAlign()); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 770 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 771 | // Walk through any array types while we're at it. |
| 772 | T = getBaseElementType(arrayType); |
| 773 | } |
Chad Rosier | 9f1210c | 2011-07-26 07:03:04 +0000 | [diff] [blame] | 774 | Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 775 | } |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 776 | |
| 777 | // Fields can be subject to extra alignment constraints, like if |
| 778 | // the field is packed, the struct is packed, or the struct has a |
| 779 | // a max-field-alignment constraint (#pragma pack). So calculate |
| 780 | // the actual alignment of the field within the struct, and then |
| 781 | // (as we're expected to) constrain that by the alignment of the type. |
| 782 | if (const FieldDecl *field = dyn_cast<FieldDecl>(VD)) { |
| 783 | // So calculate the alignment of the field. |
| 784 | const ASTRecordLayout &layout = getASTRecordLayout(field->getParent()); |
| 785 | |
| 786 | // Start with the record's overall alignment. |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 787 | unsigned fieldAlign = toBits(layout.getAlignment()); |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 788 | |
| 789 | // Use the GCD of that and the offset within the record. |
| 790 | uint64_t offset = layout.getFieldOffset(field->getFieldIndex()); |
| 791 | if (offset > 0) { |
| 792 | // Alignment is always a power of 2, so the GCD will be a power of 2, |
| 793 | // which means we get to do this crazy thing instead of Euclid's. |
| 794 | uint64_t lowBitOfOffset = offset & (~offset + 1); |
| 795 | if (lowBitOfOffset < fieldAlign) |
| 796 | fieldAlign = static_cast<unsigned>(lowBitOfOffset); |
| 797 | } |
| 798 | |
| 799 | Align = std::min(Align, fieldAlign); |
Charles Davis | 05f6247 | 2010-02-23 04:52:00 +0000 | [diff] [blame] | 800 | } |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 801 | } |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 802 | |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 803 | return toCharUnitsFromBits(Align); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 804 | } |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 805 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 806 | std::pair<CharUnits, CharUnits> |
Ken Dyck | bee5a79 | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 807 | ASTContext::getTypeInfoInChars(const Type *T) const { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 808 | std::pair<uint64_t, unsigned> Info = getTypeInfo(T); |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 809 | return std::make_pair(toCharUnitsFromBits(Info.first), |
| 810 | toCharUnitsFromBits(Info.second)); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | std::pair<CharUnits, CharUnits> |
Ken Dyck | bee5a79 | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 814 | ASTContext::getTypeInfoInChars(QualType T) const { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 815 | return getTypeInfoInChars(T.getTypePtr()); |
| 816 | } |
| 817 | |
Daniel Dunbar | bc5419a | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 818 | std::pair<uint64_t, unsigned> ASTContext::getTypeInfo(const Type *T) const { |
| 819 | TypeInfoMap::iterator it = MemoizedTypeInfo.find(T); |
| 820 | if (it != MemoizedTypeInfo.end()) |
| 821 | return it->second; |
| 822 | |
| 823 | std::pair<uint64_t, unsigned> Info = getTypeInfoImpl(T); |
| 824 | MemoizedTypeInfo.insert(std::make_pair(T, Info)); |
| 825 | return Info; |
| 826 | } |
| 827 | |
| 828 | /// getTypeInfoImpl - Return the size of the specified type, in bits. This |
| 829 | /// method does not work on incomplete types. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 830 | /// |
| 831 | /// FIXME: Pointers into different addr spaces could have different sizes and |
| 832 | /// alignment requirements: getPointerInfo should take an AddrSpace, this |
| 833 | /// should take a QualType, &c. |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 834 | std::pair<uint64_t, unsigned> |
Daniel Dunbar | bc5419a | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 835 | ASTContext::getTypeInfoImpl(const Type *T) const { |
Mike Stump | 5e30100 | 2009-02-27 18:32:39 +0000 | [diff] [blame] | 836 | uint64_t Width=0; |
| 837 | unsigned Align=8; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 838 | switch (T->getTypeClass()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 839 | #define TYPE(Class, Base) |
| 840 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 841 | #define NON_CANONICAL_TYPE(Class, Base) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 842 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 843 | #include "clang/AST/TypeNodes.def" |
John McCall | d3d49bb | 2011-06-28 16:49:23 +0000 | [diff] [blame] | 844 | llvm_unreachable("Should not see dependent types"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 845 | |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 846 | case Type::FunctionNoProto: |
| 847 | case Type::FunctionProto: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 848 | // GCC extension: alignof(function) = 32 bits |
| 849 | Width = 0; |
| 850 | Align = 32; |
| 851 | break; |
| 852 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 853 | case Type::IncompleteArray: |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 854 | case Type::VariableArray: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 855 | Width = 0; |
| 856 | Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); |
| 857 | break; |
| 858 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 859 | case Type::ConstantArray: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 860 | const ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 861 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 862 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); |
Abramo Bagnara | fea966a | 2011-12-13 11:23:52 +0000 | [diff] [blame] | 863 | uint64_t Size = CAT->getSize().getZExtValue(); |
Daniel Dunbar | bc5419a | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 864 | assert((Size == 0 || EltInfo.first <= (uint64_t)(-1)/Size) && |
| 865 | "Overflow in array type bit size evaluation"); |
Abramo Bagnara | fea966a | 2011-12-13 11:23:52 +0000 | [diff] [blame] | 866 | Width = EltInfo.first*Size; |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 867 | Align = EltInfo.second; |
Argyrios Kyrtzidis | cd88b41 | 2011-04-26 21:05:39 +0000 | [diff] [blame] | 868 | Width = llvm::RoundUpToAlignment(Width, Align); |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 869 | break; |
Christopher Lamb | 5c09a02 | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 870 | } |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 871 | case Type::ExtVector: |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 872 | case Type::Vector: { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 873 | const VectorType *VT = cast<VectorType>(T); |
| 874 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType()); |
| 875 | Width = EltInfo.first*VT->getNumElements(); |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 876 | Align = Width; |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 877 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 878 | // This happens for non-power-of-2 length vectors. |
Dan Gohman | 8eefcd3 | 2010-04-21 23:32:43 +0000 | [diff] [blame] | 879 | if (Align & (Align-1)) { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 880 | Align = llvm::NextPowerOf2(Align); |
| 881 | Width = llvm::RoundUpToAlignment(Width, Align); |
| 882 | } |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 883 | break; |
| 884 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 885 | |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 886 | case Type::Builtin: |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 887 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 888 | default: llvm_unreachable("Unknown builtin type!"); |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 889 | case BuiltinType::Void: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 890 | // GCC extension: alignof(void) = 8 bits. |
| 891 | Width = 0; |
| 892 | Align = 8; |
| 893 | break; |
| 894 | |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 895 | case BuiltinType::Bool: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 896 | Width = Target->getBoolWidth(); |
| 897 | Align = Target->getBoolAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 898 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 899 | case BuiltinType::Char_S: |
| 900 | case BuiltinType::Char_U: |
| 901 | case BuiltinType::UChar: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 902 | case BuiltinType::SChar: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 903 | Width = Target->getCharWidth(); |
| 904 | Align = Target->getCharAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 905 | break; |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 906 | case BuiltinType::WChar_S: |
| 907 | case BuiltinType::WChar_U: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 908 | Width = Target->getWCharWidth(); |
| 909 | Align = Target->getWCharAlign(); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 910 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 911 | case BuiltinType::Char16: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 912 | Width = Target->getChar16Width(); |
| 913 | Align = Target->getChar16Align(); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 914 | break; |
| 915 | case BuiltinType::Char32: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 916 | Width = Target->getChar32Width(); |
| 917 | Align = Target->getChar32Align(); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 918 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 919 | case BuiltinType::UShort: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 920 | case BuiltinType::Short: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 921 | Width = Target->getShortWidth(); |
| 922 | Align = Target->getShortAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 923 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 924 | case BuiltinType::UInt: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 925 | case BuiltinType::Int: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 926 | Width = Target->getIntWidth(); |
| 927 | Align = Target->getIntAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 928 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 929 | case BuiltinType::ULong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 930 | case BuiltinType::Long: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 931 | Width = Target->getLongWidth(); |
| 932 | Align = Target->getLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 933 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 934 | case BuiltinType::ULongLong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 935 | case BuiltinType::LongLong: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 936 | Width = Target->getLongLongWidth(); |
| 937 | Align = Target->getLongLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 938 | break; |
Chris Lattner | ec16cb9 | 2009-04-30 02:55:13 +0000 | [diff] [blame] | 939 | case BuiltinType::Int128: |
| 940 | case BuiltinType::UInt128: |
| 941 | Width = 128; |
| 942 | Align = 128; // int128_t is 128-bit aligned on all targets. |
| 943 | break; |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 944 | case BuiltinType::Half: |
| 945 | Width = Target->getHalfWidth(); |
| 946 | Align = Target->getHalfAlign(); |
| 947 | break; |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 948 | case BuiltinType::Float: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 949 | Width = Target->getFloatWidth(); |
| 950 | Align = Target->getFloatAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 951 | break; |
| 952 | case BuiltinType::Double: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 953 | Width = Target->getDoubleWidth(); |
| 954 | Align = Target->getDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 955 | break; |
| 956 | case BuiltinType::LongDouble: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 957 | Width = Target->getLongDoubleWidth(); |
| 958 | Align = Target->getLongDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 959 | break; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 960 | case BuiltinType::NullPtr: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 961 | Width = Target->getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) |
| 962 | Align = Target->getPointerAlign(0); // == sizeof(void*) |
Sebastian Redl | 1590d9c | 2009-05-27 19:34:06 +0000 | [diff] [blame] | 963 | break; |
Fariborz Jahanian | e04f5fc | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 964 | case BuiltinType::ObjCId: |
| 965 | case BuiltinType::ObjCClass: |
| 966 | case BuiltinType::ObjCSel: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 967 | Width = Target->getPointerWidth(0); |
| 968 | Align = Target->getPointerAlign(0); |
Fariborz Jahanian | e04f5fc | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 969 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 970 | } |
Chris Lattner | bfef6d7 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 971 | break; |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 972 | case Type::ObjCObjectPointer: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 973 | Width = Target->getPointerWidth(0); |
| 974 | Align = Target->getPointerAlign(0); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 975 | break; |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 976 | case Type::BlockPointer: { |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 977 | unsigned AS = getTargetAddressSpace( |
| 978 | cast<BlockPointerType>(T)->getPointeeType()); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 979 | Width = Target->getPointerWidth(AS); |
| 980 | Align = Target->getPointerAlign(AS); |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 981 | break; |
| 982 | } |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 983 | case Type::LValueReference: |
| 984 | case Type::RValueReference: { |
| 985 | // alignof and sizeof should never enter this code path here, so we go |
| 986 | // the pointer route. |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 987 | unsigned AS = getTargetAddressSpace( |
| 988 | cast<ReferenceType>(T)->getPointeeType()); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 989 | Width = Target->getPointerWidth(AS); |
| 990 | Align = Target->getPointerAlign(AS); |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 991 | break; |
| 992 | } |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 993 | case Type::Pointer: { |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 994 | unsigned AS = getTargetAddressSpace(cast<PointerType>(T)->getPointeeType()); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 995 | Width = Target->getPointerWidth(AS); |
| 996 | Align = Target->getPointerAlign(AS); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 997 | break; |
| 998 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 999 | case Type::MemberPointer: { |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 1000 | const MemberPointerType *MPT = cast<MemberPointerType>(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1001 | std::pair<uint64_t, unsigned> PtrDiffInfo = |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 1002 | getTypeInfo(getPointerDiffType()); |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 1003 | Width = PtrDiffInfo.first * ABI->getMemberPointerSize(MPT); |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 1004 | Align = PtrDiffInfo.second; |
| 1005 | break; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1006 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1007 | case Type::Complex: { |
| 1008 | // Complex types have the same alignment as their elements, but twice the |
| 1009 | // size. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1010 | std::pair<uint64_t, unsigned> EltInfo = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1011 | getTypeInfo(cast<ComplexType>(T)->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1012 | Width = EltInfo.first*2; |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1013 | Align = EltInfo.second; |
| 1014 | break; |
| 1015 | } |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1016 | case Type::ObjCObject: |
| 1017 | return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr()); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1018 | case Type::ObjCInterface: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1019 | const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1020 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1021 | Width = toBits(Layout.getSize()); |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 1022 | Align = toBits(Layout.getAlignment()); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1023 | break; |
| 1024 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1025 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1026 | case Type::Enum: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1027 | const TagType *TT = cast<TagType>(T); |
| 1028 | |
| 1029 | if (TT->getDecl()->isInvalidDecl()) { |
Douglas Gregor | 22ce41d | 2011-04-20 17:29:44 +0000 | [diff] [blame] | 1030 | Width = 8; |
| 1031 | Align = 8; |
Chris Lattner | 8389eab | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 1032 | break; |
| 1033 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1034 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1035 | if (const EnumType *ET = dyn_cast<EnumType>(TT)) |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1036 | return getTypeInfo(ET->getDecl()->getIntegerType()); |
| 1037 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1038 | const RecordType *RT = cast<RecordType>(TT); |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1039 | const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1040 | Width = toBits(Layout.getSize()); |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 1041 | Align = toBits(Layout.getAlignment()); |
Chris Lattner | dc0d73e | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 1042 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1043 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1044 | |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1045 | case Type::SubstTemplateTypeParm: |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1046 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> |
| 1047 | getReplacementType().getTypePtr()); |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1048 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1049 | case Type::Auto: { |
| 1050 | const AutoType *A = cast<AutoType>(T); |
| 1051 | assert(A->isDeduced() && "Cannot request the size of a dependent type"); |
Matt Beaumont-Gay | dc856af | 2011-02-22 20:00:16 +0000 | [diff] [blame] | 1052 | return getTypeInfo(A->getDeducedType().getTypePtr()); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1055 | case Type::Paren: |
| 1056 | return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr()); |
| 1057 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1058 | case Type::Typedef: { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1059 | const TypedefNameDecl *Typedef = cast<TypedefType>(T)->getDecl(); |
Douglas Gregor | df1367a | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 1060 | std::pair<uint64_t, unsigned> Info |
| 1061 | = getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); |
Chris Lattner | c1de52d | 2011-02-19 22:55:41 +0000 | [diff] [blame] | 1062 | // If the typedef has an aligned attribute on it, it overrides any computed |
| 1063 | // alignment we have. This violates the GCC documentation (which says that |
| 1064 | // attribute(aligned) can only round up) but matches its implementation. |
| 1065 | if (unsigned AttrAlign = Typedef->getMaxAlignment()) |
| 1066 | Align = AttrAlign; |
| 1067 | else |
| 1068 | Align = Info.second; |
Douglas Gregor | df1367a | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 1069 | Width = Info.first; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1070 | break; |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1071 | } |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1072 | |
| 1073 | case Type::TypeOfExpr: |
| 1074 | return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType() |
| 1075 | .getTypePtr()); |
| 1076 | |
| 1077 | case Type::TypeOf: |
| 1078 | return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr()); |
| 1079 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 1080 | case Type::Decltype: |
| 1081 | return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType() |
| 1082 | .getTypePtr()); |
| 1083 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1084 | case Type::UnaryTransform: |
| 1085 | return getTypeInfo(cast<UnaryTransformType>(T)->getUnderlyingType()); |
| 1086 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1087 | case Type::Elaborated: |
| 1088 | return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1089 | |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 1090 | case Type::Attributed: |
| 1091 | return getTypeInfo( |
| 1092 | cast<AttributedType>(T)->getEquivalentType().getTypePtr()); |
| 1093 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1094 | case Type::TemplateSpecialization: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1095 | assert(getCanonicalType(T) != T && |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1096 | "Cannot request the size of a dependent type"); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1097 | const TemplateSpecializationType *TST = cast<TemplateSpecializationType>(T); |
| 1098 | // A type alias template specialization may refer to a typedef with the |
| 1099 | // aligned attribute on it. |
| 1100 | if (TST->isTypeAlias()) |
| 1101 | return getTypeInfo(TST->getAliasedType().getTypePtr()); |
| 1102 | else |
| 1103 | return getTypeInfo(getCanonicalType(T)); |
| 1104 | } |
| 1105 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1106 | case Type::Atomic: { |
Eli Friedman | 2be4607 | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 1107 | std::pair<uint64_t, unsigned> Info |
| 1108 | = getTypeInfo(cast<AtomicType>(T)->getValueType()); |
| 1109 | Width = Info.first; |
| 1110 | Align = Info.second; |
| 1111 | if (Width != 0 && Width <= Target->getMaxAtomicPromoteWidth() && |
| 1112 | llvm::isPowerOf2_64(Width)) { |
| 1113 | // We can potentially perform lock-free atomic operations for this |
| 1114 | // type; promote the alignment appropriately. |
| 1115 | // FIXME: We could potentially promote the width here as well... |
| 1116 | // is that worthwhile? (Non-struct atomic types generally have |
| 1117 | // power-of-two size anyway, but structs might not. Requires a bit |
| 1118 | // of implementation work to make sure we zero out the extra bits.) |
| 1119 | Align = static_cast<unsigned>(Width); |
| 1120 | } |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1123 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1124 | |
Eli Friedman | 2be4607 | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 1125 | assert(llvm::isPowerOf2_32(Align) && "Alignment must be power of 2"); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1126 | return std::make_pair(Width, Align); |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1129 | /// toCharUnitsFromBits - Convert a size in bits to a size in characters. |
| 1130 | CharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const { |
| 1131 | return CharUnits::fromQuantity(BitSize / getCharWidth()); |
| 1132 | } |
| 1133 | |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1134 | /// toBits - Convert a size in characters to a size in characters. |
| 1135 | int64_t ASTContext::toBits(CharUnits CharSize) const { |
| 1136 | return CharSize.getQuantity() * getCharWidth(); |
| 1137 | } |
| 1138 | |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1139 | /// getTypeSizeInChars - Return the size of the specified type, in characters. |
| 1140 | /// This method does not work on incomplete types. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1141 | CharUnits ASTContext::getTypeSizeInChars(QualType T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1142 | return toCharUnitsFromBits(getTypeSize(T)); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1143 | } |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1144 | CharUnits ASTContext::getTypeSizeInChars(const Type *T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1145 | return toCharUnitsFromBits(getTypeSize(T)); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
Ken Dyck | 16e20cc | 2010-01-26 17:25:18 +0000 | [diff] [blame] | 1148 | /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1149 | /// characters. This method does not work on incomplete types. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1150 | CharUnits ASTContext::getTypeAlignInChars(QualType T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1151 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1152 | } |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1153 | CharUnits ASTContext::getTypeAlignInChars(const Type *T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1154 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1157 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 1158 | /// type for the current target in bits. This can be different than the ABI |
| 1159 | /// alignment in cases where it is beneficial for performance to overalign |
| 1160 | /// a data type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1161 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) const { |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1162 | unsigned ABIAlign = getTypeAlign(T); |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1163 | |
| 1164 | // Double and long long should be naturally aligned if possible. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1165 | if (const ComplexType* CT = T->getAs<ComplexType>()) |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1166 | T = CT->getElementType().getTypePtr(); |
| 1167 | if (T->isSpecificBuiltinType(BuiltinType::Double) || |
Chad Rosier | cde7a1d | 2012-03-21 20:20:47 +0000 | [diff] [blame] | 1168 | T->isSpecificBuiltinType(BuiltinType::LongLong) || |
| 1169 | T->isSpecificBuiltinType(BuiltinType::ULongLong)) |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1170 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); |
| 1171 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1172 | return ABIAlign; |
| 1173 | } |
| 1174 | |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1175 | /// DeepCollectObjCIvars - |
| 1176 | /// This routine first collects all declared, but not synthesized, ivars in |
| 1177 | /// super class and then collects all ivars, including those synthesized for |
| 1178 | /// current class. This routine is used for implementation of current class |
| 1179 | /// when all ivars, declared and synthesized are known. |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 1180 | /// |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1181 | void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 1182 | bool leafClass, |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 1183 | SmallVectorImpl<const ObjCIvarDecl*> &Ivars) const { |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1184 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
| 1185 | DeepCollectObjCIvars(SuperClass, false, Ivars); |
| 1186 | if (!leafClass) { |
| 1187 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 1188 | E = OI->ivar_end(); I != E; ++I) |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 1189 | Ivars.push_back(*I); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 1190 | } else { |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 1191 | ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI); |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 1192 | for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv; |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 1193 | Iv= Iv->getNextIvar()) |
| 1194 | Ivars.push_back(Iv); |
| 1195 | } |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1198 | /// CollectInheritedProtocols - Collect all protocols in current class and |
| 1199 | /// those inherited by it. |
| 1200 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 1201 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1202 | if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1203 | // We can use protocol_iterator here instead of |
| 1204 | // all_referenced_protocol_iterator since we are walking all categories. |
| 1205 | for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(), |
| 1206 | PE = OI->all_referenced_protocol_end(); P != PE; ++P) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1207 | ObjCProtocolDecl *Proto = (*P); |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1208 | Protocols.insert(Proto->getCanonicalDecl()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1209 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 1210 | PE = Proto->protocol_end(); P != PE; ++P) { |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1211 | Protocols.insert((*P)->getCanonicalDecl()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1212 | CollectInheritedProtocols(*P, Protocols); |
| 1213 | } |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 1214 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1215 | |
| 1216 | // Categories of this Interface. |
| 1217 | for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList(); |
| 1218 | CDeclChain; CDeclChain = CDeclChain->getNextClassCategory()) |
| 1219 | CollectInheritedProtocols(CDeclChain, Protocols); |
| 1220 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) |
| 1221 | while (SD) { |
| 1222 | CollectInheritedProtocols(SD, Protocols); |
| 1223 | SD = SD->getSuperClass(); |
| 1224 | } |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1225 | } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1226 | for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(), |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1227 | PE = OC->protocol_end(); P != PE; ++P) { |
| 1228 | ObjCProtocolDecl *Proto = (*P); |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1229 | Protocols.insert(Proto->getCanonicalDecl()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1230 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 1231 | PE = Proto->protocol_end(); P != PE; ++P) |
| 1232 | CollectInheritedProtocols(*P, Protocols); |
| 1233 | } |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1234 | } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1235 | for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(), |
| 1236 | PE = OP->protocol_end(); P != PE; ++P) { |
| 1237 | ObjCProtocolDecl *Proto = (*P); |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1238 | Protocols.insert(Proto->getCanonicalDecl()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1239 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 1240 | PE = Proto->protocol_end(); P != PE; ++P) |
| 1241 | CollectInheritedProtocols(*P, Protocols); |
| 1242 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1243 | } |
| 1244 | } |
| 1245 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1246 | unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const { |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1247 | unsigned count = 0; |
| 1248 | // Count ivars declared in class extension. |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 1249 | for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl; |
| 1250 | CDecl = CDecl->getNextClassExtension()) |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1251 | count += CDecl->ivar_size(); |
| 1252 | |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1253 | // Count ivar defined in this class's implementation. This |
| 1254 | // includes synthesized ivars. |
| 1255 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1256 | count += ImplDecl->ivar_size(); |
| 1257 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 1258 | return count; |
| 1259 | } |
| 1260 | |
Argyrios Kyrtzidis | 8deabc1 | 2012-02-03 05:58:16 +0000 | [diff] [blame] | 1261 | bool ASTContext::isSentinelNullExpr(const Expr *E) { |
| 1262 | if (!E) |
| 1263 | return false; |
| 1264 | |
| 1265 | // nullptr_t is always treated as null. |
| 1266 | if (E->getType()->isNullPtrType()) return true; |
| 1267 | |
| 1268 | if (E->getType()->isAnyPointerType() && |
| 1269 | E->IgnoreParenCasts()->isNullPointerConstant(*this, |
| 1270 | Expr::NPC_ValueDependentIsNull)) |
| 1271 | return true; |
| 1272 | |
| 1273 | // Unfortunately, __null has type 'int'. |
| 1274 | if (isa<GNUNullExpr>(E)) return true; |
| 1275 | |
| 1276 | return false; |
| 1277 | } |
| 1278 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1279 | /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. |
| 1280 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
| 1281 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1282 | I = ObjCImpls.find(D); |
| 1283 | if (I != ObjCImpls.end()) |
| 1284 | return cast<ObjCImplementationDecl>(I->second); |
| 1285 | return 0; |
| 1286 | } |
| 1287 | /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. |
| 1288 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
| 1289 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1290 | I = ObjCImpls.find(D); |
| 1291 | if (I != ObjCImpls.end()) |
| 1292 | return cast<ObjCCategoryImplDecl>(I->second); |
| 1293 | return 0; |
| 1294 | } |
| 1295 | |
| 1296 | /// \brief Set the implementation of ObjCInterfaceDecl. |
| 1297 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
| 1298 | ObjCImplementationDecl *ImplD) { |
| 1299 | assert(IFaceD && ImplD && "Passed null params"); |
| 1300 | ObjCImpls[IFaceD] = ImplD; |
| 1301 | } |
| 1302 | /// \brief Set the implementation of ObjCCategoryDecl. |
| 1303 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
| 1304 | ObjCCategoryImplDecl *ImplD) { |
| 1305 | assert(CatD && ImplD && "Passed null params"); |
| 1306 | ObjCImpls[CatD] = ImplD; |
| 1307 | } |
| 1308 | |
Argyrios Kyrtzidis | 87ec9c2 | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 1309 | ObjCInterfaceDecl *ASTContext::getObjContainingInterface(NamedDecl *ND) const { |
| 1310 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ND->getDeclContext())) |
| 1311 | return ID; |
| 1312 | if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(ND->getDeclContext())) |
| 1313 | return CD->getClassInterface(); |
| 1314 | if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(ND->getDeclContext())) |
| 1315 | return IMD->getClassInterface(); |
| 1316 | |
| 1317 | return 0; |
| 1318 | } |
| 1319 | |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1320 | /// \brief Get the copy initialization expression of VarDecl,or NULL if |
| 1321 | /// none exists. |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1322 | Expr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) { |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1323 | assert(VD && "Passed null params"); |
| 1324 | assert(VD->hasAttr<BlocksAttr>() && |
| 1325 | "getBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1326 | llvm::DenseMap<const VarDecl*, Expr*>::iterator |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1327 | I = BlockVarCopyInits.find(VD); |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1328 | return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0; |
| 1329 | } |
| 1330 | |
| 1331 | /// \brief Set the copy inialization expression of a block var decl. |
| 1332 | void ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) { |
| 1333 | assert(VD && Init && "Passed null params"); |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1334 | assert(VD->hasAttr<BlocksAttr>() && |
| 1335 | "setBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1336 | BlockVarCopyInits[VD] = Init; |
| 1337 | } |
| 1338 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1339 | /// \brief Allocate an uninitialized TypeSourceInfo. |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1340 | /// |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1341 | /// The caller should initialize the memory held by TypeSourceInfo using |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1342 | /// the TypeLoc wrappers. |
| 1343 | /// |
| 1344 | /// \param T the type that will be the basis for type source info. This type |
| 1345 | /// should refer to how the declarator was written in source code, not to |
| 1346 | /// what type semantic analysis resolved the declarator to. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1347 | TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1348 | unsigned DataSize) const { |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1349 | if (!DataSize) |
| 1350 | DataSize = TypeLoc::getFullDataSizeForType(T); |
| 1351 | else |
| 1352 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1353 | "incorrect data size provided to CreateTypeSourceInfo!"); |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1354 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1355 | TypeSourceInfo *TInfo = |
| 1356 | (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8); |
| 1357 | new (TInfo) TypeSourceInfo(T); |
| 1358 | return TInfo; |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1361 | TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1362 | SourceLocation L) const { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1363 | TypeSourceInfo *DI = CreateTypeSourceInfo(T); |
Douglas Gregor | c21c7e9 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 1364 | DI->getTypeLoc().initialize(const_cast<ASTContext &>(*this), L); |
John McCall | a4eb74d | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 1365 | return DI; |
| 1366 | } |
| 1367 | |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1368 | const ASTRecordLayout & |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1369 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const { |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1370 | return getObjCLayout(D, 0); |
| 1371 | } |
| 1372 | |
| 1373 | const ASTRecordLayout & |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1374 | ASTContext::getASTObjCImplementationLayout( |
| 1375 | const ObjCImplementationDecl *D) const { |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1376 | return getObjCLayout(D->getClassInterface(), D); |
| 1377 | } |
| 1378 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1379 | //===----------------------------------------------------------------------===// |
| 1380 | // Type creation/memoization methods |
| 1381 | //===----------------------------------------------------------------------===// |
| 1382 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1383 | QualType |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1384 | ASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const { |
| 1385 | unsigned fastQuals = quals.getFastQualifiers(); |
| 1386 | quals.removeFastQualifiers(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1387 | |
| 1388 | // Check if we've already instantiated this type. |
| 1389 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1390 | ExtQuals::Profile(ID, baseType, quals); |
| 1391 | void *insertPos = 0; |
| 1392 | if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, insertPos)) { |
| 1393 | assert(eq->getQualifiers() == quals); |
| 1394 | return QualType(eq, fastQuals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1397 | // If the base type is not canonical, make the appropriate canonical type. |
| 1398 | QualType canon; |
| 1399 | if (!baseType->isCanonicalUnqualified()) { |
| 1400 | SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1401 | canonSplit.Quals.addConsistentQualifiers(quals); |
| 1402 | canon = getExtQualType(canonSplit.Ty, canonSplit.Quals); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1403 | |
| 1404 | // Re-find the insert position. |
| 1405 | (void) ExtQualNodes.FindNodeOrInsertPos(ID, insertPos); |
| 1406 | } |
| 1407 | |
| 1408 | ExtQuals *eq = new (*this, TypeAlignment) ExtQuals(baseType, canon, quals); |
| 1409 | ExtQualNodes.InsertNode(eq, insertPos); |
| 1410 | return QualType(eq, fastQuals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1413 | QualType |
| 1414 | ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) const { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1415 | QualType CanT = getCanonicalType(T); |
| 1416 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1417 | return T; |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +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 address space specified, it cannot get |
| 1425 | // another one. |
| 1426 | assert(!Quals.hasAddressSpace() && |
| 1427 | "Type cannot be in multiple addr spaces!"); |
| 1428 | Quals.addAddressSpace(AddressSpace); |
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); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1433 | QualType ASTContext::getObjCGCQualType(QualType T, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1434 | Qualifiers::GC GCAttr) const { |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1435 | QualType CanT = getCanonicalType(T); |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1436 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1437 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1438 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1439 | if (const PointerType *ptr = T->getAs<PointerType>()) { |
| 1440 | QualType Pointee = ptr->getPointeeType(); |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 1441 | if (Pointee->isAnyPointerType()) { |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1442 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 1443 | return getPointerType(ResultType); |
| 1444 | } |
| 1445 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1446 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1447 | // If we are composing extended qualifiers together, merge together |
| 1448 | // into one ExtQuals node. |
| 1449 | QualifierCollector Quals; |
| 1450 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1451 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1452 | // If this type already has an ObjCGC specified, it cannot get |
| 1453 | // another one. |
| 1454 | assert(!Quals.hasObjCGCAttr() && |
| 1455 | "Type cannot have multiple ObjCGCs!"); |
| 1456 | Quals.addObjCGCAttr(GCAttr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1457 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1458 | return getExtQualType(TypeNode, Quals); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1459 | } |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1460 | |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 1461 | const FunctionType *ASTContext::adjustFunctionType(const FunctionType *T, |
| 1462 | FunctionType::ExtInfo Info) { |
| 1463 | if (T->getExtInfo() == Info) |
| 1464 | return T; |
| 1465 | |
| 1466 | QualType Result; |
| 1467 | if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) { |
| 1468 | Result = getFunctionNoProtoType(FNPT->getResultType(), Info); |
| 1469 | } else { |
| 1470 | const FunctionProtoType *FPT = cast<FunctionProtoType>(T); |
| 1471 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 1472 | EPI.ExtInfo = Info; |
| 1473 | Result = getFunctionType(FPT->getResultType(), FPT->arg_type_begin(), |
| 1474 | FPT->getNumArgs(), EPI); |
| 1475 | } |
| 1476 | |
| 1477 | return cast<FunctionType>(Result.getTypePtr()); |
| 1478 | } |
| 1479 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1480 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 1481 | /// number with the specified element type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1482 | QualType ASTContext::getComplexType(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 | ComplexType::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 (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1490 | return QualType(CT, 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 = getComplexType(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 | ComplexType *NewIP = ComplexTypes.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 | ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1503 | Types.push_back(New); |
| 1504 | ComplexTypes.InsertNode(New, InsertPos); |
| 1505 | return QualType(New, 0); |
| 1506 | } |
| 1507 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1508 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 1509 | /// the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1510 | QualType ASTContext::getPointerType(QualType T) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1511 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1512 | // structure. |
| 1513 | llvm::FoldingSetNodeID ID; |
| 1514 | PointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1515 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1516 | void *InsertPos = 0; |
| 1517 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1518 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1519 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1520 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1521 | // so fill in the canonical type field. |
| 1522 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1523 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1524 | Canonical = getPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1525 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1526 | // Get the new insert position for the node we care about. |
| 1527 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1528 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1529 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1530 | PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1531 | Types.push_back(New); |
| 1532 | PointerTypes.InsertNode(New, InsertPos); |
| 1533 | return QualType(New, 0); |
| 1534 | } |
| 1535 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1536 | /// getBlockPointerType - Return the uniqued reference to the type for |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1537 | /// a pointer to the specified block. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1538 | QualType ASTContext::getBlockPointerType(QualType T) const { |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1539 | assert(T->isFunctionType() && "block of function types only"); |
| 1540 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1541 | // structure. |
| 1542 | llvm::FoldingSetNodeID ID; |
| 1543 | BlockPointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1544 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1545 | void *InsertPos = 0; |
| 1546 | if (BlockPointerType *PT = |
| 1547 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1548 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1549 | |
| 1550 | // 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] | 1551 | // type either so fill in the canonical type field. |
| 1552 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1553 | if (!T.isCanonical()) { |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1554 | Canonical = getBlockPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1555 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1556 | // Get the new insert position for the node we care about. |
| 1557 | BlockPointerType *NewIP = |
| 1558 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1559 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1560 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1561 | BlockPointerType *New |
| 1562 | = new (*this, TypeAlignment) BlockPointerType(T, Canonical); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1563 | Types.push_back(New); |
| 1564 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 1565 | return QualType(New, 0); |
| 1566 | } |
| 1567 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1568 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 1569 | /// lvalue reference to the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1570 | QualType |
| 1571 | ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const { |
Douglas Gregor | 9625e44 | 2011-05-21 22:16:50 +0000 | [diff] [blame] | 1572 | assert(getCanonicalType(T) != OverloadTy && |
| 1573 | "Unresolved overloaded function type"); |
| 1574 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1575 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1576 | // structure. |
| 1577 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1578 | ReferenceType::Profile(ID, T, SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1579 | |
| 1580 | void *InsertPos = 0; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1581 | if (LValueReferenceType *RT = |
| 1582 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1583 | return QualType(RT, 0); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1584 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1585 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1586 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1587 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1588 | // either, so fill in the canonical type field. |
| 1589 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1590 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
| 1591 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1592 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1593 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1594 | // Get the new insert position for the node we care about. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1595 | LValueReferenceType *NewIP = |
| 1596 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1597 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1600 | LValueReferenceType *New |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1601 | = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, |
| 1602 | SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1603 | Types.push_back(New); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1604 | LValueReferenceTypes.InsertNode(New, InsertPos); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1605 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1606 | return QualType(New, 0); |
| 1607 | } |
| 1608 | |
| 1609 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 1610 | /// rvalue reference to the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1611 | QualType ASTContext::getRValueReferenceType(QualType T) const { |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1612 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1613 | // structure. |
| 1614 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1615 | ReferenceType::Profile(ID, T, false); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1616 | |
| 1617 | void *InsertPos = 0; |
| 1618 | if (RValueReferenceType *RT = |
| 1619 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1620 | return QualType(RT, 0); |
| 1621 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1622 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1623 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1624 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1625 | // either, so fill in the canonical type field. |
| 1626 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1627 | if (InnerRef || !T.isCanonical()) { |
| 1628 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1629 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1630 | |
| 1631 | // Get the new insert position for the node we care about. |
| 1632 | RValueReferenceType *NewIP = |
| 1633 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1634 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1637 | RValueReferenceType *New |
| 1638 | = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1639 | Types.push_back(New); |
| 1640 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1641 | return QualType(New, 0); |
| 1642 | } |
| 1643 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1644 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 1645 | /// member pointer to the specified type, in the specified class. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1646 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1647 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1648 | // structure. |
| 1649 | llvm::FoldingSetNodeID ID; |
| 1650 | MemberPointerType::Profile(ID, T, Cls); |
| 1651 | |
| 1652 | void *InsertPos = 0; |
| 1653 | if (MemberPointerType *PT = |
| 1654 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1655 | return QualType(PT, 0); |
| 1656 | |
| 1657 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 1658 | // type either, so fill in the canonical type field. |
| 1659 | QualType Canonical; |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1660 | if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1661 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 1662 | |
| 1663 | // Get the new insert position for the node we care about. |
| 1664 | MemberPointerType *NewIP = |
| 1665 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1666 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1667 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1668 | MemberPointerType *New |
| 1669 | = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1670 | Types.push_back(New); |
| 1671 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 1672 | return QualType(New, 0); |
| 1673 | } |
| 1674 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1675 | /// getConstantArrayType - Return the unique reference to the type for an |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1676 | /// array of the specified element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1677 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1678 | const llvm::APInt &ArySizeIn, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1679 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1680 | unsigned IndexTypeQuals) const { |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1681 | assert((EltTy->isDependentType() || |
| 1682 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && |
Eli Friedman | 587cbdf | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1683 | "Constant array of VLAs is illegal!"); |
| 1684 | |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1685 | // Convert the array size into a canonical width matching the pointer size for |
| 1686 | // the target. |
| 1687 | llvm::APInt ArySize(ArySizeIn); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1688 | ArySize = |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1689 | ArySize.zextOrTrunc(Target->getPointerWidth(getTargetAddressSpace(EltTy))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1690 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1691 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1692 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, IndexTypeQuals); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1693 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1694 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1695 | if (ConstantArrayType *ATP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1696 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1697 | return QualType(ATP, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1698 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1699 | // If the element type isn't canonical or has qualifiers, this won't |
| 1700 | // be a canonical type either, so fill in the canonical type field. |
| 1701 | QualType Canon; |
| 1702 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 1703 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1704 | Canon = getConstantArrayType(QualType(canonSplit.Ty, 0), ArySize, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1705 | ASM, IndexTypeQuals); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1706 | Canon = getQualifiedType(Canon, canonSplit.Quals); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1707 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1708 | // Get the new insert position for the node we care about. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1709 | ConstantArrayType *NewIP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1710 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1711 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1712 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1713 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1714 | ConstantArrayType *New = new(*this,TypeAlignment) |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1715 | ConstantArrayType(EltTy, Canon, ArySize, ASM, IndexTypeQuals); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1716 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1717 | Types.push_back(New); |
| 1718 | return QualType(New, 0); |
| 1719 | } |
| 1720 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1721 | /// getVariableArrayDecayedType - Turns the given type, which may be |
| 1722 | /// variably-modified, into the corresponding type with all the known |
| 1723 | /// sizes replaced with [*]. |
| 1724 | QualType ASTContext::getVariableArrayDecayedType(QualType type) const { |
| 1725 | // Vastly most common case. |
| 1726 | if (!type->isVariablyModifiedType()) return type; |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1727 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1728 | QualType result; |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1729 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1730 | SplitQualType split = type.getSplitDesugaredType(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1731 | const Type *ty = split.Ty; |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1732 | switch (ty->getTypeClass()) { |
| 1733 | #define TYPE(Class, Base) |
| 1734 | #define ABSTRACT_TYPE(Class, Base) |
| 1735 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 1736 | #include "clang/AST/TypeNodes.def" |
| 1737 | llvm_unreachable("didn't desugar past all non-canonical types?"); |
| 1738 | |
| 1739 | // These types should never be variably-modified. |
| 1740 | case Type::Builtin: |
| 1741 | case Type::Complex: |
| 1742 | case Type::Vector: |
| 1743 | case Type::ExtVector: |
| 1744 | case Type::DependentSizedExtVector: |
| 1745 | case Type::ObjCObject: |
| 1746 | case Type::ObjCInterface: |
| 1747 | case Type::ObjCObjectPointer: |
| 1748 | case Type::Record: |
| 1749 | case Type::Enum: |
| 1750 | case Type::UnresolvedUsing: |
| 1751 | case Type::TypeOfExpr: |
| 1752 | case Type::TypeOf: |
| 1753 | case Type::Decltype: |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1754 | case Type::UnaryTransform: |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1755 | case Type::DependentName: |
| 1756 | case Type::InjectedClassName: |
| 1757 | case Type::TemplateSpecialization: |
| 1758 | case Type::DependentTemplateSpecialization: |
| 1759 | case Type::TemplateTypeParm: |
| 1760 | case Type::SubstTemplateTypeParmPack: |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1761 | case Type::Auto: |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1762 | case Type::PackExpansion: |
| 1763 | llvm_unreachable("type should never be variably-modified"); |
| 1764 | |
| 1765 | // These types can be variably-modified but should never need to |
| 1766 | // further decay. |
| 1767 | case Type::FunctionNoProto: |
| 1768 | case Type::FunctionProto: |
| 1769 | case Type::BlockPointer: |
| 1770 | case Type::MemberPointer: |
| 1771 | return type; |
| 1772 | |
| 1773 | // These types can be variably-modified. All these modifications |
| 1774 | // preserve structure except as noted by comments. |
| 1775 | // TODO: if we ever care about optimizing VLAs, there are no-op |
| 1776 | // optimizations available here. |
| 1777 | case Type::Pointer: |
| 1778 | result = getPointerType(getVariableArrayDecayedType( |
| 1779 | cast<PointerType>(ty)->getPointeeType())); |
| 1780 | break; |
| 1781 | |
| 1782 | case Type::LValueReference: { |
| 1783 | const LValueReferenceType *lv = cast<LValueReferenceType>(ty); |
| 1784 | result = getLValueReferenceType( |
| 1785 | getVariableArrayDecayedType(lv->getPointeeType()), |
| 1786 | lv->isSpelledAsLValue()); |
| 1787 | break; |
| 1788 | } |
| 1789 | |
| 1790 | case Type::RValueReference: { |
| 1791 | const RValueReferenceType *lv = cast<RValueReferenceType>(ty); |
| 1792 | result = getRValueReferenceType( |
| 1793 | getVariableArrayDecayedType(lv->getPointeeType())); |
| 1794 | break; |
| 1795 | } |
| 1796 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1797 | case Type::Atomic: { |
| 1798 | const AtomicType *at = cast<AtomicType>(ty); |
| 1799 | result = getAtomicType(getVariableArrayDecayedType(at->getValueType())); |
| 1800 | break; |
| 1801 | } |
| 1802 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1803 | case Type::ConstantArray: { |
| 1804 | const ConstantArrayType *cat = cast<ConstantArrayType>(ty); |
| 1805 | result = getConstantArrayType( |
| 1806 | getVariableArrayDecayedType(cat->getElementType()), |
| 1807 | cat->getSize(), |
| 1808 | cat->getSizeModifier(), |
| 1809 | cat->getIndexTypeCVRQualifiers()); |
| 1810 | break; |
| 1811 | } |
| 1812 | |
| 1813 | case Type::DependentSizedArray: { |
| 1814 | const DependentSizedArrayType *dat = cast<DependentSizedArrayType>(ty); |
| 1815 | result = getDependentSizedArrayType( |
| 1816 | getVariableArrayDecayedType(dat->getElementType()), |
| 1817 | dat->getSizeExpr(), |
| 1818 | dat->getSizeModifier(), |
| 1819 | dat->getIndexTypeCVRQualifiers(), |
| 1820 | dat->getBracketsRange()); |
| 1821 | break; |
| 1822 | } |
| 1823 | |
| 1824 | // Turn incomplete types into [*] types. |
| 1825 | case Type::IncompleteArray: { |
| 1826 | const IncompleteArrayType *iat = cast<IncompleteArrayType>(ty); |
| 1827 | result = getVariableArrayType( |
| 1828 | getVariableArrayDecayedType(iat->getElementType()), |
| 1829 | /*size*/ 0, |
| 1830 | ArrayType::Normal, |
| 1831 | iat->getIndexTypeCVRQualifiers(), |
| 1832 | SourceRange()); |
| 1833 | break; |
| 1834 | } |
| 1835 | |
| 1836 | // Turn VLA types into [*] types. |
| 1837 | case Type::VariableArray: { |
| 1838 | const VariableArrayType *vat = cast<VariableArrayType>(ty); |
| 1839 | result = getVariableArrayType( |
| 1840 | getVariableArrayDecayedType(vat->getElementType()), |
| 1841 | /*size*/ 0, |
| 1842 | ArrayType::Star, |
| 1843 | vat->getIndexTypeCVRQualifiers(), |
| 1844 | vat->getBracketsRange()); |
| 1845 | break; |
| 1846 | } |
| 1847 | } |
| 1848 | |
| 1849 | // Apply the top-level qualifiers from the original. |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1850 | return getQualifiedType(result, split.Quals); |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1851 | } |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1852 | |
Steve Naroff | bdbf7b0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 1853 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 1854 | /// variable array of the specified element type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1855 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 1856 | Expr *NumElts, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1857 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1858 | unsigned IndexTypeQuals, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1859 | SourceRange Brackets) const { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1860 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 1861 | // that have an expression provided for their size. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1862 | QualType Canon; |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 1863 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1864 | // Be sure to pull qualifiers off the element type. |
| 1865 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 1866 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1867 | Canon = getVariableArrayType(QualType(canonSplit.Ty, 0), NumElts, ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1868 | IndexTypeQuals, Brackets); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1869 | Canon = getQualifiedType(Canon, canonSplit.Quals); |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 1870 | } |
| 1871 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1872 | VariableArrayType *New = new(*this, TypeAlignment) |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1873 | VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1874 | |
| 1875 | VariableArrayTypes.push_back(New); |
| 1876 | Types.push_back(New); |
| 1877 | return QualType(New, 0); |
| 1878 | } |
| 1879 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1880 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 1881 | /// the type for a dependently-sized array of the specified element |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1882 | /// type. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1883 | QualType ASTContext::getDependentSizedArrayType(QualType elementType, |
| 1884 | Expr *numElements, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1885 | ArrayType::ArraySizeModifier ASM, |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1886 | unsigned elementTypeQuals, |
| 1887 | SourceRange brackets) const { |
| 1888 | assert((!numElements || numElements->isTypeDependent() || |
| 1889 | numElements->isValueDependent()) && |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1890 | "Size must be type- or value-dependent!"); |
| 1891 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1892 | // Dependently-sized array types that do not have a specified number |
| 1893 | // of elements will have their sizes deduced from a dependent |
| 1894 | // initializer. We do no canonicalization here at all, which is okay |
| 1895 | // because they can't be used in most locations. |
| 1896 | if (!numElements) { |
| 1897 | DependentSizedArrayType *newType |
| 1898 | = new (*this, TypeAlignment) |
| 1899 | DependentSizedArrayType(*this, elementType, QualType(), |
| 1900 | numElements, ASM, elementTypeQuals, |
| 1901 | brackets); |
| 1902 | Types.push_back(newType); |
| 1903 | return QualType(newType, 0); |
| 1904 | } |
| 1905 | |
| 1906 | // Otherwise, we actually build a new type every time, but we |
| 1907 | // also build a canonical type. |
| 1908 | |
| 1909 | SplitQualType canonElementType = getCanonicalType(elementType).split(); |
| 1910 | |
| 1911 | void *insertPos = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1912 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1913 | DependentSizedArrayType::Profile(ID, *this, |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1914 | QualType(canonElementType.Ty, 0), |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1915 | ASM, elementTypeQuals, numElements); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1916 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1917 | // Look for an existing type with these properties. |
| 1918 | DependentSizedArrayType *canonTy = |
| 1919 | DependentSizedArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1920 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1921 | // If we don't have one, build one. |
| 1922 | if (!canonTy) { |
| 1923 | canonTy = new (*this, TypeAlignment) |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1924 | DependentSizedArrayType(*this, QualType(canonElementType.Ty, 0), |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1925 | QualType(), numElements, ASM, elementTypeQuals, |
| 1926 | brackets); |
| 1927 | DependentSizedArrayTypes.InsertNode(canonTy, insertPos); |
| 1928 | Types.push_back(canonTy); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1931 | // Apply qualifiers from the element type to the array. |
| 1932 | QualType canon = getQualifiedType(QualType(canonTy,0), |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1933 | canonElementType.Quals); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1934 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1935 | // If we didn't need extra canonicalization for the element type, |
| 1936 | // then just use that as our result. |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1937 | if (QualType(canonElementType.Ty, 0) == elementType) |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1938 | return canon; |
| 1939 | |
| 1940 | // Otherwise, we need to build a type which follows the spelling |
| 1941 | // of the element type. |
| 1942 | DependentSizedArrayType *sugaredType |
| 1943 | = new (*this, TypeAlignment) |
| 1944 | DependentSizedArrayType(*this, elementType, canon, numElements, |
| 1945 | ASM, elementTypeQuals, brackets); |
| 1946 | Types.push_back(sugaredType); |
| 1947 | return QualType(sugaredType, 0); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1948 | } |
| 1949 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1950 | QualType ASTContext::getIncompleteArrayType(QualType elementType, |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1951 | ArrayType::ArraySizeModifier ASM, |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1952 | unsigned elementTypeQuals) const { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1953 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1954 | IncompleteArrayType::Profile(ID, elementType, ASM, elementTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1955 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1956 | void *insertPos = 0; |
| 1957 | if (IncompleteArrayType *iat = |
| 1958 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos)) |
| 1959 | return QualType(iat, 0); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1960 | |
| 1961 | // 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] | 1962 | // either, so fill in the canonical type field. We also have to pull |
| 1963 | // qualifiers off the element type. |
| 1964 | QualType canon; |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1965 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1966 | if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) { |
| 1967 | SplitQualType canonSplit = getCanonicalType(elementType).split(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1968 | canon = getIncompleteArrayType(QualType(canonSplit.Ty, 0), |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1969 | ASM, elementTypeQuals); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1970 | canon = getQualifiedType(canon, canonSplit.Quals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1971 | |
| 1972 | // Get the new insert position for the node we care about. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1973 | IncompleteArrayType *existing = |
| 1974 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
| 1975 | assert(!existing && "Shouldn't be in the map!"); (void) existing; |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1976 | } |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1977 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1978 | IncompleteArrayType *newType = new (*this, TypeAlignment) |
| 1979 | IncompleteArrayType(elementType, canon, ASM, elementTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1980 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1981 | IncompleteArrayTypes.InsertNode(newType, insertPos); |
| 1982 | Types.push_back(newType); |
| 1983 | return QualType(newType, 0); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1984 | } |
| 1985 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1986 | /// getVectorType - Return the unique reference to a vector type of |
| 1987 | /// 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] | 1988 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1989 | VectorType::VectorKind VecKind) const { |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1990 | assert(vecType->isBuiltinType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1991 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1992 | // Check if we've already instantiated a vector of this type. |
| 1993 | llvm::FoldingSetNodeID ID; |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1994 | VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind); |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1995 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1996 | void *InsertPos = 0; |
| 1997 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1998 | return QualType(VTP, 0); |
| 1999 | |
| 2000 | // If the element type isn't canonical, this won't be a canonical type either, |
| 2001 | // so fill in the canonical type field. |
| 2002 | QualType Canonical; |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 2003 | if (!vecType.isCanonical()) { |
Bob Wilson | 231da7e | 2010-11-16 00:32:20 +0000 | [diff] [blame] | 2004 | Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2005 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2006 | // Get the new insert position for the node we care about. |
| 2007 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2008 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2009 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2010 | VectorType *New = new (*this, TypeAlignment) |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2011 | VectorType(vecType, NumElts, Canonical, VecKind); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2012 | VectorTypes.InsertNode(New, InsertPos); |
| 2013 | Types.push_back(New); |
| 2014 | return QualType(New, 0); |
| 2015 | } |
| 2016 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2017 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2018 | /// 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] | 2019 | QualType |
| 2020 | ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) const { |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 2021 | assert(vecType->isBuiltinType() || vecType->isDependentType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2022 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2023 | // Check if we've already instantiated a vector of this type. |
| 2024 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 2025 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2026 | VectorType::GenericVector); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2027 | void *InsertPos = 0; |
| 2028 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2029 | return QualType(VTP, 0); |
| 2030 | |
| 2031 | // If the element type isn't canonical, this won't be a canonical type either, |
| 2032 | // so fill in the canonical type field. |
| 2033 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2034 | if (!vecType.isCanonical()) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2035 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2036 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2037 | // Get the new insert position for the node we care about. |
| 2038 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2039 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2040 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2041 | ExtVectorType *New = new (*this, TypeAlignment) |
| 2042 | ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2043 | VectorTypes.InsertNode(New, InsertPos); |
| 2044 | Types.push_back(New); |
| 2045 | return QualType(New, 0); |
| 2046 | } |
| 2047 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2048 | QualType |
| 2049 | ASTContext::getDependentSizedExtVectorType(QualType vecType, |
| 2050 | Expr *SizeExpr, |
| 2051 | SourceLocation AttrLoc) const { |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2052 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2053 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2054 | SizeExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2055 | |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2056 | void *InsertPos = 0; |
| 2057 | DependentSizedExtVectorType *Canon |
| 2058 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2059 | DependentSizedExtVectorType *New; |
| 2060 | if (Canon) { |
| 2061 | // We already have a canonical version of this array type; use it as |
| 2062 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2063 | New = new (*this, TypeAlignment) |
| 2064 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), |
| 2065 | SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2066 | } else { |
| 2067 | QualType CanonVecTy = getCanonicalType(vecType); |
| 2068 | if (CanonVecTy == vecType) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2069 | New = new (*this, TypeAlignment) |
| 2070 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, |
| 2071 | AttrLoc); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2072 | |
| 2073 | DependentSizedExtVectorType *CanonCheck |
| 2074 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2075 | assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken"); |
| 2076 | (void)CanonCheck; |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2077 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); |
| 2078 | } else { |
| 2079 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 2080 | SourceLocation()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2081 | New = new (*this, TypeAlignment) |
| 2082 | DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2083 | } |
| 2084 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2085 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 2086 | Types.push_back(New); |
| 2087 | return QualType(New, 0); |
| 2088 | } |
| 2089 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2090 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2091 | /// |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2092 | QualType |
| 2093 | ASTContext::getFunctionNoProtoType(QualType ResultTy, |
| 2094 | const FunctionType::ExtInfo &Info) const { |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2095 | const CallingConv DefaultCC = Info.getCC(); |
| 2096 | const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ? |
| 2097 | CC_X86StdCall : DefaultCC; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2098 | // Unique functions, to guarantee there is only one function of a particular |
| 2099 | // structure. |
| 2100 | llvm::FoldingSetNodeID ID; |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2101 | FunctionNoProtoType::Profile(ID, ResultTy, Info); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2102 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2103 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2104 | if (FunctionNoProtoType *FT = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2105 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2106 | return QualType(FT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2107 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2108 | QualType Canonical; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 2109 | if (!ResultTy.isCanonical() || |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2110 | getCanonicalCallConv(CallConv) != CallConv) { |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2111 | Canonical = |
| 2112 | getFunctionNoProtoType(getCanonicalType(ResultTy), |
| 2113 | Info.withCallingConv(getCanonicalCallConv(CallConv))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2114 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2115 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2116 | FunctionNoProtoType *NewIP = |
| 2117 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2118 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2119 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2120 | |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2121 | FunctionProtoType::ExtInfo newInfo = Info.withCallingConv(CallConv); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2122 | FunctionNoProtoType *New = new (*this, TypeAlignment) |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2123 | FunctionNoProtoType(ResultTy, Canonical, newInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2124 | Types.push_back(New); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2125 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2126 | return QualType(New, 0); |
| 2127 | } |
| 2128 | |
| 2129 | /// getFunctionType - Return a normal function type with a typed argument |
| 2130 | /// list. isVariadic indicates whether the argument list includes '...'. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2131 | QualType |
| 2132 | ASTContext::getFunctionType(QualType ResultTy, |
| 2133 | const QualType *ArgArray, unsigned NumArgs, |
| 2134 | const FunctionProtoType::ExtProtoInfo &EPI) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2135 | // Unique functions, to guarantee there is only one function of a particular |
| 2136 | // structure. |
| 2137 | llvm::FoldingSetNodeID ID; |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2138 | FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, EPI, *this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2139 | |
| 2140 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2141 | if (FunctionProtoType *FTP = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2142 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2143 | return QualType(FTP, 0); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2144 | |
| 2145 | // Determine whether the type being created is already canonical or not. |
Richard Smith | eefb3d5 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2146 | bool isCanonical = |
| 2147 | EPI.ExceptionSpecType == EST_None && ResultTy.isCanonical() && |
| 2148 | !EPI.HasTrailingReturn; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2149 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2150 | if (!ArgArray[i].isCanonicalAsParam()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2151 | isCanonical = false; |
| 2152 | |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2153 | const CallingConv DefaultCC = EPI.ExtInfo.getCC(); |
| 2154 | const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ? |
| 2155 | CC_X86StdCall : DefaultCC; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2156 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2157 | // If this type isn't canonical, get the canonical version of it. |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2158 | // The exception spec is not part of the canonical type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2159 | QualType Canonical; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2160 | if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2161 | SmallVector<QualType, 16> CanonicalArgs; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2162 | CanonicalArgs.reserve(NumArgs); |
| 2163 | for (unsigned i = 0; i != NumArgs; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2164 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2165 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2166 | FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI; |
Richard Smith | eefb3d5 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2167 | CanonicalEPI.HasTrailingReturn = false; |
Sebastian Redl | 8b5b409 | 2011-03-06 10:52:04 +0000 | [diff] [blame] | 2168 | CanonicalEPI.ExceptionSpecType = EST_None; |
| 2169 | CanonicalEPI.NumExceptions = 0; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2170 | CanonicalEPI.ExtInfo |
| 2171 | = CanonicalEPI.ExtInfo.withCallingConv(getCanonicalCallConv(CallConv)); |
| 2172 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2173 | Canonical = getFunctionType(getCanonicalType(ResultTy), |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2174 | CanonicalArgs.data(), NumArgs, |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2175 | CanonicalEPI); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2176 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2177 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2178 | FunctionProtoType *NewIP = |
| 2179 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2180 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2181 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2182 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2183 | // FunctionProtoType objects are allocated with extra bytes after |
| 2184 | // them for three variable size arrays at the end: |
| 2185 | // - parameter types |
| 2186 | // - exception types |
| 2187 | // - consumed-arguments flags |
| 2188 | // Instead of the exception types, there could be a noexcept |
| 2189 | // expression. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2190 | size_t Size = sizeof(FunctionProtoType) + |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2191 | NumArgs * sizeof(QualType); |
| 2192 | if (EPI.ExceptionSpecType == EST_Dynamic) |
| 2193 | Size += EPI.NumExceptions * sizeof(QualType); |
| 2194 | else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) { |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2195 | Size += sizeof(Expr*); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2196 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2197 | if (EPI.ConsumedArguments) |
| 2198 | Size += NumArgs * sizeof(bool); |
| 2199 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2200 | FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment); |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2201 | FunctionProtoType::ExtProtoInfo newEPI = EPI; |
| 2202 | newEPI.ExtInfo = EPI.ExtInfo.withCallingConv(CallConv); |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2203 | new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, Canonical, newEPI); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2204 | Types.push_back(FTP); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2205 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2206 | return QualType(FTP, 0); |
| 2207 | } |
| 2208 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2209 | #ifndef NDEBUG |
| 2210 | static bool NeedsInjectedClassNameType(const RecordDecl *D) { |
| 2211 | if (!isa<CXXRecordDecl>(D)) return false; |
| 2212 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 2213 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) |
| 2214 | return true; |
| 2215 | if (RD->getDescribedClassTemplate() && |
| 2216 | !isa<ClassTemplateSpecializationDecl>(RD)) |
| 2217 | return true; |
| 2218 | return false; |
| 2219 | } |
| 2220 | #endif |
| 2221 | |
| 2222 | /// getInjectedClassNameType - Return the unique reference to the |
| 2223 | /// injected class name type for the specified templated declaration. |
| 2224 | QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2225 | QualType TST) const { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2226 | assert(NeedsInjectedClassNameType(Decl)); |
| 2227 | if (Decl->TypeForDecl) { |
| 2228 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2229 | } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDecl()) { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2230 | assert(PrevDecl->TypeForDecl && "previous declaration has no type"); |
| 2231 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 2232 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
| 2233 | } else { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2234 | Type *newType = |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2235 | new (*this, TypeAlignment) InjectedClassNameType(Decl, TST); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2236 | Decl->TypeForDecl = newType; |
| 2237 | Types.push_back(newType); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2238 | } |
| 2239 | return QualType(Decl->TypeForDecl, 0); |
| 2240 | } |
| 2241 | |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2242 | /// getTypeDeclType - Return the unique reference to the type for the |
| 2243 | /// specified type declaration. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2244 | QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const { |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 2245 | assert(Decl && "Passed null for Decl param"); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2246 | assert(!Decl->TypeForDecl && "TypeForDecl present in slow case"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2247 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2248 | if (const TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2249 | return getTypedefType(Typedef); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2250 | |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2251 | assert(!isa<TemplateTypeParmDecl>(Decl) && |
| 2252 | "Template type parameter types are always available."); |
| 2253 | |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2254 | if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2255 | assert(!Record->getPreviousDecl() && |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2256 | "struct/union has previous declaration"); |
| 2257 | assert(!NeedsInjectedClassNameType(Record)); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2258 | return getRecordType(Record); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2259 | } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2260 | assert(!Enum->getPreviousDecl() && |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2261 | "enum has previous declaration"); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2262 | return getEnumType(Enum); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2263 | } else if (const UnresolvedUsingTypenameDecl *Using = |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2264 | dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2265 | Type *newType = new (*this, TypeAlignment) UnresolvedUsingType(Using); |
| 2266 | Decl->TypeForDecl = newType; |
| 2267 | Types.push_back(newType); |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 2268 | } else |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2269 | llvm_unreachable("TypeDecl without a type?"); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 2270 | |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 2271 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2272 | } |
| 2273 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2274 | /// getTypedefType - Return the unique reference to the type for the |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2275 | /// specified typedef name decl. |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2276 | QualType |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2277 | ASTContext::getTypedefType(const TypedefNameDecl *Decl, |
| 2278 | QualType Canonical) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2279 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2280 | |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2281 | if (Canonical.isNull()) |
| 2282 | Canonical = getCanonicalType(Decl->getUnderlyingType()); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2283 | TypedefType *newType = new(*this, TypeAlignment) |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2284 | TypedefType(Type::Typedef, Decl, Canonical); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2285 | Decl->TypeForDecl = newType; |
| 2286 | Types.push_back(newType); |
| 2287 | return QualType(newType, 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2288 | } |
| 2289 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2290 | QualType ASTContext::getRecordType(const RecordDecl *Decl) const { |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2291 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 2292 | |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2293 | if (const RecordDecl *PrevDecl = Decl->getPreviousDecl()) |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2294 | if (PrevDecl->TypeForDecl) |
| 2295 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 2296 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2297 | RecordType *newType = new (*this, TypeAlignment) RecordType(Decl); |
| 2298 | Decl->TypeForDecl = newType; |
| 2299 | Types.push_back(newType); |
| 2300 | return QualType(newType, 0); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2301 | } |
| 2302 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2303 | QualType ASTContext::getEnumType(const EnumDecl *Decl) const { |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2304 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 2305 | |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2306 | if (const EnumDecl *PrevDecl = Decl->getPreviousDecl()) |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2307 | if (PrevDecl->TypeForDecl) |
| 2308 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 2309 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2310 | EnumType *newType = new (*this, TypeAlignment) EnumType(Decl); |
| 2311 | Decl->TypeForDecl = newType; |
| 2312 | Types.push_back(newType); |
| 2313 | return QualType(newType, 0); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2314 | } |
| 2315 | |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 2316 | QualType ASTContext::getAttributedType(AttributedType::Kind attrKind, |
| 2317 | QualType modifiedType, |
| 2318 | QualType equivalentType) { |
| 2319 | llvm::FoldingSetNodeID id; |
| 2320 | AttributedType::Profile(id, attrKind, modifiedType, equivalentType); |
| 2321 | |
| 2322 | void *insertPos = 0; |
| 2323 | AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos); |
| 2324 | if (type) return QualType(type, 0); |
| 2325 | |
| 2326 | QualType canon = getCanonicalType(equivalentType); |
| 2327 | type = new (*this, TypeAlignment) |
| 2328 | AttributedType(canon, attrKind, modifiedType, equivalentType); |
| 2329 | |
| 2330 | Types.push_back(type); |
| 2331 | AttributedTypes.InsertNode(type, insertPos); |
| 2332 | |
| 2333 | return QualType(type, 0); |
| 2334 | } |
| 2335 | |
| 2336 | |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2337 | /// \brief Retrieve a substitution-result type. |
| 2338 | QualType |
| 2339 | ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2340 | QualType Replacement) const { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2341 | assert(Replacement.isCanonical() |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2342 | && "replacement types must always be canonical"); |
| 2343 | |
| 2344 | llvm::FoldingSetNodeID ID; |
| 2345 | SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); |
| 2346 | void *InsertPos = 0; |
| 2347 | SubstTemplateTypeParmType *SubstParm |
| 2348 | = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2349 | |
| 2350 | if (!SubstParm) { |
| 2351 | SubstParm = new (*this, TypeAlignment) |
| 2352 | SubstTemplateTypeParmType(Parm, Replacement); |
| 2353 | Types.push_back(SubstParm); |
| 2354 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 2355 | } |
| 2356 | |
| 2357 | return QualType(SubstParm, 0); |
| 2358 | } |
| 2359 | |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 2360 | /// \brief Retrieve a |
| 2361 | QualType ASTContext::getSubstTemplateTypeParmPackType( |
| 2362 | const TemplateTypeParmType *Parm, |
| 2363 | const TemplateArgument &ArgPack) { |
| 2364 | #ifndef NDEBUG |
| 2365 | for (TemplateArgument::pack_iterator P = ArgPack.pack_begin(), |
| 2366 | PEnd = ArgPack.pack_end(); |
| 2367 | P != PEnd; ++P) { |
| 2368 | assert(P->getKind() == TemplateArgument::Type &&"Pack contains a non-type"); |
| 2369 | assert(P->getAsType().isCanonical() && "Pack contains non-canonical type"); |
| 2370 | } |
| 2371 | #endif |
| 2372 | |
| 2373 | llvm::FoldingSetNodeID ID; |
| 2374 | SubstTemplateTypeParmPackType::Profile(ID, Parm, ArgPack); |
| 2375 | void *InsertPos = 0; |
| 2376 | if (SubstTemplateTypeParmPackType *SubstParm |
| 2377 | = SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2378 | return QualType(SubstParm, 0); |
| 2379 | |
| 2380 | QualType Canon; |
| 2381 | if (!Parm->isCanonicalUnqualified()) { |
| 2382 | Canon = getCanonicalType(QualType(Parm, 0)); |
| 2383 | Canon = getSubstTemplateTypeParmPackType(cast<TemplateTypeParmType>(Canon), |
| 2384 | ArgPack); |
| 2385 | SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2386 | } |
| 2387 | |
| 2388 | SubstTemplateTypeParmPackType *SubstParm |
| 2389 | = new (*this, TypeAlignment) SubstTemplateTypeParmPackType(Parm, Canon, |
| 2390 | ArgPack); |
| 2391 | Types.push_back(SubstParm); |
| 2392 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 2393 | return QualType(SubstParm, 0); |
| 2394 | } |
| 2395 | |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2396 | /// \brief Retrieve the template type parameter type for a template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2397 | /// parameter or parameter pack with the given depth, index, and (optionally) |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2398 | /// name. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2399 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2400 | bool ParameterPack, |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2401 | TemplateTypeParmDecl *TTPDecl) const { |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2402 | llvm::FoldingSetNodeID ID; |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2403 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, TTPDecl); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2404 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2405 | TemplateTypeParmType *TypeParm |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2406 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2407 | |
| 2408 | if (TypeParm) |
| 2409 | return QualType(TypeParm, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2410 | |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2411 | if (TTPDecl) { |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2412 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2413 | TypeParm = new (*this, TypeAlignment) TemplateTypeParmType(TTPDecl, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2414 | |
| 2415 | TemplateTypeParmType *TypeCheck |
| 2416 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2417 | assert(!TypeCheck && "Template type parameter canonical type broken"); |
| 2418 | (void)TypeCheck; |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2419 | } else |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2420 | TypeParm = new (*this, TypeAlignment) |
| 2421 | TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2422 | |
| 2423 | Types.push_back(TypeParm); |
| 2424 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 2425 | |
| 2426 | return QualType(TypeParm, 0); |
| 2427 | } |
| 2428 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2429 | TypeSourceInfo * |
| 2430 | ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name, |
| 2431 | SourceLocation NameLoc, |
| 2432 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2433 | QualType Underlying) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2434 | assert(!Name.getAsDependentTemplateName() && |
| 2435 | "No dependent template names here!"); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2436 | QualType TST = getTemplateSpecializationType(Name, Args, Underlying); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2437 | |
| 2438 | TypeSourceInfo *DI = CreateTypeSourceInfo(TST); |
| 2439 | TemplateSpecializationTypeLoc TL |
| 2440 | = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc()); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2441 | TL.setTemplateKeywordLoc(SourceLocation()); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2442 | TL.setTemplateNameLoc(NameLoc); |
| 2443 | TL.setLAngleLoc(Args.getLAngleLoc()); |
| 2444 | TL.setRAngleLoc(Args.getRAngleLoc()); |
| 2445 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 2446 | TL.setArgLocInfo(i, Args[i].getLocInfo()); |
| 2447 | return DI; |
| 2448 | } |
| 2449 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2450 | QualType |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2451 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2452 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2453 | QualType Underlying) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2454 | assert(!Template.getAsDependentTemplateName() && |
| 2455 | "No dependent template names here!"); |
| 2456 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2457 | unsigned NumArgs = Args.size(); |
| 2458 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2459 | SmallVector<TemplateArgument, 4> ArgVec; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2460 | ArgVec.reserve(NumArgs); |
| 2461 | for (unsigned i = 0; i != NumArgs; ++i) |
| 2462 | ArgVec.push_back(Args[i].getArgument()); |
| 2463 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2464 | return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2465 | Underlying); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2466 | } |
| 2467 | |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2468 | #ifndef NDEBUG |
| 2469 | static bool hasAnyPackExpansions(const TemplateArgument *Args, |
| 2470 | unsigned NumArgs) { |
| 2471 | for (unsigned I = 0; I != NumArgs; ++I) |
| 2472 | if (Args[I].isPackExpansion()) |
| 2473 | return true; |
| 2474 | |
| 2475 | return true; |
| 2476 | } |
| 2477 | #endif |
| 2478 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2479 | QualType |
| 2480 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2481 | const TemplateArgument *Args, |
| 2482 | unsigned NumArgs, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2483 | QualType Underlying) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2484 | assert(!Template.getAsDependentTemplateName() && |
| 2485 | "No dependent template names here!"); |
Douglas Gregor | 0f0ea2a | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 2486 | // Look through qualified template names. |
| 2487 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 2488 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2489 | |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2490 | bool IsTypeAlias = |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2491 | Template.getAsTemplateDecl() && |
| 2492 | isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl()); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2493 | QualType CanonType; |
| 2494 | if (!Underlying.isNull()) |
| 2495 | CanonType = getCanonicalType(Underlying); |
| 2496 | else { |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2497 | // We can get here with an alias template when the specialization contains |
| 2498 | // a pack expansion that does not match up with a parameter pack. |
| 2499 | assert((!IsTypeAlias || hasAnyPackExpansions(Args, NumArgs)) && |
| 2500 | "Caller must compute aliased type"); |
| 2501 | IsTypeAlias = false; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2502 | CanonType = getCanonicalTemplateSpecializationType(Template, Args, |
| 2503 | NumArgs); |
| 2504 | } |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 2505 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2506 | // Allocate the (non-canonical) template specialization type, but don't |
| 2507 | // try to unique it: these types typically have location information that |
| 2508 | // we don't unique and don't want to lose. |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2509 | void *Mem = Allocate(sizeof(TemplateSpecializationType) + |
| 2510 | sizeof(TemplateArgument) * NumArgs + |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2511 | (IsTypeAlias? sizeof(QualType) : 0), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2512 | TypeAlignment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2513 | TemplateSpecializationType *Spec |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2514 | = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, CanonType, |
| 2515 | IsTypeAlias ? Underlying : QualType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2516 | |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2517 | Types.push_back(Spec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2518 | return QualType(Spec, 0); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2519 | } |
| 2520 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2521 | QualType |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2522 | ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template, |
| 2523 | const TemplateArgument *Args, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2524 | unsigned NumArgs) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2525 | assert(!Template.getAsDependentTemplateName() && |
| 2526 | "No dependent template names here!"); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2527 | |
Douglas Gregor | 0f0ea2a | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 2528 | // Look through qualified template names. |
| 2529 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 2530 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2531 | |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2532 | // Build the canonical template specialization type. |
| 2533 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2534 | SmallVector<TemplateArgument, 4> CanonArgs; |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2535 | CanonArgs.reserve(NumArgs); |
| 2536 | for (unsigned I = 0; I != NumArgs; ++I) |
| 2537 | CanonArgs.push_back(getCanonicalTemplateArgument(Args[I])); |
| 2538 | |
| 2539 | // Determine whether this canonical template specialization type already |
| 2540 | // exists. |
| 2541 | llvm::FoldingSetNodeID ID; |
| 2542 | TemplateSpecializationType::Profile(ID, CanonTemplate, |
| 2543 | CanonArgs.data(), NumArgs, *this); |
| 2544 | |
| 2545 | void *InsertPos = 0; |
| 2546 | TemplateSpecializationType *Spec |
| 2547 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2548 | |
| 2549 | if (!Spec) { |
| 2550 | // Allocate a new canonical template specialization type. |
| 2551 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
| 2552 | sizeof(TemplateArgument) * NumArgs), |
| 2553 | TypeAlignment); |
| 2554 | Spec = new (Mem) TemplateSpecializationType(CanonTemplate, |
| 2555 | CanonArgs.data(), NumArgs, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2556 | QualType(), QualType()); |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2557 | Types.push_back(Spec); |
| 2558 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
| 2559 | } |
| 2560 | |
| 2561 | assert(Spec->isDependentType() && |
| 2562 | "Non-dependent template-id type must have a canonical type"); |
| 2563 | return QualType(Spec, 0); |
| 2564 | } |
| 2565 | |
| 2566 | QualType |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2567 | ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword, |
| 2568 | NestedNameSpecifier *NNS, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2569 | QualType NamedType) const { |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2570 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2571 | ElaboratedType::Profile(ID, Keyword, NNS, NamedType); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2572 | |
| 2573 | void *InsertPos = 0; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2574 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2575 | if (T) |
| 2576 | return QualType(T, 0); |
| 2577 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2578 | QualType Canon = NamedType; |
| 2579 | if (!Canon.isCanonical()) { |
| 2580 | Canon = getCanonicalType(NamedType); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2581 | ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2582 | assert(!CheckT && "Elaborated canonical type broken"); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2583 | (void)CheckT; |
| 2584 | } |
| 2585 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2586 | T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2587 | Types.push_back(T); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2588 | ElaboratedTypes.InsertNode(T, InsertPos); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2589 | return QualType(T, 0); |
| 2590 | } |
| 2591 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 2592 | QualType |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2593 | ASTContext::getParenType(QualType InnerType) const { |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 2594 | llvm::FoldingSetNodeID ID; |
| 2595 | ParenType::Profile(ID, InnerType); |
| 2596 | |
| 2597 | void *InsertPos = 0; |
| 2598 | ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2599 | if (T) |
| 2600 | return QualType(T, 0); |
| 2601 | |
| 2602 | QualType Canon = InnerType; |
| 2603 | if (!Canon.isCanonical()) { |
| 2604 | Canon = getCanonicalType(InnerType); |
| 2605 | ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2606 | assert(!CheckT && "Paren canonical type broken"); |
| 2607 | (void)CheckT; |
| 2608 | } |
| 2609 | |
| 2610 | T = new (*this) ParenType(InnerType, Canon); |
| 2611 | Types.push_back(T); |
| 2612 | ParenTypes.InsertNode(T, InsertPos); |
| 2613 | return QualType(T, 0); |
| 2614 | } |
| 2615 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2616 | QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, |
| 2617 | NestedNameSpecifier *NNS, |
| 2618 | const IdentifierInfo *Name, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2619 | QualType Canon) const { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2620 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 2621 | |
| 2622 | if (Canon.isNull()) { |
| 2623 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2624 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 2625 | if (Keyword == ETK_None) |
| 2626 | CanonKeyword = ETK_Typename; |
| 2627 | |
| 2628 | if (CanonNNS != NNS || CanonKeyword != Keyword) |
| 2629 | Canon = getDependentNameType(CanonKeyword, CanonNNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2630 | } |
| 2631 | |
| 2632 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2633 | DependentNameType::Profile(ID, Keyword, NNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2634 | |
| 2635 | void *InsertPos = 0; |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2636 | DependentNameType *T |
| 2637 | = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2638 | if (T) |
| 2639 | return QualType(T, 0); |
| 2640 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2641 | T = new (*this) DependentNameType(Keyword, NNS, Name, Canon); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2642 | Types.push_back(T); |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2643 | DependentNameTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2644 | return QualType(T, 0); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2645 | } |
| 2646 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2647 | QualType |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2648 | ASTContext::getDependentTemplateSpecializationType( |
| 2649 | ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2650 | NestedNameSpecifier *NNS, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2651 | const IdentifierInfo *Name, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2652 | const TemplateArgumentListInfo &Args) const { |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2653 | // TODO: avoid this copy |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2654 | SmallVector<TemplateArgument, 16> ArgCopy; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2655 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
| 2656 | ArgCopy.push_back(Args[I].getArgument()); |
| 2657 | return getDependentTemplateSpecializationType(Keyword, NNS, Name, |
| 2658 | ArgCopy.size(), |
| 2659 | ArgCopy.data()); |
| 2660 | } |
| 2661 | |
| 2662 | QualType |
| 2663 | ASTContext::getDependentTemplateSpecializationType( |
| 2664 | ElaboratedTypeKeyword Keyword, |
| 2665 | NestedNameSpecifier *NNS, |
| 2666 | const IdentifierInfo *Name, |
| 2667 | unsigned NumArgs, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2668 | const TemplateArgument *Args) const { |
Douglas Gregor | aa2187d | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 2669 | assert((!NNS || NNS->isDependent()) && |
| 2670 | "nested-name-specifier must be dependent"); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2671 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2672 | llvm::FoldingSetNodeID ID; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2673 | DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS, |
| 2674 | Name, NumArgs, Args); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2675 | |
| 2676 | void *InsertPos = 0; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2677 | DependentTemplateSpecializationType *T |
| 2678 | = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2679 | if (T) |
| 2680 | return QualType(T, 0); |
| 2681 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2682 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2683 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2684 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 2685 | if (Keyword == ETK_None) CanonKeyword = ETK_Typename; |
| 2686 | |
| 2687 | bool AnyNonCanonArgs = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2688 | SmallVector<TemplateArgument, 16> CanonArgs(NumArgs); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2689 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 2690 | CanonArgs[I] = getCanonicalTemplateArgument(Args[I]); |
| 2691 | if (!CanonArgs[I].structurallyEquals(Args[I])) |
| 2692 | AnyNonCanonArgs = true; |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2693 | } |
| 2694 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2695 | QualType Canon; |
| 2696 | if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) { |
| 2697 | Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS, |
| 2698 | Name, NumArgs, |
| 2699 | CanonArgs.data()); |
| 2700 | |
| 2701 | // Find the insert position again. |
| 2702 | DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2703 | } |
| 2704 | |
| 2705 | void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) + |
| 2706 | sizeof(TemplateArgument) * NumArgs), |
| 2707 | TypeAlignment); |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 2708 | T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2709 | Name, NumArgs, Args, Canon); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2710 | Types.push_back(T); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2711 | DependentTemplateSpecializationTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2712 | return QualType(T, 0); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2713 | } |
| 2714 | |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2715 | QualType ASTContext::getPackExpansionType(QualType Pattern, |
| 2716 | llvm::Optional<unsigned> NumExpansions) { |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2717 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2718 | PackExpansionType::Profile(ID, Pattern, NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2719 | |
| 2720 | assert(Pattern->containsUnexpandedParameterPack() && |
| 2721 | "Pack expansions must expand one or more parameter packs"); |
| 2722 | void *InsertPos = 0; |
| 2723 | PackExpansionType *T |
| 2724 | = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2725 | if (T) |
| 2726 | return QualType(T, 0); |
| 2727 | |
| 2728 | QualType Canon; |
| 2729 | if (!Pattern.isCanonical()) { |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2730 | Canon = getPackExpansionType(getCanonicalType(Pattern), NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2731 | |
| 2732 | // Find the insert position again. |
| 2733 | PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2734 | } |
| 2735 | |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2736 | T = new (*this) PackExpansionType(Pattern, Canon, NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2737 | Types.push_back(T); |
| 2738 | PackExpansionTypes.InsertNode(T, InsertPos); |
| 2739 | return QualType(T, 0); |
| 2740 | } |
| 2741 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2742 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 2743 | /// alphabetically. |
| 2744 | static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, |
| 2745 | const ObjCProtocolDecl *RHS) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2746 | return LHS->getDeclName() < RHS->getDeclName(); |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2747 | } |
| 2748 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2749 | static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols, |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2750 | unsigned NumProtocols) { |
| 2751 | if (NumProtocols == 0) return true; |
| 2752 | |
Douglas Gregor | 61cc296 | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 2753 | if (Protocols[0]->getCanonicalDecl() != Protocols[0]) |
| 2754 | return false; |
| 2755 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2756 | for (unsigned i = 1; i != NumProtocols; ++i) |
Douglas Gregor | 61cc296 | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 2757 | if (!CmpProtocolNames(Protocols[i-1], Protocols[i]) || |
| 2758 | Protocols[i]->getCanonicalDecl() != Protocols[i]) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2759 | return false; |
| 2760 | return true; |
| 2761 | } |
| 2762 | |
| 2763 | static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols, |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2764 | unsigned &NumProtocols) { |
| 2765 | ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2766 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2767 | // Sort protocols, keyed by name. |
| 2768 | std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); |
| 2769 | |
Douglas Gregor | 61cc296 | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 2770 | // Canonicalize. |
| 2771 | for (unsigned I = 0, N = NumProtocols; I != N; ++I) |
| 2772 | Protocols[I] = Protocols[I]->getCanonicalDecl(); |
| 2773 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2774 | // Remove duplicates. |
| 2775 | ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); |
| 2776 | NumProtocols = ProtocolsEnd-Protocols; |
| 2777 | } |
| 2778 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2779 | QualType ASTContext::getObjCObjectType(QualType BaseType, |
| 2780 | ObjCProtocolDecl * const *Protocols, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2781 | unsigned NumProtocols) const { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2782 | // If the base type is an interface and there aren't any protocols |
| 2783 | // to add, then the interface type will do just fine. |
| 2784 | if (!NumProtocols && isa<ObjCInterfaceType>(BaseType)) |
| 2785 | return BaseType; |
| 2786 | |
| 2787 | // Look in the folding set for an existing type. |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2788 | llvm::FoldingSetNodeID ID; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2789 | ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2790 | void *InsertPos = 0; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2791 | if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2792 | return QualType(QT, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2793 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2794 | // Build the canonical type, which has the canonical base type and |
| 2795 | // a sorted-and-uniqued list of protocols. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2796 | QualType Canonical; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2797 | bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols); |
| 2798 | if (!ProtocolsSorted || !BaseType.isCanonical()) { |
| 2799 | if (!ProtocolsSorted) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2800 | SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols, |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 2801 | Protocols + NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2802 | unsigned UniqueCount = NumProtocols; |
| 2803 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2804 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2805 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 2806 | &Sorted[0], UniqueCount); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2807 | } else { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2808 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 2809 | Protocols, NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2810 | } |
| 2811 | |
| 2812 | // Regenerate InsertPos. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2813 | ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2814 | } |
| 2815 | |
| 2816 | unsigned Size = sizeof(ObjCObjectTypeImpl); |
| 2817 | Size += NumProtocols * sizeof(ObjCProtocolDecl *); |
| 2818 | void *Mem = Allocate(Size, TypeAlignment); |
| 2819 | ObjCObjectTypeImpl *T = |
| 2820 | new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols); |
| 2821 | |
| 2822 | Types.push_back(T); |
| 2823 | ObjCObjectTypes.InsertNode(T, InsertPos); |
| 2824 | return QualType(T, 0); |
| 2825 | } |
| 2826 | |
| 2827 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 2828 | /// the given object type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2829 | QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2830 | llvm::FoldingSetNodeID ID; |
| 2831 | ObjCObjectPointerType::Profile(ID, ObjectT); |
| 2832 | |
| 2833 | void *InsertPos = 0; |
| 2834 | if (ObjCObjectPointerType *QT = |
| 2835 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2836 | return QualType(QT, 0); |
| 2837 | |
| 2838 | // Find the canonical object type. |
| 2839 | QualType Canonical; |
| 2840 | if (!ObjectT.isCanonical()) { |
| 2841 | Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT)); |
| 2842 | |
| 2843 | // Regenerate InsertPos. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2844 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2845 | } |
| 2846 | |
Douglas Gregor | fd6a088 | 2010-02-08 22:59:26 +0000 | [diff] [blame] | 2847 | // No match. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2848 | void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment); |
| 2849 | ObjCObjectPointerType *QType = |
| 2850 | new (Mem) ObjCObjectPointerType(Canonical, ObjectT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2851 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2852 | Types.push_back(QType); |
| 2853 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2854 | return QualType(QType, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2855 | } |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2856 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2857 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 2858 | /// specified ObjC interface decl. The list of protocols is optional. |
Douglas Gregor | 0af5501 | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 2859 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl, |
| 2860 | ObjCInterfaceDecl *PrevDecl) const { |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2861 | if (Decl->TypeForDecl) |
| 2862 | return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2863 | |
Douglas Gregor | 0af5501 | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 2864 | if (PrevDecl) { |
| 2865 | assert(PrevDecl->TypeForDecl && "previous decl has no TypeForDecl"); |
| 2866 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 2867 | return QualType(PrevDecl->TypeForDecl, 0); |
| 2868 | } |
| 2869 | |
Douglas Gregor | 8d2dbbf | 2011-12-16 16:34:57 +0000 | [diff] [blame] | 2870 | // Prefer the definition, if there is one. |
| 2871 | if (const ObjCInterfaceDecl *Def = Decl->getDefinition()) |
| 2872 | Decl = Def; |
| 2873 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2874 | void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment); |
| 2875 | ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl); |
| 2876 | Decl->TypeForDecl = T; |
| 2877 | Types.push_back(T); |
| 2878 | return QualType(T, 0); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2879 | } |
| 2880 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2881 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 2882 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2883 | /// multiple declarations that refer to "typeof(x)" all contain different |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2884 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2885 | /// on canonical type's (which are always unique). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2886 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) const { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2887 | TypeOfExprType *toe; |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2888 | if (tofExpr->isTypeDependent()) { |
| 2889 | llvm::FoldingSetNodeID ID; |
| 2890 | DependentTypeOfExprType::Profile(ID, *this, tofExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2891 | |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2892 | void *InsertPos = 0; |
| 2893 | DependentTypeOfExprType *Canon |
| 2894 | = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2895 | if (Canon) { |
| 2896 | // We already have a "canonical" version of an identical, dependent |
| 2897 | // typeof(expr) type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2898 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2899 | QualType((TypeOfExprType*)Canon, 0)); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 2900 | } else { |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2901 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2902 | Canon |
| 2903 | = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2904 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); |
| 2905 | toe = Canon; |
| 2906 | } |
| 2907 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2908 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2909 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2910 | } |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2911 | Types.push_back(toe); |
| 2912 | return QualType(toe, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2913 | } |
| 2914 | |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2915 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 2916 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 2917 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2918 | /// an issue. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2919 | /// on canonical type's (which are always unique). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2920 | QualType ASTContext::getTypeOfType(QualType tofType) const { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2921 | QualType Canonical = getCanonicalType(tofType); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2922 | TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2923 | Types.push_back(tot); |
| 2924 | return QualType(tot, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2925 | } |
| 2926 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2927 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2928 | /// getDecltypeType - Unlike many "get<Type>" functions, we don't unique |
| 2929 | /// DecltypeType AST's. The only motivation to unique these nodes would be |
| 2930 | /// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2931 | /// an issue. This doesn't effect the type checker, since it operates |
David Blaikie | 39e0203 | 2011-11-06 22:28:03 +0000 | [diff] [blame] | 2932 | /// on canonical types (which are always unique). |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 2933 | QualType ASTContext::getDecltypeType(Expr *e, QualType UnderlyingType) const { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2934 | DecltypeType *dt; |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2935 | |
| 2936 | // C++0x [temp.type]p2: |
| 2937 | // If an expression e involves a template parameter, decltype(e) denotes a |
| 2938 | // unique dependent type. Two such decltype-specifiers refer to the same |
| 2939 | // type only if their expressions are equivalent (14.5.6.1). |
| 2940 | if (e->isInstantiationDependent()) { |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2941 | llvm::FoldingSetNodeID ID; |
| 2942 | DependentDecltypeType::Profile(ID, *this, e); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2943 | |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2944 | void *InsertPos = 0; |
| 2945 | DependentDecltypeType *Canon |
| 2946 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2947 | if (Canon) { |
| 2948 | // We already have a "canonical" version of an equivalent, dependent |
| 2949 | // decltype type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2950 | dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy, |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2951 | QualType((DecltypeType*)Canon, 0)); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 2952 | } else { |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2953 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2954 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2955 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); |
| 2956 | dt = Canon; |
| 2957 | } |
| 2958 | } else { |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 2959 | dt = new (*this, TypeAlignment) DecltypeType(e, UnderlyingType, |
| 2960 | getCanonicalType(UnderlyingType)); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2961 | } |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2962 | Types.push_back(dt); |
| 2963 | return QualType(dt, 0); |
| 2964 | } |
| 2965 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 2966 | /// getUnaryTransformationType - We don't unique these, since the memory |
| 2967 | /// savings are minimal and these are rare. |
| 2968 | QualType ASTContext::getUnaryTransformType(QualType BaseType, |
| 2969 | QualType UnderlyingType, |
| 2970 | UnaryTransformType::UTTKind Kind) |
| 2971 | const { |
| 2972 | UnaryTransformType *Ty = |
Douglas Gregor | 69d9775 | 2011-05-25 17:51:54 +0000 | [diff] [blame] | 2973 | new (*this, TypeAlignment) UnaryTransformType (BaseType, UnderlyingType, |
| 2974 | Kind, |
| 2975 | UnderlyingType->isDependentType() ? |
Peter Collingbourne | 12fc4b0 | 2012-03-05 16:02:06 +0000 | [diff] [blame] | 2976 | QualType() : getCanonicalType(UnderlyingType)); |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 2977 | Types.push_back(Ty); |
| 2978 | return QualType(Ty, 0); |
| 2979 | } |
| 2980 | |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 2981 | /// getAutoType - We only unique auto types after they've been deduced. |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2982 | QualType ASTContext::getAutoType(QualType DeducedType) const { |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 2983 | void *InsertPos = 0; |
| 2984 | if (!DeducedType.isNull()) { |
| 2985 | // Look in the folding set for an existing type. |
| 2986 | llvm::FoldingSetNodeID ID; |
| 2987 | AutoType::Profile(ID, DeducedType); |
| 2988 | if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2989 | return QualType(AT, 0); |
| 2990 | } |
| 2991 | |
| 2992 | AutoType *AT = new (*this, TypeAlignment) AutoType(DeducedType); |
| 2993 | Types.push_back(AT); |
| 2994 | if (InsertPos) |
| 2995 | AutoTypes.InsertNode(AT, InsertPos); |
| 2996 | return QualType(AT, 0); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2997 | } |
| 2998 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 2999 | /// getAtomicType - Return the uniqued reference to the atomic type for |
| 3000 | /// the given value type. |
| 3001 | QualType ASTContext::getAtomicType(QualType T) const { |
| 3002 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 3003 | // structure. |
| 3004 | llvm::FoldingSetNodeID ID; |
| 3005 | AtomicType::Profile(ID, T); |
| 3006 | |
| 3007 | void *InsertPos = 0; |
| 3008 | if (AtomicType *AT = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3009 | return QualType(AT, 0); |
| 3010 | |
| 3011 | // If the atomic value type isn't canonical, this won't be a canonical type |
| 3012 | // either, so fill in the canonical type field. |
| 3013 | QualType Canonical; |
| 3014 | if (!T.isCanonical()) { |
| 3015 | Canonical = getAtomicType(getCanonicalType(T)); |
| 3016 | |
| 3017 | // Get the new insert position for the node we care about. |
| 3018 | AtomicType *NewIP = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3019 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
| 3020 | } |
| 3021 | AtomicType *New = new (*this, TypeAlignment) AtomicType(T, Canonical); |
| 3022 | Types.push_back(New); |
| 3023 | AtomicTypes.InsertNode(New, InsertPos); |
| 3024 | return QualType(New, 0); |
| 3025 | } |
| 3026 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3027 | /// getAutoDeductType - Get type pattern for deducing against 'auto'. |
| 3028 | QualType ASTContext::getAutoDeductType() const { |
| 3029 | if (AutoDeductTy.isNull()) |
| 3030 | AutoDeductTy = getAutoType(QualType()); |
| 3031 | assert(!AutoDeductTy.isNull() && "can't build 'auto' pattern"); |
| 3032 | return AutoDeductTy; |
| 3033 | } |
| 3034 | |
| 3035 | /// getAutoRRefDeductType - Get type pattern for deducing against 'auto &&'. |
| 3036 | QualType ASTContext::getAutoRRefDeductType() const { |
| 3037 | if (AutoRRefDeductTy.isNull()) |
| 3038 | AutoRRefDeductTy = getRValueReferenceType(getAutoDeductType()); |
| 3039 | assert(!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern"); |
| 3040 | return AutoRRefDeductTy; |
| 3041 | } |
| 3042 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3043 | /// getTagDeclType - Return the unique reference to the type for the |
| 3044 | /// specified TagDecl (struct/union/class/enum) decl. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3045 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) const { |
Ted Kremenek | d778f88 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 3046 | assert (Decl); |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 3047 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 3048 | // away const? mutable? |
| 3049 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3050 | } |
| 3051 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3052 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 3053 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 3054 | /// needs to agree with the definition in <stddef.h>. |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 3055 | CanQualType ASTContext::getSizeType() const { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3056 | return getFromTargetType(Target->getSizeType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3057 | } |
| 3058 | |
Hans Wennborg | 29e97cb | 2011-10-27 08:29:09 +0000 | [diff] [blame] | 3059 | /// getIntMaxType - Return the unique type for "intmax_t" (C99 7.18.1.5). |
| 3060 | CanQualType ASTContext::getIntMaxType() const { |
| 3061 | return getFromTargetType(Target->getIntMaxType()); |
| 3062 | } |
| 3063 | |
| 3064 | /// getUIntMaxType - Return the unique type for "uintmax_t" (C99 7.18.1.5). |
| 3065 | CanQualType ASTContext::getUIntMaxType() const { |
| 3066 | return getFromTargetType(Target->getUIntMaxType()); |
| 3067 | } |
| 3068 | |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3069 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 3070 | /// Used when in C++, as a GCC extension. |
| 3071 | QualType ASTContext::getSignedWCharType() const { |
| 3072 | // FIXME: derive from "Target" ? |
| 3073 | return WCharTy; |
| 3074 | } |
| 3075 | |
| 3076 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 3077 | /// Used when in C++, as a GCC extension. |
| 3078 | QualType ASTContext::getUnsignedWCharType() const { |
| 3079 | // FIXME: derive from "Target" ? |
| 3080 | return UnsignedIntTy; |
| 3081 | } |
| 3082 | |
Hans Wennborg | 29e97cb | 2011-10-27 08:29:09 +0000 | [diff] [blame] | 3083 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17) |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 3084 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 3085 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3086 | return getFromTargetType(Target->getPtrDiffType(0)); |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 3087 | } |
| 3088 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3089 | //===----------------------------------------------------------------------===// |
| 3090 | // Type Operators |
| 3091 | //===----------------------------------------------------------------------===// |
| 3092 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3093 | CanQualType ASTContext::getCanonicalParamType(QualType T) const { |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3094 | // Push qualifiers into arrays, and then discard any remaining |
| 3095 | // qualifiers. |
| 3096 | T = getCanonicalType(T); |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3097 | T = getVariableArrayDecayedType(T); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3098 | const Type *Ty = T.getTypePtr(); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3099 | QualType Result; |
| 3100 | if (isa<ArrayType>(Ty)) { |
| 3101 | Result = getArrayDecayedType(QualType(Ty,0)); |
| 3102 | } else if (isa<FunctionType>(Ty)) { |
| 3103 | Result = getPointerType(QualType(Ty, 0)); |
| 3104 | } else { |
| 3105 | Result = QualType(Ty, 0); |
| 3106 | } |
| 3107 | |
| 3108 | return CanQualType::CreateUnsafe(Result); |
| 3109 | } |
| 3110 | |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3111 | QualType ASTContext::getUnqualifiedArrayType(QualType type, |
| 3112 | Qualifiers &quals) { |
| 3113 | SplitQualType splitType = type.getSplitUnqualifiedType(); |
| 3114 | |
| 3115 | // FIXME: getSplitUnqualifiedType() actually walks all the way to |
| 3116 | // the unqualified desugared type and then drops it on the floor. |
| 3117 | // We then have to strip that sugar back off with |
| 3118 | // getUnqualifiedDesugaredType(), which is silly. |
| 3119 | const ArrayType *AT = |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3120 | dyn_cast<ArrayType>(splitType.Ty->getUnqualifiedDesugaredType()); |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3121 | |
| 3122 | // 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] | 3123 | if (!AT) { |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3124 | quals = splitType.Quals; |
| 3125 | return QualType(splitType.Ty, 0); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3126 | } |
| 3127 | |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3128 | // Otherwise, recurse on the array's element type. |
| 3129 | QualType elementType = AT->getElementType(); |
| 3130 | QualType unqualElementType = getUnqualifiedArrayType(elementType, quals); |
| 3131 | |
| 3132 | // If that didn't change the element type, AT has no qualifiers, so we |
| 3133 | // can just use the results in splitType. |
| 3134 | if (elementType == unqualElementType) { |
| 3135 | assert(quals.empty()); // from the recursive call |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3136 | quals = splitType.Quals; |
| 3137 | return QualType(splitType.Ty, 0); |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3138 | } |
| 3139 | |
| 3140 | // Otherwise, add in the qualifiers from the outermost type, then |
| 3141 | // build the type back up. |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3142 | quals.addConsistentQualifiers(splitType.Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3143 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3144 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3145 | return getConstantArrayType(unqualElementType, CAT->getSize(), |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3146 | CAT->getSizeModifier(), 0); |
| 3147 | } |
| 3148 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3149 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3150 | return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3151 | } |
| 3152 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3153 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3154 | return getVariableArrayType(unqualElementType, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3155 | VAT->getSizeExpr(), |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3156 | VAT->getSizeModifier(), |
| 3157 | VAT->getIndexTypeCVRQualifiers(), |
| 3158 | VAT->getBracketsRange()); |
| 3159 | } |
| 3160 | |
| 3161 | const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT); |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3162 | return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(), |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3163 | DSAT->getSizeModifier(), 0, |
| 3164 | SourceRange()); |
| 3165 | } |
| 3166 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3167 | /// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that |
| 3168 | /// may be similar (C++ 4.4), replaces T1 and T2 with the type that |
| 3169 | /// they point to and return true. If T1 and T2 aren't pointer types |
| 3170 | /// or pointer-to-member types, or if they are not similar at this |
| 3171 | /// level, returns false and leaves T1 and T2 unchanged. Top-level |
| 3172 | /// qualifiers on T1 and T2 are ignored. This function will typically |
| 3173 | /// be called in a loop that successively "unwraps" pointer and |
| 3174 | /// pointer-to-member types to compare them at each level. |
| 3175 | bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) { |
| 3176 | const PointerType *T1PtrType = T1->getAs<PointerType>(), |
| 3177 | *T2PtrType = T2->getAs<PointerType>(); |
| 3178 | if (T1PtrType && T2PtrType) { |
| 3179 | T1 = T1PtrType->getPointeeType(); |
| 3180 | T2 = T2PtrType->getPointeeType(); |
| 3181 | return true; |
| 3182 | } |
| 3183 | |
| 3184 | const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(), |
| 3185 | *T2MPType = T2->getAs<MemberPointerType>(); |
| 3186 | if (T1MPType && T2MPType && |
| 3187 | hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0), |
| 3188 | QualType(T2MPType->getClass(), 0))) { |
| 3189 | T1 = T1MPType->getPointeeType(); |
| 3190 | T2 = T2MPType->getPointeeType(); |
| 3191 | return true; |
| 3192 | } |
| 3193 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3194 | if (getLangOpts().ObjC1) { |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3195 | const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(), |
| 3196 | *T2OPType = T2->getAs<ObjCObjectPointerType>(); |
| 3197 | if (T1OPType && T2OPType) { |
| 3198 | T1 = T1OPType->getPointeeType(); |
| 3199 | T2 = T2OPType->getPointeeType(); |
| 3200 | return true; |
| 3201 | } |
| 3202 | } |
| 3203 | |
| 3204 | // FIXME: Block pointers, too? |
| 3205 | |
| 3206 | return false; |
| 3207 | } |
| 3208 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3209 | DeclarationNameInfo |
| 3210 | ASTContext::getNameForTemplate(TemplateName Name, |
| 3211 | SourceLocation NameLoc) const { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3212 | switch (Name.getKind()) { |
| 3213 | case TemplateName::QualifiedTemplate: |
| 3214 | case TemplateName::Template: |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3215 | // DNInfo work in progress: CHECKME: what about DNLoc? |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3216 | return DeclarationNameInfo(Name.getAsTemplateDecl()->getDeclName(), |
| 3217 | NameLoc); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3218 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3219 | case TemplateName::OverloadedTemplate: { |
| 3220 | OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate(); |
| 3221 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 3222 | return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc); |
| 3223 | } |
| 3224 | |
| 3225 | case TemplateName::DependentTemplate: { |
| 3226 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3227 | DeclarationName DName; |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3228 | if (DTN->isIdentifier()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3229 | DName = DeclarationNames.getIdentifier(DTN->getIdentifier()); |
| 3230 | return DeclarationNameInfo(DName, NameLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3231 | } else { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3232 | DName = DeclarationNames.getCXXOperatorName(DTN->getOperator()); |
| 3233 | // DNInfo work in progress: FIXME: source locations? |
| 3234 | DeclarationNameLoc DNLoc; |
| 3235 | DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding(); |
| 3236 | DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding(); |
| 3237 | return DeclarationNameInfo(DName, NameLoc, DNLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3238 | } |
| 3239 | } |
| 3240 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3241 | case TemplateName::SubstTemplateTemplateParm: { |
| 3242 | SubstTemplateTemplateParmStorage *subst |
| 3243 | = Name.getAsSubstTemplateTemplateParm(); |
| 3244 | return DeclarationNameInfo(subst->getParameter()->getDeclName(), |
| 3245 | NameLoc); |
| 3246 | } |
| 3247 | |
| 3248 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 3249 | SubstTemplateTemplateParmPackStorage *subst |
| 3250 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 3251 | return DeclarationNameInfo(subst->getParameterPack()->getDeclName(), |
| 3252 | NameLoc); |
| 3253 | } |
| 3254 | } |
| 3255 | |
| 3256 | llvm_unreachable("bad template name kind!"); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3257 | } |
| 3258 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3259 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3260 | switch (Name.getKind()) { |
| 3261 | case TemplateName::QualifiedTemplate: |
| 3262 | case TemplateName::Template: { |
| 3263 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3264 | if (TemplateTemplateParmDecl *TTP |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3265 | = dyn_cast<TemplateTemplateParmDecl>(Template)) |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3266 | Template = getCanonicalTemplateTemplateParmDecl(TTP); |
| 3267 | |
| 3268 | // The canonical template name is the canonical template declaration. |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 3269 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3270 | } |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 3271 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3272 | case TemplateName::OverloadedTemplate: |
| 3273 | llvm_unreachable("cannot canonicalize overloaded template"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3274 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3275 | case TemplateName::DependentTemplate: { |
| 3276 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 3277 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 3278 | return DTN->CanonicalTemplateName; |
| 3279 | } |
| 3280 | |
| 3281 | case TemplateName::SubstTemplateTemplateParm: { |
| 3282 | SubstTemplateTemplateParmStorage *subst |
| 3283 | = Name.getAsSubstTemplateTemplateParm(); |
| 3284 | return getCanonicalTemplateName(subst->getReplacement()); |
| 3285 | } |
| 3286 | |
| 3287 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 3288 | SubstTemplateTemplateParmPackStorage *subst |
| 3289 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 3290 | TemplateTemplateParmDecl *canonParameter |
| 3291 | = getCanonicalTemplateTemplateParmDecl(subst->getParameterPack()); |
| 3292 | TemplateArgument canonArgPack |
| 3293 | = getCanonicalTemplateArgument(subst->getArgumentPack()); |
| 3294 | return getSubstTemplateTemplateParmPack(canonParameter, canonArgPack); |
| 3295 | } |
| 3296 | } |
| 3297 | |
| 3298 | llvm_unreachable("bad template name!"); |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 3299 | } |
| 3300 | |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 3301 | bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) { |
| 3302 | X = getCanonicalTemplateName(X); |
| 3303 | Y = getCanonicalTemplateName(Y); |
| 3304 | return X.getAsVoidPointer() == Y.getAsVoidPointer(); |
| 3305 | } |
| 3306 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3307 | TemplateArgument |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3308 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const { |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3309 | switch (Arg.getKind()) { |
| 3310 | case TemplateArgument::Null: |
| 3311 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3312 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3313 | case TemplateArgument::Expression: |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3314 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3315 | |
Douglas Gregor | d2008e2 | 2012-04-06 22:40:38 +0000 | [diff] [blame^] | 3316 | case TemplateArgument::Declaration: { |
| 3317 | if (Decl *D = Arg.getAsDecl()) |
| 3318 | return TemplateArgument(D->getCanonicalDecl()); |
| 3319 | return TemplateArgument((Decl*)0); |
| 3320 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3321 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3322 | case TemplateArgument::Template: |
| 3323 | return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate())); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3324 | |
| 3325 | case TemplateArgument::TemplateExpansion: |
| 3326 | return TemplateArgument(getCanonicalTemplateName( |
| 3327 | Arg.getAsTemplateOrTemplatePattern()), |
Douglas Gregor | 2be29f4 | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 3328 | Arg.getNumTemplateExpansions()); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3329 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3330 | case TemplateArgument::Integral: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3331 | return TemplateArgument(*Arg.getAsIntegral(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3332 | getCanonicalType(Arg.getIntegralType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3333 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3334 | case TemplateArgument::Type: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3335 | return TemplateArgument(getCanonicalType(Arg.getAsType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3336 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3337 | case TemplateArgument::Pack: { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 3338 | if (Arg.pack_size() == 0) |
| 3339 | return Arg; |
| 3340 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3341 | TemplateArgument *CanonArgs |
| 3342 | = new (*this) TemplateArgument[Arg.pack_size()]; |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3343 | unsigned Idx = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3344 | for (TemplateArgument::pack_iterator A = Arg.pack_begin(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3345 | AEnd = Arg.pack_end(); |
| 3346 | A != AEnd; (void)++A, ++Idx) |
| 3347 | CanonArgs[Idx] = getCanonicalTemplateArgument(*A); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3348 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3349 | return TemplateArgument(CanonArgs, Arg.pack_size()); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3350 | } |
| 3351 | } |
| 3352 | |
| 3353 | // Silence GCC warning |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3354 | llvm_unreachable("Unhandled template argument kind"); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3355 | } |
| 3356 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3357 | NestedNameSpecifier * |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3358 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3359 | if (!NNS) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3360 | return 0; |
| 3361 | |
| 3362 | switch (NNS->getKind()) { |
| 3363 | case NestedNameSpecifier::Identifier: |
| 3364 | // Canonicalize the prefix but keep the identifier the same. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3365 | return NestedNameSpecifier::Create(*this, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3366 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 3367 | NNS->getAsIdentifier()); |
| 3368 | |
| 3369 | case NestedNameSpecifier::Namespace: |
| 3370 | // A namespace is canonical; build a nested-name-specifier with |
| 3371 | // this namespace and no prefix. |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 3372 | return NestedNameSpecifier::Create(*this, 0, |
| 3373 | NNS->getAsNamespace()->getOriginalNamespace()); |
| 3374 | |
| 3375 | case NestedNameSpecifier::NamespaceAlias: |
| 3376 | // A namespace is canonical; build a nested-name-specifier with |
| 3377 | // this namespace and no prefix. |
| 3378 | return NestedNameSpecifier::Create(*this, 0, |
| 3379 | NNS->getAsNamespaceAlias()->getNamespace() |
| 3380 | ->getOriginalNamespace()); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3381 | |
| 3382 | case NestedNameSpecifier::TypeSpec: |
| 3383 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 3384 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3385 | |
| 3386 | // If we have some kind of dependent-named type (e.g., "typename T::type"), |
| 3387 | // break it apart into its prefix and identifier, then reconsititute those |
| 3388 | // as the canonical nested-name-specifier. This is required to canonicalize |
| 3389 | // a dependent nested-name-specifier involving typedefs of dependent-name |
| 3390 | // types, e.g., |
| 3391 | // typedef typename T::type T1; |
| 3392 | // typedef typename T1::type T2; |
Eli Friedman | 16412ef | 2012-03-03 04:09:56 +0000 | [diff] [blame] | 3393 | if (const DependentNameType *DNT = T->getAs<DependentNameType>()) |
| 3394 | return NestedNameSpecifier::Create(*this, DNT->getQualifier(), |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3395 | const_cast<IdentifierInfo *>(DNT->getIdentifier())); |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3396 | |
Eli Friedman | 16412ef | 2012-03-03 04:09:56 +0000 | [diff] [blame] | 3397 | // Otherwise, just canonicalize the type, and force it to be a TypeSpec. |
| 3398 | // FIXME: Why are TypeSpec and TypeSpecWithTemplate distinct in the |
| 3399 | // first place? |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3400 | return NestedNameSpecifier::Create(*this, 0, false, |
| 3401 | const_cast<Type*>(T.getTypePtr())); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3402 | } |
| 3403 | |
| 3404 | case NestedNameSpecifier::Global: |
| 3405 | // The global specifier is canonical and unique. |
| 3406 | return NNS; |
| 3407 | } |
| 3408 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3409 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3410 | } |
| 3411 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3412 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3413 | const ArrayType *ASTContext::getAsArrayType(QualType T) const { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3414 | // Handle the non-qualified case efficiently. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3415 | if (!T.hasLocalQualifiers()) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3416 | // Handle the common positive case fast. |
| 3417 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 3418 | return AT; |
| 3419 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3420 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3421 | // Handle the common negative case fast. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3422 | if (!isa<ArrayType>(T.getCanonicalType())) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3423 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3424 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3425 | // Apply any qualifiers from the array type to the element type. This |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3426 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 3427 | // 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] | 3428 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3429 | // If we get here, we either have type qualifiers on the type, or we have |
| 3430 | // 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] | 3431 | // we must propagate them down into the element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3432 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3433 | SplitQualType split = T.getSplitDesugaredType(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3434 | Qualifiers qs = split.Quals; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3435 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3436 | // If we have a simple case, just return now. |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3437 | const ArrayType *ATy = dyn_cast<ArrayType>(split.Ty); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3438 | if (ATy == 0 || qs.empty()) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3439 | return ATy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3440 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3441 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 3442 | // qualifiers into the array element type and return a new array type. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3443 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3444 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3445 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 3446 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 3447 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3448 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3449 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 3450 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 3451 | IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3452 | IAT->getIndexTypeCVRQualifiers())); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3453 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3454 | if (const DependentSizedArrayType *DSAT |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3455 | = dyn_cast<DependentSizedArrayType>(ATy)) |
| 3456 | return cast<ArrayType>( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3457 | getDependentSizedArrayType(NewEltTy, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3458 | DSAT->getSizeExpr(), |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3459 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3460 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3461 | DSAT->getBracketsRange())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3462 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3463 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3464 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3465 | VAT->getSizeExpr(), |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3466 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3467 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3468 | VAT->getBracketsRange())); |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 3469 | } |
| 3470 | |
Douglas Gregor | 79e6bd3 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 3471 | QualType ASTContext::getAdjustedParameterType(QualType T) { |
| 3472 | // C99 6.7.5.3p7: |
| 3473 | // A declaration of a parameter as "array of type" shall be |
| 3474 | // adjusted to "qualified pointer to type", where the type |
| 3475 | // qualifiers (if any) are those specified within the [ and ] of |
| 3476 | // the array type derivation. |
| 3477 | if (T->isArrayType()) |
| 3478 | return getArrayDecayedType(T); |
| 3479 | |
| 3480 | // C99 6.7.5.3p8: |
| 3481 | // A declaration of a parameter as "function returning type" |
| 3482 | // shall be adjusted to "pointer to function returning type", as |
| 3483 | // in 6.3.2.1. |
| 3484 | if (T->isFunctionType()) |
| 3485 | return getPointerType(T); |
| 3486 | |
| 3487 | return T; |
| 3488 | } |
| 3489 | |
| 3490 | QualType ASTContext::getSignatureParameterType(QualType T) { |
| 3491 | T = getVariableArrayDecayedType(T); |
| 3492 | T = getAdjustedParameterType(T); |
| 3493 | return T.getUnqualifiedType(); |
| 3494 | } |
| 3495 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3496 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 3497 | /// specified array type to a pointer. This operation is non-trivial when |
| 3498 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 3499 | /// this returns a pointer to a properly qualified element of the array. |
| 3500 | /// |
| 3501 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3502 | QualType ASTContext::getArrayDecayedType(QualType Ty) const { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3503 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 3504 | // typedefs in the element type of the array. This also handles propagation |
| 3505 | // of type qualifiers from the array type into the element type if present |
| 3506 | // (C99 6.7.3p8). |
| 3507 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 3508 | assert(PrettyArrayType && "Not an array type!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3509 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3510 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3511 | |
| 3512 | // int x[restrict 4] -> int *restrict |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3513 | return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3514 | } |
| 3515 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3516 | QualType ASTContext::getBaseElementType(const ArrayType *array) const { |
| 3517 | return getBaseElementType(array->getElementType()); |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 3518 | } |
| 3519 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3520 | QualType ASTContext::getBaseElementType(QualType type) const { |
| 3521 | Qualifiers qs; |
| 3522 | while (true) { |
| 3523 | SplitQualType split = type.getSplitDesugaredType(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3524 | const ArrayType *array = split.Ty->getAsArrayTypeUnsafe(); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3525 | if (!array) break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3526 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3527 | type = array->getElementType(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3528 | qs.addConsistentQualifiers(split.Quals); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3529 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3530 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3531 | return getQualifiedType(type, qs); |
Anders Carlsson | 6183a99 | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 3532 | } |
| 3533 | |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 3534 | /// getConstantArrayElementCount - Returns number of constant array elements. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3535 | uint64_t |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 3536 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
| 3537 | uint64_t ElementCount = 1; |
| 3538 | do { |
| 3539 | ElementCount *= CA->getSize().getZExtValue(); |
| 3540 | CA = dyn_cast<ConstantArrayType>(CA->getElementType()); |
| 3541 | } while (CA); |
| 3542 | return ElementCount; |
| 3543 | } |
| 3544 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3545 | /// getFloatingRank - Return a relative rank for floating point types. |
| 3546 | /// 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] | 3547 | static FloatingRank getFloatingRank(QualType T) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3548 | if (const ComplexType *CT = T->getAs<ComplexType>()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3549 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3550 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3551 | assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"); |
| 3552 | switch (T->getAs<BuiltinType>()->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3553 | default: llvm_unreachable("getFloatingRank(): not a floating type"); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3554 | case BuiltinType::Half: return HalfRank; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3555 | case BuiltinType::Float: return FloatRank; |
| 3556 | case BuiltinType::Double: return DoubleRank; |
| 3557 | case BuiltinType::LongDouble: return LongDoubleRank; |
| 3558 | } |
| 3559 | } |
| 3560 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3561 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 3562 | /// point or a complex type (based on typeDomain/typeSize). |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 3563 | /// 'typeDomain' is a real floating point or complex type. |
| 3564 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 3565 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 3566 | QualType Domain) const { |
| 3567 | FloatingRank EltRank = getFloatingRank(Size); |
| 3568 | if (Domain->isComplexType()) { |
| 3569 | switch (EltRank) { |
David Blaikie | 561d3ab | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 3570 | case HalfRank: llvm_unreachable("Complex half is not supported"); |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 3571 | case FloatRank: return FloatComplexTy; |
| 3572 | case DoubleRank: return DoubleComplexTy; |
| 3573 | case LongDoubleRank: return LongDoubleComplexTy; |
| 3574 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3575 | } |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 3576 | |
| 3577 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 3578 | switch (EltRank) { |
David Blaikie | 561d3ab | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 3579 | case HalfRank: llvm_unreachable("Half ranks are not valid here"); |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 3580 | case FloatRank: return FloatTy; |
| 3581 | case DoubleRank: return DoubleTy; |
| 3582 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 3583 | } |
David Blaikie | 561d3ab | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 3584 | llvm_unreachable("getFloatingRank(): illegal value for rank"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3585 | } |
| 3586 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3587 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 3588 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 3589 | /// '_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] | 3590 | /// LHS < RHS, return -1. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3591 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const { |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3592 | FloatingRank LHSR = getFloatingRank(LHS); |
| 3593 | FloatingRank RHSR = getFloatingRank(RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3594 | |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3595 | if (LHSR == RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 3596 | return 0; |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3597 | if (LHSR > RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 3598 | return 1; |
| 3599 | return -1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3600 | } |
| 3601 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3602 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 3603 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 3604 | /// or if it is not canonicalized. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3605 | unsigned ASTContext::getIntegerRank(const Type *T) const { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 3606 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3607 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3608 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3609 | default: llvm_unreachable("getIntegerRank(): not a built-in integer"); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3610 | case BuiltinType::Bool: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3611 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3612 | case BuiltinType::Char_S: |
| 3613 | case BuiltinType::Char_U: |
| 3614 | case BuiltinType::SChar: |
| 3615 | case BuiltinType::UChar: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3616 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3617 | case BuiltinType::Short: |
| 3618 | case BuiltinType::UShort: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3619 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3620 | case BuiltinType::Int: |
| 3621 | case BuiltinType::UInt: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3622 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3623 | case BuiltinType::Long: |
| 3624 | case BuiltinType::ULong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3625 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3626 | case BuiltinType::LongLong: |
| 3627 | case BuiltinType::ULongLong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3628 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 3629 | case BuiltinType::Int128: |
| 3630 | case BuiltinType::UInt128: |
| 3631 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3632 | } |
| 3633 | } |
| 3634 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3635 | /// \brief Whether this is a promotable bitfield reference according |
| 3636 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
| 3637 | /// |
| 3638 | /// \returns the type this bit-field will promote to, or NULL if no |
| 3639 | /// promotion occurs. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3640 | QualType ASTContext::isPromotableBitField(Expr *E) const { |
Douglas Gregor | ceafbde | 2010-05-24 20:13:53 +0000 | [diff] [blame] | 3641 | if (E->isTypeDependent() || E->isValueDependent()) |
| 3642 | return QualType(); |
| 3643 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3644 | FieldDecl *Field = E->getBitField(); |
| 3645 | if (!Field) |
| 3646 | return QualType(); |
| 3647 | |
| 3648 | QualType FT = Field->getType(); |
| 3649 | |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3650 | uint64_t BitWidth = Field->getBitWidthValue(*this); |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3651 | uint64_t IntSize = getTypeSize(IntTy); |
| 3652 | // GCC extension compatibility: if the bit-field size is less than or equal |
| 3653 | // to the size of int, it gets promoted no matter what its type is. |
| 3654 | // For instance, unsigned long bf : 4 gets promoted to signed int. |
| 3655 | if (BitWidth < IntSize) |
| 3656 | return IntTy; |
| 3657 | |
| 3658 | if (BitWidth == IntSize) |
| 3659 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
| 3660 | |
| 3661 | // Types bigger than int are not subject to promotions, and therefore act |
| 3662 | // like the base type. |
| 3663 | // FIXME: This doesn't quite match what gcc does, but what gcc does here |
| 3664 | // is ridiculous. |
| 3665 | return QualType(); |
| 3666 | } |
| 3667 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3668 | /// getPromotedIntegerType - Returns the type that Promotable will |
| 3669 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
| 3670 | /// integer type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3671 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) const { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3672 | assert(!Promotable.isNull()); |
| 3673 | assert(Promotable->isPromotableIntegerType()); |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 3674 | if (const EnumType *ET = Promotable->getAs<EnumType>()) |
| 3675 | return ET->getDecl()->getPromotionType(); |
Eli Friedman | 68a2dc4 | 2011-10-26 07:22:48 +0000 | [diff] [blame] | 3676 | |
| 3677 | if (const BuiltinType *BT = Promotable->getAs<BuiltinType>()) { |
| 3678 | // C++ [conv.prom]: A prvalue of type char16_t, char32_t, or wchar_t |
| 3679 | // (3.9.1) can be converted to a prvalue of the first of the following |
| 3680 | // types that can represent all the values of its underlying type: |
| 3681 | // int, unsigned int, long int, unsigned long int, long long int, or |
| 3682 | // unsigned long long int [...] |
| 3683 | // FIXME: Is there some better way to compute this? |
| 3684 | if (BT->getKind() == BuiltinType::WChar_S || |
| 3685 | BT->getKind() == BuiltinType::WChar_U || |
| 3686 | BT->getKind() == BuiltinType::Char16 || |
| 3687 | BT->getKind() == BuiltinType::Char32) { |
| 3688 | bool FromIsSigned = BT->getKind() == BuiltinType::WChar_S; |
| 3689 | uint64_t FromSize = getTypeSize(BT); |
| 3690 | QualType PromoteTypes[] = { IntTy, UnsignedIntTy, LongTy, UnsignedLongTy, |
| 3691 | LongLongTy, UnsignedLongLongTy }; |
| 3692 | for (size_t Idx = 0; Idx < llvm::array_lengthof(PromoteTypes); ++Idx) { |
| 3693 | uint64_t ToSize = getTypeSize(PromoteTypes[Idx]); |
| 3694 | if (FromSize < ToSize || |
| 3695 | (FromSize == ToSize && |
| 3696 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) |
| 3697 | return PromoteTypes[Idx]; |
| 3698 | } |
| 3699 | llvm_unreachable("char type should fit into long long"); |
| 3700 | } |
| 3701 | } |
| 3702 | |
| 3703 | // At this point, we should have a signed or unsigned integer type. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3704 | if (Promotable->isSignedIntegerType()) |
| 3705 | return IntTy; |
| 3706 | uint64_t PromotableSize = getTypeSize(Promotable); |
| 3707 | uint64_t IntSize = getTypeSize(IntTy); |
| 3708 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
| 3709 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
| 3710 | } |
| 3711 | |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 3712 | /// \brief Recurses in pointer/array types until it finds an objc retainable |
| 3713 | /// type and returns its ownership. |
| 3714 | Qualifiers::ObjCLifetime ASTContext::getInnerObjCOwnership(QualType T) const { |
| 3715 | while (!T.isNull()) { |
| 3716 | if (T.getObjCLifetime() != Qualifiers::OCL_None) |
| 3717 | return T.getObjCLifetime(); |
| 3718 | if (T->isArrayType()) |
| 3719 | T = getBaseElementType(T); |
| 3720 | else if (const PointerType *PT = T->getAs<PointerType>()) |
| 3721 | T = PT->getPointeeType(); |
| 3722 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
Argyrios Kyrtzidis | 28445f0 | 2011-07-01 23:01:46 +0000 | [diff] [blame] | 3723 | T = RT->getPointeeType(); |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 3724 | else |
| 3725 | break; |
| 3726 | } |
| 3727 | |
| 3728 | return Qualifiers::OCL_None; |
| 3729 | } |
| 3730 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3731 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3732 | /// 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] | 3733 | /// LHS < RHS, return -1. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3734 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3735 | const Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 3736 | const Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3737 | if (LHSC == RHSC) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3738 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3739 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 3740 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3741 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3742 | unsigned LHSRank = getIntegerRank(LHSC); |
| 3743 | unsigned RHSRank = getIntegerRank(RHSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3744 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3745 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 3746 | if (LHSRank == RHSRank) return 0; |
| 3747 | return LHSRank > RHSRank ? 1 : -1; |
| 3748 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3749 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3750 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 3751 | if (LHSUnsigned) { |
| 3752 | // If the unsigned [LHS] type is larger, return it. |
| 3753 | if (LHSRank >= RHSRank) |
| 3754 | return 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3755 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3756 | // If the signed type can represent all values of the unsigned type, it |
| 3757 | // 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] | 3758 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3759 | return -1; |
| 3760 | } |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3761 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3762 | // If the unsigned [RHS] type is larger, return it. |
| 3763 | if (RHSRank >= LHSRank) |
| 3764 | return -1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3765 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3766 | // If the signed type can represent all values of the unsigned type, it |
| 3767 | // 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] | 3768 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3769 | return 1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3770 | } |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3771 | |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3772 | static RecordDecl * |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3773 | CreateRecordDecl(const ASTContext &Ctx, RecordDecl::TagKind TK, |
| 3774 | DeclContext *DC, IdentifierInfo *Id) { |
| 3775 | SourceLocation Loc; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3776 | if (Ctx.getLangOpts().CPlusPlus) |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3777 | return CXXRecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3778 | else |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3779 | return RecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3780 | } |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3781 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3782 | // getCFConstantStringType - Return the type used for constant CFStrings. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3783 | QualType ASTContext::getCFConstantStringType() const { |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3784 | if (!CFConstantStringTypeDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3785 | CFConstantStringTypeDecl = |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3786 | CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3787 | &Idents.get("NSConstantString")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3788 | CFConstantStringTypeDecl->startDefinition(); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3789 | |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 3790 | QualType FieldTypes[4]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3791 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3792 | // const int *isa; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3793 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 3794 | // int flags; |
| 3795 | FieldTypes[1] = IntTy; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3796 | // const char *str; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3797 | FieldTypes[2] = getPointerType(CharTy.withConst()); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3798 | // long length; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3799 | FieldTypes[3] = LongTy; |
| 3800 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3801 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3802 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3803 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3804 | SourceLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3805 | SourceLocation(), 0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3806 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3807 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3808 | /*Mutable=*/false, |
| 3809 | /*HasInit=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3810 | Field->setAccess(AS_public); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3811 | CFConstantStringTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3812 | } |
| 3813 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3814 | CFConstantStringTypeDecl->completeDefinition(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3815 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3816 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3817 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 8467583 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 3818 | } |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 3819 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3820 | void ASTContext::setCFConstantStringType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3821 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3822 | assert(Rec && "Invalid CFConstantStringType"); |
| 3823 | CFConstantStringTypeDecl = Rec->getDecl(); |
| 3824 | } |
| 3825 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3826 | QualType ASTContext::getBlockDescriptorType() const { |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3827 | if (BlockDescriptorType) |
| 3828 | return getTagDeclType(BlockDescriptorType); |
| 3829 | |
| 3830 | RecordDecl *T; |
| 3831 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3832 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3833 | &Idents.get("__block_descriptor")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3834 | T->startDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3835 | |
| 3836 | QualType FieldTypes[] = { |
| 3837 | UnsignedLongTy, |
| 3838 | UnsignedLongTy, |
| 3839 | }; |
| 3840 | |
| 3841 | const char *FieldNames[] = { |
| 3842 | "reserved", |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3843 | "Size" |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3844 | }; |
| 3845 | |
| 3846 | for (size_t i = 0; i < 2; ++i) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3847 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3848 | SourceLocation(), |
| 3849 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3850 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3851 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3852 | /*Mutable=*/false, |
| 3853 | /*HasInit=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3854 | Field->setAccess(AS_public); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3855 | T->addDecl(Field); |
| 3856 | } |
| 3857 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3858 | T->completeDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3859 | |
| 3860 | BlockDescriptorType = T; |
| 3861 | |
| 3862 | return getTagDeclType(BlockDescriptorType); |
| 3863 | } |
| 3864 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3865 | QualType ASTContext::getBlockDescriptorExtendedType() const { |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3866 | if (BlockDescriptorExtendedType) |
| 3867 | return getTagDeclType(BlockDescriptorExtendedType); |
| 3868 | |
| 3869 | RecordDecl *T; |
| 3870 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3871 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3872 | &Idents.get("__block_descriptor_withcopydispose")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3873 | T->startDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3874 | |
| 3875 | QualType FieldTypes[] = { |
| 3876 | UnsignedLongTy, |
| 3877 | UnsignedLongTy, |
| 3878 | getPointerType(VoidPtrTy), |
| 3879 | getPointerType(VoidPtrTy) |
| 3880 | }; |
| 3881 | |
| 3882 | const char *FieldNames[] = { |
| 3883 | "reserved", |
| 3884 | "Size", |
| 3885 | "CopyFuncPtr", |
| 3886 | "DestroyFuncPtr" |
| 3887 | }; |
| 3888 | |
| 3889 | for (size_t i = 0; i < 4; ++i) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3890 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3891 | SourceLocation(), |
| 3892 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3893 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3894 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3895 | /*Mutable=*/false, |
| 3896 | /*HasInit=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3897 | Field->setAccess(AS_public); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3898 | T->addDecl(Field); |
| 3899 | } |
| 3900 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3901 | T->completeDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3902 | |
| 3903 | BlockDescriptorExtendedType = T; |
| 3904 | |
| 3905 | return getTagDeclType(BlockDescriptorExtendedType); |
| 3906 | } |
| 3907 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3908 | bool ASTContext::BlockRequiresCopying(QualType Ty) const { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3909 | if (Ty->isObjCRetainableType()) |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3910 | return true; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3911 | if (getLangOpts().CPlusPlus) { |
Fariborz Jahanian | e38be61 | 2010-11-17 00:21:28 +0000 | [diff] [blame] | 3912 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3913 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 3914 | return RD->hasConstCopyConstructor(); |
Fariborz Jahanian | e38be61 | 2010-11-17 00:21:28 +0000 | [diff] [blame] | 3915 | |
| 3916 | } |
| 3917 | } |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3918 | return false; |
| 3919 | } |
| 3920 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3921 | QualType |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3922 | ASTContext::BuildByRefType(StringRef DeclName, QualType Ty) const { |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3923 | // type = struct __Block_byref_1_X { |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3924 | // void *__isa; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3925 | // struct __Block_byref_1_X *__forwarding; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3926 | // unsigned int __flags; |
| 3927 | // unsigned int __size; |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 3928 | // void *__copy_helper; // as needed |
| 3929 | // void *__destroy_help // as needed |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3930 | // int X; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3931 | // } * |
| 3932 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3933 | bool HasCopyAndDispose = BlockRequiresCopying(Ty); |
| 3934 | |
| 3935 | // FIXME: Move up |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 3936 | SmallString<36> Name; |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 3937 | llvm::raw_svector_ostream(Name) << "__Block_byref_" << |
| 3938 | ++UniqueBlockByRefTypeID << '_' << DeclName; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3939 | RecordDecl *T; |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3940 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, &Idents.get(Name.str())); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3941 | T->startDefinition(); |
| 3942 | QualType Int32Ty = IntTy; |
| 3943 | assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported"); |
| 3944 | QualType FieldTypes[] = { |
| 3945 | getPointerType(VoidPtrTy), |
| 3946 | getPointerType(getTagDeclType(T)), |
| 3947 | Int32Ty, |
| 3948 | Int32Ty, |
| 3949 | getPointerType(VoidPtrTy), |
| 3950 | getPointerType(VoidPtrTy), |
| 3951 | Ty |
| 3952 | }; |
| 3953 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3954 | StringRef FieldNames[] = { |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3955 | "__isa", |
| 3956 | "__forwarding", |
| 3957 | "__flags", |
| 3958 | "__size", |
| 3959 | "__copy_helper", |
| 3960 | "__destroy_helper", |
| 3961 | DeclName, |
| 3962 | }; |
| 3963 | |
| 3964 | for (size_t i = 0; i < 7; ++i) { |
| 3965 | if (!HasCopyAndDispose && i >=4 && i <= 5) |
| 3966 | continue; |
| 3967 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3968 | SourceLocation(), |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3969 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3970 | FieldTypes[i], /*TInfo=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3971 | /*BitWidth=*/0, /*Mutable=*/false, |
| 3972 | /*HasInit=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3973 | Field->setAccess(AS_public); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3974 | T->addDecl(Field); |
| 3975 | } |
| 3976 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3977 | T->completeDefinition(); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3978 | |
| 3979 | return getPointerType(getTagDeclType(T)); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3980 | } |
| 3981 | |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 3982 | TypedefDecl *ASTContext::getObjCInstanceTypeDecl() { |
| 3983 | if (!ObjCInstanceTypeDecl) |
| 3984 | ObjCInstanceTypeDecl = TypedefDecl::Create(*this, |
| 3985 | getTranslationUnitDecl(), |
| 3986 | SourceLocation(), |
| 3987 | SourceLocation(), |
| 3988 | &Idents.get("instancetype"), |
| 3989 | getTrivialTypeSourceInfo(getObjCIdType())); |
| 3990 | return ObjCInstanceTypeDecl; |
| 3991 | } |
| 3992 | |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3993 | // This returns true if a type has been typedefed to BOOL: |
| 3994 | // typedef <type> BOOL; |
Chris Lattner | 2d99833 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 3995 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3996 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | bb49c3e | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 3997 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 3998 | return II->isStr("BOOL"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3999 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4000 | return false; |
| 4001 | } |
| 4002 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4003 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4004 | /// purpose. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4005 | CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const { |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4006 | if (!type->isIncompleteArrayType() && type->isIncompleteType()) |
| 4007 | return CharUnits::Zero(); |
| 4008 | |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4009 | CharUnits sz = getTypeSizeInChars(type); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4010 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4011 | // 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] | 4012 | if (sz.isPositive() && type->isIntegralOrEnumerationType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4013 | sz = std::max(sz, getTypeSizeInChars(IntTy)); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4014 | // Treat arrays as pointers, since that's how they're passed in. |
| 4015 | else if (type->isArrayType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4016 | sz = getTypeSizeInChars(VoidPtrTy); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4017 | return sz; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4018 | } |
| 4019 | |
| 4020 | static inline |
| 4021 | std::string charUnitsToString(const CharUnits &CU) { |
| 4022 | return llvm::itostr(CU.getQuantity()); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4023 | } |
| 4024 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4025 | /// getObjCEncodingForBlock - Return the encoded type for this block |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4026 | /// declaration. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4027 | std::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const { |
| 4028 | std::string S; |
| 4029 | |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4030 | const BlockDecl *Decl = Expr->getBlockDecl(); |
| 4031 | QualType BlockTy = |
| 4032 | Expr->getType()->getAs<BlockPointerType>()->getPointeeType(); |
| 4033 | // Encode result type. |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 4034 | getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4035 | // Compute size of all parameters. |
| 4036 | // Start with computing size of a pointer in number of bytes. |
| 4037 | // FIXME: There might(should) be a better way of doing this computation! |
| 4038 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4039 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
| 4040 | CharUnits ParmOffset = PtrSize; |
Fariborz Jahanian | 6f46c26 | 2010-04-08 18:06:22 +0000 | [diff] [blame] | 4041 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4042 | E = Decl->param_end(); PI != E; ++PI) { |
| 4043 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4044 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4045 | assert (sz.isPositive() && "BlockExpr - Incomplete param type"); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4046 | ParmOffset += sz; |
| 4047 | } |
| 4048 | // Size of the argument frame |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4049 | S += charUnitsToString(ParmOffset); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4050 | // Block pointer and offset. |
| 4051 | S += "@?0"; |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4052 | |
| 4053 | // Argument types. |
| 4054 | ParmOffset = PtrSize; |
| 4055 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E = |
| 4056 | Decl->param_end(); PI != E; ++PI) { |
| 4057 | ParmVarDecl *PVDecl = *PI; |
| 4058 | QualType PType = PVDecl->getOriginalType(); |
| 4059 | if (const ArrayType *AT = |
| 4060 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4061 | // Use array's original type only if it has known number of |
| 4062 | // elements. |
| 4063 | if (!isa<ConstantArrayType>(AT)) |
| 4064 | PType = PVDecl->getType(); |
| 4065 | } else if (PType->isFunctionType()) |
| 4066 | PType = PVDecl->getType(); |
| 4067 | getObjCEncodingForType(PType, S); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4068 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4069 | ParmOffset += getObjCEncodingTypeSize(PType); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4070 | } |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4071 | |
| 4072 | return S; |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4073 | } |
| 4074 | |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4075 | bool ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl, |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4076 | std::string& S) { |
| 4077 | // Encode result type. |
| 4078 | getObjCEncodingForType(Decl->getResultType(), S); |
| 4079 | CharUnits ParmOffset; |
| 4080 | // Compute size of all parameters. |
| 4081 | for (FunctionDecl::param_const_iterator PI = Decl->param_begin(), |
| 4082 | E = Decl->param_end(); PI != E; ++PI) { |
| 4083 | QualType PType = (*PI)->getType(); |
| 4084 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4085 | if (sz.isZero()) |
| 4086 | return true; |
| 4087 | |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4088 | assert (sz.isPositive() && |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4089 | "getObjCEncodingForFunctionDecl - Incomplete param type"); |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4090 | ParmOffset += sz; |
| 4091 | } |
| 4092 | S += charUnitsToString(ParmOffset); |
| 4093 | ParmOffset = CharUnits::Zero(); |
| 4094 | |
| 4095 | // Argument types. |
| 4096 | for (FunctionDecl::param_const_iterator PI = Decl->param_begin(), |
| 4097 | E = Decl->param_end(); PI != E; ++PI) { |
| 4098 | ParmVarDecl *PVDecl = *PI; |
| 4099 | QualType PType = PVDecl->getOriginalType(); |
| 4100 | if (const ArrayType *AT = |
| 4101 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4102 | // Use array's original type only if it has known number of |
| 4103 | // elements. |
| 4104 | if (!isa<ConstantArrayType>(AT)) |
| 4105 | PType = PVDecl->getType(); |
| 4106 | } else if (PType->isFunctionType()) |
| 4107 | PType = PVDecl->getType(); |
| 4108 | getObjCEncodingForType(PType, S); |
| 4109 | S += charUnitsToString(ParmOffset); |
| 4110 | ParmOffset += getObjCEncodingTypeSize(PType); |
| 4111 | } |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4112 | |
| 4113 | return false; |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4114 | } |
| 4115 | |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4116 | /// getObjCEncodingForMethodParameter - Return the encoded type for a single |
| 4117 | /// method parameter or return type. If Extended, include class names and |
| 4118 | /// block object types. |
| 4119 | void ASTContext::getObjCEncodingForMethodParameter(Decl::ObjCDeclQualifier QT, |
| 4120 | QualType T, std::string& S, |
| 4121 | bool Extended) const { |
| 4122 | // Encode type qualifer, 'in', 'inout', etc. for the parameter. |
| 4123 | getObjCEncodingForTypeQualifier(QT, S); |
| 4124 | // Encode parameter type. |
| 4125 | getObjCEncodingForTypeImpl(T, S, true, true, 0, |
| 4126 | true /*OutermostType*/, |
| 4127 | false /*EncodingProperty*/, |
| 4128 | false /*StructField*/, |
| 4129 | Extended /*EncodeBlockParameters*/, |
| 4130 | Extended /*EncodeClassNames*/); |
| 4131 | } |
| 4132 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4133 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4134 | /// declaration. |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4135 | bool ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4136 | std::string& S, |
| 4137 | bool Extended) const { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4138 | // FIXME: This is not very efficient. |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4139 | // Encode return type. |
| 4140 | getObjCEncodingForMethodParameter(Decl->getObjCDeclQualifier(), |
| 4141 | Decl->getResultType(), S, Extended); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4142 | // Compute size of all parameters. |
| 4143 | // Start with computing size of a pointer in number of bytes. |
| 4144 | // FIXME: There might(should) be a better way of doing this computation! |
| 4145 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4146 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4147 | // The first two arguments (self and _cmd) are pointers; account for |
| 4148 | // their size. |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4149 | CharUnits ParmOffset = 2 * PtrSize; |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4150 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 4151 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 4152 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4153 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4154 | if (sz.isZero()) |
| 4155 | return true; |
| 4156 | |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4157 | assert (sz.isPositive() && |
| 4158 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4159 | ParmOffset += sz; |
| 4160 | } |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4161 | S += charUnitsToString(ParmOffset); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4162 | S += "@0:"; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4163 | S += charUnitsToString(PtrSize); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4164 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4165 | // Argument types. |
| 4166 | ParmOffset = 2 * PtrSize; |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4167 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 4168 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4169 | const ParmVarDecl *PVDecl = *PI; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4170 | QualType PType = PVDecl->getOriginalType(); |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 4171 | if (const ArrayType *AT = |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 4172 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4173 | // Use array's original type only if it has known number of |
| 4174 | // elements. |
Steve Naroff | bb3fde3 | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 4175 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 4176 | PType = PVDecl->getType(); |
| 4177 | } else if (PType->isFunctionType()) |
| 4178 | PType = PVDecl->getType(); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4179 | getObjCEncodingForMethodParameter(PVDecl->getObjCDeclQualifier(), |
| 4180 | PType, S, Extended); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4181 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4182 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4183 | } |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4184 | |
| 4185 | return false; |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4186 | } |
| 4187 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4188 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4189 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4190 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 4191 | /// NULL when getting encodings for protocol properties. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4192 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 4193 | /// attributes readonly and bycopy are encoded as single characters. The |
| 4194 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 4195 | /// encoded as single characters, followed by an identifier. Property types |
| 4196 | /// are also encoded as a parametrized attribute. The characters used to encode |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4197 | /// these attributes are defined by the following enumeration: |
| 4198 | /// @code |
| 4199 | /// enum PropertyAttributes { |
| 4200 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 4201 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 4202 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 4203 | /// kPropertyDynamic = 'D', // property is dynamic |
| 4204 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 4205 | /// kPropertySetter = 'S', // followed by setter selector name |
| 4206 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
Bob Wilson | 0d4cb85 | 2012-03-22 17:48:02 +0000 | [diff] [blame] | 4207 | /// kPropertyType = 'T' // followed by old-style type encoding. |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4208 | /// kPropertyWeak = 'W' // 'weak' property |
| 4209 | /// kPropertyStrong = 'P' // property GC'able |
| 4210 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 4211 | /// }; |
| 4212 | /// @endcode |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4213 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4214 | const Decl *Container, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4215 | std::string& S) const { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4216 | // Collect information from the property implementation decl(s). |
| 4217 | bool Dynamic = false; |
| 4218 | ObjCPropertyImplDecl *SynthesizePID = 0; |
| 4219 | |
| 4220 | // FIXME: Duplicated code due to poor abstraction. |
| 4221 | if (Container) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4222 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4223 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
| 4224 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4225 | i = CID->propimpl_begin(), e = CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4226 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4227 | ObjCPropertyImplDecl *PID = *i; |
| 4228 | if (PID->getPropertyDecl() == PD) { |
| 4229 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 4230 | Dynamic = true; |
| 4231 | } else { |
| 4232 | SynthesizePID = PID; |
| 4233 | } |
| 4234 | } |
| 4235 | } |
| 4236 | } else { |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4237 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4238 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4239 | i = OID->propimpl_begin(), e = OID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4240 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4241 | ObjCPropertyImplDecl *PID = *i; |
| 4242 | if (PID->getPropertyDecl() == PD) { |
| 4243 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 4244 | Dynamic = true; |
| 4245 | } else { |
| 4246 | SynthesizePID = PID; |
| 4247 | } |
| 4248 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4249 | } |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4250 | } |
| 4251 | } |
| 4252 | |
| 4253 | // FIXME: This is not very efficient. |
| 4254 | S = "T"; |
| 4255 | |
| 4256 | // Encode result type. |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4257 | // GCC has some special rules regarding encoding of properties which |
| 4258 | // closely resembles encoding of ivars. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4259 | getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4260 | true /* outermost type */, |
| 4261 | true /* encoding for property */); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4262 | |
| 4263 | if (PD->isReadOnly()) { |
| 4264 | S += ",R"; |
| 4265 | } else { |
| 4266 | switch (PD->getSetterKind()) { |
| 4267 | case ObjCPropertyDecl::Assign: break; |
| 4268 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4269 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
Fariborz Jahanian | 3a02b44 | 2011-08-12 20:47:08 +0000 | [diff] [blame] | 4270 | case ObjCPropertyDecl::Weak: S += ",W"; break; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4271 | } |
| 4272 | } |
| 4273 | |
| 4274 | // It really isn't clear at all what this means, since properties |
| 4275 | // are "dynamic by default". |
| 4276 | if (Dynamic) |
| 4277 | S += ",D"; |
| 4278 | |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4279 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 4280 | S += ",N"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4281 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4282 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 4283 | S += ",G"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4284 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4285 | } |
| 4286 | |
| 4287 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 4288 | S += ",S"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4289 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4290 | } |
| 4291 | |
| 4292 | if (SynthesizePID) { |
| 4293 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 4294 | S += ",V"; |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 4295 | S += OID->getNameAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4296 | } |
| 4297 | |
| 4298 | // FIXME: OBJCGC: weak & strong |
| 4299 | } |
| 4300 | |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4301 | /// getLegacyIntegralTypeEncoding - |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4302 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
| 4303 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4304 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
| 4305 | /// |
| 4306 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 4307 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4308 | if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) { |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4309 | if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4310 | PointeeTy = UnsignedIntTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4311 | else |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4312 | if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4313 | PointeeTy = IntTy; |
| 4314 | } |
| 4315 | } |
| 4316 | } |
| 4317 | |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4318 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4319 | const FieldDecl *Field) const { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4320 | // We follow the behavior of gcc, expanding structures which are |
| 4321 | // directly pointed to, and expanding embedded structures. Note that |
| 4322 | // these rules are sufficient to prevent recursive encoding of the |
| 4323 | // same type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4324 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
Fariborz Jahanian | 5b8c7d9 | 2008-12-22 23:22:27 +0000 | [diff] [blame] | 4325 | true /* outermost type */); |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4326 | } |
| 4327 | |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4328 | static char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) { |
| 4329 | switch (T->getAs<BuiltinType>()->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4330 | default: llvm_unreachable("Unhandled builtin type kind"); |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4331 | case BuiltinType::Void: return 'v'; |
| 4332 | case BuiltinType::Bool: return 'B'; |
| 4333 | case BuiltinType::Char_U: |
| 4334 | case BuiltinType::UChar: return 'C'; |
| 4335 | case BuiltinType::UShort: return 'S'; |
| 4336 | case BuiltinType::UInt: return 'I'; |
| 4337 | case BuiltinType::ULong: |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4338 | return C->getIntWidth(T) == 32 ? 'L' : 'Q'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4339 | case BuiltinType::UInt128: return 'T'; |
| 4340 | case BuiltinType::ULongLong: return 'Q'; |
| 4341 | case BuiltinType::Char_S: |
| 4342 | case BuiltinType::SChar: return 'c'; |
| 4343 | case BuiltinType::Short: return 's'; |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 4344 | case BuiltinType::WChar_S: |
| 4345 | case BuiltinType::WChar_U: |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4346 | case BuiltinType::Int: return 'i'; |
| 4347 | case BuiltinType::Long: |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4348 | return C->getIntWidth(T) == 32 ? 'l' : 'q'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4349 | case BuiltinType::LongLong: return 'q'; |
| 4350 | case BuiltinType::Int128: return 't'; |
| 4351 | case BuiltinType::Float: return 'f'; |
| 4352 | case BuiltinType::Double: return 'd'; |
Daniel Dunbar | 3a0be84 | 2010-10-11 21:13:48 +0000 | [diff] [blame] | 4353 | case BuiltinType::LongDouble: return 'D'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4354 | } |
| 4355 | } |
| 4356 | |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4357 | static char ObjCEncodingForEnumType(const ASTContext *C, const EnumType *ET) { |
| 4358 | EnumDecl *Enum = ET->getDecl(); |
| 4359 | |
| 4360 | // The encoding of an non-fixed enum type is always 'i', regardless of size. |
| 4361 | if (!Enum->isFixed()) |
| 4362 | return 'i'; |
| 4363 | |
| 4364 | // The encoding of a fixed enum type matches its fixed underlying type. |
| 4365 | return ObjCEncodingForPrimitiveKind(C, Enum->getIntegerType()); |
| 4366 | } |
| 4367 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4368 | static void EncodeBitField(const ASTContext *Ctx, std::string& S, |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4369 | QualType T, const FieldDecl *FD) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4370 | assert(FD->isBitField() && "not a bitfield - getObjCEncodingForTypeImpl"); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4371 | S += 'b'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4372 | // The NeXT runtime encodes bit fields as b followed by the number of bits. |
| 4373 | // The GNU runtime requires more information; bitfields are encoded as b, |
| 4374 | // then the offset (in bits) of the first element, then the type of the |
| 4375 | // bitfield, then the size in bits. For example, in this structure: |
| 4376 | // |
| 4377 | // struct |
| 4378 | // { |
| 4379 | // int integer; |
| 4380 | // int flags:2; |
| 4381 | // }; |
| 4382 | // On a 32-bit system, the encoding for flags would be b2 for the NeXT |
| 4383 | // runtime, but b32i2 for the GNU runtime. The reason for this extra |
| 4384 | // information is not especially sensible, but we're stuck with it for |
| 4385 | // compatibility with GCC, although providing it breaks anything that |
| 4386 | // actually uses runtime introspection and wants to work on both runtimes... |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4387 | if (!Ctx->getLangOpts().NeXTRuntime) { |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4388 | const RecordDecl *RD = FD->getParent(); |
| 4389 | const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD); |
Eli Friedman | 8290574 | 2011-07-07 01:54:01 +0000 | [diff] [blame] | 4390 | S += llvm::utostr(RL.getFieldOffset(FD->getFieldIndex())); |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4391 | if (const EnumType *ET = T->getAs<EnumType>()) |
| 4392 | S += ObjCEncodingForEnumType(Ctx, ET); |
David Chisnall | c7ff82c | 2010-12-26 20:12:30 +0000 | [diff] [blame] | 4393 | else |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4394 | S += ObjCEncodingForPrimitiveKind(Ctx, T); |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4395 | } |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4396 | S += llvm::utostr(FD->getBitWidthValue(*Ctx)); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4397 | } |
| 4398 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 4399 | // FIXME: Use SmallString for accumulating string. |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4400 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 4401 | bool ExpandPointedToStructures, |
| 4402 | bool ExpandStructures, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 4403 | const FieldDecl *FD, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4404 | bool OutermostType, |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4405 | bool EncodingProperty, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4406 | bool StructField, |
| 4407 | bool EncodeBlockParameters, |
| 4408 | bool EncodeClassNames) const { |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4409 | if (T->getAs<BuiltinType>()) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4410 | if (FD && FD->isBitField()) |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4411 | return EncodeBitField(this, S, T, FD); |
| 4412 | S += ObjCEncodingForPrimitiveKind(this, T); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4413 | return; |
| 4414 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4415 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4416 | if (const ComplexType *CT = T->getAs<ComplexType>()) { |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 4417 | S += 'j'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4418 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false, |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 4419 | false); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4420 | return; |
| 4421 | } |
Fariborz Jahanian | 60bce3e | 2009-11-23 20:40:50 +0000 | [diff] [blame] | 4422 | |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4423 | // encoding for pointer or r3eference types. |
| 4424 | QualType PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4425 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Fariborz Jahanian | 8d2c0a9 | 2009-11-30 18:43:52 +0000 | [diff] [blame] | 4426 | if (PT->isObjCSelType()) { |
| 4427 | S += ':'; |
| 4428 | return; |
| 4429 | } |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4430 | PointeeTy = PT->getPointeeType(); |
| 4431 | } |
| 4432 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 4433 | PointeeTy = RT->getPointeeType(); |
| 4434 | if (!PointeeTy.isNull()) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4435 | bool isReadOnly = false; |
| 4436 | // For historical/compatibility reasons, the read-only qualifier of the |
| 4437 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 4438 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4439 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 4440 | if (isa<TypedefType>(T.getTypePtr())) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4441 | if (OutermostType && T.isConstQualified()) { |
| 4442 | isReadOnly = true; |
| 4443 | S += 'r'; |
| 4444 | } |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 4445 | } else if (OutermostType) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4446 | QualType P = PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4447 | while (P->getAs<PointerType>()) |
| 4448 | P = P->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4449 | if (P.isConstQualified()) { |
| 4450 | isReadOnly = true; |
| 4451 | S += 'r'; |
| 4452 | } |
| 4453 | } |
| 4454 | if (isReadOnly) { |
| 4455 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 4456 | // combinations need to be rearranged. |
| 4457 | // Rewrite "in const" from "nr" to "rn" |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4458 | if (StringRef(S).endswith("nr")) |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 4459 | S.replace(S.end()-2, S.end(), "rn"); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4460 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4461 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4462 | if (PointeeTy->isCharType()) { |
| 4463 | // char pointer types should be encoded as '*' unless it is a |
| 4464 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 4465 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4466 | S += '*'; |
| 4467 | return; |
| 4468 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4469 | } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) { |
Steve Naroff | 9533a7f | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 4470 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 4471 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 4472 | S += '#'; |
| 4473 | return; |
| 4474 | } |
| 4475 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 4476 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 4477 | S += '@'; |
| 4478 | return; |
| 4479 | } |
| 4480 | // fall through... |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4481 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4482 | S += '^'; |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4483 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 4484 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4485 | getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4486 | NULL); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4487 | return; |
| 4488 | } |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4489 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4490 | if (const ArrayType *AT = |
| 4491 | // Ignore type qualifiers etc. |
| 4492 | dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) { |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4493 | if (isa<IncompleteArrayType>(AT) && !StructField) { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4494 | // Incomplete arrays are encoded as a pointer to the array element. |
| 4495 | S += '^'; |
| 4496 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4497 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4498 | false, ExpandStructures, FD); |
| 4499 | } else { |
| 4500 | S += '['; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4501 | |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4502 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
| 4503 | if (getTypeSize(CAT->getElementType()) == 0) |
| 4504 | S += '0'; |
| 4505 | else |
| 4506 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 4507 | } else { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4508 | //Variable length arrays are encoded as a regular array with 0 elements. |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4509 | assert((isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) && |
| 4510 | "Unknown array type!"); |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4511 | S += '0'; |
| 4512 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4513 | |
| 4514 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4515 | false, ExpandStructures, FD); |
| 4516 | S += ']'; |
| 4517 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4518 | return; |
| 4519 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4520 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4521 | if (T->getAs<FunctionType>()) { |
Anders Carlsson | c0a87b7 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 4522 | S += '?'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4523 | return; |
| 4524 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4525 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4526 | if (const RecordType *RTy = T->getAs<RecordType>()) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4527 | RecordDecl *RDecl = RTy->getDecl(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4528 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 4529 | // Anonymous structures print as '?' |
| 4530 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 4531 | S += II->getName(); |
Fariborz Jahanian | 6fb9439 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 4532 | if (ClassTemplateSpecializationDecl *Spec |
| 4533 | = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) { |
| 4534 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 4535 | std::string TemplateArgsStr |
| 4536 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4537 | TemplateArgs.data(), |
| 4538 | TemplateArgs.size(), |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 4539 | (*this).getPrintingPolicy()); |
Fariborz Jahanian | 6fb9439 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 4540 | |
| 4541 | S += TemplateArgsStr; |
| 4542 | } |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 4543 | } else { |
| 4544 | S += '?'; |
| 4545 | } |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 4546 | if (ExpandStructures) { |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4547 | S += '='; |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4548 | if (!RDecl->isUnion()) { |
| 4549 | getObjCEncodingForStructureImpl(RDecl, S, FD); |
| 4550 | } else { |
| 4551 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 4552 | FieldEnd = RDecl->field_end(); |
| 4553 | Field != FieldEnd; ++Field) { |
| 4554 | if (FD) { |
| 4555 | S += '"'; |
| 4556 | S += Field->getNameAsString(); |
| 4557 | S += '"'; |
| 4558 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4559 | |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4560 | // Special case bit-fields. |
| 4561 | if (Field->isBitField()) { |
| 4562 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
| 4563 | (*Field)); |
| 4564 | } else { |
| 4565 | QualType qt = Field->getType(); |
| 4566 | getLegacyIntegralTypeEncoding(qt); |
| 4567 | getObjCEncodingForTypeImpl(qt, S, false, true, |
| 4568 | FD, /*OutermostType*/false, |
| 4569 | /*EncodingProperty*/false, |
| 4570 | /*StructField*/true); |
| 4571 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4572 | } |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4573 | } |
Fariborz Jahanian | 6de88a8 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 4574 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4575 | S += RDecl->isUnion() ? ')' : '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4576 | return; |
| 4577 | } |
Fariborz Jahanian | e6012c7 | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 4578 | |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4579 | if (const EnumType *ET = T->getAs<EnumType>()) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4580 | if (FD && FD->isBitField()) |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4581 | EncodeBitField(this, S, T, FD); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4582 | else |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4583 | S += ObjCEncodingForEnumType(this, ET); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4584 | return; |
| 4585 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4586 | |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4587 | if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) { |
Steve Naroff | 21a98b1 | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 4588 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4589 | if (EncodeBlockParameters) { |
| 4590 | const FunctionType *FT = BT->getPointeeType()->getAs<FunctionType>(); |
| 4591 | |
| 4592 | S += '<'; |
| 4593 | // Block return type |
| 4594 | getObjCEncodingForTypeImpl(FT->getResultType(), S, |
| 4595 | ExpandPointedToStructures, ExpandStructures, |
| 4596 | FD, |
| 4597 | false /* OutermostType */, |
| 4598 | EncodingProperty, |
| 4599 | false /* StructField */, |
| 4600 | EncodeBlockParameters, |
| 4601 | EncodeClassNames); |
| 4602 | // Block self |
| 4603 | S += "@?"; |
| 4604 | // Block parameters |
| 4605 | if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) { |
| 4606 | for (FunctionProtoType::arg_type_iterator I = FPT->arg_type_begin(), |
| 4607 | E = FPT->arg_type_end(); I && (I != E); ++I) { |
| 4608 | getObjCEncodingForTypeImpl(*I, S, |
| 4609 | ExpandPointedToStructures, |
| 4610 | ExpandStructures, |
| 4611 | FD, |
| 4612 | false /* OutermostType */, |
| 4613 | EncodingProperty, |
| 4614 | false /* StructField */, |
| 4615 | EncodeBlockParameters, |
| 4616 | EncodeClassNames); |
| 4617 | } |
| 4618 | } |
| 4619 | S += '>'; |
| 4620 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4621 | return; |
| 4622 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4623 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4624 | // Ignore protocol qualifiers when mangling at this level. |
| 4625 | if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>()) |
| 4626 | T = OT->getBaseType(); |
| 4627 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4628 | if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4629 | // @encode(class_name) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4630 | ObjCInterfaceDecl *OI = OIT->getDecl(); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4631 | S += '{'; |
| 4632 | const IdentifierInfo *II = OI->getIdentifier(); |
| 4633 | S += II->getName(); |
| 4634 | S += '='; |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4635 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4636 | DeepCollectObjCIvars(OI, true, Ivars); |
| 4637 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) { |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4638 | const FieldDecl *Field = cast<FieldDecl>(Ivars[i]); |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4639 | if (Field->isBitField()) |
| 4640 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4641 | else |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4642 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4643 | } |
| 4644 | S += '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4645 | return; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4646 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4647 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4648 | if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4649 | if (OPT->isObjCIdType()) { |
| 4650 | S += '@'; |
| 4651 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4652 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4653 | |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4654 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
| 4655 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
| 4656 | // Since this is a binary compatibility issue, need to consult with runtime |
| 4657 | // folks. Fortunately, this is a *very* obsure construct. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4658 | S += '#'; |
| 4659 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4660 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4661 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4662 | if (OPT->isObjCQualifiedIdType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4663 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4664 | ExpandPointedToStructures, |
| 4665 | ExpandStructures, FD); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4666 | if (FD || EncodingProperty || EncodeClassNames) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4667 | // Note that we do extended encoding of protocol qualifer list |
| 4668 | // Only when doing ivar or property encoding. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4669 | S += '"'; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4670 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 4671 | E = OPT->qual_end(); I != E; ++I) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4672 | S += '<'; |
| 4673 | S += (*I)->getNameAsString(); |
| 4674 | S += '>'; |
| 4675 | } |
| 4676 | S += '"'; |
| 4677 | } |
| 4678 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4679 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4680 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4681 | QualType PointeeTy = OPT->getPointeeType(); |
| 4682 | if (!EncodingProperty && |
| 4683 | isa<TypedefType>(PointeeTy.getTypePtr())) { |
| 4684 | // Another historical/compatibility reason. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4685 | // We encode the underlying type which comes out as |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4686 | // {...}; |
| 4687 | S += '^'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4688 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 4689 | false, ExpandPointedToStructures, |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4690 | NULL); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4691 | return; |
| 4692 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4693 | |
| 4694 | S += '@'; |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4695 | if (OPT->getInterfaceDecl() && |
| 4696 | (FD || EncodingProperty || EncodeClassNames)) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4697 | S += '"'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4698 | S += OPT->getInterfaceDecl()->getIdentifier()->getName(); |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4699 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 4700 | E = OPT->qual_end(); I != E; ++I) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4701 | S += '<'; |
| 4702 | S += (*I)->getNameAsString(); |
| 4703 | S += '>'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4704 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4705 | S += '"'; |
| 4706 | } |
| 4707 | return; |
| 4708 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4709 | |
John McCall | 532ec7b | 2010-05-17 23:56:34 +0000 | [diff] [blame] | 4710 | // gcc just blithely ignores member pointers. |
| 4711 | // TODO: maybe there should be a mangling for these |
| 4712 | if (T->getAs<MemberPointerType>()) |
| 4713 | return; |
Fariborz Jahanian | e6012c7 | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 4714 | |
| 4715 | if (T->isVectorType()) { |
| 4716 | // This matches gcc's encoding, even though technically it is |
| 4717 | // insufficient. |
| 4718 | // FIXME. We should do a better job than gcc. |
| 4719 | return; |
| 4720 | } |
| 4721 | |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4722 | llvm_unreachable("@encode for type not implemented!"); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4723 | } |
| 4724 | |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4725 | void ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl, |
| 4726 | std::string &S, |
| 4727 | const FieldDecl *FD, |
| 4728 | bool includeVBases) const { |
| 4729 | assert(RDecl && "Expected non-null RecordDecl"); |
| 4730 | assert(!RDecl->isUnion() && "Should not be called for unions"); |
| 4731 | if (!RDecl->getDefinition()) |
| 4732 | return; |
| 4733 | |
| 4734 | CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(RDecl); |
| 4735 | std::multimap<uint64_t, NamedDecl *> FieldOrBaseOffsets; |
| 4736 | const ASTRecordLayout &layout = getASTRecordLayout(RDecl); |
| 4737 | |
| 4738 | if (CXXRec) { |
| 4739 | for (CXXRecordDecl::base_class_iterator |
| 4740 | BI = CXXRec->bases_begin(), |
| 4741 | BE = CXXRec->bases_end(); BI != BE; ++BI) { |
| 4742 | if (!BI->isVirtual()) { |
| 4743 | CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 829f200 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 4744 | if (base->isEmpty()) |
| 4745 | continue; |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4746 | uint64_t offs = layout.getBaseClassOffsetInBits(base); |
| 4747 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 4748 | std::make_pair(offs, base)); |
| 4749 | } |
| 4750 | } |
| 4751 | } |
| 4752 | |
| 4753 | unsigned i = 0; |
| 4754 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 4755 | FieldEnd = RDecl->field_end(); |
| 4756 | Field != FieldEnd; ++Field, ++i) { |
| 4757 | uint64_t offs = layout.getFieldOffset(i); |
| 4758 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 4759 | std::make_pair(offs, *Field)); |
| 4760 | } |
| 4761 | |
| 4762 | if (CXXRec && includeVBases) { |
| 4763 | for (CXXRecordDecl::base_class_iterator |
| 4764 | BI = CXXRec->vbases_begin(), |
| 4765 | BE = CXXRec->vbases_end(); BI != BE; ++BI) { |
| 4766 | CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 829f200 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 4767 | if (base->isEmpty()) |
| 4768 | continue; |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4769 | uint64_t offs = layout.getVBaseClassOffsetInBits(base); |
Argyrios Kyrtzidis | 19aa860 | 2011-09-26 18:14:24 +0000 | [diff] [blame] | 4770 | if (FieldOrBaseOffsets.find(offs) == FieldOrBaseOffsets.end()) |
| 4771 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.end(), |
| 4772 | std::make_pair(offs, base)); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4773 | } |
| 4774 | } |
| 4775 | |
| 4776 | CharUnits size; |
| 4777 | if (CXXRec) { |
| 4778 | size = includeVBases ? layout.getSize() : layout.getNonVirtualSize(); |
| 4779 | } else { |
| 4780 | size = layout.getSize(); |
| 4781 | } |
| 4782 | |
| 4783 | uint64_t CurOffs = 0; |
| 4784 | std::multimap<uint64_t, NamedDecl *>::iterator |
| 4785 | CurLayObj = FieldOrBaseOffsets.begin(); |
| 4786 | |
Argyrios Kyrtzidis | cb8061e | 2011-08-22 16:03:14 +0000 | [diff] [blame] | 4787 | if ((CurLayObj != FieldOrBaseOffsets.end() && CurLayObj->first != 0) || |
| 4788 | (CurLayObj == FieldOrBaseOffsets.end() && |
| 4789 | CXXRec && CXXRec->isDynamicClass())) { |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4790 | assert(CXXRec && CXXRec->isDynamicClass() && |
| 4791 | "Offset 0 was empty but no VTable ?"); |
| 4792 | if (FD) { |
| 4793 | S += "\"_vptr$"; |
| 4794 | std::string recname = CXXRec->getNameAsString(); |
| 4795 | if (recname.empty()) recname = "?"; |
| 4796 | S += recname; |
| 4797 | S += '"'; |
| 4798 | } |
| 4799 | S += "^^?"; |
| 4800 | CurOffs += getTypeSize(VoidPtrTy); |
| 4801 | } |
| 4802 | |
| 4803 | if (!RDecl->hasFlexibleArrayMember()) { |
| 4804 | // Mark the end of the structure. |
| 4805 | uint64_t offs = toBits(size); |
| 4806 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 4807 | std::make_pair(offs, (NamedDecl*)0)); |
| 4808 | } |
| 4809 | |
| 4810 | for (; CurLayObj != FieldOrBaseOffsets.end(); ++CurLayObj) { |
| 4811 | assert(CurOffs <= CurLayObj->first); |
| 4812 | |
| 4813 | if (CurOffs < CurLayObj->first) { |
| 4814 | uint64_t padding = CurLayObj->first - CurOffs; |
| 4815 | // FIXME: There doesn't seem to be a way to indicate in the encoding that |
| 4816 | // packing/alignment of members is different that normal, in which case |
| 4817 | // the encoding will be out-of-sync with the real layout. |
| 4818 | // If the runtime switches to just consider the size of types without |
| 4819 | // taking into account alignment, we could make padding explicit in the |
| 4820 | // encoding (e.g. using arrays of chars). The encoding strings would be |
| 4821 | // longer then though. |
| 4822 | CurOffs += padding; |
| 4823 | } |
| 4824 | |
| 4825 | NamedDecl *dcl = CurLayObj->second; |
| 4826 | if (dcl == 0) |
| 4827 | break; // reached end of structure. |
| 4828 | |
| 4829 | if (CXXRecordDecl *base = dyn_cast<CXXRecordDecl>(dcl)) { |
| 4830 | // We expand the bases without their virtual bases since those are going |
| 4831 | // in the initial structure. Note that this differs from gcc which |
| 4832 | // expands virtual bases each time one is encountered in the hierarchy, |
| 4833 | // making the encoding type bigger than it really is. |
| 4834 | getObjCEncodingForStructureImpl(base, S, FD, /*includeVBases*/false); |
Argyrios Kyrtzidis | 829f200 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 4835 | assert(!base->isEmpty()); |
| 4836 | CurOffs += toBits(getASTRecordLayout(base).getNonVirtualSize()); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4837 | } else { |
| 4838 | FieldDecl *field = cast<FieldDecl>(dcl); |
| 4839 | if (FD) { |
| 4840 | S += '"'; |
| 4841 | S += field->getNameAsString(); |
| 4842 | S += '"'; |
| 4843 | } |
| 4844 | |
| 4845 | if (field->isBitField()) { |
| 4846 | EncodeBitField(this, S, field->getType(), field); |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4847 | CurOffs += field->getBitWidthValue(*this); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4848 | } else { |
| 4849 | QualType qt = field->getType(); |
| 4850 | getLegacyIntegralTypeEncoding(qt); |
| 4851 | getObjCEncodingForTypeImpl(qt, S, false, true, FD, |
| 4852 | /*OutermostType*/false, |
| 4853 | /*EncodingProperty*/false, |
| 4854 | /*StructField*/true); |
| 4855 | CurOffs += getTypeSize(field->getType()); |
| 4856 | } |
| 4857 | } |
| 4858 | } |
| 4859 | } |
| 4860 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4861 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 4862 | std::string& S) const { |
| 4863 | if (QT & Decl::OBJC_TQ_In) |
| 4864 | S += 'n'; |
| 4865 | if (QT & Decl::OBJC_TQ_Inout) |
| 4866 | S += 'N'; |
| 4867 | if (QT & Decl::OBJC_TQ_Out) |
| 4868 | S += 'o'; |
| 4869 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 4870 | S += 'O'; |
| 4871 | if (QT & Decl::OBJC_TQ_Byref) |
| 4872 | S += 'R'; |
| 4873 | if (QT & Decl::OBJC_TQ_Oneway) |
| 4874 | S += 'V'; |
| 4875 | } |
| 4876 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4877 | void ASTContext::setBuiltinVaListType(QualType T) { |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 4878 | assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4879 | |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 4880 | BuiltinVaListType = T; |
| 4881 | } |
| 4882 | |
Douglas Gregor | 4dfd02a | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 4883 | TypedefDecl *ASTContext::getObjCIdDecl() const { |
| 4884 | if (!ObjCIdDecl) { |
| 4885 | QualType T = getObjCObjectType(ObjCBuiltinIdTy, 0, 0); |
| 4886 | T = getObjCObjectPointerType(T); |
| 4887 | TypeSourceInfo *IdInfo = getTrivialTypeSourceInfo(T); |
| 4888 | ObjCIdDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 4889 | getTranslationUnitDecl(), |
| 4890 | SourceLocation(), SourceLocation(), |
| 4891 | &Idents.get("id"), IdInfo); |
| 4892 | } |
| 4893 | |
| 4894 | return ObjCIdDecl; |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 4895 | } |
| 4896 | |
Douglas Gregor | 7a27ea5 | 2011-08-12 06:17:30 +0000 | [diff] [blame] | 4897 | TypedefDecl *ASTContext::getObjCSelDecl() const { |
| 4898 | if (!ObjCSelDecl) { |
| 4899 | QualType SelT = getPointerType(ObjCBuiltinSelTy); |
| 4900 | TypeSourceInfo *SelInfo = getTrivialTypeSourceInfo(SelT); |
| 4901 | ObjCSelDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 4902 | getTranslationUnitDecl(), |
| 4903 | SourceLocation(), SourceLocation(), |
| 4904 | &Idents.get("SEL"), SelInfo); |
| 4905 | } |
| 4906 | return ObjCSelDecl; |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 4907 | } |
| 4908 | |
Douglas Gregor | 79d6726 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 4909 | TypedefDecl *ASTContext::getObjCClassDecl() const { |
| 4910 | if (!ObjCClassDecl) { |
| 4911 | QualType T = getObjCObjectType(ObjCBuiltinClassTy, 0, 0); |
| 4912 | T = getObjCObjectPointerType(T); |
| 4913 | TypeSourceInfo *ClassInfo = getTrivialTypeSourceInfo(T); |
| 4914 | ObjCClassDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 4915 | getTranslationUnitDecl(), |
| 4916 | SourceLocation(), SourceLocation(), |
| 4917 | &Idents.get("Class"), ClassInfo); |
| 4918 | } |
| 4919 | |
| 4920 | return ObjCClassDecl; |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 4921 | } |
| 4922 | |
Douglas Gregor | a6ea10e | 2012-01-17 18:09:05 +0000 | [diff] [blame] | 4923 | ObjCInterfaceDecl *ASTContext::getObjCProtocolDecl() const { |
| 4924 | if (!ObjCProtocolClassDecl) { |
| 4925 | ObjCProtocolClassDecl |
| 4926 | = ObjCInterfaceDecl::Create(*this, getTranslationUnitDecl(), |
| 4927 | SourceLocation(), |
| 4928 | &Idents.get("Protocol"), |
| 4929 | /*PrevDecl=*/0, |
| 4930 | SourceLocation(), true); |
| 4931 | } |
| 4932 | |
| 4933 | return ObjCProtocolClassDecl; |
| 4934 | } |
| 4935 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4936 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4937 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 4938 | "'NSConstantString' type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4939 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4940 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 4941 | } |
| 4942 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4943 | /// \brief Retrieve the template name that corresponds to a non-empty |
| 4944 | /// lookup. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4945 | TemplateName |
| 4946 | ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin, |
| 4947 | UnresolvedSetIterator End) const { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4948 | unsigned size = End - Begin; |
| 4949 | assert(size > 1 && "set is not overloaded!"); |
| 4950 | |
| 4951 | void *memory = Allocate(sizeof(OverloadedTemplateStorage) + |
| 4952 | size * sizeof(FunctionTemplateDecl*)); |
| 4953 | OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size); |
| 4954 | |
| 4955 | NamedDecl **Storage = OT->getStorage(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4956 | for (UnresolvedSetIterator I = Begin; I != End; ++I) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4957 | NamedDecl *D = *I; |
| 4958 | assert(isa<FunctionTemplateDecl>(D) || |
| 4959 | (isa<UsingShadowDecl>(D) && |
| 4960 | isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))); |
| 4961 | *Storage++ = D; |
| 4962 | } |
| 4963 | |
| 4964 | return TemplateName(OT); |
| 4965 | } |
| 4966 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4967 | /// \brief Retrieve the template name that represents a qualified |
| 4968 | /// template name such as \c std::vector. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4969 | TemplateName |
| 4970 | ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
| 4971 | bool TemplateKeyword, |
| 4972 | TemplateDecl *Template) const { |
Douglas Gregor | 0f0ea2a | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 4973 | assert(NNS && "Missing nested-name-specifier in qualified template name"); |
| 4974 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4975 | // FIXME: Canonicalization? |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4976 | llvm::FoldingSetNodeID ID; |
| 4977 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 4978 | |
| 4979 | void *InsertPos = 0; |
| 4980 | QualifiedTemplateName *QTN = |
| 4981 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4982 | if (!QTN) { |
| 4983 | QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); |
| 4984 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 4985 | } |
| 4986 | |
| 4987 | return TemplateName(QTN); |
| 4988 | } |
| 4989 | |
| 4990 | /// \brief Retrieve the template name that represents a dependent |
| 4991 | /// template name such as \c MetaFun::template apply. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4992 | TemplateName |
| 4993 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
| 4994 | const IdentifierInfo *Name) const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4995 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 4996 | "Nested name specifier must be dependent"); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4997 | |
| 4998 | llvm::FoldingSetNodeID ID; |
| 4999 | DependentTemplateName::Profile(ID, NNS, Name); |
| 5000 | |
| 5001 | void *InsertPos = 0; |
| 5002 | DependentTemplateName *QTN = |
| 5003 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 5004 | |
| 5005 | if (QTN) |
| 5006 | return TemplateName(QTN); |
| 5007 | |
| 5008 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 5009 | if (CanonNNS == NNS) { |
| 5010 | QTN = new (*this,4) DependentTemplateName(NNS, Name); |
| 5011 | } else { |
| 5012 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
| 5013 | QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 5014 | DependentTemplateName *CheckQTN = |
| 5015 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 5016 | assert(!CheckQTN && "Dependent type name canonicalization broken"); |
| 5017 | (void)CheckQTN; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 5018 | } |
| 5019 | |
| 5020 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 5021 | return TemplateName(QTN); |
| 5022 | } |
| 5023 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 5024 | /// \brief Retrieve the template name that represents a dependent |
| 5025 | /// template name such as \c MetaFun::template operator+. |
| 5026 | TemplateName |
| 5027 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5028 | OverloadedOperatorKind Operator) const { |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 5029 | assert((!NNS || NNS->isDependent()) && |
| 5030 | "Nested name specifier must be dependent"); |
| 5031 | |
| 5032 | llvm::FoldingSetNodeID ID; |
| 5033 | DependentTemplateName::Profile(ID, NNS, Operator); |
| 5034 | |
| 5035 | void *InsertPos = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 5036 | DependentTemplateName *QTN |
| 5037 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 5038 | |
| 5039 | if (QTN) |
| 5040 | return TemplateName(QTN); |
| 5041 | |
| 5042 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 5043 | if (CanonNNS == NNS) { |
| 5044 | QTN = new (*this,4) DependentTemplateName(NNS, Operator); |
| 5045 | } else { |
| 5046 | TemplateName Canon = getDependentTemplateName(CanonNNS, Operator); |
| 5047 | QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 5048 | |
| 5049 | DependentTemplateName *CheckQTN |
| 5050 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 5051 | assert(!CheckQTN && "Dependent template name canonicalization broken"); |
| 5052 | (void)CheckQTN; |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 5053 | } |
| 5054 | |
| 5055 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 5056 | return TemplateName(QTN); |
| 5057 | } |
| 5058 | |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 5059 | TemplateName |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5060 | ASTContext::getSubstTemplateTemplateParm(TemplateTemplateParmDecl *param, |
| 5061 | TemplateName replacement) const { |
| 5062 | llvm::FoldingSetNodeID ID; |
| 5063 | SubstTemplateTemplateParmStorage::Profile(ID, param, replacement); |
| 5064 | |
| 5065 | void *insertPos = 0; |
| 5066 | SubstTemplateTemplateParmStorage *subst |
| 5067 | = SubstTemplateTemplateParms.FindNodeOrInsertPos(ID, insertPos); |
| 5068 | |
| 5069 | if (!subst) { |
| 5070 | subst = new (*this) SubstTemplateTemplateParmStorage(param, replacement); |
| 5071 | SubstTemplateTemplateParms.InsertNode(subst, insertPos); |
| 5072 | } |
| 5073 | |
| 5074 | return TemplateName(subst); |
| 5075 | } |
| 5076 | |
| 5077 | TemplateName |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 5078 | ASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param, |
| 5079 | const TemplateArgument &ArgPack) const { |
| 5080 | ASTContext &Self = const_cast<ASTContext &>(*this); |
| 5081 | llvm::FoldingSetNodeID ID; |
| 5082 | SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack); |
| 5083 | |
| 5084 | void *InsertPos = 0; |
| 5085 | SubstTemplateTemplateParmPackStorage *Subst |
| 5086 | = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos); |
| 5087 | |
| 5088 | if (!Subst) { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5089 | Subst = new (*this) SubstTemplateTemplateParmPackStorage(Param, |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 5090 | ArgPack.pack_size(), |
| 5091 | ArgPack.pack_begin()); |
| 5092 | SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos); |
| 5093 | } |
| 5094 | |
| 5095 | return TemplateName(Subst); |
| 5096 | } |
| 5097 | |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 5098 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | d934112 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 5099 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 5100 | /// is actually a value of type @c TargetInfo::IntType. |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 5101 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 5102 | switch (Type) { |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 5103 | case TargetInfo::NoInt: return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 5104 | case TargetInfo::SignedShort: return ShortTy; |
| 5105 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 5106 | case TargetInfo::SignedInt: return IntTy; |
| 5107 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 5108 | case TargetInfo::SignedLong: return LongTy; |
| 5109 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 5110 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 5111 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 5112 | } |
| 5113 | |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5114 | llvm_unreachable("Unhandled TargetInfo::IntType value"); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 5115 | } |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 5116 | |
| 5117 | //===----------------------------------------------------------------------===// |
| 5118 | // Type Predicates. |
| 5119 | //===----------------------------------------------------------------------===// |
| 5120 | |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 5121 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 5122 | /// garbage collection attribute. |
| 5123 | /// |
John McCall | ae278a3 | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 5124 | Qualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5125 | if (getLangOpts().getGC() == LangOptions::NonGC) |
John McCall | ae278a3 | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 5126 | return Qualifiers::GCNone; |
| 5127 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5128 | assert(getLangOpts().ObjC1); |
John McCall | ae278a3 | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 5129 | Qualifiers::GC GCAttrs = Ty.getObjCGCAttr(); |
| 5130 | |
| 5131 | // Default behaviour under objective-C's gc is for ObjC pointers |
| 5132 | // (or pointers to them) be treated as though they were declared |
| 5133 | // as __strong. |
| 5134 | if (GCAttrs == Qualifiers::GCNone) { |
| 5135 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
| 5136 | return Qualifiers::Strong; |
| 5137 | else if (Ty->isPointerType()) |
| 5138 | return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); |
| 5139 | } else { |
| 5140 | // It's not valid to set GC attributes on anything that isn't a |
| 5141 | // pointer. |
| 5142 | #ifndef NDEBUG |
| 5143 | QualType CT = Ty->getCanonicalTypeInternal(); |
| 5144 | while (const ArrayType *AT = dyn_cast<ArrayType>(CT)) |
| 5145 | CT = AT->getElementType(); |
| 5146 | assert(CT->isAnyPointerType() || CT->isBlockPointerType()); |
| 5147 | #endif |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 5148 | } |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 5149 | return GCAttrs; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 5150 | } |
| 5151 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5152 | //===----------------------------------------------------------------------===// |
| 5153 | // Type Compatibility Testing |
| 5154 | //===----------------------------------------------------------------------===// |
Chris Lattner | 770951b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 5155 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5156 | /// areCompatVectorTypes - Return true if the two specified vector types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5157 | /// compatible. |
| 5158 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 5159 | const VectorType *RHS) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 5160 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5161 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5162 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5163 | } |
| 5164 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5165 | bool ASTContext::areCompatibleVectorTypes(QualType FirstVec, |
| 5166 | QualType SecondVec) { |
| 5167 | assert(FirstVec->isVectorType() && "FirstVec should be a vector type"); |
| 5168 | assert(SecondVec->isVectorType() && "SecondVec should be a vector type"); |
| 5169 | |
| 5170 | if (hasSameUnqualifiedType(FirstVec, SecondVec)) |
| 5171 | return true; |
| 5172 | |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 5173 | // Treat Neon vector types and most AltiVec vector types as if they are the |
| 5174 | // equivalent GCC vector types. |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5175 | const VectorType *First = FirstVec->getAs<VectorType>(); |
| 5176 | const VectorType *Second = SecondVec->getAs<VectorType>(); |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 5177 | if (First->getNumElements() == Second->getNumElements() && |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5178 | hasSameType(First->getElementType(), Second->getElementType()) && |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 5179 | First->getVectorKind() != VectorType::AltiVecPixel && |
| 5180 | First->getVectorKind() != VectorType::AltiVecBool && |
| 5181 | Second->getVectorKind() != VectorType::AltiVecPixel && |
| 5182 | Second->getVectorKind() != VectorType::AltiVecBool) |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5183 | return true; |
| 5184 | |
| 5185 | return false; |
| 5186 | } |
| 5187 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5188 | //===----------------------------------------------------------------------===// |
| 5189 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 5190 | //===----------------------------------------------------------------------===// |
| 5191 | |
| 5192 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 5193 | /// inheritance hierarchy of 'rProto'. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5194 | bool |
| 5195 | ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 5196 | ObjCProtocolDecl *rProto) const { |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 5197 | if (declaresSameEntity(lProto, rProto)) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5198 | return true; |
| 5199 | for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), |
| 5200 | E = rProto->protocol_end(); PI != E; ++PI) |
| 5201 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 5202 | return true; |
| 5203 | return false; |
| 5204 | } |
| 5205 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5206 | /// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...> |
| 5207 | /// return true if lhs's protocols conform to rhs's protocol; false |
| 5208 | /// otherwise. |
| 5209 | bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) { |
| 5210 | if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType()) |
| 5211 | return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false); |
| 5212 | return false; |
| 5213 | } |
| 5214 | |
Fariborz Jahanian | a8f8dac | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 5215 | /// ObjCQualifiedClassTypesAreCompatible - compare Class<p,...> and |
| 5216 | /// Class<p1, ...>. |
| 5217 | bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs, |
| 5218 | QualType rhs) { |
| 5219 | const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>(); |
| 5220 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
| 5221 | assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible"); |
| 5222 | |
| 5223 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5224 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5225 | bool match = false; |
| 5226 | ObjCProtocolDecl *lhsProto = *I; |
| 5227 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 5228 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 5229 | ObjCProtocolDecl *rhsProto = *J; |
| 5230 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) { |
| 5231 | match = true; |
| 5232 | break; |
| 5233 | } |
| 5234 | } |
| 5235 | if (!match) |
| 5236 | return false; |
| 5237 | } |
| 5238 | return true; |
| 5239 | } |
| 5240 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5241 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 5242 | /// ObjCQualifiedIDType. |
| 5243 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 5244 | bool compare) { |
| 5245 | // Allow id<P..> and an 'id' or void* type in all cases. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5246 | if (lhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5247 | lhs->isObjCIdType() || lhs->isObjCClassType()) |
| 5248 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5249 | else if (rhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5250 | rhs->isObjCIdType() || rhs->isObjCClassType()) |
| 5251 | return true; |
| 5252 | |
| 5253 | if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5254 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5255 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5256 | if (!rhsOPT) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5257 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5258 | if (rhsOPT->qual_empty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5259 | // If the RHS is a unqualified interface pointer "NSString*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5260 | // make sure we check the class hierarchy. |
| 5261 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 5262 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5263 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5264 | // when comparing an id<P> on lhs with a static type on rhs, |
| 5265 | // see if static class implements all of id's protocols, directly or |
| 5266 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 5267 | if (!rhsID->ClassImplementsProtocol(*I, true)) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5268 | return false; |
| 5269 | } |
| 5270 | } |
| 5271 | // If there are no qualifiers and no interface, we have an 'id'. |
| 5272 | return true; |
| 5273 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5274 | // Both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5275 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5276 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5277 | ObjCProtocolDecl *lhsProto = *I; |
| 5278 | bool match = false; |
| 5279 | |
| 5280 | // when comparing an id<P> on lhs with a static type on rhs, |
| 5281 | // see if static class implements all of id's protocols, directly or |
| 5282 | // through its super class and categories. |
| 5283 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 5284 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 5285 | ObjCProtocolDecl *rhsProto = *J; |
| 5286 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 5287 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 5288 | match = true; |
| 5289 | break; |
| 5290 | } |
| 5291 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5292 | // If the RHS is a qualified interface pointer "NSString<P>*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5293 | // make sure we check the class hierarchy. |
| 5294 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 5295 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5296 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5297 | // when comparing an id<P> on lhs with a static type on rhs, |
| 5298 | // see if static class implements all of id's protocols, directly or |
| 5299 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 5300 | if (rhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5301 | match = true; |
| 5302 | break; |
| 5303 | } |
| 5304 | } |
| 5305 | } |
| 5306 | if (!match) |
| 5307 | return false; |
| 5308 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5309 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5310 | return true; |
| 5311 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5312 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5313 | const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 5314 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 5315 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5316 | if (const ObjCObjectPointerType *lhsOPT = |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5317 | lhs->getAsObjCInterfacePointerType()) { |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5318 | // If both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5319 | for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(), |
| 5320 | E = lhsOPT->qual_end(); I != E; ++I) { |
| 5321 | ObjCProtocolDecl *lhsProto = *I; |
| 5322 | bool match = false; |
| 5323 | |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5324 | // 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] | 5325 | // see if static class implements all of id's protocols, directly or |
| 5326 | // through its super class and categories. |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5327 | // First, lhs protocols in the qualifier list must be found, direct |
| 5328 | // or indirect in rhs's qualifier list or it is a mismatch. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5329 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 5330 | E = rhsQID->qual_end(); J != E; ++J) { |
| 5331 | ObjCProtocolDecl *rhsProto = *J; |
| 5332 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 5333 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 5334 | match = true; |
| 5335 | break; |
| 5336 | } |
| 5337 | } |
| 5338 | if (!match) |
| 5339 | return false; |
| 5340 | } |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5341 | |
| 5342 | // Static class's protocols, or its super class or category protocols |
| 5343 | // must be found, direct or indirect in rhs's qualifier list or it is a mismatch. |
| 5344 | if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { |
| 5345 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
| 5346 | CollectInheritedProtocols(lhsID, LHSInheritedProtocols); |
| 5347 | // This is rather dubious but matches gcc's behavior. If lhs has |
| 5348 | // no type qualifier and its class has no static protocol(s) |
| 5349 | // assume that it is mismatch. |
| 5350 | if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty()) |
| 5351 | return false; |
| 5352 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 5353 | LHSInheritedProtocols.begin(), |
| 5354 | E = LHSInheritedProtocols.end(); I != E; ++I) { |
| 5355 | bool match = false; |
| 5356 | ObjCProtocolDecl *lhsProto = (*I); |
| 5357 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 5358 | E = rhsQID->qual_end(); J != E; ++J) { |
| 5359 | ObjCProtocolDecl *rhsProto = *J; |
| 5360 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 5361 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 5362 | match = true; |
| 5363 | break; |
| 5364 | } |
| 5365 | } |
| 5366 | if (!match) |
| 5367 | return false; |
| 5368 | } |
| 5369 | } |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5370 | return true; |
| 5371 | } |
| 5372 | return false; |
| 5373 | } |
| 5374 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5375 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5376 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 5377 | /// protocol qualifiers on the LHS or RHS. |
| 5378 | /// |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5379 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 5380 | const ObjCObjectPointerType *RHSOPT) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5381 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 5382 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 5383 | |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 5384 | // 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] | 5385 | if (LHS->isObjCUnqualifiedIdOrClass() || |
| 5386 | RHS->isObjCUnqualifiedIdOrClass()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5387 | return true; |
| 5388 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5389 | if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5390 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 5391 | QualType(RHSOPT,0), |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5392 | false); |
Fariborz Jahanian | a8f8dac | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 5393 | |
| 5394 | if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass()) |
| 5395 | return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0), |
| 5396 | QualType(RHSOPT,0)); |
| 5397 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5398 | // If we have 2 user-defined types, fall into that path. |
| 5399 | if (LHS->getInterface() && RHS->getInterface()) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5400 | return canAssignObjCInterfaces(LHS, RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5401 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5402 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5403 | } |
| 5404 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5405 | /// canAssignObjCInterfacesInBlockPointer - This routine is specifically written |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 5406 | /// for providing type-safety for objective-c pointers used to pass/return |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5407 | /// arguments in block literals. When passed as arguments, passing 'A*' where |
| 5408 | /// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is |
| 5409 | /// not OK. For the return type, the opposite is not OK. |
| 5410 | bool ASTContext::canAssignObjCInterfacesInBlockPointer( |
| 5411 | const ObjCObjectPointerType *LHSOPT, |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5412 | const ObjCObjectPointerType *RHSOPT, |
| 5413 | bool BlockReturnType) { |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 5414 | if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5415 | return true; |
| 5416 | |
| 5417 | if (LHSOPT->isObjCBuiltinType()) { |
| 5418 | return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType(); |
| 5419 | } |
| 5420 | |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 5421 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5422 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 5423 | QualType(RHSOPT,0), |
| 5424 | false); |
| 5425 | |
| 5426 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 5427 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 5428 | if (LHS && RHS) { // We have 2 user-defined types. |
| 5429 | if (LHS != RHS) { |
| 5430 | if (LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5431 | return BlockReturnType; |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5432 | if (RHS->getDecl()->isSuperClassOf(LHS->getDecl())) |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5433 | return !BlockReturnType; |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5434 | } |
| 5435 | else |
| 5436 | return true; |
| 5437 | } |
| 5438 | return false; |
| 5439 | } |
| 5440 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5441 | /// getIntersectionOfProtocols - This routine finds the intersection of set |
| 5442 | /// of protocols inherited from two distinct objective-c pointer objects. |
| 5443 | /// It is used to build composite qualifier list of the composite type of |
| 5444 | /// the conditional expression involving two objective-c pointer objects. |
| 5445 | static |
| 5446 | void getIntersectionOfProtocols(ASTContext &Context, |
| 5447 | const ObjCObjectPointerType *LHSOPT, |
| 5448 | const ObjCObjectPointerType *RHSOPT, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5449 | SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5450 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5451 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 5452 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 5453 | assert(LHS->getInterface() && "LHS must have an interface base"); |
| 5454 | assert(RHS->getInterface() && "RHS must have an interface base"); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5455 | |
| 5456 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet; |
| 5457 | unsigned LHSNumProtocols = LHS->getNumProtocols(); |
| 5458 | if (LHSNumProtocols > 0) |
| 5459 | InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end()); |
| 5460 | else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 5461 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5462 | Context.CollectInheritedProtocols(LHS->getInterface(), |
| 5463 | LHSInheritedProtocols); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5464 | InheritedProtocolSet.insert(LHSInheritedProtocols.begin(), |
| 5465 | LHSInheritedProtocols.end()); |
| 5466 | } |
| 5467 | |
| 5468 | unsigned RHSNumProtocols = RHS->getNumProtocols(); |
| 5469 | if (RHSNumProtocols > 0) { |
Dan Gohman | cb421fa | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 5470 | ObjCProtocolDecl **RHSProtocols = |
| 5471 | const_cast<ObjCProtocolDecl **>(RHS->qual_begin()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5472 | for (unsigned i = 0; i < RHSNumProtocols; ++i) |
| 5473 | if (InheritedProtocolSet.count(RHSProtocols[i])) |
| 5474 | IntersectionOfProtocols.push_back(RHSProtocols[i]); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 5475 | } else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 5476 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5477 | Context.CollectInheritedProtocols(RHS->getInterface(), |
| 5478 | RHSInheritedProtocols); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 5479 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 5480 | RHSInheritedProtocols.begin(), |
| 5481 | E = RHSInheritedProtocols.end(); I != E; ++I) |
| 5482 | if (InheritedProtocolSet.count((*I))) |
| 5483 | IntersectionOfProtocols.push_back((*I)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5484 | } |
| 5485 | } |
| 5486 | |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 5487 | /// areCommonBaseCompatible - Returns common base class of the two classes if |
| 5488 | /// one found. Note that this is O'2 algorithm. But it will be called as the |
| 5489 | /// last type comparison in a ?-exp of ObjC pointer types before a |
| 5490 | /// warning is issued. So, its invokation is extremely rare. |
| 5491 | QualType ASTContext::areCommonBaseCompatible( |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5492 | const ObjCObjectPointerType *Lptr, |
| 5493 | const ObjCObjectPointerType *Rptr) { |
| 5494 | const ObjCObjectType *LHS = Lptr->getObjectType(); |
| 5495 | const ObjCObjectType *RHS = Rptr->getObjectType(); |
| 5496 | const ObjCInterfaceDecl* LDecl = LHS->getInterface(); |
| 5497 | const ObjCInterfaceDecl* RDecl = RHS->getInterface(); |
Douglas Gregor | 60ef308 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 5498 | if (!LDecl || !RDecl || (declaresSameEntity(LDecl, RDecl))) |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 5499 | return QualType(); |
| 5500 | |
Fariborz Jahanian | 7c2bdcb | 2011-04-18 21:16:59 +0000 | [diff] [blame] | 5501 | do { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5502 | LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5503 | if (canAssignObjCInterfaces(LHS, RHS)) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5504 | SmallVector<ObjCProtocolDecl *, 8> Protocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5505 | getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols); |
| 5506 | |
| 5507 | QualType Result = QualType(LHS, 0); |
| 5508 | if (!Protocols.empty()) |
| 5509 | Result = getObjCObjectType(Result, Protocols.data(), Protocols.size()); |
| 5510 | Result = getObjCObjectPointerType(Result); |
| 5511 | return Result; |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5512 | } |
Fariborz Jahanian | 7c2bdcb | 2011-04-18 21:16:59 +0000 | [diff] [blame] | 5513 | } while ((LDecl = LDecl->getSuperClass())); |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 5514 | |
| 5515 | return QualType(); |
| 5516 | } |
| 5517 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5518 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS, |
| 5519 | const ObjCObjectType *RHS) { |
| 5520 | assert(LHS->getInterface() && "LHS is not an interface type"); |
| 5521 | assert(RHS->getInterface() && "RHS is not an interface type"); |
| 5522 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5523 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 5524 | // the LHS. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5525 | if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface())) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5526 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5527 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5528 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 5529 | // protocol qualified at all, then we are good. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 5530 | if (LHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5531 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5532 | |
Fariborz Jahanian | b7bc34a | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 5533 | // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, |
| 5534 | // more detailed analysis is required. |
| 5535 | if (RHS->getNumProtocols() == 0) { |
| 5536 | // OK, if LHS is a superclass of RHS *and* |
| 5537 | // this superclass is assignment compatible with LHS. |
| 5538 | // false otherwise. |
Fariborz Jahanian | 627788c | 2011-04-12 16:34:14 +0000 | [diff] [blame] | 5539 | bool IsSuperClass = |
| 5540 | LHS->getInterface()->isSuperClassOf(RHS->getInterface()); |
| 5541 | if (IsSuperClass) { |
Fariborz Jahanian | b7bc34a | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 5542 | // OK if conversion of LHS to SuperClass results in narrowing of types |
| 5543 | // ; i.e., SuperClass may implement at least one of the protocols |
| 5544 | // in LHS's protocol list. Example, SuperObj<P1> = lhs<P1,P2> is ok. |
| 5545 | // But not SuperObj<P1,P2,P3> = lhs<P1,P2>. |
| 5546 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> SuperClassInheritedProtocols; |
Fariborz Jahanian | 627788c | 2011-04-12 16:34:14 +0000 | [diff] [blame] | 5547 | CollectInheritedProtocols(RHS->getInterface(), SuperClassInheritedProtocols); |
Fariborz Jahanian | b7bc34a | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 5548 | // If super class has no protocols, it is not a match. |
| 5549 | if (SuperClassInheritedProtocols.empty()) |
| 5550 | return false; |
| 5551 | |
| 5552 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 5553 | LHSPE = LHS->qual_end(); |
| 5554 | LHSPI != LHSPE; LHSPI++) { |
| 5555 | bool SuperImplementsProtocol = false; |
| 5556 | ObjCProtocolDecl *LHSProto = (*LHSPI); |
| 5557 | |
| 5558 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 5559 | SuperClassInheritedProtocols.begin(), |
| 5560 | E = SuperClassInheritedProtocols.end(); I != E; ++I) { |
| 5561 | ObjCProtocolDecl *SuperClassProto = (*I); |
| 5562 | if (SuperClassProto->lookupProtocolNamed(LHSProto->getIdentifier())) { |
| 5563 | SuperImplementsProtocol = true; |
| 5564 | break; |
| 5565 | } |
| 5566 | } |
| 5567 | if (!SuperImplementsProtocol) |
| 5568 | return false; |
| 5569 | } |
| 5570 | return true; |
| 5571 | } |
| 5572 | return false; |
| 5573 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5574 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5575 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 5576 | LHSPE = LHS->qual_end(); |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 5577 | LHSPI != LHSPE; LHSPI++) { |
| 5578 | bool RHSImplementsProtocol = false; |
| 5579 | |
| 5580 | // If the RHS doesn't implement the protocol on the left, the types |
| 5581 | // are incompatible. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5582 | for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(), |
| 5583 | RHSPE = RHS->qual_end(); |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 5584 | RHSPI != RHSPE; RHSPI++) { |
| 5585 | if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) { |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 5586 | RHSImplementsProtocol = true; |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 5587 | break; |
| 5588 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 5589 | } |
| 5590 | // FIXME: For better diagnostics, consider passing back the protocol name. |
| 5591 | if (!RHSImplementsProtocol) |
| 5592 | return false; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5593 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 5594 | // The RHS implements all protocols listed on the LHS. |
| 5595 | return true; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5596 | } |
| 5597 | |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 5598 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 5599 | // get the "pointed to" types |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5600 | const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
| 5601 | const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5602 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5603 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 5604 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5605 | |
| 5606 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 5607 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 5608 | } |
| 5609 | |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 5610 | bool ASTContext::canBindObjCObjectType(QualType To, QualType From) { |
| 5611 | return canAssignObjCInterfaces( |
| 5612 | getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(), |
| 5613 | getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>()); |
| 5614 | } |
| 5615 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5616 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5617 | /// both shall have the identically qualified version of a compatible type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5618 | /// 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] | 5619 | /// same. See 6.7.[2,3,5] for additional rules. |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5620 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS, |
| 5621 | bool CompareUnqualified) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5622 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 5623 | return hasSameType(LHS, RHS); |
| 5624 | |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5625 | return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5626 | } |
| 5627 | |
Fariborz Jahanian | c286f38 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 5628 | bool ASTContext::propertyTypesAreCompatible(QualType LHS, QualType RHS) { |
Fariborz Jahanian | 8237839 | 2011-07-12 23:20:13 +0000 | [diff] [blame] | 5629 | return typesAreCompatible(LHS, RHS); |
Fariborz Jahanian | c286f38 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 5630 | } |
| 5631 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5632 | bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) { |
| 5633 | return !mergeTypes(LHS, RHS, true).isNull(); |
| 5634 | } |
| 5635 | |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 5636 | /// mergeTransparentUnionType - if T is a transparent union type and a member |
| 5637 | /// of T is compatible with SubType, return the merged type, else return |
| 5638 | /// QualType() |
| 5639 | QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType, |
| 5640 | bool OfBlockPointer, |
| 5641 | bool Unqualified) { |
| 5642 | if (const RecordType *UT = T->getAsUnionType()) { |
| 5643 | RecordDecl *UD = UT->getDecl(); |
| 5644 | if (UD->hasAttr<TransparentUnionAttr>()) { |
| 5645 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 5646 | itend = UD->field_end(); it != itend; ++it) { |
Peter Collingbourne | f91d757 | 2010-12-02 21:00:06 +0000 | [diff] [blame] | 5647 | QualType ET = it->getType().getUnqualifiedType(); |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 5648 | QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified); |
| 5649 | if (!MT.isNull()) |
| 5650 | return MT; |
| 5651 | } |
| 5652 | } |
| 5653 | } |
| 5654 | |
| 5655 | return QualType(); |
| 5656 | } |
| 5657 | |
| 5658 | /// mergeFunctionArgumentTypes - merge two types which appear as function |
| 5659 | /// argument types |
| 5660 | QualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs, |
| 5661 | bool OfBlockPointer, |
| 5662 | bool Unqualified) { |
| 5663 | // GNU extension: two types are compatible if they appear as a function |
| 5664 | // argument, one of the types is a transparent union type and the other |
| 5665 | // type is compatible with a union member |
| 5666 | QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer, |
| 5667 | Unqualified); |
| 5668 | if (!lmerge.isNull()) |
| 5669 | return lmerge; |
| 5670 | |
| 5671 | QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer, |
| 5672 | Unqualified); |
| 5673 | if (!rmerge.isNull()) |
| 5674 | return rmerge; |
| 5675 | |
| 5676 | return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified); |
| 5677 | } |
| 5678 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5679 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs, |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5680 | bool OfBlockPointer, |
| 5681 | bool Unqualified) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5682 | const FunctionType *lbase = lhs->getAs<FunctionType>(); |
| 5683 | const FunctionType *rbase = rhs->getAs<FunctionType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5684 | const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 5685 | const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5686 | bool allLTypes = true; |
| 5687 | bool allRTypes = true; |
| 5688 | |
| 5689 | // Check return type |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5690 | QualType retType; |
Fariborz Jahanian | d263fd1 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 5691 | if (OfBlockPointer) { |
| 5692 | QualType RHS = rbase->getResultType(); |
| 5693 | QualType LHS = lbase->getResultType(); |
| 5694 | bool UnqualifiedResult = Unqualified; |
| 5695 | if (!UnqualifiedResult) |
| 5696 | UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers()); |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5697 | retType = mergeTypes(LHS, RHS, true, UnqualifiedResult, true); |
Fariborz Jahanian | d263fd1 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 5698 | } |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5699 | else |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5700 | retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), false, |
| 5701 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5702 | if (retType.isNull()) return QualType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5703 | |
| 5704 | if (Unqualified) |
| 5705 | retType = retType.getUnqualifiedType(); |
| 5706 | |
| 5707 | CanQualType LRetType = getCanonicalType(lbase->getResultType()); |
| 5708 | CanQualType RRetType = getCanonicalType(rbase->getResultType()); |
| 5709 | if (Unqualified) { |
| 5710 | LRetType = LRetType.getUnqualifiedType(); |
| 5711 | RRetType = RRetType.getUnqualifiedType(); |
| 5712 | } |
| 5713 | |
| 5714 | if (getCanonicalType(retType) != LRetType) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5715 | allLTypes = false; |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5716 | if (getCanonicalType(retType) != RRetType) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5717 | allRTypes = false; |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5718 | |
Daniel Dunbar | 6a15c85 | 2010-04-28 16:20:58 +0000 | [diff] [blame] | 5719 | // FIXME: double check this |
| 5720 | // FIXME: should we error if lbase->getRegParmAttr() != 0 && |
| 5721 | // rbase->getRegParmAttr() != 0 && |
| 5722 | // lbase->getRegParmAttr() != rbase->getRegParmAttr()? |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 5723 | FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo(); |
| 5724 | FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo(); |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5725 | |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 5726 | // Compatible functions must have compatible calling conventions |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5727 | if (!isSameCallConv(lbaseInfo.getCC(), rbaseInfo.getCC())) |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 5728 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5729 | |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5730 | // Regparm is part of the calling convention. |
Eli Friedman | a49218e | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 5731 | if (lbaseInfo.getHasRegParm() != rbaseInfo.getHasRegParm()) |
| 5732 | return QualType(); |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5733 | if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm()) |
| 5734 | return QualType(); |
| 5735 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5736 | if (lbaseInfo.getProducesResult() != rbaseInfo.getProducesResult()) |
| 5737 | return QualType(); |
| 5738 | |
Fariborz Jahanian | 53c8167 | 2011-10-05 00:05:34 +0000 | [diff] [blame] | 5739 | // functypes which return are preferred over those that do not. |
| 5740 | if (lbaseInfo.getNoReturn() && !rbaseInfo.getNoReturn()) |
| 5741 | allLTypes = false; |
| 5742 | else if (!lbaseInfo.getNoReturn() && rbaseInfo.getNoReturn()) |
| 5743 | allRTypes = false; |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5744 | // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'. |
| 5745 | bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn(); |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5746 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5747 | FunctionType::ExtInfo einfo = lbaseInfo.withNoReturn(NoReturn); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5748 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5749 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 5750 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 5751 | "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5752 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 5753 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 5754 | |
| 5755 | // Compatible functions must have the same number of arguments |
| 5756 | if (lproto_nargs != rproto_nargs) |
| 5757 | return QualType(); |
| 5758 | |
| 5759 | // Variadic and non-variadic functions aren't compatible |
| 5760 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 5761 | return QualType(); |
| 5762 | |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 5763 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 5764 | return QualType(); |
| 5765 | |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 5766 | if (LangOpts.ObjCAutoRefCount && |
| 5767 | !FunctionTypesMatchOnNSConsumedAttrs(rproto, lproto)) |
| 5768 | return QualType(); |
| 5769 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5770 | // Check argument compatibility |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5771 | SmallVector<QualType, 10> types; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5772 | for (unsigned i = 0; i < lproto_nargs; i++) { |
| 5773 | QualType largtype = lproto->getArgType(i).getUnqualifiedType(); |
| 5774 | QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 5775 | QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype, |
| 5776 | OfBlockPointer, |
| 5777 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5778 | if (argtype.isNull()) return QualType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5779 | |
| 5780 | if (Unqualified) |
| 5781 | argtype = argtype.getUnqualifiedType(); |
| 5782 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5783 | types.push_back(argtype); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5784 | if (Unqualified) { |
| 5785 | largtype = largtype.getUnqualifiedType(); |
| 5786 | rargtype = rargtype.getUnqualifiedType(); |
| 5787 | } |
| 5788 | |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5789 | if (getCanonicalType(argtype) != getCanonicalType(largtype)) |
| 5790 | allLTypes = false; |
| 5791 | if (getCanonicalType(argtype) != getCanonicalType(rargtype)) |
| 5792 | allRTypes = false; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5793 | } |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 5794 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5795 | if (allLTypes) return lhs; |
| 5796 | if (allRTypes) return rhs; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5797 | |
| 5798 | FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo(); |
| 5799 | EPI.ExtInfo = einfo; |
| 5800 | return getFunctionType(retType, types.begin(), types.size(), EPI); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5801 | } |
| 5802 | |
| 5803 | if (lproto) allRTypes = false; |
| 5804 | if (rproto) allLTypes = false; |
| 5805 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5806 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5807 | if (proto) { |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 5808 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5809 | if (proto->isVariadic()) return QualType(); |
| 5810 | // Check that the types are compatible with the types that |
| 5811 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 5812 | // The only types actually affected are promotable integer |
| 5813 | // types and floats, which would be passed as a different |
| 5814 | // type depending on whether the prototype is visible. |
| 5815 | unsigned proto_nargs = proto->getNumArgs(); |
| 5816 | for (unsigned i = 0; i < proto_nargs; ++i) { |
| 5817 | QualType argTy = proto->getArgType(i); |
Douglas Gregor | b0f8eac | 2010-02-03 19:27:29 +0000 | [diff] [blame] | 5818 | |
| 5819 | // Look at the promotion type of enum types, since that is the type used |
| 5820 | // to pass enum values. |
| 5821 | if (const EnumType *Enum = argTy->getAs<EnumType>()) |
| 5822 | argTy = Enum->getDecl()->getPromotionType(); |
| 5823 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5824 | if (argTy->isPromotableIntegerType() || |
| 5825 | getCanonicalType(argTy).getUnqualifiedType() == FloatTy) |
| 5826 | return QualType(); |
| 5827 | } |
| 5828 | |
| 5829 | if (allLTypes) return lhs; |
| 5830 | if (allRTypes) return rhs; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5831 | |
| 5832 | FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo(); |
| 5833 | EPI.ExtInfo = einfo; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5834 | return getFunctionType(retType, proto->arg_type_begin(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5835 | proto->getNumArgs(), EPI); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5836 | } |
| 5837 | |
| 5838 | if (allLTypes) return lhs; |
| 5839 | if (allRTypes) return rhs; |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5840 | return getFunctionNoProtoType(retType, einfo); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5841 | } |
| 5842 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5843 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5844 | bool OfBlockPointer, |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5845 | bool Unqualified, bool BlockReturnType) { |
Bill Wendling | 43d6975 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 5846 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 5847 | // type is adjusted to "T" prior to any further analysis, the expression |
| 5848 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 5849 | // expression is an lvalue unless the reference is an rvalue reference and |
| 5850 | // the expression is a function call (possibly inside parentheses). |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 5851 | assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?"); |
| 5852 | assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?"); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5853 | |
| 5854 | if (Unqualified) { |
| 5855 | LHS = LHS.getUnqualifiedType(); |
| 5856 | RHS = RHS.getUnqualifiedType(); |
| 5857 | } |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 5858 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5859 | QualType LHSCan = getCanonicalType(LHS), |
| 5860 | RHSCan = getCanonicalType(RHS); |
| 5861 | |
| 5862 | // If two types are identical, they are compatible. |
| 5863 | if (LHSCan == RHSCan) |
| 5864 | return LHS; |
| 5865 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5866 | // If the qualifiers are different, the types aren't compatible... mostly. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 5867 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 5868 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5869 | if (LQuals != RQuals) { |
| 5870 | // If any of these qualifiers are different, we have a type |
| 5871 | // mismatch. |
| 5872 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5873 | LQuals.getAddressSpace() != RQuals.getAddressSpace() || |
| 5874 | LQuals.getObjCLifetime() != RQuals.getObjCLifetime()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5875 | return QualType(); |
| 5876 | |
| 5877 | // Exactly one GC qualifier difference is allowed: __strong is |
| 5878 | // okay if the other type has no GC qualifier but is an Objective |
| 5879 | // C object pointer (i.e. implicitly strong by default). We fix |
| 5880 | // this by pretending that the unqualified type was actually |
| 5881 | // qualified __strong. |
| 5882 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 5883 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 5884 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 5885 | |
| 5886 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 5887 | return QualType(); |
| 5888 | |
| 5889 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
| 5890 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); |
| 5891 | } |
| 5892 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
| 5893 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); |
| 5894 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5895 | return QualType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5896 | } |
| 5897 | |
| 5898 | // Okay, qualifiers are equal. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5899 | |
Eli Friedman | 852d63b | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 5900 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 5901 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5902 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5903 | // We want to consider the two function types to be the same for these |
| 5904 | // comparisons, just force one to the other. |
| 5905 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 5906 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 4c721d3 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 5907 | |
| 5908 | // Same as above for arrays |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 5909 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 5910 | LHSClass = Type::ConstantArray; |
| 5911 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 5912 | RHSClass = Type::ConstantArray; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5913 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5914 | // ObjCInterfaces are just specialized ObjCObjects. |
| 5915 | if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject; |
| 5916 | if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject; |
| 5917 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 5918 | // Canonicalize ExtVector -> Vector. |
| 5919 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 5920 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5921 | |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 5922 | // If the canonical type classes don't match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5923 | if (LHSClass != RHSClass) { |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5924 | // 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] | 5925 | // a signed integer type, or an unsigned integer type. |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 5926 | // Compatibility is based on the underlying type, not the promotion |
| 5927 | // type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5928 | if (const EnumType* ETy = LHS->getAs<EnumType>()) { |
Fariborz Jahanian | b918d6b | 2012-02-06 19:06:20 +0000 | [diff] [blame] | 5929 | QualType TINT = ETy->getDecl()->getIntegerType(); |
| 5930 | if (!TINT.isNull() && hasSameType(TINT, RHSCan.getUnqualifiedType())) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5931 | return RHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 5932 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5933 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
Fariborz Jahanian | b918d6b | 2012-02-06 19:06:20 +0000 | [diff] [blame] | 5934 | QualType TINT = ETy->getDecl()->getIntegerType(); |
| 5935 | if (!TINT.isNull() && hasSameType(TINT, LHSCan.getUnqualifiedType())) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5936 | return LHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 5937 | } |
Fariborz Jahanian | e7cff2c | 2012-01-26 00:45:38 +0000 | [diff] [blame] | 5938 | // allow block pointer type to match an 'id' type. |
Fariborz Jahanian | 4196363 | 2012-01-26 17:08:50 +0000 | [diff] [blame] | 5939 | if (OfBlockPointer && !BlockReturnType) { |
| 5940 | if (LHS->isObjCIdType() && RHS->isBlockPointerType()) |
| 5941 | return LHS; |
| 5942 | if (RHS->isObjCIdType() && LHS->isBlockPointerType()) |
| 5943 | return RHS; |
| 5944 | } |
Fariborz Jahanian | e7cff2c | 2012-01-26 00:45:38 +0000 | [diff] [blame] | 5945 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5946 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5947 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5948 | |
Steve Naroff | 4a74678 | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 5949 | // The canonical type classes match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5950 | switch (LHSClass) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5951 | #define TYPE(Class, Base) |
| 5952 | #define ABSTRACT_TYPE(Class, Base) |
John McCall | ad5e738 | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 5953 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5954 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 5955 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 5956 | #include "clang/AST/TypeNodes.def" |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5957 | llvm_unreachable("Non-canonical and dependent types shouldn't get here"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5958 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 5959 | case Type::LValueReference: |
| 5960 | case Type::RValueReference: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5961 | case Type::MemberPointer: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5962 | llvm_unreachable("C++ should never be in mergeTypes"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5963 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5964 | case Type::ObjCInterface: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5965 | case Type::IncompleteArray: |
| 5966 | case Type::VariableArray: |
| 5967 | case Type::FunctionProto: |
| 5968 | case Type::ExtVector: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5969 | llvm_unreachable("Types are eliminated above"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5970 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5971 | case Type::Pointer: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5972 | { |
| 5973 | // Merge two pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5974 | QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); |
| 5975 | QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5976 | if (Unqualified) { |
| 5977 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 5978 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 5979 | } |
| 5980 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false, |
| 5981 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5982 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 5983 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5984 | return LHS; |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 5985 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5986 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5987 | return getPointerType(ResultType); |
| 5988 | } |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 5989 | case Type::BlockPointer: |
| 5990 | { |
| 5991 | // Merge two block pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5992 | QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); |
| 5993 | QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5994 | if (Unqualified) { |
| 5995 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 5996 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 5997 | } |
| 5998 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer, |
| 5999 | Unqualified); |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 6000 | if (ResultType.isNull()) return QualType(); |
| 6001 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 6002 | return LHS; |
| 6003 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 6004 | return RHS; |
| 6005 | return getBlockPointerType(ResultType); |
| 6006 | } |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6007 | case Type::Atomic: |
| 6008 | { |
| 6009 | // Merge two pointer types, while trying to preserve typedef info |
| 6010 | QualType LHSValue = LHS->getAs<AtomicType>()->getValueType(); |
| 6011 | QualType RHSValue = RHS->getAs<AtomicType>()->getValueType(); |
| 6012 | if (Unqualified) { |
| 6013 | LHSValue = LHSValue.getUnqualifiedType(); |
| 6014 | RHSValue = RHSValue.getUnqualifiedType(); |
| 6015 | } |
| 6016 | QualType ResultType = mergeTypes(LHSValue, RHSValue, false, |
| 6017 | Unqualified); |
| 6018 | if (ResultType.isNull()) return QualType(); |
| 6019 | if (getCanonicalType(LHSValue) == getCanonicalType(ResultType)) |
| 6020 | return LHS; |
| 6021 | if (getCanonicalType(RHSValue) == getCanonicalType(ResultType)) |
| 6022 | return RHS; |
| 6023 | return getAtomicType(ResultType); |
| 6024 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6025 | case Type::ConstantArray: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6026 | { |
| 6027 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 6028 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 6029 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 6030 | return QualType(); |
| 6031 | |
| 6032 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 6033 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6034 | if (Unqualified) { |
| 6035 | LHSElem = LHSElem.getUnqualifiedType(); |
| 6036 | RHSElem = RHSElem.getUnqualifiedType(); |
| 6037 | } |
| 6038 | |
| 6039 | QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6040 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6041 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 6042 | return LHS; |
| 6043 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 6044 | return RHS; |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 6045 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 6046 | ArrayType::ArraySizeModifier(), 0); |
| 6047 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 6048 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6049 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 6050 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6051 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 6052 | return LHS; |
| 6053 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 6054 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6055 | if (LVAT) { |
| 6056 | // FIXME: This isn't correct! But tricky to implement because |
| 6057 | // the array's size has to be the size of LHS, but the type |
| 6058 | // has to be different. |
| 6059 | return LHS; |
| 6060 | } |
| 6061 | if (RVAT) { |
| 6062 | // FIXME: This isn't correct! But tricky to implement because |
| 6063 | // the array's size has to be the size of RHS, but the type |
| 6064 | // has to be different. |
| 6065 | return RHS; |
| 6066 | } |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 6067 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 6068 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 6069 | return getIncompleteArrayType(ResultType, |
| 6070 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6071 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6072 | case Type::FunctionNoProto: |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6073 | return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6074 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6075 | case Type::Enum: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6076 | return QualType(); |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6077 | case Type::Builtin: |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 6078 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6079 | return QualType(); |
Daniel Dunbar | 64cfdb7 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 6080 | case Type::Complex: |
| 6081 | // Distinct complex types are incompatible. |
| 6082 | return QualType(); |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 6083 | case Type::Vector: |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 6084 | // FIXME: The merged type should be an ExtVector! |
John McCall | 1c471f3 | 2010-03-12 23:14:13 +0000 | [diff] [blame] | 6085 | if (areCompatVectorTypes(LHSCan->getAs<VectorType>(), |
| 6086 | RHSCan->getAs<VectorType>())) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6087 | return LHS; |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6088 | return QualType(); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6089 | case Type::ObjCObject: { |
| 6090 | // Check if the types are assignment compatible. |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 6091 | // FIXME: This should be type compatibility, e.g. whether |
| 6092 | // "LHS x; RHS x;" at global scope is legal. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6093 | const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>(); |
| 6094 | const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>(); |
| 6095 | if (canAssignObjCInterfaces(LHSIface, RHSIface)) |
Steve Naroff | 5fd659d | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 6096 | return LHS; |
| 6097 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6098 | return QualType(); |
Cedric Venet | 61490e9 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 6099 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6100 | case Type::ObjCObjectPointer: { |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6101 | if (OfBlockPointer) { |
| 6102 | if (canAssignObjCInterfacesInBlockPointer( |
| 6103 | LHS->getAs<ObjCObjectPointerType>(), |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6104 | RHS->getAs<ObjCObjectPointerType>(), |
| 6105 | BlockReturnType)) |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 6106 | return LHS; |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6107 | return QualType(); |
| 6108 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6109 | if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(), |
| 6110 | RHS->getAs<ObjCObjectPointerType>())) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6111 | return LHS; |
| 6112 | |
Steve Naroff | bc76dd0 | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 6113 | return QualType(); |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 6114 | } |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 6115 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6116 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 6117 | llvm_unreachable("Invalid Type::Class!"); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 6118 | } |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 6119 | |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 6120 | bool ASTContext::FunctionTypesMatchOnNSConsumedAttrs( |
| 6121 | const FunctionProtoType *FromFunctionType, |
| 6122 | const FunctionProtoType *ToFunctionType) { |
| 6123 | if (FromFunctionType->hasAnyConsumedArgs() != |
| 6124 | ToFunctionType->hasAnyConsumedArgs()) |
| 6125 | return false; |
| 6126 | FunctionProtoType::ExtProtoInfo FromEPI = |
| 6127 | FromFunctionType->getExtProtoInfo(); |
| 6128 | FunctionProtoType::ExtProtoInfo ToEPI = |
| 6129 | ToFunctionType->getExtProtoInfo(); |
| 6130 | if (FromEPI.ConsumedArguments && ToEPI.ConsumedArguments) |
| 6131 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 6132 | ArgIdx != NumArgs; ++ArgIdx) { |
| 6133 | if (FromEPI.ConsumedArguments[ArgIdx] != |
| 6134 | ToEPI.ConsumedArguments[ArgIdx]) |
| 6135 | return false; |
| 6136 | } |
| 6137 | return true; |
| 6138 | } |
| 6139 | |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 6140 | /// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and |
| 6141 | /// 'RHS' attributes and returns the merged version; including for function |
| 6142 | /// return types. |
| 6143 | QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) { |
| 6144 | QualType LHSCan = getCanonicalType(LHS), |
| 6145 | RHSCan = getCanonicalType(RHS); |
| 6146 | // If two types are identical, they are compatible. |
| 6147 | if (LHSCan == RHSCan) |
| 6148 | return LHS; |
| 6149 | if (RHSCan->isFunctionType()) { |
| 6150 | if (!LHSCan->isFunctionType()) |
| 6151 | return QualType(); |
| 6152 | QualType OldReturnType = |
| 6153 | cast<FunctionType>(RHSCan.getTypePtr())->getResultType(); |
| 6154 | QualType NewReturnType = |
| 6155 | cast<FunctionType>(LHSCan.getTypePtr())->getResultType(); |
| 6156 | QualType ResReturnType = |
| 6157 | mergeObjCGCQualifiers(NewReturnType, OldReturnType); |
| 6158 | if (ResReturnType.isNull()) |
| 6159 | return QualType(); |
| 6160 | if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) { |
| 6161 | // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo(); |
| 6162 | // In either case, use OldReturnType to build the new function type. |
| 6163 | const FunctionType *F = LHS->getAs<FunctionType>(); |
| 6164 | if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6165 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 6166 | EPI.ExtInfo = getFunctionExtInfo(LHS); |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 6167 | QualType ResultType |
| 6168 | = getFunctionType(OldReturnType, FPT->arg_type_begin(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6169 | FPT->getNumArgs(), EPI); |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 6170 | return ResultType; |
| 6171 | } |
| 6172 | } |
| 6173 | return QualType(); |
| 6174 | } |
| 6175 | |
| 6176 | // If the qualifiers are different, the types can still be merged. |
| 6177 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 6178 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
| 6179 | if (LQuals != RQuals) { |
| 6180 | // If any of these qualifiers are different, we have a type mismatch. |
| 6181 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 6182 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 6183 | return QualType(); |
| 6184 | |
| 6185 | // Exactly one GC qualifier difference is allowed: __strong is |
| 6186 | // okay if the other type has no GC qualifier but is an Objective |
| 6187 | // C object pointer (i.e. implicitly strong by default). We fix |
| 6188 | // this by pretending that the unqualified type was actually |
| 6189 | // qualified __strong. |
| 6190 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 6191 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 6192 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 6193 | |
| 6194 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 6195 | return QualType(); |
| 6196 | |
| 6197 | if (GC_L == Qualifiers::Strong) |
| 6198 | return LHS; |
| 6199 | if (GC_R == Qualifiers::Strong) |
| 6200 | return RHS; |
| 6201 | return QualType(); |
| 6202 | } |
| 6203 | |
| 6204 | if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) { |
| 6205 | QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 6206 | QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 6207 | QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT); |
| 6208 | if (ResQT == LHSBaseQT) |
| 6209 | return LHS; |
| 6210 | if (ResQT == RHSBaseQT) |
| 6211 | return RHS; |
| 6212 | } |
| 6213 | return QualType(); |
| 6214 | } |
| 6215 | |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 6216 | //===----------------------------------------------------------------------===// |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6217 | // Integer Predicates |
| 6218 | //===----------------------------------------------------------------------===// |
Chris Lattner | 88054de | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 6219 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6220 | unsigned ASTContext::getIntWidth(QualType T) const { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 6221 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
Eli Friedman | 29a7f33 | 2009-12-10 22:29:29 +0000 | [diff] [blame] | 6222 | T = ET->getDecl()->getIntegerType(); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6223 | if (T->isBooleanType()) |
| 6224 | return 1; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 6225 | // For builtin types, just use the standard type sizing method |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6226 | return (unsigned)getTypeSize(T); |
| 6227 | } |
| 6228 | |
| 6229 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) { |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6230 | assert(T->hasSignedIntegerRepresentation() && "Unexpected type"); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6231 | |
| 6232 | // Turn <4 x signed int> -> <4 x unsigned int> |
| 6233 | if (const VectorType *VTy = T->getAs<VectorType>()) |
| 6234 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 6235 | VTy->getNumElements(), VTy->getVectorKind()); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6236 | |
| 6237 | // For enums, we return the unsigned version of the base type. |
| 6238 | if (const EnumType *ETy = T->getAs<EnumType>()) |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6239 | T = ETy->getDecl()->getIntegerType(); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6240 | |
| 6241 | const BuiltinType *BTy = T->getAs<BuiltinType>(); |
| 6242 | assert(BTy && "Unexpected signed integer type"); |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6243 | switch (BTy->getKind()) { |
| 6244 | case BuiltinType::Char_S: |
| 6245 | case BuiltinType::SChar: |
| 6246 | return UnsignedCharTy; |
| 6247 | case BuiltinType::Short: |
| 6248 | return UnsignedShortTy; |
| 6249 | case BuiltinType::Int: |
| 6250 | return UnsignedIntTy; |
| 6251 | case BuiltinType::Long: |
| 6252 | return UnsignedLongTy; |
| 6253 | case BuiltinType::LongLong: |
| 6254 | return UnsignedLongLongTy; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 6255 | case BuiltinType::Int128: |
| 6256 | return UnsignedInt128Ty; |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6257 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6258 | llvm_unreachable("Unexpected signed integer type"); |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6259 | } |
| 6260 | } |
| 6261 | |
Argyrios Kyrtzidis | 7b90340 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 6262 | ASTMutationListener::~ASTMutationListener() { } |
| 6263 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6264 | |
| 6265 | //===----------------------------------------------------------------------===// |
| 6266 | // Builtin Type Computation |
| 6267 | //===----------------------------------------------------------------------===// |
| 6268 | |
| 6269 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6270 | /// pointer over the consumed characters. This returns the resultant type. If |
| 6271 | /// AllowTypeModifiers is false then modifier like * are not parsed, just basic |
| 6272 | /// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of |
| 6273 | /// a vector of "i*". |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6274 | /// |
| 6275 | /// RequiresICE is filled in on return to indicate whether the value is required |
| 6276 | /// to be an Integer Constant Expression. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6277 | static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context, |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6278 | ASTContext::GetBuiltinTypeError &Error, |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6279 | bool &RequiresICE, |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6280 | bool AllowTypeModifiers) { |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6281 | // Modifiers. |
| 6282 | int HowLong = 0; |
| 6283 | bool Signed = false, Unsigned = false; |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6284 | RequiresICE = false; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6285 | |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6286 | // Read the prefixed modifiers first. |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6287 | bool Done = false; |
| 6288 | while (!Done) { |
| 6289 | switch (*Str++) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6290 | default: Done = true; --Str; break; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6291 | case 'I': |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6292 | RequiresICE = true; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6293 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6294 | case 'S': |
| 6295 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 6296 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 6297 | Signed = true; |
| 6298 | break; |
| 6299 | case 'U': |
| 6300 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
| 6301 | assert(!Unsigned && "Can't use 'S' modifier multiple times!"); |
| 6302 | Unsigned = true; |
| 6303 | break; |
| 6304 | case 'L': |
| 6305 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 6306 | ++HowLong; |
| 6307 | break; |
| 6308 | } |
| 6309 | } |
| 6310 | |
| 6311 | QualType Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6312 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6313 | // Read the base type. |
| 6314 | switch (*Str++) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6315 | default: llvm_unreachable("Unknown builtin type letter!"); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6316 | case 'v': |
| 6317 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 6318 | "Bad modifiers used with 'v'!"); |
| 6319 | Type = Context.VoidTy; |
| 6320 | break; |
| 6321 | case 'f': |
| 6322 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 6323 | "Bad modifiers used with 'f'!"); |
| 6324 | Type = Context.FloatTy; |
| 6325 | break; |
| 6326 | case 'd': |
| 6327 | assert(HowLong < 2 && !Signed && !Unsigned && |
| 6328 | "Bad modifiers used with 'd'!"); |
| 6329 | if (HowLong) |
| 6330 | Type = Context.LongDoubleTy; |
| 6331 | else |
| 6332 | Type = Context.DoubleTy; |
| 6333 | break; |
| 6334 | case 's': |
| 6335 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 6336 | if (Unsigned) |
| 6337 | Type = Context.UnsignedShortTy; |
| 6338 | else |
| 6339 | Type = Context.ShortTy; |
| 6340 | break; |
| 6341 | case 'i': |
| 6342 | if (HowLong == 3) |
| 6343 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 6344 | else if (HowLong == 2) |
| 6345 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 6346 | else if (HowLong == 1) |
| 6347 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 6348 | else |
| 6349 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 6350 | break; |
| 6351 | case 'c': |
| 6352 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 6353 | if (Signed) |
| 6354 | Type = Context.SignedCharTy; |
| 6355 | else if (Unsigned) |
| 6356 | Type = Context.UnsignedCharTy; |
| 6357 | else |
| 6358 | Type = Context.CharTy; |
| 6359 | break; |
| 6360 | case 'b': // boolean |
| 6361 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 6362 | Type = Context.BoolTy; |
| 6363 | break; |
| 6364 | case 'z': // size_t. |
| 6365 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 6366 | Type = Context.getSizeType(); |
| 6367 | break; |
| 6368 | case 'F': |
| 6369 | Type = Context.getCFConstantStringType(); |
| 6370 | break; |
Fariborz Jahanian | ba8bda0 | 2010-11-09 21:38:20 +0000 | [diff] [blame] | 6371 | case 'G': |
| 6372 | Type = Context.getObjCIdType(); |
| 6373 | break; |
| 6374 | case 'H': |
| 6375 | Type = Context.getObjCSelType(); |
| 6376 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6377 | case 'a': |
| 6378 | Type = Context.getBuiltinVaListType(); |
| 6379 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 6380 | break; |
| 6381 | case 'A': |
| 6382 | // This is a "reference" to a va_list; however, what exactly |
| 6383 | // this means depends on how va_list is defined. There are two |
| 6384 | // different kinds of va_list: ones passed by value, and ones |
| 6385 | // passed by reference. An example of a by-value va_list is |
| 6386 | // x86, where va_list is a char*. An example of by-ref va_list |
| 6387 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 6388 | // we want this argument to be a char*&; for x86-64, we want |
| 6389 | // it to be a __va_list_tag*. |
| 6390 | Type = Context.getBuiltinVaListType(); |
| 6391 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6392 | if (Type->isArrayType()) |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6393 | Type = Context.getArrayDecayedType(Type); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6394 | else |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6395 | Type = Context.getLValueReferenceType(Type); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6396 | break; |
| 6397 | case 'V': { |
| 6398 | char *End; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6399 | unsigned NumElements = strtoul(Str, &End, 10); |
| 6400 | assert(End != Str && "Missing vector size"); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6401 | Str = End; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6402 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6403 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, |
| 6404 | RequiresICE, false); |
| 6405 | assert(!RequiresICE && "Can't require vector ICE"); |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6406 | |
| 6407 | // TODO: No way to make AltiVec vectors in builtins yet. |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 6408 | Type = Context.getVectorType(ElementType, NumElements, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 6409 | VectorType::GenericVector); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6410 | break; |
| 6411 | } |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 6412 | case 'X': { |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6413 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
| 6414 | false); |
| 6415 | assert(!RequiresICE && "Can't require complex ICE"); |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 6416 | Type = Context.getComplexType(ElementType); |
| 6417 | break; |
Fariborz Jahanian | cc075e4 | 2011-08-23 23:33:09 +0000 | [diff] [blame] | 6418 | } |
| 6419 | case 'Y' : { |
| 6420 | Type = Context.getPointerDiffType(); |
| 6421 | break; |
| 6422 | } |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 6423 | case 'P': |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 6424 | Type = Context.getFILEType(); |
| 6425 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6426 | Error = ASTContext::GE_Missing_stdio; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6427 | return QualType(); |
| 6428 | } |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 6429 | break; |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 6430 | case 'J': |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6431 | if (Signed) |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 6432 | Type = Context.getsigjmp_bufType(); |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6433 | else |
| 6434 | Type = Context.getjmp_bufType(); |
| 6435 | |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 6436 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6437 | Error = ASTContext::GE_Missing_setjmp; |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 6438 | return QualType(); |
| 6439 | } |
| 6440 | break; |
Rafael Espindola | e2d4f4e | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 6441 | case 'K': |
| 6442 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'K'!"); |
| 6443 | Type = Context.getucontext_tType(); |
| 6444 | |
| 6445 | if (Type.isNull()) { |
| 6446 | Error = ASTContext::GE_Missing_ucontext; |
| 6447 | return QualType(); |
| 6448 | } |
| 6449 | break; |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 6450 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6451 | |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6452 | // If there are modifiers and if we're allowed to parse them, go for it. |
| 6453 | Done = !AllowTypeModifiers; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6454 | while (!Done) { |
John McCall | 187ab37 | 2010-03-12 04:21:28 +0000 | [diff] [blame] | 6455 | switch (char c = *Str++) { |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6456 | default: Done = true; --Str; break; |
| 6457 | case '*': |
| 6458 | case '&': { |
| 6459 | // Both pointers and references can have their pointee types |
| 6460 | // qualified with an address space. |
| 6461 | char *End; |
| 6462 | unsigned AddrSpace = strtoul(Str, &End, 10); |
| 6463 | if (End != Str && AddrSpace != 0) { |
| 6464 | Type = Context.getAddrSpaceQualType(Type, AddrSpace); |
| 6465 | Str = End; |
| 6466 | } |
| 6467 | if (c == '*') |
| 6468 | Type = Context.getPointerType(Type); |
| 6469 | else |
| 6470 | Type = Context.getLValueReferenceType(Type); |
| 6471 | break; |
| 6472 | } |
| 6473 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 6474 | case 'C': |
| 6475 | Type = Type.withConst(); |
| 6476 | break; |
| 6477 | case 'D': |
| 6478 | Type = Context.getVolatileType(Type); |
| 6479 | break; |
Ted Kremenek | 18932a0 | 2012-01-20 21:40:12 +0000 | [diff] [blame] | 6480 | case 'R': |
| 6481 | Type = Type.withRestrict(); |
| 6482 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6483 | } |
| 6484 | } |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6485 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6486 | assert((!RequiresICE || Type->isIntegralOrEnumerationType()) && |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6487 | "Integer constant 'I' type must be an integer"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6488 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6489 | return Type; |
| 6490 | } |
| 6491 | |
| 6492 | /// GetBuiltinType - Return the type for the specified builtin. |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6493 | QualType ASTContext::GetBuiltinType(unsigned Id, |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6494 | GetBuiltinTypeError &Error, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6495 | unsigned *IntegerConstantArgs) const { |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6496 | const char *TypeStr = BuiltinInfo.GetTypeString(Id); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6497 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6498 | SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6499 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6500 | bool RequiresICE = false; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6501 | Error = GE_None; |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6502 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error, |
| 6503 | RequiresICE, true); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6504 | if (Error != GE_None) |
| 6505 | return QualType(); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6506 | |
| 6507 | assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE"); |
| 6508 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6509 | while (TypeStr[0] && TypeStr[0] != '.') { |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6510 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6511 | if (Error != GE_None) |
| 6512 | return QualType(); |
| 6513 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6514 | // If this argument is required to be an IntegerConstantExpression and the |
| 6515 | // caller cares, fill in the bitmask we return. |
| 6516 | if (RequiresICE && IntegerConstantArgs) |
| 6517 | *IntegerConstantArgs |= 1 << ArgTypes.size(); |
| 6518 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6519 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 6520 | if (Ty->isArrayType()) |
| 6521 | Ty = getArrayDecayedType(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6522 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6523 | ArgTypes.push_back(Ty); |
| 6524 | } |
| 6525 | |
| 6526 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 6527 | "'.' should only occur at end of builtin type list!"); |
| 6528 | |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 6529 | FunctionType::ExtInfo EI; |
| 6530 | if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true); |
| 6531 | |
| 6532 | bool Variadic = (TypeStr[0] == '.'); |
| 6533 | |
| 6534 | // We really shouldn't be making a no-proto type here, especially in C++. |
| 6535 | if (ArgTypes.empty() && Variadic) |
| 6536 | return getFunctionNoProtoType(ResType, EI); |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 6537 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6538 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 6539 | EPI.ExtInfo = EI; |
| 6540 | EPI.Variadic = Variadic; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6541 | |
| 6542 | return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), EPI); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6543 | } |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 6544 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6545 | GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) { |
| 6546 | GVALinkage External = GVA_StrongExternal; |
| 6547 | |
| 6548 | Linkage L = FD->getLinkage(); |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6549 | switch (L) { |
| 6550 | case NoLinkage: |
| 6551 | case InternalLinkage: |
| 6552 | case UniqueExternalLinkage: |
| 6553 | return GVA_Internal; |
| 6554 | |
| 6555 | case ExternalLinkage: |
| 6556 | switch (FD->getTemplateSpecializationKind()) { |
| 6557 | case TSK_Undeclared: |
| 6558 | case TSK_ExplicitSpecialization: |
| 6559 | External = GVA_StrongExternal; |
| 6560 | break; |
| 6561 | |
| 6562 | case TSK_ExplicitInstantiationDefinition: |
| 6563 | return GVA_ExplicitTemplateInstantiation; |
| 6564 | |
| 6565 | case TSK_ExplicitInstantiationDeclaration: |
| 6566 | case TSK_ImplicitInstantiation: |
| 6567 | External = GVA_TemplateInstantiation; |
| 6568 | break; |
| 6569 | } |
| 6570 | } |
| 6571 | |
| 6572 | if (!FD->isInlined()) |
| 6573 | return External; |
| 6574 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6575 | if (!getLangOpts().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) { |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6576 | // GNU or C99 inline semantics. Determine whether this symbol should be |
| 6577 | // externally visible. |
| 6578 | if (FD->isInlineDefinitionExternallyVisible()) |
| 6579 | return External; |
| 6580 | |
| 6581 | // C99 inline semantics, where the symbol is not externally visible. |
| 6582 | return GVA_C99Inline; |
| 6583 | } |
| 6584 | |
| 6585 | // C++0x [temp.explicit]p9: |
| 6586 | // [ Note: The intent is that an inline function that is the subject of |
| 6587 | // an explicit instantiation declaration will still be implicitly |
| 6588 | // instantiated when used so that the body can be considered for |
| 6589 | // inlining, but that no out-of-line copy of the inline function would be |
| 6590 | // generated in the translation unit. -- end note ] |
| 6591 | if (FD->getTemplateSpecializationKind() |
| 6592 | == TSK_ExplicitInstantiationDeclaration) |
| 6593 | return GVA_C99Inline; |
| 6594 | |
| 6595 | return GVA_CXXInline; |
| 6596 | } |
| 6597 | |
| 6598 | GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) { |
| 6599 | // If this is a static data member, compute the kind of template |
| 6600 | // specialization. Otherwise, this variable is not part of a |
| 6601 | // template. |
| 6602 | TemplateSpecializationKind TSK = TSK_Undeclared; |
| 6603 | if (VD->isStaticDataMember()) |
| 6604 | TSK = VD->getTemplateSpecializationKind(); |
| 6605 | |
| 6606 | Linkage L = VD->getLinkage(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6607 | if (L == ExternalLinkage && getLangOpts().CPlusPlus && |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6608 | VD->getType()->getLinkage() == UniqueExternalLinkage) |
| 6609 | L = UniqueExternalLinkage; |
| 6610 | |
| 6611 | switch (L) { |
| 6612 | case NoLinkage: |
| 6613 | case InternalLinkage: |
| 6614 | case UniqueExternalLinkage: |
| 6615 | return GVA_Internal; |
| 6616 | |
| 6617 | case ExternalLinkage: |
| 6618 | switch (TSK) { |
| 6619 | case TSK_Undeclared: |
| 6620 | case TSK_ExplicitSpecialization: |
| 6621 | return GVA_StrongExternal; |
| 6622 | |
| 6623 | case TSK_ExplicitInstantiationDeclaration: |
| 6624 | llvm_unreachable("Variable should not be instantiated"); |
| 6625 | // Fall through to treat this like any other instantiation. |
| 6626 | |
| 6627 | case TSK_ExplicitInstantiationDefinition: |
| 6628 | return GVA_ExplicitTemplateInstantiation; |
| 6629 | |
| 6630 | case TSK_ImplicitInstantiation: |
| 6631 | return GVA_TemplateInstantiation; |
| 6632 | } |
| 6633 | } |
| 6634 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 6635 | llvm_unreachable("Invalid Linkage!"); |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6636 | } |
| 6637 | |
Argyrios Kyrtzidis | 4ac7c0b | 2010-07-29 20:08:05 +0000 | [diff] [blame] | 6638 | bool ASTContext::DeclMustBeEmitted(const Decl *D) { |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6639 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 6640 | if (!VD->isFileVarDecl()) |
| 6641 | return false; |
Argyrios Kyrtzidis | 144b38a | 2011-09-13 21:35:00 +0000 | [diff] [blame] | 6642 | } else if (!isa<FunctionDecl>(D)) |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6643 | return false; |
| 6644 | |
Argyrios Kyrtzidis | ab411c8 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 6645 | // Weak references don't produce any output by themselves. |
| 6646 | if (D->hasAttr<WeakRefAttr>()) |
| 6647 | return false; |
| 6648 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6649 | // Aliases and used decls are required. |
| 6650 | if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>()) |
| 6651 | return true; |
| 6652 | |
| 6653 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 6654 | // Forward declarations aren't required. |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 6655 | if (!FD->doesThisDeclarationHaveABody()) |
Nick Lewycky | dce67a7 | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 6656 | return FD->doesDeclarationForceExternallyVisibleDefinition(); |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6657 | |
| 6658 | // Constructors and destructors are required. |
| 6659 | if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>()) |
| 6660 | return true; |
| 6661 | |
| 6662 | // The key function for a class is required. |
| 6663 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 6664 | const CXXRecordDecl *RD = MD->getParent(); |
| 6665 | if (MD->isOutOfLine() && RD->isDynamicClass()) { |
| 6666 | const CXXMethodDecl *KeyFunc = getKeyFunction(RD); |
| 6667 | if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl()) |
| 6668 | return true; |
| 6669 | } |
| 6670 | } |
| 6671 | |
| 6672 | GVALinkage Linkage = GetGVALinkageForFunction(FD); |
| 6673 | |
| 6674 | // static, static inline, always_inline, and extern inline functions can |
| 6675 | // always be deferred. Normal inline functions can be deferred in C99/C++. |
| 6676 | // Implicit template instantiations can also be deferred in C++. |
| 6677 | if (Linkage == GVA_Internal || Linkage == GVA_C99Inline || |
Anton Yartsev | 3a5aca8 | 2012-02-02 06:06:34 +0000 | [diff] [blame] | 6678 | Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6679 | return false; |
| 6680 | return true; |
| 6681 | } |
Douglas Gregor | 94da158 | 2011-09-10 00:22:34 +0000 | [diff] [blame] | 6682 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6683 | const VarDecl *VD = cast<VarDecl>(D); |
| 6684 | assert(VD->isFileVarDecl() && "Expected file scoped var"); |
| 6685 | |
Argyrios Kyrtzidis | ab411c8 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 6686 | if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly) |
| 6687 | return false; |
| 6688 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6689 | // Structs that have non-trivial constructors or destructors are required. |
| 6690 | |
| 6691 | // FIXME: Handle references. |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 6692 | // FIXME: Be more selective about which constructors we care about. |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6693 | if (const RecordType *RT = VD->getType()->getAs<RecordType>()) { |
| 6694 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 6695 | if (RD->hasDefinition() && !(RD->hasTrivialDefaultConstructor() && |
| 6696 | RD->hasTrivialCopyConstructor() && |
| 6697 | RD->hasTrivialMoveConstructor() && |
| 6698 | RD->hasTrivialDestructor())) |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6699 | return true; |
| 6700 | } |
| 6701 | } |
| 6702 | |
| 6703 | GVALinkage L = GetGVALinkageForVariable(VD); |
| 6704 | if (L == GVA_Internal || L == GVA_TemplateInstantiation) { |
| 6705 | if (!(VD->getInit() && VD->getInit()->HasSideEffects(*this))) |
| 6706 | return false; |
| 6707 | } |
| 6708 | |
| 6709 | return true; |
| 6710 | } |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 6711 | |
Charles Davis | ee743f9 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 6712 | CallingConv ASTContext::getDefaultMethodCallConv() { |
| 6713 | // Pass through to the C++ ABI object |
| 6714 | return ABI->getDefaultMethodCallConv(); |
| 6715 | } |
| 6716 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6717 | bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const { |
Anders Carlsson | dae0cb5 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 6718 | // Pass through to the C++ ABI object |
| 6719 | return ABI->isNearlyEmpty(RD); |
| 6720 | } |
| 6721 | |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 6722 | MangleContext *ASTContext::createMangleContext() { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 6723 | switch (Target->getCXXABI()) { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 6724 | case CXXABI_ARM: |
| 6725 | case CXXABI_Itanium: |
| 6726 | return createItaniumMangleContext(*this, getDiagnostics()); |
| 6727 | case CXXABI_Microsoft: |
| 6728 | return createMicrosoftMangleContext(*this, getDiagnostics()); |
| 6729 | } |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6730 | llvm_unreachable("Unsupported ABI"); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 6731 | } |
| 6732 | |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 6733 | CXXABI::~CXXABI() {} |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 6734 | |
| 6735 | size_t ASTContext::getSideTableAllocatedMemory() const { |
Ted Kremenek | 0c8cd1a | 2011-07-27 18:41:12 +0000 | [diff] [blame] | 6736 | return ASTRecordLayouts.getMemorySize() |
| 6737 | + llvm::capacity_in_bytes(ObjCLayouts) |
| 6738 | + llvm::capacity_in_bytes(KeyFunctions) |
| 6739 | + llvm::capacity_in_bytes(ObjCImpls) |
| 6740 | + llvm::capacity_in_bytes(BlockVarCopyInits) |
| 6741 | + llvm::capacity_in_bytes(DeclAttrs) |
| 6742 | + llvm::capacity_in_bytes(InstantiatedFromStaticDataMember) |
| 6743 | + llvm::capacity_in_bytes(InstantiatedFromUsingDecl) |
| 6744 | + llvm::capacity_in_bytes(InstantiatedFromUsingShadowDecl) |
| 6745 | + llvm::capacity_in_bytes(InstantiatedFromUnnamedFieldDecl) |
| 6746 | + llvm::capacity_in_bytes(OverriddenMethods) |
| 6747 | + llvm::capacity_in_bytes(Types) |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 6748 | + llvm::capacity_in_bytes(VariableArrayTypes) |
Francois Pichet | 0d95f0d | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 6749 | + llvm::capacity_in_bytes(ClassScopeSpecializationPattern); |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 6750 | } |
Ted Kremenek | d211cb7 | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 6751 | |
Douglas Gregor | 9e8c92a | 2012-02-20 19:44:39 +0000 | [diff] [blame] | 6752 | unsigned ASTContext::getLambdaManglingNumber(CXXMethodDecl *CallOperator) { |
| 6753 | CXXRecordDecl *Lambda = CallOperator->getParent(); |
| 6754 | return LambdaMangleContexts[Lambda->getDeclContext()] |
| 6755 | .getManglingNumber(CallOperator); |
| 6756 | } |
| 6757 | |
| 6758 | |
Ted Kremenek | d211cb7 | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 6759 | void ASTContext::setParameterIndex(const ParmVarDecl *D, unsigned int index) { |
| 6760 | ParamIndices[D] = index; |
| 6761 | } |
| 6762 | |
| 6763 | unsigned ASTContext::getParameterIndex(const ParmVarDecl *D) const { |
| 6764 | ParameterIndexTable::const_iterator I = ParamIndices.find(D); |
| 6765 | assert(I != ParamIndices.end() && |
| 6766 | "ParmIndices lacks entry set by ParmVarDecl"); |
| 6767 | return I->second; |
| 6768 | } |