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 | |
James Molloy | 392da48 | 2012-05-04 10:55:22 +0000 | [diff] [blame^] | 439 | WIntTy = getFromTargetType(Target.getWIntType()); |
| 440 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 441 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 442 | InitBuiltinType(Char16Ty, BuiltinType::Char16); |
| 443 | else // C99 |
| 444 | Char16Ty = getFromTargetType(Target.getChar16Type()); |
| 445 | |
| 446 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 447 | InitBuiltinType(Char32Ty, BuiltinType::Char32); |
| 448 | else // C99 |
| 449 | Char32Ty = getFromTargetType(Target.getChar32Type()); |
| 450 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 451 | // Placeholder type for type-dependent expressions whose type is |
| 452 | // completely unknown. No code should ever check a type against |
| 453 | // DependentTy and users should never see it; however, it is here to |
| 454 | // help diagnose failures to properly check for type-dependent |
| 455 | // expressions. |
| 456 | InitBuiltinType(DependentTy, BuiltinType::Dependent); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 457 | |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 458 | // Placeholder type for functions. |
| 459 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
| 460 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 461 | // Placeholder type for bound members. |
| 462 | InitBuiltinType(BoundMemberTy, BuiltinType::BoundMember); |
| 463 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 464 | // Placeholder type for pseudo-objects. |
| 465 | InitBuiltinType(PseudoObjectTy, BuiltinType::PseudoObject); |
| 466 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 467 | // "any" type; useful for debugger-like clients. |
| 468 | InitBuiltinType(UnknownAnyTy, BuiltinType::UnknownAny); |
| 469 | |
John McCall | 0ddaeb9 | 2011-10-17 18:09:15 +0000 | [diff] [blame] | 470 | // Placeholder type for unbridged ARC casts. |
| 471 | InitBuiltinType(ARCUnbridgedCastTy, BuiltinType::ARCUnbridgedCast); |
| 472 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 473 | // C99 6.2.5p11. |
| 474 | FloatComplexTy = getComplexType(FloatTy); |
| 475 | DoubleComplexTy = getComplexType(DoubleTy); |
| 476 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 477 | |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 478 | BuiltinVaListType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 479 | |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 480 | // Builtin types for 'id', 'Class', and 'SEL'. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 481 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); |
| 482 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 483 | InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 484 | |
| 485 | // Builtin type for __objc_yes and __objc_no |
Fariborz Jahanian | 93a4994 | 2012-04-16 21:03:30 +0000 | [diff] [blame] | 486 | ObjCBuiltinBoolTy = (Target.useSignedCharForObjCBool() ? |
| 487 | SignedCharTy : BoolTy); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 488 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 489 | ObjCConstantStringType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 490 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 491 | // void * type |
| 492 | VoidPtrTy = getPointerType(VoidTy); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 493 | |
| 494 | // nullptr type (C++0x 2.14.7) |
| 495 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 496 | |
| 497 | // half type (OpenCL 6.1.1.1) / ARM NEON __fp16 |
| 498 | InitBuiltinType(HalfTy, BuiltinType::Half); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 499 | } |
| 500 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 501 | DiagnosticsEngine &ASTContext::getDiagnostics() const { |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 502 | return SourceMgr.getDiagnostics(); |
| 503 | } |
| 504 | |
Douglas Gregor | 6320064 | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 505 | AttrVec& ASTContext::getDeclAttrs(const Decl *D) { |
| 506 | AttrVec *&Result = DeclAttrs[D]; |
| 507 | if (!Result) { |
| 508 | void *Mem = Allocate(sizeof(AttrVec)); |
| 509 | Result = new (Mem) AttrVec; |
| 510 | } |
| 511 | |
| 512 | return *Result; |
| 513 | } |
| 514 | |
| 515 | /// \brief Erase the attributes corresponding to the given declaration. |
| 516 | void ASTContext::eraseDeclAttrs(const Decl *D) { |
| 517 | llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D); |
| 518 | if (Pos != DeclAttrs.end()) { |
| 519 | Pos->second->~AttrVec(); |
| 520 | DeclAttrs.erase(Pos); |
| 521 | } |
| 522 | } |
| 523 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 524 | MemberSpecializationInfo * |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 525 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 526 | assert(Var->isStaticDataMember() && "Not a static data member"); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 527 | llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 528 | = InstantiatedFromStaticDataMember.find(Var); |
| 529 | if (Pos == InstantiatedFromStaticDataMember.end()) |
| 530 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 531 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 532 | return Pos->second; |
| 533 | } |
| 534 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 535 | void |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 536 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, |
Argyrios Kyrtzidis | 9421adc | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 537 | TemplateSpecializationKind TSK, |
| 538 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 539 | assert(Inst->isStaticDataMember() && "Not a static data member"); |
| 540 | assert(Tmpl->isStaticDataMember() && "Not a static data member"); |
| 541 | assert(!InstantiatedFromStaticDataMember[Inst] && |
| 542 | "Already noted what static data member was instantiated from"); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 543 | InstantiatedFromStaticDataMember[Inst] |
Argyrios Kyrtzidis | 9421adc | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 544 | = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 547 | FunctionDecl *ASTContext::getClassScopeSpecializationPattern( |
| 548 | const FunctionDecl *FD){ |
| 549 | assert(FD && "Specialization is 0"); |
| 550 | llvm::DenseMap<const FunctionDecl*, FunctionDecl *>::const_iterator Pos |
Francois Pichet | 0d95f0d | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 551 | = ClassScopeSpecializationPattern.find(FD); |
| 552 | if (Pos == ClassScopeSpecializationPattern.end()) |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 553 | return 0; |
| 554 | |
| 555 | return Pos->second; |
| 556 | } |
| 557 | |
| 558 | void ASTContext::setClassScopeSpecializationPattern(FunctionDecl *FD, |
| 559 | FunctionDecl *Pattern) { |
| 560 | assert(FD && "Specialization is 0"); |
| 561 | assert(Pattern && "Class scope specialization pattern is 0"); |
Francois Pichet | 0d95f0d | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 562 | ClassScopeSpecializationPattern[FD] = Pattern; |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 563 | } |
| 564 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 565 | NamedDecl * |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 566 | ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 567 | llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 568 | = InstantiatedFromUsingDecl.find(UUD); |
| 569 | if (Pos == InstantiatedFromUsingDecl.end()) |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 570 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 571 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 572 | return Pos->second; |
| 573 | } |
| 574 | |
| 575 | void |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 576 | ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) { |
| 577 | assert((isa<UsingDecl>(Pattern) || |
| 578 | isa<UnresolvedUsingValueDecl>(Pattern) || |
| 579 | isa<UnresolvedUsingTypenameDecl>(Pattern)) && |
| 580 | "pattern decl is not a using decl"); |
| 581 | assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists"); |
| 582 | InstantiatedFromUsingDecl[Inst] = Pattern; |
| 583 | } |
| 584 | |
| 585 | UsingShadowDecl * |
| 586 | ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) { |
| 587 | llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos |
| 588 | = InstantiatedFromUsingShadowDecl.find(Inst); |
| 589 | if (Pos == InstantiatedFromUsingShadowDecl.end()) |
| 590 | return 0; |
| 591 | |
| 592 | return Pos->second; |
| 593 | } |
| 594 | |
| 595 | void |
| 596 | ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, |
| 597 | UsingShadowDecl *Pattern) { |
| 598 | assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists"); |
| 599 | InstantiatedFromUsingShadowDecl[Inst] = Pattern; |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 602 | FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { |
| 603 | llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos |
| 604 | = InstantiatedFromUnnamedFieldDecl.find(Field); |
| 605 | if (Pos == InstantiatedFromUnnamedFieldDecl.end()) |
| 606 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 607 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 608 | return Pos->second; |
| 609 | } |
| 610 | |
| 611 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, |
| 612 | FieldDecl *Tmpl) { |
| 613 | assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed"); |
| 614 | assert(!Tmpl->getDeclName() && "Template field decl is not unnamed"); |
| 615 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] && |
| 616 | "Already noted what unnamed field was instantiated from"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 617 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 618 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; |
| 619 | } |
| 620 | |
Fariborz Jahanian | 14d56ef | 2011-04-27 17:14:21 +0000 | [diff] [blame] | 621 | bool ASTContext::ZeroBitfieldFollowsNonBitfield(const FieldDecl *FD, |
| 622 | const FieldDecl *LastFD) const { |
| 623 | return (FD->isBitField() && LastFD && !LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 624 | FD->getBitWidthValue(*this) == 0); |
Fariborz Jahanian | 14d56ef | 2011-04-27 17:14:21 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Fariborz Jahanian | 340fa24 | 2011-05-02 17:20:56 +0000 | [diff] [blame] | 627 | bool ASTContext::ZeroBitfieldFollowsBitfield(const FieldDecl *FD, |
| 628 | const FieldDecl *LastFD) const { |
| 629 | return (FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 630 | FD->getBitWidthValue(*this) == 0 && |
| 631 | LastFD->getBitWidthValue(*this) != 0); |
Fariborz Jahanian | 340fa24 | 2011-05-02 17:20:56 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Fariborz Jahanian | 9b3acaa | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 634 | bool ASTContext::BitfieldFollowsBitfield(const FieldDecl *FD, |
| 635 | const FieldDecl *LastFD) const { |
| 636 | return (FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 637 | FD->getBitWidthValue(*this) && |
| 638 | LastFD->getBitWidthValue(*this)); |
Fariborz Jahanian | 9b3acaa | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Chad Rosier | dd7fddb | 2011-08-04 23:34:15 +0000 | [diff] [blame] | 641 | bool ASTContext::NonBitfieldFollowsBitfield(const FieldDecl *FD, |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 642 | const FieldDecl *LastFD) const { |
| 643 | return (!FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 644 | LastFD->getBitWidthValue(*this)); |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Chad Rosier | dd7fddb | 2011-08-04 23:34:15 +0000 | [diff] [blame] | 647 | bool ASTContext::BitfieldFollowsNonBitfield(const FieldDecl *FD, |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 648 | const FieldDecl *LastFD) const { |
| 649 | return (FD->isBitField() && LastFD && !LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 650 | FD->getBitWidthValue(*this)); |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 653 | ASTContext::overridden_cxx_method_iterator |
| 654 | ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const { |
| 655 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 656 | = OverriddenMethods.find(Method); |
| 657 | if (Pos == OverriddenMethods.end()) |
| 658 | return 0; |
| 659 | |
| 660 | return Pos->second.begin(); |
| 661 | } |
| 662 | |
| 663 | ASTContext::overridden_cxx_method_iterator |
| 664 | ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const { |
| 665 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 666 | = OverriddenMethods.find(Method); |
| 667 | if (Pos == OverriddenMethods.end()) |
| 668 | return 0; |
| 669 | |
| 670 | return Pos->second.end(); |
| 671 | } |
| 672 | |
Argyrios Kyrtzidis | c91e9f4 | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 673 | unsigned |
| 674 | ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const { |
| 675 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 676 | = OverriddenMethods.find(Method); |
| 677 | if (Pos == OverriddenMethods.end()) |
| 678 | return 0; |
| 679 | |
| 680 | return Pos->second.size(); |
| 681 | } |
| 682 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 683 | void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method, |
| 684 | const CXXMethodDecl *Overridden) { |
| 685 | OverriddenMethods[Method].push_back(Overridden); |
| 686 | } |
| 687 | |
Douglas Gregor | e664977 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 688 | void ASTContext::addedLocalImportDecl(ImportDecl *Import) { |
| 689 | assert(!Import->NextLocalImport && "Import declaration already in the chain"); |
| 690 | assert(!Import->isFromASTFile() && "Non-local import declaration"); |
| 691 | if (!FirstLocalImport) { |
| 692 | FirstLocalImport = Import; |
| 693 | LastLocalImport = Import; |
| 694 | return; |
| 695 | } |
| 696 | |
| 697 | LastLocalImport->NextLocalImport = Import; |
| 698 | LastLocalImport = Import; |
| 699 | } |
| 700 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 701 | //===----------------------------------------------------------------------===// |
| 702 | // Type Sizing and Analysis |
| 703 | //===----------------------------------------------------------------------===// |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 704 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 705 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 706 | /// scalar floating point type. |
| 707 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 708 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 709 | assert(BT && "Not a floating point type!"); |
| 710 | switch (BT->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 711 | default: llvm_unreachable("Not a floating point type!"); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 712 | case BuiltinType::Half: return Target->getHalfFormat(); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 713 | case BuiltinType::Float: return Target->getFloatFormat(); |
| 714 | case BuiltinType::Double: return Target->getDoubleFormat(); |
| 715 | case BuiltinType::LongDouble: return Target->getLongDoubleFormat(); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 716 | } |
| 717 | } |
| 718 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 719 | /// getDeclAlign - Return a conservative estimate of the alignment of the |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 720 | /// specified decl. Note that bitfields do not have a valid alignment, so |
| 721 | /// this method will assert on them. |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 722 | /// If @p RefAsPointee, references are treated like their underlying type |
| 723 | /// (for alignof), else they're treated like pointers (for CodeGen). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 724 | CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) const { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 725 | unsigned Align = Target->getCharWidth(); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 726 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 727 | bool UseAlignAttrOnly = false; |
| 728 | if (unsigned AlignFromAttr = D->getMaxAlignment()) { |
| 729 | Align = AlignFromAttr; |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 730 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 731 | // __attribute__((aligned)) can increase or decrease alignment |
| 732 | // *except* on a struct or struct member, where it only increases |
| 733 | // alignment unless 'packed' is also specified. |
| 734 | // |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 735 | // It is an error for alignas to decrease alignment, so we can |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 736 | // ignore that possibility; Sema should diagnose it. |
| 737 | if (isa<FieldDecl>(D)) { |
| 738 | UseAlignAttrOnly = D->hasAttr<PackedAttr>() || |
| 739 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
| 740 | } else { |
| 741 | UseAlignAttrOnly = true; |
| 742 | } |
| 743 | } |
Fariborz Jahanian | 78a7d7d | 2011-05-05 21:19:14 +0000 | [diff] [blame] | 744 | else if (isa<FieldDecl>(D)) |
| 745 | UseAlignAttrOnly = |
| 746 | D->hasAttr<PackedAttr>() || |
| 747 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 748 | |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 749 | // If we're using the align attribute only, just ignore everything |
| 750 | // else about the declaration and its type. |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 751 | if (UseAlignAttrOnly) { |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 752 | // do nothing |
| 753 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 754 | } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 755 | QualType T = VD->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 756 | if (const ReferenceType* RT = T->getAs<ReferenceType>()) { |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 757 | if (RefAsPointee) |
| 758 | T = RT->getPointeeType(); |
| 759 | else |
| 760 | T = getPointerType(RT->getPointeeType()); |
| 761 | } |
| 762 | if (!T->isIncompleteType() && !T->isFunctionType()) { |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 763 | // Adjust alignments of declarations with array type by the |
| 764 | // large-array alignment on the target. |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 765 | unsigned MinWidth = Target->getLargeArrayMinWidth(); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 766 | const ArrayType *arrayType; |
| 767 | if (MinWidth && (arrayType = getAsArrayType(T))) { |
| 768 | if (isa<VariableArrayType>(arrayType)) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 769 | Align = std::max(Align, Target->getLargeArrayAlign()); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 770 | else if (isa<ConstantArrayType>(arrayType) && |
| 771 | MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType))) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 772 | Align = std::max(Align, Target->getLargeArrayAlign()); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 773 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 774 | // Walk through any array types while we're at it. |
| 775 | T = getBaseElementType(arrayType); |
| 776 | } |
Chad Rosier | 9f1210c | 2011-07-26 07:03:04 +0000 | [diff] [blame] | 777 | Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 778 | } |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 779 | |
| 780 | // Fields can be subject to extra alignment constraints, like if |
| 781 | // the field is packed, the struct is packed, or the struct has a |
| 782 | // a max-field-alignment constraint (#pragma pack). So calculate |
| 783 | // the actual alignment of the field within the struct, and then |
| 784 | // (as we're expected to) constrain that by the alignment of the type. |
| 785 | if (const FieldDecl *field = dyn_cast<FieldDecl>(VD)) { |
| 786 | // So calculate the alignment of the field. |
| 787 | const ASTRecordLayout &layout = getASTRecordLayout(field->getParent()); |
| 788 | |
| 789 | // Start with the record's overall alignment. |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 790 | unsigned fieldAlign = toBits(layout.getAlignment()); |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 791 | |
| 792 | // Use the GCD of that and the offset within the record. |
| 793 | uint64_t offset = layout.getFieldOffset(field->getFieldIndex()); |
| 794 | if (offset > 0) { |
| 795 | // Alignment is always a power of 2, so the GCD will be a power of 2, |
| 796 | // which means we get to do this crazy thing instead of Euclid's. |
| 797 | uint64_t lowBitOfOffset = offset & (~offset + 1); |
| 798 | if (lowBitOfOffset < fieldAlign) |
| 799 | fieldAlign = static_cast<unsigned>(lowBitOfOffset); |
| 800 | } |
| 801 | |
| 802 | Align = std::min(Align, fieldAlign); |
Charles Davis | 05f6247 | 2010-02-23 04:52:00 +0000 | [diff] [blame] | 803 | } |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 804 | } |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 805 | |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 806 | return toCharUnitsFromBits(Align); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 807 | } |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 808 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 809 | std::pair<CharUnits, CharUnits> |
Ken Dyck | bee5a79 | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 810 | ASTContext::getTypeInfoInChars(const Type *T) const { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 811 | std::pair<uint64_t, unsigned> Info = getTypeInfo(T); |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 812 | return std::make_pair(toCharUnitsFromBits(Info.first), |
| 813 | toCharUnitsFromBits(Info.second)); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | std::pair<CharUnits, CharUnits> |
Ken Dyck | bee5a79 | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 817 | ASTContext::getTypeInfoInChars(QualType T) const { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 818 | return getTypeInfoInChars(T.getTypePtr()); |
| 819 | } |
| 820 | |
Daniel Dunbar | bc5419a | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 821 | std::pair<uint64_t, unsigned> ASTContext::getTypeInfo(const Type *T) const { |
| 822 | TypeInfoMap::iterator it = MemoizedTypeInfo.find(T); |
| 823 | if (it != MemoizedTypeInfo.end()) |
| 824 | return it->second; |
| 825 | |
| 826 | std::pair<uint64_t, unsigned> Info = getTypeInfoImpl(T); |
| 827 | MemoizedTypeInfo.insert(std::make_pair(T, Info)); |
| 828 | return Info; |
| 829 | } |
| 830 | |
| 831 | /// getTypeInfoImpl - Return the size of the specified type, in bits. This |
| 832 | /// method does not work on incomplete types. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 833 | /// |
| 834 | /// FIXME: Pointers into different addr spaces could have different sizes and |
| 835 | /// alignment requirements: getPointerInfo should take an AddrSpace, this |
| 836 | /// should take a QualType, &c. |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 837 | std::pair<uint64_t, unsigned> |
Daniel Dunbar | bc5419a | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 838 | ASTContext::getTypeInfoImpl(const Type *T) const { |
Mike Stump | 5e30100 | 2009-02-27 18:32:39 +0000 | [diff] [blame] | 839 | uint64_t Width=0; |
| 840 | unsigned Align=8; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 841 | switch (T->getTypeClass()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 842 | #define TYPE(Class, Base) |
| 843 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 844 | #define NON_CANONICAL_TYPE(Class, Base) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 845 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 846 | #include "clang/AST/TypeNodes.def" |
John McCall | d3d49bb | 2011-06-28 16:49:23 +0000 | [diff] [blame] | 847 | llvm_unreachable("Should not see dependent types"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 848 | |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 849 | case Type::FunctionNoProto: |
| 850 | case Type::FunctionProto: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 851 | // GCC extension: alignof(function) = 32 bits |
| 852 | Width = 0; |
| 853 | Align = 32; |
| 854 | break; |
| 855 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 856 | case Type::IncompleteArray: |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 857 | case Type::VariableArray: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 858 | Width = 0; |
| 859 | Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); |
| 860 | break; |
| 861 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 862 | case Type::ConstantArray: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 863 | const ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 864 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 865 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); |
Abramo Bagnara | fea966a | 2011-12-13 11:23:52 +0000 | [diff] [blame] | 866 | uint64_t Size = CAT->getSize().getZExtValue(); |
Daniel Dunbar | bc5419a | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 867 | assert((Size == 0 || EltInfo.first <= (uint64_t)(-1)/Size) && |
| 868 | "Overflow in array type bit size evaluation"); |
Abramo Bagnara | fea966a | 2011-12-13 11:23:52 +0000 | [diff] [blame] | 869 | Width = EltInfo.first*Size; |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 870 | Align = EltInfo.second; |
Argyrios Kyrtzidis | cd88b41 | 2011-04-26 21:05:39 +0000 | [diff] [blame] | 871 | Width = llvm::RoundUpToAlignment(Width, Align); |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 872 | break; |
Christopher Lamb | 5c09a02 | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 873 | } |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 874 | case Type::ExtVector: |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 875 | case Type::Vector: { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 876 | const VectorType *VT = cast<VectorType>(T); |
| 877 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType()); |
| 878 | Width = EltInfo.first*VT->getNumElements(); |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 879 | Align = Width; |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 880 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 881 | // This happens for non-power-of-2 length vectors. |
Dan Gohman | 8eefcd3 | 2010-04-21 23:32:43 +0000 | [diff] [blame] | 882 | if (Align & (Align-1)) { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 883 | Align = llvm::NextPowerOf2(Align); |
| 884 | Width = llvm::RoundUpToAlignment(Width, Align); |
| 885 | } |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 886 | break; |
| 887 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 888 | |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 889 | case Type::Builtin: |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 890 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 891 | default: llvm_unreachable("Unknown builtin type!"); |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 892 | case BuiltinType::Void: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 893 | // GCC extension: alignof(void) = 8 bits. |
| 894 | Width = 0; |
| 895 | Align = 8; |
| 896 | break; |
| 897 | |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 898 | case BuiltinType::Bool: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 899 | Width = Target->getBoolWidth(); |
| 900 | Align = Target->getBoolAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 901 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 902 | case BuiltinType::Char_S: |
| 903 | case BuiltinType::Char_U: |
| 904 | case BuiltinType::UChar: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 905 | case BuiltinType::SChar: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 906 | Width = Target->getCharWidth(); |
| 907 | Align = Target->getCharAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 908 | break; |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 909 | case BuiltinType::WChar_S: |
| 910 | case BuiltinType::WChar_U: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 911 | Width = Target->getWCharWidth(); |
| 912 | Align = Target->getWCharAlign(); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 913 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 914 | case BuiltinType::Char16: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 915 | Width = Target->getChar16Width(); |
| 916 | Align = Target->getChar16Align(); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 917 | break; |
| 918 | case BuiltinType::Char32: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 919 | Width = Target->getChar32Width(); |
| 920 | Align = Target->getChar32Align(); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 921 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 922 | case BuiltinType::UShort: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 923 | case BuiltinType::Short: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 924 | Width = Target->getShortWidth(); |
| 925 | Align = Target->getShortAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 926 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 927 | case BuiltinType::UInt: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 928 | case BuiltinType::Int: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 929 | Width = Target->getIntWidth(); |
| 930 | Align = Target->getIntAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 931 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 932 | case BuiltinType::ULong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 933 | case BuiltinType::Long: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 934 | Width = Target->getLongWidth(); |
| 935 | Align = Target->getLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 936 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 937 | case BuiltinType::ULongLong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 938 | case BuiltinType::LongLong: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 939 | Width = Target->getLongLongWidth(); |
| 940 | Align = Target->getLongLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 941 | break; |
Chris Lattner | ec16cb9 | 2009-04-30 02:55:13 +0000 | [diff] [blame] | 942 | case BuiltinType::Int128: |
| 943 | case BuiltinType::UInt128: |
| 944 | Width = 128; |
| 945 | Align = 128; // int128_t is 128-bit aligned on all targets. |
| 946 | break; |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 947 | case BuiltinType::Half: |
| 948 | Width = Target->getHalfWidth(); |
| 949 | Align = Target->getHalfAlign(); |
| 950 | break; |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 951 | case BuiltinType::Float: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 952 | Width = Target->getFloatWidth(); |
| 953 | Align = Target->getFloatAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 954 | break; |
| 955 | case BuiltinType::Double: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 956 | Width = Target->getDoubleWidth(); |
| 957 | Align = Target->getDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 958 | break; |
| 959 | case BuiltinType::LongDouble: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 960 | Width = Target->getLongDoubleWidth(); |
| 961 | Align = Target->getLongDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 962 | break; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 963 | case BuiltinType::NullPtr: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 964 | Width = Target->getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) |
| 965 | Align = Target->getPointerAlign(0); // == sizeof(void*) |
Sebastian Redl | 1590d9c | 2009-05-27 19:34:06 +0000 | [diff] [blame] | 966 | break; |
Fariborz Jahanian | e04f5fc | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 967 | case BuiltinType::ObjCId: |
| 968 | case BuiltinType::ObjCClass: |
| 969 | case BuiltinType::ObjCSel: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 970 | Width = Target->getPointerWidth(0); |
| 971 | Align = Target->getPointerAlign(0); |
Fariborz Jahanian | e04f5fc | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 972 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 973 | } |
Chris Lattner | bfef6d7 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 974 | break; |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 975 | case Type::ObjCObjectPointer: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 976 | Width = Target->getPointerWidth(0); |
| 977 | Align = Target->getPointerAlign(0); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 978 | break; |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 979 | case Type::BlockPointer: { |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 980 | unsigned AS = getTargetAddressSpace( |
| 981 | cast<BlockPointerType>(T)->getPointeeType()); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 982 | Width = Target->getPointerWidth(AS); |
| 983 | Align = Target->getPointerAlign(AS); |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 984 | break; |
| 985 | } |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 986 | case Type::LValueReference: |
| 987 | case Type::RValueReference: { |
| 988 | // alignof and sizeof should never enter this code path here, so we go |
| 989 | // the pointer route. |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 990 | unsigned AS = getTargetAddressSpace( |
| 991 | cast<ReferenceType>(T)->getPointeeType()); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 992 | Width = Target->getPointerWidth(AS); |
| 993 | Align = Target->getPointerAlign(AS); |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 994 | break; |
| 995 | } |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 996 | case Type::Pointer: { |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 997 | unsigned AS = getTargetAddressSpace(cast<PointerType>(T)->getPointeeType()); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 998 | Width = Target->getPointerWidth(AS); |
| 999 | Align = Target->getPointerAlign(AS); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 1000 | break; |
| 1001 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1002 | case Type::MemberPointer: { |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 1003 | const MemberPointerType *MPT = cast<MemberPointerType>(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1004 | std::pair<uint64_t, unsigned> PtrDiffInfo = |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 1005 | getTypeInfo(getPointerDiffType()); |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 1006 | Width = PtrDiffInfo.first * ABI->getMemberPointerSize(MPT); |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 1007 | Align = PtrDiffInfo.second; |
| 1008 | break; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1009 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1010 | case Type::Complex: { |
| 1011 | // Complex types have the same alignment as their elements, but twice the |
| 1012 | // size. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1013 | std::pair<uint64_t, unsigned> EltInfo = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1014 | getTypeInfo(cast<ComplexType>(T)->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1015 | Width = EltInfo.first*2; |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1016 | Align = EltInfo.second; |
| 1017 | break; |
| 1018 | } |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1019 | case Type::ObjCObject: |
| 1020 | return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr()); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1021 | case Type::ObjCInterface: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1022 | const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1023 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1024 | Width = toBits(Layout.getSize()); |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 1025 | Align = toBits(Layout.getAlignment()); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1026 | break; |
| 1027 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1028 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1029 | case Type::Enum: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1030 | const TagType *TT = cast<TagType>(T); |
| 1031 | |
| 1032 | if (TT->getDecl()->isInvalidDecl()) { |
Douglas Gregor | 22ce41d | 2011-04-20 17:29:44 +0000 | [diff] [blame] | 1033 | Width = 8; |
| 1034 | Align = 8; |
Chris Lattner | 8389eab | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 1035 | break; |
| 1036 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1037 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1038 | if (const EnumType *ET = dyn_cast<EnumType>(TT)) |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1039 | return getTypeInfo(ET->getDecl()->getIntegerType()); |
| 1040 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1041 | const RecordType *RT = cast<RecordType>(TT); |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1042 | const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1043 | Width = toBits(Layout.getSize()); |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 1044 | Align = toBits(Layout.getAlignment()); |
Chris Lattner | dc0d73e | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 1045 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1046 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1047 | |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1048 | case Type::SubstTemplateTypeParm: |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1049 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> |
| 1050 | getReplacementType().getTypePtr()); |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1051 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1052 | case Type::Auto: { |
| 1053 | const AutoType *A = cast<AutoType>(T); |
| 1054 | assert(A->isDeduced() && "Cannot request the size of a dependent type"); |
Matt Beaumont-Gay | dc856af | 2011-02-22 20:00:16 +0000 | [diff] [blame] | 1055 | return getTypeInfo(A->getDeducedType().getTypePtr()); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1058 | case Type::Paren: |
| 1059 | return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr()); |
| 1060 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1061 | case Type::Typedef: { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1062 | const TypedefNameDecl *Typedef = cast<TypedefType>(T)->getDecl(); |
Douglas Gregor | df1367a | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 1063 | std::pair<uint64_t, unsigned> Info |
| 1064 | = getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); |
Chris Lattner | c1de52d | 2011-02-19 22:55:41 +0000 | [diff] [blame] | 1065 | // If the typedef has an aligned attribute on it, it overrides any computed |
| 1066 | // alignment we have. This violates the GCC documentation (which says that |
| 1067 | // attribute(aligned) can only round up) but matches its implementation. |
| 1068 | if (unsigned AttrAlign = Typedef->getMaxAlignment()) |
| 1069 | Align = AttrAlign; |
| 1070 | else |
| 1071 | Align = Info.second; |
Douglas Gregor | df1367a | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 1072 | Width = Info.first; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1073 | break; |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1074 | } |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1075 | |
| 1076 | case Type::TypeOfExpr: |
| 1077 | return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType() |
| 1078 | .getTypePtr()); |
| 1079 | |
| 1080 | case Type::TypeOf: |
| 1081 | return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr()); |
| 1082 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 1083 | case Type::Decltype: |
| 1084 | return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType() |
| 1085 | .getTypePtr()); |
| 1086 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1087 | case Type::UnaryTransform: |
| 1088 | return getTypeInfo(cast<UnaryTransformType>(T)->getUnderlyingType()); |
| 1089 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1090 | case Type::Elaborated: |
| 1091 | return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1092 | |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 1093 | case Type::Attributed: |
| 1094 | return getTypeInfo( |
| 1095 | cast<AttributedType>(T)->getEquivalentType().getTypePtr()); |
| 1096 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1097 | case Type::TemplateSpecialization: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1098 | assert(getCanonicalType(T) != T && |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1099 | "Cannot request the size of a dependent type"); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1100 | const TemplateSpecializationType *TST = cast<TemplateSpecializationType>(T); |
| 1101 | // A type alias template specialization may refer to a typedef with the |
| 1102 | // aligned attribute on it. |
| 1103 | if (TST->isTypeAlias()) |
| 1104 | return getTypeInfo(TST->getAliasedType().getTypePtr()); |
| 1105 | else |
| 1106 | return getTypeInfo(getCanonicalType(T)); |
| 1107 | } |
| 1108 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1109 | case Type::Atomic: { |
Eli Friedman | 2be4607 | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 1110 | std::pair<uint64_t, unsigned> Info |
| 1111 | = getTypeInfo(cast<AtomicType>(T)->getValueType()); |
| 1112 | Width = Info.first; |
| 1113 | Align = Info.second; |
| 1114 | if (Width != 0 && Width <= Target->getMaxAtomicPromoteWidth() && |
| 1115 | llvm::isPowerOf2_64(Width)) { |
| 1116 | // We can potentially perform lock-free atomic operations for this |
| 1117 | // type; promote the alignment appropriately. |
| 1118 | // FIXME: We could potentially promote the width here as well... |
| 1119 | // is that worthwhile? (Non-struct atomic types generally have |
| 1120 | // power-of-two size anyway, but structs might not. Requires a bit |
| 1121 | // of implementation work to make sure we zero out the extra bits.) |
| 1122 | Align = static_cast<unsigned>(Width); |
| 1123 | } |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1126 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1127 | |
Eli Friedman | 2be4607 | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 1128 | assert(llvm::isPowerOf2_32(Align) && "Alignment must be power of 2"); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1129 | return std::make_pair(Width, Align); |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1132 | /// toCharUnitsFromBits - Convert a size in bits to a size in characters. |
| 1133 | CharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const { |
| 1134 | return CharUnits::fromQuantity(BitSize / getCharWidth()); |
| 1135 | } |
| 1136 | |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1137 | /// toBits - Convert a size in characters to a size in characters. |
| 1138 | int64_t ASTContext::toBits(CharUnits CharSize) const { |
| 1139 | return CharSize.getQuantity() * getCharWidth(); |
| 1140 | } |
| 1141 | |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1142 | /// getTypeSizeInChars - Return the size of the specified type, in characters. |
| 1143 | /// This method does not work on incomplete types. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1144 | CharUnits ASTContext::getTypeSizeInChars(QualType 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 | } |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1147 | CharUnits ASTContext::getTypeSizeInChars(const Type *T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1148 | return toCharUnitsFromBits(getTypeSize(T)); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
Ken Dyck | 16e20cc | 2010-01-26 17:25:18 +0000 | [diff] [blame] | 1151 | /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1152 | /// characters. This method does not work on incomplete types. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1153 | CharUnits ASTContext::getTypeAlignInChars(QualType 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 | } |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1156 | CharUnits ASTContext::getTypeAlignInChars(const Type *T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1157 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1160 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 1161 | /// type for the current target in bits. This can be different than the ABI |
| 1162 | /// alignment in cases where it is beneficial for performance to overalign |
| 1163 | /// a data type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1164 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) const { |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1165 | unsigned ABIAlign = getTypeAlign(T); |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1166 | |
| 1167 | // Double and long long should be naturally aligned if possible. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1168 | if (const ComplexType* CT = T->getAs<ComplexType>()) |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1169 | T = CT->getElementType().getTypePtr(); |
| 1170 | if (T->isSpecificBuiltinType(BuiltinType::Double) || |
Chad Rosier | cde7a1d | 2012-03-21 20:20:47 +0000 | [diff] [blame] | 1171 | T->isSpecificBuiltinType(BuiltinType::LongLong) || |
| 1172 | T->isSpecificBuiltinType(BuiltinType::ULongLong)) |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1173 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); |
| 1174 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1175 | return ABIAlign; |
| 1176 | } |
| 1177 | |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1178 | /// DeepCollectObjCIvars - |
| 1179 | /// This routine first collects all declared, but not synthesized, ivars in |
| 1180 | /// super class and then collects all ivars, including those synthesized for |
| 1181 | /// current class. This routine is used for implementation of current class |
| 1182 | /// when all ivars, declared and synthesized are known. |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 1183 | /// |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1184 | void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 1185 | bool leafClass, |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 1186 | SmallVectorImpl<const ObjCIvarDecl*> &Ivars) const { |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1187 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
| 1188 | DeepCollectObjCIvars(SuperClass, false, Ivars); |
| 1189 | if (!leafClass) { |
| 1190 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 1191 | E = OI->ivar_end(); I != E; ++I) |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1192 | Ivars.push_back(&*I); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 1193 | } else { |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 1194 | ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI); |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 1195 | for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv; |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 1196 | Iv= Iv->getNextIvar()) |
| 1197 | Ivars.push_back(Iv); |
| 1198 | } |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1201 | /// CollectInheritedProtocols - Collect all protocols in current class and |
| 1202 | /// those inherited by it. |
| 1203 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 1204 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1205 | if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1206 | // We can use protocol_iterator here instead of |
| 1207 | // all_referenced_protocol_iterator since we are walking all categories. |
| 1208 | for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(), |
| 1209 | PE = OI->all_referenced_protocol_end(); P != PE; ++P) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1210 | ObjCProtocolDecl *Proto = (*P); |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1211 | Protocols.insert(Proto->getCanonicalDecl()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1212 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 1213 | PE = Proto->protocol_end(); P != PE; ++P) { |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1214 | Protocols.insert((*P)->getCanonicalDecl()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1215 | CollectInheritedProtocols(*P, Protocols); |
| 1216 | } |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 1217 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1218 | |
| 1219 | // Categories of this Interface. |
| 1220 | for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList(); |
| 1221 | CDeclChain; CDeclChain = CDeclChain->getNextClassCategory()) |
| 1222 | CollectInheritedProtocols(CDeclChain, Protocols); |
| 1223 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) |
| 1224 | while (SD) { |
| 1225 | CollectInheritedProtocols(SD, Protocols); |
| 1226 | SD = SD->getSuperClass(); |
| 1227 | } |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1228 | } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1229 | for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(), |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1230 | PE = OC->protocol_end(); P != PE; ++P) { |
| 1231 | ObjCProtocolDecl *Proto = (*P); |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1232 | Protocols.insert(Proto->getCanonicalDecl()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1233 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 1234 | PE = Proto->protocol_end(); P != PE; ++P) |
| 1235 | CollectInheritedProtocols(*P, Protocols); |
| 1236 | } |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1237 | } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1238 | for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(), |
| 1239 | PE = OP->protocol_end(); P != PE; ++P) { |
| 1240 | ObjCProtocolDecl *Proto = (*P); |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1241 | Protocols.insert(Proto->getCanonicalDecl()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1242 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 1243 | PE = Proto->protocol_end(); P != PE; ++P) |
| 1244 | CollectInheritedProtocols(*P, Protocols); |
| 1245 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1246 | } |
| 1247 | } |
| 1248 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1249 | unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const { |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1250 | unsigned count = 0; |
| 1251 | // Count ivars declared in class extension. |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 1252 | for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl; |
| 1253 | CDecl = CDecl->getNextClassExtension()) |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1254 | count += CDecl->ivar_size(); |
| 1255 | |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1256 | // Count ivar defined in this class's implementation. This |
| 1257 | // includes synthesized ivars. |
| 1258 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1259 | count += ImplDecl->ivar_size(); |
| 1260 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 1261 | return count; |
| 1262 | } |
| 1263 | |
Argyrios Kyrtzidis | 8deabc1 | 2012-02-03 05:58:16 +0000 | [diff] [blame] | 1264 | bool ASTContext::isSentinelNullExpr(const Expr *E) { |
| 1265 | if (!E) |
| 1266 | return false; |
| 1267 | |
| 1268 | // nullptr_t is always treated as null. |
| 1269 | if (E->getType()->isNullPtrType()) return true; |
| 1270 | |
| 1271 | if (E->getType()->isAnyPointerType() && |
| 1272 | E->IgnoreParenCasts()->isNullPointerConstant(*this, |
| 1273 | Expr::NPC_ValueDependentIsNull)) |
| 1274 | return true; |
| 1275 | |
| 1276 | // Unfortunately, __null has type 'int'. |
| 1277 | if (isa<GNUNullExpr>(E)) return true; |
| 1278 | |
| 1279 | return false; |
| 1280 | } |
| 1281 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1282 | /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. |
| 1283 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
| 1284 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1285 | I = ObjCImpls.find(D); |
| 1286 | if (I != ObjCImpls.end()) |
| 1287 | return cast<ObjCImplementationDecl>(I->second); |
| 1288 | return 0; |
| 1289 | } |
| 1290 | /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. |
| 1291 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
| 1292 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1293 | I = ObjCImpls.find(D); |
| 1294 | if (I != ObjCImpls.end()) |
| 1295 | return cast<ObjCCategoryImplDecl>(I->second); |
| 1296 | return 0; |
| 1297 | } |
| 1298 | |
| 1299 | /// \brief Set the implementation of ObjCInterfaceDecl. |
| 1300 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
| 1301 | ObjCImplementationDecl *ImplD) { |
| 1302 | assert(IFaceD && ImplD && "Passed null params"); |
| 1303 | ObjCImpls[IFaceD] = ImplD; |
| 1304 | } |
| 1305 | /// \brief Set the implementation of ObjCCategoryDecl. |
| 1306 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
| 1307 | ObjCCategoryImplDecl *ImplD) { |
| 1308 | assert(CatD && ImplD && "Passed null params"); |
| 1309 | ObjCImpls[CatD] = ImplD; |
| 1310 | } |
| 1311 | |
Argyrios Kyrtzidis | 87ec9c2 | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 1312 | ObjCInterfaceDecl *ASTContext::getObjContainingInterface(NamedDecl *ND) const { |
| 1313 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ND->getDeclContext())) |
| 1314 | return ID; |
| 1315 | if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(ND->getDeclContext())) |
| 1316 | return CD->getClassInterface(); |
| 1317 | if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(ND->getDeclContext())) |
| 1318 | return IMD->getClassInterface(); |
| 1319 | |
| 1320 | return 0; |
| 1321 | } |
| 1322 | |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1323 | /// \brief Get the copy initialization expression of VarDecl,or NULL if |
| 1324 | /// none exists. |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1325 | Expr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) { |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1326 | assert(VD && "Passed null params"); |
| 1327 | assert(VD->hasAttr<BlocksAttr>() && |
| 1328 | "getBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1329 | llvm::DenseMap<const VarDecl*, Expr*>::iterator |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1330 | I = BlockVarCopyInits.find(VD); |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1331 | return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0; |
| 1332 | } |
| 1333 | |
| 1334 | /// \brief Set the copy inialization expression of a block var decl. |
| 1335 | void ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) { |
| 1336 | assert(VD && Init && "Passed null params"); |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1337 | assert(VD->hasAttr<BlocksAttr>() && |
| 1338 | "setBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1339 | BlockVarCopyInits[VD] = Init; |
| 1340 | } |
| 1341 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1342 | /// \brief Allocate an uninitialized TypeSourceInfo. |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1343 | /// |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1344 | /// The caller should initialize the memory held by TypeSourceInfo using |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1345 | /// the TypeLoc wrappers. |
| 1346 | /// |
| 1347 | /// \param T the type that will be the basis for type source info. This type |
| 1348 | /// should refer to how the declarator was written in source code, not to |
| 1349 | /// what type semantic analysis resolved the declarator to. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1350 | TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1351 | unsigned DataSize) const { |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1352 | if (!DataSize) |
| 1353 | DataSize = TypeLoc::getFullDataSizeForType(T); |
| 1354 | else |
| 1355 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1356 | "incorrect data size provided to CreateTypeSourceInfo!"); |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1357 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1358 | TypeSourceInfo *TInfo = |
| 1359 | (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8); |
| 1360 | new (TInfo) TypeSourceInfo(T); |
| 1361 | return TInfo; |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1364 | TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1365 | SourceLocation L) const { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1366 | TypeSourceInfo *DI = CreateTypeSourceInfo(T); |
Douglas Gregor | c21c7e9 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 1367 | DI->getTypeLoc().initialize(const_cast<ASTContext &>(*this), L); |
John McCall | a4eb74d | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 1368 | return DI; |
| 1369 | } |
| 1370 | |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1371 | const ASTRecordLayout & |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1372 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const { |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1373 | return getObjCLayout(D, 0); |
| 1374 | } |
| 1375 | |
| 1376 | const ASTRecordLayout & |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1377 | ASTContext::getASTObjCImplementationLayout( |
| 1378 | const ObjCImplementationDecl *D) const { |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1379 | return getObjCLayout(D->getClassInterface(), D); |
| 1380 | } |
| 1381 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1382 | //===----------------------------------------------------------------------===// |
| 1383 | // Type creation/memoization methods |
| 1384 | //===----------------------------------------------------------------------===// |
| 1385 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1386 | QualType |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1387 | ASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const { |
| 1388 | unsigned fastQuals = quals.getFastQualifiers(); |
| 1389 | quals.removeFastQualifiers(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1390 | |
| 1391 | // Check if we've already instantiated this type. |
| 1392 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1393 | ExtQuals::Profile(ID, baseType, quals); |
| 1394 | void *insertPos = 0; |
| 1395 | if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, insertPos)) { |
| 1396 | assert(eq->getQualifiers() == quals); |
| 1397 | return QualType(eq, fastQuals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1400 | // If the base type is not canonical, make the appropriate canonical type. |
| 1401 | QualType canon; |
| 1402 | if (!baseType->isCanonicalUnqualified()) { |
| 1403 | SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1404 | canonSplit.Quals.addConsistentQualifiers(quals); |
| 1405 | canon = getExtQualType(canonSplit.Ty, canonSplit.Quals); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1406 | |
| 1407 | // Re-find the insert position. |
| 1408 | (void) ExtQualNodes.FindNodeOrInsertPos(ID, insertPos); |
| 1409 | } |
| 1410 | |
| 1411 | ExtQuals *eq = new (*this, TypeAlignment) ExtQuals(baseType, canon, quals); |
| 1412 | ExtQualNodes.InsertNode(eq, insertPos); |
| 1413 | return QualType(eq, fastQuals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1416 | QualType |
| 1417 | ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) const { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1418 | QualType CanT = getCanonicalType(T); |
| 1419 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1420 | return T; |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1421 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1422 | // If we are composing extended qualifiers together, merge together |
| 1423 | // into one ExtQuals node. |
| 1424 | QualifierCollector Quals; |
| 1425 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1426 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1427 | // If this type already has an address space specified, it cannot get |
| 1428 | // another one. |
| 1429 | assert(!Quals.hasAddressSpace() && |
| 1430 | "Type cannot be in multiple addr spaces!"); |
| 1431 | Quals.addAddressSpace(AddressSpace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1432 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1433 | return getExtQualType(TypeNode, Quals); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1436 | QualType ASTContext::getObjCGCQualType(QualType T, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1437 | Qualifiers::GC GCAttr) const { |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1438 | QualType CanT = getCanonicalType(T); |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1439 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1440 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1441 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1442 | if (const PointerType *ptr = T->getAs<PointerType>()) { |
| 1443 | QualType Pointee = ptr->getPointeeType(); |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 1444 | if (Pointee->isAnyPointerType()) { |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1445 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 1446 | return getPointerType(ResultType); |
| 1447 | } |
| 1448 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1449 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1450 | // If we are composing extended qualifiers together, merge together |
| 1451 | // into one ExtQuals node. |
| 1452 | QualifierCollector Quals; |
| 1453 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1454 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1455 | // If this type already has an ObjCGC specified, it cannot get |
| 1456 | // another one. |
| 1457 | assert(!Quals.hasObjCGCAttr() && |
| 1458 | "Type cannot have multiple ObjCGCs!"); |
| 1459 | Quals.addObjCGCAttr(GCAttr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1460 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1461 | return getExtQualType(TypeNode, Quals); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1462 | } |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1463 | |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 1464 | const FunctionType *ASTContext::adjustFunctionType(const FunctionType *T, |
| 1465 | FunctionType::ExtInfo Info) { |
| 1466 | if (T->getExtInfo() == Info) |
| 1467 | return T; |
| 1468 | |
| 1469 | QualType Result; |
| 1470 | if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) { |
| 1471 | Result = getFunctionNoProtoType(FNPT->getResultType(), Info); |
| 1472 | } else { |
| 1473 | const FunctionProtoType *FPT = cast<FunctionProtoType>(T); |
| 1474 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 1475 | EPI.ExtInfo = Info; |
| 1476 | Result = getFunctionType(FPT->getResultType(), FPT->arg_type_begin(), |
| 1477 | FPT->getNumArgs(), EPI); |
| 1478 | } |
| 1479 | |
| 1480 | return cast<FunctionType>(Result.getTypePtr()); |
| 1481 | } |
| 1482 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1483 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 1484 | /// number with the specified element type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1485 | QualType ASTContext::getComplexType(QualType T) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1486 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1487 | // structure. |
| 1488 | llvm::FoldingSetNodeID ID; |
| 1489 | ComplexType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1490 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1491 | void *InsertPos = 0; |
| 1492 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1493 | return QualType(CT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1494 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1495 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1496 | // so fill in the canonical type field. |
| 1497 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1498 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1499 | Canonical = getComplexType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1500 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1501 | // Get the new insert position for the node we care about. |
| 1502 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1503 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1504 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1505 | ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1506 | Types.push_back(New); |
| 1507 | ComplexTypes.InsertNode(New, InsertPos); |
| 1508 | return QualType(New, 0); |
| 1509 | } |
| 1510 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1511 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 1512 | /// the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1513 | QualType ASTContext::getPointerType(QualType T) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1514 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1515 | // structure. |
| 1516 | llvm::FoldingSetNodeID ID; |
| 1517 | PointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1518 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1519 | void *InsertPos = 0; |
| 1520 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1521 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1522 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1523 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1524 | // so fill in the canonical type field. |
| 1525 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1526 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1527 | Canonical = getPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1528 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1529 | // Get the new insert position for the node we care about. |
| 1530 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1531 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1532 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1533 | PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1534 | Types.push_back(New); |
| 1535 | PointerTypes.InsertNode(New, InsertPos); |
| 1536 | return QualType(New, 0); |
| 1537 | } |
| 1538 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1539 | /// getBlockPointerType - Return the uniqued reference to the type for |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1540 | /// a pointer to the specified block. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1541 | QualType ASTContext::getBlockPointerType(QualType T) const { |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1542 | assert(T->isFunctionType() && "block of function types only"); |
| 1543 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1544 | // structure. |
| 1545 | llvm::FoldingSetNodeID ID; |
| 1546 | BlockPointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1547 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1548 | void *InsertPos = 0; |
| 1549 | if (BlockPointerType *PT = |
| 1550 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1551 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1552 | |
| 1553 | // 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] | 1554 | // type either so fill in the canonical type field. |
| 1555 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1556 | if (!T.isCanonical()) { |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1557 | Canonical = getBlockPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1558 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1559 | // Get the new insert position for the node we care about. |
| 1560 | BlockPointerType *NewIP = |
| 1561 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1562 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1563 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1564 | BlockPointerType *New |
| 1565 | = new (*this, TypeAlignment) BlockPointerType(T, Canonical); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1566 | Types.push_back(New); |
| 1567 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 1568 | return QualType(New, 0); |
| 1569 | } |
| 1570 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1571 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 1572 | /// lvalue reference to the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1573 | QualType |
| 1574 | ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const { |
Douglas Gregor | 9625e44 | 2011-05-21 22:16:50 +0000 | [diff] [blame] | 1575 | assert(getCanonicalType(T) != OverloadTy && |
| 1576 | "Unresolved overloaded function type"); |
| 1577 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1578 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1579 | // structure. |
| 1580 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1581 | ReferenceType::Profile(ID, T, SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1582 | |
| 1583 | void *InsertPos = 0; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1584 | if (LValueReferenceType *RT = |
| 1585 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1586 | return QualType(RT, 0); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1587 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1588 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1589 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1590 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1591 | // either, so fill in the canonical type field. |
| 1592 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1593 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
| 1594 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1595 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1596 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1597 | // Get the new insert position for the node we care about. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1598 | LValueReferenceType *NewIP = |
| 1599 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1600 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1601 | } |
| 1602 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1603 | LValueReferenceType *New |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1604 | = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, |
| 1605 | SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1606 | Types.push_back(New); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1607 | LValueReferenceTypes.InsertNode(New, InsertPos); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1608 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1609 | return QualType(New, 0); |
| 1610 | } |
| 1611 | |
| 1612 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 1613 | /// rvalue reference to the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1614 | QualType ASTContext::getRValueReferenceType(QualType T) const { |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1615 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1616 | // structure. |
| 1617 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1618 | ReferenceType::Profile(ID, T, false); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1619 | |
| 1620 | void *InsertPos = 0; |
| 1621 | if (RValueReferenceType *RT = |
| 1622 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1623 | return QualType(RT, 0); |
| 1624 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1625 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1626 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1627 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1628 | // either, so fill in the canonical type field. |
| 1629 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1630 | if (InnerRef || !T.isCanonical()) { |
| 1631 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1632 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1633 | |
| 1634 | // Get the new insert position for the node we care about. |
| 1635 | RValueReferenceType *NewIP = |
| 1636 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1637 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1638 | } |
| 1639 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1640 | RValueReferenceType *New |
| 1641 | = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1642 | Types.push_back(New); |
| 1643 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1644 | return QualType(New, 0); |
| 1645 | } |
| 1646 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1647 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 1648 | /// member pointer to the specified type, in the specified class. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1649 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1650 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1651 | // structure. |
| 1652 | llvm::FoldingSetNodeID ID; |
| 1653 | MemberPointerType::Profile(ID, T, Cls); |
| 1654 | |
| 1655 | void *InsertPos = 0; |
| 1656 | if (MemberPointerType *PT = |
| 1657 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1658 | return QualType(PT, 0); |
| 1659 | |
| 1660 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 1661 | // type either, so fill in the canonical type field. |
| 1662 | QualType Canonical; |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1663 | if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1664 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 1665 | |
| 1666 | // Get the new insert position for the node we care about. |
| 1667 | MemberPointerType *NewIP = |
| 1668 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1669 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1670 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1671 | MemberPointerType *New |
| 1672 | = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1673 | Types.push_back(New); |
| 1674 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 1675 | return QualType(New, 0); |
| 1676 | } |
| 1677 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1678 | /// getConstantArrayType - Return the unique reference to the type for an |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1679 | /// array of the specified element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1680 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1681 | const llvm::APInt &ArySizeIn, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1682 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1683 | unsigned IndexTypeQuals) const { |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1684 | assert((EltTy->isDependentType() || |
| 1685 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && |
Eli Friedman | 587cbdf | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1686 | "Constant array of VLAs is illegal!"); |
| 1687 | |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1688 | // Convert the array size into a canonical width matching the pointer size for |
| 1689 | // the target. |
| 1690 | llvm::APInt ArySize(ArySizeIn); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1691 | ArySize = |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1692 | ArySize.zextOrTrunc(Target->getPointerWidth(getTargetAddressSpace(EltTy))); |
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 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1695 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, IndexTypeQuals); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1696 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1697 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1698 | if (ConstantArrayType *ATP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1699 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1700 | return QualType(ATP, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1701 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1702 | // If the element type isn't canonical or has qualifiers, this won't |
| 1703 | // be a canonical type either, so fill in the canonical type field. |
| 1704 | QualType Canon; |
| 1705 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 1706 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1707 | Canon = getConstantArrayType(QualType(canonSplit.Ty, 0), ArySize, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1708 | ASM, IndexTypeQuals); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1709 | Canon = getQualifiedType(Canon, canonSplit.Quals); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1710 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1711 | // Get the new insert position for the node we care about. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1712 | ConstantArrayType *NewIP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1713 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1714 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1715 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1716 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1717 | ConstantArrayType *New = new(*this,TypeAlignment) |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1718 | ConstantArrayType(EltTy, Canon, ArySize, ASM, IndexTypeQuals); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1719 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1720 | Types.push_back(New); |
| 1721 | return QualType(New, 0); |
| 1722 | } |
| 1723 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1724 | /// getVariableArrayDecayedType - Turns the given type, which may be |
| 1725 | /// variably-modified, into the corresponding type with all the known |
| 1726 | /// sizes replaced with [*]. |
| 1727 | QualType ASTContext::getVariableArrayDecayedType(QualType type) const { |
| 1728 | // Vastly most common case. |
| 1729 | if (!type->isVariablyModifiedType()) return type; |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1730 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1731 | QualType result; |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1732 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1733 | SplitQualType split = type.getSplitDesugaredType(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1734 | const Type *ty = split.Ty; |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1735 | switch (ty->getTypeClass()) { |
| 1736 | #define TYPE(Class, Base) |
| 1737 | #define ABSTRACT_TYPE(Class, Base) |
| 1738 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 1739 | #include "clang/AST/TypeNodes.def" |
| 1740 | llvm_unreachable("didn't desugar past all non-canonical types?"); |
| 1741 | |
| 1742 | // These types should never be variably-modified. |
| 1743 | case Type::Builtin: |
| 1744 | case Type::Complex: |
| 1745 | case Type::Vector: |
| 1746 | case Type::ExtVector: |
| 1747 | case Type::DependentSizedExtVector: |
| 1748 | case Type::ObjCObject: |
| 1749 | case Type::ObjCInterface: |
| 1750 | case Type::ObjCObjectPointer: |
| 1751 | case Type::Record: |
| 1752 | case Type::Enum: |
| 1753 | case Type::UnresolvedUsing: |
| 1754 | case Type::TypeOfExpr: |
| 1755 | case Type::TypeOf: |
| 1756 | case Type::Decltype: |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1757 | case Type::UnaryTransform: |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1758 | case Type::DependentName: |
| 1759 | case Type::InjectedClassName: |
| 1760 | case Type::TemplateSpecialization: |
| 1761 | case Type::DependentTemplateSpecialization: |
| 1762 | case Type::TemplateTypeParm: |
| 1763 | case Type::SubstTemplateTypeParmPack: |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1764 | case Type::Auto: |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1765 | case Type::PackExpansion: |
| 1766 | llvm_unreachable("type should never be variably-modified"); |
| 1767 | |
| 1768 | // These types can be variably-modified but should never need to |
| 1769 | // further decay. |
| 1770 | case Type::FunctionNoProto: |
| 1771 | case Type::FunctionProto: |
| 1772 | case Type::BlockPointer: |
| 1773 | case Type::MemberPointer: |
| 1774 | return type; |
| 1775 | |
| 1776 | // These types can be variably-modified. All these modifications |
| 1777 | // preserve structure except as noted by comments. |
| 1778 | // TODO: if we ever care about optimizing VLAs, there are no-op |
| 1779 | // optimizations available here. |
| 1780 | case Type::Pointer: |
| 1781 | result = getPointerType(getVariableArrayDecayedType( |
| 1782 | cast<PointerType>(ty)->getPointeeType())); |
| 1783 | break; |
| 1784 | |
| 1785 | case Type::LValueReference: { |
| 1786 | const LValueReferenceType *lv = cast<LValueReferenceType>(ty); |
| 1787 | result = getLValueReferenceType( |
| 1788 | getVariableArrayDecayedType(lv->getPointeeType()), |
| 1789 | lv->isSpelledAsLValue()); |
| 1790 | break; |
| 1791 | } |
| 1792 | |
| 1793 | case Type::RValueReference: { |
| 1794 | const RValueReferenceType *lv = cast<RValueReferenceType>(ty); |
| 1795 | result = getRValueReferenceType( |
| 1796 | getVariableArrayDecayedType(lv->getPointeeType())); |
| 1797 | break; |
| 1798 | } |
| 1799 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1800 | case Type::Atomic: { |
| 1801 | const AtomicType *at = cast<AtomicType>(ty); |
| 1802 | result = getAtomicType(getVariableArrayDecayedType(at->getValueType())); |
| 1803 | break; |
| 1804 | } |
| 1805 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1806 | case Type::ConstantArray: { |
| 1807 | const ConstantArrayType *cat = cast<ConstantArrayType>(ty); |
| 1808 | result = getConstantArrayType( |
| 1809 | getVariableArrayDecayedType(cat->getElementType()), |
| 1810 | cat->getSize(), |
| 1811 | cat->getSizeModifier(), |
| 1812 | cat->getIndexTypeCVRQualifiers()); |
| 1813 | break; |
| 1814 | } |
| 1815 | |
| 1816 | case Type::DependentSizedArray: { |
| 1817 | const DependentSizedArrayType *dat = cast<DependentSizedArrayType>(ty); |
| 1818 | result = getDependentSizedArrayType( |
| 1819 | getVariableArrayDecayedType(dat->getElementType()), |
| 1820 | dat->getSizeExpr(), |
| 1821 | dat->getSizeModifier(), |
| 1822 | dat->getIndexTypeCVRQualifiers(), |
| 1823 | dat->getBracketsRange()); |
| 1824 | break; |
| 1825 | } |
| 1826 | |
| 1827 | // Turn incomplete types into [*] types. |
| 1828 | case Type::IncompleteArray: { |
| 1829 | const IncompleteArrayType *iat = cast<IncompleteArrayType>(ty); |
| 1830 | result = getVariableArrayType( |
| 1831 | getVariableArrayDecayedType(iat->getElementType()), |
| 1832 | /*size*/ 0, |
| 1833 | ArrayType::Normal, |
| 1834 | iat->getIndexTypeCVRQualifiers(), |
| 1835 | SourceRange()); |
| 1836 | break; |
| 1837 | } |
| 1838 | |
| 1839 | // Turn VLA types into [*] types. |
| 1840 | case Type::VariableArray: { |
| 1841 | const VariableArrayType *vat = cast<VariableArrayType>(ty); |
| 1842 | result = getVariableArrayType( |
| 1843 | getVariableArrayDecayedType(vat->getElementType()), |
| 1844 | /*size*/ 0, |
| 1845 | ArrayType::Star, |
| 1846 | vat->getIndexTypeCVRQualifiers(), |
| 1847 | vat->getBracketsRange()); |
| 1848 | break; |
| 1849 | } |
| 1850 | } |
| 1851 | |
| 1852 | // Apply the top-level qualifiers from the original. |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1853 | return getQualifiedType(result, split.Quals); |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1854 | } |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1855 | |
Steve Naroff | bdbf7b0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 1856 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 1857 | /// variable array of the specified element type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1858 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 1859 | Expr *NumElts, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1860 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1861 | unsigned IndexTypeQuals, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1862 | SourceRange Brackets) const { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1863 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 1864 | // that have an expression provided for their size. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1865 | QualType Canon; |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 1866 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1867 | // Be sure to pull qualifiers off the element type. |
| 1868 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 1869 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1870 | Canon = getVariableArrayType(QualType(canonSplit.Ty, 0), NumElts, ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1871 | IndexTypeQuals, Brackets); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1872 | Canon = getQualifiedType(Canon, canonSplit.Quals); |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 1873 | } |
| 1874 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1875 | VariableArrayType *New = new(*this, TypeAlignment) |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1876 | VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1877 | |
| 1878 | VariableArrayTypes.push_back(New); |
| 1879 | Types.push_back(New); |
| 1880 | return QualType(New, 0); |
| 1881 | } |
| 1882 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1883 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 1884 | /// the type for a dependently-sized array of the specified element |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1885 | /// type. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1886 | QualType ASTContext::getDependentSizedArrayType(QualType elementType, |
| 1887 | Expr *numElements, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1888 | ArrayType::ArraySizeModifier ASM, |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1889 | unsigned elementTypeQuals, |
| 1890 | SourceRange brackets) const { |
| 1891 | assert((!numElements || numElements->isTypeDependent() || |
| 1892 | numElements->isValueDependent()) && |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1893 | "Size must be type- or value-dependent!"); |
| 1894 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1895 | // Dependently-sized array types that do not have a specified number |
| 1896 | // of elements will have their sizes deduced from a dependent |
| 1897 | // initializer. We do no canonicalization here at all, which is okay |
| 1898 | // because they can't be used in most locations. |
| 1899 | if (!numElements) { |
| 1900 | DependentSizedArrayType *newType |
| 1901 | = new (*this, TypeAlignment) |
| 1902 | DependentSizedArrayType(*this, elementType, QualType(), |
| 1903 | numElements, ASM, elementTypeQuals, |
| 1904 | brackets); |
| 1905 | Types.push_back(newType); |
| 1906 | return QualType(newType, 0); |
| 1907 | } |
| 1908 | |
| 1909 | // Otherwise, we actually build a new type every time, but we |
| 1910 | // also build a canonical type. |
| 1911 | |
| 1912 | SplitQualType canonElementType = getCanonicalType(elementType).split(); |
| 1913 | |
| 1914 | void *insertPos = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1915 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1916 | DependentSizedArrayType::Profile(ID, *this, |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1917 | QualType(canonElementType.Ty, 0), |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1918 | ASM, elementTypeQuals, numElements); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1919 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1920 | // Look for an existing type with these properties. |
| 1921 | DependentSizedArrayType *canonTy = |
| 1922 | DependentSizedArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1923 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1924 | // If we don't have one, build one. |
| 1925 | if (!canonTy) { |
| 1926 | canonTy = new (*this, TypeAlignment) |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1927 | DependentSizedArrayType(*this, QualType(canonElementType.Ty, 0), |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1928 | QualType(), numElements, ASM, elementTypeQuals, |
| 1929 | brackets); |
| 1930 | DependentSizedArrayTypes.InsertNode(canonTy, insertPos); |
| 1931 | Types.push_back(canonTy); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1932 | } |
| 1933 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1934 | // Apply qualifiers from the element type to the array. |
| 1935 | QualType canon = getQualifiedType(QualType(canonTy,0), |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1936 | canonElementType.Quals); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1937 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1938 | // If we didn't need extra canonicalization for the element type, |
| 1939 | // then just use that as our result. |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1940 | if (QualType(canonElementType.Ty, 0) == elementType) |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1941 | return canon; |
| 1942 | |
| 1943 | // Otherwise, we need to build a type which follows the spelling |
| 1944 | // of the element type. |
| 1945 | DependentSizedArrayType *sugaredType |
| 1946 | = new (*this, TypeAlignment) |
| 1947 | DependentSizedArrayType(*this, elementType, canon, numElements, |
| 1948 | ASM, elementTypeQuals, brackets); |
| 1949 | Types.push_back(sugaredType); |
| 1950 | return QualType(sugaredType, 0); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1951 | } |
| 1952 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1953 | QualType ASTContext::getIncompleteArrayType(QualType elementType, |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1954 | ArrayType::ArraySizeModifier ASM, |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1955 | unsigned elementTypeQuals) const { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1956 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1957 | IncompleteArrayType::Profile(ID, elementType, ASM, elementTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1958 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1959 | void *insertPos = 0; |
| 1960 | if (IncompleteArrayType *iat = |
| 1961 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos)) |
| 1962 | return QualType(iat, 0); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1963 | |
| 1964 | // 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] | 1965 | // either, so fill in the canonical type field. We also have to pull |
| 1966 | // qualifiers off the element type. |
| 1967 | QualType canon; |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1968 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1969 | if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) { |
| 1970 | SplitQualType canonSplit = getCanonicalType(elementType).split(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1971 | canon = getIncompleteArrayType(QualType(canonSplit.Ty, 0), |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1972 | ASM, elementTypeQuals); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1973 | canon = getQualifiedType(canon, canonSplit.Quals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1974 | |
| 1975 | // Get the new insert position for the node we care about. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1976 | IncompleteArrayType *existing = |
| 1977 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
| 1978 | assert(!existing && "Shouldn't be in the map!"); (void) existing; |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1979 | } |
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 | IncompleteArrayType *newType = new (*this, TypeAlignment) |
| 1982 | IncompleteArrayType(elementType, canon, ASM, elementTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1983 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1984 | IncompleteArrayTypes.InsertNode(newType, insertPos); |
| 1985 | Types.push_back(newType); |
| 1986 | return QualType(newType, 0); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1989 | /// getVectorType - Return the unique reference to a vector type of |
| 1990 | /// 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] | 1991 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1992 | VectorType::VectorKind VecKind) const { |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1993 | assert(vecType->isBuiltinType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1994 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1995 | // Check if we've already instantiated a vector of this type. |
| 1996 | llvm::FoldingSetNodeID ID; |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1997 | VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind); |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1998 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1999 | void *InsertPos = 0; |
| 2000 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2001 | return QualType(VTP, 0); |
| 2002 | |
| 2003 | // If the element type isn't canonical, this won't be a canonical type either, |
| 2004 | // so fill in the canonical type field. |
| 2005 | QualType Canonical; |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 2006 | if (!vecType.isCanonical()) { |
Bob Wilson | 231da7e | 2010-11-16 00:32:20 +0000 | [diff] [blame] | 2007 | Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2008 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2009 | // Get the new insert position for the node we care about. |
| 2010 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2011 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2012 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2013 | VectorType *New = new (*this, TypeAlignment) |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2014 | VectorType(vecType, NumElts, Canonical, VecKind); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2015 | VectorTypes.InsertNode(New, InsertPos); |
| 2016 | Types.push_back(New); |
| 2017 | return QualType(New, 0); |
| 2018 | } |
| 2019 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2020 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2021 | /// 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] | 2022 | QualType |
| 2023 | ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) const { |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 2024 | assert(vecType->isBuiltinType() || vecType->isDependentType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2025 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2026 | // Check if we've already instantiated a vector of this type. |
| 2027 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 2028 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2029 | VectorType::GenericVector); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2030 | void *InsertPos = 0; |
| 2031 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2032 | return QualType(VTP, 0); |
| 2033 | |
| 2034 | // If the element type isn't canonical, this won't be a canonical type either, |
| 2035 | // so fill in the canonical type field. |
| 2036 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2037 | if (!vecType.isCanonical()) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2038 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2039 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2040 | // Get the new insert position for the node we care about. |
| 2041 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2042 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2043 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2044 | ExtVectorType *New = new (*this, TypeAlignment) |
| 2045 | ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2046 | VectorTypes.InsertNode(New, InsertPos); |
| 2047 | Types.push_back(New); |
| 2048 | return QualType(New, 0); |
| 2049 | } |
| 2050 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2051 | QualType |
| 2052 | ASTContext::getDependentSizedExtVectorType(QualType vecType, |
| 2053 | Expr *SizeExpr, |
| 2054 | SourceLocation AttrLoc) const { |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2055 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2056 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2057 | SizeExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2058 | |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2059 | void *InsertPos = 0; |
| 2060 | DependentSizedExtVectorType *Canon |
| 2061 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2062 | DependentSizedExtVectorType *New; |
| 2063 | if (Canon) { |
| 2064 | // We already have a canonical version of this array type; use it as |
| 2065 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2066 | New = new (*this, TypeAlignment) |
| 2067 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), |
| 2068 | SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2069 | } else { |
| 2070 | QualType CanonVecTy = getCanonicalType(vecType); |
| 2071 | if (CanonVecTy == vecType) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2072 | New = new (*this, TypeAlignment) |
| 2073 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, |
| 2074 | AttrLoc); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2075 | |
| 2076 | DependentSizedExtVectorType *CanonCheck |
| 2077 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2078 | assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken"); |
| 2079 | (void)CanonCheck; |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2080 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); |
| 2081 | } else { |
| 2082 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 2083 | SourceLocation()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2084 | New = new (*this, TypeAlignment) |
| 2085 | DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2086 | } |
| 2087 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2088 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 2089 | Types.push_back(New); |
| 2090 | return QualType(New, 0); |
| 2091 | } |
| 2092 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2093 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2094 | /// |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2095 | QualType |
| 2096 | ASTContext::getFunctionNoProtoType(QualType ResultTy, |
| 2097 | const FunctionType::ExtInfo &Info) const { |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2098 | const CallingConv DefaultCC = Info.getCC(); |
| 2099 | const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ? |
| 2100 | CC_X86StdCall : DefaultCC; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2101 | // Unique functions, to guarantee there is only one function of a particular |
| 2102 | // structure. |
| 2103 | llvm::FoldingSetNodeID ID; |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2104 | FunctionNoProtoType::Profile(ID, ResultTy, Info); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2105 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2106 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2107 | if (FunctionNoProtoType *FT = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2108 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2109 | return QualType(FT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2110 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2111 | QualType Canonical; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 2112 | if (!ResultTy.isCanonical() || |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2113 | getCanonicalCallConv(CallConv) != CallConv) { |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2114 | Canonical = |
| 2115 | getFunctionNoProtoType(getCanonicalType(ResultTy), |
| 2116 | Info.withCallingConv(getCanonicalCallConv(CallConv))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2117 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2118 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2119 | FunctionNoProtoType *NewIP = |
| 2120 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2121 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2122 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2123 | |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2124 | FunctionProtoType::ExtInfo newInfo = Info.withCallingConv(CallConv); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2125 | FunctionNoProtoType *New = new (*this, TypeAlignment) |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2126 | FunctionNoProtoType(ResultTy, Canonical, newInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2127 | Types.push_back(New); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2128 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2129 | return QualType(New, 0); |
| 2130 | } |
| 2131 | |
| 2132 | /// getFunctionType - Return a normal function type with a typed argument |
| 2133 | /// list. isVariadic indicates whether the argument list includes '...'. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2134 | QualType |
| 2135 | ASTContext::getFunctionType(QualType ResultTy, |
| 2136 | const QualType *ArgArray, unsigned NumArgs, |
| 2137 | const FunctionProtoType::ExtProtoInfo &EPI) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2138 | // Unique functions, to guarantee there is only one function of a particular |
| 2139 | // structure. |
| 2140 | llvm::FoldingSetNodeID ID; |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2141 | FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, EPI, *this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2142 | |
| 2143 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2144 | if (FunctionProtoType *FTP = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2145 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2146 | return QualType(FTP, 0); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2147 | |
| 2148 | // Determine whether the type being created is already canonical or not. |
Richard Smith | eefb3d5 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2149 | bool isCanonical = |
| 2150 | EPI.ExceptionSpecType == EST_None && ResultTy.isCanonical() && |
| 2151 | !EPI.HasTrailingReturn; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2152 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2153 | if (!ArgArray[i].isCanonicalAsParam()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2154 | isCanonical = false; |
| 2155 | |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2156 | const CallingConv DefaultCC = EPI.ExtInfo.getCC(); |
| 2157 | const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ? |
| 2158 | CC_X86StdCall : DefaultCC; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2159 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2160 | // If this type isn't canonical, get the canonical version of it. |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2161 | // The exception spec is not part of the canonical type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2162 | QualType Canonical; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2163 | if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2164 | SmallVector<QualType, 16> CanonicalArgs; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2165 | CanonicalArgs.reserve(NumArgs); |
| 2166 | for (unsigned i = 0; i != NumArgs; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2167 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2168 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2169 | FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI; |
Richard Smith | eefb3d5 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2170 | CanonicalEPI.HasTrailingReturn = false; |
Sebastian Redl | 8b5b409 | 2011-03-06 10:52:04 +0000 | [diff] [blame] | 2171 | CanonicalEPI.ExceptionSpecType = EST_None; |
| 2172 | CanonicalEPI.NumExceptions = 0; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2173 | CanonicalEPI.ExtInfo |
| 2174 | = CanonicalEPI.ExtInfo.withCallingConv(getCanonicalCallConv(CallConv)); |
| 2175 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2176 | Canonical = getFunctionType(getCanonicalType(ResultTy), |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2177 | CanonicalArgs.data(), NumArgs, |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2178 | CanonicalEPI); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2179 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2180 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2181 | FunctionProtoType *NewIP = |
| 2182 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2183 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2184 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2185 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2186 | // FunctionProtoType objects are allocated with extra bytes after |
| 2187 | // them for three variable size arrays at the end: |
| 2188 | // - parameter types |
| 2189 | // - exception types |
| 2190 | // - consumed-arguments flags |
| 2191 | // Instead of the exception types, there could be a noexcept |
| 2192 | // expression. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2193 | size_t Size = sizeof(FunctionProtoType) + |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2194 | NumArgs * sizeof(QualType); |
| 2195 | if (EPI.ExceptionSpecType == EST_Dynamic) |
| 2196 | Size += EPI.NumExceptions * sizeof(QualType); |
| 2197 | else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) { |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2198 | Size += sizeof(Expr*); |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 2199 | } else if (EPI.ExceptionSpecType == EST_Uninstantiated) { |
Richard Smith | 13bffc5 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 2200 | Size += 2 * sizeof(FunctionDecl*); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2201 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2202 | if (EPI.ConsumedArguments) |
| 2203 | Size += NumArgs * sizeof(bool); |
| 2204 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2205 | FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment); |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2206 | FunctionProtoType::ExtProtoInfo newEPI = EPI; |
| 2207 | newEPI.ExtInfo = EPI.ExtInfo.withCallingConv(CallConv); |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2208 | new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, Canonical, newEPI); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2209 | Types.push_back(FTP); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2210 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2211 | return QualType(FTP, 0); |
| 2212 | } |
| 2213 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2214 | #ifndef NDEBUG |
| 2215 | static bool NeedsInjectedClassNameType(const RecordDecl *D) { |
| 2216 | if (!isa<CXXRecordDecl>(D)) return false; |
| 2217 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 2218 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) |
| 2219 | return true; |
| 2220 | if (RD->getDescribedClassTemplate() && |
| 2221 | !isa<ClassTemplateSpecializationDecl>(RD)) |
| 2222 | return true; |
| 2223 | return false; |
| 2224 | } |
| 2225 | #endif |
| 2226 | |
| 2227 | /// getInjectedClassNameType - Return the unique reference to the |
| 2228 | /// injected class name type for the specified templated declaration. |
| 2229 | QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2230 | QualType TST) const { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2231 | assert(NeedsInjectedClassNameType(Decl)); |
| 2232 | if (Decl->TypeForDecl) { |
| 2233 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2234 | } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDecl()) { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2235 | assert(PrevDecl->TypeForDecl && "previous declaration has no type"); |
| 2236 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 2237 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
| 2238 | } else { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2239 | Type *newType = |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2240 | new (*this, TypeAlignment) InjectedClassNameType(Decl, TST); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2241 | Decl->TypeForDecl = newType; |
| 2242 | Types.push_back(newType); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2243 | } |
| 2244 | return QualType(Decl->TypeForDecl, 0); |
| 2245 | } |
| 2246 | |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2247 | /// getTypeDeclType - Return the unique reference to the type for the |
| 2248 | /// specified type declaration. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2249 | QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const { |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 2250 | assert(Decl && "Passed null for Decl param"); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2251 | assert(!Decl->TypeForDecl && "TypeForDecl present in slow case"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2252 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2253 | if (const TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2254 | return getTypedefType(Typedef); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2255 | |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2256 | assert(!isa<TemplateTypeParmDecl>(Decl) && |
| 2257 | "Template type parameter types are always available."); |
| 2258 | |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2259 | if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2260 | assert(!Record->getPreviousDecl() && |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2261 | "struct/union has previous declaration"); |
| 2262 | assert(!NeedsInjectedClassNameType(Record)); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2263 | return getRecordType(Record); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2264 | } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2265 | assert(!Enum->getPreviousDecl() && |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2266 | "enum has previous declaration"); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2267 | return getEnumType(Enum); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2268 | } else if (const UnresolvedUsingTypenameDecl *Using = |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2269 | dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2270 | Type *newType = new (*this, TypeAlignment) UnresolvedUsingType(Using); |
| 2271 | Decl->TypeForDecl = newType; |
| 2272 | Types.push_back(newType); |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 2273 | } else |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2274 | llvm_unreachable("TypeDecl without a type?"); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 2275 | |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 2276 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2277 | } |
| 2278 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2279 | /// getTypedefType - Return the unique reference to the type for the |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2280 | /// specified typedef name decl. |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2281 | QualType |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2282 | ASTContext::getTypedefType(const TypedefNameDecl *Decl, |
| 2283 | QualType Canonical) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2284 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2285 | |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2286 | if (Canonical.isNull()) |
| 2287 | Canonical = getCanonicalType(Decl->getUnderlyingType()); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2288 | TypedefType *newType = new(*this, TypeAlignment) |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2289 | TypedefType(Type::Typedef, Decl, Canonical); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2290 | Decl->TypeForDecl = newType; |
| 2291 | Types.push_back(newType); |
| 2292 | return QualType(newType, 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2293 | } |
| 2294 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2295 | QualType ASTContext::getRecordType(const RecordDecl *Decl) const { |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2296 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 2297 | |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2298 | if (const RecordDecl *PrevDecl = Decl->getPreviousDecl()) |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2299 | if (PrevDecl->TypeForDecl) |
| 2300 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 2301 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2302 | RecordType *newType = new (*this, TypeAlignment) RecordType(Decl); |
| 2303 | Decl->TypeForDecl = newType; |
| 2304 | Types.push_back(newType); |
| 2305 | return QualType(newType, 0); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2306 | } |
| 2307 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2308 | QualType ASTContext::getEnumType(const EnumDecl *Decl) const { |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2309 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 2310 | |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2311 | if (const EnumDecl *PrevDecl = Decl->getPreviousDecl()) |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2312 | if (PrevDecl->TypeForDecl) |
| 2313 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 2314 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2315 | EnumType *newType = new (*this, TypeAlignment) EnumType(Decl); |
| 2316 | Decl->TypeForDecl = newType; |
| 2317 | Types.push_back(newType); |
| 2318 | return QualType(newType, 0); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2319 | } |
| 2320 | |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 2321 | QualType ASTContext::getAttributedType(AttributedType::Kind attrKind, |
| 2322 | QualType modifiedType, |
| 2323 | QualType equivalentType) { |
| 2324 | llvm::FoldingSetNodeID id; |
| 2325 | AttributedType::Profile(id, attrKind, modifiedType, equivalentType); |
| 2326 | |
| 2327 | void *insertPos = 0; |
| 2328 | AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos); |
| 2329 | if (type) return QualType(type, 0); |
| 2330 | |
| 2331 | QualType canon = getCanonicalType(equivalentType); |
| 2332 | type = new (*this, TypeAlignment) |
| 2333 | AttributedType(canon, attrKind, modifiedType, equivalentType); |
| 2334 | |
| 2335 | Types.push_back(type); |
| 2336 | AttributedTypes.InsertNode(type, insertPos); |
| 2337 | |
| 2338 | return QualType(type, 0); |
| 2339 | } |
| 2340 | |
| 2341 | |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2342 | /// \brief Retrieve a substitution-result type. |
| 2343 | QualType |
| 2344 | ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2345 | QualType Replacement) const { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2346 | assert(Replacement.isCanonical() |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2347 | && "replacement types must always be canonical"); |
| 2348 | |
| 2349 | llvm::FoldingSetNodeID ID; |
| 2350 | SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); |
| 2351 | void *InsertPos = 0; |
| 2352 | SubstTemplateTypeParmType *SubstParm |
| 2353 | = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2354 | |
| 2355 | if (!SubstParm) { |
| 2356 | SubstParm = new (*this, TypeAlignment) |
| 2357 | SubstTemplateTypeParmType(Parm, Replacement); |
| 2358 | Types.push_back(SubstParm); |
| 2359 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 2360 | } |
| 2361 | |
| 2362 | return QualType(SubstParm, 0); |
| 2363 | } |
| 2364 | |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 2365 | /// \brief Retrieve a |
| 2366 | QualType ASTContext::getSubstTemplateTypeParmPackType( |
| 2367 | const TemplateTypeParmType *Parm, |
| 2368 | const TemplateArgument &ArgPack) { |
| 2369 | #ifndef NDEBUG |
| 2370 | for (TemplateArgument::pack_iterator P = ArgPack.pack_begin(), |
| 2371 | PEnd = ArgPack.pack_end(); |
| 2372 | P != PEnd; ++P) { |
| 2373 | assert(P->getKind() == TemplateArgument::Type &&"Pack contains a non-type"); |
| 2374 | assert(P->getAsType().isCanonical() && "Pack contains non-canonical type"); |
| 2375 | } |
| 2376 | #endif |
| 2377 | |
| 2378 | llvm::FoldingSetNodeID ID; |
| 2379 | SubstTemplateTypeParmPackType::Profile(ID, Parm, ArgPack); |
| 2380 | void *InsertPos = 0; |
| 2381 | if (SubstTemplateTypeParmPackType *SubstParm |
| 2382 | = SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2383 | return QualType(SubstParm, 0); |
| 2384 | |
| 2385 | QualType Canon; |
| 2386 | if (!Parm->isCanonicalUnqualified()) { |
| 2387 | Canon = getCanonicalType(QualType(Parm, 0)); |
| 2388 | Canon = getSubstTemplateTypeParmPackType(cast<TemplateTypeParmType>(Canon), |
| 2389 | ArgPack); |
| 2390 | SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2391 | } |
| 2392 | |
| 2393 | SubstTemplateTypeParmPackType *SubstParm |
| 2394 | = new (*this, TypeAlignment) SubstTemplateTypeParmPackType(Parm, Canon, |
| 2395 | ArgPack); |
| 2396 | Types.push_back(SubstParm); |
| 2397 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 2398 | return QualType(SubstParm, 0); |
| 2399 | } |
| 2400 | |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2401 | /// \brief Retrieve the template type parameter type for a template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2402 | /// parameter or parameter pack with the given depth, index, and (optionally) |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2403 | /// name. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2404 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2405 | bool ParameterPack, |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2406 | TemplateTypeParmDecl *TTPDecl) const { |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2407 | llvm::FoldingSetNodeID ID; |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2408 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, TTPDecl); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2409 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2410 | TemplateTypeParmType *TypeParm |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2411 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2412 | |
| 2413 | if (TypeParm) |
| 2414 | return QualType(TypeParm, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2415 | |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2416 | if (TTPDecl) { |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2417 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2418 | TypeParm = new (*this, TypeAlignment) TemplateTypeParmType(TTPDecl, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2419 | |
| 2420 | TemplateTypeParmType *TypeCheck |
| 2421 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2422 | assert(!TypeCheck && "Template type parameter canonical type broken"); |
| 2423 | (void)TypeCheck; |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2424 | } else |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2425 | TypeParm = new (*this, TypeAlignment) |
| 2426 | TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2427 | |
| 2428 | Types.push_back(TypeParm); |
| 2429 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 2430 | |
| 2431 | return QualType(TypeParm, 0); |
| 2432 | } |
| 2433 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2434 | TypeSourceInfo * |
| 2435 | ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name, |
| 2436 | SourceLocation NameLoc, |
| 2437 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2438 | QualType Underlying) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2439 | assert(!Name.getAsDependentTemplateName() && |
| 2440 | "No dependent template names here!"); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2441 | QualType TST = getTemplateSpecializationType(Name, Args, Underlying); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2442 | |
| 2443 | TypeSourceInfo *DI = CreateTypeSourceInfo(TST); |
| 2444 | TemplateSpecializationTypeLoc TL |
| 2445 | = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc()); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2446 | TL.setTemplateKeywordLoc(SourceLocation()); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2447 | TL.setTemplateNameLoc(NameLoc); |
| 2448 | TL.setLAngleLoc(Args.getLAngleLoc()); |
| 2449 | TL.setRAngleLoc(Args.getRAngleLoc()); |
| 2450 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 2451 | TL.setArgLocInfo(i, Args[i].getLocInfo()); |
| 2452 | return DI; |
| 2453 | } |
| 2454 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2455 | QualType |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2456 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2457 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2458 | QualType Underlying) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2459 | assert(!Template.getAsDependentTemplateName() && |
| 2460 | "No dependent template names here!"); |
| 2461 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2462 | unsigned NumArgs = Args.size(); |
| 2463 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2464 | SmallVector<TemplateArgument, 4> ArgVec; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2465 | ArgVec.reserve(NumArgs); |
| 2466 | for (unsigned i = 0; i != NumArgs; ++i) |
| 2467 | ArgVec.push_back(Args[i].getArgument()); |
| 2468 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2469 | return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2470 | Underlying); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2471 | } |
| 2472 | |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2473 | #ifndef NDEBUG |
| 2474 | static bool hasAnyPackExpansions(const TemplateArgument *Args, |
| 2475 | unsigned NumArgs) { |
| 2476 | for (unsigned I = 0; I != NumArgs; ++I) |
| 2477 | if (Args[I].isPackExpansion()) |
| 2478 | return true; |
| 2479 | |
| 2480 | return true; |
| 2481 | } |
| 2482 | #endif |
| 2483 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2484 | QualType |
| 2485 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2486 | const TemplateArgument *Args, |
| 2487 | unsigned NumArgs, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2488 | QualType Underlying) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2489 | assert(!Template.getAsDependentTemplateName() && |
| 2490 | "No dependent template names here!"); |
Douglas Gregor | 0f0ea2a | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 2491 | // Look through qualified template names. |
| 2492 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 2493 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2494 | |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2495 | bool IsTypeAlias = |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2496 | Template.getAsTemplateDecl() && |
| 2497 | isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl()); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2498 | QualType CanonType; |
| 2499 | if (!Underlying.isNull()) |
| 2500 | CanonType = getCanonicalType(Underlying); |
| 2501 | else { |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2502 | // We can get here with an alias template when the specialization contains |
| 2503 | // a pack expansion that does not match up with a parameter pack. |
| 2504 | assert((!IsTypeAlias || hasAnyPackExpansions(Args, NumArgs)) && |
| 2505 | "Caller must compute aliased type"); |
| 2506 | IsTypeAlias = false; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2507 | CanonType = getCanonicalTemplateSpecializationType(Template, Args, |
| 2508 | NumArgs); |
| 2509 | } |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 2510 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2511 | // Allocate the (non-canonical) template specialization type, but don't |
| 2512 | // try to unique it: these types typically have location information that |
| 2513 | // we don't unique and don't want to lose. |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2514 | void *Mem = Allocate(sizeof(TemplateSpecializationType) + |
| 2515 | sizeof(TemplateArgument) * NumArgs + |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2516 | (IsTypeAlias? sizeof(QualType) : 0), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2517 | TypeAlignment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2518 | TemplateSpecializationType *Spec |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2519 | = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, CanonType, |
| 2520 | IsTypeAlias ? Underlying : QualType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2521 | |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2522 | Types.push_back(Spec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2523 | return QualType(Spec, 0); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2524 | } |
| 2525 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2526 | QualType |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2527 | ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template, |
| 2528 | const TemplateArgument *Args, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2529 | unsigned NumArgs) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2530 | assert(!Template.getAsDependentTemplateName() && |
| 2531 | "No dependent template names here!"); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2532 | |
Douglas Gregor | 0f0ea2a | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 2533 | // Look through qualified template names. |
| 2534 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 2535 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2536 | |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2537 | // Build the canonical template specialization type. |
| 2538 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2539 | SmallVector<TemplateArgument, 4> CanonArgs; |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2540 | CanonArgs.reserve(NumArgs); |
| 2541 | for (unsigned I = 0; I != NumArgs; ++I) |
| 2542 | CanonArgs.push_back(getCanonicalTemplateArgument(Args[I])); |
| 2543 | |
| 2544 | // Determine whether this canonical template specialization type already |
| 2545 | // exists. |
| 2546 | llvm::FoldingSetNodeID ID; |
| 2547 | TemplateSpecializationType::Profile(ID, CanonTemplate, |
| 2548 | CanonArgs.data(), NumArgs, *this); |
| 2549 | |
| 2550 | void *InsertPos = 0; |
| 2551 | TemplateSpecializationType *Spec |
| 2552 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2553 | |
| 2554 | if (!Spec) { |
| 2555 | // Allocate a new canonical template specialization type. |
| 2556 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
| 2557 | sizeof(TemplateArgument) * NumArgs), |
| 2558 | TypeAlignment); |
| 2559 | Spec = new (Mem) TemplateSpecializationType(CanonTemplate, |
| 2560 | CanonArgs.data(), NumArgs, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2561 | QualType(), QualType()); |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2562 | Types.push_back(Spec); |
| 2563 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
| 2564 | } |
| 2565 | |
| 2566 | assert(Spec->isDependentType() && |
| 2567 | "Non-dependent template-id type must have a canonical type"); |
| 2568 | return QualType(Spec, 0); |
| 2569 | } |
| 2570 | |
| 2571 | QualType |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2572 | ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword, |
| 2573 | NestedNameSpecifier *NNS, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2574 | QualType NamedType) const { |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2575 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2576 | ElaboratedType::Profile(ID, Keyword, NNS, NamedType); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2577 | |
| 2578 | void *InsertPos = 0; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2579 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2580 | if (T) |
| 2581 | return QualType(T, 0); |
| 2582 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2583 | QualType Canon = NamedType; |
| 2584 | if (!Canon.isCanonical()) { |
| 2585 | Canon = getCanonicalType(NamedType); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2586 | ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2587 | assert(!CheckT && "Elaborated canonical type broken"); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2588 | (void)CheckT; |
| 2589 | } |
| 2590 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2591 | T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2592 | Types.push_back(T); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2593 | ElaboratedTypes.InsertNode(T, InsertPos); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2594 | return QualType(T, 0); |
| 2595 | } |
| 2596 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 2597 | QualType |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2598 | ASTContext::getParenType(QualType InnerType) const { |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 2599 | llvm::FoldingSetNodeID ID; |
| 2600 | ParenType::Profile(ID, InnerType); |
| 2601 | |
| 2602 | void *InsertPos = 0; |
| 2603 | ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2604 | if (T) |
| 2605 | return QualType(T, 0); |
| 2606 | |
| 2607 | QualType Canon = InnerType; |
| 2608 | if (!Canon.isCanonical()) { |
| 2609 | Canon = getCanonicalType(InnerType); |
| 2610 | ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2611 | assert(!CheckT && "Paren canonical type broken"); |
| 2612 | (void)CheckT; |
| 2613 | } |
| 2614 | |
| 2615 | T = new (*this) ParenType(InnerType, Canon); |
| 2616 | Types.push_back(T); |
| 2617 | ParenTypes.InsertNode(T, InsertPos); |
| 2618 | return QualType(T, 0); |
| 2619 | } |
| 2620 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2621 | QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, |
| 2622 | NestedNameSpecifier *NNS, |
| 2623 | const IdentifierInfo *Name, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2624 | QualType Canon) const { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2625 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 2626 | |
| 2627 | if (Canon.isNull()) { |
| 2628 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2629 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 2630 | if (Keyword == ETK_None) |
| 2631 | CanonKeyword = ETK_Typename; |
| 2632 | |
| 2633 | if (CanonNNS != NNS || CanonKeyword != Keyword) |
| 2634 | Canon = getDependentNameType(CanonKeyword, CanonNNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2635 | } |
| 2636 | |
| 2637 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2638 | DependentNameType::Profile(ID, Keyword, NNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2639 | |
| 2640 | void *InsertPos = 0; |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2641 | DependentNameType *T |
| 2642 | = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2643 | if (T) |
| 2644 | return QualType(T, 0); |
| 2645 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2646 | T = new (*this) DependentNameType(Keyword, NNS, Name, Canon); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2647 | Types.push_back(T); |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2648 | DependentNameTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2649 | return QualType(T, 0); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2650 | } |
| 2651 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2652 | QualType |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2653 | ASTContext::getDependentTemplateSpecializationType( |
| 2654 | ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2655 | NestedNameSpecifier *NNS, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2656 | const IdentifierInfo *Name, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2657 | const TemplateArgumentListInfo &Args) const { |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2658 | // TODO: avoid this copy |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2659 | SmallVector<TemplateArgument, 16> ArgCopy; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2660 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
| 2661 | ArgCopy.push_back(Args[I].getArgument()); |
| 2662 | return getDependentTemplateSpecializationType(Keyword, NNS, Name, |
| 2663 | ArgCopy.size(), |
| 2664 | ArgCopy.data()); |
| 2665 | } |
| 2666 | |
| 2667 | QualType |
| 2668 | ASTContext::getDependentTemplateSpecializationType( |
| 2669 | ElaboratedTypeKeyword Keyword, |
| 2670 | NestedNameSpecifier *NNS, |
| 2671 | const IdentifierInfo *Name, |
| 2672 | unsigned NumArgs, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2673 | const TemplateArgument *Args) const { |
Douglas Gregor | aa2187d | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 2674 | assert((!NNS || NNS->isDependent()) && |
| 2675 | "nested-name-specifier must be dependent"); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2676 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2677 | llvm::FoldingSetNodeID ID; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2678 | DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS, |
| 2679 | Name, NumArgs, Args); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2680 | |
| 2681 | void *InsertPos = 0; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2682 | DependentTemplateSpecializationType *T |
| 2683 | = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2684 | if (T) |
| 2685 | return QualType(T, 0); |
| 2686 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2687 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2688 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2689 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 2690 | if (Keyword == ETK_None) CanonKeyword = ETK_Typename; |
| 2691 | |
| 2692 | bool AnyNonCanonArgs = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2693 | SmallVector<TemplateArgument, 16> CanonArgs(NumArgs); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2694 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 2695 | CanonArgs[I] = getCanonicalTemplateArgument(Args[I]); |
| 2696 | if (!CanonArgs[I].structurallyEquals(Args[I])) |
| 2697 | AnyNonCanonArgs = true; |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2698 | } |
| 2699 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2700 | QualType Canon; |
| 2701 | if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) { |
| 2702 | Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS, |
| 2703 | Name, NumArgs, |
| 2704 | CanonArgs.data()); |
| 2705 | |
| 2706 | // Find the insert position again. |
| 2707 | DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2708 | } |
| 2709 | |
| 2710 | void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) + |
| 2711 | sizeof(TemplateArgument) * NumArgs), |
| 2712 | TypeAlignment); |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 2713 | T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2714 | Name, NumArgs, Args, Canon); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2715 | Types.push_back(T); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2716 | DependentTemplateSpecializationTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2717 | return QualType(T, 0); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2718 | } |
| 2719 | |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2720 | QualType ASTContext::getPackExpansionType(QualType Pattern, |
| 2721 | llvm::Optional<unsigned> NumExpansions) { |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2722 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2723 | PackExpansionType::Profile(ID, Pattern, NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2724 | |
| 2725 | assert(Pattern->containsUnexpandedParameterPack() && |
| 2726 | "Pack expansions must expand one or more parameter packs"); |
| 2727 | void *InsertPos = 0; |
| 2728 | PackExpansionType *T |
| 2729 | = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2730 | if (T) |
| 2731 | return QualType(T, 0); |
| 2732 | |
| 2733 | QualType Canon; |
| 2734 | if (!Pattern.isCanonical()) { |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2735 | Canon = getPackExpansionType(getCanonicalType(Pattern), NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2736 | |
| 2737 | // Find the insert position again. |
| 2738 | PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2739 | } |
| 2740 | |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2741 | T = new (*this) PackExpansionType(Pattern, Canon, NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2742 | Types.push_back(T); |
| 2743 | PackExpansionTypes.InsertNode(T, InsertPos); |
| 2744 | return QualType(T, 0); |
| 2745 | } |
| 2746 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2747 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 2748 | /// alphabetically. |
| 2749 | static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, |
| 2750 | const ObjCProtocolDecl *RHS) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2751 | return LHS->getDeclName() < RHS->getDeclName(); |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2752 | } |
| 2753 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2754 | static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols, |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2755 | unsigned NumProtocols) { |
| 2756 | if (NumProtocols == 0) return true; |
| 2757 | |
Douglas Gregor | 61cc296 | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 2758 | if (Protocols[0]->getCanonicalDecl() != Protocols[0]) |
| 2759 | return false; |
| 2760 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2761 | for (unsigned i = 1; i != NumProtocols; ++i) |
Douglas Gregor | 61cc296 | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 2762 | if (!CmpProtocolNames(Protocols[i-1], Protocols[i]) || |
| 2763 | Protocols[i]->getCanonicalDecl() != Protocols[i]) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2764 | return false; |
| 2765 | return true; |
| 2766 | } |
| 2767 | |
| 2768 | static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols, |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2769 | unsigned &NumProtocols) { |
| 2770 | ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2771 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2772 | // Sort protocols, keyed by name. |
| 2773 | std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); |
| 2774 | |
Douglas Gregor | 61cc296 | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 2775 | // Canonicalize. |
| 2776 | for (unsigned I = 0, N = NumProtocols; I != N; ++I) |
| 2777 | Protocols[I] = Protocols[I]->getCanonicalDecl(); |
| 2778 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2779 | // Remove duplicates. |
| 2780 | ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); |
| 2781 | NumProtocols = ProtocolsEnd-Protocols; |
| 2782 | } |
| 2783 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2784 | QualType ASTContext::getObjCObjectType(QualType BaseType, |
| 2785 | ObjCProtocolDecl * const *Protocols, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2786 | unsigned NumProtocols) const { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2787 | // If the base type is an interface and there aren't any protocols |
| 2788 | // to add, then the interface type will do just fine. |
| 2789 | if (!NumProtocols && isa<ObjCInterfaceType>(BaseType)) |
| 2790 | return BaseType; |
| 2791 | |
| 2792 | // Look in the folding set for an existing type. |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2793 | llvm::FoldingSetNodeID ID; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2794 | ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2795 | void *InsertPos = 0; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2796 | if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2797 | return QualType(QT, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2798 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2799 | // Build the canonical type, which has the canonical base type and |
| 2800 | // a sorted-and-uniqued list of protocols. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2801 | QualType Canonical; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2802 | bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols); |
| 2803 | if (!ProtocolsSorted || !BaseType.isCanonical()) { |
| 2804 | if (!ProtocolsSorted) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2805 | SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols, |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 2806 | Protocols + NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2807 | unsigned UniqueCount = NumProtocols; |
| 2808 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2809 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2810 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 2811 | &Sorted[0], UniqueCount); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2812 | } else { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2813 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 2814 | Protocols, NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2815 | } |
| 2816 | |
| 2817 | // Regenerate InsertPos. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2818 | ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2819 | } |
| 2820 | |
| 2821 | unsigned Size = sizeof(ObjCObjectTypeImpl); |
| 2822 | Size += NumProtocols * sizeof(ObjCProtocolDecl *); |
| 2823 | void *Mem = Allocate(Size, TypeAlignment); |
| 2824 | ObjCObjectTypeImpl *T = |
| 2825 | new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols); |
| 2826 | |
| 2827 | Types.push_back(T); |
| 2828 | ObjCObjectTypes.InsertNode(T, InsertPos); |
| 2829 | return QualType(T, 0); |
| 2830 | } |
| 2831 | |
| 2832 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 2833 | /// the given object type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2834 | QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2835 | llvm::FoldingSetNodeID ID; |
| 2836 | ObjCObjectPointerType::Profile(ID, ObjectT); |
| 2837 | |
| 2838 | void *InsertPos = 0; |
| 2839 | if (ObjCObjectPointerType *QT = |
| 2840 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2841 | return QualType(QT, 0); |
| 2842 | |
| 2843 | // Find the canonical object type. |
| 2844 | QualType Canonical; |
| 2845 | if (!ObjectT.isCanonical()) { |
| 2846 | Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT)); |
| 2847 | |
| 2848 | // Regenerate InsertPos. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2849 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2850 | } |
| 2851 | |
Douglas Gregor | fd6a088 | 2010-02-08 22:59:26 +0000 | [diff] [blame] | 2852 | // No match. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2853 | void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment); |
| 2854 | ObjCObjectPointerType *QType = |
| 2855 | new (Mem) ObjCObjectPointerType(Canonical, ObjectT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2856 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2857 | Types.push_back(QType); |
| 2858 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2859 | return QualType(QType, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2860 | } |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2861 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2862 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 2863 | /// specified ObjC interface decl. The list of protocols is optional. |
Douglas Gregor | 0af5501 | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 2864 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl, |
| 2865 | ObjCInterfaceDecl *PrevDecl) const { |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2866 | if (Decl->TypeForDecl) |
| 2867 | return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2868 | |
Douglas Gregor | 0af5501 | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 2869 | if (PrevDecl) { |
| 2870 | assert(PrevDecl->TypeForDecl && "previous decl has no TypeForDecl"); |
| 2871 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 2872 | return QualType(PrevDecl->TypeForDecl, 0); |
| 2873 | } |
| 2874 | |
Douglas Gregor | 8d2dbbf | 2011-12-16 16:34:57 +0000 | [diff] [blame] | 2875 | // Prefer the definition, if there is one. |
| 2876 | if (const ObjCInterfaceDecl *Def = Decl->getDefinition()) |
| 2877 | Decl = Def; |
| 2878 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2879 | void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment); |
| 2880 | ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl); |
| 2881 | Decl->TypeForDecl = T; |
| 2882 | Types.push_back(T); |
| 2883 | return QualType(T, 0); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2884 | } |
| 2885 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2886 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 2887 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2888 | /// multiple declarations that refer to "typeof(x)" all contain different |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2889 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2890 | /// on canonical type's (which are always unique). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2891 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) const { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2892 | TypeOfExprType *toe; |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2893 | if (tofExpr->isTypeDependent()) { |
| 2894 | llvm::FoldingSetNodeID ID; |
| 2895 | DependentTypeOfExprType::Profile(ID, *this, tofExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2896 | |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2897 | void *InsertPos = 0; |
| 2898 | DependentTypeOfExprType *Canon |
| 2899 | = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2900 | if (Canon) { |
| 2901 | // We already have a "canonical" version of an identical, dependent |
| 2902 | // typeof(expr) type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2903 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2904 | QualType((TypeOfExprType*)Canon, 0)); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 2905 | } else { |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2906 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2907 | Canon |
| 2908 | = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2909 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); |
| 2910 | toe = Canon; |
| 2911 | } |
| 2912 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2913 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2914 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2915 | } |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2916 | Types.push_back(toe); |
| 2917 | return QualType(toe, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2918 | } |
| 2919 | |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2920 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 2921 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 2922 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2923 | /// an issue. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2924 | /// on canonical type's (which are always unique). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2925 | QualType ASTContext::getTypeOfType(QualType tofType) const { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2926 | QualType Canonical = getCanonicalType(tofType); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2927 | TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2928 | Types.push_back(tot); |
| 2929 | return QualType(tot, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2930 | } |
| 2931 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2932 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2933 | /// getDecltypeType - Unlike many "get<Type>" functions, we don't unique |
| 2934 | /// DecltypeType AST's. The only motivation to unique these nodes would be |
| 2935 | /// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2936 | /// an issue. This doesn't effect the type checker, since it operates |
David Blaikie | 39e0203 | 2011-11-06 22:28:03 +0000 | [diff] [blame] | 2937 | /// on canonical types (which are always unique). |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 2938 | QualType ASTContext::getDecltypeType(Expr *e, QualType UnderlyingType) const { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2939 | DecltypeType *dt; |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2940 | |
| 2941 | // C++0x [temp.type]p2: |
| 2942 | // If an expression e involves a template parameter, decltype(e) denotes a |
| 2943 | // unique dependent type. Two such decltype-specifiers refer to the same |
| 2944 | // type only if their expressions are equivalent (14.5.6.1). |
| 2945 | if (e->isInstantiationDependent()) { |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2946 | llvm::FoldingSetNodeID ID; |
| 2947 | DependentDecltypeType::Profile(ID, *this, e); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2948 | |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2949 | void *InsertPos = 0; |
| 2950 | DependentDecltypeType *Canon |
| 2951 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2952 | if (Canon) { |
| 2953 | // We already have a "canonical" version of an equivalent, dependent |
| 2954 | // decltype type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2955 | dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy, |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2956 | QualType((DecltypeType*)Canon, 0)); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 2957 | } else { |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2958 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2959 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2960 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); |
| 2961 | dt = Canon; |
| 2962 | } |
| 2963 | } else { |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 2964 | dt = new (*this, TypeAlignment) DecltypeType(e, UnderlyingType, |
| 2965 | getCanonicalType(UnderlyingType)); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2966 | } |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2967 | Types.push_back(dt); |
| 2968 | return QualType(dt, 0); |
| 2969 | } |
| 2970 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 2971 | /// getUnaryTransformationType - We don't unique these, since the memory |
| 2972 | /// savings are minimal and these are rare. |
| 2973 | QualType ASTContext::getUnaryTransformType(QualType BaseType, |
| 2974 | QualType UnderlyingType, |
| 2975 | UnaryTransformType::UTTKind Kind) |
| 2976 | const { |
| 2977 | UnaryTransformType *Ty = |
Douglas Gregor | 69d9775 | 2011-05-25 17:51:54 +0000 | [diff] [blame] | 2978 | new (*this, TypeAlignment) UnaryTransformType (BaseType, UnderlyingType, |
| 2979 | Kind, |
| 2980 | UnderlyingType->isDependentType() ? |
Peter Collingbourne | 12fc4b0 | 2012-03-05 16:02:06 +0000 | [diff] [blame] | 2981 | QualType() : getCanonicalType(UnderlyingType)); |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 2982 | Types.push_back(Ty); |
| 2983 | return QualType(Ty, 0); |
| 2984 | } |
| 2985 | |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 2986 | /// getAutoType - We only unique auto types after they've been deduced. |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2987 | QualType ASTContext::getAutoType(QualType DeducedType) const { |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 2988 | void *InsertPos = 0; |
| 2989 | if (!DeducedType.isNull()) { |
| 2990 | // Look in the folding set for an existing type. |
| 2991 | llvm::FoldingSetNodeID ID; |
| 2992 | AutoType::Profile(ID, DeducedType); |
| 2993 | if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2994 | return QualType(AT, 0); |
| 2995 | } |
| 2996 | |
| 2997 | AutoType *AT = new (*this, TypeAlignment) AutoType(DeducedType); |
| 2998 | Types.push_back(AT); |
| 2999 | if (InsertPos) |
| 3000 | AutoTypes.InsertNode(AT, InsertPos); |
| 3001 | return QualType(AT, 0); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3002 | } |
| 3003 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3004 | /// getAtomicType - Return the uniqued reference to the atomic type for |
| 3005 | /// the given value type. |
| 3006 | QualType ASTContext::getAtomicType(QualType T) const { |
| 3007 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 3008 | // structure. |
| 3009 | llvm::FoldingSetNodeID ID; |
| 3010 | AtomicType::Profile(ID, T); |
| 3011 | |
| 3012 | void *InsertPos = 0; |
| 3013 | if (AtomicType *AT = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3014 | return QualType(AT, 0); |
| 3015 | |
| 3016 | // If the atomic value type isn't canonical, this won't be a canonical type |
| 3017 | // either, so fill in the canonical type field. |
| 3018 | QualType Canonical; |
| 3019 | if (!T.isCanonical()) { |
| 3020 | Canonical = getAtomicType(getCanonicalType(T)); |
| 3021 | |
| 3022 | // Get the new insert position for the node we care about. |
| 3023 | AtomicType *NewIP = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3024 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
| 3025 | } |
| 3026 | AtomicType *New = new (*this, TypeAlignment) AtomicType(T, Canonical); |
| 3027 | Types.push_back(New); |
| 3028 | AtomicTypes.InsertNode(New, InsertPos); |
| 3029 | return QualType(New, 0); |
| 3030 | } |
| 3031 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3032 | /// getAutoDeductType - Get type pattern for deducing against 'auto'. |
| 3033 | QualType ASTContext::getAutoDeductType() const { |
| 3034 | if (AutoDeductTy.isNull()) |
| 3035 | AutoDeductTy = getAutoType(QualType()); |
| 3036 | assert(!AutoDeductTy.isNull() && "can't build 'auto' pattern"); |
| 3037 | return AutoDeductTy; |
| 3038 | } |
| 3039 | |
| 3040 | /// getAutoRRefDeductType - Get type pattern for deducing against 'auto &&'. |
| 3041 | QualType ASTContext::getAutoRRefDeductType() const { |
| 3042 | if (AutoRRefDeductTy.isNull()) |
| 3043 | AutoRRefDeductTy = getRValueReferenceType(getAutoDeductType()); |
| 3044 | assert(!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern"); |
| 3045 | return AutoRRefDeductTy; |
| 3046 | } |
| 3047 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3048 | /// getTagDeclType - Return the unique reference to the type for the |
| 3049 | /// specified TagDecl (struct/union/class/enum) decl. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3050 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) const { |
Ted Kremenek | d778f88 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 3051 | assert (Decl); |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 3052 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 3053 | // away const? mutable? |
| 3054 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3055 | } |
| 3056 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3057 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 3058 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 3059 | /// needs to agree with the definition in <stddef.h>. |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 3060 | CanQualType ASTContext::getSizeType() const { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3061 | return getFromTargetType(Target->getSizeType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3062 | } |
| 3063 | |
Hans Wennborg | 29e97cb | 2011-10-27 08:29:09 +0000 | [diff] [blame] | 3064 | /// getIntMaxType - Return the unique type for "intmax_t" (C99 7.18.1.5). |
| 3065 | CanQualType ASTContext::getIntMaxType() const { |
| 3066 | return getFromTargetType(Target->getIntMaxType()); |
| 3067 | } |
| 3068 | |
| 3069 | /// getUIntMaxType - Return the unique type for "uintmax_t" (C99 7.18.1.5). |
| 3070 | CanQualType ASTContext::getUIntMaxType() const { |
| 3071 | return getFromTargetType(Target->getUIntMaxType()); |
| 3072 | } |
| 3073 | |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3074 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 3075 | /// Used when in C++, as a GCC extension. |
| 3076 | QualType ASTContext::getSignedWCharType() const { |
| 3077 | // FIXME: derive from "Target" ? |
| 3078 | return WCharTy; |
| 3079 | } |
| 3080 | |
| 3081 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 3082 | /// Used when in C++, as a GCC extension. |
| 3083 | QualType ASTContext::getUnsignedWCharType() const { |
| 3084 | // FIXME: derive from "Target" ? |
| 3085 | return UnsignedIntTy; |
| 3086 | } |
| 3087 | |
Hans Wennborg | 29e97cb | 2011-10-27 08:29:09 +0000 | [diff] [blame] | 3088 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17) |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 3089 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 3090 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3091 | return getFromTargetType(Target->getPtrDiffType(0)); |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 3092 | } |
| 3093 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3094 | //===----------------------------------------------------------------------===// |
| 3095 | // Type Operators |
| 3096 | //===----------------------------------------------------------------------===// |
| 3097 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3098 | CanQualType ASTContext::getCanonicalParamType(QualType T) const { |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3099 | // Push qualifiers into arrays, and then discard any remaining |
| 3100 | // qualifiers. |
| 3101 | T = getCanonicalType(T); |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3102 | T = getVariableArrayDecayedType(T); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3103 | const Type *Ty = T.getTypePtr(); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3104 | QualType Result; |
| 3105 | if (isa<ArrayType>(Ty)) { |
| 3106 | Result = getArrayDecayedType(QualType(Ty,0)); |
| 3107 | } else if (isa<FunctionType>(Ty)) { |
| 3108 | Result = getPointerType(QualType(Ty, 0)); |
| 3109 | } else { |
| 3110 | Result = QualType(Ty, 0); |
| 3111 | } |
| 3112 | |
| 3113 | return CanQualType::CreateUnsafe(Result); |
| 3114 | } |
| 3115 | |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3116 | QualType ASTContext::getUnqualifiedArrayType(QualType type, |
| 3117 | Qualifiers &quals) { |
| 3118 | SplitQualType splitType = type.getSplitUnqualifiedType(); |
| 3119 | |
| 3120 | // FIXME: getSplitUnqualifiedType() actually walks all the way to |
| 3121 | // the unqualified desugared type and then drops it on the floor. |
| 3122 | // We then have to strip that sugar back off with |
| 3123 | // getUnqualifiedDesugaredType(), which is silly. |
| 3124 | const ArrayType *AT = |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3125 | dyn_cast<ArrayType>(splitType.Ty->getUnqualifiedDesugaredType()); |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3126 | |
| 3127 | // 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] | 3128 | if (!AT) { |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3129 | quals = splitType.Quals; |
| 3130 | return QualType(splitType.Ty, 0); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3131 | } |
| 3132 | |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3133 | // Otherwise, recurse on the array's element type. |
| 3134 | QualType elementType = AT->getElementType(); |
| 3135 | QualType unqualElementType = getUnqualifiedArrayType(elementType, quals); |
| 3136 | |
| 3137 | // If that didn't change the element type, AT has no qualifiers, so we |
| 3138 | // can just use the results in splitType. |
| 3139 | if (elementType == unqualElementType) { |
| 3140 | assert(quals.empty()); // from the recursive call |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3141 | quals = splitType.Quals; |
| 3142 | return QualType(splitType.Ty, 0); |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3143 | } |
| 3144 | |
| 3145 | // Otherwise, add in the qualifiers from the outermost type, then |
| 3146 | // build the type back up. |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3147 | quals.addConsistentQualifiers(splitType.Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3148 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3149 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3150 | return getConstantArrayType(unqualElementType, CAT->getSize(), |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3151 | CAT->getSizeModifier(), 0); |
| 3152 | } |
| 3153 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3154 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3155 | return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3156 | } |
| 3157 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3158 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3159 | return getVariableArrayType(unqualElementType, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3160 | VAT->getSizeExpr(), |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3161 | VAT->getSizeModifier(), |
| 3162 | VAT->getIndexTypeCVRQualifiers(), |
| 3163 | VAT->getBracketsRange()); |
| 3164 | } |
| 3165 | |
| 3166 | const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT); |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3167 | return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(), |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3168 | DSAT->getSizeModifier(), 0, |
| 3169 | SourceRange()); |
| 3170 | } |
| 3171 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3172 | /// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that |
| 3173 | /// may be similar (C++ 4.4), replaces T1 and T2 with the type that |
| 3174 | /// they point to and return true. If T1 and T2 aren't pointer types |
| 3175 | /// or pointer-to-member types, or if they are not similar at this |
| 3176 | /// level, returns false and leaves T1 and T2 unchanged. Top-level |
| 3177 | /// qualifiers on T1 and T2 are ignored. This function will typically |
| 3178 | /// be called in a loop that successively "unwraps" pointer and |
| 3179 | /// pointer-to-member types to compare them at each level. |
| 3180 | bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) { |
| 3181 | const PointerType *T1PtrType = T1->getAs<PointerType>(), |
| 3182 | *T2PtrType = T2->getAs<PointerType>(); |
| 3183 | if (T1PtrType && T2PtrType) { |
| 3184 | T1 = T1PtrType->getPointeeType(); |
| 3185 | T2 = T2PtrType->getPointeeType(); |
| 3186 | return true; |
| 3187 | } |
| 3188 | |
| 3189 | const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(), |
| 3190 | *T2MPType = T2->getAs<MemberPointerType>(); |
| 3191 | if (T1MPType && T2MPType && |
| 3192 | hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0), |
| 3193 | QualType(T2MPType->getClass(), 0))) { |
| 3194 | T1 = T1MPType->getPointeeType(); |
| 3195 | T2 = T2MPType->getPointeeType(); |
| 3196 | return true; |
| 3197 | } |
| 3198 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3199 | if (getLangOpts().ObjC1) { |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3200 | const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(), |
| 3201 | *T2OPType = T2->getAs<ObjCObjectPointerType>(); |
| 3202 | if (T1OPType && T2OPType) { |
| 3203 | T1 = T1OPType->getPointeeType(); |
| 3204 | T2 = T2OPType->getPointeeType(); |
| 3205 | return true; |
| 3206 | } |
| 3207 | } |
| 3208 | |
| 3209 | // FIXME: Block pointers, too? |
| 3210 | |
| 3211 | return false; |
| 3212 | } |
| 3213 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3214 | DeclarationNameInfo |
| 3215 | ASTContext::getNameForTemplate(TemplateName Name, |
| 3216 | SourceLocation NameLoc) const { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3217 | switch (Name.getKind()) { |
| 3218 | case TemplateName::QualifiedTemplate: |
| 3219 | case TemplateName::Template: |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3220 | // DNInfo work in progress: CHECKME: what about DNLoc? |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3221 | return DeclarationNameInfo(Name.getAsTemplateDecl()->getDeclName(), |
| 3222 | NameLoc); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3223 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3224 | case TemplateName::OverloadedTemplate: { |
| 3225 | OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate(); |
| 3226 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 3227 | return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc); |
| 3228 | } |
| 3229 | |
| 3230 | case TemplateName::DependentTemplate: { |
| 3231 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3232 | DeclarationName DName; |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3233 | if (DTN->isIdentifier()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3234 | DName = DeclarationNames.getIdentifier(DTN->getIdentifier()); |
| 3235 | return DeclarationNameInfo(DName, NameLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3236 | } else { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3237 | DName = DeclarationNames.getCXXOperatorName(DTN->getOperator()); |
| 3238 | // DNInfo work in progress: FIXME: source locations? |
| 3239 | DeclarationNameLoc DNLoc; |
| 3240 | DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding(); |
| 3241 | DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding(); |
| 3242 | return DeclarationNameInfo(DName, NameLoc, DNLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3243 | } |
| 3244 | } |
| 3245 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3246 | case TemplateName::SubstTemplateTemplateParm: { |
| 3247 | SubstTemplateTemplateParmStorage *subst |
| 3248 | = Name.getAsSubstTemplateTemplateParm(); |
| 3249 | return DeclarationNameInfo(subst->getParameter()->getDeclName(), |
| 3250 | NameLoc); |
| 3251 | } |
| 3252 | |
| 3253 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 3254 | SubstTemplateTemplateParmPackStorage *subst |
| 3255 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 3256 | return DeclarationNameInfo(subst->getParameterPack()->getDeclName(), |
| 3257 | NameLoc); |
| 3258 | } |
| 3259 | } |
| 3260 | |
| 3261 | llvm_unreachable("bad template name kind!"); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3262 | } |
| 3263 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3264 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3265 | switch (Name.getKind()) { |
| 3266 | case TemplateName::QualifiedTemplate: |
| 3267 | case TemplateName::Template: { |
| 3268 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3269 | if (TemplateTemplateParmDecl *TTP |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3270 | = dyn_cast<TemplateTemplateParmDecl>(Template)) |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3271 | Template = getCanonicalTemplateTemplateParmDecl(TTP); |
| 3272 | |
| 3273 | // The canonical template name is the canonical template declaration. |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 3274 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3275 | } |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 3276 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3277 | case TemplateName::OverloadedTemplate: |
| 3278 | llvm_unreachable("cannot canonicalize overloaded template"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3279 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3280 | case TemplateName::DependentTemplate: { |
| 3281 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 3282 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 3283 | return DTN->CanonicalTemplateName; |
| 3284 | } |
| 3285 | |
| 3286 | case TemplateName::SubstTemplateTemplateParm: { |
| 3287 | SubstTemplateTemplateParmStorage *subst |
| 3288 | = Name.getAsSubstTemplateTemplateParm(); |
| 3289 | return getCanonicalTemplateName(subst->getReplacement()); |
| 3290 | } |
| 3291 | |
| 3292 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 3293 | SubstTemplateTemplateParmPackStorage *subst |
| 3294 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 3295 | TemplateTemplateParmDecl *canonParameter |
| 3296 | = getCanonicalTemplateTemplateParmDecl(subst->getParameterPack()); |
| 3297 | TemplateArgument canonArgPack |
| 3298 | = getCanonicalTemplateArgument(subst->getArgumentPack()); |
| 3299 | return getSubstTemplateTemplateParmPack(canonParameter, canonArgPack); |
| 3300 | } |
| 3301 | } |
| 3302 | |
| 3303 | llvm_unreachable("bad template name!"); |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 3304 | } |
| 3305 | |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 3306 | bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) { |
| 3307 | X = getCanonicalTemplateName(X); |
| 3308 | Y = getCanonicalTemplateName(Y); |
| 3309 | return X.getAsVoidPointer() == Y.getAsVoidPointer(); |
| 3310 | } |
| 3311 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3312 | TemplateArgument |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3313 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const { |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3314 | switch (Arg.getKind()) { |
| 3315 | case TemplateArgument::Null: |
| 3316 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3317 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3318 | case TemplateArgument::Expression: |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3319 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3320 | |
Douglas Gregor | d2008e2 | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 3321 | case TemplateArgument::Declaration: { |
| 3322 | if (Decl *D = Arg.getAsDecl()) |
| 3323 | return TemplateArgument(D->getCanonicalDecl()); |
| 3324 | return TemplateArgument((Decl*)0); |
| 3325 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3326 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3327 | case TemplateArgument::Template: |
| 3328 | return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate())); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3329 | |
| 3330 | case TemplateArgument::TemplateExpansion: |
| 3331 | return TemplateArgument(getCanonicalTemplateName( |
| 3332 | Arg.getAsTemplateOrTemplatePattern()), |
Douglas Gregor | 2be29f4 | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 3333 | Arg.getNumTemplateExpansions()); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3334 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3335 | case TemplateArgument::Integral: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3336 | return TemplateArgument(*Arg.getAsIntegral(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3337 | getCanonicalType(Arg.getIntegralType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3338 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3339 | case TemplateArgument::Type: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3340 | return TemplateArgument(getCanonicalType(Arg.getAsType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3341 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3342 | case TemplateArgument::Pack: { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 3343 | if (Arg.pack_size() == 0) |
| 3344 | return Arg; |
| 3345 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3346 | TemplateArgument *CanonArgs |
| 3347 | = new (*this) TemplateArgument[Arg.pack_size()]; |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3348 | unsigned Idx = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3349 | for (TemplateArgument::pack_iterator A = Arg.pack_begin(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3350 | AEnd = Arg.pack_end(); |
| 3351 | A != AEnd; (void)++A, ++Idx) |
| 3352 | CanonArgs[Idx] = getCanonicalTemplateArgument(*A); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3353 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3354 | return TemplateArgument(CanonArgs, Arg.pack_size()); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3355 | } |
| 3356 | } |
| 3357 | |
| 3358 | // Silence GCC warning |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3359 | llvm_unreachable("Unhandled template argument kind"); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3360 | } |
| 3361 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3362 | NestedNameSpecifier * |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3363 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3364 | if (!NNS) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3365 | return 0; |
| 3366 | |
| 3367 | switch (NNS->getKind()) { |
| 3368 | case NestedNameSpecifier::Identifier: |
| 3369 | // Canonicalize the prefix but keep the identifier the same. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3370 | return NestedNameSpecifier::Create(*this, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3371 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 3372 | NNS->getAsIdentifier()); |
| 3373 | |
| 3374 | case NestedNameSpecifier::Namespace: |
| 3375 | // A namespace is canonical; build a nested-name-specifier with |
| 3376 | // this namespace and no prefix. |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 3377 | return NestedNameSpecifier::Create(*this, 0, |
| 3378 | NNS->getAsNamespace()->getOriginalNamespace()); |
| 3379 | |
| 3380 | case NestedNameSpecifier::NamespaceAlias: |
| 3381 | // A namespace is canonical; build a nested-name-specifier with |
| 3382 | // this namespace and no prefix. |
| 3383 | return NestedNameSpecifier::Create(*this, 0, |
| 3384 | NNS->getAsNamespaceAlias()->getNamespace() |
| 3385 | ->getOriginalNamespace()); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3386 | |
| 3387 | case NestedNameSpecifier::TypeSpec: |
| 3388 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 3389 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3390 | |
| 3391 | // If we have some kind of dependent-named type (e.g., "typename T::type"), |
| 3392 | // break it apart into its prefix and identifier, then reconsititute those |
| 3393 | // as the canonical nested-name-specifier. This is required to canonicalize |
| 3394 | // a dependent nested-name-specifier involving typedefs of dependent-name |
| 3395 | // types, e.g., |
| 3396 | // typedef typename T::type T1; |
| 3397 | // typedef typename T1::type T2; |
Eli Friedman | 16412ef | 2012-03-03 04:09:56 +0000 | [diff] [blame] | 3398 | if (const DependentNameType *DNT = T->getAs<DependentNameType>()) |
| 3399 | return NestedNameSpecifier::Create(*this, DNT->getQualifier(), |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3400 | const_cast<IdentifierInfo *>(DNT->getIdentifier())); |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3401 | |
Eli Friedman | 16412ef | 2012-03-03 04:09:56 +0000 | [diff] [blame] | 3402 | // Otherwise, just canonicalize the type, and force it to be a TypeSpec. |
| 3403 | // FIXME: Why are TypeSpec and TypeSpecWithTemplate distinct in the |
| 3404 | // first place? |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3405 | return NestedNameSpecifier::Create(*this, 0, false, |
| 3406 | const_cast<Type*>(T.getTypePtr())); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3407 | } |
| 3408 | |
| 3409 | case NestedNameSpecifier::Global: |
| 3410 | // The global specifier is canonical and unique. |
| 3411 | return NNS; |
| 3412 | } |
| 3413 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3414 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3415 | } |
| 3416 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3417 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3418 | const ArrayType *ASTContext::getAsArrayType(QualType T) const { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3419 | // Handle the non-qualified case efficiently. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3420 | if (!T.hasLocalQualifiers()) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3421 | // Handle the common positive case fast. |
| 3422 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 3423 | return AT; |
| 3424 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3425 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3426 | // Handle the common negative case fast. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3427 | if (!isa<ArrayType>(T.getCanonicalType())) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3428 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3429 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3430 | // Apply any qualifiers from the array type to the element type. This |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3431 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 3432 | // 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] | 3433 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3434 | // If we get here, we either have type qualifiers on the type, or we have |
| 3435 | // 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] | 3436 | // we must propagate them down into the element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3437 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3438 | SplitQualType split = T.getSplitDesugaredType(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3439 | Qualifiers qs = split.Quals; |
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 | // If we have a simple case, just return now. |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3442 | const ArrayType *ATy = dyn_cast<ArrayType>(split.Ty); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3443 | if (ATy == 0 || qs.empty()) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3444 | return ATy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3445 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3446 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 3447 | // qualifiers into the array element type and return a new array type. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3448 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3449 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3450 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 3451 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 3452 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3453 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3454 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 3455 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 3456 | IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3457 | IAT->getIndexTypeCVRQualifiers())); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3458 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3459 | if (const DependentSizedArrayType *DSAT |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3460 | = dyn_cast<DependentSizedArrayType>(ATy)) |
| 3461 | return cast<ArrayType>( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3462 | getDependentSizedArrayType(NewEltTy, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3463 | DSAT->getSizeExpr(), |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3464 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3465 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3466 | DSAT->getBracketsRange())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3467 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3468 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3469 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3470 | VAT->getSizeExpr(), |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3471 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3472 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3473 | VAT->getBracketsRange())); |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 3474 | } |
| 3475 | |
Douglas Gregor | 79e6bd3 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 3476 | QualType ASTContext::getAdjustedParameterType(QualType T) { |
| 3477 | // C99 6.7.5.3p7: |
| 3478 | // A declaration of a parameter as "array of type" shall be |
| 3479 | // adjusted to "qualified pointer to type", where the type |
| 3480 | // qualifiers (if any) are those specified within the [ and ] of |
| 3481 | // the array type derivation. |
| 3482 | if (T->isArrayType()) |
| 3483 | return getArrayDecayedType(T); |
| 3484 | |
| 3485 | // C99 6.7.5.3p8: |
| 3486 | // A declaration of a parameter as "function returning type" |
| 3487 | // shall be adjusted to "pointer to function returning type", as |
| 3488 | // in 6.3.2.1. |
| 3489 | if (T->isFunctionType()) |
| 3490 | return getPointerType(T); |
| 3491 | |
| 3492 | return T; |
| 3493 | } |
| 3494 | |
| 3495 | QualType ASTContext::getSignatureParameterType(QualType T) { |
| 3496 | T = getVariableArrayDecayedType(T); |
| 3497 | T = getAdjustedParameterType(T); |
| 3498 | return T.getUnqualifiedType(); |
| 3499 | } |
| 3500 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3501 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 3502 | /// specified array type to a pointer. This operation is non-trivial when |
| 3503 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 3504 | /// this returns a pointer to a properly qualified element of the array. |
| 3505 | /// |
| 3506 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3507 | QualType ASTContext::getArrayDecayedType(QualType Ty) const { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3508 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 3509 | // typedefs in the element type of the array. This also handles propagation |
| 3510 | // of type qualifiers from the array type into the element type if present |
| 3511 | // (C99 6.7.3p8). |
| 3512 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 3513 | assert(PrettyArrayType && "Not an array type!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3514 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3515 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3516 | |
| 3517 | // int x[restrict 4] -> int *restrict |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3518 | return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3519 | } |
| 3520 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3521 | QualType ASTContext::getBaseElementType(const ArrayType *array) const { |
| 3522 | return getBaseElementType(array->getElementType()); |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 3523 | } |
| 3524 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3525 | QualType ASTContext::getBaseElementType(QualType type) const { |
| 3526 | Qualifiers qs; |
| 3527 | while (true) { |
| 3528 | SplitQualType split = type.getSplitDesugaredType(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3529 | const ArrayType *array = split.Ty->getAsArrayTypeUnsafe(); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3530 | if (!array) break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3531 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3532 | type = array->getElementType(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3533 | qs.addConsistentQualifiers(split.Quals); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3534 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3535 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3536 | return getQualifiedType(type, qs); |
Anders Carlsson | 6183a99 | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 3537 | } |
| 3538 | |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 3539 | /// getConstantArrayElementCount - Returns number of constant array elements. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3540 | uint64_t |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 3541 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
| 3542 | uint64_t ElementCount = 1; |
| 3543 | do { |
| 3544 | ElementCount *= CA->getSize().getZExtValue(); |
| 3545 | CA = dyn_cast<ConstantArrayType>(CA->getElementType()); |
| 3546 | } while (CA); |
| 3547 | return ElementCount; |
| 3548 | } |
| 3549 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3550 | /// getFloatingRank - Return a relative rank for floating point types. |
| 3551 | /// 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] | 3552 | static FloatingRank getFloatingRank(QualType T) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3553 | if (const ComplexType *CT = T->getAs<ComplexType>()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3554 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3555 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3556 | assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"); |
| 3557 | switch (T->getAs<BuiltinType>()->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3558 | default: llvm_unreachable("getFloatingRank(): not a floating type"); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3559 | case BuiltinType::Half: return HalfRank; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3560 | case BuiltinType::Float: return FloatRank; |
| 3561 | case BuiltinType::Double: return DoubleRank; |
| 3562 | case BuiltinType::LongDouble: return LongDoubleRank; |
| 3563 | } |
| 3564 | } |
| 3565 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3566 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 3567 | /// point or a complex type (based on typeDomain/typeSize). |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 3568 | /// 'typeDomain' is a real floating point or complex type. |
| 3569 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 3570 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 3571 | QualType Domain) const { |
| 3572 | FloatingRank EltRank = getFloatingRank(Size); |
| 3573 | if (Domain->isComplexType()) { |
| 3574 | switch (EltRank) { |
David Blaikie | 561d3ab | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 3575 | case HalfRank: llvm_unreachable("Complex half is not supported"); |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 3576 | case FloatRank: return FloatComplexTy; |
| 3577 | case DoubleRank: return DoubleComplexTy; |
| 3578 | case LongDoubleRank: return LongDoubleComplexTy; |
| 3579 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3580 | } |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 3581 | |
| 3582 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 3583 | switch (EltRank) { |
David Blaikie | 561d3ab | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 3584 | case HalfRank: llvm_unreachable("Half ranks are not valid here"); |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 3585 | case FloatRank: return FloatTy; |
| 3586 | case DoubleRank: return DoubleTy; |
| 3587 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 3588 | } |
David Blaikie | 561d3ab | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 3589 | llvm_unreachable("getFloatingRank(): illegal value for rank"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3590 | } |
| 3591 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3592 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 3593 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 3594 | /// '_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] | 3595 | /// LHS < RHS, return -1. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3596 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const { |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3597 | FloatingRank LHSR = getFloatingRank(LHS); |
| 3598 | FloatingRank RHSR = getFloatingRank(RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3599 | |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3600 | if (LHSR == RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 3601 | return 0; |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3602 | if (LHSR > RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 3603 | return 1; |
| 3604 | return -1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3605 | } |
| 3606 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3607 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 3608 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 3609 | /// or if it is not canonicalized. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3610 | unsigned ASTContext::getIntegerRank(const Type *T) const { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 3611 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3612 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3613 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3614 | default: llvm_unreachable("getIntegerRank(): not a built-in integer"); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3615 | case BuiltinType::Bool: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3616 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3617 | case BuiltinType::Char_S: |
| 3618 | case BuiltinType::Char_U: |
| 3619 | case BuiltinType::SChar: |
| 3620 | case BuiltinType::UChar: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3621 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3622 | case BuiltinType::Short: |
| 3623 | case BuiltinType::UShort: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3624 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3625 | case BuiltinType::Int: |
| 3626 | case BuiltinType::UInt: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3627 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3628 | case BuiltinType::Long: |
| 3629 | case BuiltinType::ULong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3630 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3631 | case BuiltinType::LongLong: |
| 3632 | case BuiltinType::ULongLong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3633 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 3634 | case BuiltinType::Int128: |
| 3635 | case BuiltinType::UInt128: |
| 3636 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3637 | } |
| 3638 | } |
| 3639 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3640 | /// \brief Whether this is a promotable bitfield reference according |
| 3641 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
| 3642 | /// |
| 3643 | /// \returns the type this bit-field will promote to, or NULL if no |
| 3644 | /// promotion occurs. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3645 | QualType ASTContext::isPromotableBitField(Expr *E) const { |
Douglas Gregor | ceafbde | 2010-05-24 20:13:53 +0000 | [diff] [blame] | 3646 | if (E->isTypeDependent() || E->isValueDependent()) |
| 3647 | return QualType(); |
| 3648 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3649 | FieldDecl *Field = E->getBitField(); |
| 3650 | if (!Field) |
| 3651 | return QualType(); |
| 3652 | |
| 3653 | QualType FT = Field->getType(); |
| 3654 | |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3655 | uint64_t BitWidth = Field->getBitWidthValue(*this); |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3656 | uint64_t IntSize = getTypeSize(IntTy); |
| 3657 | // GCC extension compatibility: if the bit-field size is less than or equal |
| 3658 | // to the size of int, it gets promoted no matter what its type is. |
| 3659 | // For instance, unsigned long bf : 4 gets promoted to signed int. |
| 3660 | if (BitWidth < IntSize) |
| 3661 | return IntTy; |
| 3662 | |
| 3663 | if (BitWidth == IntSize) |
| 3664 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
| 3665 | |
| 3666 | // Types bigger than int are not subject to promotions, and therefore act |
| 3667 | // like the base type. |
| 3668 | // FIXME: This doesn't quite match what gcc does, but what gcc does here |
| 3669 | // is ridiculous. |
| 3670 | return QualType(); |
| 3671 | } |
| 3672 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3673 | /// getPromotedIntegerType - Returns the type that Promotable will |
| 3674 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
| 3675 | /// integer type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3676 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) const { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3677 | assert(!Promotable.isNull()); |
| 3678 | assert(Promotable->isPromotableIntegerType()); |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 3679 | if (const EnumType *ET = Promotable->getAs<EnumType>()) |
| 3680 | return ET->getDecl()->getPromotionType(); |
Eli Friedman | 68a2dc4 | 2011-10-26 07:22:48 +0000 | [diff] [blame] | 3681 | |
| 3682 | if (const BuiltinType *BT = Promotable->getAs<BuiltinType>()) { |
| 3683 | // C++ [conv.prom]: A prvalue of type char16_t, char32_t, or wchar_t |
| 3684 | // (3.9.1) can be converted to a prvalue of the first of the following |
| 3685 | // types that can represent all the values of its underlying type: |
| 3686 | // int, unsigned int, long int, unsigned long int, long long int, or |
| 3687 | // unsigned long long int [...] |
| 3688 | // FIXME: Is there some better way to compute this? |
| 3689 | if (BT->getKind() == BuiltinType::WChar_S || |
| 3690 | BT->getKind() == BuiltinType::WChar_U || |
| 3691 | BT->getKind() == BuiltinType::Char16 || |
| 3692 | BT->getKind() == BuiltinType::Char32) { |
| 3693 | bool FromIsSigned = BT->getKind() == BuiltinType::WChar_S; |
| 3694 | uint64_t FromSize = getTypeSize(BT); |
| 3695 | QualType PromoteTypes[] = { IntTy, UnsignedIntTy, LongTy, UnsignedLongTy, |
| 3696 | LongLongTy, UnsignedLongLongTy }; |
| 3697 | for (size_t Idx = 0; Idx < llvm::array_lengthof(PromoteTypes); ++Idx) { |
| 3698 | uint64_t ToSize = getTypeSize(PromoteTypes[Idx]); |
| 3699 | if (FromSize < ToSize || |
| 3700 | (FromSize == ToSize && |
| 3701 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) |
| 3702 | return PromoteTypes[Idx]; |
| 3703 | } |
| 3704 | llvm_unreachable("char type should fit into long long"); |
| 3705 | } |
| 3706 | } |
| 3707 | |
| 3708 | // At this point, we should have a signed or unsigned integer type. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3709 | if (Promotable->isSignedIntegerType()) |
| 3710 | return IntTy; |
| 3711 | uint64_t PromotableSize = getTypeSize(Promotable); |
| 3712 | uint64_t IntSize = getTypeSize(IntTy); |
| 3713 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
| 3714 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
| 3715 | } |
| 3716 | |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 3717 | /// \brief Recurses in pointer/array types until it finds an objc retainable |
| 3718 | /// type and returns its ownership. |
| 3719 | Qualifiers::ObjCLifetime ASTContext::getInnerObjCOwnership(QualType T) const { |
| 3720 | while (!T.isNull()) { |
| 3721 | if (T.getObjCLifetime() != Qualifiers::OCL_None) |
| 3722 | return T.getObjCLifetime(); |
| 3723 | if (T->isArrayType()) |
| 3724 | T = getBaseElementType(T); |
| 3725 | else if (const PointerType *PT = T->getAs<PointerType>()) |
| 3726 | T = PT->getPointeeType(); |
| 3727 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
Argyrios Kyrtzidis | 28445f0 | 2011-07-01 23:01:46 +0000 | [diff] [blame] | 3728 | T = RT->getPointeeType(); |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 3729 | else |
| 3730 | break; |
| 3731 | } |
| 3732 | |
| 3733 | return Qualifiers::OCL_None; |
| 3734 | } |
| 3735 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3736 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3737 | /// 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] | 3738 | /// LHS < RHS, return -1. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3739 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3740 | const Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 3741 | const Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3742 | if (LHSC == RHSC) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3743 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3744 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 3745 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3746 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3747 | unsigned LHSRank = getIntegerRank(LHSC); |
| 3748 | unsigned RHSRank = getIntegerRank(RHSC); |
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 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 3751 | if (LHSRank == RHSRank) return 0; |
| 3752 | return LHSRank > RHSRank ? 1 : -1; |
| 3753 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3754 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3755 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 3756 | if (LHSUnsigned) { |
| 3757 | // If the unsigned [LHS] type is larger, return it. |
| 3758 | if (LHSRank >= RHSRank) |
| 3759 | return 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3760 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3761 | // If the signed type can represent all values of the unsigned type, it |
| 3762 | // 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] | 3763 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3764 | return -1; |
| 3765 | } |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3766 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3767 | // If the unsigned [RHS] type is larger, return it. |
| 3768 | if (RHSRank >= LHSRank) |
| 3769 | return -1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3770 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3771 | // If the signed type can represent all values of the unsigned type, it |
| 3772 | // 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] | 3773 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3774 | return 1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3775 | } |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3776 | |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3777 | static RecordDecl * |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3778 | CreateRecordDecl(const ASTContext &Ctx, RecordDecl::TagKind TK, |
| 3779 | DeclContext *DC, IdentifierInfo *Id) { |
| 3780 | SourceLocation Loc; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3781 | if (Ctx.getLangOpts().CPlusPlus) |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3782 | return CXXRecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3783 | else |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3784 | return RecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3785 | } |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3786 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3787 | // getCFConstantStringType - Return the type used for constant CFStrings. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3788 | QualType ASTContext::getCFConstantStringType() const { |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3789 | if (!CFConstantStringTypeDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3790 | CFConstantStringTypeDecl = |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3791 | CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3792 | &Idents.get("NSConstantString")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3793 | CFConstantStringTypeDecl->startDefinition(); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3794 | |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 3795 | QualType FieldTypes[4]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3796 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3797 | // const int *isa; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3798 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 3799 | // int flags; |
| 3800 | FieldTypes[1] = IntTy; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3801 | // const char *str; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3802 | FieldTypes[2] = getPointerType(CharTy.withConst()); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3803 | // long length; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3804 | FieldTypes[3] = LongTy; |
| 3805 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3806 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3807 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3808 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3809 | SourceLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3810 | SourceLocation(), 0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3811 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3812 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3813 | /*Mutable=*/false, |
| 3814 | /*HasInit=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3815 | Field->setAccess(AS_public); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3816 | CFConstantStringTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3817 | } |
| 3818 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3819 | CFConstantStringTypeDecl->completeDefinition(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3820 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3821 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3822 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 8467583 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 3823 | } |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 3824 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3825 | void ASTContext::setCFConstantStringType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3826 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3827 | assert(Rec && "Invalid CFConstantStringType"); |
| 3828 | CFConstantStringTypeDecl = Rec->getDecl(); |
| 3829 | } |
| 3830 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3831 | QualType ASTContext::getBlockDescriptorType() const { |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3832 | if (BlockDescriptorType) |
| 3833 | return getTagDeclType(BlockDescriptorType); |
| 3834 | |
| 3835 | RecordDecl *T; |
| 3836 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3837 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3838 | &Idents.get("__block_descriptor")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3839 | T->startDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3840 | |
| 3841 | QualType FieldTypes[] = { |
| 3842 | UnsignedLongTy, |
| 3843 | UnsignedLongTy, |
| 3844 | }; |
| 3845 | |
| 3846 | const char *FieldNames[] = { |
| 3847 | "reserved", |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3848 | "Size" |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3849 | }; |
| 3850 | |
| 3851 | for (size_t i = 0; i < 2; ++i) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3852 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3853 | SourceLocation(), |
| 3854 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3855 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3856 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3857 | /*Mutable=*/false, |
| 3858 | /*HasInit=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3859 | Field->setAccess(AS_public); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3860 | T->addDecl(Field); |
| 3861 | } |
| 3862 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3863 | T->completeDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3864 | |
| 3865 | BlockDescriptorType = T; |
| 3866 | |
| 3867 | return getTagDeclType(BlockDescriptorType); |
| 3868 | } |
| 3869 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3870 | QualType ASTContext::getBlockDescriptorExtendedType() const { |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3871 | if (BlockDescriptorExtendedType) |
| 3872 | return getTagDeclType(BlockDescriptorExtendedType); |
| 3873 | |
| 3874 | RecordDecl *T; |
| 3875 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3876 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3877 | &Idents.get("__block_descriptor_withcopydispose")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3878 | T->startDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3879 | |
| 3880 | QualType FieldTypes[] = { |
| 3881 | UnsignedLongTy, |
| 3882 | UnsignedLongTy, |
| 3883 | getPointerType(VoidPtrTy), |
| 3884 | getPointerType(VoidPtrTy) |
| 3885 | }; |
| 3886 | |
| 3887 | const char *FieldNames[] = { |
| 3888 | "reserved", |
| 3889 | "Size", |
| 3890 | "CopyFuncPtr", |
| 3891 | "DestroyFuncPtr" |
| 3892 | }; |
| 3893 | |
| 3894 | for (size_t i = 0; i < 4; ++i) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3895 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3896 | SourceLocation(), |
| 3897 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3898 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3899 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3900 | /*Mutable=*/false, |
| 3901 | /*HasInit=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3902 | Field->setAccess(AS_public); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3903 | T->addDecl(Field); |
| 3904 | } |
| 3905 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3906 | T->completeDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3907 | |
| 3908 | BlockDescriptorExtendedType = T; |
| 3909 | |
| 3910 | return getTagDeclType(BlockDescriptorExtendedType); |
| 3911 | } |
| 3912 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3913 | bool ASTContext::BlockRequiresCopying(QualType Ty) const { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3914 | if (Ty->isObjCRetainableType()) |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3915 | return true; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3916 | if (getLangOpts().CPlusPlus) { |
Fariborz Jahanian | e38be61 | 2010-11-17 00:21:28 +0000 | [diff] [blame] | 3917 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3918 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 3919 | return RD->hasConstCopyConstructor(); |
Fariborz Jahanian | e38be61 | 2010-11-17 00:21:28 +0000 | [diff] [blame] | 3920 | |
| 3921 | } |
| 3922 | } |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3923 | return false; |
| 3924 | } |
| 3925 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3926 | QualType |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3927 | ASTContext::BuildByRefType(StringRef DeclName, QualType Ty) const { |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3928 | // type = struct __Block_byref_1_X { |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3929 | // void *__isa; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3930 | // struct __Block_byref_1_X *__forwarding; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3931 | // unsigned int __flags; |
| 3932 | // unsigned int __size; |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 3933 | // void *__copy_helper; // as needed |
| 3934 | // void *__destroy_help // as needed |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3935 | // int X; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3936 | // } * |
| 3937 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3938 | bool HasCopyAndDispose = BlockRequiresCopying(Ty); |
| 3939 | |
| 3940 | // FIXME: Move up |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 3941 | SmallString<36> Name; |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 3942 | llvm::raw_svector_ostream(Name) << "__Block_byref_" << |
| 3943 | ++UniqueBlockByRefTypeID << '_' << DeclName; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3944 | RecordDecl *T; |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3945 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, &Idents.get(Name.str())); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3946 | T->startDefinition(); |
| 3947 | QualType Int32Ty = IntTy; |
| 3948 | assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported"); |
| 3949 | QualType FieldTypes[] = { |
| 3950 | getPointerType(VoidPtrTy), |
| 3951 | getPointerType(getTagDeclType(T)), |
| 3952 | Int32Ty, |
| 3953 | Int32Ty, |
| 3954 | getPointerType(VoidPtrTy), |
| 3955 | getPointerType(VoidPtrTy), |
| 3956 | Ty |
| 3957 | }; |
| 3958 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3959 | StringRef FieldNames[] = { |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3960 | "__isa", |
| 3961 | "__forwarding", |
| 3962 | "__flags", |
| 3963 | "__size", |
| 3964 | "__copy_helper", |
| 3965 | "__destroy_helper", |
| 3966 | DeclName, |
| 3967 | }; |
| 3968 | |
| 3969 | for (size_t i = 0; i < 7; ++i) { |
| 3970 | if (!HasCopyAndDispose && i >=4 && i <= 5) |
| 3971 | continue; |
| 3972 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3973 | SourceLocation(), |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3974 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3975 | FieldTypes[i], /*TInfo=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3976 | /*BitWidth=*/0, /*Mutable=*/false, |
| 3977 | /*HasInit=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3978 | Field->setAccess(AS_public); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3979 | T->addDecl(Field); |
| 3980 | } |
| 3981 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3982 | T->completeDefinition(); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3983 | |
| 3984 | return getPointerType(getTagDeclType(T)); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3985 | } |
| 3986 | |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 3987 | TypedefDecl *ASTContext::getObjCInstanceTypeDecl() { |
| 3988 | if (!ObjCInstanceTypeDecl) |
| 3989 | ObjCInstanceTypeDecl = TypedefDecl::Create(*this, |
| 3990 | getTranslationUnitDecl(), |
| 3991 | SourceLocation(), |
| 3992 | SourceLocation(), |
| 3993 | &Idents.get("instancetype"), |
| 3994 | getTrivialTypeSourceInfo(getObjCIdType())); |
| 3995 | return ObjCInstanceTypeDecl; |
| 3996 | } |
| 3997 | |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3998 | // This returns true if a type has been typedefed to BOOL: |
| 3999 | // typedef <type> BOOL; |
Chris Lattner | 2d99833 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 4000 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 4001 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | bb49c3e | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 4002 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 4003 | return II->isStr("BOOL"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4004 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4005 | return false; |
| 4006 | } |
| 4007 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4008 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4009 | /// purpose. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4010 | CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const { |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4011 | if (!type->isIncompleteArrayType() && type->isIncompleteType()) |
| 4012 | return CharUnits::Zero(); |
| 4013 | |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4014 | CharUnits sz = getTypeSizeInChars(type); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4015 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4016 | // 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] | 4017 | if (sz.isPositive() && type->isIntegralOrEnumerationType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4018 | sz = std::max(sz, getTypeSizeInChars(IntTy)); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4019 | // Treat arrays as pointers, since that's how they're passed in. |
| 4020 | else if (type->isArrayType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4021 | sz = getTypeSizeInChars(VoidPtrTy); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4022 | return sz; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4023 | } |
| 4024 | |
| 4025 | static inline |
| 4026 | std::string charUnitsToString(const CharUnits &CU) { |
| 4027 | return llvm::itostr(CU.getQuantity()); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4028 | } |
| 4029 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4030 | /// getObjCEncodingForBlock - Return the encoded type for this block |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4031 | /// declaration. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4032 | std::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const { |
| 4033 | std::string S; |
| 4034 | |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4035 | const BlockDecl *Decl = Expr->getBlockDecl(); |
| 4036 | QualType BlockTy = |
| 4037 | Expr->getType()->getAs<BlockPointerType>()->getPointeeType(); |
| 4038 | // Encode result type. |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 4039 | getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4040 | // Compute size of all parameters. |
| 4041 | // Start with computing size of a pointer in number of bytes. |
| 4042 | // FIXME: There might(should) be a better way of doing this computation! |
| 4043 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4044 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
| 4045 | CharUnits ParmOffset = PtrSize; |
Fariborz Jahanian | 6f46c26 | 2010-04-08 18:06:22 +0000 | [diff] [blame] | 4046 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4047 | E = Decl->param_end(); PI != E; ++PI) { |
| 4048 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4049 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4050 | assert (sz.isPositive() && "BlockExpr - Incomplete param type"); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4051 | ParmOffset += sz; |
| 4052 | } |
| 4053 | // Size of the argument frame |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4054 | S += charUnitsToString(ParmOffset); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4055 | // Block pointer and offset. |
| 4056 | S += "@?0"; |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4057 | |
| 4058 | // Argument types. |
| 4059 | ParmOffset = PtrSize; |
| 4060 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E = |
| 4061 | Decl->param_end(); PI != E; ++PI) { |
| 4062 | ParmVarDecl *PVDecl = *PI; |
| 4063 | QualType PType = PVDecl->getOriginalType(); |
| 4064 | if (const ArrayType *AT = |
| 4065 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4066 | // Use array's original type only if it has known number of |
| 4067 | // elements. |
| 4068 | if (!isa<ConstantArrayType>(AT)) |
| 4069 | PType = PVDecl->getType(); |
| 4070 | } else if (PType->isFunctionType()) |
| 4071 | PType = PVDecl->getType(); |
| 4072 | getObjCEncodingForType(PType, S); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4073 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4074 | ParmOffset += getObjCEncodingTypeSize(PType); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4075 | } |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4076 | |
| 4077 | return S; |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4078 | } |
| 4079 | |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4080 | bool ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl, |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4081 | std::string& S) { |
| 4082 | // Encode result type. |
| 4083 | getObjCEncodingForType(Decl->getResultType(), S); |
| 4084 | CharUnits ParmOffset; |
| 4085 | // Compute size of all parameters. |
| 4086 | for (FunctionDecl::param_const_iterator PI = Decl->param_begin(), |
| 4087 | E = Decl->param_end(); PI != E; ++PI) { |
| 4088 | QualType PType = (*PI)->getType(); |
| 4089 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4090 | if (sz.isZero()) |
| 4091 | return true; |
| 4092 | |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4093 | assert (sz.isPositive() && |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4094 | "getObjCEncodingForFunctionDecl - Incomplete param type"); |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4095 | ParmOffset += sz; |
| 4096 | } |
| 4097 | S += charUnitsToString(ParmOffset); |
| 4098 | ParmOffset = CharUnits::Zero(); |
| 4099 | |
| 4100 | // Argument types. |
| 4101 | for (FunctionDecl::param_const_iterator PI = Decl->param_begin(), |
| 4102 | E = Decl->param_end(); PI != E; ++PI) { |
| 4103 | ParmVarDecl *PVDecl = *PI; |
| 4104 | QualType PType = PVDecl->getOriginalType(); |
| 4105 | if (const ArrayType *AT = |
| 4106 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4107 | // Use array's original type only if it has known number of |
| 4108 | // elements. |
| 4109 | if (!isa<ConstantArrayType>(AT)) |
| 4110 | PType = PVDecl->getType(); |
| 4111 | } else if (PType->isFunctionType()) |
| 4112 | PType = PVDecl->getType(); |
| 4113 | getObjCEncodingForType(PType, S); |
| 4114 | S += charUnitsToString(ParmOffset); |
| 4115 | ParmOffset += getObjCEncodingTypeSize(PType); |
| 4116 | } |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4117 | |
| 4118 | return false; |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4119 | } |
| 4120 | |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4121 | /// getObjCEncodingForMethodParameter - Return the encoded type for a single |
| 4122 | /// method parameter or return type. If Extended, include class names and |
| 4123 | /// block object types. |
| 4124 | void ASTContext::getObjCEncodingForMethodParameter(Decl::ObjCDeclQualifier QT, |
| 4125 | QualType T, std::string& S, |
| 4126 | bool Extended) const { |
| 4127 | // Encode type qualifer, 'in', 'inout', etc. for the parameter. |
| 4128 | getObjCEncodingForTypeQualifier(QT, S); |
| 4129 | // Encode parameter type. |
| 4130 | getObjCEncodingForTypeImpl(T, S, true, true, 0, |
| 4131 | true /*OutermostType*/, |
| 4132 | false /*EncodingProperty*/, |
| 4133 | false /*StructField*/, |
| 4134 | Extended /*EncodeBlockParameters*/, |
| 4135 | Extended /*EncodeClassNames*/); |
| 4136 | } |
| 4137 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4138 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4139 | /// declaration. |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4140 | bool ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4141 | std::string& S, |
| 4142 | bool Extended) const { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4143 | // FIXME: This is not very efficient. |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4144 | // Encode return type. |
| 4145 | getObjCEncodingForMethodParameter(Decl->getObjCDeclQualifier(), |
| 4146 | Decl->getResultType(), S, Extended); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4147 | // Compute size of all parameters. |
| 4148 | // Start with computing size of a pointer in number of bytes. |
| 4149 | // FIXME: There might(should) be a better way of doing this computation! |
| 4150 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4151 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4152 | // The first two arguments (self and _cmd) are pointers; account for |
| 4153 | // their size. |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4154 | CharUnits ParmOffset = 2 * PtrSize; |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4155 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 4156 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 4157 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4158 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4159 | if (sz.isZero()) |
| 4160 | return true; |
| 4161 | |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4162 | assert (sz.isPositive() && |
| 4163 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4164 | ParmOffset += sz; |
| 4165 | } |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4166 | S += charUnitsToString(ParmOffset); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4167 | S += "@0:"; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4168 | S += charUnitsToString(PtrSize); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4169 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4170 | // Argument types. |
| 4171 | ParmOffset = 2 * PtrSize; |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4172 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 4173 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4174 | const ParmVarDecl *PVDecl = *PI; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4175 | QualType PType = PVDecl->getOriginalType(); |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 4176 | if (const ArrayType *AT = |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 4177 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4178 | // Use array's original type only if it has known number of |
| 4179 | // elements. |
Steve Naroff | bb3fde3 | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 4180 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 4181 | PType = PVDecl->getType(); |
| 4182 | } else if (PType->isFunctionType()) |
| 4183 | PType = PVDecl->getType(); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4184 | getObjCEncodingForMethodParameter(PVDecl->getObjCDeclQualifier(), |
| 4185 | PType, S, Extended); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4186 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4187 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4188 | } |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4189 | |
| 4190 | return false; |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4191 | } |
| 4192 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4193 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4194 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4195 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 4196 | /// NULL when getting encodings for protocol properties. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4197 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 4198 | /// attributes readonly and bycopy are encoded as single characters. The |
| 4199 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 4200 | /// encoded as single characters, followed by an identifier. Property types |
| 4201 | /// are also encoded as a parametrized attribute. The characters used to encode |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4202 | /// these attributes are defined by the following enumeration: |
| 4203 | /// @code |
| 4204 | /// enum PropertyAttributes { |
| 4205 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 4206 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 4207 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 4208 | /// kPropertyDynamic = 'D', // property is dynamic |
| 4209 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 4210 | /// kPropertySetter = 'S', // followed by setter selector name |
| 4211 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
Bob Wilson | 0d4cb85 | 2012-03-22 17:48:02 +0000 | [diff] [blame] | 4212 | /// kPropertyType = 'T' // followed by old-style type encoding. |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4213 | /// kPropertyWeak = 'W' // 'weak' property |
| 4214 | /// kPropertyStrong = 'P' // property GC'able |
| 4215 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 4216 | /// }; |
| 4217 | /// @endcode |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4218 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4219 | const Decl *Container, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4220 | std::string& S) const { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4221 | // Collect information from the property implementation decl(s). |
| 4222 | bool Dynamic = false; |
| 4223 | ObjCPropertyImplDecl *SynthesizePID = 0; |
| 4224 | |
| 4225 | // FIXME: Duplicated code due to poor abstraction. |
| 4226 | if (Container) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4227 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4228 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
| 4229 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4230 | i = CID->propimpl_begin(), e = CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4231 | i != e; ++i) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 4232 | ObjCPropertyImplDecl *PID = &*i; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4233 | if (PID->getPropertyDecl() == PD) { |
| 4234 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 4235 | Dynamic = true; |
| 4236 | } else { |
| 4237 | SynthesizePID = PID; |
| 4238 | } |
| 4239 | } |
| 4240 | } |
| 4241 | } else { |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4242 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4243 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4244 | i = OID->propimpl_begin(), e = OID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4245 | i != e; ++i) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 4246 | ObjCPropertyImplDecl *PID = &*i; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4247 | if (PID->getPropertyDecl() == PD) { |
| 4248 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 4249 | Dynamic = true; |
| 4250 | } else { |
| 4251 | SynthesizePID = PID; |
| 4252 | } |
| 4253 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4254 | } |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4255 | } |
| 4256 | } |
| 4257 | |
| 4258 | // FIXME: This is not very efficient. |
| 4259 | S = "T"; |
| 4260 | |
| 4261 | // Encode result type. |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4262 | // GCC has some special rules regarding encoding of properties which |
| 4263 | // closely resembles encoding of ivars. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4264 | getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4265 | true /* outermost type */, |
| 4266 | true /* encoding for property */); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4267 | |
| 4268 | if (PD->isReadOnly()) { |
| 4269 | S += ",R"; |
| 4270 | } else { |
| 4271 | switch (PD->getSetterKind()) { |
| 4272 | case ObjCPropertyDecl::Assign: break; |
| 4273 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4274 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
Fariborz Jahanian | 3a02b44 | 2011-08-12 20:47:08 +0000 | [diff] [blame] | 4275 | case ObjCPropertyDecl::Weak: S += ",W"; break; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4276 | } |
| 4277 | } |
| 4278 | |
| 4279 | // It really isn't clear at all what this means, since properties |
| 4280 | // are "dynamic by default". |
| 4281 | if (Dynamic) |
| 4282 | S += ",D"; |
| 4283 | |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4284 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 4285 | S += ",N"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4286 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4287 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 4288 | S += ",G"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4289 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4290 | } |
| 4291 | |
| 4292 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 4293 | S += ",S"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4294 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4295 | } |
| 4296 | |
| 4297 | if (SynthesizePID) { |
| 4298 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 4299 | S += ",V"; |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 4300 | S += OID->getNameAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4301 | } |
| 4302 | |
| 4303 | // FIXME: OBJCGC: weak & strong |
| 4304 | } |
| 4305 | |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4306 | /// getLegacyIntegralTypeEncoding - |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4307 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
| 4308 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4309 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
| 4310 | /// |
| 4311 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 4312 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4313 | if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) { |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4314 | if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4315 | PointeeTy = UnsignedIntTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4316 | else |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4317 | if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4318 | PointeeTy = IntTy; |
| 4319 | } |
| 4320 | } |
| 4321 | } |
| 4322 | |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4323 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4324 | const FieldDecl *Field) const { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4325 | // We follow the behavior of gcc, expanding structures which are |
| 4326 | // directly pointed to, and expanding embedded structures. Note that |
| 4327 | // these rules are sufficient to prevent recursive encoding of the |
| 4328 | // same type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4329 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
Fariborz Jahanian | 5b8c7d9 | 2008-12-22 23:22:27 +0000 | [diff] [blame] | 4330 | true /* outermost type */); |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4331 | } |
| 4332 | |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4333 | static char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) { |
| 4334 | switch (T->getAs<BuiltinType>()->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4335 | default: llvm_unreachable("Unhandled builtin type kind"); |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4336 | case BuiltinType::Void: return 'v'; |
| 4337 | case BuiltinType::Bool: return 'B'; |
| 4338 | case BuiltinType::Char_U: |
| 4339 | case BuiltinType::UChar: return 'C'; |
| 4340 | case BuiltinType::UShort: return 'S'; |
| 4341 | case BuiltinType::UInt: return 'I'; |
| 4342 | case BuiltinType::ULong: |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4343 | return C->getIntWidth(T) == 32 ? 'L' : 'Q'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4344 | case BuiltinType::UInt128: return 'T'; |
| 4345 | case BuiltinType::ULongLong: return 'Q'; |
| 4346 | case BuiltinType::Char_S: |
| 4347 | case BuiltinType::SChar: return 'c'; |
| 4348 | case BuiltinType::Short: return 's'; |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 4349 | case BuiltinType::WChar_S: |
| 4350 | case BuiltinType::WChar_U: |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4351 | case BuiltinType::Int: return 'i'; |
| 4352 | case BuiltinType::Long: |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4353 | return C->getIntWidth(T) == 32 ? 'l' : 'q'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4354 | case BuiltinType::LongLong: return 'q'; |
| 4355 | case BuiltinType::Int128: return 't'; |
| 4356 | case BuiltinType::Float: return 'f'; |
| 4357 | case BuiltinType::Double: return 'd'; |
Daniel Dunbar | 3a0be84 | 2010-10-11 21:13:48 +0000 | [diff] [blame] | 4358 | case BuiltinType::LongDouble: return 'D'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4359 | } |
| 4360 | } |
| 4361 | |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4362 | static char ObjCEncodingForEnumType(const ASTContext *C, const EnumType *ET) { |
| 4363 | EnumDecl *Enum = ET->getDecl(); |
| 4364 | |
| 4365 | // The encoding of an non-fixed enum type is always 'i', regardless of size. |
| 4366 | if (!Enum->isFixed()) |
| 4367 | return 'i'; |
| 4368 | |
| 4369 | // The encoding of a fixed enum type matches its fixed underlying type. |
| 4370 | return ObjCEncodingForPrimitiveKind(C, Enum->getIntegerType()); |
| 4371 | } |
| 4372 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4373 | static void EncodeBitField(const ASTContext *Ctx, std::string& S, |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4374 | QualType T, const FieldDecl *FD) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4375 | assert(FD->isBitField() && "not a bitfield - getObjCEncodingForTypeImpl"); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4376 | S += 'b'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4377 | // The NeXT runtime encodes bit fields as b followed by the number of bits. |
| 4378 | // The GNU runtime requires more information; bitfields are encoded as b, |
| 4379 | // then the offset (in bits) of the first element, then the type of the |
| 4380 | // bitfield, then the size in bits. For example, in this structure: |
| 4381 | // |
| 4382 | // struct |
| 4383 | // { |
| 4384 | // int integer; |
| 4385 | // int flags:2; |
| 4386 | // }; |
| 4387 | // On a 32-bit system, the encoding for flags would be b2 for the NeXT |
| 4388 | // runtime, but b32i2 for the GNU runtime. The reason for this extra |
| 4389 | // information is not especially sensible, but we're stuck with it for |
| 4390 | // compatibility with GCC, although providing it breaks anything that |
| 4391 | // actually uses runtime introspection and wants to work on both runtimes... |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4392 | if (!Ctx->getLangOpts().NeXTRuntime) { |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4393 | const RecordDecl *RD = FD->getParent(); |
| 4394 | const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD); |
Eli Friedman | 8290574 | 2011-07-07 01:54:01 +0000 | [diff] [blame] | 4395 | S += llvm::utostr(RL.getFieldOffset(FD->getFieldIndex())); |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4396 | if (const EnumType *ET = T->getAs<EnumType>()) |
| 4397 | S += ObjCEncodingForEnumType(Ctx, ET); |
David Chisnall | c7ff82c | 2010-12-26 20:12:30 +0000 | [diff] [blame] | 4398 | else |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4399 | S += ObjCEncodingForPrimitiveKind(Ctx, T); |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4400 | } |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4401 | S += llvm::utostr(FD->getBitWidthValue(*Ctx)); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4402 | } |
| 4403 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 4404 | // FIXME: Use SmallString for accumulating string. |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4405 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 4406 | bool ExpandPointedToStructures, |
| 4407 | bool ExpandStructures, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 4408 | const FieldDecl *FD, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4409 | bool OutermostType, |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4410 | bool EncodingProperty, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4411 | bool StructField, |
| 4412 | bool EncodeBlockParameters, |
| 4413 | bool EncodeClassNames) const { |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4414 | if (T->getAs<BuiltinType>()) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4415 | if (FD && FD->isBitField()) |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4416 | return EncodeBitField(this, S, T, FD); |
| 4417 | S += ObjCEncodingForPrimitiveKind(this, T); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4418 | return; |
| 4419 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4420 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4421 | if (const ComplexType *CT = T->getAs<ComplexType>()) { |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 4422 | S += 'j'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4423 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false, |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 4424 | false); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4425 | return; |
| 4426 | } |
Fariborz Jahanian | 60bce3e | 2009-11-23 20:40:50 +0000 | [diff] [blame] | 4427 | |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4428 | // encoding for pointer or r3eference types. |
| 4429 | QualType PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4430 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Fariborz Jahanian | 8d2c0a9 | 2009-11-30 18:43:52 +0000 | [diff] [blame] | 4431 | if (PT->isObjCSelType()) { |
| 4432 | S += ':'; |
| 4433 | return; |
| 4434 | } |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4435 | PointeeTy = PT->getPointeeType(); |
| 4436 | } |
| 4437 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 4438 | PointeeTy = RT->getPointeeType(); |
| 4439 | if (!PointeeTy.isNull()) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4440 | bool isReadOnly = false; |
| 4441 | // For historical/compatibility reasons, the read-only qualifier of the |
| 4442 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 4443 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4444 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 4445 | if (isa<TypedefType>(T.getTypePtr())) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4446 | if (OutermostType && T.isConstQualified()) { |
| 4447 | isReadOnly = true; |
| 4448 | S += 'r'; |
| 4449 | } |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 4450 | } else if (OutermostType) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4451 | QualType P = PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4452 | while (P->getAs<PointerType>()) |
| 4453 | P = P->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4454 | if (P.isConstQualified()) { |
| 4455 | isReadOnly = true; |
| 4456 | S += 'r'; |
| 4457 | } |
| 4458 | } |
| 4459 | if (isReadOnly) { |
| 4460 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 4461 | // combinations need to be rearranged. |
| 4462 | // Rewrite "in const" from "nr" to "rn" |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4463 | if (StringRef(S).endswith("nr")) |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 4464 | S.replace(S.end()-2, S.end(), "rn"); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4465 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4466 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4467 | if (PointeeTy->isCharType()) { |
| 4468 | // char pointer types should be encoded as '*' unless it is a |
| 4469 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 4470 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4471 | S += '*'; |
| 4472 | return; |
| 4473 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4474 | } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) { |
Steve Naroff | 9533a7f | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 4475 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 4476 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 4477 | S += '#'; |
| 4478 | return; |
| 4479 | } |
| 4480 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 4481 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 4482 | S += '@'; |
| 4483 | return; |
| 4484 | } |
| 4485 | // fall through... |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4486 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4487 | S += '^'; |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4488 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 4489 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4490 | getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4491 | NULL); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4492 | return; |
| 4493 | } |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4494 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4495 | if (const ArrayType *AT = |
| 4496 | // Ignore type qualifiers etc. |
| 4497 | dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) { |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4498 | if (isa<IncompleteArrayType>(AT) && !StructField) { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4499 | // Incomplete arrays are encoded as a pointer to the array element. |
| 4500 | S += '^'; |
| 4501 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4502 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4503 | false, ExpandStructures, FD); |
| 4504 | } else { |
| 4505 | S += '['; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4506 | |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4507 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
| 4508 | if (getTypeSize(CAT->getElementType()) == 0) |
| 4509 | S += '0'; |
| 4510 | else |
| 4511 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 4512 | } else { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4513 | //Variable length arrays are encoded as a regular array with 0 elements. |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4514 | assert((isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) && |
| 4515 | "Unknown array type!"); |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4516 | S += '0'; |
| 4517 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4518 | |
| 4519 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4520 | false, ExpandStructures, FD); |
| 4521 | S += ']'; |
| 4522 | } |
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 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4526 | if (T->getAs<FunctionType>()) { |
Anders Carlsson | c0a87b7 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 4527 | S += '?'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4528 | return; |
| 4529 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4530 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4531 | if (const RecordType *RTy = T->getAs<RecordType>()) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4532 | RecordDecl *RDecl = RTy->getDecl(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4533 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 4534 | // Anonymous structures print as '?' |
| 4535 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 4536 | S += II->getName(); |
Fariborz Jahanian | 6fb9439 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 4537 | if (ClassTemplateSpecializationDecl *Spec |
| 4538 | = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) { |
| 4539 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 4540 | std::string TemplateArgsStr |
| 4541 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4542 | TemplateArgs.data(), |
| 4543 | TemplateArgs.size(), |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 4544 | (*this).getPrintingPolicy()); |
Fariborz Jahanian | 6fb9439 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 4545 | |
| 4546 | S += TemplateArgsStr; |
| 4547 | } |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 4548 | } else { |
| 4549 | S += '?'; |
| 4550 | } |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 4551 | if (ExpandStructures) { |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4552 | S += '='; |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4553 | if (!RDecl->isUnion()) { |
| 4554 | getObjCEncodingForStructureImpl(RDecl, S, FD); |
| 4555 | } else { |
| 4556 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 4557 | FieldEnd = RDecl->field_end(); |
| 4558 | Field != FieldEnd; ++Field) { |
| 4559 | if (FD) { |
| 4560 | S += '"'; |
| 4561 | S += Field->getNameAsString(); |
| 4562 | S += '"'; |
| 4563 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4564 | |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4565 | // Special case bit-fields. |
| 4566 | if (Field->isBitField()) { |
| 4567 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 4568 | &*Field); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4569 | } else { |
| 4570 | QualType qt = Field->getType(); |
| 4571 | getLegacyIntegralTypeEncoding(qt); |
| 4572 | getObjCEncodingForTypeImpl(qt, S, false, true, |
| 4573 | FD, /*OutermostType*/false, |
| 4574 | /*EncodingProperty*/false, |
| 4575 | /*StructField*/true); |
| 4576 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4577 | } |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4578 | } |
Fariborz Jahanian | 6de88a8 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 4579 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4580 | S += RDecl->isUnion() ? ')' : '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4581 | return; |
| 4582 | } |
Fariborz Jahanian | e6012c7 | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 4583 | |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4584 | if (const EnumType *ET = T->getAs<EnumType>()) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4585 | if (FD && FD->isBitField()) |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4586 | EncodeBitField(this, S, T, FD); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4587 | else |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4588 | S += ObjCEncodingForEnumType(this, ET); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4589 | return; |
| 4590 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4591 | |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4592 | if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) { |
Steve Naroff | 21a98b1 | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 4593 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4594 | if (EncodeBlockParameters) { |
| 4595 | const FunctionType *FT = BT->getPointeeType()->getAs<FunctionType>(); |
| 4596 | |
| 4597 | S += '<'; |
| 4598 | // Block return type |
| 4599 | getObjCEncodingForTypeImpl(FT->getResultType(), S, |
| 4600 | ExpandPointedToStructures, ExpandStructures, |
| 4601 | FD, |
| 4602 | false /* OutermostType */, |
| 4603 | EncodingProperty, |
| 4604 | false /* StructField */, |
| 4605 | EncodeBlockParameters, |
| 4606 | EncodeClassNames); |
| 4607 | // Block self |
| 4608 | S += "@?"; |
| 4609 | // Block parameters |
| 4610 | if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) { |
| 4611 | for (FunctionProtoType::arg_type_iterator I = FPT->arg_type_begin(), |
| 4612 | E = FPT->arg_type_end(); I && (I != E); ++I) { |
| 4613 | getObjCEncodingForTypeImpl(*I, S, |
| 4614 | ExpandPointedToStructures, |
| 4615 | ExpandStructures, |
| 4616 | FD, |
| 4617 | false /* OutermostType */, |
| 4618 | EncodingProperty, |
| 4619 | false /* StructField */, |
| 4620 | EncodeBlockParameters, |
| 4621 | EncodeClassNames); |
| 4622 | } |
| 4623 | } |
| 4624 | S += '>'; |
| 4625 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4626 | return; |
| 4627 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4628 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4629 | // Ignore protocol qualifiers when mangling at this level. |
| 4630 | if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>()) |
| 4631 | T = OT->getBaseType(); |
| 4632 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4633 | if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4634 | // @encode(class_name) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4635 | ObjCInterfaceDecl *OI = OIT->getDecl(); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4636 | S += '{'; |
| 4637 | const IdentifierInfo *II = OI->getIdentifier(); |
| 4638 | S += II->getName(); |
| 4639 | S += '='; |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4640 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4641 | DeepCollectObjCIvars(OI, true, Ivars); |
| 4642 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) { |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4643 | const FieldDecl *Field = cast<FieldDecl>(Ivars[i]); |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4644 | if (Field->isBitField()) |
| 4645 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4646 | else |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4647 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4648 | } |
| 4649 | S += '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4650 | return; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4651 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4652 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4653 | if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4654 | if (OPT->isObjCIdType()) { |
| 4655 | S += '@'; |
| 4656 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4657 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4658 | |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4659 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
| 4660 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
| 4661 | // Since this is a binary compatibility issue, need to consult with runtime |
| 4662 | // folks. Fortunately, this is a *very* obsure construct. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4663 | S += '#'; |
| 4664 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4665 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4666 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4667 | if (OPT->isObjCQualifiedIdType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4668 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4669 | ExpandPointedToStructures, |
| 4670 | ExpandStructures, FD); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4671 | if (FD || EncodingProperty || EncodeClassNames) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4672 | // Note that we do extended encoding of protocol qualifer list |
| 4673 | // Only when doing ivar or property encoding. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4674 | S += '"'; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4675 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 4676 | E = OPT->qual_end(); I != E; ++I) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4677 | S += '<'; |
| 4678 | S += (*I)->getNameAsString(); |
| 4679 | S += '>'; |
| 4680 | } |
| 4681 | S += '"'; |
| 4682 | } |
| 4683 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4684 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4685 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4686 | QualType PointeeTy = OPT->getPointeeType(); |
| 4687 | if (!EncodingProperty && |
| 4688 | isa<TypedefType>(PointeeTy.getTypePtr())) { |
| 4689 | // Another historical/compatibility reason. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4690 | // We encode the underlying type which comes out as |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4691 | // {...}; |
| 4692 | S += '^'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4693 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 4694 | false, ExpandPointedToStructures, |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4695 | NULL); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4696 | return; |
| 4697 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4698 | |
| 4699 | S += '@'; |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4700 | if (OPT->getInterfaceDecl() && |
| 4701 | (FD || EncodingProperty || EncodeClassNames)) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4702 | S += '"'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4703 | S += OPT->getInterfaceDecl()->getIdentifier()->getName(); |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4704 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 4705 | E = OPT->qual_end(); I != E; ++I) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4706 | S += '<'; |
| 4707 | S += (*I)->getNameAsString(); |
| 4708 | S += '>'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4709 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4710 | S += '"'; |
| 4711 | } |
| 4712 | return; |
| 4713 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4714 | |
John McCall | 532ec7b | 2010-05-17 23:56:34 +0000 | [diff] [blame] | 4715 | // gcc just blithely ignores member pointers. |
| 4716 | // TODO: maybe there should be a mangling for these |
| 4717 | if (T->getAs<MemberPointerType>()) |
| 4718 | return; |
Fariborz Jahanian | e6012c7 | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 4719 | |
| 4720 | if (T->isVectorType()) { |
| 4721 | // This matches gcc's encoding, even though technically it is |
| 4722 | // insufficient. |
| 4723 | // FIXME. We should do a better job than gcc. |
| 4724 | return; |
| 4725 | } |
| 4726 | |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4727 | llvm_unreachable("@encode for type not implemented!"); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4728 | } |
| 4729 | |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4730 | void ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl, |
| 4731 | std::string &S, |
| 4732 | const FieldDecl *FD, |
| 4733 | bool includeVBases) const { |
| 4734 | assert(RDecl && "Expected non-null RecordDecl"); |
| 4735 | assert(!RDecl->isUnion() && "Should not be called for unions"); |
| 4736 | if (!RDecl->getDefinition()) |
| 4737 | return; |
| 4738 | |
| 4739 | CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(RDecl); |
| 4740 | std::multimap<uint64_t, NamedDecl *> FieldOrBaseOffsets; |
| 4741 | const ASTRecordLayout &layout = getASTRecordLayout(RDecl); |
| 4742 | |
| 4743 | if (CXXRec) { |
| 4744 | for (CXXRecordDecl::base_class_iterator |
| 4745 | BI = CXXRec->bases_begin(), |
| 4746 | BE = CXXRec->bases_end(); BI != BE; ++BI) { |
| 4747 | if (!BI->isVirtual()) { |
| 4748 | CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 829f200 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 4749 | if (base->isEmpty()) |
| 4750 | continue; |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4751 | uint64_t offs = layout.getBaseClassOffsetInBits(base); |
| 4752 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 4753 | std::make_pair(offs, base)); |
| 4754 | } |
| 4755 | } |
| 4756 | } |
| 4757 | |
| 4758 | unsigned i = 0; |
| 4759 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 4760 | FieldEnd = RDecl->field_end(); |
| 4761 | Field != FieldEnd; ++Field, ++i) { |
| 4762 | uint64_t offs = layout.getFieldOffset(i); |
| 4763 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 4764 | std::make_pair(offs, &*Field)); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4765 | } |
| 4766 | |
| 4767 | if (CXXRec && includeVBases) { |
| 4768 | for (CXXRecordDecl::base_class_iterator |
| 4769 | BI = CXXRec->vbases_begin(), |
| 4770 | BE = CXXRec->vbases_end(); BI != BE; ++BI) { |
| 4771 | CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 829f200 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 4772 | if (base->isEmpty()) |
| 4773 | continue; |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4774 | uint64_t offs = layout.getVBaseClassOffsetInBits(base); |
Argyrios Kyrtzidis | 19aa860 | 2011-09-26 18:14:24 +0000 | [diff] [blame] | 4775 | if (FieldOrBaseOffsets.find(offs) == FieldOrBaseOffsets.end()) |
| 4776 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.end(), |
| 4777 | std::make_pair(offs, base)); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4778 | } |
| 4779 | } |
| 4780 | |
| 4781 | CharUnits size; |
| 4782 | if (CXXRec) { |
| 4783 | size = includeVBases ? layout.getSize() : layout.getNonVirtualSize(); |
| 4784 | } else { |
| 4785 | size = layout.getSize(); |
| 4786 | } |
| 4787 | |
| 4788 | uint64_t CurOffs = 0; |
| 4789 | std::multimap<uint64_t, NamedDecl *>::iterator |
| 4790 | CurLayObj = FieldOrBaseOffsets.begin(); |
| 4791 | |
Douglas Gregor | 58db7a5 | 2012-04-27 22:30:01 +0000 | [diff] [blame] | 4792 | if (CXXRec && CXXRec->isDynamicClass() && |
| 4793 | (CurLayObj == FieldOrBaseOffsets.end() || CurLayObj->first != 0)) { |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4794 | if (FD) { |
| 4795 | S += "\"_vptr$"; |
| 4796 | std::string recname = CXXRec->getNameAsString(); |
| 4797 | if (recname.empty()) recname = "?"; |
| 4798 | S += recname; |
| 4799 | S += '"'; |
| 4800 | } |
| 4801 | S += "^^?"; |
| 4802 | CurOffs += getTypeSize(VoidPtrTy); |
| 4803 | } |
| 4804 | |
| 4805 | if (!RDecl->hasFlexibleArrayMember()) { |
| 4806 | // Mark the end of the structure. |
| 4807 | uint64_t offs = toBits(size); |
| 4808 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 4809 | std::make_pair(offs, (NamedDecl*)0)); |
| 4810 | } |
| 4811 | |
| 4812 | for (; CurLayObj != FieldOrBaseOffsets.end(); ++CurLayObj) { |
| 4813 | assert(CurOffs <= CurLayObj->first); |
| 4814 | |
| 4815 | if (CurOffs < CurLayObj->first) { |
| 4816 | uint64_t padding = CurLayObj->first - CurOffs; |
| 4817 | // FIXME: There doesn't seem to be a way to indicate in the encoding that |
| 4818 | // packing/alignment of members is different that normal, in which case |
| 4819 | // the encoding will be out-of-sync with the real layout. |
| 4820 | // If the runtime switches to just consider the size of types without |
| 4821 | // taking into account alignment, we could make padding explicit in the |
| 4822 | // encoding (e.g. using arrays of chars). The encoding strings would be |
| 4823 | // longer then though. |
| 4824 | CurOffs += padding; |
| 4825 | } |
| 4826 | |
| 4827 | NamedDecl *dcl = CurLayObj->second; |
| 4828 | if (dcl == 0) |
| 4829 | break; // reached end of structure. |
| 4830 | |
| 4831 | if (CXXRecordDecl *base = dyn_cast<CXXRecordDecl>(dcl)) { |
| 4832 | // We expand the bases without their virtual bases since those are going |
| 4833 | // in the initial structure. Note that this differs from gcc which |
| 4834 | // expands virtual bases each time one is encountered in the hierarchy, |
| 4835 | // making the encoding type bigger than it really is. |
| 4836 | getObjCEncodingForStructureImpl(base, S, FD, /*includeVBases*/false); |
Argyrios Kyrtzidis | 829f200 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 4837 | assert(!base->isEmpty()); |
| 4838 | CurOffs += toBits(getASTRecordLayout(base).getNonVirtualSize()); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4839 | } else { |
| 4840 | FieldDecl *field = cast<FieldDecl>(dcl); |
| 4841 | if (FD) { |
| 4842 | S += '"'; |
| 4843 | S += field->getNameAsString(); |
| 4844 | S += '"'; |
| 4845 | } |
| 4846 | |
| 4847 | if (field->isBitField()) { |
| 4848 | EncodeBitField(this, S, field->getType(), field); |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4849 | CurOffs += field->getBitWidthValue(*this); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4850 | } else { |
| 4851 | QualType qt = field->getType(); |
| 4852 | getLegacyIntegralTypeEncoding(qt); |
| 4853 | getObjCEncodingForTypeImpl(qt, S, false, true, FD, |
| 4854 | /*OutermostType*/false, |
| 4855 | /*EncodingProperty*/false, |
| 4856 | /*StructField*/true); |
| 4857 | CurOffs += getTypeSize(field->getType()); |
| 4858 | } |
| 4859 | } |
| 4860 | } |
| 4861 | } |
| 4862 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4863 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 4864 | std::string& S) const { |
| 4865 | if (QT & Decl::OBJC_TQ_In) |
| 4866 | S += 'n'; |
| 4867 | if (QT & Decl::OBJC_TQ_Inout) |
| 4868 | S += 'N'; |
| 4869 | if (QT & Decl::OBJC_TQ_Out) |
| 4870 | S += 'o'; |
| 4871 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 4872 | S += 'O'; |
| 4873 | if (QT & Decl::OBJC_TQ_Byref) |
| 4874 | S += 'R'; |
| 4875 | if (QT & Decl::OBJC_TQ_Oneway) |
| 4876 | S += 'V'; |
| 4877 | } |
| 4878 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4879 | void ASTContext::setBuiltinVaListType(QualType T) { |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 4880 | assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4881 | |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 4882 | BuiltinVaListType = T; |
| 4883 | } |
| 4884 | |
Douglas Gregor | 4dfd02a | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 4885 | TypedefDecl *ASTContext::getObjCIdDecl() const { |
| 4886 | if (!ObjCIdDecl) { |
| 4887 | QualType T = getObjCObjectType(ObjCBuiltinIdTy, 0, 0); |
| 4888 | T = getObjCObjectPointerType(T); |
| 4889 | TypeSourceInfo *IdInfo = getTrivialTypeSourceInfo(T); |
| 4890 | ObjCIdDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 4891 | getTranslationUnitDecl(), |
| 4892 | SourceLocation(), SourceLocation(), |
| 4893 | &Idents.get("id"), IdInfo); |
| 4894 | } |
| 4895 | |
| 4896 | return ObjCIdDecl; |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 4897 | } |
| 4898 | |
Douglas Gregor | 7a27ea5 | 2011-08-12 06:17:30 +0000 | [diff] [blame] | 4899 | TypedefDecl *ASTContext::getObjCSelDecl() const { |
| 4900 | if (!ObjCSelDecl) { |
| 4901 | QualType SelT = getPointerType(ObjCBuiltinSelTy); |
| 4902 | TypeSourceInfo *SelInfo = getTrivialTypeSourceInfo(SelT); |
| 4903 | ObjCSelDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 4904 | getTranslationUnitDecl(), |
| 4905 | SourceLocation(), SourceLocation(), |
| 4906 | &Idents.get("SEL"), SelInfo); |
| 4907 | } |
| 4908 | return ObjCSelDecl; |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 4909 | } |
| 4910 | |
Douglas Gregor | 79d6726 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 4911 | TypedefDecl *ASTContext::getObjCClassDecl() const { |
| 4912 | if (!ObjCClassDecl) { |
| 4913 | QualType T = getObjCObjectType(ObjCBuiltinClassTy, 0, 0); |
| 4914 | T = getObjCObjectPointerType(T); |
| 4915 | TypeSourceInfo *ClassInfo = getTrivialTypeSourceInfo(T); |
| 4916 | ObjCClassDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 4917 | getTranslationUnitDecl(), |
| 4918 | SourceLocation(), SourceLocation(), |
| 4919 | &Idents.get("Class"), ClassInfo); |
| 4920 | } |
| 4921 | |
| 4922 | return ObjCClassDecl; |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 4923 | } |
| 4924 | |
Douglas Gregor | a6ea10e | 2012-01-17 18:09:05 +0000 | [diff] [blame] | 4925 | ObjCInterfaceDecl *ASTContext::getObjCProtocolDecl() const { |
| 4926 | if (!ObjCProtocolClassDecl) { |
| 4927 | ObjCProtocolClassDecl |
| 4928 | = ObjCInterfaceDecl::Create(*this, getTranslationUnitDecl(), |
| 4929 | SourceLocation(), |
| 4930 | &Idents.get("Protocol"), |
| 4931 | /*PrevDecl=*/0, |
| 4932 | SourceLocation(), true); |
| 4933 | } |
| 4934 | |
| 4935 | return ObjCProtocolClassDecl; |
| 4936 | } |
| 4937 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4938 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4939 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 4940 | "'NSConstantString' type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4941 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4942 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 4943 | } |
| 4944 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4945 | /// \brief Retrieve the template name that corresponds to a non-empty |
| 4946 | /// lookup. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4947 | TemplateName |
| 4948 | ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin, |
| 4949 | UnresolvedSetIterator End) const { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4950 | unsigned size = End - Begin; |
| 4951 | assert(size > 1 && "set is not overloaded!"); |
| 4952 | |
| 4953 | void *memory = Allocate(sizeof(OverloadedTemplateStorage) + |
| 4954 | size * sizeof(FunctionTemplateDecl*)); |
| 4955 | OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size); |
| 4956 | |
| 4957 | NamedDecl **Storage = OT->getStorage(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4958 | for (UnresolvedSetIterator I = Begin; I != End; ++I) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4959 | NamedDecl *D = *I; |
| 4960 | assert(isa<FunctionTemplateDecl>(D) || |
| 4961 | (isa<UsingShadowDecl>(D) && |
| 4962 | isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))); |
| 4963 | *Storage++ = D; |
| 4964 | } |
| 4965 | |
| 4966 | return TemplateName(OT); |
| 4967 | } |
| 4968 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4969 | /// \brief Retrieve the template name that represents a qualified |
| 4970 | /// template name such as \c std::vector. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4971 | TemplateName |
| 4972 | ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
| 4973 | bool TemplateKeyword, |
| 4974 | TemplateDecl *Template) const { |
Douglas Gregor | 0f0ea2a | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 4975 | assert(NNS && "Missing nested-name-specifier in qualified template name"); |
| 4976 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4977 | // FIXME: Canonicalization? |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4978 | llvm::FoldingSetNodeID ID; |
| 4979 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 4980 | |
| 4981 | void *InsertPos = 0; |
| 4982 | QualifiedTemplateName *QTN = |
| 4983 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4984 | if (!QTN) { |
| 4985 | QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); |
| 4986 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 4987 | } |
| 4988 | |
| 4989 | return TemplateName(QTN); |
| 4990 | } |
| 4991 | |
| 4992 | /// \brief Retrieve the template name that represents a dependent |
| 4993 | /// template name such as \c MetaFun::template apply. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4994 | TemplateName |
| 4995 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
| 4996 | const IdentifierInfo *Name) const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4997 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 4998 | "Nested name specifier must be dependent"); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4999 | |
| 5000 | llvm::FoldingSetNodeID ID; |
| 5001 | DependentTemplateName::Profile(ID, NNS, Name); |
| 5002 | |
| 5003 | void *InsertPos = 0; |
| 5004 | DependentTemplateName *QTN = |
| 5005 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 5006 | |
| 5007 | if (QTN) |
| 5008 | return TemplateName(QTN); |
| 5009 | |
| 5010 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 5011 | if (CanonNNS == NNS) { |
| 5012 | QTN = new (*this,4) DependentTemplateName(NNS, Name); |
| 5013 | } else { |
| 5014 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
| 5015 | QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 5016 | DependentTemplateName *CheckQTN = |
| 5017 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 5018 | assert(!CheckQTN && "Dependent type name canonicalization broken"); |
| 5019 | (void)CheckQTN; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 5020 | } |
| 5021 | |
| 5022 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 5023 | return TemplateName(QTN); |
| 5024 | } |
| 5025 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 5026 | /// \brief Retrieve the template name that represents a dependent |
| 5027 | /// template name such as \c MetaFun::template operator+. |
| 5028 | TemplateName |
| 5029 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5030 | OverloadedOperatorKind Operator) const { |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 5031 | assert((!NNS || NNS->isDependent()) && |
| 5032 | "Nested name specifier must be dependent"); |
| 5033 | |
| 5034 | llvm::FoldingSetNodeID ID; |
| 5035 | DependentTemplateName::Profile(ID, NNS, Operator); |
| 5036 | |
| 5037 | void *InsertPos = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 5038 | DependentTemplateName *QTN |
| 5039 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 5040 | |
| 5041 | if (QTN) |
| 5042 | return TemplateName(QTN); |
| 5043 | |
| 5044 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 5045 | if (CanonNNS == NNS) { |
| 5046 | QTN = new (*this,4) DependentTemplateName(NNS, Operator); |
| 5047 | } else { |
| 5048 | TemplateName Canon = getDependentTemplateName(CanonNNS, Operator); |
| 5049 | QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 5050 | |
| 5051 | DependentTemplateName *CheckQTN |
| 5052 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 5053 | assert(!CheckQTN && "Dependent template name canonicalization broken"); |
| 5054 | (void)CheckQTN; |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 5055 | } |
| 5056 | |
| 5057 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 5058 | return TemplateName(QTN); |
| 5059 | } |
| 5060 | |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 5061 | TemplateName |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5062 | ASTContext::getSubstTemplateTemplateParm(TemplateTemplateParmDecl *param, |
| 5063 | TemplateName replacement) const { |
| 5064 | llvm::FoldingSetNodeID ID; |
| 5065 | SubstTemplateTemplateParmStorage::Profile(ID, param, replacement); |
| 5066 | |
| 5067 | void *insertPos = 0; |
| 5068 | SubstTemplateTemplateParmStorage *subst |
| 5069 | = SubstTemplateTemplateParms.FindNodeOrInsertPos(ID, insertPos); |
| 5070 | |
| 5071 | if (!subst) { |
| 5072 | subst = new (*this) SubstTemplateTemplateParmStorage(param, replacement); |
| 5073 | SubstTemplateTemplateParms.InsertNode(subst, insertPos); |
| 5074 | } |
| 5075 | |
| 5076 | return TemplateName(subst); |
| 5077 | } |
| 5078 | |
| 5079 | TemplateName |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 5080 | ASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param, |
| 5081 | const TemplateArgument &ArgPack) const { |
| 5082 | ASTContext &Self = const_cast<ASTContext &>(*this); |
| 5083 | llvm::FoldingSetNodeID ID; |
| 5084 | SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack); |
| 5085 | |
| 5086 | void *InsertPos = 0; |
| 5087 | SubstTemplateTemplateParmPackStorage *Subst |
| 5088 | = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos); |
| 5089 | |
| 5090 | if (!Subst) { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5091 | Subst = new (*this) SubstTemplateTemplateParmPackStorage(Param, |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 5092 | ArgPack.pack_size(), |
| 5093 | ArgPack.pack_begin()); |
| 5094 | SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos); |
| 5095 | } |
| 5096 | |
| 5097 | return TemplateName(Subst); |
| 5098 | } |
| 5099 | |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 5100 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | d934112 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 5101 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 5102 | /// is actually a value of type @c TargetInfo::IntType. |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 5103 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 5104 | switch (Type) { |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 5105 | case TargetInfo::NoInt: return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 5106 | case TargetInfo::SignedShort: return ShortTy; |
| 5107 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 5108 | case TargetInfo::SignedInt: return IntTy; |
| 5109 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 5110 | case TargetInfo::SignedLong: return LongTy; |
| 5111 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 5112 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 5113 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 5114 | } |
| 5115 | |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5116 | llvm_unreachable("Unhandled TargetInfo::IntType value"); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 5117 | } |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 5118 | |
| 5119 | //===----------------------------------------------------------------------===// |
| 5120 | // Type Predicates. |
| 5121 | //===----------------------------------------------------------------------===// |
| 5122 | |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 5123 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 5124 | /// garbage collection attribute. |
| 5125 | /// |
John McCall | ae278a3 | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 5126 | Qualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5127 | if (getLangOpts().getGC() == LangOptions::NonGC) |
John McCall | ae278a3 | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 5128 | return Qualifiers::GCNone; |
| 5129 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5130 | assert(getLangOpts().ObjC1); |
John McCall | ae278a3 | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 5131 | Qualifiers::GC GCAttrs = Ty.getObjCGCAttr(); |
| 5132 | |
| 5133 | // Default behaviour under objective-C's gc is for ObjC pointers |
| 5134 | // (or pointers to them) be treated as though they were declared |
| 5135 | // as __strong. |
| 5136 | if (GCAttrs == Qualifiers::GCNone) { |
| 5137 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
| 5138 | return Qualifiers::Strong; |
| 5139 | else if (Ty->isPointerType()) |
| 5140 | return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); |
| 5141 | } else { |
| 5142 | // It's not valid to set GC attributes on anything that isn't a |
| 5143 | // pointer. |
| 5144 | #ifndef NDEBUG |
| 5145 | QualType CT = Ty->getCanonicalTypeInternal(); |
| 5146 | while (const ArrayType *AT = dyn_cast<ArrayType>(CT)) |
| 5147 | CT = AT->getElementType(); |
| 5148 | assert(CT->isAnyPointerType() || CT->isBlockPointerType()); |
| 5149 | #endif |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 5150 | } |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 5151 | return GCAttrs; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 5152 | } |
| 5153 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5154 | //===----------------------------------------------------------------------===// |
| 5155 | // Type Compatibility Testing |
| 5156 | //===----------------------------------------------------------------------===// |
Chris Lattner | 770951b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 5157 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5158 | /// areCompatVectorTypes - Return true if the two specified vector types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5159 | /// compatible. |
| 5160 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 5161 | const VectorType *RHS) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 5162 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5163 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5164 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5165 | } |
| 5166 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5167 | bool ASTContext::areCompatibleVectorTypes(QualType FirstVec, |
| 5168 | QualType SecondVec) { |
| 5169 | assert(FirstVec->isVectorType() && "FirstVec should be a vector type"); |
| 5170 | assert(SecondVec->isVectorType() && "SecondVec should be a vector type"); |
| 5171 | |
| 5172 | if (hasSameUnqualifiedType(FirstVec, SecondVec)) |
| 5173 | return true; |
| 5174 | |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 5175 | // Treat Neon vector types and most AltiVec vector types as if they are the |
| 5176 | // equivalent GCC vector types. |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5177 | const VectorType *First = FirstVec->getAs<VectorType>(); |
| 5178 | const VectorType *Second = SecondVec->getAs<VectorType>(); |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 5179 | if (First->getNumElements() == Second->getNumElements() && |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5180 | hasSameType(First->getElementType(), Second->getElementType()) && |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 5181 | First->getVectorKind() != VectorType::AltiVecPixel && |
| 5182 | First->getVectorKind() != VectorType::AltiVecBool && |
| 5183 | Second->getVectorKind() != VectorType::AltiVecPixel && |
| 5184 | Second->getVectorKind() != VectorType::AltiVecBool) |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5185 | return true; |
| 5186 | |
| 5187 | return false; |
| 5188 | } |
| 5189 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5190 | //===----------------------------------------------------------------------===// |
| 5191 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 5192 | //===----------------------------------------------------------------------===// |
| 5193 | |
| 5194 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 5195 | /// inheritance hierarchy of 'rProto'. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5196 | bool |
| 5197 | ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 5198 | ObjCProtocolDecl *rProto) const { |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 5199 | if (declaresSameEntity(lProto, rProto)) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5200 | return true; |
| 5201 | for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), |
| 5202 | E = rProto->protocol_end(); PI != E; ++PI) |
| 5203 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 5204 | return true; |
| 5205 | return false; |
| 5206 | } |
| 5207 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5208 | /// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...> |
| 5209 | /// return true if lhs's protocols conform to rhs's protocol; false |
| 5210 | /// otherwise. |
| 5211 | bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) { |
| 5212 | if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType()) |
| 5213 | return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false); |
| 5214 | return false; |
| 5215 | } |
| 5216 | |
Fariborz Jahanian | a8f8dac | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 5217 | /// ObjCQualifiedClassTypesAreCompatible - compare Class<p,...> and |
| 5218 | /// Class<p1, ...>. |
| 5219 | bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs, |
| 5220 | QualType rhs) { |
| 5221 | const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>(); |
| 5222 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
| 5223 | assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible"); |
| 5224 | |
| 5225 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5226 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5227 | bool match = false; |
| 5228 | ObjCProtocolDecl *lhsProto = *I; |
| 5229 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 5230 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 5231 | ObjCProtocolDecl *rhsProto = *J; |
| 5232 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) { |
| 5233 | match = true; |
| 5234 | break; |
| 5235 | } |
| 5236 | } |
| 5237 | if (!match) |
| 5238 | return false; |
| 5239 | } |
| 5240 | return true; |
| 5241 | } |
| 5242 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5243 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 5244 | /// ObjCQualifiedIDType. |
| 5245 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 5246 | bool compare) { |
| 5247 | // Allow id<P..> and an 'id' or void* type in all cases. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5248 | if (lhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5249 | lhs->isObjCIdType() || lhs->isObjCClassType()) |
| 5250 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5251 | else if (rhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5252 | rhs->isObjCIdType() || rhs->isObjCClassType()) |
| 5253 | return true; |
| 5254 | |
| 5255 | if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5256 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
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) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5259 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5260 | if (rhsOPT->qual_empty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5261 | // If the RHS is a unqualified interface pointer "NSString*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5262 | // make sure we check the class hierarchy. |
| 5263 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 5264 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5265 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5266 | // when comparing an id<P> on lhs with a static type on rhs, |
| 5267 | // see if static class implements all of id's protocols, directly or |
| 5268 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 5269 | if (!rhsID->ClassImplementsProtocol(*I, true)) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5270 | return false; |
| 5271 | } |
| 5272 | } |
| 5273 | // If there are no qualifiers and no interface, we have an 'id'. |
| 5274 | return true; |
| 5275 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5276 | // Both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5277 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5278 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5279 | ObjCProtocolDecl *lhsProto = *I; |
| 5280 | bool match = false; |
| 5281 | |
| 5282 | // when comparing an id<P> on lhs with a static type on rhs, |
| 5283 | // see if static class implements all of id's protocols, directly or |
| 5284 | // through its super class and categories. |
| 5285 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 5286 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 5287 | ObjCProtocolDecl *rhsProto = *J; |
| 5288 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 5289 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 5290 | match = true; |
| 5291 | break; |
| 5292 | } |
| 5293 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5294 | // If the RHS is a qualified interface pointer "NSString<P>*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5295 | // make sure we check the class hierarchy. |
| 5296 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 5297 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5298 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5299 | // when comparing an id<P> on lhs with a static type on rhs, |
| 5300 | // see if static class implements all of id's protocols, directly or |
| 5301 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 5302 | if (rhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5303 | match = true; |
| 5304 | break; |
| 5305 | } |
| 5306 | } |
| 5307 | } |
| 5308 | if (!match) |
| 5309 | return false; |
| 5310 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5311 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5312 | return true; |
| 5313 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5314 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5315 | const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 5316 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 5317 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5318 | if (const ObjCObjectPointerType *lhsOPT = |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5319 | lhs->getAsObjCInterfacePointerType()) { |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5320 | // If both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5321 | for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(), |
| 5322 | E = lhsOPT->qual_end(); I != E; ++I) { |
| 5323 | ObjCProtocolDecl *lhsProto = *I; |
| 5324 | bool match = false; |
| 5325 | |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5326 | // 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] | 5327 | // see if static class implements all of id's protocols, directly or |
| 5328 | // through its super class and categories. |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5329 | // First, lhs protocols in the qualifier list must be found, direct |
| 5330 | // or indirect in rhs's qualifier list or it is a mismatch. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5331 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 5332 | E = rhsQID->qual_end(); J != E; ++J) { |
| 5333 | ObjCProtocolDecl *rhsProto = *J; |
| 5334 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 5335 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 5336 | match = true; |
| 5337 | break; |
| 5338 | } |
| 5339 | } |
| 5340 | if (!match) |
| 5341 | return false; |
| 5342 | } |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5343 | |
| 5344 | // Static class's protocols, or its super class or category protocols |
| 5345 | // must be found, direct or indirect in rhs's qualifier list or it is a mismatch. |
| 5346 | if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { |
| 5347 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
| 5348 | CollectInheritedProtocols(lhsID, LHSInheritedProtocols); |
| 5349 | // This is rather dubious but matches gcc's behavior. If lhs has |
| 5350 | // no type qualifier and its class has no static protocol(s) |
| 5351 | // assume that it is mismatch. |
| 5352 | if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty()) |
| 5353 | return false; |
| 5354 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 5355 | LHSInheritedProtocols.begin(), |
| 5356 | E = LHSInheritedProtocols.end(); I != E; ++I) { |
| 5357 | bool match = false; |
| 5358 | ObjCProtocolDecl *lhsProto = (*I); |
| 5359 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 5360 | E = rhsQID->qual_end(); J != E; ++J) { |
| 5361 | ObjCProtocolDecl *rhsProto = *J; |
| 5362 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 5363 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 5364 | match = true; |
| 5365 | break; |
| 5366 | } |
| 5367 | } |
| 5368 | if (!match) |
| 5369 | return false; |
| 5370 | } |
| 5371 | } |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5372 | return true; |
| 5373 | } |
| 5374 | return false; |
| 5375 | } |
| 5376 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5377 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5378 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 5379 | /// protocol qualifiers on the LHS or RHS. |
| 5380 | /// |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5381 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 5382 | const ObjCObjectPointerType *RHSOPT) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5383 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 5384 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 5385 | |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 5386 | // 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] | 5387 | if (LHS->isObjCUnqualifiedIdOrClass() || |
| 5388 | RHS->isObjCUnqualifiedIdOrClass()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5389 | return true; |
| 5390 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5391 | if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5392 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 5393 | QualType(RHSOPT,0), |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5394 | false); |
Fariborz Jahanian | a8f8dac | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 5395 | |
| 5396 | if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass()) |
| 5397 | return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0), |
| 5398 | QualType(RHSOPT,0)); |
| 5399 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5400 | // If we have 2 user-defined types, fall into that path. |
| 5401 | if (LHS->getInterface() && RHS->getInterface()) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5402 | return canAssignObjCInterfaces(LHS, RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5403 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5404 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5405 | } |
| 5406 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5407 | /// canAssignObjCInterfacesInBlockPointer - This routine is specifically written |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 5408 | /// for providing type-safety for objective-c pointers used to pass/return |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5409 | /// arguments in block literals. When passed as arguments, passing 'A*' where |
| 5410 | /// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is |
| 5411 | /// not OK. For the return type, the opposite is not OK. |
| 5412 | bool ASTContext::canAssignObjCInterfacesInBlockPointer( |
| 5413 | const ObjCObjectPointerType *LHSOPT, |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5414 | const ObjCObjectPointerType *RHSOPT, |
| 5415 | bool BlockReturnType) { |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 5416 | if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5417 | return true; |
| 5418 | |
| 5419 | if (LHSOPT->isObjCBuiltinType()) { |
| 5420 | return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType(); |
| 5421 | } |
| 5422 | |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 5423 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5424 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 5425 | QualType(RHSOPT,0), |
| 5426 | false); |
| 5427 | |
| 5428 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 5429 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 5430 | if (LHS && RHS) { // We have 2 user-defined types. |
| 5431 | if (LHS != RHS) { |
| 5432 | if (LHS->getDecl()->isSuperClassOf(RHS->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 | if (RHS->getDecl()->isSuperClassOf(LHS->getDecl())) |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5435 | return !BlockReturnType; |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5436 | } |
| 5437 | else |
| 5438 | return true; |
| 5439 | } |
| 5440 | return false; |
| 5441 | } |
| 5442 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5443 | /// getIntersectionOfProtocols - This routine finds the intersection of set |
| 5444 | /// of protocols inherited from two distinct objective-c pointer objects. |
| 5445 | /// It is used to build composite qualifier list of the composite type of |
| 5446 | /// the conditional expression involving two objective-c pointer objects. |
| 5447 | static |
| 5448 | void getIntersectionOfProtocols(ASTContext &Context, |
| 5449 | const ObjCObjectPointerType *LHSOPT, |
| 5450 | const ObjCObjectPointerType *RHSOPT, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5451 | SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5452 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5453 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 5454 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 5455 | assert(LHS->getInterface() && "LHS must have an interface base"); |
| 5456 | assert(RHS->getInterface() && "RHS must have an interface base"); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5457 | |
| 5458 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet; |
| 5459 | unsigned LHSNumProtocols = LHS->getNumProtocols(); |
| 5460 | if (LHSNumProtocols > 0) |
| 5461 | InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end()); |
| 5462 | else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 5463 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5464 | Context.CollectInheritedProtocols(LHS->getInterface(), |
| 5465 | LHSInheritedProtocols); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5466 | InheritedProtocolSet.insert(LHSInheritedProtocols.begin(), |
| 5467 | LHSInheritedProtocols.end()); |
| 5468 | } |
| 5469 | |
| 5470 | unsigned RHSNumProtocols = RHS->getNumProtocols(); |
| 5471 | if (RHSNumProtocols > 0) { |
Dan Gohman | cb421fa | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 5472 | ObjCProtocolDecl **RHSProtocols = |
| 5473 | const_cast<ObjCProtocolDecl **>(RHS->qual_begin()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5474 | for (unsigned i = 0; i < RHSNumProtocols; ++i) |
| 5475 | if (InheritedProtocolSet.count(RHSProtocols[i])) |
| 5476 | IntersectionOfProtocols.push_back(RHSProtocols[i]); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 5477 | } else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 5478 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5479 | Context.CollectInheritedProtocols(RHS->getInterface(), |
| 5480 | RHSInheritedProtocols); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 5481 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 5482 | RHSInheritedProtocols.begin(), |
| 5483 | E = RHSInheritedProtocols.end(); I != E; ++I) |
| 5484 | if (InheritedProtocolSet.count((*I))) |
| 5485 | IntersectionOfProtocols.push_back((*I)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5486 | } |
| 5487 | } |
| 5488 | |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 5489 | /// areCommonBaseCompatible - Returns common base class of the two classes if |
| 5490 | /// one found. Note that this is O'2 algorithm. But it will be called as the |
| 5491 | /// last type comparison in a ?-exp of ObjC pointer types before a |
| 5492 | /// warning is issued. So, its invokation is extremely rare. |
| 5493 | QualType ASTContext::areCommonBaseCompatible( |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5494 | const ObjCObjectPointerType *Lptr, |
| 5495 | const ObjCObjectPointerType *Rptr) { |
| 5496 | const ObjCObjectType *LHS = Lptr->getObjectType(); |
| 5497 | const ObjCObjectType *RHS = Rptr->getObjectType(); |
| 5498 | const ObjCInterfaceDecl* LDecl = LHS->getInterface(); |
| 5499 | const ObjCInterfaceDecl* RDecl = RHS->getInterface(); |
Douglas Gregor | 60ef308 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 5500 | if (!LDecl || !RDecl || (declaresSameEntity(LDecl, RDecl))) |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 5501 | return QualType(); |
| 5502 | |
Fariborz Jahanian | 7c2bdcb | 2011-04-18 21:16:59 +0000 | [diff] [blame] | 5503 | do { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5504 | LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5505 | if (canAssignObjCInterfaces(LHS, RHS)) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5506 | SmallVector<ObjCProtocolDecl *, 8> Protocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5507 | getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols); |
| 5508 | |
| 5509 | QualType Result = QualType(LHS, 0); |
| 5510 | if (!Protocols.empty()) |
| 5511 | Result = getObjCObjectType(Result, Protocols.data(), Protocols.size()); |
| 5512 | Result = getObjCObjectPointerType(Result); |
| 5513 | return Result; |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5514 | } |
Fariborz Jahanian | 7c2bdcb | 2011-04-18 21:16:59 +0000 | [diff] [blame] | 5515 | } while ((LDecl = LDecl->getSuperClass())); |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 5516 | |
| 5517 | return QualType(); |
| 5518 | } |
| 5519 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5520 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS, |
| 5521 | const ObjCObjectType *RHS) { |
| 5522 | assert(LHS->getInterface() && "LHS is not an interface type"); |
| 5523 | assert(RHS->getInterface() && "RHS is not an interface type"); |
| 5524 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5525 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 5526 | // the LHS. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5527 | if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface())) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5528 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5529 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5530 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 5531 | // protocol qualified at all, then we are good. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 5532 | if (LHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5533 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5534 | |
Fariborz Jahanian | b7bc34a | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 5535 | // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, |
| 5536 | // more detailed analysis is required. |
| 5537 | if (RHS->getNumProtocols() == 0) { |
| 5538 | // OK, if LHS is a superclass of RHS *and* |
| 5539 | // this superclass is assignment compatible with LHS. |
| 5540 | // false otherwise. |
Fariborz Jahanian | 627788c | 2011-04-12 16:34:14 +0000 | [diff] [blame] | 5541 | bool IsSuperClass = |
| 5542 | LHS->getInterface()->isSuperClassOf(RHS->getInterface()); |
| 5543 | if (IsSuperClass) { |
Fariborz Jahanian | b7bc34a | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 5544 | // OK if conversion of LHS to SuperClass results in narrowing of types |
| 5545 | // ; i.e., SuperClass may implement at least one of the protocols |
| 5546 | // in LHS's protocol list. Example, SuperObj<P1> = lhs<P1,P2> is ok. |
| 5547 | // But not SuperObj<P1,P2,P3> = lhs<P1,P2>. |
| 5548 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> SuperClassInheritedProtocols; |
Fariborz Jahanian | 627788c | 2011-04-12 16:34:14 +0000 | [diff] [blame] | 5549 | CollectInheritedProtocols(RHS->getInterface(), SuperClassInheritedProtocols); |
Fariborz Jahanian | b7bc34a | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 5550 | // If super class has no protocols, it is not a match. |
| 5551 | if (SuperClassInheritedProtocols.empty()) |
| 5552 | return false; |
| 5553 | |
| 5554 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 5555 | LHSPE = LHS->qual_end(); |
| 5556 | LHSPI != LHSPE; LHSPI++) { |
| 5557 | bool SuperImplementsProtocol = false; |
| 5558 | ObjCProtocolDecl *LHSProto = (*LHSPI); |
| 5559 | |
| 5560 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 5561 | SuperClassInheritedProtocols.begin(), |
| 5562 | E = SuperClassInheritedProtocols.end(); I != E; ++I) { |
| 5563 | ObjCProtocolDecl *SuperClassProto = (*I); |
| 5564 | if (SuperClassProto->lookupProtocolNamed(LHSProto->getIdentifier())) { |
| 5565 | SuperImplementsProtocol = true; |
| 5566 | break; |
| 5567 | } |
| 5568 | } |
| 5569 | if (!SuperImplementsProtocol) |
| 5570 | return false; |
| 5571 | } |
| 5572 | return true; |
| 5573 | } |
| 5574 | return false; |
| 5575 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5576 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5577 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 5578 | LHSPE = LHS->qual_end(); |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 5579 | LHSPI != LHSPE; LHSPI++) { |
| 5580 | bool RHSImplementsProtocol = false; |
| 5581 | |
| 5582 | // If the RHS doesn't implement the protocol on the left, the types |
| 5583 | // are incompatible. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5584 | for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(), |
| 5585 | RHSPE = RHS->qual_end(); |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 5586 | RHSPI != RHSPE; RHSPI++) { |
| 5587 | if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) { |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 5588 | RHSImplementsProtocol = true; |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 5589 | break; |
| 5590 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 5591 | } |
| 5592 | // FIXME: For better diagnostics, consider passing back the protocol name. |
| 5593 | if (!RHSImplementsProtocol) |
| 5594 | return false; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5595 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 5596 | // The RHS implements all protocols listed on the LHS. |
| 5597 | return true; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5598 | } |
| 5599 | |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 5600 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 5601 | // get the "pointed to" types |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5602 | const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
| 5603 | const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5604 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5605 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 5606 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5607 | |
| 5608 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 5609 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 5610 | } |
| 5611 | |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 5612 | bool ASTContext::canBindObjCObjectType(QualType To, QualType From) { |
| 5613 | return canAssignObjCInterfaces( |
| 5614 | getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(), |
| 5615 | getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>()); |
| 5616 | } |
| 5617 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5618 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5619 | /// both shall have the identically qualified version of a compatible type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5620 | /// 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] | 5621 | /// same. See 6.7.[2,3,5] for additional rules. |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5622 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS, |
| 5623 | bool CompareUnqualified) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5624 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 5625 | return hasSameType(LHS, RHS); |
| 5626 | |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5627 | return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5628 | } |
| 5629 | |
Fariborz Jahanian | c286f38 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 5630 | bool ASTContext::propertyTypesAreCompatible(QualType LHS, QualType RHS) { |
Fariborz Jahanian | 8237839 | 2011-07-12 23:20:13 +0000 | [diff] [blame] | 5631 | return typesAreCompatible(LHS, RHS); |
Fariborz Jahanian | c286f38 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 5632 | } |
| 5633 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5634 | bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) { |
| 5635 | return !mergeTypes(LHS, RHS, true).isNull(); |
| 5636 | } |
| 5637 | |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 5638 | /// mergeTransparentUnionType - if T is a transparent union type and a member |
| 5639 | /// of T is compatible with SubType, return the merged type, else return |
| 5640 | /// QualType() |
| 5641 | QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType, |
| 5642 | bool OfBlockPointer, |
| 5643 | bool Unqualified) { |
| 5644 | if (const RecordType *UT = T->getAsUnionType()) { |
| 5645 | RecordDecl *UD = UT->getDecl(); |
| 5646 | if (UD->hasAttr<TransparentUnionAttr>()) { |
| 5647 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 5648 | itend = UD->field_end(); it != itend; ++it) { |
Peter Collingbourne | f91d757 | 2010-12-02 21:00:06 +0000 | [diff] [blame] | 5649 | QualType ET = it->getType().getUnqualifiedType(); |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 5650 | QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified); |
| 5651 | if (!MT.isNull()) |
| 5652 | return MT; |
| 5653 | } |
| 5654 | } |
| 5655 | } |
| 5656 | |
| 5657 | return QualType(); |
| 5658 | } |
| 5659 | |
| 5660 | /// mergeFunctionArgumentTypes - merge two types which appear as function |
| 5661 | /// argument types |
| 5662 | QualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs, |
| 5663 | bool OfBlockPointer, |
| 5664 | bool Unqualified) { |
| 5665 | // GNU extension: two types are compatible if they appear as a function |
| 5666 | // argument, one of the types is a transparent union type and the other |
| 5667 | // type is compatible with a union member |
| 5668 | QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer, |
| 5669 | Unqualified); |
| 5670 | if (!lmerge.isNull()) |
| 5671 | return lmerge; |
| 5672 | |
| 5673 | QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer, |
| 5674 | Unqualified); |
| 5675 | if (!rmerge.isNull()) |
| 5676 | return rmerge; |
| 5677 | |
| 5678 | return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified); |
| 5679 | } |
| 5680 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5681 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs, |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5682 | bool OfBlockPointer, |
| 5683 | bool Unqualified) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5684 | const FunctionType *lbase = lhs->getAs<FunctionType>(); |
| 5685 | const FunctionType *rbase = rhs->getAs<FunctionType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5686 | const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 5687 | const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5688 | bool allLTypes = true; |
| 5689 | bool allRTypes = true; |
| 5690 | |
| 5691 | // Check return type |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5692 | QualType retType; |
Fariborz Jahanian | d263fd1 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 5693 | if (OfBlockPointer) { |
| 5694 | QualType RHS = rbase->getResultType(); |
| 5695 | QualType LHS = lbase->getResultType(); |
| 5696 | bool UnqualifiedResult = Unqualified; |
| 5697 | if (!UnqualifiedResult) |
| 5698 | UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers()); |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5699 | retType = mergeTypes(LHS, RHS, true, UnqualifiedResult, true); |
Fariborz Jahanian | d263fd1 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 5700 | } |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5701 | else |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5702 | retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), false, |
| 5703 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5704 | if (retType.isNull()) return QualType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5705 | |
| 5706 | if (Unqualified) |
| 5707 | retType = retType.getUnqualifiedType(); |
| 5708 | |
| 5709 | CanQualType LRetType = getCanonicalType(lbase->getResultType()); |
| 5710 | CanQualType RRetType = getCanonicalType(rbase->getResultType()); |
| 5711 | if (Unqualified) { |
| 5712 | LRetType = LRetType.getUnqualifiedType(); |
| 5713 | RRetType = RRetType.getUnqualifiedType(); |
| 5714 | } |
| 5715 | |
| 5716 | if (getCanonicalType(retType) != LRetType) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5717 | allLTypes = false; |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5718 | if (getCanonicalType(retType) != RRetType) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5719 | allRTypes = false; |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5720 | |
Daniel Dunbar | 6a15c85 | 2010-04-28 16:20:58 +0000 | [diff] [blame] | 5721 | // FIXME: double check this |
| 5722 | // FIXME: should we error if lbase->getRegParmAttr() != 0 && |
| 5723 | // rbase->getRegParmAttr() != 0 && |
| 5724 | // lbase->getRegParmAttr() != rbase->getRegParmAttr()? |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 5725 | FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo(); |
| 5726 | FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo(); |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5727 | |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 5728 | // Compatible functions must have compatible calling conventions |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5729 | if (!isSameCallConv(lbaseInfo.getCC(), rbaseInfo.getCC())) |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 5730 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5731 | |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5732 | // Regparm is part of the calling convention. |
Eli Friedman | a49218e | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 5733 | if (lbaseInfo.getHasRegParm() != rbaseInfo.getHasRegParm()) |
| 5734 | return QualType(); |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5735 | if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm()) |
| 5736 | return QualType(); |
| 5737 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5738 | if (lbaseInfo.getProducesResult() != rbaseInfo.getProducesResult()) |
| 5739 | return QualType(); |
| 5740 | |
Fariborz Jahanian | 53c8167 | 2011-10-05 00:05:34 +0000 | [diff] [blame] | 5741 | // functypes which return are preferred over those that do not. |
| 5742 | if (lbaseInfo.getNoReturn() && !rbaseInfo.getNoReturn()) |
| 5743 | allLTypes = false; |
| 5744 | else if (!lbaseInfo.getNoReturn() && rbaseInfo.getNoReturn()) |
| 5745 | allRTypes = false; |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5746 | // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'. |
| 5747 | bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn(); |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5748 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5749 | FunctionType::ExtInfo einfo = lbaseInfo.withNoReturn(NoReturn); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5750 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5751 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 5752 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 5753 | "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5754 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 5755 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 5756 | |
| 5757 | // Compatible functions must have the same number of arguments |
| 5758 | if (lproto_nargs != rproto_nargs) |
| 5759 | return QualType(); |
| 5760 | |
| 5761 | // Variadic and non-variadic functions aren't compatible |
| 5762 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 5763 | return QualType(); |
| 5764 | |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 5765 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 5766 | return QualType(); |
| 5767 | |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 5768 | if (LangOpts.ObjCAutoRefCount && |
| 5769 | !FunctionTypesMatchOnNSConsumedAttrs(rproto, lproto)) |
| 5770 | return QualType(); |
| 5771 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5772 | // Check argument compatibility |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5773 | SmallVector<QualType, 10> types; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5774 | for (unsigned i = 0; i < lproto_nargs; i++) { |
| 5775 | QualType largtype = lproto->getArgType(i).getUnqualifiedType(); |
| 5776 | QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 5777 | QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype, |
| 5778 | OfBlockPointer, |
| 5779 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5780 | if (argtype.isNull()) return QualType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5781 | |
| 5782 | if (Unqualified) |
| 5783 | argtype = argtype.getUnqualifiedType(); |
| 5784 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5785 | types.push_back(argtype); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5786 | if (Unqualified) { |
| 5787 | largtype = largtype.getUnqualifiedType(); |
| 5788 | rargtype = rargtype.getUnqualifiedType(); |
| 5789 | } |
| 5790 | |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5791 | if (getCanonicalType(argtype) != getCanonicalType(largtype)) |
| 5792 | allLTypes = false; |
| 5793 | if (getCanonicalType(argtype) != getCanonicalType(rargtype)) |
| 5794 | allRTypes = false; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5795 | } |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 5796 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5797 | if (allLTypes) return lhs; |
| 5798 | if (allRTypes) return rhs; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5799 | |
| 5800 | FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo(); |
| 5801 | EPI.ExtInfo = einfo; |
| 5802 | return getFunctionType(retType, types.begin(), types.size(), EPI); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5803 | } |
| 5804 | |
| 5805 | if (lproto) allRTypes = false; |
| 5806 | if (rproto) allLTypes = false; |
| 5807 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5808 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5809 | if (proto) { |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 5810 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5811 | if (proto->isVariadic()) return QualType(); |
| 5812 | // Check that the types are compatible with the types that |
| 5813 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 5814 | // The only types actually affected are promotable integer |
| 5815 | // types and floats, which would be passed as a different |
| 5816 | // type depending on whether the prototype is visible. |
| 5817 | unsigned proto_nargs = proto->getNumArgs(); |
| 5818 | for (unsigned i = 0; i < proto_nargs; ++i) { |
| 5819 | QualType argTy = proto->getArgType(i); |
Douglas Gregor | b0f8eac | 2010-02-03 19:27:29 +0000 | [diff] [blame] | 5820 | |
| 5821 | // Look at the promotion type of enum types, since that is the type used |
| 5822 | // to pass enum values. |
| 5823 | if (const EnumType *Enum = argTy->getAs<EnumType>()) |
| 5824 | argTy = Enum->getDecl()->getPromotionType(); |
| 5825 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5826 | if (argTy->isPromotableIntegerType() || |
| 5827 | getCanonicalType(argTy).getUnqualifiedType() == FloatTy) |
| 5828 | return QualType(); |
| 5829 | } |
| 5830 | |
| 5831 | if (allLTypes) return lhs; |
| 5832 | if (allRTypes) return rhs; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5833 | |
| 5834 | FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo(); |
| 5835 | EPI.ExtInfo = einfo; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5836 | return getFunctionType(retType, proto->arg_type_begin(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5837 | proto->getNumArgs(), EPI); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5838 | } |
| 5839 | |
| 5840 | if (allLTypes) return lhs; |
| 5841 | if (allRTypes) return rhs; |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5842 | return getFunctionNoProtoType(retType, einfo); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5843 | } |
| 5844 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5845 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5846 | bool OfBlockPointer, |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5847 | bool Unqualified, bool BlockReturnType) { |
Bill Wendling | 43d6975 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 5848 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 5849 | // type is adjusted to "T" prior to any further analysis, the expression |
| 5850 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 5851 | // expression is an lvalue unless the reference is an rvalue reference and |
| 5852 | // the expression is a function call (possibly inside parentheses). |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 5853 | assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?"); |
| 5854 | assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?"); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5855 | |
| 5856 | if (Unqualified) { |
| 5857 | LHS = LHS.getUnqualifiedType(); |
| 5858 | RHS = RHS.getUnqualifiedType(); |
| 5859 | } |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 5860 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5861 | QualType LHSCan = getCanonicalType(LHS), |
| 5862 | RHSCan = getCanonicalType(RHS); |
| 5863 | |
| 5864 | // If two types are identical, they are compatible. |
| 5865 | if (LHSCan == RHSCan) |
| 5866 | return LHS; |
| 5867 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5868 | // If the qualifiers are different, the types aren't compatible... mostly. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 5869 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 5870 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5871 | if (LQuals != RQuals) { |
| 5872 | // If any of these qualifiers are different, we have a type |
| 5873 | // mismatch. |
| 5874 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5875 | LQuals.getAddressSpace() != RQuals.getAddressSpace() || |
| 5876 | LQuals.getObjCLifetime() != RQuals.getObjCLifetime()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5877 | return QualType(); |
| 5878 | |
| 5879 | // Exactly one GC qualifier difference is allowed: __strong is |
| 5880 | // okay if the other type has no GC qualifier but is an Objective |
| 5881 | // C object pointer (i.e. implicitly strong by default). We fix |
| 5882 | // this by pretending that the unqualified type was actually |
| 5883 | // qualified __strong. |
| 5884 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 5885 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 5886 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 5887 | |
| 5888 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 5889 | return QualType(); |
| 5890 | |
| 5891 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
| 5892 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); |
| 5893 | } |
| 5894 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
| 5895 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); |
| 5896 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5897 | return QualType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5898 | } |
| 5899 | |
| 5900 | // Okay, qualifiers are equal. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5901 | |
Eli Friedman | 852d63b | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 5902 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 5903 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5904 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5905 | // We want to consider the two function types to be the same for these |
| 5906 | // comparisons, just force one to the other. |
| 5907 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 5908 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 4c721d3 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 5909 | |
| 5910 | // Same as above for arrays |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 5911 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 5912 | LHSClass = Type::ConstantArray; |
| 5913 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 5914 | RHSClass = Type::ConstantArray; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5915 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5916 | // ObjCInterfaces are just specialized ObjCObjects. |
| 5917 | if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject; |
| 5918 | if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject; |
| 5919 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 5920 | // Canonicalize ExtVector -> Vector. |
| 5921 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 5922 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5923 | |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 5924 | // If the canonical type classes don't match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5925 | if (LHSClass != RHSClass) { |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5926 | // 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] | 5927 | // a signed integer type, or an unsigned integer type. |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 5928 | // Compatibility is based on the underlying type, not the promotion |
| 5929 | // type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5930 | if (const EnumType* ETy = LHS->getAs<EnumType>()) { |
Fariborz Jahanian | b918d6b | 2012-02-06 19:06:20 +0000 | [diff] [blame] | 5931 | QualType TINT = ETy->getDecl()->getIntegerType(); |
| 5932 | if (!TINT.isNull() && hasSameType(TINT, RHSCan.getUnqualifiedType())) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5933 | return RHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 5934 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5935 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
Fariborz Jahanian | b918d6b | 2012-02-06 19:06:20 +0000 | [diff] [blame] | 5936 | QualType TINT = ETy->getDecl()->getIntegerType(); |
| 5937 | if (!TINT.isNull() && hasSameType(TINT, LHSCan.getUnqualifiedType())) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5938 | return LHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 5939 | } |
Fariborz Jahanian | e7cff2c | 2012-01-26 00:45:38 +0000 | [diff] [blame] | 5940 | // allow block pointer type to match an 'id' type. |
Fariborz Jahanian | 4196363 | 2012-01-26 17:08:50 +0000 | [diff] [blame] | 5941 | if (OfBlockPointer && !BlockReturnType) { |
| 5942 | if (LHS->isObjCIdType() && RHS->isBlockPointerType()) |
| 5943 | return LHS; |
| 5944 | if (RHS->isObjCIdType() && LHS->isBlockPointerType()) |
| 5945 | return RHS; |
| 5946 | } |
Fariborz Jahanian | e7cff2c | 2012-01-26 00:45:38 +0000 | [diff] [blame] | 5947 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5948 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5949 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5950 | |
Steve Naroff | 4a74678 | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 5951 | // The canonical type classes match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5952 | switch (LHSClass) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5953 | #define TYPE(Class, Base) |
| 5954 | #define ABSTRACT_TYPE(Class, Base) |
John McCall | ad5e738 | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 5955 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5956 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 5957 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 5958 | #include "clang/AST/TypeNodes.def" |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5959 | llvm_unreachable("Non-canonical and dependent types shouldn't get here"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5960 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 5961 | case Type::LValueReference: |
| 5962 | case Type::RValueReference: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5963 | case Type::MemberPointer: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5964 | llvm_unreachable("C++ should never be in mergeTypes"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5965 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5966 | case Type::ObjCInterface: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5967 | case Type::IncompleteArray: |
| 5968 | case Type::VariableArray: |
| 5969 | case Type::FunctionProto: |
| 5970 | case Type::ExtVector: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5971 | llvm_unreachable("Types are eliminated above"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5972 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5973 | case Type::Pointer: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5974 | { |
| 5975 | // Merge two pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5976 | QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); |
| 5977 | QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5978 | if (Unqualified) { |
| 5979 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 5980 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 5981 | } |
| 5982 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false, |
| 5983 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5984 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 5985 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5986 | return LHS; |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 5987 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5988 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5989 | return getPointerType(ResultType); |
| 5990 | } |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 5991 | case Type::BlockPointer: |
| 5992 | { |
| 5993 | // Merge two block pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5994 | QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); |
| 5995 | QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5996 | if (Unqualified) { |
| 5997 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 5998 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 5999 | } |
| 6000 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer, |
| 6001 | Unqualified); |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 6002 | if (ResultType.isNull()) return QualType(); |
| 6003 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 6004 | return LHS; |
| 6005 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 6006 | return RHS; |
| 6007 | return getBlockPointerType(ResultType); |
| 6008 | } |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6009 | case Type::Atomic: |
| 6010 | { |
| 6011 | // Merge two pointer types, while trying to preserve typedef info |
| 6012 | QualType LHSValue = LHS->getAs<AtomicType>()->getValueType(); |
| 6013 | QualType RHSValue = RHS->getAs<AtomicType>()->getValueType(); |
| 6014 | if (Unqualified) { |
| 6015 | LHSValue = LHSValue.getUnqualifiedType(); |
| 6016 | RHSValue = RHSValue.getUnqualifiedType(); |
| 6017 | } |
| 6018 | QualType ResultType = mergeTypes(LHSValue, RHSValue, false, |
| 6019 | Unqualified); |
| 6020 | if (ResultType.isNull()) return QualType(); |
| 6021 | if (getCanonicalType(LHSValue) == getCanonicalType(ResultType)) |
| 6022 | return LHS; |
| 6023 | if (getCanonicalType(RHSValue) == getCanonicalType(ResultType)) |
| 6024 | return RHS; |
| 6025 | return getAtomicType(ResultType); |
| 6026 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6027 | case Type::ConstantArray: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6028 | { |
| 6029 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 6030 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 6031 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 6032 | return QualType(); |
| 6033 | |
| 6034 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 6035 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6036 | if (Unqualified) { |
| 6037 | LHSElem = LHSElem.getUnqualifiedType(); |
| 6038 | RHSElem = RHSElem.getUnqualifiedType(); |
| 6039 | } |
| 6040 | |
| 6041 | QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6042 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6043 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 6044 | return LHS; |
| 6045 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 6046 | return RHS; |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 6047 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 6048 | ArrayType::ArraySizeModifier(), 0); |
| 6049 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 6050 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6051 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 6052 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6053 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 6054 | return LHS; |
| 6055 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 6056 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6057 | if (LVAT) { |
| 6058 | // FIXME: This isn't correct! But tricky to implement because |
| 6059 | // the array's size has to be the size of LHS, but the type |
| 6060 | // has to be different. |
| 6061 | return LHS; |
| 6062 | } |
| 6063 | if (RVAT) { |
| 6064 | // FIXME: This isn't correct! But tricky to implement because |
| 6065 | // the array's size has to be the size of RHS, but the type |
| 6066 | // has to be different. |
| 6067 | return RHS; |
| 6068 | } |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 6069 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 6070 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 6071 | return getIncompleteArrayType(ResultType, |
| 6072 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6073 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6074 | case Type::FunctionNoProto: |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6075 | return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6076 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6077 | case Type::Enum: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6078 | return QualType(); |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6079 | case Type::Builtin: |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 6080 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6081 | return QualType(); |
Daniel Dunbar | 64cfdb7 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 6082 | case Type::Complex: |
| 6083 | // Distinct complex types are incompatible. |
| 6084 | return QualType(); |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 6085 | case Type::Vector: |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 6086 | // FIXME: The merged type should be an ExtVector! |
John McCall | 1c471f3 | 2010-03-12 23:14:13 +0000 | [diff] [blame] | 6087 | if (areCompatVectorTypes(LHSCan->getAs<VectorType>(), |
| 6088 | RHSCan->getAs<VectorType>())) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6089 | return LHS; |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6090 | return QualType(); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6091 | case Type::ObjCObject: { |
| 6092 | // Check if the types are assignment compatible. |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 6093 | // FIXME: This should be type compatibility, e.g. whether |
| 6094 | // "LHS x; RHS x;" at global scope is legal. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6095 | const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>(); |
| 6096 | const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>(); |
| 6097 | if (canAssignObjCInterfaces(LHSIface, RHSIface)) |
Steve Naroff | 5fd659d | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 6098 | return LHS; |
| 6099 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6100 | return QualType(); |
Cedric Venet | 61490e9 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 6101 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6102 | case Type::ObjCObjectPointer: { |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6103 | if (OfBlockPointer) { |
| 6104 | if (canAssignObjCInterfacesInBlockPointer( |
| 6105 | LHS->getAs<ObjCObjectPointerType>(), |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6106 | RHS->getAs<ObjCObjectPointerType>(), |
| 6107 | BlockReturnType)) |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 6108 | return LHS; |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6109 | return QualType(); |
| 6110 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6111 | if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(), |
| 6112 | RHS->getAs<ObjCObjectPointerType>())) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6113 | return LHS; |
| 6114 | |
Steve Naroff | bc76dd0 | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 6115 | return QualType(); |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 6116 | } |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 6117 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6118 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 6119 | llvm_unreachable("Invalid Type::Class!"); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 6120 | } |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 6121 | |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 6122 | bool ASTContext::FunctionTypesMatchOnNSConsumedAttrs( |
| 6123 | const FunctionProtoType *FromFunctionType, |
| 6124 | const FunctionProtoType *ToFunctionType) { |
| 6125 | if (FromFunctionType->hasAnyConsumedArgs() != |
| 6126 | ToFunctionType->hasAnyConsumedArgs()) |
| 6127 | return false; |
| 6128 | FunctionProtoType::ExtProtoInfo FromEPI = |
| 6129 | FromFunctionType->getExtProtoInfo(); |
| 6130 | FunctionProtoType::ExtProtoInfo ToEPI = |
| 6131 | ToFunctionType->getExtProtoInfo(); |
| 6132 | if (FromEPI.ConsumedArguments && ToEPI.ConsumedArguments) |
| 6133 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 6134 | ArgIdx != NumArgs; ++ArgIdx) { |
| 6135 | if (FromEPI.ConsumedArguments[ArgIdx] != |
| 6136 | ToEPI.ConsumedArguments[ArgIdx]) |
| 6137 | return false; |
| 6138 | } |
| 6139 | return true; |
| 6140 | } |
| 6141 | |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 6142 | /// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and |
| 6143 | /// 'RHS' attributes and returns the merged version; including for function |
| 6144 | /// return types. |
| 6145 | QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) { |
| 6146 | QualType LHSCan = getCanonicalType(LHS), |
| 6147 | RHSCan = getCanonicalType(RHS); |
| 6148 | // If two types are identical, they are compatible. |
| 6149 | if (LHSCan == RHSCan) |
| 6150 | return LHS; |
| 6151 | if (RHSCan->isFunctionType()) { |
| 6152 | if (!LHSCan->isFunctionType()) |
| 6153 | return QualType(); |
| 6154 | QualType OldReturnType = |
| 6155 | cast<FunctionType>(RHSCan.getTypePtr())->getResultType(); |
| 6156 | QualType NewReturnType = |
| 6157 | cast<FunctionType>(LHSCan.getTypePtr())->getResultType(); |
| 6158 | QualType ResReturnType = |
| 6159 | mergeObjCGCQualifiers(NewReturnType, OldReturnType); |
| 6160 | if (ResReturnType.isNull()) |
| 6161 | return QualType(); |
| 6162 | if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) { |
| 6163 | // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo(); |
| 6164 | // In either case, use OldReturnType to build the new function type. |
| 6165 | const FunctionType *F = LHS->getAs<FunctionType>(); |
| 6166 | if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6167 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 6168 | EPI.ExtInfo = getFunctionExtInfo(LHS); |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 6169 | QualType ResultType |
| 6170 | = getFunctionType(OldReturnType, FPT->arg_type_begin(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6171 | FPT->getNumArgs(), EPI); |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 6172 | return ResultType; |
| 6173 | } |
| 6174 | } |
| 6175 | return QualType(); |
| 6176 | } |
| 6177 | |
| 6178 | // If the qualifiers are different, the types can still be merged. |
| 6179 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 6180 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
| 6181 | if (LQuals != RQuals) { |
| 6182 | // If any of these qualifiers are different, we have a type mismatch. |
| 6183 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 6184 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 6185 | return QualType(); |
| 6186 | |
| 6187 | // Exactly one GC qualifier difference is allowed: __strong is |
| 6188 | // okay if the other type has no GC qualifier but is an Objective |
| 6189 | // C object pointer (i.e. implicitly strong by default). We fix |
| 6190 | // this by pretending that the unqualified type was actually |
| 6191 | // qualified __strong. |
| 6192 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 6193 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 6194 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 6195 | |
| 6196 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 6197 | return QualType(); |
| 6198 | |
| 6199 | if (GC_L == Qualifiers::Strong) |
| 6200 | return LHS; |
| 6201 | if (GC_R == Qualifiers::Strong) |
| 6202 | return RHS; |
| 6203 | return QualType(); |
| 6204 | } |
| 6205 | |
| 6206 | if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) { |
| 6207 | QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 6208 | QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 6209 | QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT); |
| 6210 | if (ResQT == LHSBaseQT) |
| 6211 | return LHS; |
| 6212 | if (ResQT == RHSBaseQT) |
| 6213 | return RHS; |
| 6214 | } |
| 6215 | return QualType(); |
| 6216 | } |
| 6217 | |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 6218 | //===----------------------------------------------------------------------===// |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6219 | // Integer Predicates |
| 6220 | //===----------------------------------------------------------------------===// |
Chris Lattner | 88054de | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 6221 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6222 | unsigned ASTContext::getIntWidth(QualType T) const { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 6223 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
Eli Friedman | 29a7f33 | 2009-12-10 22:29:29 +0000 | [diff] [blame] | 6224 | T = ET->getDecl()->getIntegerType(); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6225 | if (T->isBooleanType()) |
| 6226 | return 1; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 6227 | // For builtin types, just use the standard type sizing method |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6228 | return (unsigned)getTypeSize(T); |
| 6229 | } |
| 6230 | |
| 6231 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) { |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6232 | assert(T->hasSignedIntegerRepresentation() && "Unexpected type"); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6233 | |
| 6234 | // Turn <4 x signed int> -> <4 x unsigned int> |
| 6235 | if (const VectorType *VTy = T->getAs<VectorType>()) |
| 6236 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 6237 | VTy->getNumElements(), VTy->getVectorKind()); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6238 | |
| 6239 | // For enums, we return the unsigned version of the base type. |
| 6240 | if (const EnumType *ETy = T->getAs<EnumType>()) |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6241 | T = ETy->getDecl()->getIntegerType(); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6242 | |
| 6243 | const BuiltinType *BTy = T->getAs<BuiltinType>(); |
| 6244 | assert(BTy && "Unexpected signed integer type"); |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6245 | switch (BTy->getKind()) { |
| 6246 | case BuiltinType::Char_S: |
| 6247 | case BuiltinType::SChar: |
| 6248 | return UnsignedCharTy; |
| 6249 | case BuiltinType::Short: |
| 6250 | return UnsignedShortTy; |
| 6251 | case BuiltinType::Int: |
| 6252 | return UnsignedIntTy; |
| 6253 | case BuiltinType::Long: |
| 6254 | return UnsignedLongTy; |
| 6255 | case BuiltinType::LongLong: |
| 6256 | return UnsignedLongLongTy; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 6257 | case BuiltinType::Int128: |
| 6258 | return UnsignedInt128Ty; |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6259 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6260 | llvm_unreachable("Unexpected signed integer type"); |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6261 | } |
| 6262 | } |
| 6263 | |
Argyrios Kyrtzidis | 7b90340 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 6264 | ASTMutationListener::~ASTMutationListener() { } |
| 6265 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6266 | |
| 6267 | //===----------------------------------------------------------------------===// |
| 6268 | // Builtin Type Computation |
| 6269 | //===----------------------------------------------------------------------===// |
| 6270 | |
| 6271 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6272 | /// pointer over the consumed characters. This returns the resultant type. If |
| 6273 | /// AllowTypeModifiers is false then modifier like * are not parsed, just basic |
| 6274 | /// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of |
| 6275 | /// a vector of "i*". |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6276 | /// |
| 6277 | /// RequiresICE is filled in on return to indicate whether the value is required |
| 6278 | /// to be an Integer Constant Expression. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6279 | static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context, |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6280 | ASTContext::GetBuiltinTypeError &Error, |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6281 | bool &RequiresICE, |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6282 | bool AllowTypeModifiers) { |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6283 | // Modifiers. |
| 6284 | int HowLong = 0; |
| 6285 | bool Signed = false, Unsigned = false; |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6286 | RequiresICE = false; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6287 | |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6288 | // Read the prefixed modifiers first. |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6289 | bool Done = false; |
| 6290 | while (!Done) { |
| 6291 | switch (*Str++) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6292 | default: Done = true; --Str; break; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6293 | case 'I': |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6294 | RequiresICE = true; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6295 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6296 | case 'S': |
| 6297 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 6298 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 6299 | Signed = true; |
| 6300 | break; |
| 6301 | case 'U': |
| 6302 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
| 6303 | assert(!Unsigned && "Can't use 'S' modifier multiple times!"); |
| 6304 | Unsigned = true; |
| 6305 | break; |
| 6306 | case 'L': |
| 6307 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 6308 | ++HowLong; |
| 6309 | break; |
| 6310 | } |
| 6311 | } |
| 6312 | |
| 6313 | QualType Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6314 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6315 | // Read the base type. |
| 6316 | switch (*Str++) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6317 | default: llvm_unreachable("Unknown builtin type letter!"); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6318 | case 'v': |
| 6319 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 6320 | "Bad modifiers used with 'v'!"); |
| 6321 | Type = Context.VoidTy; |
| 6322 | break; |
| 6323 | case 'f': |
| 6324 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 6325 | "Bad modifiers used with 'f'!"); |
| 6326 | Type = Context.FloatTy; |
| 6327 | break; |
| 6328 | case 'd': |
| 6329 | assert(HowLong < 2 && !Signed && !Unsigned && |
| 6330 | "Bad modifiers used with 'd'!"); |
| 6331 | if (HowLong) |
| 6332 | Type = Context.LongDoubleTy; |
| 6333 | else |
| 6334 | Type = Context.DoubleTy; |
| 6335 | break; |
| 6336 | case 's': |
| 6337 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 6338 | if (Unsigned) |
| 6339 | Type = Context.UnsignedShortTy; |
| 6340 | else |
| 6341 | Type = Context.ShortTy; |
| 6342 | break; |
| 6343 | case 'i': |
| 6344 | if (HowLong == 3) |
| 6345 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 6346 | else if (HowLong == 2) |
| 6347 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 6348 | else if (HowLong == 1) |
| 6349 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 6350 | else |
| 6351 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 6352 | break; |
| 6353 | case 'c': |
| 6354 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 6355 | if (Signed) |
| 6356 | Type = Context.SignedCharTy; |
| 6357 | else if (Unsigned) |
| 6358 | Type = Context.UnsignedCharTy; |
| 6359 | else |
| 6360 | Type = Context.CharTy; |
| 6361 | break; |
| 6362 | case 'b': // boolean |
| 6363 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 6364 | Type = Context.BoolTy; |
| 6365 | break; |
| 6366 | case 'z': // size_t. |
| 6367 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 6368 | Type = Context.getSizeType(); |
| 6369 | break; |
| 6370 | case 'F': |
| 6371 | Type = Context.getCFConstantStringType(); |
| 6372 | break; |
Fariborz Jahanian | ba8bda0 | 2010-11-09 21:38:20 +0000 | [diff] [blame] | 6373 | case 'G': |
| 6374 | Type = Context.getObjCIdType(); |
| 6375 | break; |
| 6376 | case 'H': |
| 6377 | Type = Context.getObjCSelType(); |
| 6378 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6379 | case 'a': |
| 6380 | Type = Context.getBuiltinVaListType(); |
| 6381 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 6382 | break; |
| 6383 | case 'A': |
| 6384 | // This is a "reference" to a va_list; however, what exactly |
| 6385 | // this means depends on how va_list is defined. There are two |
| 6386 | // different kinds of va_list: ones passed by value, and ones |
| 6387 | // passed by reference. An example of a by-value va_list is |
| 6388 | // x86, where va_list is a char*. An example of by-ref va_list |
| 6389 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 6390 | // we want this argument to be a char*&; for x86-64, we want |
| 6391 | // it to be a __va_list_tag*. |
| 6392 | Type = Context.getBuiltinVaListType(); |
| 6393 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6394 | if (Type->isArrayType()) |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6395 | Type = Context.getArrayDecayedType(Type); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6396 | else |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6397 | Type = Context.getLValueReferenceType(Type); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6398 | break; |
| 6399 | case 'V': { |
| 6400 | char *End; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6401 | unsigned NumElements = strtoul(Str, &End, 10); |
| 6402 | assert(End != Str && "Missing vector size"); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6403 | Str = End; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6404 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6405 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, |
| 6406 | RequiresICE, false); |
| 6407 | assert(!RequiresICE && "Can't require vector ICE"); |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6408 | |
| 6409 | // TODO: No way to make AltiVec vectors in builtins yet. |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 6410 | Type = Context.getVectorType(ElementType, NumElements, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 6411 | VectorType::GenericVector); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6412 | break; |
| 6413 | } |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 6414 | case 'X': { |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6415 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
| 6416 | false); |
| 6417 | assert(!RequiresICE && "Can't require complex ICE"); |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 6418 | Type = Context.getComplexType(ElementType); |
| 6419 | break; |
Fariborz Jahanian | cc075e4 | 2011-08-23 23:33:09 +0000 | [diff] [blame] | 6420 | } |
| 6421 | case 'Y' : { |
| 6422 | Type = Context.getPointerDiffType(); |
| 6423 | break; |
| 6424 | } |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 6425 | case 'P': |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 6426 | Type = Context.getFILEType(); |
| 6427 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6428 | Error = ASTContext::GE_Missing_stdio; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6429 | return QualType(); |
| 6430 | } |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 6431 | break; |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 6432 | case 'J': |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6433 | if (Signed) |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 6434 | Type = Context.getsigjmp_bufType(); |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6435 | else |
| 6436 | Type = Context.getjmp_bufType(); |
| 6437 | |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 6438 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6439 | Error = ASTContext::GE_Missing_setjmp; |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 6440 | return QualType(); |
| 6441 | } |
| 6442 | break; |
Rafael Espindola | e2d4f4e | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 6443 | case 'K': |
| 6444 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'K'!"); |
| 6445 | Type = Context.getucontext_tType(); |
| 6446 | |
| 6447 | if (Type.isNull()) { |
| 6448 | Error = ASTContext::GE_Missing_ucontext; |
| 6449 | return QualType(); |
| 6450 | } |
| 6451 | break; |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 6452 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6453 | |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6454 | // If there are modifiers and if we're allowed to parse them, go for it. |
| 6455 | Done = !AllowTypeModifiers; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6456 | while (!Done) { |
John McCall | 187ab37 | 2010-03-12 04:21:28 +0000 | [diff] [blame] | 6457 | switch (char c = *Str++) { |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6458 | default: Done = true; --Str; break; |
| 6459 | case '*': |
| 6460 | case '&': { |
| 6461 | // Both pointers and references can have their pointee types |
| 6462 | // qualified with an address space. |
| 6463 | char *End; |
| 6464 | unsigned AddrSpace = strtoul(Str, &End, 10); |
| 6465 | if (End != Str && AddrSpace != 0) { |
| 6466 | Type = Context.getAddrSpaceQualType(Type, AddrSpace); |
| 6467 | Str = End; |
| 6468 | } |
| 6469 | if (c == '*') |
| 6470 | Type = Context.getPointerType(Type); |
| 6471 | else |
| 6472 | Type = Context.getLValueReferenceType(Type); |
| 6473 | break; |
| 6474 | } |
| 6475 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 6476 | case 'C': |
| 6477 | Type = Type.withConst(); |
| 6478 | break; |
| 6479 | case 'D': |
| 6480 | Type = Context.getVolatileType(Type); |
| 6481 | break; |
Ted Kremenek | 18932a0 | 2012-01-20 21:40:12 +0000 | [diff] [blame] | 6482 | case 'R': |
| 6483 | Type = Type.withRestrict(); |
| 6484 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6485 | } |
| 6486 | } |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6487 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6488 | assert((!RequiresICE || Type->isIntegralOrEnumerationType()) && |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6489 | "Integer constant 'I' type must be an integer"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6490 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6491 | return Type; |
| 6492 | } |
| 6493 | |
| 6494 | /// GetBuiltinType - Return the type for the specified builtin. |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6495 | QualType ASTContext::GetBuiltinType(unsigned Id, |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6496 | GetBuiltinTypeError &Error, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6497 | unsigned *IntegerConstantArgs) const { |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6498 | const char *TypeStr = BuiltinInfo.GetTypeString(Id); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6499 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6500 | SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6501 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6502 | bool RequiresICE = false; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6503 | Error = GE_None; |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6504 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error, |
| 6505 | RequiresICE, true); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6506 | if (Error != GE_None) |
| 6507 | return QualType(); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6508 | |
| 6509 | assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE"); |
| 6510 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6511 | while (TypeStr[0] && TypeStr[0] != '.') { |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6512 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6513 | if (Error != GE_None) |
| 6514 | return QualType(); |
| 6515 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6516 | // If this argument is required to be an IntegerConstantExpression and the |
| 6517 | // caller cares, fill in the bitmask we return. |
| 6518 | if (RequiresICE && IntegerConstantArgs) |
| 6519 | *IntegerConstantArgs |= 1 << ArgTypes.size(); |
| 6520 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6521 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 6522 | if (Ty->isArrayType()) |
| 6523 | Ty = getArrayDecayedType(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6524 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6525 | ArgTypes.push_back(Ty); |
| 6526 | } |
| 6527 | |
| 6528 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 6529 | "'.' should only occur at end of builtin type list!"); |
| 6530 | |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 6531 | FunctionType::ExtInfo EI; |
| 6532 | if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true); |
| 6533 | |
| 6534 | bool Variadic = (TypeStr[0] == '.'); |
| 6535 | |
| 6536 | // We really shouldn't be making a no-proto type here, especially in C++. |
| 6537 | if (ArgTypes.empty() && Variadic) |
| 6538 | return getFunctionNoProtoType(ResType, EI); |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 6539 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6540 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 6541 | EPI.ExtInfo = EI; |
| 6542 | EPI.Variadic = Variadic; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6543 | |
| 6544 | return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), EPI); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6545 | } |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 6546 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6547 | GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) { |
| 6548 | GVALinkage External = GVA_StrongExternal; |
| 6549 | |
| 6550 | Linkage L = FD->getLinkage(); |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6551 | switch (L) { |
| 6552 | case NoLinkage: |
| 6553 | case InternalLinkage: |
| 6554 | case UniqueExternalLinkage: |
| 6555 | return GVA_Internal; |
| 6556 | |
| 6557 | case ExternalLinkage: |
| 6558 | switch (FD->getTemplateSpecializationKind()) { |
| 6559 | case TSK_Undeclared: |
| 6560 | case TSK_ExplicitSpecialization: |
| 6561 | External = GVA_StrongExternal; |
| 6562 | break; |
| 6563 | |
| 6564 | case TSK_ExplicitInstantiationDefinition: |
| 6565 | return GVA_ExplicitTemplateInstantiation; |
| 6566 | |
| 6567 | case TSK_ExplicitInstantiationDeclaration: |
| 6568 | case TSK_ImplicitInstantiation: |
| 6569 | External = GVA_TemplateInstantiation; |
| 6570 | break; |
| 6571 | } |
| 6572 | } |
| 6573 | |
| 6574 | if (!FD->isInlined()) |
| 6575 | return External; |
| 6576 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6577 | if (!getLangOpts().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) { |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6578 | // GNU or C99 inline semantics. Determine whether this symbol should be |
| 6579 | // externally visible. |
| 6580 | if (FD->isInlineDefinitionExternallyVisible()) |
| 6581 | return External; |
| 6582 | |
| 6583 | // C99 inline semantics, where the symbol is not externally visible. |
| 6584 | return GVA_C99Inline; |
| 6585 | } |
| 6586 | |
| 6587 | // C++0x [temp.explicit]p9: |
| 6588 | // [ Note: The intent is that an inline function that is the subject of |
| 6589 | // an explicit instantiation declaration will still be implicitly |
| 6590 | // instantiated when used so that the body can be considered for |
| 6591 | // inlining, but that no out-of-line copy of the inline function would be |
| 6592 | // generated in the translation unit. -- end note ] |
| 6593 | if (FD->getTemplateSpecializationKind() |
| 6594 | == TSK_ExplicitInstantiationDeclaration) |
| 6595 | return GVA_C99Inline; |
| 6596 | |
| 6597 | return GVA_CXXInline; |
| 6598 | } |
| 6599 | |
| 6600 | GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) { |
| 6601 | // If this is a static data member, compute the kind of template |
| 6602 | // specialization. Otherwise, this variable is not part of a |
| 6603 | // template. |
| 6604 | TemplateSpecializationKind TSK = TSK_Undeclared; |
| 6605 | if (VD->isStaticDataMember()) |
| 6606 | TSK = VD->getTemplateSpecializationKind(); |
| 6607 | |
| 6608 | Linkage L = VD->getLinkage(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6609 | if (L == ExternalLinkage && getLangOpts().CPlusPlus && |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6610 | VD->getType()->getLinkage() == UniqueExternalLinkage) |
| 6611 | L = UniqueExternalLinkage; |
| 6612 | |
| 6613 | switch (L) { |
| 6614 | case NoLinkage: |
| 6615 | case InternalLinkage: |
| 6616 | case UniqueExternalLinkage: |
| 6617 | return GVA_Internal; |
| 6618 | |
| 6619 | case ExternalLinkage: |
| 6620 | switch (TSK) { |
| 6621 | case TSK_Undeclared: |
| 6622 | case TSK_ExplicitSpecialization: |
| 6623 | return GVA_StrongExternal; |
| 6624 | |
| 6625 | case TSK_ExplicitInstantiationDeclaration: |
| 6626 | llvm_unreachable("Variable should not be instantiated"); |
| 6627 | // Fall through to treat this like any other instantiation. |
| 6628 | |
| 6629 | case TSK_ExplicitInstantiationDefinition: |
| 6630 | return GVA_ExplicitTemplateInstantiation; |
| 6631 | |
| 6632 | case TSK_ImplicitInstantiation: |
| 6633 | return GVA_TemplateInstantiation; |
| 6634 | } |
| 6635 | } |
| 6636 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 6637 | llvm_unreachable("Invalid Linkage!"); |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6638 | } |
| 6639 | |
Argyrios Kyrtzidis | 4ac7c0b | 2010-07-29 20:08:05 +0000 | [diff] [blame] | 6640 | bool ASTContext::DeclMustBeEmitted(const Decl *D) { |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6641 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 6642 | if (!VD->isFileVarDecl()) |
| 6643 | return false; |
Argyrios Kyrtzidis | 144b38a | 2011-09-13 21:35:00 +0000 | [diff] [blame] | 6644 | } else if (!isa<FunctionDecl>(D)) |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6645 | return false; |
| 6646 | |
Argyrios Kyrtzidis | ab411c8 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 6647 | // Weak references don't produce any output by themselves. |
| 6648 | if (D->hasAttr<WeakRefAttr>()) |
| 6649 | return false; |
| 6650 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6651 | // Aliases and used decls are required. |
| 6652 | if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>()) |
| 6653 | return true; |
| 6654 | |
| 6655 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 6656 | // Forward declarations aren't required. |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 6657 | if (!FD->doesThisDeclarationHaveABody()) |
Nick Lewycky | dce67a7 | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 6658 | return FD->doesDeclarationForceExternallyVisibleDefinition(); |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6659 | |
| 6660 | // Constructors and destructors are required. |
| 6661 | if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>()) |
| 6662 | return true; |
| 6663 | |
| 6664 | // The key function for a class is required. |
| 6665 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 6666 | const CXXRecordDecl *RD = MD->getParent(); |
| 6667 | if (MD->isOutOfLine() && RD->isDynamicClass()) { |
| 6668 | const CXXMethodDecl *KeyFunc = getKeyFunction(RD); |
| 6669 | if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl()) |
| 6670 | return true; |
| 6671 | } |
| 6672 | } |
| 6673 | |
| 6674 | GVALinkage Linkage = GetGVALinkageForFunction(FD); |
| 6675 | |
| 6676 | // static, static inline, always_inline, and extern inline functions can |
| 6677 | // always be deferred. Normal inline functions can be deferred in C99/C++. |
| 6678 | // Implicit template instantiations can also be deferred in C++. |
| 6679 | if (Linkage == GVA_Internal || Linkage == GVA_C99Inline || |
Anton Yartsev | 3a5aca8 | 2012-02-02 06:06:34 +0000 | [diff] [blame] | 6680 | Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6681 | return false; |
| 6682 | return true; |
| 6683 | } |
Douglas Gregor | 94da158 | 2011-09-10 00:22:34 +0000 | [diff] [blame] | 6684 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6685 | const VarDecl *VD = cast<VarDecl>(D); |
| 6686 | assert(VD->isFileVarDecl() && "Expected file scoped var"); |
| 6687 | |
Argyrios Kyrtzidis | ab411c8 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 6688 | if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly) |
| 6689 | return false; |
| 6690 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6691 | // Structs that have non-trivial constructors or destructors are required. |
| 6692 | |
| 6693 | // FIXME: Handle references. |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 6694 | // FIXME: Be more selective about which constructors we care about. |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6695 | if (const RecordType *RT = VD->getType()->getAs<RecordType>()) { |
| 6696 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 6697 | if (RD->hasDefinition() && !(RD->hasTrivialDefaultConstructor() && |
| 6698 | RD->hasTrivialCopyConstructor() && |
| 6699 | RD->hasTrivialMoveConstructor() && |
| 6700 | RD->hasTrivialDestructor())) |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6701 | return true; |
| 6702 | } |
| 6703 | } |
| 6704 | |
| 6705 | GVALinkage L = GetGVALinkageForVariable(VD); |
| 6706 | if (L == GVA_Internal || L == GVA_TemplateInstantiation) { |
| 6707 | if (!(VD->getInit() && VD->getInit()->HasSideEffects(*this))) |
| 6708 | return false; |
| 6709 | } |
| 6710 | |
| 6711 | return true; |
| 6712 | } |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 6713 | |
Charles Davis | ee743f9 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 6714 | CallingConv ASTContext::getDefaultMethodCallConv() { |
| 6715 | // Pass through to the C++ ABI object |
| 6716 | return ABI->getDefaultMethodCallConv(); |
| 6717 | } |
| 6718 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6719 | bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const { |
Anders Carlsson | dae0cb5 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 6720 | // Pass through to the C++ ABI object |
| 6721 | return ABI->isNearlyEmpty(RD); |
| 6722 | } |
| 6723 | |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 6724 | MangleContext *ASTContext::createMangleContext() { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 6725 | switch (Target->getCXXABI()) { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 6726 | case CXXABI_ARM: |
| 6727 | case CXXABI_Itanium: |
| 6728 | return createItaniumMangleContext(*this, getDiagnostics()); |
| 6729 | case CXXABI_Microsoft: |
| 6730 | return createMicrosoftMangleContext(*this, getDiagnostics()); |
| 6731 | } |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6732 | llvm_unreachable("Unsupported ABI"); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 6733 | } |
| 6734 | |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 6735 | CXXABI::~CXXABI() {} |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 6736 | |
| 6737 | size_t ASTContext::getSideTableAllocatedMemory() const { |
Ted Kremenek | 0c8cd1a | 2011-07-27 18:41:12 +0000 | [diff] [blame] | 6738 | return ASTRecordLayouts.getMemorySize() |
| 6739 | + llvm::capacity_in_bytes(ObjCLayouts) |
| 6740 | + llvm::capacity_in_bytes(KeyFunctions) |
| 6741 | + llvm::capacity_in_bytes(ObjCImpls) |
| 6742 | + llvm::capacity_in_bytes(BlockVarCopyInits) |
| 6743 | + llvm::capacity_in_bytes(DeclAttrs) |
| 6744 | + llvm::capacity_in_bytes(InstantiatedFromStaticDataMember) |
| 6745 | + llvm::capacity_in_bytes(InstantiatedFromUsingDecl) |
| 6746 | + llvm::capacity_in_bytes(InstantiatedFromUsingShadowDecl) |
| 6747 | + llvm::capacity_in_bytes(InstantiatedFromUnnamedFieldDecl) |
| 6748 | + llvm::capacity_in_bytes(OverriddenMethods) |
| 6749 | + llvm::capacity_in_bytes(Types) |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 6750 | + llvm::capacity_in_bytes(VariableArrayTypes) |
Francois Pichet | 0d95f0d | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 6751 | + llvm::capacity_in_bytes(ClassScopeSpecializationPattern); |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 6752 | } |
Ted Kremenek | d211cb7 | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 6753 | |
Douglas Gregor | 9e8c92a | 2012-02-20 19:44:39 +0000 | [diff] [blame] | 6754 | unsigned ASTContext::getLambdaManglingNumber(CXXMethodDecl *CallOperator) { |
| 6755 | CXXRecordDecl *Lambda = CallOperator->getParent(); |
| 6756 | return LambdaMangleContexts[Lambda->getDeclContext()] |
| 6757 | .getManglingNumber(CallOperator); |
| 6758 | } |
| 6759 | |
| 6760 | |
Ted Kremenek | d211cb7 | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 6761 | void ASTContext::setParameterIndex(const ParmVarDecl *D, unsigned int index) { |
| 6762 | ParamIndices[D] = index; |
| 6763 | } |
| 6764 | |
| 6765 | unsigned ASTContext::getParameterIndex(const ParmVarDecl *D) const { |
| 6766 | ParameterIndexTable::const_iterator I = ParamIndices.find(D); |
| 6767 | assert(I != ParamIndices.end() && |
| 6768 | "ParmIndices lacks entry set by ParmVarDecl"); |
| 6769 | return I->second; |
| 6770 | } |