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 |
Peter Collingbourne | 4dc34eb | 2012-05-20 21:08:35 +0000 | [diff] [blame] | 209 | 3, // opencl_constant |
| 210 | 4, // cuda_device |
| 211 | 5, // cuda_constant |
| 212 | 6 // cuda_shared |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 213 | }; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 214 | return &FakeAddrSpaceMap; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 215 | } else { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 216 | return &T.getAddressSpaceMap(); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
Douglas Gregor | 3e3cd93 | 2011-09-01 20:23:19 +0000 | [diff] [blame] | 220 | ASTContext::ASTContext(LangOptions& LOpts, SourceManager &SM, |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 221 | const TargetInfo *t, |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 222 | IdentifierTable &idents, SelectorTable &sels, |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 223 | Builtin::Context &builtins, |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 224 | unsigned size_reserve, |
| 225 | bool DelayInitialization) |
| 226 | : FunctionProtoTypes(this_()), |
| 227 | TemplateSpecializationTypes(this_()), |
| 228 | DependentTemplateSpecializationTypes(this_()), |
| 229 | SubstTemplateTemplateParmPacks(this_()), |
| 230 | GlobalNestedNameSpecifier(0), |
| 231 | Int128Decl(0), UInt128Decl(0), |
Douglas Gregor | a6ea10e | 2012-01-17 18:09:05 +0000 | [diff] [blame] | 232 | ObjCIdDecl(0), ObjCSelDecl(0), ObjCClassDecl(0), ObjCProtocolClassDecl(0), |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 233 | CFConstantStringTypeDecl(0), ObjCInstanceTypeDecl(0), |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 234 | FILEDecl(0), |
Rafael Espindola | e2d4f4e | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 235 | jmp_bufDecl(0), sigjmp_bufDecl(0), ucontext_tDecl(0), |
| 236 | BlockDescriptorType(0), BlockDescriptorExtendedType(0), |
| 237 | cudaConfigureCallDecl(0), |
Douglas Gregor | e664977 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 238 | NullTypeSourceInfo(QualType()), |
| 239 | FirstLocalImport(), LastLocalImport(), |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 240 | SourceMgr(SM), LangOpts(LOpts), |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 241 | AddrSpaceMap(0), Target(t), PrintingPolicy(LOpts), |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 242 | Idents(idents), Selectors(sels), |
| 243 | BuiltinInfo(builtins), |
| 244 | DeclarationNames(*this), |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 245 | ExternalSource(0), Listener(0), |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 246 | LastSDM(0, 0), |
| 247 | UniqueBlockByRefTypeID(0) |
| 248 | { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 249 | if (size_reserve > 0) Types.reserve(size_reserve); |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 250 | TUDecl = TranslationUnitDecl::Create(*this); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 251 | |
| 252 | if (!DelayInitialization) { |
| 253 | assert(t && "No target supplied for ASTContext initialization"); |
| 254 | InitBuiltinTypes(*t); |
| 255 | } |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 258 | ASTContext::~ASTContext() { |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 259 | // Release the DenseMaps associated with DeclContext objects. |
| 260 | // FIXME: Is this the ideal solution? |
| 261 | ReleaseDeclContextMaps(); |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 262 | |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 263 | // Call all of the deallocation functions. |
| 264 | for (unsigned I = 0, N = Deallocations.size(); I != N; ++I) |
| 265 | Deallocations[I].first(Deallocations[I].second); |
Douglas Gregor | 0054531 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 266 | |
Ted Kremenek | dcfcfbe | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 267 | // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 268 | // because they can contain DenseMaps. |
| 269 | for (llvm::DenseMap<const ObjCContainerDecl*, |
| 270 | const ASTRecordLayout*>::iterator |
| 271 | I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) |
| 272 | // Increment in loop to prevent using deallocated memory. |
| 273 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 274 | R->Destroy(*this); |
| 275 | |
Ted Kremenek | dcfcfbe | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 276 | for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator |
| 277 | I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) { |
| 278 | // Increment in loop to prevent using deallocated memory. |
| 279 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 280 | R->Destroy(*this); |
| 281 | } |
Douglas Gregor | 6320064 | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 282 | |
| 283 | for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(), |
| 284 | AEnd = DeclAttrs.end(); |
| 285 | A != AEnd; ++A) |
| 286 | A->second->~AttrVec(); |
| 287 | } |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 288 | |
Douglas Gregor | 0054531 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 289 | void ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) { |
| 290 | Deallocations.push_back(std::make_pair(Callback, Data)); |
| 291 | } |
| 292 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 293 | void |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 294 | ASTContext::setExternalSource(OwningPtr<ExternalASTSource> &Source) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 295 | ExternalSource.reset(Source.take()); |
| 296 | } |
| 297 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 298 | void ASTContext::PrintStats() const { |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 299 | llvm::errs() << "\n*** AST Context Stats:\n"; |
| 300 | llvm::errs() << " " << Types.size() << " types total.\n"; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 301 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 302 | unsigned counts[] = { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 303 | #define TYPE(Name, Parent) 0, |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 304 | #define ABSTRACT_TYPE(Name, Parent) |
| 305 | #include "clang/AST/TypeNodes.def" |
| 306 | 0 // Extra |
| 307 | }; |
Douglas Gregor | c2ee10d | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 308 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 309 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 310 | Type *T = Types[i]; |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 311 | counts[(unsigned)T->getTypeClass()]++; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 314 | unsigned Idx = 0; |
| 315 | unsigned TotalBytes = 0; |
| 316 | #define TYPE(Name, Parent) \ |
| 317 | if (counts[Idx]) \ |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 318 | llvm::errs() << " " << counts[Idx] << " " << #Name \ |
| 319 | << " types\n"; \ |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 320 | TotalBytes += counts[Idx] * sizeof(Name##Type); \ |
| 321 | ++Idx; |
| 322 | #define ABSTRACT_TYPE(Name, Parent) |
| 323 | #include "clang/AST/TypeNodes.def" |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 324 | |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 325 | llvm::errs() << "Total bytes = " << TotalBytes << "\n"; |
| 326 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 327 | // Implicit special member functions. |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 328 | llvm::errs() << NumImplicitDefaultConstructorsDeclared << "/" |
| 329 | << NumImplicitDefaultConstructors |
| 330 | << " implicit default constructors created\n"; |
| 331 | llvm::errs() << NumImplicitCopyConstructorsDeclared << "/" |
| 332 | << NumImplicitCopyConstructors |
| 333 | << " implicit copy constructors created\n"; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 334 | if (getLangOpts().CPlusPlus) |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 335 | llvm::errs() << NumImplicitMoveConstructorsDeclared << "/" |
| 336 | << NumImplicitMoveConstructors |
| 337 | << " implicit move constructors created\n"; |
| 338 | llvm::errs() << NumImplicitCopyAssignmentOperatorsDeclared << "/" |
| 339 | << NumImplicitCopyAssignmentOperators |
| 340 | << " implicit copy assignment operators created\n"; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 341 | if (getLangOpts().CPlusPlus) |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 342 | llvm::errs() << NumImplicitMoveAssignmentOperatorsDeclared << "/" |
| 343 | << NumImplicitMoveAssignmentOperators |
| 344 | << " implicit move assignment operators created\n"; |
| 345 | llvm::errs() << NumImplicitDestructorsDeclared << "/" |
| 346 | << NumImplicitDestructors |
| 347 | << " implicit destructors created\n"; |
| 348 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 349 | if (ExternalSource.get()) { |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 350 | llvm::errs() << "\n"; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 351 | ExternalSource->PrintStats(); |
| 352 | } |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 353 | |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 354 | BumpAlloc.PrintStats(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Douglas Gregor | 772eeae | 2011-08-12 06:49:56 +0000 | [diff] [blame] | 357 | TypedefDecl *ASTContext::getInt128Decl() const { |
| 358 | if (!Int128Decl) { |
| 359 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(Int128Ty); |
| 360 | Int128Decl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 361 | getTranslationUnitDecl(), |
| 362 | SourceLocation(), |
| 363 | SourceLocation(), |
| 364 | &Idents.get("__int128_t"), |
| 365 | TInfo); |
| 366 | } |
| 367 | |
| 368 | return Int128Decl; |
| 369 | } |
| 370 | |
| 371 | TypedefDecl *ASTContext::getUInt128Decl() const { |
| 372 | if (!UInt128Decl) { |
| 373 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(UnsignedInt128Ty); |
| 374 | UInt128Decl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 375 | getTranslationUnitDecl(), |
| 376 | SourceLocation(), |
| 377 | SourceLocation(), |
| 378 | &Idents.get("__uint128_t"), |
| 379 | TInfo); |
| 380 | } |
| 381 | |
| 382 | return UInt128Decl; |
| 383 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 384 | |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 385 | void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 386 | BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 387 | R = CanQualType::CreateUnsafe(QualType(Ty, 0)); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 388 | Types.push_back(Ty); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 391 | void ASTContext::InitBuiltinTypes(const TargetInfo &Target) { |
| 392 | assert((!this->Target || this->Target == &Target) && |
| 393 | "Incorrect target reinitialization"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 394 | assert(VoidTy.isNull() && "Context reinitialized?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 395 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 396 | this->Target = &Target; |
| 397 | |
| 398 | ABI.reset(createCXXABI(Target)); |
| 399 | AddrSpaceMap = getAddressSpaceMap(Target, LangOpts); |
| 400 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 401 | // C99 6.2.5p19. |
| 402 | InitBuiltinType(VoidTy, BuiltinType::Void); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 403 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 404 | // C99 6.2.5p2. |
| 405 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
| 406 | // C99 6.2.5p3. |
Eli Friedman | 15b9176 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 407 | if (LangOpts.CharIsSigned) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 408 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 409 | else |
| 410 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
| 411 | // C99 6.2.5p4. |
| 412 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 413 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 414 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 415 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 416 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 417 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 418 | // C99 6.2.5p6. |
| 419 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 420 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 421 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 422 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 423 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 424 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 425 | // C99 6.2.5p10. |
| 426 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 427 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 428 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 429 | |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 430 | // GNU extension, 128-bit integers. |
| 431 | InitBuiltinType(Int128Ty, BuiltinType::Int128); |
| 432 | InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128); |
| 433 | |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 434 | if (LangOpts.CPlusPlus) { // C++ 3.9.1p5 |
Eli Friedman | d3d77cd | 2011-04-30 19:24:24 +0000 | [diff] [blame] | 435 | if (TargetInfo::isTypeSigned(Target.getWCharType())) |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 436 | InitBuiltinType(WCharTy, BuiltinType::WChar_S); |
| 437 | else // -fshort-wchar makes wchar_t be unsigned. |
| 438 | InitBuiltinType(WCharTy, BuiltinType::WChar_U); |
| 439 | } else // C99 |
Chris Lattner | 3a25032 | 2009-02-26 23:43:47 +0000 | [diff] [blame] | 440 | WCharTy = getFromTargetType(Target.getWCharType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 441 | |
James Molloy | 392da48 | 2012-05-04 10:55:22 +0000 | [diff] [blame] | 442 | WIntTy = getFromTargetType(Target.getWIntType()); |
| 443 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 444 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 445 | InitBuiltinType(Char16Ty, BuiltinType::Char16); |
| 446 | else // C99 |
| 447 | Char16Ty = getFromTargetType(Target.getChar16Type()); |
| 448 | |
| 449 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 450 | InitBuiltinType(Char32Ty, BuiltinType::Char32); |
| 451 | else // C99 |
| 452 | Char32Ty = getFromTargetType(Target.getChar32Type()); |
| 453 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 454 | // Placeholder type for type-dependent expressions whose type is |
| 455 | // completely unknown. No code should ever check a type against |
| 456 | // DependentTy and users should never see it; however, it is here to |
| 457 | // help diagnose failures to properly check for type-dependent |
| 458 | // expressions. |
| 459 | InitBuiltinType(DependentTy, BuiltinType::Dependent); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 460 | |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 461 | // Placeholder type for functions. |
| 462 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
| 463 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 464 | // Placeholder type for bound members. |
| 465 | InitBuiltinType(BoundMemberTy, BuiltinType::BoundMember); |
| 466 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 467 | // Placeholder type for pseudo-objects. |
| 468 | InitBuiltinType(PseudoObjectTy, BuiltinType::PseudoObject); |
| 469 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 470 | // "any" type; useful for debugger-like clients. |
| 471 | InitBuiltinType(UnknownAnyTy, BuiltinType::UnknownAny); |
| 472 | |
John McCall | 0ddaeb9 | 2011-10-17 18:09:15 +0000 | [diff] [blame] | 473 | // Placeholder type for unbridged ARC casts. |
| 474 | InitBuiltinType(ARCUnbridgedCastTy, BuiltinType::ARCUnbridgedCast); |
| 475 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 476 | // C99 6.2.5p11. |
| 477 | FloatComplexTy = getComplexType(FloatTy); |
| 478 | DoubleComplexTy = getComplexType(DoubleTy); |
| 479 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 480 | |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 481 | BuiltinVaListType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 482 | |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 483 | // Builtin types for 'id', 'Class', and 'SEL'. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 484 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); |
| 485 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 486 | InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 487 | |
| 488 | // Builtin type for __objc_yes and __objc_no |
Fariborz Jahanian | 93a4994 | 2012-04-16 21:03:30 +0000 | [diff] [blame] | 489 | ObjCBuiltinBoolTy = (Target.useSignedCharForObjCBool() ? |
| 490 | SignedCharTy : BoolTy); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 491 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 492 | ObjCConstantStringType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 493 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 494 | // void * type |
| 495 | VoidPtrTy = getPointerType(VoidTy); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 496 | |
| 497 | // nullptr type (C++0x 2.14.7) |
| 498 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 499 | |
| 500 | // half type (OpenCL 6.1.1.1) / ARM NEON __fp16 |
| 501 | InitBuiltinType(HalfTy, BuiltinType::Half); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 502 | } |
| 503 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 504 | DiagnosticsEngine &ASTContext::getDiagnostics() const { |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 505 | return SourceMgr.getDiagnostics(); |
| 506 | } |
| 507 | |
Douglas Gregor | 6320064 | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 508 | AttrVec& ASTContext::getDeclAttrs(const Decl *D) { |
| 509 | AttrVec *&Result = DeclAttrs[D]; |
| 510 | if (!Result) { |
| 511 | void *Mem = Allocate(sizeof(AttrVec)); |
| 512 | Result = new (Mem) AttrVec; |
| 513 | } |
| 514 | |
| 515 | return *Result; |
| 516 | } |
| 517 | |
| 518 | /// \brief Erase the attributes corresponding to the given declaration. |
| 519 | void ASTContext::eraseDeclAttrs(const Decl *D) { |
| 520 | llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D); |
| 521 | if (Pos != DeclAttrs.end()) { |
| 522 | Pos->second->~AttrVec(); |
| 523 | DeclAttrs.erase(Pos); |
| 524 | } |
| 525 | } |
| 526 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 527 | MemberSpecializationInfo * |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 528 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 529 | assert(Var->isStaticDataMember() && "Not a static data member"); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 530 | llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 531 | = InstantiatedFromStaticDataMember.find(Var); |
| 532 | if (Pos == InstantiatedFromStaticDataMember.end()) |
| 533 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 534 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 535 | return Pos->second; |
| 536 | } |
| 537 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 538 | void |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 539 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, |
Argyrios Kyrtzidis | 9421adc | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 540 | TemplateSpecializationKind TSK, |
| 541 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 542 | assert(Inst->isStaticDataMember() && "Not a static data member"); |
| 543 | assert(Tmpl->isStaticDataMember() && "Not a static data member"); |
| 544 | assert(!InstantiatedFromStaticDataMember[Inst] && |
| 545 | "Already noted what static data member was instantiated from"); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 546 | InstantiatedFromStaticDataMember[Inst] |
Argyrios Kyrtzidis | 9421adc | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 547 | = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 550 | FunctionDecl *ASTContext::getClassScopeSpecializationPattern( |
| 551 | const FunctionDecl *FD){ |
| 552 | assert(FD && "Specialization is 0"); |
| 553 | llvm::DenseMap<const FunctionDecl*, FunctionDecl *>::const_iterator Pos |
Francois Pichet | 0d95f0d | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 554 | = ClassScopeSpecializationPattern.find(FD); |
| 555 | if (Pos == ClassScopeSpecializationPattern.end()) |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 556 | return 0; |
| 557 | |
| 558 | return Pos->second; |
| 559 | } |
| 560 | |
| 561 | void ASTContext::setClassScopeSpecializationPattern(FunctionDecl *FD, |
| 562 | FunctionDecl *Pattern) { |
| 563 | assert(FD && "Specialization is 0"); |
| 564 | assert(Pattern && "Class scope specialization pattern is 0"); |
Francois Pichet | 0d95f0d | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 565 | ClassScopeSpecializationPattern[FD] = Pattern; |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 566 | } |
| 567 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 568 | NamedDecl * |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 569 | ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 570 | llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 571 | = InstantiatedFromUsingDecl.find(UUD); |
| 572 | if (Pos == InstantiatedFromUsingDecl.end()) |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 573 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 574 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 575 | return Pos->second; |
| 576 | } |
| 577 | |
| 578 | void |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 579 | ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) { |
| 580 | assert((isa<UsingDecl>(Pattern) || |
| 581 | isa<UnresolvedUsingValueDecl>(Pattern) || |
| 582 | isa<UnresolvedUsingTypenameDecl>(Pattern)) && |
| 583 | "pattern decl is not a using decl"); |
| 584 | assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists"); |
| 585 | InstantiatedFromUsingDecl[Inst] = Pattern; |
| 586 | } |
| 587 | |
| 588 | UsingShadowDecl * |
| 589 | ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) { |
| 590 | llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos |
| 591 | = InstantiatedFromUsingShadowDecl.find(Inst); |
| 592 | if (Pos == InstantiatedFromUsingShadowDecl.end()) |
| 593 | return 0; |
| 594 | |
| 595 | return Pos->second; |
| 596 | } |
| 597 | |
| 598 | void |
| 599 | ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, |
| 600 | UsingShadowDecl *Pattern) { |
| 601 | assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists"); |
| 602 | InstantiatedFromUsingShadowDecl[Inst] = Pattern; |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 605 | FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { |
| 606 | llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos |
| 607 | = InstantiatedFromUnnamedFieldDecl.find(Field); |
| 608 | if (Pos == InstantiatedFromUnnamedFieldDecl.end()) |
| 609 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 610 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 611 | return Pos->second; |
| 612 | } |
| 613 | |
| 614 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, |
| 615 | FieldDecl *Tmpl) { |
| 616 | assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed"); |
| 617 | assert(!Tmpl->getDeclName() && "Template field decl is not unnamed"); |
| 618 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] && |
| 619 | "Already noted what unnamed field was instantiated from"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 620 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 621 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; |
| 622 | } |
| 623 | |
Fariborz Jahanian | 14d56ef | 2011-04-27 17:14:21 +0000 | [diff] [blame] | 624 | bool ASTContext::ZeroBitfieldFollowsNonBitfield(const FieldDecl *FD, |
| 625 | const FieldDecl *LastFD) const { |
| 626 | return (FD->isBitField() && LastFD && !LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 627 | FD->getBitWidthValue(*this) == 0); |
Fariborz Jahanian | 14d56ef | 2011-04-27 17:14:21 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Fariborz Jahanian | 340fa24 | 2011-05-02 17:20:56 +0000 | [diff] [blame] | 630 | bool ASTContext::ZeroBitfieldFollowsBitfield(const FieldDecl *FD, |
| 631 | const FieldDecl *LastFD) const { |
| 632 | return (FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 633 | FD->getBitWidthValue(*this) == 0 && |
| 634 | LastFD->getBitWidthValue(*this) != 0); |
Fariborz Jahanian | 340fa24 | 2011-05-02 17:20:56 +0000 | [diff] [blame] | 635 | } |
| 636 | |
Fariborz Jahanian | 9b3acaa | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 637 | bool ASTContext::BitfieldFollowsBitfield(const FieldDecl *FD, |
| 638 | const FieldDecl *LastFD) const { |
| 639 | return (FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 640 | FD->getBitWidthValue(*this) && |
| 641 | LastFD->getBitWidthValue(*this)); |
Fariborz Jahanian | 9b3acaa | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 642 | } |
| 643 | |
Chad Rosier | dd7fddb | 2011-08-04 23:34:15 +0000 | [diff] [blame] | 644 | bool ASTContext::NonBitfieldFollowsBitfield(const FieldDecl *FD, |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 645 | const FieldDecl *LastFD) const { |
| 646 | return (!FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 647 | LastFD->getBitWidthValue(*this)); |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 648 | } |
| 649 | |
Chad Rosier | dd7fddb | 2011-08-04 23:34:15 +0000 | [diff] [blame] | 650 | bool ASTContext::BitfieldFollowsNonBitfield(const FieldDecl *FD, |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 651 | const FieldDecl *LastFD) const { |
| 652 | return (FD->isBitField() && LastFD && !LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 653 | FD->getBitWidthValue(*this)); |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 654 | } |
| 655 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 656 | ASTContext::overridden_cxx_method_iterator |
| 657 | ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const { |
| 658 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 659 | = OverriddenMethods.find(Method); |
| 660 | if (Pos == OverriddenMethods.end()) |
| 661 | return 0; |
| 662 | |
| 663 | return Pos->second.begin(); |
| 664 | } |
| 665 | |
| 666 | ASTContext::overridden_cxx_method_iterator |
| 667 | ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const { |
| 668 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 669 | = OverriddenMethods.find(Method); |
| 670 | if (Pos == OverriddenMethods.end()) |
| 671 | return 0; |
| 672 | |
| 673 | return Pos->second.end(); |
| 674 | } |
| 675 | |
Argyrios Kyrtzidis | c91e9f4 | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 676 | unsigned |
| 677 | ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const { |
| 678 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 679 | = OverriddenMethods.find(Method); |
| 680 | if (Pos == OverriddenMethods.end()) |
| 681 | return 0; |
| 682 | |
| 683 | return Pos->second.size(); |
| 684 | } |
| 685 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 686 | void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method, |
| 687 | const CXXMethodDecl *Overridden) { |
| 688 | OverriddenMethods[Method].push_back(Overridden); |
| 689 | } |
| 690 | |
Douglas Gregor | e664977 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 691 | void ASTContext::addedLocalImportDecl(ImportDecl *Import) { |
| 692 | assert(!Import->NextLocalImport && "Import declaration already in the chain"); |
| 693 | assert(!Import->isFromASTFile() && "Non-local import declaration"); |
| 694 | if (!FirstLocalImport) { |
| 695 | FirstLocalImport = Import; |
| 696 | LastLocalImport = Import; |
| 697 | return; |
| 698 | } |
| 699 | |
| 700 | LastLocalImport->NextLocalImport = Import; |
| 701 | LastLocalImport = Import; |
| 702 | } |
| 703 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 704 | //===----------------------------------------------------------------------===// |
| 705 | // Type Sizing and Analysis |
| 706 | //===----------------------------------------------------------------------===// |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 707 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 708 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 709 | /// scalar floating point type. |
| 710 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 711 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 712 | assert(BT && "Not a floating point type!"); |
| 713 | switch (BT->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 714 | default: llvm_unreachable("Not a floating point type!"); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 715 | case BuiltinType::Half: return Target->getHalfFormat(); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 716 | case BuiltinType::Float: return Target->getFloatFormat(); |
| 717 | case BuiltinType::Double: return Target->getDoubleFormat(); |
| 718 | case BuiltinType::LongDouble: return Target->getLongDoubleFormat(); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 722 | /// getDeclAlign - Return a conservative estimate of the alignment of the |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 723 | /// specified decl. Note that bitfields do not have a valid alignment, so |
| 724 | /// this method will assert on them. |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 725 | /// If @p RefAsPointee, references are treated like their underlying type |
| 726 | /// (for alignof), else they're treated like pointers (for CodeGen). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 727 | CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) const { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 728 | unsigned Align = Target->getCharWidth(); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 729 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 730 | bool UseAlignAttrOnly = false; |
| 731 | if (unsigned AlignFromAttr = D->getMaxAlignment()) { |
| 732 | Align = AlignFromAttr; |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 733 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 734 | // __attribute__((aligned)) can increase or decrease alignment |
| 735 | // *except* on a struct or struct member, where it only increases |
| 736 | // alignment unless 'packed' is also specified. |
| 737 | // |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 738 | // It is an error for alignas to decrease alignment, so we can |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 739 | // ignore that possibility; Sema should diagnose it. |
| 740 | if (isa<FieldDecl>(D)) { |
| 741 | UseAlignAttrOnly = D->hasAttr<PackedAttr>() || |
| 742 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
| 743 | } else { |
| 744 | UseAlignAttrOnly = true; |
| 745 | } |
| 746 | } |
Fariborz Jahanian | 78a7d7d | 2011-05-05 21:19:14 +0000 | [diff] [blame] | 747 | else if (isa<FieldDecl>(D)) |
| 748 | UseAlignAttrOnly = |
| 749 | D->hasAttr<PackedAttr>() || |
| 750 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 751 | |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 752 | // If we're using the align attribute only, just ignore everything |
| 753 | // else about the declaration and its type. |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 754 | if (UseAlignAttrOnly) { |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 755 | // do nothing |
| 756 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 757 | } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 758 | QualType T = VD->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 759 | if (const ReferenceType* RT = T->getAs<ReferenceType>()) { |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 760 | if (RefAsPointee) |
| 761 | T = RT->getPointeeType(); |
| 762 | else |
| 763 | T = getPointerType(RT->getPointeeType()); |
| 764 | } |
| 765 | if (!T->isIncompleteType() && !T->isFunctionType()) { |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 766 | // Adjust alignments of declarations with array type by the |
| 767 | // large-array alignment on the target. |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 768 | unsigned MinWidth = Target->getLargeArrayMinWidth(); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 769 | const ArrayType *arrayType; |
| 770 | if (MinWidth && (arrayType = getAsArrayType(T))) { |
| 771 | if (isa<VariableArrayType>(arrayType)) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 772 | Align = std::max(Align, Target->getLargeArrayAlign()); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 773 | else if (isa<ConstantArrayType>(arrayType) && |
| 774 | MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType))) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 775 | Align = std::max(Align, Target->getLargeArrayAlign()); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 776 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 777 | // Walk through any array types while we're at it. |
| 778 | T = getBaseElementType(arrayType); |
| 779 | } |
Chad Rosier | 9f1210c | 2011-07-26 07:03:04 +0000 | [diff] [blame] | 780 | Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 781 | } |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 782 | |
| 783 | // Fields can be subject to extra alignment constraints, like if |
| 784 | // the field is packed, the struct is packed, or the struct has a |
| 785 | // a max-field-alignment constraint (#pragma pack). So calculate |
| 786 | // the actual alignment of the field within the struct, and then |
| 787 | // (as we're expected to) constrain that by the alignment of the type. |
| 788 | if (const FieldDecl *field = dyn_cast<FieldDecl>(VD)) { |
| 789 | // So calculate the alignment of the field. |
| 790 | const ASTRecordLayout &layout = getASTRecordLayout(field->getParent()); |
| 791 | |
| 792 | // Start with the record's overall alignment. |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 793 | unsigned fieldAlign = toBits(layout.getAlignment()); |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 794 | |
| 795 | // Use the GCD of that and the offset within the record. |
| 796 | uint64_t offset = layout.getFieldOffset(field->getFieldIndex()); |
| 797 | if (offset > 0) { |
| 798 | // Alignment is always a power of 2, so the GCD will be a power of 2, |
| 799 | // which means we get to do this crazy thing instead of Euclid's. |
| 800 | uint64_t lowBitOfOffset = offset & (~offset + 1); |
| 801 | if (lowBitOfOffset < fieldAlign) |
| 802 | fieldAlign = static_cast<unsigned>(lowBitOfOffset); |
| 803 | } |
| 804 | |
| 805 | Align = std::min(Align, fieldAlign); |
Charles Davis | 05f6247 | 2010-02-23 04:52:00 +0000 | [diff] [blame] | 806 | } |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 807 | } |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 808 | |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 809 | return toCharUnitsFromBits(Align); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 810 | } |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 811 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 812 | std::pair<CharUnits, CharUnits> |
Ken Dyck | bee5a79 | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 813 | ASTContext::getTypeInfoInChars(const Type *T) const { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 814 | std::pair<uint64_t, unsigned> Info = getTypeInfo(T); |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 815 | return std::make_pair(toCharUnitsFromBits(Info.first), |
| 816 | toCharUnitsFromBits(Info.second)); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | std::pair<CharUnits, CharUnits> |
Ken Dyck | bee5a79 | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 820 | ASTContext::getTypeInfoInChars(QualType T) const { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 821 | return getTypeInfoInChars(T.getTypePtr()); |
| 822 | } |
| 823 | |
Daniel Dunbar | bc5419a | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 824 | std::pair<uint64_t, unsigned> ASTContext::getTypeInfo(const Type *T) const { |
| 825 | TypeInfoMap::iterator it = MemoizedTypeInfo.find(T); |
| 826 | if (it != MemoizedTypeInfo.end()) |
| 827 | return it->second; |
| 828 | |
| 829 | std::pair<uint64_t, unsigned> Info = getTypeInfoImpl(T); |
| 830 | MemoizedTypeInfo.insert(std::make_pair(T, Info)); |
| 831 | return Info; |
| 832 | } |
| 833 | |
| 834 | /// getTypeInfoImpl - Return the size of the specified type, in bits. This |
| 835 | /// method does not work on incomplete types. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 836 | /// |
| 837 | /// FIXME: Pointers into different addr spaces could have different sizes and |
| 838 | /// alignment requirements: getPointerInfo should take an AddrSpace, this |
| 839 | /// should take a QualType, &c. |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 840 | std::pair<uint64_t, unsigned> |
Daniel Dunbar | bc5419a | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 841 | ASTContext::getTypeInfoImpl(const Type *T) const { |
Mike Stump | 5e30100 | 2009-02-27 18:32:39 +0000 | [diff] [blame] | 842 | uint64_t Width=0; |
| 843 | unsigned Align=8; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 844 | switch (T->getTypeClass()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 845 | #define TYPE(Class, Base) |
| 846 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 847 | #define NON_CANONICAL_TYPE(Class, Base) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 848 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 849 | #include "clang/AST/TypeNodes.def" |
John McCall | d3d49bb | 2011-06-28 16:49:23 +0000 | [diff] [blame] | 850 | llvm_unreachable("Should not see dependent types"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 851 | |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 852 | case Type::FunctionNoProto: |
| 853 | case Type::FunctionProto: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 854 | // GCC extension: alignof(function) = 32 bits |
| 855 | Width = 0; |
| 856 | Align = 32; |
| 857 | break; |
| 858 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 859 | case Type::IncompleteArray: |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 860 | case Type::VariableArray: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 861 | Width = 0; |
| 862 | Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); |
| 863 | break; |
| 864 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 865 | case Type::ConstantArray: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 866 | const ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 867 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 868 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); |
Abramo Bagnara | fea966a | 2011-12-13 11:23:52 +0000 | [diff] [blame] | 869 | uint64_t Size = CAT->getSize().getZExtValue(); |
Daniel Dunbar | bc5419a | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 870 | assert((Size == 0 || EltInfo.first <= (uint64_t)(-1)/Size) && |
| 871 | "Overflow in array type bit size evaluation"); |
Abramo Bagnara | fea966a | 2011-12-13 11:23:52 +0000 | [diff] [blame] | 872 | Width = EltInfo.first*Size; |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 873 | Align = EltInfo.second; |
Argyrios Kyrtzidis | cd88b41 | 2011-04-26 21:05:39 +0000 | [diff] [blame] | 874 | Width = llvm::RoundUpToAlignment(Width, Align); |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 875 | break; |
Christopher Lamb | 5c09a02 | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 876 | } |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 877 | case Type::ExtVector: |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 878 | case Type::Vector: { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 879 | const VectorType *VT = cast<VectorType>(T); |
| 880 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType()); |
| 881 | Width = EltInfo.first*VT->getNumElements(); |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 882 | Align = Width; |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 883 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 884 | // This happens for non-power-of-2 length vectors. |
Dan Gohman | 8eefcd3 | 2010-04-21 23:32:43 +0000 | [diff] [blame] | 885 | if (Align & (Align-1)) { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 886 | Align = llvm::NextPowerOf2(Align); |
| 887 | Width = llvm::RoundUpToAlignment(Width, Align); |
| 888 | } |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 889 | break; |
| 890 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 891 | |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 892 | case Type::Builtin: |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 893 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 894 | default: llvm_unreachable("Unknown builtin type!"); |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 895 | case BuiltinType::Void: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 896 | // GCC extension: alignof(void) = 8 bits. |
| 897 | Width = 0; |
| 898 | Align = 8; |
| 899 | break; |
| 900 | |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 901 | case BuiltinType::Bool: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 902 | Width = Target->getBoolWidth(); |
| 903 | Align = Target->getBoolAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 904 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 905 | case BuiltinType::Char_S: |
| 906 | case BuiltinType::Char_U: |
| 907 | case BuiltinType::UChar: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 908 | case BuiltinType::SChar: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 909 | Width = Target->getCharWidth(); |
| 910 | Align = Target->getCharAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 911 | break; |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 912 | case BuiltinType::WChar_S: |
| 913 | case BuiltinType::WChar_U: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 914 | Width = Target->getWCharWidth(); |
| 915 | Align = Target->getWCharAlign(); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 916 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 917 | case BuiltinType::Char16: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 918 | Width = Target->getChar16Width(); |
| 919 | Align = Target->getChar16Align(); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 920 | break; |
| 921 | case BuiltinType::Char32: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 922 | Width = Target->getChar32Width(); |
| 923 | Align = Target->getChar32Align(); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 924 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 925 | case BuiltinType::UShort: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 926 | case BuiltinType::Short: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 927 | Width = Target->getShortWidth(); |
| 928 | Align = Target->getShortAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 929 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 930 | case BuiltinType::UInt: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 931 | case BuiltinType::Int: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 932 | Width = Target->getIntWidth(); |
| 933 | Align = Target->getIntAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 934 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 935 | case BuiltinType::ULong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 936 | case BuiltinType::Long: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 937 | Width = Target->getLongWidth(); |
| 938 | Align = Target->getLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 939 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 940 | case BuiltinType::ULongLong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 941 | case BuiltinType::LongLong: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 942 | Width = Target->getLongLongWidth(); |
| 943 | Align = Target->getLongLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 944 | break; |
Chris Lattner | ec16cb9 | 2009-04-30 02:55:13 +0000 | [diff] [blame] | 945 | case BuiltinType::Int128: |
| 946 | case BuiltinType::UInt128: |
| 947 | Width = 128; |
| 948 | Align = 128; // int128_t is 128-bit aligned on all targets. |
| 949 | break; |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 950 | case BuiltinType::Half: |
| 951 | Width = Target->getHalfWidth(); |
| 952 | Align = Target->getHalfAlign(); |
| 953 | break; |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 954 | case BuiltinType::Float: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 955 | Width = Target->getFloatWidth(); |
| 956 | Align = Target->getFloatAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 957 | break; |
| 958 | case BuiltinType::Double: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 959 | Width = Target->getDoubleWidth(); |
| 960 | Align = Target->getDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 961 | break; |
| 962 | case BuiltinType::LongDouble: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 963 | Width = Target->getLongDoubleWidth(); |
| 964 | Align = Target->getLongDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 965 | break; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 966 | case BuiltinType::NullPtr: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 967 | Width = Target->getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) |
| 968 | Align = Target->getPointerAlign(0); // == sizeof(void*) |
Sebastian Redl | 1590d9c | 2009-05-27 19:34:06 +0000 | [diff] [blame] | 969 | break; |
Fariborz Jahanian | e04f5fc | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 970 | case BuiltinType::ObjCId: |
| 971 | case BuiltinType::ObjCClass: |
| 972 | case BuiltinType::ObjCSel: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 973 | Width = Target->getPointerWidth(0); |
| 974 | Align = Target->getPointerAlign(0); |
Fariborz Jahanian | e04f5fc | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 975 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 976 | } |
Chris Lattner | bfef6d7 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 977 | break; |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 978 | case Type::ObjCObjectPointer: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 979 | Width = Target->getPointerWidth(0); |
| 980 | Align = Target->getPointerAlign(0); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 981 | break; |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 982 | case Type::BlockPointer: { |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 983 | unsigned AS = getTargetAddressSpace( |
| 984 | cast<BlockPointerType>(T)->getPointeeType()); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 985 | Width = Target->getPointerWidth(AS); |
| 986 | Align = Target->getPointerAlign(AS); |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 987 | break; |
| 988 | } |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 989 | case Type::LValueReference: |
| 990 | case Type::RValueReference: { |
| 991 | // alignof and sizeof should never enter this code path here, so we go |
| 992 | // the pointer route. |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 993 | unsigned AS = getTargetAddressSpace( |
| 994 | cast<ReferenceType>(T)->getPointeeType()); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 995 | Width = Target->getPointerWidth(AS); |
| 996 | Align = Target->getPointerAlign(AS); |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 997 | break; |
| 998 | } |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 999 | case Type::Pointer: { |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1000 | unsigned AS = getTargetAddressSpace(cast<PointerType>(T)->getPointeeType()); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1001 | Width = Target->getPointerWidth(AS); |
| 1002 | Align = Target->getPointerAlign(AS); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 1003 | break; |
| 1004 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1005 | case Type::MemberPointer: { |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 1006 | const MemberPointerType *MPT = cast<MemberPointerType>(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1007 | std::pair<uint64_t, unsigned> PtrDiffInfo = |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 1008 | getTypeInfo(getPointerDiffType()); |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 1009 | Width = PtrDiffInfo.first * ABI->getMemberPointerSize(MPT); |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 1010 | Align = PtrDiffInfo.second; |
| 1011 | break; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1012 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1013 | case Type::Complex: { |
| 1014 | // Complex types have the same alignment as their elements, but twice the |
| 1015 | // size. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1016 | std::pair<uint64_t, unsigned> EltInfo = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1017 | getTypeInfo(cast<ComplexType>(T)->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1018 | Width = EltInfo.first*2; |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1019 | Align = EltInfo.second; |
| 1020 | break; |
| 1021 | } |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1022 | case Type::ObjCObject: |
| 1023 | return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr()); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1024 | case Type::ObjCInterface: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1025 | const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1026 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1027 | Width = toBits(Layout.getSize()); |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 1028 | Align = toBits(Layout.getAlignment()); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1029 | break; |
| 1030 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1031 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1032 | case Type::Enum: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1033 | const TagType *TT = cast<TagType>(T); |
| 1034 | |
| 1035 | if (TT->getDecl()->isInvalidDecl()) { |
Douglas Gregor | 22ce41d | 2011-04-20 17:29:44 +0000 | [diff] [blame] | 1036 | Width = 8; |
| 1037 | Align = 8; |
Chris Lattner | 8389eab | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 1038 | break; |
| 1039 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1040 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1041 | if (const EnumType *ET = dyn_cast<EnumType>(TT)) |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1042 | return getTypeInfo(ET->getDecl()->getIntegerType()); |
| 1043 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1044 | const RecordType *RT = cast<RecordType>(TT); |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1045 | const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1046 | Width = toBits(Layout.getSize()); |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 1047 | Align = toBits(Layout.getAlignment()); |
Chris Lattner | dc0d73e | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 1048 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1049 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1050 | |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1051 | case Type::SubstTemplateTypeParm: |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1052 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> |
| 1053 | getReplacementType().getTypePtr()); |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1054 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1055 | case Type::Auto: { |
| 1056 | const AutoType *A = cast<AutoType>(T); |
| 1057 | assert(A->isDeduced() && "Cannot request the size of a dependent type"); |
Matt Beaumont-Gay | dc856af | 2011-02-22 20:00:16 +0000 | [diff] [blame] | 1058 | return getTypeInfo(A->getDeducedType().getTypePtr()); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1061 | case Type::Paren: |
| 1062 | return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr()); |
| 1063 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1064 | case Type::Typedef: { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1065 | const TypedefNameDecl *Typedef = cast<TypedefType>(T)->getDecl(); |
Douglas Gregor | df1367a | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 1066 | std::pair<uint64_t, unsigned> Info |
| 1067 | = getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); |
Chris Lattner | c1de52d | 2011-02-19 22:55:41 +0000 | [diff] [blame] | 1068 | // If the typedef has an aligned attribute on it, it overrides any computed |
| 1069 | // alignment we have. This violates the GCC documentation (which says that |
| 1070 | // attribute(aligned) can only round up) but matches its implementation. |
| 1071 | if (unsigned AttrAlign = Typedef->getMaxAlignment()) |
| 1072 | Align = AttrAlign; |
| 1073 | else |
| 1074 | Align = Info.second; |
Douglas Gregor | df1367a | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 1075 | Width = Info.first; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1076 | break; |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1077 | } |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1078 | |
| 1079 | case Type::TypeOfExpr: |
| 1080 | return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType() |
| 1081 | .getTypePtr()); |
| 1082 | |
| 1083 | case Type::TypeOf: |
| 1084 | return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr()); |
| 1085 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 1086 | case Type::Decltype: |
| 1087 | return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType() |
| 1088 | .getTypePtr()); |
| 1089 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1090 | case Type::UnaryTransform: |
| 1091 | return getTypeInfo(cast<UnaryTransformType>(T)->getUnderlyingType()); |
| 1092 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1093 | case Type::Elaborated: |
| 1094 | return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1095 | |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 1096 | case Type::Attributed: |
| 1097 | return getTypeInfo( |
| 1098 | cast<AttributedType>(T)->getEquivalentType().getTypePtr()); |
| 1099 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1100 | case Type::TemplateSpecialization: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1101 | assert(getCanonicalType(T) != T && |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1102 | "Cannot request the size of a dependent type"); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1103 | const TemplateSpecializationType *TST = cast<TemplateSpecializationType>(T); |
| 1104 | // A type alias template specialization may refer to a typedef with the |
| 1105 | // aligned attribute on it. |
| 1106 | if (TST->isTypeAlias()) |
| 1107 | return getTypeInfo(TST->getAliasedType().getTypePtr()); |
| 1108 | else |
| 1109 | return getTypeInfo(getCanonicalType(T)); |
| 1110 | } |
| 1111 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1112 | case Type::Atomic: { |
Eli Friedman | 2be4607 | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 1113 | std::pair<uint64_t, unsigned> Info |
| 1114 | = getTypeInfo(cast<AtomicType>(T)->getValueType()); |
| 1115 | Width = Info.first; |
| 1116 | Align = Info.second; |
| 1117 | if (Width != 0 && Width <= Target->getMaxAtomicPromoteWidth() && |
| 1118 | llvm::isPowerOf2_64(Width)) { |
| 1119 | // We can potentially perform lock-free atomic operations for this |
| 1120 | // type; promote the alignment appropriately. |
| 1121 | // FIXME: We could potentially promote the width here as well... |
| 1122 | // is that worthwhile? (Non-struct atomic types generally have |
| 1123 | // power-of-two size anyway, but structs might not. Requires a bit |
| 1124 | // of implementation work to make sure we zero out the extra bits.) |
| 1125 | Align = static_cast<unsigned>(Width); |
| 1126 | } |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1129 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1130 | |
Eli Friedman | 2be4607 | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 1131 | assert(llvm::isPowerOf2_32(Align) && "Alignment must be power of 2"); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1132 | return std::make_pair(Width, Align); |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1135 | /// toCharUnitsFromBits - Convert a size in bits to a size in characters. |
| 1136 | CharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const { |
| 1137 | return CharUnits::fromQuantity(BitSize / getCharWidth()); |
| 1138 | } |
| 1139 | |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1140 | /// toBits - Convert a size in characters to a size in characters. |
| 1141 | int64_t ASTContext::toBits(CharUnits CharSize) const { |
| 1142 | return CharSize.getQuantity() * getCharWidth(); |
| 1143 | } |
| 1144 | |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1145 | /// getTypeSizeInChars - Return the size of the specified type, in characters. |
| 1146 | /// This method does not work on incomplete types. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1147 | CharUnits ASTContext::getTypeSizeInChars(QualType 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 | } |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1150 | CharUnits ASTContext::getTypeSizeInChars(const Type *T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1151 | return toCharUnitsFromBits(getTypeSize(T)); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
Ken Dyck | 16e20cc | 2010-01-26 17:25:18 +0000 | [diff] [blame] | 1154 | /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1155 | /// characters. This method does not work on incomplete types. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1156 | CharUnits ASTContext::getTypeAlignInChars(QualType 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 | } |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1159 | CharUnits ASTContext::getTypeAlignInChars(const Type *T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1160 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1163 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 1164 | /// type for the current target in bits. This can be different than the ABI |
| 1165 | /// alignment in cases where it is beneficial for performance to overalign |
| 1166 | /// a data type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1167 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) const { |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1168 | unsigned ABIAlign = getTypeAlign(T); |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1169 | |
| 1170 | // Double and long long should be naturally aligned if possible. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1171 | if (const ComplexType* CT = T->getAs<ComplexType>()) |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1172 | T = CT->getElementType().getTypePtr(); |
| 1173 | if (T->isSpecificBuiltinType(BuiltinType::Double) || |
Chad Rosier | cde7a1d | 2012-03-21 20:20:47 +0000 | [diff] [blame] | 1174 | T->isSpecificBuiltinType(BuiltinType::LongLong) || |
| 1175 | T->isSpecificBuiltinType(BuiltinType::ULongLong)) |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1176 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); |
| 1177 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1178 | return ABIAlign; |
| 1179 | } |
| 1180 | |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1181 | /// DeepCollectObjCIvars - |
| 1182 | /// This routine first collects all declared, but not synthesized, ivars in |
| 1183 | /// super class and then collects all ivars, including those synthesized for |
| 1184 | /// current class. This routine is used for implementation of current class |
| 1185 | /// when all ivars, declared and synthesized are known. |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 1186 | /// |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1187 | void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 1188 | bool leafClass, |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 1189 | SmallVectorImpl<const ObjCIvarDecl*> &Ivars) const { |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1190 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
| 1191 | DeepCollectObjCIvars(SuperClass, false, Ivars); |
| 1192 | if (!leafClass) { |
| 1193 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 1194 | E = OI->ivar_end(); I != E; ++I) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1195 | Ivars.push_back(*I); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 1196 | } else { |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 1197 | ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI); |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 1198 | for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv; |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 1199 | Iv= Iv->getNextIvar()) |
| 1200 | Ivars.push_back(Iv); |
| 1201 | } |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1204 | /// CollectInheritedProtocols - Collect all protocols in current class and |
| 1205 | /// those inherited by it. |
| 1206 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 1207 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1208 | if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1209 | // We can use protocol_iterator here instead of |
| 1210 | // all_referenced_protocol_iterator since we are walking all categories. |
| 1211 | for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(), |
| 1212 | PE = OI->all_referenced_protocol_end(); P != PE; ++P) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1213 | ObjCProtocolDecl *Proto = (*P); |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1214 | Protocols.insert(Proto->getCanonicalDecl()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1215 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 1216 | PE = Proto->protocol_end(); P != PE; ++P) { |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1217 | Protocols.insert((*P)->getCanonicalDecl()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1218 | CollectInheritedProtocols(*P, Protocols); |
| 1219 | } |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 1220 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1221 | |
| 1222 | // Categories of this Interface. |
| 1223 | for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList(); |
| 1224 | CDeclChain; CDeclChain = CDeclChain->getNextClassCategory()) |
| 1225 | CollectInheritedProtocols(CDeclChain, Protocols); |
| 1226 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) |
| 1227 | while (SD) { |
| 1228 | CollectInheritedProtocols(SD, Protocols); |
| 1229 | SD = SD->getSuperClass(); |
| 1230 | } |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1231 | } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1232 | for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(), |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1233 | PE = OC->protocol_end(); P != PE; ++P) { |
| 1234 | ObjCProtocolDecl *Proto = (*P); |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1235 | Protocols.insert(Proto->getCanonicalDecl()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1236 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 1237 | PE = Proto->protocol_end(); P != PE; ++P) |
| 1238 | CollectInheritedProtocols(*P, Protocols); |
| 1239 | } |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1240 | } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1241 | for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(), |
| 1242 | PE = OP->protocol_end(); P != PE; ++P) { |
| 1243 | ObjCProtocolDecl *Proto = (*P); |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1244 | Protocols.insert(Proto->getCanonicalDecl()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1245 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 1246 | PE = Proto->protocol_end(); P != PE; ++P) |
| 1247 | CollectInheritedProtocols(*P, Protocols); |
| 1248 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1249 | } |
| 1250 | } |
| 1251 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1252 | unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const { |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1253 | unsigned count = 0; |
| 1254 | // Count ivars declared in class extension. |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 1255 | for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl; |
| 1256 | CDecl = CDecl->getNextClassExtension()) |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1257 | count += CDecl->ivar_size(); |
| 1258 | |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1259 | // Count ivar defined in this class's implementation. This |
| 1260 | // includes synthesized ivars. |
| 1261 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1262 | count += ImplDecl->ivar_size(); |
| 1263 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 1264 | return count; |
| 1265 | } |
| 1266 | |
Argyrios Kyrtzidis | 8deabc1 | 2012-02-03 05:58:16 +0000 | [diff] [blame] | 1267 | bool ASTContext::isSentinelNullExpr(const Expr *E) { |
| 1268 | if (!E) |
| 1269 | return false; |
| 1270 | |
| 1271 | // nullptr_t is always treated as null. |
| 1272 | if (E->getType()->isNullPtrType()) return true; |
| 1273 | |
| 1274 | if (E->getType()->isAnyPointerType() && |
| 1275 | E->IgnoreParenCasts()->isNullPointerConstant(*this, |
| 1276 | Expr::NPC_ValueDependentIsNull)) |
| 1277 | return true; |
| 1278 | |
| 1279 | // Unfortunately, __null has type 'int'. |
| 1280 | if (isa<GNUNullExpr>(E)) return true; |
| 1281 | |
| 1282 | return false; |
| 1283 | } |
| 1284 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1285 | /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. |
| 1286 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
| 1287 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1288 | I = ObjCImpls.find(D); |
| 1289 | if (I != ObjCImpls.end()) |
| 1290 | return cast<ObjCImplementationDecl>(I->second); |
| 1291 | return 0; |
| 1292 | } |
| 1293 | /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. |
| 1294 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
| 1295 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1296 | I = ObjCImpls.find(D); |
| 1297 | if (I != ObjCImpls.end()) |
| 1298 | return cast<ObjCCategoryImplDecl>(I->second); |
| 1299 | return 0; |
| 1300 | } |
| 1301 | |
| 1302 | /// \brief Set the implementation of ObjCInterfaceDecl. |
| 1303 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
| 1304 | ObjCImplementationDecl *ImplD) { |
| 1305 | assert(IFaceD && ImplD && "Passed null params"); |
| 1306 | ObjCImpls[IFaceD] = ImplD; |
| 1307 | } |
| 1308 | /// \brief Set the implementation of ObjCCategoryDecl. |
| 1309 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
| 1310 | ObjCCategoryImplDecl *ImplD) { |
| 1311 | assert(CatD && ImplD && "Passed null params"); |
| 1312 | ObjCImpls[CatD] = ImplD; |
| 1313 | } |
| 1314 | |
Argyrios Kyrtzidis | 87ec9c2 | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 1315 | ObjCInterfaceDecl *ASTContext::getObjContainingInterface(NamedDecl *ND) const { |
| 1316 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ND->getDeclContext())) |
| 1317 | return ID; |
| 1318 | if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(ND->getDeclContext())) |
| 1319 | return CD->getClassInterface(); |
| 1320 | if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(ND->getDeclContext())) |
| 1321 | return IMD->getClassInterface(); |
| 1322 | |
| 1323 | return 0; |
| 1324 | } |
| 1325 | |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1326 | /// \brief Get the copy initialization expression of VarDecl,or NULL if |
| 1327 | /// none exists. |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1328 | Expr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) { |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1329 | assert(VD && "Passed null params"); |
| 1330 | assert(VD->hasAttr<BlocksAttr>() && |
| 1331 | "getBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1332 | llvm::DenseMap<const VarDecl*, Expr*>::iterator |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1333 | I = BlockVarCopyInits.find(VD); |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1334 | return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0; |
| 1335 | } |
| 1336 | |
| 1337 | /// \brief Set the copy inialization expression of a block var decl. |
| 1338 | void ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) { |
| 1339 | assert(VD && Init && "Passed null params"); |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1340 | assert(VD->hasAttr<BlocksAttr>() && |
| 1341 | "setBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1342 | BlockVarCopyInits[VD] = Init; |
| 1343 | } |
| 1344 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1345 | /// \brief Allocate an uninitialized TypeSourceInfo. |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1346 | /// |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1347 | /// The caller should initialize the memory held by TypeSourceInfo using |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1348 | /// the TypeLoc wrappers. |
| 1349 | /// |
| 1350 | /// \param T the type that will be the basis for type source info. This type |
| 1351 | /// should refer to how the declarator was written in source code, not to |
| 1352 | /// what type semantic analysis resolved the declarator to. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1353 | TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1354 | unsigned DataSize) const { |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1355 | if (!DataSize) |
| 1356 | DataSize = TypeLoc::getFullDataSizeForType(T); |
| 1357 | else |
| 1358 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1359 | "incorrect data size provided to CreateTypeSourceInfo!"); |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1360 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1361 | TypeSourceInfo *TInfo = |
| 1362 | (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8); |
| 1363 | new (TInfo) TypeSourceInfo(T); |
| 1364 | return TInfo; |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1367 | TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1368 | SourceLocation L) const { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1369 | TypeSourceInfo *DI = CreateTypeSourceInfo(T); |
Douglas Gregor | c21c7e9 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 1370 | DI->getTypeLoc().initialize(const_cast<ASTContext &>(*this), L); |
John McCall | a4eb74d | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 1371 | return DI; |
| 1372 | } |
| 1373 | |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1374 | const ASTRecordLayout & |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1375 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const { |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1376 | return getObjCLayout(D, 0); |
| 1377 | } |
| 1378 | |
| 1379 | const ASTRecordLayout & |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1380 | ASTContext::getASTObjCImplementationLayout( |
| 1381 | const ObjCImplementationDecl *D) const { |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1382 | return getObjCLayout(D->getClassInterface(), D); |
| 1383 | } |
| 1384 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1385 | //===----------------------------------------------------------------------===// |
| 1386 | // Type creation/memoization methods |
| 1387 | //===----------------------------------------------------------------------===// |
| 1388 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1389 | QualType |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1390 | ASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const { |
| 1391 | unsigned fastQuals = quals.getFastQualifiers(); |
| 1392 | quals.removeFastQualifiers(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1393 | |
| 1394 | // Check if we've already instantiated this type. |
| 1395 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1396 | ExtQuals::Profile(ID, baseType, quals); |
| 1397 | void *insertPos = 0; |
| 1398 | if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, insertPos)) { |
| 1399 | assert(eq->getQualifiers() == quals); |
| 1400 | return QualType(eq, fastQuals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1403 | // If the base type is not canonical, make the appropriate canonical type. |
| 1404 | QualType canon; |
| 1405 | if (!baseType->isCanonicalUnqualified()) { |
| 1406 | SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1407 | canonSplit.Quals.addConsistentQualifiers(quals); |
| 1408 | canon = getExtQualType(canonSplit.Ty, canonSplit.Quals); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1409 | |
| 1410 | // Re-find the insert position. |
| 1411 | (void) ExtQualNodes.FindNodeOrInsertPos(ID, insertPos); |
| 1412 | } |
| 1413 | |
| 1414 | ExtQuals *eq = new (*this, TypeAlignment) ExtQuals(baseType, canon, quals); |
| 1415 | ExtQualNodes.InsertNode(eq, insertPos); |
| 1416 | return QualType(eq, fastQuals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1419 | QualType |
| 1420 | ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) const { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1421 | QualType CanT = getCanonicalType(T); |
| 1422 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1423 | return T; |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1424 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1425 | // If we are composing extended qualifiers together, merge together |
| 1426 | // into one ExtQuals node. |
| 1427 | QualifierCollector Quals; |
| 1428 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1429 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1430 | // If this type already has an address space specified, it cannot get |
| 1431 | // another one. |
| 1432 | assert(!Quals.hasAddressSpace() && |
| 1433 | "Type cannot be in multiple addr spaces!"); |
| 1434 | Quals.addAddressSpace(AddressSpace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1435 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1436 | return getExtQualType(TypeNode, Quals); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1439 | QualType ASTContext::getObjCGCQualType(QualType T, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1440 | Qualifiers::GC GCAttr) const { |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1441 | QualType CanT = getCanonicalType(T); |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1442 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1443 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1444 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1445 | if (const PointerType *ptr = T->getAs<PointerType>()) { |
| 1446 | QualType Pointee = ptr->getPointeeType(); |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 1447 | if (Pointee->isAnyPointerType()) { |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1448 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 1449 | return getPointerType(ResultType); |
| 1450 | } |
| 1451 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1452 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1453 | // If we are composing extended qualifiers together, merge together |
| 1454 | // into one ExtQuals node. |
| 1455 | QualifierCollector Quals; |
| 1456 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1457 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1458 | // If this type already has an ObjCGC specified, it cannot get |
| 1459 | // another one. |
| 1460 | assert(!Quals.hasObjCGCAttr() && |
| 1461 | "Type cannot have multiple ObjCGCs!"); |
| 1462 | Quals.addObjCGCAttr(GCAttr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1463 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1464 | return getExtQualType(TypeNode, Quals); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1465 | } |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1466 | |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 1467 | const FunctionType *ASTContext::adjustFunctionType(const FunctionType *T, |
| 1468 | FunctionType::ExtInfo Info) { |
| 1469 | if (T->getExtInfo() == Info) |
| 1470 | return T; |
| 1471 | |
| 1472 | QualType Result; |
| 1473 | if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) { |
| 1474 | Result = getFunctionNoProtoType(FNPT->getResultType(), Info); |
| 1475 | } else { |
| 1476 | const FunctionProtoType *FPT = cast<FunctionProtoType>(T); |
| 1477 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 1478 | EPI.ExtInfo = Info; |
| 1479 | Result = getFunctionType(FPT->getResultType(), FPT->arg_type_begin(), |
| 1480 | FPT->getNumArgs(), EPI); |
| 1481 | } |
| 1482 | |
| 1483 | return cast<FunctionType>(Result.getTypePtr()); |
| 1484 | } |
| 1485 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1486 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 1487 | /// number with the specified element type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1488 | QualType ASTContext::getComplexType(QualType T) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1489 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1490 | // structure. |
| 1491 | llvm::FoldingSetNodeID ID; |
| 1492 | ComplexType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1493 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1494 | void *InsertPos = 0; |
| 1495 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1496 | return QualType(CT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1497 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1498 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1499 | // so fill in the canonical type field. |
| 1500 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1501 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1502 | Canonical = getComplexType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1503 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1504 | // Get the new insert position for the node we care about. |
| 1505 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1506 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1507 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1508 | ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1509 | Types.push_back(New); |
| 1510 | ComplexTypes.InsertNode(New, InsertPos); |
| 1511 | return QualType(New, 0); |
| 1512 | } |
| 1513 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1514 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 1515 | /// the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1516 | QualType ASTContext::getPointerType(QualType T) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1517 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1518 | // structure. |
| 1519 | llvm::FoldingSetNodeID ID; |
| 1520 | PointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1521 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1522 | void *InsertPos = 0; |
| 1523 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1524 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1525 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1526 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1527 | // so fill in the canonical type field. |
| 1528 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1529 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1530 | Canonical = getPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1531 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1532 | // Get the new insert position for the node we care about. |
| 1533 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1534 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1535 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1536 | PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1537 | Types.push_back(New); |
| 1538 | PointerTypes.InsertNode(New, InsertPos); |
| 1539 | return QualType(New, 0); |
| 1540 | } |
| 1541 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1542 | /// getBlockPointerType - Return the uniqued reference to the type for |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1543 | /// a pointer to the specified block. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1544 | QualType ASTContext::getBlockPointerType(QualType T) const { |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1545 | assert(T->isFunctionType() && "block of function types only"); |
| 1546 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1547 | // structure. |
| 1548 | llvm::FoldingSetNodeID ID; |
| 1549 | BlockPointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1550 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1551 | void *InsertPos = 0; |
| 1552 | if (BlockPointerType *PT = |
| 1553 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1554 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1555 | |
| 1556 | // 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] | 1557 | // type either so fill in the canonical type field. |
| 1558 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1559 | if (!T.isCanonical()) { |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1560 | Canonical = getBlockPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1561 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1562 | // Get the new insert position for the node we care about. |
| 1563 | BlockPointerType *NewIP = |
| 1564 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1565 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1566 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1567 | BlockPointerType *New |
| 1568 | = new (*this, TypeAlignment) BlockPointerType(T, Canonical); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1569 | Types.push_back(New); |
| 1570 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 1571 | return QualType(New, 0); |
| 1572 | } |
| 1573 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1574 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 1575 | /// lvalue reference to the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1576 | QualType |
| 1577 | ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const { |
Douglas Gregor | 9625e44 | 2011-05-21 22:16:50 +0000 | [diff] [blame] | 1578 | assert(getCanonicalType(T) != OverloadTy && |
| 1579 | "Unresolved overloaded function type"); |
| 1580 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1581 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1582 | // structure. |
| 1583 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1584 | ReferenceType::Profile(ID, T, SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1585 | |
| 1586 | void *InsertPos = 0; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1587 | if (LValueReferenceType *RT = |
| 1588 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1589 | return QualType(RT, 0); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1590 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1591 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1592 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1593 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1594 | // either, so fill in the canonical type field. |
| 1595 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1596 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
| 1597 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1598 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1599 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1600 | // Get the new insert position for the node we care about. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1601 | LValueReferenceType *NewIP = |
| 1602 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1603 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1606 | LValueReferenceType *New |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1607 | = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, |
| 1608 | SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1609 | Types.push_back(New); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1610 | LValueReferenceTypes.InsertNode(New, InsertPos); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1611 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1612 | return QualType(New, 0); |
| 1613 | } |
| 1614 | |
| 1615 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 1616 | /// rvalue reference to the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1617 | QualType ASTContext::getRValueReferenceType(QualType T) const { |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1618 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1619 | // structure. |
| 1620 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1621 | ReferenceType::Profile(ID, T, false); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1622 | |
| 1623 | void *InsertPos = 0; |
| 1624 | if (RValueReferenceType *RT = |
| 1625 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1626 | return QualType(RT, 0); |
| 1627 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1628 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1629 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1630 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1631 | // either, so fill in the canonical type field. |
| 1632 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1633 | if (InnerRef || !T.isCanonical()) { |
| 1634 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1635 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1636 | |
| 1637 | // Get the new insert position for the node we care about. |
| 1638 | RValueReferenceType *NewIP = |
| 1639 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1640 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1641 | } |
| 1642 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1643 | RValueReferenceType *New |
| 1644 | = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1645 | Types.push_back(New); |
| 1646 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1647 | return QualType(New, 0); |
| 1648 | } |
| 1649 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1650 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 1651 | /// member pointer to the specified type, in the specified class. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1652 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1653 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1654 | // structure. |
| 1655 | llvm::FoldingSetNodeID ID; |
| 1656 | MemberPointerType::Profile(ID, T, Cls); |
| 1657 | |
| 1658 | void *InsertPos = 0; |
| 1659 | if (MemberPointerType *PT = |
| 1660 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1661 | return QualType(PT, 0); |
| 1662 | |
| 1663 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 1664 | // type either, so fill in the canonical type field. |
| 1665 | QualType Canonical; |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1666 | if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1667 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 1668 | |
| 1669 | // Get the new insert position for the node we care about. |
| 1670 | MemberPointerType *NewIP = |
| 1671 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1672 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1673 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1674 | MemberPointerType *New |
| 1675 | = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1676 | Types.push_back(New); |
| 1677 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 1678 | return QualType(New, 0); |
| 1679 | } |
| 1680 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1681 | /// getConstantArrayType - Return the unique reference to the type for an |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1682 | /// array of the specified element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1683 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1684 | const llvm::APInt &ArySizeIn, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1685 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1686 | unsigned IndexTypeQuals) const { |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1687 | assert((EltTy->isDependentType() || |
| 1688 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && |
Eli Friedman | 587cbdf | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1689 | "Constant array of VLAs is illegal!"); |
| 1690 | |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1691 | // Convert the array size into a canonical width matching the pointer size for |
| 1692 | // the target. |
| 1693 | llvm::APInt ArySize(ArySizeIn); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1694 | ArySize = |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1695 | ArySize.zextOrTrunc(Target->getPointerWidth(getTargetAddressSpace(EltTy))); |
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 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1698 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, IndexTypeQuals); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1699 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1700 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1701 | if (ConstantArrayType *ATP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1702 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1703 | return QualType(ATP, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1704 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1705 | // If the element type isn't canonical or has qualifiers, this won't |
| 1706 | // be a canonical type either, so fill in the canonical type field. |
| 1707 | QualType Canon; |
| 1708 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 1709 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1710 | Canon = getConstantArrayType(QualType(canonSplit.Ty, 0), ArySize, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1711 | ASM, IndexTypeQuals); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1712 | Canon = getQualifiedType(Canon, canonSplit.Quals); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1713 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1714 | // Get the new insert position for the node we care about. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1715 | ConstantArrayType *NewIP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1716 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1717 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1718 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1719 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1720 | ConstantArrayType *New = new(*this,TypeAlignment) |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1721 | ConstantArrayType(EltTy, Canon, ArySize, ASM, IndexTypeQuals); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1722 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1723 | Types.push_back(New); |
| 1724 | return QualType(New, 0); |
| 1725 | } |
| 1726 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1727 | /// getVariableArrayDecayedType - Turns the given type, which may be |
| 1728 | /// variably-modified, into the corresponding type with all the known |
| 1729 | /// sizes replaced with [*]. |
| 1730 | QualType ASTContext::getVariableArrayDecayedType(QualType type) const { |
| 1731 | // Vastly most common case. |
| 1732 | if (!type->isVariablyModifiedType()) return type; |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1733 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1734 | QualType result; |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1735 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1736 | SplitQualType split = type.getSplitDesugaredType(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1737 | const Type *ty = split.Ty; |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1738 | switch (ty->getTypeClass()) { |
| 1739 | #define TYPE(Class, Base) |
| 1740 | #define ABSTRACT_TYPE(Class, Base) |
| 1741 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 1742 | #include "clang/AST/TypeNodes.def" |
| 1743 | llvm_unreachable("didn't desugar past all non-canonical types?"); |
| 1744 | |
| 1745 | // These types should never be variably-modified. |
| 1746 | case Type::Builtin: |
| 1747 | case Type::Complex: |
| 1748 | case Type::Vector: |
| 1749 | case Type::ExtVector: |
| 1750 | case Type::DependentSizedExtVector: |
| 1751 | case Type::ObjCObject: |
| 1752 | case Type::ObjCInterface: |
| 1753 | case Type::ObjCObjectPointer: |
| 1754 | case Type::Record: |
| 1755 | case Type::Enum: |
| 1756 | case Type::UnresolvedUsing: |
| 1757 | case Type::TypeOfExpr: |
| 1758 | case Type::TypeOf: |
| 1759 | case Type::Decltype: |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1760 | case Type::UnaryTransform: |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1761 | case Type::DependentName: |
| 1762 | case Type::InjectedClassName: |
| 1763 | case Type::TemplateSpecialization: |
| 1764 | case Type::DependentTemplateSpecialization: |
| 1765 | case Type::TemplateTypeParm: |
| 1766 | case Type::SubstTemplateTypeParmPack: |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1767 | case Type::Auto: |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1768 | case Type::PackExpansion: |
| 1769 | llvm_unreachable("type should never be variably-modified"); |
| 1770 | |
| 1771 | // These types can be variably-modified but should never need to |
| 1772 | // further decay. |
| 1773 | case Type::FunctionNoProto: |
| 1774 | case Type::FunctionProto: |
| 1775 | case Type::BlockPointer: |
| 1776 | case Type::MemberPointer: |
| 1777 | return type; |
| 1778 | |
| 1779 | // These types can be variably-modified. All these modifications |
| 1780 | // preserve structure except as noted by comments. |
| 1781 | // TODO: if we ever care about optimizing VLAs, there are no-op |
| 1782 | // optimizations available here. |
| 1783 | case Type::Pointer: |
| 1784 | result = getPointerType(getVariableArrayDecayedType( |
| 1785 | cast<PointerType>(ty)->getPointeeType())); |
| 1786 | break; |
| 1787 | |
| 1788 | case Type::LValueReference: { |
| 1789 | const LValueReferenceType *lv = cast<LValueReferenceType>(ty); |
| 1790 | result = getLValueReferenceType( |
| 1791 | getVariableArrayDecayedType(lv->getPointeeType()), |
| 1792 | lv->isSpelledAsLValue()); |
| 1793 | break; |
| 1794 | } |
| 1795 | |
| 1796 | case Type::RValueReference: { |
| 1797 | const RValueReferenceType *lv = cast<RValueReferenceType>(ty); |
| 1798 | result = getRValueReferenceType( |
| 1799 | getVariableArrayDecayedType(lv->getPointeeType())); |
| 1800 | break; |
| 1801 | } |
| 1802 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1803 | case Type::Atomic: { |
| 1804 | const AtomicType *at = cast<AtomicType>(ty); |
| 1805 | result = getAtomicType(getVariableArrayDecayedType(at->getValueType())); |
| 1806 | break; |
| 1807 | } |
| 1808 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1809 | case Type::ConstantArray: { |
| 1810 | const ConstantArrayType *cat = cast<ConstantArrayType>(ty); |
| 1811 | result = getConstantArrayType( |
| 1812 | getVariableArrayDecayedType(cat->getElementType()), |
| 1813 | cat->getSize(), |
| 1814 | cat->getSizeModifier(), |
| 1815 | cat->getIndexTypeCVRQualifiers()); |
| 1816 | break; |
| 1817 | } |
| 1818 | |
| 1819 | case Type::DependentSizedArray: { |
| 1820 | const DependentSizedArrayType *dat = cast<DependentSizedArrayType>(ty); |
| 1821 | result = getDependentSizedArrayType( |
| 1822 | getVariableArrayDecayedType(dat->getElementType()), |
| 1823 | dat->getSizeExpr(), |
| 1824 | dat->getSizeModifier(), |
| 1825 | dat->getIndexTypeCVRQualifiers(), |
| 1826 | dat->getBracketsRange()); |
| 1827 | break; |
| 1828 | } |
| 1829 | |
| 1830 | // Turn incomplete types into [*] types. |
| 1831 | case Type::IncompleteArray: { |
| 1832 | const IncompleteArrayType *iat = cast<IncompleteArrayType>(ty); |
| 1833 | result = getVariableArrayType( |
| 1834 | getVariableArrayDecayedType(iat->getElementType()), |
| 1835 | /*size*/ 0, |
| 1836 | ArrayType::Normal, |
| 1837 | iat->getIndexTypeCVRQualifiers(), |
| 1838 | SourceRange()); |
| 1839 | break; |
| 1840 | } |
| 1841 | |
| 1842 | // Turn VLA types into [*] types. |
| 1843 | case Type::VariableArray: { |
| 1844 | const VariableArrayType *vat = cast<VariableArrayType>(ty); |
| 1845 | result = getVariableArrayType( |
| 1846 | getVariableArrayDecayedType(vat->getElementType()), |
| 1847 | /*size*/ 0, |
| 1848 | ArrayType::Star, |
| 1849 | vat->getIndexTypeCVRQualifiers(), |
| 1850 | vat->getBracketsRange()); |
| 1851 | break; |
| 1852 | } |
| 1853 | } |
| 1854 | |
| 1855 | // Apply the top-level qualifiers from the original. |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1856 | return getQualifiedType(result, split.Quals); |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1857 | } |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1858 | |
Steve Naroff | bdbf7b0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 1859 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 1860 | /// variable array of the specified element type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1861 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 1862 | Expr *NumElts, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1863 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1864 | unsigned IndexTypeQuals, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1865 | SourceRange Brackets) const { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1866 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 1867 | // that have an expression provided for their size. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1868 | QualType Canon; |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 1869 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1870 | // Be sure to pull qualifiers off the element type. |
| 1871 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 1872 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1873 | Canon = getVariableArrayType(QualType(canonSplit.Ty, 0), NumElts, ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1874 | IndexTypeQuals, Brackets); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1875 | Canon = getQualifiedType(Canon, canonSplit.Quals); |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 1876 | } |
| 1877 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1878 | VariableArrayType *New = new(*this, TypeAlignment) |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1879 | VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1880 | |
| 1881 | VariableArrayTypes.push_back(New); |
| 1882 | Types.push_back(New); |
| 1883 | return QualType(New, 0); |
| 1884 | } |
| 1885 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1886 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 1887 | /// the type for a dependently-sized array of the specified element |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1888 | /// type. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1889 | QualType ASTContext::getDependentSizedArrayType(QualType elementType, |
| 1890 | Expr *numElements, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1891 | ArrayType::ArraySizeModifier ASM, |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1892 | unsigned elementTypeQuals, |
| 1893 | SourceRange brackets) const { |
| 1894 | assert((!numElements || numElements->isTypeDependent() || |
| 1895 | numElements->isValueDependent()) && |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1896 | "Size must be type- or value-dependent!"); |
| 1897 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1898 | // Dependently-sized array types that do not have a specified number |
| 1899 | // of elements will have their sizes deduced from a dependent |
| 1900 | // initializer. We do no canonicalization here at all, which is okay |
| 1901 | // because they can't be used in most locations. |
| 1902 | if (!numElements) { |
| 1903 | DependentSizedArrayType *newType |
| 1904 | = new (*this, TypeAlignment) |
| 1905 | DependentSizedArrayType(*this, elementType, QualType(), |
| 1906 | numElements, ASM, elementTypeQuals, |
| 1907 | brackets); |
| 1908 | Types.push_back(newType); |
| 1909 | return QualType(newType, 0); |
| 1910 | } |
| 1911 | |
| 1912 | // Otherwise, we actually build a new type every time, but we |
| 1913 | // also build a canonical type. |
| 1914 | |
| 1915 | SplitQualType canonElementType = getCanonicalType(elementType).split(); |
| 1916 | |
| 1917 | void *insertPos = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1918 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1919 | DependentSizedArrayType::Profile(ID, *this, |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1920 | QualType(canonElementType.Ty, 0), |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1921 | ASM, elementTypeQuals, numElements); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1922 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1923 | // Look for an existing type with these properties. |
| 1924 | DependentSizedArrayType *canonTy = |
| 1925 | DependentSizedArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1926 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1927 | // If we don't have one, build one. |
| 1928 | if (!canonTy) { |
| 1929 | canonTy = new (*this, TypeAlignment) |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1930 | DependentSizedArrayType(*this, QualType(canonElementType.Ty, 0), |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1931 | QualType(), numElements, ASM, elementTypeQuals, |
| 1932 | brackets); |
| 1933 | DependentSizedArrayTypes.InsertNode(canonTy, insertPos); |
| 1934 | Types.push_back(canonTy); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1935 | } |
| 1936 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1937 | // Apply qualifiers from the element type to the array. |
| 1938 | QualType canon = getQualifiedType(QualType(canonTy,0), |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1939 | canonElementType.Quals); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1940 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1941 | // If we didn't need extra canonicalization for the element type, |
| 1942 | // then just use that as our result. |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1943 | if (QualType(canonElementType.Ty, 0) == elementType) |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1944 | return canon; |
| 1945 | |
| 1946 | // Otherwise, we need to build a type which follows the spelling |
| 1947 | // of the element type. |
| 1948 | DependentSizedArrayType *sugaredType |
| 1949 | = new (*this, TypeAlignment) |
| 1950 | DependentSizedArrayType(*this, elementType, canon, numElements, |
| 1951 | ASM, elementTypeQuals, brackets); |
| 1952 | Types.push_back(sugaredType); |
| 1953 | return QualType(sugaredType, 0); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1954 | } |
| 1955 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1956 | QualType ASTContext::getIncompleteArrayType(QualType elementType, |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1957 | ArrayType::ArraySizeModifier ASM, |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1958 | unsigned elementTypeQuals) const { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1959 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1960 | IncompleteArrayType::Profile(ID, elementType, ASM, elementTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1961 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1962 | void *insertPos = 0; |
| 1963 | if (IncompleteArrayType *iat = |
| 1964 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos)) |
| 1965 | return QualType(iat, 0); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1966 | |
| 1967 | // 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] | 1968 | // either, so fill in the canonical type field. We also have to pull |
| 1969 | // qualifiers off the element type. |
| 1970 | QualType canon; |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1971 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1972 | if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) { |
| 1973 | SplitQualType canonSplit = getCanonicalType(elementType).split(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1974 | canon = getIncompleteArrayType(QualType(canonSplit.Ty, 0), |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1975 | ASM, elementTypeQuals); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1976 | canon = getQualifiedType(canon, canonSplit.Quals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1977 | |
| 1978 | // Get the new insert position for the node we care about. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1979 | IncompleteArrayType *existing = |
| 1980 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
| 1981 | assert(!existing && "Shouldn't be in the map!"); (void) existing; |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1982 | } |
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 | IncompleteArrayType *newType = new (*this, TypeAlignment) |
| 1985 | IncompleteArrayType(elementType, canon, ASM, elementTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1986 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1987 | IncompleteArrayTypes.InsertNode(newType, insertPos); |
| 1988 | Types.push_back(newType); |
| 1989 | return QualType(newType, 0); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1992 | /// getVectorType - Return the unique reference to a vector type of |
| 1993 | /// 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] | 1994 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1995 | VectorType::VectorKind VecKind) const { |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1996 | assert(vecType->isBuiltinType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1997 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1998 | // Check if we've already instantiated a vector of this type. |
| 1999 | llvm::FoldingSetNodeID ID; |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2000 | VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind); |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 2001 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2002 | void *InsertPos = 0; |
| 2003 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2004 | return QualType(VTP, 0); |
| 2005 | |
| 2006 | // If the element type isn't canonical, this won't be a canonical type either, |
| 2007 | // so fill in the canonical type field. |
| 2008 | QualType Canonical; |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 2009 | if (!vecType.isCanonical()) { |
Bob Wilson | 231da7e | 2010-11-16 00:32:20 +0000 | [diff] [blame] | 2010 | Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2011 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2012 | // Get the new insert position for the node we care about. |
| 2013 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2014 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2015 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2016 | VectorType *New = new (*this, TypeAlignment) |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2017 | VectorType(vecType, NumElts, Canonical, VecKind); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2018 | VectorTypes.InsertNode(New, InsertPos); |
| 2019 | Types.push_back(New); |
| 2020 | return QualType(New, 0); |
| 2021 | } |
| 2022 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2023 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2024 | /// 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] | 2025 | QualType |
| 2026 | ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) const { |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 2027 | assert(vecType->isBuiltinType() || vecType->isDependentType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2028 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2029 | // Check if we've already instantiated a vector of this type. |
| 2030 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 2031 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2032 | VectorType::GenericVector); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2033 | void *InsertPos = 0; |
| 2034 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2035 | return QualType(VTP, 0); |
| 2036 | |
| 2037 | // If the element type isn't canonical, this won't be a canonical type either, |
| 2038 | // so fill in the canonical type field. |
| 2039 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2040 | if (!vecType.isCanonical()) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2041 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2042 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2043 | // Get the new insert position for the node we care about. |
| 2044 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2045 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2046 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2047 | ExtVectorType *New = new (*this, TypeAlignment) |
| 2048 | ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2049 | VectorTypes.InsertNode(New, InsertPos); |
| 2050 | Types.push_back(New); |
| 2051 | return QualType(New, 0); |
| 2052 | } |
| 2053 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2054 | QualType |
| 2055 | ASTContext::getDependentSizedExtVectorType(QualType vecType, |
| 2056 | Expr *SizeExpr, |
| 2057 | SourceLocation AttrLoc) const { |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2058 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2059 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2060 | SizeExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2061 | |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2062 | void *InsertPos = 0; |
| 2063 | DependentSizedExtVectorType *Canon |
| 2064 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2065 | DependentSizedExtVectorType *New; |
| 2066 | if (Canon) { |
| 2067 | // We already have a canonical version of this array type; use it as |
| 2068 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2069 | New = new (*this, TypeAlignment) |
| 2070 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), |
| 2071 | SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2072 | } else { |
| 2073 | QualType CanonVecTy = getCanonicalType(vecType); |
| 2074 | if (CanonVecTy == vecType) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2075 | New = new (*this, TypeAlignment) |
| 2076 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, |
| 2077 | AttrLoc); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2078 | |
| 2079 | DependentSizedExtVectorType *CanonCheck |
| 2080 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2081 | assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken"); |
| 2082 | (void)CanonCheck; |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2083 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); |
| 2084 | } else { |
| 2085 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 2086 | SourceLocation()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2087 | New = new (*this, TypeAlignment) |
| 2088 | DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2089 | } |
| 2090 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2091 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 2092 | Types.push_back(New); |
| 2093 | return QualType(New, 0); |
| 2094 | } |
| 2095 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2096 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2097 | /// |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2098 | QualType |
| 2099 | ASTContext::getFunctionNoProtoType(QualType ResultTy, |
| 2100 | const FunctionType::ExtInfo &Info) const { |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2101 | const CallingConv DefaultCC = Info.getCC(); |
| 2102 | const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ? |
| 2103 | CC_X86StdCall : DefaultCC; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2104 | // Unique functions, to guarantee there is only one function of a particular |
| 2105 | // structure. |
| 2106 | llvm::FoldingSetNodeID ID; |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2107 | FunctionNoProtoType::Profile(ID, ResultTy, Info); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2108 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2109 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2110 | if (FunctionNoProtoType *FT = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2111 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2112 | return QualType(FT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2113 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2114 | QualType Canonical; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 2115 | if (!ResultTy.isCanonical() || |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2116 | getCanonicalCallConv(CallConv) != CallConv) { |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2117 | Canonical = |
| 2118 | getFunctionNoProtoType(getCanonicalType(ResultTy), |
| 2119 | Info.withCallingConv(getCanonicalCallConv(CallConv))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2120 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2121 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2122 | FunctionNoProtoType *NewIP = |
| 2123 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2124 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2125 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2126 | |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2127 | FunctionProtoType::ExtInfo newInfo = Info.withCallingConv(CallConv); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2128 | FunctionNoProtoType *New = new (*this, TypeAlignment) |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2129 | FunctionNoProtoType(ResultTy, Canonical, newInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2130 | Types.push_back(New); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2131 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2132 | return QualType(New, 0); |
| 2133 | } |
| 2134 | |
| 2135 | /// getFunctionType - Return a normal function type with a typed argument |
| 2136 | /// list. isVariadic indicates whether the argument list includes '...'. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2137 | QualType |
| 2138 | ASTContext::getFunctionType(QualType ResultTy, |
| 2139 | const QualType *ArgArray, unsigned NumArgs, |
| 2140 | const FunctionProtoType::ExtProtoInfo &EPI) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2141 | // Unique functions, to guarantee there is only one function of a particular |
| 2142 | // structure. |
| 2143 | llvm::FoldingSetNodeID ID; |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2144 | FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, EPI, *this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2145 | |
| 2146 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2147 | if (FunctionProtoType *FTP = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2148 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2149 | return QualType(FTP, 0); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2150 | |
| 2151 | // Determine whether the type being created is already canonical or not. |
Richard Smith | eefb3d5 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2152 | bool isCanonical = |
| 2153 | EPI.ExceptionSpecType == EST_None && ResultTy.isCanonical() && |
| 2154 | !EPI.HasTrailingReturn; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2155 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2156 | if (!ArgArray[i].isCanonicalAsParam()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2157 | isCanonical = false; |
| 2158 | |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2159 | const CallingConv DefaultCC = EPI.ExtInfo.getCC(); |
| 2160 | const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ? |
| 2161 | CC_X86StdCall : DefaultCC; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2162 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2163 | // If this type isn't canonical, get the canonical version of it. |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2164 | // The exception spec is not part of the canonical type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2165 | QualType Canonical; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2166 | if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2167 | SmallVector<QualType, 16> CanonicalArgs; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2168 | CanonicalArgs.reserve(NumArgs); |
| 2169 | for (unsigned i = 0; i != NumArgs; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2170 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2171 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2172 | FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI; |
Richard Smith | eefb3d5 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2173 | CanonicalEPI.HasTrailingReturn = false; |
Sebastian Redl | 8b5b409 | 2011-03-06 10:52:04 +0000 | [diff] [blame] | 2174 | CanonicalEPI.ExceptionSpecType = EST_None; |
| 2175 | CanonicalEPI.NumExceptions = 0; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2176 | CanonicalEPI.ExtInfo |
| 2177 | = CanonicalEPI.ExtInfo.withCallingConv(getCanonicalCallConv(CallConv)); |
| 2178 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2179 | Canonical = getFunctionType(getCanonicalType(ResultTy), |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2180 | CanonicalArgs.data(), NumArgs, |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2181 | CanonicalEPI); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2182 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2183 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2184 | FunctionProtoType *NewIP = |
| 2185 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2186 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2187 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2188 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2189 | // FunctionProtoType objects are allocated with extra bytes after |
| 2190 | // them for three variable size arrays at the end: |
| 2191 | // - parameter types |
| 2192 | // - exception types |
| 2193 | // - consumed-arguments flags |
| 2194 | // Instead of the exception types, there could be a noexcept |
| 2195 | // expression. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2196 | size_t Size = sizeof(FunctionProtoType) + |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2197 | NumArgs * sizeof(QualType); |
| 2198 | if (EPI.ExceptionSpecType == EST_Dynamic) |
| 2199 | Size += EPI.NumExceptions * sizeof(QualType); |
| 2200 | else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) { |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2201 | Size += sizeof(Expr*); |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 2202 | } else if (EPI.ExceptionSpecType == EST_Uninstantiated) { |
Richard Smith | 13bffc5 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 2203 | Size += 2 * sizeof(FunctionDecl*); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2204 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2205 | if (EPI.ConsumedArguments) |
| 2206 | Size += NumArgs * sizeof(bool); |
| 2207 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2208 | FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment); |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2209 | FunctionProtoType::ExtProtoInfo newEPI = EPI; |
| 2210 | newEPI.ExtInfo = EPI.ExtInfo.withCallingConv(CallConv); |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2211 | new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, Canonical, newEPI); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2212 | Types.push_back(FTP); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2213 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2214 | return QualType(FTP, 0); |
| 2215 | } |
| 2216 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2217 | #ifndef NDEBUG |
| 2218 | static bool NeedsInjectedClassNameType(const RecordDecl *D) { |
| 2219 | if (!isa<CXXRecordDecl>(D)) return false; |
| 2220 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 2221 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) |
| 2222 | return true; |
| 2223 | if (RD->getDescribedClassTemplate() && |
| 2224 | !isa<ClassTemplateSpecializationDecl>(RD)) |
| 2225 | return true; |
| 2226 | return false; |
| 2227 | } |
| 2228 | #endif |
| 2229 | |
| 2230 | /// getInjectedClassNameType - Return the unique reference to the |
| 2231 | /// injected class name type for the specified templated declaration. |
| 2232 | QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2233 | QualType TST) const { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2234 | assert(NeedsInjectedClassNameType(Decl)); |
| 2235 | if (Decl->TypeForDecl) { |
| 2236 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2237 | } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDecl()) { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2238 | assert(PrevDecl->TypeForDecl && "previous declaration has no type"); |
| 2239 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 2240 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
| 2241 | } else { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2242 | Type *newType = |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2243 | new (*this, TypeAlignment) InjectedClassNameType(Decl, TST); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2244 | Decl->TypeForDecl = newType; |
| 2245 | Types.push_back(newType); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2246 | } |
| 2247 | return QualType(Decl->TypeForDecl, 0); |
| 2248 | } |
| 2249 | |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2250 | /// getTypeDeclType - Return the unique reference to the type for the |
| 2251 | /// specified type declaration. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2252 | QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const { |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 2253 | assert(Decl && "Passed null for Decl param"); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2254 | assert(!Decl->TypeForDecl && "TypeForDecl present in slow case"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2255 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2256 | if (const TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2257 | return getTypedefType(Typedef); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2258 | |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2259 | assert(!isa<TemplateTypeParmDecl>(Decl) && |
| 2260 | "Template type parameter types are always available."); |
| 2261 | |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2262 | if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2263 | assert(!Record->getPreviousDecl() && |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2264 | "struct/union has previous declaration"); |
| 2265 | assert(!NeedsInjectedClassNameType(Record)); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2266 | return getRecordType(Record); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2267 | } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2268 | assert(!Enum->getPreviousDecl() && |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2269 | "enum has previous declaration"); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2270 | return getEnumType(Enum); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2271 | } else if (const UnresolvedUsingTypenameDecl *Using = |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2272 | dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2273 | Type *newType = new (*this, TypeAlignment) UnresolvedUsingType(Using); |
| 2274 | Decl->TypeForDecl = newType; |
| 2275 | Types.push_back(newType); |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 2276 | } else |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2277 | llvm_unreachable("TypeDecl without a type?"); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 2278 | |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 2279 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2280 | } |
| 2281 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2282 | /// getTypedefType - Return the unique reference to the type for the |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2283 | /// specified typedef name decl. |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2284 | QualType |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2285 | ASTContext::getTypedefType(const TypedefNameDecl *Decl, |
| 2286 | QualType Canonical) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2287 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2288 | |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2289 | if (Canonical.isNull()) |
| 2290 | Canonical = getCanonicalType(Decl->getUnderlyingType()); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2291 | TypedefType *newType = new(*this, TypeAlignment) |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2292 | TypedefType(Type::Typedef, Decl, Canonical); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2293 | Decl->TypeForDecl = newType; |
| 2294 | Types.push_back(newType); |
| 2295 | return QualType(newType, 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2296 | } |
| 2297 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2298 | QualType ASTContext::getRecordType(const RecordDecl *Decl) const { |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2299 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 2300 | |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2301 | if (const RecordDecl *PrevDecl = Decl->getPreviousDecl()) |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2302 | if (PrevDecl->TypeForDecl) |
| 2303 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 2304 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2305 | RecordType *newType = new (*this, TypeAlignment) RecordType(Decl); |
| 2306 | Decl->TypeForDecl = newType; |
| 2307 | Types.push_back(newType); |
| 2308 | return QualType(newType, 0); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2309 | } |
| 2310 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2311 | QualType ASTContext::getEnumType(const EnumDecl *Decl) const { |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2312 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 2313 | |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2314 | if (const EnumDecl *PrevDecl = Decl->getPreviousDecl()) |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2315 | if (PrevDecl->TypeForDecl) |
| 2316 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 2317 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2318 | EnumType *newType = new (*this, TypeAlignment) EnumType(Decl); |
| 2319 | Decl->TypeForDecl = newType; |
| 2320 | Types.push_back(newType); |
| 2321 | return QualType(newType, 0); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2322 | } |
| 2323 | |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 2324 | QualType ASTContext::getAttributedType(AttributedType::Kind attrKind, |
| 2325 | QualType modifiedType, |
| 2326 | QualType equivalentType) { |
| 2327 | llvm::FoldingSetNodeID id; |
| 2328 | AttributedType::Profile(id, attrKind, modifiedType, equivalentType); |
| 2329 | |
| 2330 | void *insertPos = 0; |
| 2331 | AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos); |
| 2332 | if (type) return QualType(type, 0); |
| 2333 | |
| 2334 | QualType canon = getCanonicalType(equivalentType); |
| 2335 | type = new (*this, TypeAlignment) |
| 2336 | AttributedType(canon, attrKind, modifiedType, equivalentType); |
| 2337 | |
| 2338 | Types.push_back(type); |
| 2339 | AttributedTypes.InsertNode(type, insertPos); |
| 2340 | |
| 2341 | return QualType(type, 0); |
| 2342 | } |
| 2343 | |
| 2344 | |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2345 | /// \brief Retrieve a substitution-result type. |
| 2346 | QualType |
| 2347 | ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2348 | QualType Replacement) const { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2349 | assert(Replacement.isCanonical() |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2350 | && "replacement types must always be canonical"); |
| 2351 | |
| 2352 | llvm::FoldingSetNodeID ID; |
| 2353 | SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); |
| 2354 | void *InsertPos = 0; |
| 2355 | SubstTemplateTypeParmType *SubstParm |
| 2356 | = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2357 | |
| 2358 | if (!SubstParm) { |
| 2359 | SubstParm = new (*this, TypeAlignment) |
| 2360 | SubstTemplateTypeParmType(Parm, Replacement); |
| 2361 | Types.push_back(SubstParm); |
| 2362 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 2363 | } |
| 2364 | |
| 2365 | return QualType(SubstParm, 0); |
| 2366 | } |
| 2367 | |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 2368 | /// \brief Retrieve a |
| 2369 | QualType ASTContext::getSubstTemplateTypeParmPackType( |
| 2370 | const TemplateTypeParmType *Parm, |
| 2371 | const TemplateArgument &ArgPack) { |
| 2372 | #ifndef NDEBUG |
| 2373 | for (TemplateArgument::pack_iterator P = ArgPack.pack_begin(), |
| 2374 | PEnd = ArgPack.pack_end(); |
| 2375 | P != PEnd; ++P) { |
| 2376 | assert(P->getKind() == TemplateArgument::Type &&"Pack contains a non-type"); |
| 2377 | assert(P->getAsType().isCanonical() && "Pack contains non-canonical type"); |
| 2378 | } |
| 2379 | #endif |
| 2380 | |
| 2381 | llvm::FoldingSetNodeID ID; |
| 2382 | SubstTemplateTypeParmPackType::Profile(ID, Parm, ArgPack); |
| 2383 | void *InsertPos = 0; |
| 2384 | if (SubstTemplateTypeParmPackType *SubstParm |
| 2385 | = SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2386 | return QualType(SubstParm, 0); |
| 2387 | |
| 2388 | QualType Canon; |
| 2389 | if (!Parm->isCanonicalUnqualified()) { |
| 2390 | Canon = getCanonicalType(QualType(Parm, 0)); |
| 2391 | Canon = getSubstTemplateTypeParmPackType(cast<TemplateTypeParmType>(Canon), |
| 2392 | ArgPack); |
| 2393 | SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2394 | } |
| 2395 | |
| 2396 | SubstTemplateTypeParmPackType *SubstParm |
| 2397 | = new (*this, TypeAlignment) SubstTemplateTypeParmPackType(Parm, Canon, |
| 2398 | ArgPack); |
| 2399 | Types.push_back(SubstParm); |
| 2400 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 2401 | return QualType(SubstParm, 0); |
| 2402 | } |
| 2403 | |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2404 | /// \brief Retrieve the template type parameter type for a template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2405 | /// parameter or parameter pack with the given depth, index, and (optionally) |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2406 | /// name. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2407 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2408 | bool ParameterPack, |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2409 | TemplateTypeParmDecl *TTPDecl) const { |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2410 | llvm::FoldingSetNodeID ID; |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2411 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, TTPDecl); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2412 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2413 | TemplateTypeParmType *TypeParm |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2414 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2415 | |
| 2416 | if (TypeParm) |
| 2417 | return QualType(TypeParm, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2418 | |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2419 | if (TTPDecl) { |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2420 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2421 | TypeParm = new (*this, TypeAlignment) TemplateTypeParmType(TTPDecl, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2422 | |
| 2423 | TemplateTypeParmType *TypeCheck |
| 2424 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2425 | assert(!TypeCheck && "Template type parameter canonical type broken"); |
| 2426 | (void)TypeCheck; |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2427 | } else |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2428 | TypeParm = new (*this, TypeAlignment) |
| 2429 | TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2430 | |
| 2431 | Types.push_back(TypeParm); |
| 2432 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 2433 | |
| 2434 | return QualType(TypeParm, 0); |
| 2435 | } |
| 2436 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2437 | TypeSourceInfo * |
| 2438 | ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name, |
| 2439 | SourceLocation NameLoc, |
| 2440 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2441 | QualType Underlying) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2442 | assert(!Name.getAsDependentTemplateName() && |
| 2443 | "No dependent template names here!"); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2444 | QualType TST = getTemplateSpecializationType(Name, Args, Underlying); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2445 | |
| 2446 | TypeSourceInfo *DI = CreateTypeSourceInfo(TST); |
| 2447 | TemplateSpecializationTypeLoc TL |
| 2448 | = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc()); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2449 | TL.setTemplateKeywordLoc(SourceLocation()); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2450 | TL.setTemplateNameLoc(NameLoc); |
| 2451 | TL.setLAngleLoc(Args.getLAngleLoc()); |
| 2452 | TL.setRAngleLoc(Args.getRAngleLoc()); |
| 2453 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 2454 | TL.setArgLocInfo(i, Args[i].getLocInfo()); |
| 2455 | return DI; |
| 2456 | } |
| 2457 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2458 | QualType |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2459 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2460 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2461 | QualType Underlying) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2462 | assert(!Template.getAsDependentTemplateName() && |
| 2463 | "No dependent template names here!"); |
| 2464 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2465 | unsigned NumArgs = Args.size(); |
| 2466 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2467 | SmallVector<TemplateArgument, 4> ArgVec; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2468 | ArgVec.reserve(NumArgs); |
| 2469 | for (unsigned i = 0; i != NumArgs; ++i) |
| 2470 | ArgVec.push_back(Args[i].getArgument()); |
| 2471 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2472 | return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2473 | Underlying); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2474 | } |
| 2475 | |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2476 | #ifndef NDEBUG |
| 2477 | static bool hasAnyPackExpansions(const TemplateArgument *Args, |
| 2478 | unsigned NumArgs) { |
| 2479 | for (unsigned I = 0; I != NumArgs; ++I) |
| 2480 | if (Args[I].isPackExpansion()) |
| 2481 | return true; |
| 2482 | |
| 2483 | return true; |
| 2484 | } |
| 2485 | #endif |
| 2486 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2487 | QualType |
| 2488 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2489 | const TemplateArgument *Args, |
| 2490 | unsigned NumArgs, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2491 | QualType Underlying) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2492 | assert(!Template.getAsDependentTemplateName() && |
| 2493 | "No dependent template names here!"); |
Douglas Gregor | 0f0ea2a | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 2494 | // Look through qualified template names. |
| 2495 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 2496 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2497 | |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2498 | bool IsTypeAlias = |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2499 | Template.getAsTemplateDecl() && |
| 2500 | isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl()); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2501 | QualType CanonType; |
| 2502 | if (!Underlying.isNull()) |
| 2503 | CanonType = getCanonicalType(Underlying); |
| 2504 | else { |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2505 | // We can get here with an alias template when the specialization contains |
| 2506 | // a pack expansion that does not match up with a parameter pack. |
| 2507 | assert((!IsTypeAlias || hasAnyPackExpansions(Args, NumArgs)) && |
| 2508 | "Caller must compute aliased type"); |
| 2509 | IsTypeAlias = false; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2510 | CanonType = getCanonicalTemplateSpecializationType(Template, Args, |
| 2511 | NumArgs); |
| 2512 | } |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 2513 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2514 | // Allocate the (non-canonical) template specialization type, but don't |
| 2515 | // try to unique it: these types typically have location information that |
| 2516 | // we don't unique and don't want to lose. |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2517 | void *Mem = Allocate(sizeof(TemplateSpecializationType) + |
| 2518 | sizeof(TemplateArgument) * NumArgs + |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2519 | (IsTypeAlias? sizeof(QualType) : 0), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2520 | TypeAlignment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2521 | TemplateSpecializationType *Spec |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2522 | = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, CanonType, |
| 2523 | IsTypeAlias ? Underlying : QualType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2524 | |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2525 | Types.push_back(Spec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2526 | return QualType(Spec, 0); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2527 | } |
| 2528 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2529 | QualType |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2530 | ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template, |
| 2531 | const TemplateArgument *Args, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2532 | unsigned NumArgs) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2533 | assert(!Template.getAsDependentTemplateName() && |
| 2534 | "No dependent template names here!"); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2535 | |
Douglas Gregor | 0f0ea2a | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 2536 | // Look through qualified template names. |
| 2537 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 2538 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2539 | |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2540 | // Build the canonical template specialization type. |
| 2541 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2542 | SmallVector<TemplateArgument, 4> CanonArgs; |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2543 | CanonArgs.reserve(NumArgs); |
| 2544 | for (unsigned I = 0; I != NumArgs; ++I) |
| 2545 | CanonArgs.push_back(getCanonicalTemplateArgument(Args[I])); |
| 2546 | |
| 2547 | // Determine whether this canonical template specialization type already |
| 2548 | // exists. |
| 2549 | llvm::FoldingSetNodeID ID; |
| 2550 | TemplateSpecializationType::Profile(ID, CanonTemplate, |
| 2551 | CanonArgs.data(), NumArgs, *this); |
| 2552 | |
| 2553 | void *InsertPos = 0; |
| 2554 | TemplateSpecializationType *Spec |
| 2555 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2556 | |
| 2557 | if (!Spec) { |
| 2558 | // Allocate a new canonical template specialization type. |
| 2559 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
| 2560 | sizeof(TemplateArgument) * NumArgs), |
| 2561 | TypeAlignment); |
| 2562 | Spec = new (Mem) TemplateSpecializationType(CanonTemplate, |
| 2563 | CanonArgs.data(), NumArgs, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2564 | QualType(), QualType()); |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2565 | Types.push_back(Spec); |
| 2566 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
| 2567 | } |
| 2568 | |
| 2569 | assert(Spec->isDependentType() && |
| 2570 | "Non-dependent template-id type must have a canonical type"); |
| 2571 | return QualType(Spec, 0); |
| 2572 | } |
| 2573 | |
| 2574 | QualType |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2575 | ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword, |
| 2576 | NestedNameSpecifier *NNS, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2577 | QualType NamedType) const { |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2578 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2579 | ElaboratedType::Profile(ID, Keyword, NNS, NamedType); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2580 | |
| 2581 | void *InsertPos = 0; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2582 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2583 | if (T) |
| 2584 | return QualType(T, 0); |
| 2585 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2586 | QualType Canon = NamedType; |
| 2587 | if (!Canon.isCanonical()) { |
| 2588 | Canon = getCanonicalType(NamedType); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2589 | ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2590 | assert(!CheckT && "Elaborated canonical type broken"); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2591 | (void)CheckT; |
| 2592 | } |
| 2593 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2594 | T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2595 | Types.push_back(T); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2596 | ElaboratedTypes.InsertNode(T, InsertPos); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2597 | return QualType(T, 0); |
| 2598 | } |
| 2599 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 2600 | QualType |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2601 | ASTContext::getParenType(QualType InnerType) const { |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 2602 | llvm::FoldingSetNodeID ID; |
| 2603 | ParenType::Profile(ID, InnerType); |
| 2604 | |
| 2605 | void *InsertPos = 0; |
| 2606 | ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2607 | if (T) |
| 2608 | return QualType(T, 0); |
| 2609 | |
| 2610 | QualType Canon = InnerType; |
| 2611 | if (!Canon.isCanonical()) { |
| 2612 | Canon = getCanonicalType(InnerType); |
| 2613 | ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2614 | assert(!CheckT && "Paren canonical type broken"); |
| 2615 | (void)CheckT; |
| 2616 | } |
| 2617 | |
| 2618 | T = new (*this) ParenType(InnerType, Canon); |
| 2619 | Types.push_back(T); |
| 2620 | ParenTypes.InsertNode(T, InsertPos); |
| 2621 | return QualType(T, 0); |
| 2622 | } |
| 2623 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2624 | QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, |
| 2625 | NestedNameSpecifier *NNS, |
| 2626 | const IdentifierInfo *Name, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2627 | QualType Canon) const { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2628 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 2629 | |
| 2630 | if (Canon.isNull()) { |
| 2631 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2632 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 2633 | if (Keyword == ETK_None) |
| 2634 | CanonKeyword = ETK_Typename; |
| 2635 | |
| 2636 | if (CanonNNS != NNS || CanonKeyword != Keyword) |
| 2637 | Canon = getDependentNameType(CanonKeyword, CanonNNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2638 | } |
| 2639 | |
| 2640 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2641 | DependentNameType::Profile(ID, Keyword, NNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2642 | |
| 2643 | void *InsertPos = 0; |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2644 | DependentNameType *T |
| 2645 | = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2646 | if (T) |
| 2647 | return QualType(T, 0); |
| 2648 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2649 | T = new (*this) DependentNameType(Keyword, NNS, Name, Canon); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2650 | Types.push_back(T); |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2651 | DependentNameTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2652 | return QualType(T, 0); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2653 | } |
| 2654 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2655 | QualType |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2656 | ASTContext::getDependentTemplateSpecializationType( |
| 2657 | ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2658 | NestedNameSpecifier *NNS, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2659 | const IdentifierInfo *Name, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2660 | const TemplateArgumentListInfo &Args) const { |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2661 | // TODO: avoid this copy |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2662 | SmallVector<TemplateArgument, 16> ArgCopy; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2663 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
| 2664 | ArgCopy.push_back(Args[I].getArgument()); |
| 2665 | return getDependentTemplateSpecializationType(Keyword, NNS, Name, |
| 2666 | ArgCopy.size(), |
| 2667 | ArgCopy.data()); |
| 2668 | } |
| 2669 | |
| 2670 | QualType |
| 2671 | ASTContext::getDependentTemplateSpecializationType( |
| 2672 | ElaboratedTypeKeyword Keyword, |
| 2673 | NestedNameSpecifier *NNS, |
| 2674 | const IdentifierInfo *Name, |
| 2675 | unsigned NumArgs, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2676 | const TemplateArgument *Args) const { |
Douglas Gregor | aa2187d | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 2677 | assert((!NNS || NNS->isDependent()) && |
| 2678 | "nested-name-specifier must be dependent"); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2679 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2680 | llvm::FoldingSetNodeID ID; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2681 | DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS, |
| 2682 | Name, NumArgs, Args); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2683 | |
| 2684 | void *InsertPos = 0; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2685 | DependentTemplateSpecializationType *T |
| 2686 | = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2687 | if (T) |
| 2688 | return QualType(T, 0); |
| 2689 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2690 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2691 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2692 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 2693 | if (Keyword == ETK_None) CanonKeyword = ETK_Typename; |
| 2694 | |
| 2695 | bool AnyNonCanonArgs = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2696 | SmallVector<TemplateArgument, 16> CanonArgs(NumArgs); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2697 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 2698 | CanonArgs[I] = getCanonicalTemplateArgument(Args[I]); |
| 2699 | if (!CanonArgs[I].structurallyEquals(Args[I])) |
| 2700 | AnyNonCanonArgs = true; |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2701 | } |
| 2702 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2703 | QualType Canon; |
| 2704 | if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) { |
| 2705 | Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS, |
| 2706 | Name, NumArgs, |
| 2707 | CanonArgs.data()); |
| 2708 | |
| 2709 | // Find the insert position again. |
| 2710 | DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2711 | } |
| 2712 | |
| 2713 | void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) + |
| 2714 | sizeof(TemplateArgument) * NumArgs), |
| 2715 | TypeAlignment); |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 2716 | T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2717 | Name, NumArgs, Args, Canon); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2718 | Types.push_back(T); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2719 | DependentTemplateSpecializationTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2720 | return QualType(T, 0); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2721 | } |
| 2722 | |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2723 | QualType ASTContext::getPackExpansionType(QualType Pattern, |
| 2724 | llvm::Optional<unsigned> NumExpansions) { |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2725 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2726 | PackExpansionType::Profile(ID, Pattern, NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2727 | |
| 2728 | assert(Pattern->containsUnexpandedParameterPack() && |
| 2729 | "Pack expansions must expand one or more parameter packs"); |
| 2730 | void *InsertPos = 0; |
| 2731 | PackExpansionType *T |
| 2732 | = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2733 | if (T) |
| 2734 | return QualType(T, 0); |
| 2735 | |
| 2736 | QualType Canon; |
| 2737 | if (!Pattern.isCanonical()) { |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2738 | Canon = getPackExpansionType(getCanonicalType(Pattern), NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2739 | |
| 2740 | // Find the insert position again. |
| 2741 | PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2742 | } |
| 2743 | |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2744 | T = new (*this) PackExpansionType(Pattern, Canon, NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2745 | Types.push_back(T); |
| 2746 | PackExpansionTypes.InsertNode(T, InsertPos); |
| 2747 | return QualType(T, 0); |
| 2748 | } |
| 2749 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2750 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 2751 | /// alphabetically. |
| 2752 | static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, |
| 2753 | const ObjCProtocolDecl *RHS) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2754 | return LHS->getDeclName() < RHS->getDeclName(); |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2755 | } |
| 2756 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2757 | static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols, |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2758 | unsigned NumProtocols) { |
| 2759 | if (NumProtocols == 0) return true; |
| 2760 | |
Douglas Gregor | 61cc296 | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 2761 | if (Protocols[0]->getCanonicalDecl() != Protocols[0]) |
| 2762 | return false; |
| 2763 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2764 | for (unsigned i = 1; i != NumProtocols; ++i) |
Douglas Gregor | 61cc296 | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 2765 | if (!CmpProtocolNames(Protocols[i-1], Protocols[i]) || |
| 2766 | Protocols[i]->getCanonicalDecl() != Protocols[i]) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2767 | return false; |
| 2768 | return true; |
| 2769 | } |
| 2770 | |
| 2771 | static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols, |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2772 | unsigned &NumProtocols) { |
| 2773 | ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2774 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2775 | // Sort protocols, keyed by name. |
| 2776 | std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); |
| 2777 | |
Douglas Gregor | 61cc296 | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 2778 | // Canonicalize. |
| 2779 | for (unsigned I = 0, N = NumProtocols; I != N; ++I) |
| 2780 | Protocols[I] = Protocols[I]->getCanonicalDecl(); |
| 2781 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2782 | // Remove duplicates. |
| 2783 | ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); |
| 2784 | NumProtocols = ProtocolsEnd-Protocols; |
| 2785 | } |
| 2786 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2787 | QualType ASTContext::getObjCObjectType(QualType BaseType, |
| 2788 | ObjCProtocolDecl * const *Protocols, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2789 | unsigned NumProtocols) const { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2790 | // If the base type is an interface and there aren't any protocols |
| 2791 | // to add, then the interface type will do just fine. |
| 2792 | if (!NumProtocols && isa<ObjCInterfaceType>(BaseType)) |
| 2793 | return BaseType; |
| 2794 | |
| 2795 | // Look in the folding set for an existing type. |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2796 | llvm::FoldingSetNodeID ID; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2797 | ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2798 | void *InsertPos = 0; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2799 | if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2800 | return QualType(QT, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2801 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2802 | // Build the canonical type, which has the canonical base type and |
| 2803 | // a sorted-and-uniqued list of protocols. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2804 | QualType Canonical; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2805 | bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols); |
| 2806 | if (!ProtocolsSorted || !BaseType.isCanonical()) { |
| 2807 | if (!ProtocolsSorted) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2808 | SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols, |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 2809 | Protocols + NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2810 | unsigned UniqueCount = NumProtocols; |
| 2811 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2812 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2813 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 2814 | &Sorted[0], UniqueCount); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2815 | } else { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2816 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 2817 | Protocols, NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2818 | } |
| 2819 | |
| 2820 | // Regenerate InsertPos. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2821 | ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2822 | } |
| 2823 | |
| 2824 | unsigned Size = sizeof(ObjCObjectTypeImpl); |
| 2825 | Size += NumProtocols * sizeof(ObjCProtocolDecl *); |
| 2826 | void *Mem = Allocate(Size, TypeAlignment); |
| 2827 | ObjCObjectTypeImpl *T = |
| 2828 | new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols); |
| 2829 | |
| 2830 | Types.push_back(T); |
| 2831 | ObjCObjectTypes.InsertNode(T, InsertPos); |
| 2832 | return QualType(T, 0); |
| 2833 | } |
| 2834 | |
| 2835 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 2836 | /// the given object type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2837 | QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2838 | llvm::FoldingSetNodeID ID; |
| 2839 | ObjCObjectPointerType::Profile(ID, ObjectT); |
| 2840 | |
| 2841 | void *InsertPos = 0; |
| 2842 | if (ObjCObjectPointerType *QT = |
| 2843 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2844 | return QualType(QT, 0); |
| 2845 | |
| 2846 | // Find the canonical object type. |
| 2847 | QualType Canonical; |
| 2848 | if (!ObjectT.isCanonical()) { |
| 2849 | Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT)); |
| 2850 | |
| 2851 | // Regenerate InsertPos. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2852 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2853 | } |
| 2854 | |
Douglas Gregor | fd6a088 | 2010-02-08 22:59:26 +0000 | [diff] [blame] | 2855 | // No match. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2856 | void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment); |
| 2857 | ObjCObjectPointerType *QType = |
| 2858 | new (Mem) ObjCObjectPointerType(Canonical, ObjectT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2859 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2860 | Types.push_back(QType); |
| 2861 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2862 | return QualType(QType, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2863 | } |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2864 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2865 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 2866 | /// specified ObjC interface decl. The list of protocols is optional. |
Douglas Gregor | 0af5501 | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 2867 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl, |
| 2868 | ObjCInterfaceDecl *PrevDecl) const { |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2869 | if (Decl->TypeForDecl) |
| 2870 | return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2871 | |
Douglas Gregor | 0af5501 | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 2872 | if (PrevDecl) { |
| 2873 | assert(PrevDecl->TypeForDecl && "previous decl has no TypeForDecl"); |
| 2874 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 2875 | return QualType(PrevDecl->TypeForDecl, 0); |
| 2876 | } |
| 2877 | |
Douglas Gregor | 8d2dbbf | 2011-12-16 16:34:57 +0000 | [diff] [blame] | 2878 | // Prefer the definition, if there is one. |
| 2879 | if (const ObjCInterfaceDecl *Def = Decl->getDefinition()) |
| 2880 | Decl = Def; |
| 2881 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2882 | void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment); |
| 2883 | ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl); |
| 2884 | Decl->TypeForDecl = T; |
| 2885 | Types.push_back(T); |
| 2886 | return QualType(T, 0); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2887 | } |
| 2888 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2889 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 2890 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2891 | /// multiple declarations that refer to "typeof(x)" all contain different |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2892 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2893 | /// on canonical type's (which are always unique). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2894 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) const { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2895 | TypeOfExprType *toe; |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2896 | if (tofExpr->isTypeDependent()) { |
| 2897 | llvm::FoldingSetNodeID ID; |
| 2898 | DependentTypeOfExprType::Profile(ID, *this, tofExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2899 | |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2900 | void *InsertPos = 0; |
| 2901 | DependentTypeOfExprType *Canon |
| 2902 | = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2903 | if (Canon) { |
| 2904 | // We already have a "canonical" version of an identical, dependent |
| 2905 | // typeof(expr) type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2906 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2907 | QualType((TypeOfExprType*)Canon, 0)); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 2908 | } else { |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2909 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2910 | Canon |
| 2911 | = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2912 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); |
| 2913 | toe = Canon; |
| 2914 | } |
| 2915 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2916 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2917 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2918 | } |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2919 | Types.push_back(toe); |
| 2920 | return QualType(toe, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2921 | } |
| 2922 | |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2923 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 2924 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 2925 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2926 | /// an issue. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2927 | /// on canonical type's (which are always unique). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2928 | QualType ASTContext::getTypeOfType(QualType tofType) const { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2929 | QualType Canonical = getCanonicalType(tofType); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2930 | TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2931 | Types.push_back(tot); |
| 2932 | return QualType(tot, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2933 | } |
| 2934 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2935 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2936 | /// getDecltypeType - Unlike many "get<Type>" functions, we don't unique |
| 2937 | /// DecltypeType AST's. The only motivation to unique these nodes would be |
| 2938 | /// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2939 | /// an issue. This doesn't effect the type checker, since it operates |
David Blaikie | 39e0203 | 2011-11-06 22:28:03 +0000 | [diff] [blame] | 2940 | /// on canonical types (which are always unique). |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 2941 | QualType ASTContext::getDecltypeType(Expr *e, QualType UnderlyingType) const { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2942 | DecltypeType *dt; |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2943 | |
| 2944 | // C++0x [temp.type]p2: |
| 2945 | // If an expression e involves a template parameter, decltype(e) denotes a |
| 2946 | // unique dependent type. Two such decltype-specifiers refer to the same |
| 2947 | // type only if their expressions are equivalent (14.5.6.1). |
| 2948 | if (e->isInstantiationDependent()) { |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2949 | llvm::FoldingSetNodeID ID; |
| 2950 | DependentDecltypeType::Profile(ID, *this, e); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2951 | |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2952 | void *InsertPos = 0; |
| 2953 | DependentDecltypeType *Canon |
| 2954 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2955 | if (Canon) { |
| 2956 | // We already have a "canonical" version of an equivalent, dependent |
| 2957 | // decltype type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2958 | dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy, |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2959 | QualType((DecltypeType*)Canon, 0)); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 2960 | } else { |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2961 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2962 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2963 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); |
| 2964 | dt = Canon; |
| 2965 | } |
| 2966 | } else { |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 2967 | dt = new (*this, TypeAlignment) DecltypeType(e, UnderlyingType, |
| 2968 | getCanonicalType(UnderlyingType)); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2969 | } |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2970 | Types.push_back(dt); |
| 2971 | return QualType(dt, 0); |
| 2972 | } |
| 2973 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 2974 | /// getUnaryTransformationType - We don't unique these, since the memory |
| 2975 | /// savings are minimal and these are rare. |
| 2976 | QualType ASTContext::getUnaryTransformType(QualType BaseType, |
| 2977 | QualType UnderlyingType, |
| 2978 | UnaryTransformType::UTTKind Kind) |
| 2979 | const { |
| 2980 | UnaryTransformType *Ty = |
Douglas Gregor | 69d9775 | 2011-05-25 17:51:54 +0000 | [diff] [blame] | 2981 | new (*this, TypeAlignment) UnaryTransformType (BaseType, UnderlyingType, |
| 2982 | Kind, |
| 2983 | UnderlyingType->isDependentType() ? |
Peter Collingbourne | 12fc4b0 | 2012-03-05 16:02:06 +0000 | [diff] [blame] | 2984 | QualType() : getCanonicalType(UnderlyingType)); |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 2985 | Types.push_back(Ty); |
| 2986 | return QualType(Ty, 0); |
| 2987 | } |
| 2988 | |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 2989 | /// getAutoType - We only unique auto types after they've been deduced. |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2990 | QualType ASTContext::getAutoType(QualType DeducedType) const { |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 2991 | void *InsertPos = 0; |
| 2992 | if (!DeducedType.isNull()) { |
| 2993 | // Look in the folding set for an existing type. |
| 2994 | llvm::FoldingSetNodeID ID; |
| 2995 | AutoType::Profile(ID, DeducedType); |
| 2996 | if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2997 | return QualType(AT, 0); |
| 2998 | } |
| 2999 | |
| 3000 | AutoType *AT = new (*this, TypeAlignment) AutoType(DeducedType); |
| 3001 | Types.push_back(AT); |
| 3002 | if (InsertPos) |
| 3003 | AutoTypes.InsertNode(AT, InsertPos); |
| 3004 | return QualType(AT, 0); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3005 | } |
| 3006 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3007 | /// getAtomicType - Return the uniqued reference to the atomic type for |
| 3008 | /// the given value type. |
| 3009 | QualType ASTContext::getAtomicType(QualType T) const { |
| 3010 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 3011 | // structure. |
| 3012 | llvm::FoldingSetNodeID ID; |
| 3013 | AtomicType::Profile(ID, T); |
| 3014 | |
| 3015 | void *InsertPos = 0; |
| 3016 | if (AtomicType *AT = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3017 | return QualType(AT, 0); |
| 3018 | |
| 3019 | // If the atomic value type isn't canonical, this won't be a canonical type |
| 3020 | // either, so fill in the canonical type field. |
| 3021 | QualType Canonical; |
| 3022 | if (!T.isCanonical()) { |
| 3023 | Canonical = getAtomicType(getCanonicalType(T)); |
| 3024 | |
| 3025 | // Get the new insert position for the node we care about. |
| 3026 | AtomicType *NewIP = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3027 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
| 3028 | } |
| 3029 | AtomicType *New = new (*this, TypeAlignment) AtomicType(T, Canonical); |
| 3030 | Types.push_back(New); |
| 3031 | AtomicTypes.InsertNode(New, InsertPos); |
| 3032 | return QualType(New, 0); |
| 3033 | } |
| 3034 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3035 | /// getAutoDeductType - Get type pattern for deducing against 'auto'. |
| 3036 | QualType ASTContext::getAutoDeductType() const { |
| 3037 | if (AutoDeductTy.isNull()) |
| 3038 | AutoDeductTy = getAutoType(QualType()); |
| 3039 | assert(!AutoDeductTy.isNull() && "can't build 'auto' pattern"); |
| 3040 | return AutoDeductTy; |
| 3041 | } |
| 3042 | |
| 3043 | /// getAutoRRefDeductType - Get type pattern for deducing against 'auto &&'. |
| 3044 | QualType ASTContext::getAutoRRefDeductType() const { |
| 3045 | if (AutoRRefDeductTy.isNull()) |
| 3046 | AutoRRefDeductTy = getRValueReferenceType(getAutoDeductType()); |
| 3047 | assert(!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern"); |
| 3048 | return AutoRRefDeductTy; |
| 3049 | } |
| 3050 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3051 | /// getTagDeclType - Return the unique reference to the type for the |
| 3052 | /// specified TagDecl (struct/union/class/enum) decl. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3053 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) const { |
Ted Kremenek | d778f88 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 3054 | assert (Decl); |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 3055 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 3056 | // away const? mutable? |
| 3057 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3058 | } |
| 3059 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3060 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 3061 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 3062 | /// needs to agree with the definition in <stddef.h>. |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 3063 | CanQualType ASTContext::getSizeType() const { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3064 | return getFromTargetType(Target->getSizeType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3065 | } |
| 3066 | |
Hans Wennborg | 29e97cb | 2011-10-27 08:29:09 +0000 | [diff] [blame] | 3067 | /// getIntMaxType - Return the unique type for "intmax_t" (C99 7.18.1.5). |
| 3068 | CanQualType ASTContext::getIntMaxType() const { |
| 3069 | return getFromTargetType(Target->getIntMaxType()); |
| 3070 | } |
| 3071 | |
| 3072 | /// getUIntMaxType - Return the unique type for "uintmax_t" (C99 7.18.1.5). |
| 3073 | CanQualType ASTContext::getUIntMaxType() const { |
| 3074 | return getFromTargetType(Target->getUIntMaxType()); |
| 3075 | } |
| 3076 | |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3077 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 3078 | /// Used when in C++, as a GCC extension. |
| 3079 | QualType ASTContext::getSignedWCharType() const { |
| 3080 | // FIXME: derive from "Target" ? |
| 3081 | return WCharTy; |
| 3082 | } |
| 3083 | |
| 3084 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 3085 | /// Used when in C++, as a GCC extension. |
| 3086 | QualType ASTContext::getUnsignedWCharType() const { |
| 3087 | // FIXME: derive from "Target" ? |
| 3088 | return UnsignedIntTy; |
| 3089 | } |
| 3090 | |
Hans Wennborg | 29e97cb | 2011-10-27 08:29:09 +0000 | [diff] [blame] | 3091 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17) |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 3092 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 3093 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3094 | return getFromTargetType(Target->getPtrDiffType(0)); |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 3095 | } |
| 3096 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3097 | //===----------------------------------------------------------------------===// |
| 3098 | // Type Operators |
| 3099 | //===----------------------------------------------------------------------===// |
| 3100 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3101 | CanQualType ASTContext::getCanonicalParamType(QualType T) const { |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3102 | // Push qualifiers into arrays, and then discard any remaining |
| 3103 | // qualifiers. |
| 3104 | T = getCanonicalType(T); |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3105 | T = getVariableArrayDecayedType(T); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3106 | const Type *Ty = T.getTypePtr(); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3107 | QualType Result; |
| 3108 | if (isa<ArrayType>(Ty)) { |
| 3109 | Result = getArrayDecayedType(QualType(Ty,0)); |
| 3110 | } else if (isa<FunctionType>(Ty)) { |
| 3111 | Result = getPointerType(QualType(Ty, 0)); |
| 3112 | } else { |
| 3113 | Result = QualType(Ty, 0); |
| 3114 | } |
| 3115 | |
| 3116 | return CanQualType::CreateUnsafe(Result); |
| 3117 | } |
| 3118 | |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3119 | QualType ASTContext::getUnqualifiedArrayType(QualType type, |
| 3120 | Qualifiers &quals) { |
| 3121 | SplitQualType splitType = type.getSplitUnqualifiedType(); |
| 3122 | |
| 3123 | // FIXME: getSplitUnqualifiedType() actually walks all the way to |
| 3124 | // the unqualified desugared type and then drops it on the floor. |
| 3125 | // We then have to strip that sugar back off with |
| 3126 | // getUnqualifiedDesugaredType(), which is silly. |
| 3127 | const ArrayType *AT = |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3128 | dyn_cast<ArrayType>(splitType.Ty->getUnqualifiedDesugaredType()); |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3129 | |
| 3130 | // 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] | 3131 | if (!AT) { |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3132 | quals = splitType.Quals; |
| 3133 | return QualType(splitType.Ty, 0); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3134 | } |
| 3135 | |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3136 | // Otherwise, recurse on the array's element type. |
| 3137 | QualType elementType = AT->getElementType(); |
| 3138 | QualType unqualElementType = getUnqualifiedArrayType(elementType, quals); |
| 3139 | |
| 3140 | // If that didn't change the element type, AT has no qualifiers, so we |
| 3141 | // can just use the results in splitType. |
| 3142 | if (elementType == unqualElementType) { |
| 3143 | assert(quals.empty()); // from the recursive call |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3144 | quals = splitType.Quals; |
| 3145 | return QualType(splitType.Ty, 0); |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3146 | } |
| 3147 | |
| 3148 | // Otherwise, add in the qualifiers from the outermost type, then |
| 3149 | // build the type back up. |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3150 | quals.addConsistentQualifiers(splitType.Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3151 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3152 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3153 | return getConstantArrayType(unqualElementType, CAT->getSize(), |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3154 | CAT->getSizeModifier(), 0); |
| 3155 | } |
| 3156 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3157 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3158 | return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3159 | } |
| 3160 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3161 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3162 | return getVariableArrayType(unqualElementType, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3163 | VAT->getSizeExpr(), |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3164 | VAT->getSizeModifier(), |
| 3165 | VAT->getIndexTypeCVRQualifiers(), |
| 3166 | VAT->getBracketsRange()); |
| 3167 | } |
| 3168 | |
| 3169 | const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT); |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3170 | return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(), |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3171 | DSAT->getSizeModifier(), 0, |
| 3172 | SourceRange()); |
| 3173 | } |
| 3174 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3175 | /// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that |
| 3176 | /// may be similar (C++ 4.4), replaces T1 and T2 with the type that |
| 3177 | /// they point to and return true. If T1 and T2 aren't pointer types |
| 3178 | /// or pointer-to-member types, or if they are not similar at this |
| 3179 | /// level, returns false and leaves T1 and T2 unchanged. Top-level |
| 3180 | /// qualifiers on T1 and T2 are ignored. This function will typically |
| 3181 | /// be called in a loop that successively "unwraps" pointer and |
| 3182 | /// pointer-to-member types to compare them at each level. |
| 3183 | bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) { |
| 3184 | const PointerType *T1PtrType = T1->getAs<PointerType>(), |
| 3185 | *T2PtrType = T2->getAs<PointerType>(); |
| 3186 | if (T1PtrType && T2PtrType) { |
| 3187 | T1 = T1PtrType->getPointeeType(); |
| 3188 | T2 = T2PtrType->getPointeeType(); |
| 3189 | return true; |
| 3190 | } |
| 3191 | |
| 3192 | const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(), |
| 3193 | *T2MPType = T2->getAs<MemberPointerType>(); |
| 3194 | if (T1MPType && T2MPType && |
| 3195 | hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0), |
| 3196 | QualType(T2MPType->getClass(), 0))) { |
| 3197 | T1 = T1MPType->getPointeeType(); |
| 3198 | T2 = T2MPType->getPointeeType(); |
| 3199 | return true; |
| 3200 | } |
| 3201 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3202 | if (getLangOpts().ObjC1) { |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3203 | const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(), |
| 3204 | *T2OPType = T2->getAs<ObjCObjectPointerType>(); |
| 3205 | if (T1OPType && T2OPType) { |
| 3206 | T1 = T1OPType->getPointeeType(); |
| 3207 | T2 = T2OPType->getPointeeType(); |
| 3208 | return true; |
| 3209 | } |
| 3210 | } |
| 3211 | |
| 3212 | // FIXME: Block pointers, too? |
| 3213 | |
| 3214 | return false; |
| 3215 | } |
| 3216 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3217 | DeclarationNameInfo |
| 3218 | ASTContext::getNameForTemplate(TemplateName Name, |
| 3219 | SourceLocation NameLoc) const { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3220 | switch (Name.getKind()) { |
| 3221 | case TemplateName::QualifiedTemplate: |
| 3222 | case TemplateName::Template: |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3223 | // DNInfo work in progress: CHECKME: what about DNLoc? |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3224 | return DeclarationNameInfo(Name.getAsTemplateDecl()->getDeclName(), |
| 3225 | NameLoc); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3226 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3227 | case TemplateName::OverloadedTemplate: { |
| 3228 | OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate(); |
| 3229 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 3230 | return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc); |
| 3231 | } |
| 3232 | |
| 3233 | case TemplateName::DependentTemplate: { |
| 3234 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3235 | DeclarationName DName; |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3236 | if (DTN->isIdentifier()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3237 | DName = DeclarationNames.getIdentifier(DTN->getIdentifier()); |
| 3238 | return DeclarationNameInfo(DName, NameLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3239 | } else { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3240 | DName = DeclarationNames.getCXXOperatorName(DTN->getOperator()); |
| 3241 | // DNInfo work in progress: FIXME: source locations? |
| 3242 | DeclarationNameLoc DNLoc; |
| 3243 | DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding(); |
| 3244 | DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding(); |
| 3245 | return DeclarationNameInfo(DName, NameLoc, DNLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3246 | } |
| 3247 | } |
| 3248 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3249 | case TemplateName::SubstTemplateTemplateParm: { |
| 3250 | SubstTemplateTemplateParmStorage *subst |
| 3251 | = Name.getAsSubstTemplateTemplateParm(); |
| 3252 | return DeclarationNameInfo(subst->getParameter()->getDeclName(), |
| 3253 | NameLoc); |
| 3254 | } |
| 3255 | |
| 3256 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 3257 | SubstTemplateTemplateParmPackStorage *subst |
| 3258 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 3259 | return DeclarationNameInfo(subst->getParameterPack()->getDeclName(), |
| 3260 | NameLoc); |
| 3261 | } |
| 3262 | } |
| 3263 | |
| 3264 | llvm_unreachable("bad template name kind!"); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3265 | } |
| 3266 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3267 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3268 | switch (Name.getKind()) { |
| 3269 | case TemplateName::QualifiedTemplate: |
| 3270 | case TemplateName::Template: { |
| 3271 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3272 | if (TemplateTemplateParmDecl *TTP |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3273 | = dyn_cast<TemplateTemplateParmDecl>(Template)) |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3274 | Template = getCanonicalTemplateTemplateParmDecl(TTP); |
| 3275 | |
| 3276 | // The canonical template name is the canonical template declaration. |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 3277 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3278 | } |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 3279 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3280 | case TemplateName::OverloadedTemplate: |
| 3281 | llvm_unreachable("cannot canonicalize overloaded template"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3282 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3283 | case TemplateName::DependentTemplate: { |
| 3284 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 3285 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 3286 | return DTN->CanonicalTemplateName; |
| 3287 | } |
| 3288 | |
| 3289 | case TemplateName::SubstTemplateTemplateParm: { |
| 3290 | SubstTemplateTemplateParmStorage *subst |
| 3291 | = Name.getAsSubstTemplateTemplateParm(); |
| 3292 | return getCanonicalTemplateName(subst->getReplacement()); |
| 3293 | } |
| 3294 | |
| 3295 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 3296 | SubstTemplateTemplateParmPackStorage *subst |
| 3297 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 3298 | TemplateTemplateParmDecl *canonParameter |
| 3299 | = getCanonicalTemplateTemplateParmDecl(subst->getParameterPack()); |
| 3300 | TemplateArgument canonArgPack |
| 3301 | = getCanonicalTemplateArgument(subst->getArgumentPack()); |
| 3302 | return getSubstTemplateTemplateParmPack(canonParameter, canonArgPack); |
| 3303 | } |
| 3304 | } |
| 3305 | |
| 3306 | llvm_unreachable("bad template name!"); |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 3307 | } |
| 3308 | |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 3309 | bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) { |
| 3310 | X = getCanonicalTemplateName(X); |
| 3311 | Y = getCanonicalTemplateName(Y); |
| 3312 | return X.getAsVoidPointer() == Y.getAsVoidPointer(); |
| 3313 | } |
| 3314 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3315 | TemplateArgument |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3316 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const { |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3317 | switch (Arg.getKind()) { |
| 3318 | case TemplateArgument::Null: |
| 3319 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3320 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3321 | case TemplateArgument::Expression: |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3322 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3323 | |
Douglas Gregor | d2008e2 | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 3324 | case TemplateArgument::Declaration: { |
| 3325 | if (Decl *D = Arg.getAsDecl()) |
| 3326 | return TemplateArgument(D->getCanonicalDecl()); |
| 3327 | return TemplateArgument((Decl*)0); |
| 3328 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3329 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3330 | case TemplateArgument::Template: |
| 3331 | return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate())); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3332 | |
| 3333 | case TemplateArgument::TemplateExpansion: |
| 3334 | return TemplateArgument(getCanonicalTemplateName( |
| 3335 | Arg.getAsTemplateOrTemplatePattern()), |
Douglas Gregor | 2be29f4 | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 3336 | Arg.getNumTemplateExpansions()); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3337 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3338 | case TemplateArgument::Integral: |
Benjamin Kramer | 8552437 | 2012-06-07 15:09:51 +0000 | [diff] [blame^] | 3339 | return TemplateArgument(Arg, getCanonicalType(Arg.getIntegralType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3340 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3341 | case TemplateArgument::Type: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3342 | return TemplateArgument(getCanonicalType(Arg.getAsType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3343 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3344 | case TemplateArgument::Pack: { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 3345 | if (Arg.pack_size() == 0) |
| 3346 | return Arg; |
| 3347 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3348 | TemplateArgument *CanonArgs |
| 3349 | = new (*this) TemplateArgument[Arg.pack_size()]; |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3350 | unsigned Idx = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3351 | for (TemplateArgument::pack_iterator A = Arg.pack_begin(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3352 | AEnd = Arg.pack_end(); |
| 3353 | A != AEnd; (void)++A, ++Idx) |
| 3354 | CanonArgs[Idx] = getCanonicalTemplateArgument(*A); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3355 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3356 | return TemplateArgument(CanonArgs, Arg.pack_size()); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3357 | } |
| 3358 | } |
| 3359 | |
| 3360 | // Silence GCC warning |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3361 | llvm_unreachable("Unhandled template argument kind"); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3362 | } |
| 3363 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3364 | NestedNameSpecifier * |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3365 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3366 | if (!NNS) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3367 | return 0; |
| 3368 | |
| 3369 | switch (NNS->getKind()) { |
| 3370 | case NestedNameSpecifier::Identifier: |
| 3371 | // Canonicalize the prefix but keep the identifier the same. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3372 | return NestedNameSpecifier::Create(*this, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3373 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 3374 | NNS->getAsIdentifier()); |
| 3375 | |
| 3376 | case NestedNameSpecifier::Namespace: |
| 3377 | // A namespace is canonical; build a nested-name-specifier with |
| 3378 | // this namespace and no prefix. |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 3379 | return NestedNameSpecifier::Create(*this, 0, |
| 3380 | NNS->getAsNamespace()->getOriginalNamespace()); |
| 3381 | |
| 3382 | case NestedNameSpecifier::NamespaceAlias: |
| 3383 | // A namespace is canonical; build a nested-name-specifier with |
| 3384 | // this namespace and no prefix. |
| 3385 | return NestedNameSpecifier::Create(*this, 0, |
| 3386 | NNS->getAsNamespaceAlias()->getNamespace() |
| 3387 | ->getOriginalNamespace()); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3388 | |
| 3389 | case NestedNameSpecifier::TypeSpec: |
| 3390 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 3391 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3392 | |
| 3393 | // If we have some kind of dependent-named type (e.g., "typename T::type"), |
| 3394 | // break it apart into its prefix and identifier, then reconsititute those |
| 3395 | // as the canonical nested-name-specifier. This is required to canonicalize |
| 3396 | // a dependent nested-name-specifier involving typedefs of dependent-name |
| 3397 | // types, e.g., |
| 3398 | // typedef typename T::type T1; |
| 3399 | // typedef typename T1::type T2; |
Eli Friedman | 16412ef | 2012-03-03 04:09:56 +0000 | [diff] [blame] | 3400 | if (const DependentNameType *DNT = T->getAs<DependentNameType>()) |
| 3401 | return NestedNameSpecifier::Create(*this, DNT->getQualifier(), |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3402 | const_cast<IdentifierInfo *>(DNT->getIdentifier())); |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3403 | |
Eli Friedman | 16412ef | 2012-03-03 04:09:56 +0000 | [diff] [blame] | 3404 | // Otherwise, just canonicalize the type, and force it to be a TypeSpec. |
| 3405 | // FIXME: Why are TypeSpec and TypeSpecWithTemplate distinct in the |
| 3406 | // first place? |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3407 | return NestedNameSpecifier::Create(*this, 0, false, |
| 3408 | const_cast<Type*>(T.getTypePtr())); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3409 | } |
| 3410 | |
| 3411 | case NestedNameSpecifier::Global: |
| 3412 | // The global specifier is canonical and unique. |
| 3413 | return NNS; |
| 3414 | } |
| 3415 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3416 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3417 | } |
| 3418 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3419 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3420 | const ArrayType *ASTContext::getAsArrayType(QualType T) const { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3421 | // Handle the non-qualified case efficiently. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3422 | if (!T.hasLocalQualifiers()) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3423 | // Handle the common positive case fast. |
| 3424 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 3425 | return AT; |
| 3426 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3427 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3428 | // Handle the common negative case fast. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3429 | if (!isa<ArrayType>(T.getCanonicalType())) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3430 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3431 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3432 | // Apply any qualifiers from the array type to the element type. This |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3433 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 3434 | // 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] | 3435 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3436 | // If we get here, we either have type qualifiers on the type, or we have |
| 3437 | // 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] | 3438 | // we must propagate them down into the element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3439 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3440 | SplitQualType split = T.getSplitDesugaredType(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3441 | Qualifiers qs = split.Quals; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3442 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3443 | // If we have a simple case, just return now. |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3444 | const ArrayType *ATy = dyn_cast<ArrayType>(split.Ty); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3445 | if (ATy == 0 || qs.empty()) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3446 | return ATy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3447 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3448 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 3449 | // qualifiers into the array element type and return a new array type. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3450 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3451 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3452 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 3453 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 3454 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3455 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3456 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 3457 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 3458 | IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3459 | IAT->getIndexTypeCVRQualifiers())); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3460 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3461 | if (const DependentSizedArrayType *DSAT |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3462 | = dyn_cast<DependentSizedArrayType>(ATy)) |
| 3463 | return cast<ArrayType>( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3464 | getDependentSizedArrayType(NewEltTy, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3465 | DSAT->getSizeExpr(), |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3466 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3467 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3468 | DSAT->getBracketsRange())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3469 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3470 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3471 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3472 | VAT->getSizeExpr(), |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3473 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3474 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3475 | VAT->getBracketsRange())); |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 3476 | } |
| 3477 | |
Abramo Bagnara | ad9689f | 2012-05-17 12:44:05 +0000 | [diff] [blame] | 3478 | QualType ASTContext::getAdjustedParameterType(QualType T) const { |
Douglas Gregor | 79e6bd3 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 3479 | // C99 6.7.5.3p7: |
| 3480 | // A declaration of a parameter as "array of type" shall be |
| 3481 | // adjusted to "qualified pointer to type", where the type |
| 3482 | // qualifiers (if any) are those specified within the [ and ] of |
| 3483 | // the array type derivation. |
| 3484 | if (T->isArrayType()) |
| 3485 | return getArrayDecayedType(T); |
| 3486 | |
| 3487 | // C99 6.7.5.3p8: |
| 3488 | // A declaration of a parameter as "function returning type" |
| 3489 | // shall be adjusted to "pointer to function returning type", as |
| 3490 | // in 6.3.2.1. |
| 3491 | if (T->isFunctionType()) |
| 3492 | return getPointerType(T); |
| 3493 | |
| 3494 | return T; |
| 3495 | } |
| 3496 | |
Abramo Bagnara | ad9689f | 2012-05-17 12:44:05 +0000 | [diff] [blame] | 3497 | QualType ASTContext::getSignatureParameterType(QualType T) const { |
Douglas Gregor | 79e6bd3 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 3498 | T = getVariableArrayDecayedType(T); |
| 3499 | T = getAdjustedParameterType(T); |
| 3500 | return T.getUnqualifiedType(); |
| 3501 | } |
| 3502 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3503 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 3504 | /// specified array type to a pointer. This operation is non-trivial when |
| 3505 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 3506 | /// this returns a pointer to a properly qualified element of the array. |
| 3507 | /// |
| 3508 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3509 | QualType ASTContext::getArrayDecayedType(QualType Ty) const { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3510 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 3511 | // typedefs in the element type of the array. This also handles propagation |
| 3512 | // of type qualifiers from the array type into the element type if present |
| 3513 | // (C99 6.7.3p8). |
| 3514 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 3515 | assert(PrettyArrayType && "Not an array type!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3516 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3517 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3518 | |
| 3519 | // int x[restrict 4] -> int *restrict |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3520 | return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3521 | } |
| 3522 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3523 | QualType ASTContext::getBaseElementType(const ArrayType *array) const { |
| 3524 | return getBaseElementType(array->getElementType()); |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 3525 | } |
| 3526 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3527 | QualType ASTContext::getBaseElementType(QualType type) const { |
| 3528 | Qualifiers qs; |
| 3529 | while (true) { |
| 3530 | SplitQualType split = type.getSplitDesugaredType(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3531 | const ArrayType *array = split.Ty->getAsArrayTypeUnsafe(); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3532 | if (!array) break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3533 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3534 | type = array->getElementType(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3535 | qs.addConsistentQualifiers(split.Quals); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3536 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3537 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3538 | return getQualifiedType(type, qs); |
Anders Carlsson | 6183a99 | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 3539 | } |
| 3540 | |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 3541 | /// getConstantArrayElementCount - Returns number of constant array elements. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3542 | uint64_t |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 3543 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
| 3544 | uint64_t ElementCount = 1; |
| 3545 | do { |
| 3546 | ElementCount *= CA->getSize().getZExtValue(); |
| 3547 | CA = dyn_cast<ConstantArrayType>(CA->getElementType()); |
| 3548 | } while (CA); |
| 3549 | return ElementCount; |
| 3550 | } |
| 3551 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3552 | /// getFloatingRank - Return a relative rank for floating point types. |
| 3553 | /// 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] | 3554 | static FloatingRank getFloatingRank(QualType T) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3555 | if (const ComplexType *CT = T->getAs<ComplexType>()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3556 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3557 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3558 | assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"); |
| 3559 | switch (T->getAs<BuiltinType>()->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3560 | default: llvm_unreachable("getFloatingRank(): not a floating type"); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3561 | case BuiltinType::Half: return HalfRank; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3562 | case BuiltinType::Float: return FloatRank; |
| 3563 | case BuiltinType::Double: return DoubleRank; |
| 3564 | case BuiltinType::LongDouble: return LongDoubleRank; |
| 3565 | } |
| 3566 | } |
| 3567 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3568 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 3569 | /// point or a complex type (based on typeDomain/typeSize). |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 3570 | /// 'typeDomain' is a real floating point or complex type. |
| 3571 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 3572 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 3573 | QualType Domain) const { |
| 3574 | FloatingRank EltRank = getFloatingRank(Size); |
| 3575 | if (Domain->isComplexType()) { |
| 3576 | switch (EltRank) { |
David Blaikie | 561d3ab | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 3577 | case HalfRank: llvm_unreachable("Complex half is not supported"); |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 3578 | case FloatRank: return FloatComplexTy; |
| 3579 | case DoubleRank: return DoubleComplexTy; |
| 3580 | case LongDoubleRank: return LongDoubleComplexTy; |
| 3581 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3582 | } |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 3583 | |
| 3584 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 3585 | switch (EltRank) { |
David Blaikie | 561d3ab | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 3586 | case HalfRank: llvm_unreachable("Half ranks are not valid here"); |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 3587 | case FloatRank: return FloatTy; |
| 3588 | case DoubleRank: return DoubleTy; |
| 3589 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 3590 | } |
David Blaikie | 561d3ab | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 3591 | llvm_unreachable("getFloatingRank(): illegal value for rank"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3592 | } |
| 3593 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3594 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 3595 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 3596 | /// '_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] | 3597 | /// LHS < RHS, return -1. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3598 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const { |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3599 | FloatingRank LHSR = getFloatingRank(LHS); |
| 3600 | FloatingRank RHSR = getFloatingRank(RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3601 | |
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 0; |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3604 | if (LHSR > RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 3605 | return 1; |
| 3606 | return -1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3607 | } |
| 3608 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3609 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 3610 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 3611 | /// or if it is not canonicalized. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3612 | unsigned ASTContext::getIntegerRank(const Type *T) const { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 3613 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3614 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3615 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3616 | default: llvm_unreachable("getIntegerRank(): not a built-in integer"); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3617 | case BuiltinType::Bool: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3618 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3619 | case BuiltinType::Char_S: |
| 3620 | case BuiltinType::Char_U: |
| 3621 | case BuiltinType::SChar: |
| 3622 | case BuiltinType::UChar: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3623 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3624 | case BuiltinType::Short: |
| 3625 | case BuiltinType::UShort: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3626 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3627 | case BuiltinType::Int: |
| 3628 | case BuiltinType::UInt: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3629 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3630 | case BuiltinType::Long: |
| 3631 | case BuiltinType::ULong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3632 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3633 | case BuiltinType::LongLong: |
| 3634 | case BuiltinType::ULongLong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3635 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 3636 | case BuiltinType::Int128: |
| 3637 | case BuiltinType::UInt128: |
| 3638 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3639 | } |
| 3640 | } |
| 3641 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3642 | /// \brief Whether this is a promotable bitfield reference according |
| 3643 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
| 3644 | /// |
| 3645 | /// \returns the type this bit-field will promote to, or NULL if no |
| 3646 | /// promotion occurs. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3647 | QualType ASTContext::isPromotableBitField(Expr *E) const { |
Douglas Gregor | ceafbde | 2010-05-24 20:13:53 +0000 | [diff] [blame] | 3648 | if (E->isTypeDependent() || E->isValueDependent()) |
| 3649 | return QualType(); |
| 3650 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3651 | FieldDecl *Field = E->getBitField(); |
| 3652 | if (!Field) |
| 3653 | return QualType(); |
| 3654 | |
| 3655 | QualType FT = Field->getType(); |
| 3656 | |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3657 | uint64_t BitWidth = Field->getBitWidthValue(*this); |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3658 | uint64_t IntSize = getTypeSize(IntTy); |
| 3659 | // GCC extension compatibility: if the bit-field size is less than or equal |
| 3660 | // to the size of int, it gets promoted no matter what its type is. |
| 3661 | // For instance, unsigned long bf : 4 gets promoted to signed int. |
| 3662 | if (BitWidth < IntSize) |
| 3663 | return IntTy; |
| 3664 | |
| 3665 | if (BitWidth == IntSize) |
| 3666 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
| 3667 | |
| 3668 | // Types bigger than int are not subject to promotions, and therefore act |
| 3669 | // like the base type. |
| 3670 | // FIXME: This doesn't quite match what gcc does, but what gcc does here |
| 3671 | // is ridiculous. |
| 3672 | return QualType(); |
| 3673 | } |
| 3674 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3675 | /// getPromotedIntegerType - Returns the type that Promotable will |
| 3676 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
| 3677 | /// integer type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3678 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) const { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3679 | assert(!Promotable.isNull()); |
| 3680 | assert(Promotable->isPromotableIntegerType()); |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 3681 | if (const EnumType *ET = Promotable->getAs<EnumType>()) |
| 3682 | return ET->getDecl()->getPromotionType(); |
Eli Friedman | 68a2dc4 | 2011-10-26 07:22:48 +0000 | [diff] [blame] | 3683 | |
| 3684 | if (const BuiltinType *BT = Promotable->getAs<BuiltinType>()) { |
| 3685 | // C++ [conv.prom]: A prvalue of type char16_t, char32_t, or wchar_t |
| 3686 | // (3.9.1) can be converted to a prvalue of the first of the following |
| 3687 | // types that can represent all the values of its underlying type: |
| 3688 | // int, unsigned int, long int, unsigned long int, long long int, or |
| 3689 | // unsigned long long int [...] |
| 3690 | // FIXME: Is there some better way to compute this? |
| 3691 | if (BT->getKind() == BuiltinType::WChar_S || |
| 3692 | BT->getKind() == BuiltinType::WChar_U || |
| 3693 | BT->getKind() == BuiltinType::Char16 || |
| 3694 | BT->getKind() == BuiltinType::Char32) { |
| 3695 | bool FromIsSigned = BT->getKind() == BuiltinType::WChar_S; |
| 3696 | uint64_t FromSize = getTypeSize(BT); |
| 3697 | QualType PromoteTypes[] = { IntTy, UnsignedIntTy, LongTy, UnsignedLongTy, |
| 3698 | LongLongTy, UnsignedLongLongTy }; |
| 3699 | for (size_t Idx = 0; Idx < llvm::array_lengthof(PromoteTypes); ++Idx) { |
| 3700 | uint64_t ToSize = getTypeSize(PromoteTypes[Idx]); |
| 3701 | if (FromSize < ToSize || |
| 3702 | (FromSize == ToSize && |
| 3703 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) |
| 3704 | return PromoteTypes[Idx]; |
| 3705 | } |
| 3706 | llvm_unreachable("char type should fit into long long"); |
| 3707 | } |
| 3708 | } |
| 3709 | |
| 3710 | // At this point, we should have a signed or unsigned integer type. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3711 | if (Promotable->isSignedIntegerType()) |
| 3712 | return IntTy; |
| 3713 | uint64_t PromotableSize = getTypeSize(Promotable); |
| 3714 | uint64_t IntSize = getTypeSize(IntTy); |
| 3715 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
| 3716 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
| 3717 | } |
| 3718 | |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 3719 | /// \brief Recurses in pointer/array types until it finds an objc retainable |
| 3720 | /// type and returns its ownership. |
| 3721 | Qualifiers::ObjCLifetime ASTContext::getInnerObjCOwnership(QualType T) const { |
| 3722 | while (!T.isNull()) { |
| 3723 | if (T.getObjCLifetime() != Qualifiers::OCL_None) |
| 3724 | return T.getObjCLifetime(); |
| 3725 | if (T->isArrayType()) |
| 3726 | T = getBaseElementType(T); |
| 3727 | else if (const PointerType *PT = T->getAs<PointerType>()) |
| 3728 | T = PT->getPointeeType(); |
| 3729 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
Argyrios Kyrtzidis | 28445f0 | 2011-07-01 23:01:46 +0000 | [diff] [blame] | 3730 | T = RT->getPointeeType(); |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 3731 | else |
| 3732 | break; |
| 3733 | } |
| 3734 | |
| 3735 | return Qualifiers::OCL_None; |
| 3736 | } |
| 3737 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3738 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3739 | /// 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] | 3740 | /// LHS < RHS, return -1. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3741 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3742 | const Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 3743 | const Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3744 | if (LHSC == RHSC) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3745 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3746 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 3747 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3748 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3749 | unsigned LHSRank = getIntegerRank(LHSC); |
| 3750 | unsigned RHSRank = getIntegerRank(RHSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3751 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3752 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 3753 | if (LHSRank == RHSRank) return 0; |
| 3754 | return LHSRank > RHSRank ? 1 : -1; |
| 3755 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3756 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3757 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 3758 | if (LHSUnsigned) { |
| 3759 | // If the unsigned [LHS] type is larger, return it. |
| 3760 | if (LHSRank >= RHSRank) |
| 3761 | return 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3762 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3763 | // If the signed type can represent all values of the unsigned type, it |
| 3764 | // 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] | 3765 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3766 | return -1; |
| 3767 | } |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3768 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3769 | // If the unsigned [RHS] type is larger, return it. |
| 3770 | if (RHSRank >= LHSRank) |
| 3771 | return -1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3772 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3773 | // If the signed type can represent all values of the unsigned type, it |
| 3774 | // 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] | 3775 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3776 | return 1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3777 | } |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3778 | |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3779 | static RecordDecl * |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3780 | CreateRecordDecl(const ASTContext &Ctx, RecordDecl::TagKind TK, |
| 3781 | DeclContext *DC, IdentifierInfo *Id) { |
| 3782 | SourceLocation Loc; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3783 | if (Ctx.getLangOpts().CPlusPlus) |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3784 | return CXXRecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3785 | else |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3786 | return RecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3787 | } |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3788 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3789 | // getCFConstantStringType - Return the type used for constant CFStrings. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3790 | QualType ASTContext::getCFConstantStringType() const { |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3791 | if (!CFConstantStringTypeDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3792 | CFConstantStringTypeDecl = |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3793 | CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3794 | &Idents.get("NSConstantString")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3795 | CFConstantStringTypeDecl->startDefinition(); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3796 | |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 3797 | QualType FieldTypes[4]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3798 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3799 | // const int *isa; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3800 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 3801 | // int flags; |
| 3802 | FieldTypes[1] = IntTy; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3803 | // const char *str; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3804 | FieldTypes[2] = getPointerType(CharTy.withConst()); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3805 | // long length; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3806 | FieldTypes[3] = LongTy; |
| 3807 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3808 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3809 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3810 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3811 | SourceLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3812 | SourceLocation(), 0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3813 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3814 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3815 | /*Mutable=*/false, |
| 3816 | /*HasInit=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3817 | Field->setAccess(AS_public); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3818 | CFConstantStringTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3819 | } |
| 3820 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3821 | CFConstantStringTypeDecl->completeDefinition(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3822 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3823 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3824 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 8467583 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 3825 | } |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 3826 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3827 | void ASTContext::setCFConstantStringType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3828 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3829 | assert(Rec && "Invalid CFConstantStringType"); |
| 3830 | CFConstantStringTypeDecl = Rec->getDecl(); |
| 3831 | } |
| 3832 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3833 | QualType ASTContext::getBlockDescriptorType() const { |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3834 | if (BlockDescriptorType) |
| 3835 | return getTagDeclType(BlockDescriptorType); |
| 3836 | |
| 3837 | RecordDecl *T; |
| 3838 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3839 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3840 | &Idents.get("__block_descriptor")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3841 | T->startDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3842 | |
| 3843 | QualType FieldTypes[] = { |
| 3844 | UnsignedLongTy, |
| 3845 | UnsignedLongTy, |
| 3846 | }; |
| 3847 | |
| 3848 | const char *FieldNames[] = { |
| 3849 | "reserved", |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3850 | "Size" |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3851 | }; |
| 3852 | |
| 3853 | for (size_t i = 0; i < 2; ++i) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3854 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3855 | SourceLocation(), |
| 3856 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3857 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3858 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3859 | /*Mutable=*/false, |
| 3860 | /*HasInit=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3861 | Field->setAccess(AS_public); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3862 | T->addDecl(Field); |
| 3863 | } |
| 3864 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3865 | T->completeDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3866 | |
| 3867 | BlockDescriptorType = T; |
| 3868 | |
| 3869 | return getTagDeclType(BlockDescriptorType); |
| 3870 | } |
| 3871 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3872 | QualType ASTContext::getBlockDescriptorExtendedType() const { |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3873 | if (BlockDescriptorExtendedType) |
| 3874 | return getTagDeclType(BlockDescriptorExtendedType); |
| 3875 | |
| 3876 | RecordDecl *T; |
| 3877 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3878 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3879 | &Idents.get("__block_descriptor_withcopydispose")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3880 | T->startDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3881 | |
| 3882 | QualType FieldTypes[] = { |
| 3883 | UnsignedLongTy, |
| 3884 | UnsignedLongTy, |
| 3885 | getPointerType(VoidPtrTy), |
| 3886 | getPointerType(VoidPtrTy) |
| 3887 | }; |
| 3888 | |
| 3889 | const char *FieldNames[] = { |
| 3890 | "reserved", |
| 3891 | "Size", |
| 3892 | "CopyFuncPtr", |
| 3893 | "DestroyFuncPtr" |
| 3894 | }; |
| 3895 | |
| 3896 | for (size_t i = 0; i < 4; ++i) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3897 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3898 | SourceLocation(), |
| 3899 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3900 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3901 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3902 | /*Mutable=*/false, |
| 3903 | /*HasInit=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3904 | Field->setAccess(AS_public); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3905 | T->addDecl(Field); |
| 3906 | } |
| 3907 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3908 | T->completeDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3909 | |
| 3910 | BlockDescriptorExtendedType = T; |
| 3911 | |
| 3912 | return getTagDeclType(BlockDescriptorExtendedType); |
| 3913 | } |
| 3914 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3915 | bool ASTContext::BlockRequiresCopying(QualType Ty) const { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3916 | if (Ty->isObjCRetainableType()) |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3917 | return true; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3918 | if (getLangOpts().CPlusPlus) { |
Fariborz Jahanian | e38be61 | 2010-11-17 00:21:28 +0000 | [diff] [blame] | 3919 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3920 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 3921 | return RD->hasConstCopyConstructor(); |
Fariborz Jahanian | e38be61 | 2010-11-17 00:21:28 +0000 | [diff] [blame] | 3922 | |
| 3923 | } |
| 3924 | } |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3925 | return false; |
| 3926 | } |
| 3927 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3928 | QualType |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3929 | ASTContext::BuildByRefType(StringRef DeclName, QualType Ty) const { |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3930 | // type = struct __Block_byref_1_X { |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3931 | // void *__isa; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3932 | // struct __Block_byref_1_X *__forwarding; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3933 | // unsigned int __flags; |
| 3934 | // unsigned int __size; |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 3935 | // void *__copy_helper; // as needed |
| 3936 | // void *__destroy_help // as needed |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3937 | // int X; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3938 | // } * |
| 3939 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3940 | bool HasCopyAndDispose = BlockRequiresCopying(Ty); |
| 3941 | |
| 3942 | // FIXME: Move up |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 3943 | SmallString<36> Name; |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 3944 | llvm::raw_svector_ostream(Name) << "__Block_byref_" << |
| 3945 | ++UniqueBlockByRefTypeID << '_' << DeclName; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3946 | RecordDecl *T; |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3947 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, &Idents.get(Name.str())); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3948 | T->startDefinition(); |
| 3949 | QualType Int32Ty = IntTy; |
| 3950 | assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported"); |
| 3951 | QualType FieldTypes[] = { |
| 3952 | getPointerType(VoidPtrTy), |
| 3953 | getPointerType(getTagDeclType(T)), |
| 3954 | Int32Ty, |
| 3955 | Int32Ty, |
| 3956 | getPointerType(VoidPtrTy), |
| 3957 | getPointerType(VoidPtrTy), |
| 3958 | Ty |
| 3959 | }; |
| 3960 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3961 | StringRef FieldNames[] = { |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3962 | "__isa", |
| 3963 | "__forwarding", |
| 3964 | "__flags", |
| 3965 | "__size", |
| 3966 | "__copy_helper", |
| 3967 | "__destroy_helper", |
| 3968 | DeclName, |
| 3969 | }; |
| 3970 | |
| 3971 | for (size_t i = 0; i < 7; ++i) { |
| 3972 | if (!HasCopyAndDispose && i >=4 && i <= 5) |
| 3973 | continue; |
| 3974 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3975 | SourceLocation(), |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3976 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3977 | FieldTypes[i], /*TInfo=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3978 | /*BitWidth=*/0, /*Mutable=*/false, |
| 3979 | /*HasInit=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3980 | Field->setAccess(AS_public); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3981 | T->addDecl(Field); |
| 3982 | } |
| 3983 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3984 | T->completeDefinition(); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3985 | |
| 3986 | return getPointerType(getTagDeclType(T)); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3987 | } |
| 3988 | |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 3989 | TypedefDecl *ASTContext::getObjCInstanceTypeDecl() { |
| 3990 | if (!ObjCInstanceTypeDecl) |
| 3991 | ObjCInstanceTypeDecl = TypedefDecl::Create(*this, |
| 3992 | getTranslationUnitDecl(), |
| 3993 | SourceLocation(), |
| 3994 | SourceLocation(), |
| 3995 | &Idents.get("instancetype"), |
| 3996 | getTrivialTypeSourceInfo(getObjCIdType())); |
| 3997 | return ObjCInstanceTypeDecl; |
| 3998 | } |
| 3999 | |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 4000 | // This returns true if a type has been typedefed to BOOL: |
| 4001 | // typedef <type> BOOL; |
Chris Lattner | 2d99833 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 4002 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 4003 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | bb49c3e | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 4004 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 4005 | return II->isStr("BOOL"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4006 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4007 | return false; |
| 4008 | } |
| 4009 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4010 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4011 | /// purpose. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4012 | CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const { |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4013 | if (!type->isIncompleteArrayType() && type->isIncompleteType()) |
| 4014 | return CharUnits::Zero(); |
| 4015 | |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4016 | CharUnits sz = getTypeSizeInChars(type); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4017 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4018 | // 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] | 4019 | if (sz.isPositive() && type->isIntegralOrEnumerationType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4020 | sz = std::max(sz, getTypeSizeInChars(IntTy)); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4021 | // Treat arrays as pointers, since that's how they're passed in. |
| 4022 | else if (type->isArrayType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4023 | sz = getTypeSizeInChars(VoidPtrTy); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4024 | return sz; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4025 | } |
| 4026 | |
| 4027 | static inline |
| 4028 | std::string charUnitsToString(const CharUnits &CU) { |
| 4029 | return llvm::itostr(CU.getQuantity()); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4030 | } |
| 4031 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4032 | /// getObjCEncodingForBlock - Return the encoded type for this block |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4033 | /// declaration. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4034 | std::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const { |
| 4035 | std::string S; |
| 4036 | |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4037 | const BlockDecl *Decl = Expr->getBlockDecl(); |
| 4038 | QualType BlockTy = |
| 4039 | Expr->getType()->getAs<BlockPointerType>()->getPointeeType(); |
| 4040 | // Encode result type. |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 4041 | getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4042 | // Compute size of all parameters. |
| 4043 | // Start with computing size of a pointer in number of bytes. |
| 4044 | // FIXME: There might(should) be a better way of doing this computation! |
| 4045 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4046 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
| 4047 | CharUnits ParmOffset = PtrSize; |
Fariborz Jahanian | 6f46c26 | 2010-04-08 18:06:22 +0000 | [diff] [blame] | 4048 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4049 | E = Decl->param_end(); PI != E; ++PI) { |
| 4050 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4051 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4052 | assert (sz.isPositive() && "BlockExpr - Incomplete param type"); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4053 | ParmOffset += sz; |
| 4054 | } |
| 4055 | // Size of the argument frame |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4056 | S += charUnitsToString(ParmOffset); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4057 | // Block pointer and offset. |
| 4058 | S += "@?0"; |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4059 | |
| 4060 | // Argument types. |
| 4061 | ParmOffset = PtrSize; |
| 4062 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E = |
| 4063 | Decl->param_end(); PI != E; ++PI) { |
| 4064 | ParmVarDecl *PVDecl = *PI; |
| 4065 | QualType PType = PVDecl->getOriginalType(); |
| 4066 | if (const ArrayType *AT = |
| 4067 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4068 | // Use array's original type only if it has known number of |
| 4069 | // elements. |
| 4070 | if (!isa<ConstantArrayType>(AT)) |
| 4071 | PType = PVDecl->getType(); |
| 4072 | } else if (PType->isFunctionType()) |
| 4073 | PType = PVDecl->getType(); |
| 4074 | getObjCEncodingForType(PType, S); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4075 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4076 | ParmOffset += getObjCEncodingTypeSize(PType); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4077 | } |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4078 | |
| 4079 | return S; |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4080 | } |
| 4081 | |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4082 | bool ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl, |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4083 | std::string& S) { |
| 4084 | // Encode result type. |
| 4085 | getObjCEncodingForType(Decl->getResultType(), S); |
| 4086 | CharUnits ParmOffset; |
| 4087 | // Compute size of all parameters. |
| 4088 | for (FunctionDecl::param_const_iterator PI = Decl->param_begin(), |
| 4089 | E = Decl->param_end(); PI != E; ++PI) { |
| 4090 | QualType PType = (*PI)->getType(); |
| 4091 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4092 | if (sz.isZero()) |
| 4093 | return true; |
| 4094 | |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4095 | assert (sz.isPositive() && |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4096 | "getObjCEncodingForFunctionDecl - Incomplete param type"); |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4097 | ParmOffset += sz; |
| 4098 | } |
| 4099 | S += charUnitsToString(ParmOffset); |
| 4100 | ParmOffset = CharUnits::Zero(); |
| 4101 | |
| 4102 | // Argument types. |
| 4103 | for (FunctionDecl::param_const_iterator PI = Decl->param_begin(), |
| 4104 | E = Decl->param_end(); PI != E; ++PI) { |
| 4105 | ParmVarDecl *PVDecl = *PI; |
| 4106 | QualType PType = PVDecl->getOriginalType(); |
| 4107 | if (const ArrayType *AT = |
| 4108 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4109 | // Use array's original type only if it has known number of |
| 4110 | // elements. |
| 4111 | if (!isa<ConstantArrayType>(AT)) |
| 4112 | PType = PVDecl->getType(); |
| 4113 | } else if (PType->isFunctionType()) |
| 4114 | PType = PVDecl->getType(); |
| 4115 | getObjCEncodingForType(PType, S); |
| 4116 | S += charUnitsToString(ParmOffset); |
| 4117 | ParmOffset += getObjCEncodingTypeSize(PType); |
| 4118 | } |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4119 | |
| 4120 | return false; |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4121 | } |
| 4122 | |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4123 | /// getObjCEncodingForMethodParameter - Return the encoded type for a single |
| 4124 | /// method parameter or return type. If Extended, include class names and |
| 4125 | /// block object types. |
| 4126 | void ASTContext::getObjCEncodingForMethodParameter(Decl::ObjCDeclQualifier QT, |
| 4127 | QualType T, std::string& S, |
| 4128 | bool Extended) const { |
| 4129 | // Encode type qualifer, 'in', 'inout', etc. for the parameter. |
| 4130 | getObjCEncodingForTypeQualifier(QT, S); |
| 4131 | // Encode parameter type. |
| 4132 | getObjCEncodingForTypeImpl(T, S, true, true, 0, |
| 4133 | true /*OutermostType*/, |
| 4134 | false /*EncodingProperty*/, |
| 4135 | false /*StructField*/, |
| 4136 | Extended /*EncodeBlockParameters*/, |
| 4137 | Extended /*EncodeClassNames*/); |
| 4138 | } |
| 4139 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4140 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4141 | /// declaration. |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4142 | bool ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4143 | std::string& S, |
| 4144 | bool Extended) const { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4145 | // FIXME: This is not very efficient. |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4146 | // Encode return type. |
| 4147 | getObjCEncodingForMethodParameter(Decl->getObjCDeclQualifier(), |
| 4148 | Decl->getResultType(), S, Extended); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4149 | // Compute size of all parameters. |
| 4150 | // Start with computing size of a pointer in number of bytes. |
| 4151 | // FIXME: There might(should) be a better way of doing this computation! |
| 4152 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4153 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4154 | // The first two arguments (self and _cmd) are pointers; account for |
| 4155 | // their size. |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4156 | CharUnits ParmOffset = 2 * PtrSize; |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4157 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 4158 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 4159 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4160 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4161 | if (sz.isZero()) |
| 4162 | return true; |
| 4163 | |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4164 | assert (sz.isPositive() && |
| 4165 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4166 | ParmOffset += sz; |
| 4167 | } |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4168 | S += charUnitsToString(ParmOffset); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4169 | S += "@0:"; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4170 | S += charUnitsToString(PtrSize); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4171 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4172 | // Argument types. |
| 4173 | ParmOffset = 2 * PtrSize; |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4174 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 4175 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4176 | const ParmVarDecl *PVDecl = *PI; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4177 | QualType PType = PVDecl->getOriginalType(); |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 4178 | if (const ArrayType *AT = |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 4179 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4180 | // Use array's original type only if it has known number of |
| 4181 | // elements. |
Steve Naroff | bb3fde3 | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 4182 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 4183 | PType = PVDecl->getType(); |
| 4184 | } else if (PType->isFunctionType()) |
| 4185 | PType = PVDecl->getType(); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4186 | getObjCEncodingForMethodParameter(PVDecl->getObjCDeclQualifier(), |
| 4187 | PType, S, Extended); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4188 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4189 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4190 | } |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4191 | |
| 4192 | return false; |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4193 | } |
| 4194 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4195 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4196 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4197 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 4198 | /// NULL when getting encodings for protocol properties. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4199 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 4200 | /// attributes readonly and bycopy are encoded as single characters. The |
| 4201 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 4202 | /// encoded as single characters, followed by an identifier. Property types |
| 4203 | /// are also encoded as a parametrized attribute. The characters used to encode |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4204 | /// these attributes are defined by the following enumeration: |
| 4205 | /// @code |
| 4206 | /// enum PropertyAttributes { |
| 4207 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 4208 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 4209 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 4210 | /// kPropertyDynamic = 'D', // property is dynamic |
| 4211 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 4212 | /// kPropertySetter = 'S', // followed by setter selector name |
| 4213 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
Bob Wilson | 0d4cb85 | 2012-03-22 17:48:02 +0000 | [diff] [blame] | 4214 | /// kPropertyType = 'T' // followed by old-style type encoding. |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4215 | /// kPropertyWeak = 'W' // 'weak' property |
| 4216 | /// kPropertyStrong = 'P' // property GC'able |
| 4217 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 4218 | /// }; |
| 4219 | /// @endcode |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4220 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4221 | const Decl *Container, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4222 | std::string& S) const { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4223 | // Collect information from the property implementation decl(s). |
| 4224 | bool Dynamic = false; |
| 4225 | ObjCPropertyImplDecl *SynthesizePID = 0; |
| 4226 | |
| 4227 | // FIXME: Duplicated code due to poor abstraction. |
| 4228 | if (Container) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4229 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4230 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
| 4231 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4232 | i = CID->propimpl_begin(), e = CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4233 | i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4234 | ObjCPropertyImplDecl *PID = *i; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4235 | if (PID->getPropertyDecl() == PD) { |
| 4236 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 4237 | Dynamic = true; |
| 4238 | } else { |
| 4239 | SynthesizePID = PID; |
| 4240 | } |
| 4241 | } |
| 4242 | } |
| 4243 | } else { |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4244 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4245 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4246 | i = OID->propimpl_begin(), e = OID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4247 | i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4248 | ObjCPropertyImplDecl *PID = *i; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4249 | if (PID->getPropertyDecl() == PD) { |
| 4250 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 4251 | Dynamic = true; |
| 4252 | } else { |
| 4253 | SynthesizePID = PID; |
| 4254 | } |
| 4255 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4256 | } |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4257 | } |
| 4258 | } |
| 4259 | |
| 4260 | // FIXME: This is not very efficient. |
| 4261 | S = "T"; |
| 4262 | |
| 4263 | // Encode result type. |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4264 | // GCC has some special rules regarding encoding of properties which |
| 4265 | // closely resembles encoding of ivars. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4266 | getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4267 | true /* outermost type */, |
| 4268 | true /* encoding for property */); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4269 | |
| 4270 | if (PD->isReadOnly()) { |
| 4271 | S += ",R"; |
| 4272 | } else { |
| 4273 | switch (PD->getSetterKind()) { |
| 4274 | case ObjCPropertyDecl::Assign: break; |
| 4275 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4276 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
Fariborz Jahanian | 3a02b44 | 2011-08-12 20:47:08 +0000 | [diff] [blame] | 4277 | case ObjCPropertyDecl::Weak: S += ",W"; break; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4278 | } |
| 4279 | } |
| 4280 | |
| 4281 | // It really isn't clear at all what this means, since properties |
| 4282 | // are "dynamic by default". |
| 4283 | if (Dynamic) |
| 4284 | S += ",D"; |
| 4285 | |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4286 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 4287 | S += ",N"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4288 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4289 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 4290 | S += ",G"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4291 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4292 | } |
| 4293 | |
| 4294 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 4295 | S += ",S"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4296 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4297 | } |
| 4298 | |
| 4299 | if (SynthesizePID) { |
| 4300 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 4301 | S += ",V"; |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 4302 | S += OID->getNameAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4303 | } |
| 4304 | |
| 4305 | // FIXME: OBJCGC: weak & strong |
| 4306 | } |
| 4307 | |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4308 | /// getLegacyIntegralTypeEncoding - |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4309 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
| 4310 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4311 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
| 4312 | /// |
| 4313 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 4314 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4315 | if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) { |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4316 | if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4317 | PointeeTy = UnsignedIntTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4318 | else |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4319 | if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4320 | PointeeTy = IntTy; |
| 4321 | } |
| 4322 | } |
| 4323 | } |
| 4324 | |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4325 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4326 | const FieldDecl *Field) const { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4327 | // We follow the behavior of gcc, expanding structures which are |
| 4328 | // directly pointed to, and expanding embedded structures. Note that |
| 4329 | // these rules are sufficient to prevent recursive encoding of the |
| 4330 | // same type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4331 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
Fariborz Jahanian | 5b8c7d9 | 2008-12-22 23:22:27 +0000 | [diff] [blame] | 4332 | true /* outermost type */); |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4333 | } |
| 4334 | |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4335 | static char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) { |
| 4336 | switch (T->getAs<BuiltinType>()->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4337 | default: llvm_unreachable("Unhandled builtin type kind"); |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4338 | case BuiltinType::Void: return 'v'; |
| 4339 | case BuiltinType::Bool: return 'B'; |
| 4340 | case BuiltinType::Char_U: |
| 4341 | case BuiltinType::UChar: return 'C'; |
| 4342 | case BuiltinType::UShort: return 'S'; |
| 4343 | case BuiltinType::UInt: return 'I'; |
| 4344 | case BuiltinType::ULong: |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4345 | return C->getIntWidth(T) == 32 ? 'L' : 'Q'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4346 | case BuiltinType::UInt128: return 'T'; |
| 4347 | case BuiltinType::ULongLong: return 'Q'; |
| 4348 | case BuiltinType::Char_S: |
| 4349 | case BuiltinType::SChar: return 'c'; |
| 4350 | case BuiltinType::Short: return 's'; |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 4351 | case BuiltinType::WChar_S: |
| 4352 | case BuiltinType::WChar_U: |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4353 | case BuiltinType::Int: return 'i'; |
| 4354 | case BuiltinType::Long: |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4355 | return C->getIntWidth(T) == 32 ? 'l' : 'q'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4356 | case BuiltinType::LongLong: return 'q'; |
| 4357 | case BuiltinType::Int128: return 't'; |
| 4358 | case BuiltinType::Float: return 'f'; |
| 4359 | case BuiltinType::Double: return 'd'; |
Daniel Dunbar | 3a0be84 | 2010-10-11 21:13:48 +0000 | [diff] [blame] | 4360 | case BuiltinType::LongDouble: return 'D'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4361 | } |
| 4362 | } |
| 4363 | |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4364 | static char ObjCEncodingForEnumType(const ASTContext *C, const EnumType *ET) { |
| 4365 | EnumDecl *Enum = ET->getDecl(); |
| 4366 | |
| 4367 | // The encoding of an non-fixed enum type is always 'i', regardless of size. |
| 4368 | if (!Enum->isFixed()) |
| 4369 | return 'i'; |
| 4370 | |
| 4371 | // The encoding of a fixed enum type matches its fixed underlying type. |
| 4372 | return ObjCEncodingForPrimitiveKind(C, Enum->getIntegerType()); |
| 4373 | } |
| 4374 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4375 | static void EncodeBitField(const ASTContext *Ctx, std::string& S, |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4376 | QualType T, const FieldDecl *FD) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4377 | assert(FD->isBitField() && "not a bitfield - getObjCEncodingForTypeImpl"); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4378 | S += 'b'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4379 | // The NeXT runtime encodes bit fields as b followed by the number of bits. |
| 4380 | // The GNU runtime requires more information; bitfields are encoded as b, |
| 4381 | // then the offset (in bits) of the first element, then the type of the |
| 4382 | // bitfield, then the size in bits. For example, in this structure: |
| 4383 | // |
| 4384 | // struct |
| 4385 | // { |
| 4386 | // int integer; |
| 4387 | // int flags:2; |
| 4388 | // }; |
| 4389 | // On a 32-bit system, the encoding for flags would be b2 for the NeXT |
| 4390 | // runtime, but b32i2 for the GNU runtime. The reason for this extra |
| 4391 | // information is not especially sensible, but we're stuck with it for |
| 4392 | // compatibility with GCC, although providing it breaks anything that |
| 4393 | // actually uses runtime introspection and wants to work on both runtimes... |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4394 | if (!Ctx->getLangOpts().NeXTRuntime) { |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4395 | const RecordDecl *RD = FD->getParent(); |
| 4396 | const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD); |
Eli Friedman | 8290574 | 2011-07-07 01:54:01 +0000 | [diff] [blame] | 4397 | S += llvm::utostr(RL.getFieldOffset(FD->getFieldIndex())); |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4398 | if (const EnumType *ET = T->getAs<EnumType>()) |
| 4399 | S += ObjCEncodingForEnumType(Ctx, ET); |
David Chisnall | c7ff82c | 2010-12-26 20:12:30 +0000 | [diff] [blame] | 4400 | else |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4401 | S += ObjCEncodingForPrimitiveKind(Ctx, T); |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4402 | } |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4403 | S += llvm::utostr(FD->getBitWidthValue(*Ctx)); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4404 | } |
| 4405 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 4406 | // FIXME: Use SmallString for accumulating string. |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4407 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 4408 | bool ExpandPointedToStructures, |
| 4409 | bool ExpandStructures, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 4410 | const FieldDecl *FD, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4411 | bool OutermostType, |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4412 | bool EncodingProperty, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4413 | bool StructField, |
| 4414 | bool EncodeBlockParameters, |
| 4415 | bool EncodeClassNames) const { |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4416 | if (T->getAs<BuiltinType>()) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4417 | if (FD && FD->isBitField()) |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4418 | return EncodeBitField(this, S, T, FD); |
| 4419 | S += ObjCEncodingForPrimitiveKind(this, T); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4420 | return; |
| 4421 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4422 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4423 | if (const ComplexType *CT = T->getAs<ComplexType>()) { |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 4424 | S += 'j'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4425 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false, |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 4426 | false); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4427 | return; |
| 4428 | } |
Fariborz Jahanian | 60bce3e | 2009-11-23 20:40:50 +0000 | [diff] [blame] | 4429 | |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4430 | // encoding for pointer or r3eference types. |
| 4431 | QualType PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4432 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Fariborz Jahanian | 8d2c0a9 | 2009-11-30 18:43:52 +0000 | [diff] [blame] | 4433 | if (PT->isObjCSelType()) { |
| 4434 | S += ':'; |
| 4435 | return; |
| 4436 | } |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4437 | PointeeTy = PT->getPointeeType(); |
| 4438 | } |
| 4439 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 4440 | PointeeTy = RT->getPointeeType(); |
| 4441 | if (!PointeeTy.isNull()) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4442 | bool isReadOnly = false; |
| 4443 | // For historical/compatibility reasons, the read-only qualifier of the |
| 4444 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 4445 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4446 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 4447 | if (isa<TypedefType>(T.getTypePtr())) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4448 | if (OutermostType && T.isConstQualified()) { |
| 4449 | isReadOnly = true; |
| 4450 | S += 'r'; |
| 4451 | } |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 4452 | } else if (OutermostType) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4453 | QualType P = PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4454 | while (P->getAs<PointerType>()) |
| 4455 | P = P->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4456 | if (P.isConstQualified()) { |
| 4457 | isReadOnly = true; |
| 4458 | S += 'r'; |
| 4459 | } |
| 4460 | } |
| 4461 | if (isReadOnly) { |
| 4462 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 4463 | // combinations need to be rearranged. |
| 4464 | // Rewrite "in const" from "nr" to "rn" |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4465 | if (StringRef(S).endswith("nr")) |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 4466 | S.replace(S.end()-2, S.end(), "rn"); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4467 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4468 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4469 | if (PointeeTy->isCharType()) { |
| 4470 | // char pointer types should be encoded as '*' unless it is a |
| 4471 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 4472 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4473 | S += '*'; |
| 4474 | return; |
| 4475 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4476 | } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) { |
Steve Naroff | 9533a7f | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 4477 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 4478 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 4479 | S += '#'; |
| 4480 | return; |
| 4481 | } |
| 4482 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 4483 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 4484 | S += '@'; |
| 4485 | return; |
| 4486 | } |
| 4487 | // fall through... |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4488 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4489 | S += '^'; |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4490 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 4491 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4492 | getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4493 | NULL); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4494 | return; |
| 4495 | } |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4496 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4497 | if (const ArrayType *AT = |
| 4498 | // Ignore type qualifiers etc. |
| 4499 | dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) { |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4500 | if (isa<IncompleteArrayType>(AT) && !StructField) { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4501 | // Incomplete arrays are encoded as a pointer to the array element. |
| 4502 | S += '^'; |
| 4503 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4504 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4505 | false, ExpandStructures, FD); |
| 4506 | } else { |
| 4507 | S += '['; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4508 | |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4509 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
| 4510 | if (getTypeSize(CAT->getElementType()) == 0) |
| 4511 | S += '0'; |
| 4512 | else |
| 4513 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 4514 | } else { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4515 | //Variable length arrays are encoded as a regular array with 0 elements. |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4516 | assert((isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) && |
| 4517 | "Unknown array type!"); |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4518 | S += '0'; |
| 4519 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4520 | |
| 4521 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4522 | false, ExpandStructures, FD); |
| 4523 | S += ']'; |
| 4524 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4525 | return; |
| 4526 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4527 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4528 | if (T->getAs<FunctionType>()) { |
Anders Carlsson | c0a87b7 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 4529 | S += '?'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4530 | return; |
| 4531 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4532 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4533 | if (const RecordType *RTy = T->getAs<RecordType>()) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4534 | RecordDecl *RDecl = RTy->getDecl(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4535 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 4536 | // Anonymous structures print as '?' |
| 4537 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 4538 | S += II->getName(); |
Fariborz Jahanian | 6fb9439 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 4539 | if (ClassTemplateSpecializationDecl *Spec |
| 4540 | = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) { |
| 4541 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 4542 | std::string TemplateArgsStr |
| 4543 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4544 | TemplateArgs.data(), |
| 4545 | TemplateArgs.size(), |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 4546 | (*this).getPrintingPolicy()); |
Fariborz Jahanian | 6fb9439 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 4547 | |
| 4548 | S += TemplateArgsStr; |
| 4549 | } |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 4550 | } else { |
| 4551 | S += '?'; |
| 4552 | } |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 4553 | if (ExpandStructures) { |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4554 | S += '='; |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4555 | if (!RDecl->isUnion()) { |
| 4556 | getObjCEncodingForStructureImpl(RDecl, S, FD); |
| 4557 | } else { |
| 4558 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 4559 | FieldEnd = RDecl->field_end(); |
| 4560 | Field != FieldEnd; ++Field) { |
| 4561 | if (FD) { |
| 4562 | S += '"'; |
| 4563 | S += Field->getNameAsString(); |
| 4564 | S += '"'; |
| 4565 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4566 | |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4567 | // Special case bit-fields. |
| 4568 | if (Field->isBitField()) { |
| 4569 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4570 | *Field); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4571 | } else { |
| 4572 | QualType qt = Field->getType(); |
| 4573 | getLegacyIntegralTypeEncoding(qt); |
| 4574 | getObjCEncodingForTypeImpl(qt, S, false, true, |
| 4575 | FD, /*OutermostType*/false, |
| 4576 | /*EncodingProperty*/false, |
| 4577 | /*StructField*/true); |
| 4578 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4579 | } |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4580 | } |
Fariborz Jahanian | 6de88a8 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 4581 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4582 | S += RDecl->isUnion() ? ')' : '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4583 | return; |
| 4584 | } |
Fariborz Jahanian | e6012c7 | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 4585 | |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4586 | if (const EnumType *ET = T->getAs<EnumType>()) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4587 | if (FD && FD->isBitField()) |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4588 | EncodeBitField(this, S, T, FD); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4589 | else |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4590 | S += ObjCEncodingForEnumType(this, ET); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4591 | return; |
| 4592 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4593 | |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4594 | if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) { |
Steve Naroff | 21a98b1 | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 4595 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4596 | if (EncodeBlockParameters) { |
| 4597 | const FunctionType *FT = BT->getPointeeType()->getAs<FunctionType>(); |
| 4598 | |
| 4599 | S += '<'; |
| 4600 | // Block return type |
| 4601 | getObjCEncodingForTypeImpl(FT->getResultType(), S, |
| 4602 | ExpandPointedToStructures, ExpandStructures, |
| 4603 | FD, |
| 4604 | false /* OutermostType */, |
| 4605 | EncodingProperty, |
| 4606 | false /* StructField */, |
| 4607 | EncodeBlockParameters, |
| 4608 | EncodeClassNames); |
| 4609 | // Block self |
| 4610 | S += "@?"; |
| 4611 | // Block parameters |
| 4612 | if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) { |
| 4613 | for (FunctionProtoType::arg_type_iterator I = FPT->arg_type_begin(), |
| 4614 | E = FPT->arg_type_end(); I && (I != E); ++I) { |
| 4615 | getObjCEncodingForTypeImpl(*I, S, |
| 4616 | ExpandPointedToStructures, |
| 4617 | ExpandStructures, |
| 4618 | FD, |
| 4619 | false /* OutermostType */, |
| 4620 | EncodingProperty, |
| 4621 | false /* StructField */, |
| 4622 | EncodeBlockParameters, |
| 4623 | EncodeClassNames); |
| 4624 | } |
| 4625 | } |
| 4626 | S += '>'; |
| 4627 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4628 | return; |
| 4629 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4630 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4631 | // Ignore protocol qualifiers when mangling at this level. |
| 4632 | if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>()) |
| 4633 | T = OT->getBaseType(); |
| 4634 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4635 | if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4636 | // @encode(class_name) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4637 | ObjCInterfaceDecl *OI = OIT->getDecl(); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4638 | S += '{'; |
| 4639 | const IdentifierInfo *II = OI->getIdentifier(); |
| 4640 | S += II->getName(); |
| 4641 | S += '='; |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4642 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4643 | DeepCollectObjCIvars(OI, true, Ivars); |
| 4644 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) { |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4645 | const FieldDecl *Field = cast<FieldDecl>(Ivars[i]); |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4646 | if (Field->isBitField()) |
| 4647 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4648 | else |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4649 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4650 | } |
| 4651 | S += '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4652 | return; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4653 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4654 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4655 | if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4656 | if (OPT->isObjCIdType()) { |
| 4657 | S += '@'; |
| 4658 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4659 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4660 | |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4661 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
| 4662 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
| 4663 | // Since this is a binary compatibility issue, need to consult with runtime |
| 4664 | // folks. Fortunately, this is a *very* obsure construct. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4665 | S += '#'; |
| 4666 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4667 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4668 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4669 | if (OPT->isObjCQualifiedIdType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4670 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4671 | ExpandPointedToStructures, |
| 4672 | ExpandStructures, FD); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4673 | if (FD || EncodingProperty || EncodeClassNames) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4674 | // Note that we do extended encoding of protocol qualifer list |
| 4675 | // Only when doing ivar or property encoding. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4676 | S += '"'; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4677 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 4678 | E = OPT->qual_end(); I != E; ++I) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4679 | S += '<'; |
| 4680 | S += (*I)->getNameAsString(); |
| 4681 | S += '>'; |
| 4682 | } |
| 4683 | S += '"'; |
| 4684 | } |
| 4685 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4686 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4687 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4688 | QualType PointeeTy = OPT->getPointeeType(); |
| 4689 | if (!EncodingProperty && |
| 4690 | isa<TypedefType>(PointeeTy.getTypePtr())) { |
| 4691 | // Another historical/compatibility reason. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4692 | // We encode the underlying type which comes out as |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4693 | // {...}; |
| 4694 | S += '^'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4695 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 4696 | false, ExpandPointedToStructures, |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4697 | NULL); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4698 | return; |
| 4699 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4700 | |
| 4701 | S += '@'; |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4702 | if (OPT->getInterfaceDecl() && |
| 4703 | (FD || EncodingProperty || EncodeClassNames)) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4704 | S += '"'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4705 | S += OPT->getInterfaceDecl()->getIdentifier()->getName(); |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4706 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 4707 | E = OPT->qual_end(); I != E; ++I) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4708 | S += '<'; |
| 4709 | S += (*I)->getNameAsString(); |
| 4710 | S += '>'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4711 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4712 | S += '"'; |
| 4713 | } |
| 4714 | return; |
| 4715 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4716 | |
John McCall | 532ec7b | 2010-05-17 23:56:34 +0000 | [diff] [blame] | 4717 | // gcc just blithely ignores member pointers. |
| 4718 | // TODO: maybe there should be a mangling for these |
| 4719 | if (T->getAs<MemberPointerType>()) |
| 4720 | return; |
Fariborz Jahanian | e6012c7 | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 4721 | |
| 4722 | if (T->isVectorType()) { |
| 4723 | // This matches gcc's encoding, even though technically it is |
| 4724 | // insufficient. |
| 4725 | // FIXME. We should do a better job than gcc. |
| 4726 | return; |
| 4727 | } |
| 4728 | |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4729 | llvm_unreachable("@encode for type not implemented!"); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4730 | } |
| 4731 | |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4732 | void ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl, |
| 4733 | std::string &S, |
| 4734 | const FieldDecl *FD, |
| 4735 | bool includeVBases) const { |
| 4736 | assert(RDecl && "Expected non-null RecordDecl"); |
| 4737 | assert(!RDecl->isUnion() && "Should not be called for unions"); |
| 4738 | if (!RDecl->getDefinition()) |
| 4739 | return; |
| 4740 | |
| 4741 | CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(RDecl); |
| 4742 | std::multimap<uint64_t, NamedDecl *> FieldOrBaseOffsets; |
| 4743 | const ASTRecordLayout &layout = getASTRecordLayout(RDecl); |
| 4744 | |
| 4745 | if (CXXRec) { |
| 4746 | for (CXXRecordDecl::base_class_iterator |
| 4747 | BI = CXXRec->bases_begin(), |
| 4748 | BE = CXXRec->bases_end(); BI != BE; ++BI) { |
| 4749 | if (!BI->isVirtual()) { |
| 4750 | CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 829f200 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 4751 | if (base->isEmpty()) |
| 4752 | continue; |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4753 | uint64_t offs = layout.getBaseClassOffsetInBits(base); |
| 4754 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 4755 | std::make_pair(offs, base)); |
| 4756 | } |
| 4757 | } |
| 4758 | } |
| 4759 | |
| 4760 | unsigned i = 0; |
| 4761 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 4762 | FieldEnd = RDecl->field_end(); |
| 4763 | Field != FieldEnd; ++Field, ++i) { |
| 4764 | uint64_t offs = layout.getFieldOffset(i); |
| 4765 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4766 | std::make_pair(offs, *Field)); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4767 | } |
| 4768 | |
| 4769 | if (CXXRec && includeVBases) { |
| 4770 | for (CXXRecordDecl::base_class_iterator |
| 4771 | BI = CXXRec->vbases_begin(), |
| 4772 | BE = CXXRec->vbases_end(); BI != BE; ++BI) { |
| 4773 | CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 829f200 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 4774 | if (base->isEmpty()) |
| 4775 | continue; |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4776 | uint64_t offs = layout.getVBaseClassOffsetInBits(base); |
Argyrios Kyrtzidis | 19aa860 | 2011-09-26 18:14:24 +0000 | [diff] [blame] | 4777 | if (FieldOrBaseOffsets.find(offs) == FieldOrBaseOffsets.end()) |
| 4778 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.end(), |
| 4779 | std::make_pair(offs, base)); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4780 | } |
| 4781 | } |
| 4782 | |
| 4783 | CharUnits size; |
| 4784 | if (CXXRec) { |
| 4785 | size = includeVBases ? layout.getSize() : layout.getNonVirtualSize(); |
| 4786 | } else { |
| 4787 | size = layout.getSize(); |
| 4788 | } |
| 4789 | |
| 4790 | uint64_t CurOffs = 0; |
| 4791 | std::multimap<uint64_t, NamedDecl *>::iterator |
| 4792 | CurLayObj = FieldOrBaseOffsets.begin(); |
| 4793 | |
Douglas Gregor | 58db7a5 | 2012-04-27 22:30:01 +0000 | [diff] [blame] | 4794 | if (CXXRec && CXXRec->isDynamicClass() && |
| 4795 | (CurLayObj == FieldOrBaseOffsets.end() || CurLayObj->first != 0)) { |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4796 | if (FD) { |
| 4797 | S += "\"_vptr$"; |
| 4798 | std::string recname = CXXRec->getNameAsString(); |
| 4799 | if (recname.empty()) recname = "?"; |
| 4800 | S += recname; |
| 4801 | S += '"'; |
| 4802 | } |
| 4803 | S += "^^?"; |
| 4804 | CurOffs += getTypeSize(VoidPtrTy); |
| 4805 | } |
| 4806 | |
| 4807 | if (!RDecl->hasFlexibleArrayMember()) { |
| 4808 | // Mark the end of the structure. |
| 4809 | uint64_t offs = toBits(size); |
| 4810 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 4811 | std::make_pair(offs, (NamedDecl*)0)); |
| 4812 | } |
| 4813 | |
| 4814 | for (; CurLayObj != FieldOrBaseOffsets.end(); ++CurLayObj) { |
| 4815 | assert(CurOffs <= CurLayObj->first); |
| 4816 | |
| 4817 | if (CurOffs < CurLayObj->first) { |
| 4818 | uint64_t padding = CurLayObj->first - CurOffs; |
| 4819 | // FIXME: There doesn't seem to be a way to indicate in the encoding that |
| 4820 | // packing/alignment of members is different that normal, in which case |
| 4821 | // the encoding will be out-of-sync with the real layout. |
| 4822 | // If the runtime switches to just consider the size of types without |
| 4823 | // taking into account alignment, we could make padding explicit in the |
| 4824 | // encoding (e.g. using arrays of chars). The encoding strings would be |
| 4825 | // longer then though. |
| 4826 | CurOffs += padding; |
| 4827 | } |
| 4828 | |
| 4829 | NamedDecl *dcl = CurLayObj->second; |
| 4830 | if (dcl == 0) |
| 4831 | break; // reached end of structure. |
| 4832 | |
| 4833 | if (CXXRecordDecl *base = dyn_cast<CXXRecordDecl>(dcl)) { |
| 4834 | // We expand the bases without their virtual bases since those are going |
| 4835 | // in the initial structure. Note that this differs from gcc which |
| 4836 | // expands virtual bases each time one is encountered in the hierarchy, |
| 4837 | // making the encoding type bigger than it really is. |
| 4838 | getObjCEncodingForStructureImpl(base, S, FD, /*includeVBases*/false); |
Argyrios Kyrtzidis | 829f200 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 4839 | assert(!base->isEmpty()); |
| 4840 | CurOffs += toBits(getASTRecordLayout(base).getNonVirtualSize()); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4841 | } else { |
| 4842 | FieldDecl *field = cast<FieldDecl>(dcl); |
| 4843 | if (FD) { |
| 4844 | S += '"'; |
| 4845 | S += field->getNameAsString(); |
| 4846 | S += '"'; |
| 4847 | } |
| 4848 | |
| 4849 | if (field->isBitField()) { |
| 4850 | EncodeBitField(this, S, field->getType(), field); |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4851 | CurOffs += field->getBitWidthValue(*this); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4852 | } else { |
| 4853 | QualType qt = field->getType(); |
| 4854 | getLegacyIntegralTypeEncoding(qt); |
| 4855 | getObjCEncodingForTypeImpl(qt, S, false, true, FD, |
| 4856 | /*OutermostType*/false, |
| 4857 | /*EncodingProperty*/false, |
| 4858 | /*StructField*/true); |
| 4859 | CurOffs += getTypeSize(field->getType()); |
| 4860 | } |
| 4861 | } |
| 4862 | } |
| 4863 | } |
| 4864 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4865 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 4866 | std::string& S) const { |
| 4867 | if (QT & Decl::OBJC_TQ_In) |
| 4868 | S += 'n'; |
| 4869 | if (QT & Decl::OBJC_TQ_Inout) |
| 4870 | S += 'N'; |
| 4871 | if (QT & Decl::OBJC_TQ_Out) |
| 4872 | S += 'o'; |
| 4873 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 4874 | S += 'O'; |
| 4875 | if (QT & Decl::OBJC_TQ_Byref) |
| 4876 | S += 'R'; |
| 4877 | if (QT & Decl::OBJC_TQ_Oneway) |
| 4878 | S += 'V'; |
| 4879 | } |
| 4880 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4881 | void ASTContext::setBuiltinVaListType(QualType T) { |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 4882 | assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4883 | |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 4884 | BuiltinVaListType = T; |
| 4885 | } |
| 4886 | |
Douglas Gregor | 4dfd02a | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 4887 | TypedefDecl *ASTContext::getObjCIdDecl() const { |
| 4888 | if (!ObjCIdDecl) { |
| 4889 | QualType T = getObjCObjectType(ObjCBuiltinIdTy, 0, 0); |
| 4890 | T = getObjCObjectPointerType(T); |
| 4891 | TypeSourceInfo *IdInfo = getTrivialTypeSourceInfo(T); |
| 4892 | ObjCIdDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 4893 | getTranslationUnitDecl(), |
| 4894 | SourceLocation(), SourceLocation(), |
| 4895 | &Idents.get("id"), IdInfo); |
| 4896 | } |
| 4897 | |
| 4898 | return ObjCIdDecl; |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 4899 | } |
| 4900 | |
Douglas Gregor | 7a27ea5 | 2011-08-12 06:17:30 +0000 | [diff] [blame] | 4901 | TypedefDecl *ASTContext::getObjCSelDecl() const { |
| 4902 | if (!ObjCSelDecl) { |
| 4903 | QualType SelT = getPointerType(ObjCBuiltinSelTy); |
| 4904 | TypeSourceInfo *SelInfo = getTrivialTypeSourceInfo(SelT); |
| 4905 | ObjCSelDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 4906 | getTranslationUnitDecl(), |
| 4907 | SourceLocation(), SourceLocation(), |
| 4908 | &Idents.get("SEL"), SelInfo); |
| 4909 | } |
| 4910 | return ObjCSelDecl; |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 4911 | } |
| 4912 | |
Douglas Gregor | 79d6726 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 4913 | TypedefDecl *ASTContext::getObjCClassDecl() const { |
| 4914 | if (!ObjCClassDecl) { |
| 4915 | QualType T = getObjCObjectType(ObjCBuiltinClassTy, 0, 0); |
| 4916 | T = getObjCObjectPointerType(T); |
| 4917 | TypeSourceInfo *ClassInfo = getTrivialTypeSourceInfo(T); |
| 4918 | ObjCClassDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 4919 | getTranslationUnitDecl(), |
| 4920 | SourceLocation(), SourceLocation(), |
| 4921 | &Idents.get("Class"), ClassInfo); |
| 4922 | } |
| 4923 | |
| 4924 | return ObjCClassDecl; |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 4925 | } |
| 4926 | |
Douglas Gregor | a6ea10e | 2012-01-17 18:09:05 +0000 | [diff] [blame] | 4927 | ObjCInterfaceDecl *ASTContext::getObjCProtocolDecl() const { |
| 4928 | if (!ObjCProtocolClassDecl) { |
| 4929 | ObjCProtocolClassDecl |
| 4930 | = ObjCInterfaceDecl::Create(*this, getTranslationUnitDecl(), |
| 4931 | SourceLocation(), |
| 4932 | &Idents.get("Protocol"), |
| 4933 | /*PrevDecl=*/0, |
| 4934 | SourceLocation(), true); |
| 4935 | } |
| 4936 | |
| 4937 | return ObjCProtocolClassDecl; |
| 4938 | } |
| 4939 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4940 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4941 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 4942 | "'NSConstantString' type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4943 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4944 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 4945 | } |
| 4946 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4947 | /// \brief Retrieve the template name that corresponds to a non-empty |
| 4948 | /// lookup. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4949 | TemplateName |
| 4950 | ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin, |
| 4951 | UnresolvedSetIterator End) const { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4952 | unsigned size = End - Begin; |
| 4953 | assert(size > 1 && "set is not overloaded!"); |
| 4954 | |
| 4955 | void *memory = Allocate(sizeof(OverloadedTemplateStorage) + |
| 4956 | size * sizeof(FunctionTemplateDecl*)); |
| 4957 | OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size); |
| 4958 | |
| 4959 | NamedDecl **Storage = OT->getStorage(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4960 | for (UnresolvedSetIterator I = Begin; I != End; ++I) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4961 | NamedDecl *D = *I; |
| 4962 | assert(isa<FunctionTemplateDecl>(D) || |
| 4963 | (isa<UsingShadowDecl>(D) && |
| 4964 | isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))); |
| 4965 | *Storage++ = D; |
| 4966 | } |
| 4967 | |
| 4968 | return TemplateName(OT); |
| 4969 | } |
| 4970 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4971 | /// \brief Retrieve the template name that represents a qualified |
| 4972 | /// template name such as \c std::vector. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4973 | TemplateName |
| 4974 | ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
| 4975 | bool TemplateKeyword, |
| 4976 | TemplateDecl *Template) const { |
Douglas Gregor | 0f0ea2a | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 4977 | assert(NNS && "Missing nested-name-specifier in qualified template name"); |
| 4978 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4979 | // FIXME: Canonicalization? |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4980 | llvm::FoldingSetNodeID ID; |
| 4981 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 4982 | |
| 4983 | void *InsertPos = 0; |
| 4984 | QualifiedTemplateName *QTN = |
| 4985 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4986 | if (!QTN) { |
| 4987 | QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); |
| 4988 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 4989 | } |
| 4990 | |
| 4991 | return TemplateName(QTN); |
| 4992 | } |
| 4993 | |
| 4994 | /// \brief Retrieve the template name that represents a dependent |
| 4995 | /// template name such as \c MetaFun::template apply. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4996 | TemplateName |
| 4997 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
| 4998 | const IdentifierInfo *Name) const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4999 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 5000 | "Nested name specifier must be dependent"); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 5001 | |
| 5002 | llvm::FoldingSetNodeID ID; |
| 5003 | DependentTemplateName::Profile(ID, NNS, Name); |
| 5004 | |
| 5005 | void *InsertPos = 0; |
| 5006 | DependentTemplateName *QTN = |
| 5007 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 5008 | |
| 5009 | if (QTN) |
| 5010 | return TemplateName(QTN); |
| 5011 | |
| 5012 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 5013 | if (CanonNNS == NNS) { |
| 5014 | QTN = new (*this,4) DependentTemplateName(NNS, Name); |
| 5015 | } else { |
| 5016 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
| 5017 | QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 5018 | DependentTemplateName *CheckQTN = |
| 5019 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 5020 | assert(!CheckQTN && "Dependent type name canonicalization broken"); |
| 5021 | (void)CheckQTN; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 5022 | } |
| 5023 | |
| 5024 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 5025 | return TemplateName(QTN); |
| 5026 | } |
| 5027 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 5028 | /// \brief Retrieve the template name that represents a dependent |
| 5029 | /// template name such as \c MetaFun::template operator+. |
| 5030 | TemplateName |
| 5031 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5032 | OverloadedOperatorKind Operator) const { |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 5033 | assert((!NNS || NNS->isDependent()) && |
| 5034 | "Nested name specifier must be dependent"); |
| 5035 | |
| 5036 | llvm::FoldingSetNodeID ID; |
| 5037 | DependentTemplateName::Profile(ID, NNS, Operator); |
| 5038 | |
| 5039 | void *InsertPos = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 5040 | DependentTemplateName *QTN |
| 5041 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 5042 | |
| 5043 | if (QTN) |
| 5044 | return TemplateName(QTN); |
| 5045 | |
| 5046 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 5047 | if (CanonNNS == NNS) { |
| 5048 | QTN = new (*this,4) DependentTemplateName(NNS, Operator); |
| 5049 | } else { |
| 5050 | TemplateName Canon = getDependentTemplateName(CanonNNS, Operator); |
| 5051 | QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 5052 | |
| 5053 | DependentTemplateName *CheckQTN |
| 5054 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 5055 | assert(!CheckQTN && "Dependent template name canonicalization broken"); |
| 5056 | (void)CheckQTN; |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 5057 | } |
| 5058 | |
| 5059 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 5060 | return TemplateName(QTN); |
| 5061 | } |
| 5062 | |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 5063 | TemplateName |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5064 | ASTContext::getSubstTemplateTemplateParm(TemplateTemplateParmDecl *param, |
| 5065 | TemplateName replacement) const { |
| 5066 | llvm::FoldingSetNodeID ID; |
| 5067 | SubstTemplateTemplateParmStorage::Profile(ID, param, replacement); |
| 5068 | |
| 5069 | void *insertPos = 0; |
| 5070 | SubstTemplateTemplateParmStorage *subst |
| 5071 | = SubstTemplateTemplateParms.FindNodeOrInsertPos(ID, insertPos); |
| 5072 | |
| 5073 | if (!subst) { |
| 5074 | subst = new (*this) SubstTemplateTemplateParmStorage(param, replacement); |
| 5075 | SubstTemplateTemplateParms.InsertNode(subst, insertPos); |
| 5076 | } |
| 5077 | |
| 5078 | return TemplateName(subst); |
| 5079 | } |
| 5080 | |
| 5081 | TemplateName |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 5082 | ASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param, |
| 5083 | const TemplateArgument &ArgPack) const { |
| 5084 | ASTContext &Self = const_cast<ASTContext &>(*this); |
| 5085 | llvm::FoldingSetNodeID ID; |
| 5086 | SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack); |
| 5087 | |
| 5088 | void *InsertPos = 0; |
| 5089 | SubstTemplateTemplateParmPackStorage *Subst |
| 5090 | = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos); |
| 5091 | |
| 5092 | if (!Subst) { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5093 | Subst = new (*this) SubstTemplateTemplateParmPackStorage(Param, |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 5094 | ArgPack.pack_size(), |
| 5095 | ArgPack.pack_begin()); |
| 5096 | SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos); |
| 5097 | } |
| 5098 | |
| 5099 | return TemplateName(Subst); |
| 5100 | } |
| 5101 | |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 5102 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | d934112 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 5103 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 5104 | /// is actually a value of type @c TargetInfo::IntType. |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 5105 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 5106 | switch (Type) { |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 5107 | case TargetInfo::NoInt: return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 5108 | case TargetInfo::SignedShort: return ShortTy; |
| 5109 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 5110 | case TargetInfo::SignedInt: return IntTy; |
| 5111 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 5112 | case TargetInfo::SignedLong: return LongTy; |
| 5113 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 5114 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 5115 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 5116 | } |
| 5117 | |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5118 | llvm_unreachable("Unhandled TargetInfo::IntType value"); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 5119 | } |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 5120 | |
| 5121 | //===----------------------------------------------------------------------===// |
| 5122 | // Type Predicates. |
| 5123 | //===----------------------------------------------------------------------===// |
| 5124 | |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 5125 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 5126 | /// garbage collection attribute. |
| 5127 | /// |
John McCall | ae278a3 | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 5128 | Qualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5129 | if (getLangOpts().getGC() == LangOptions::NonGC) |
John McCall | ae278a3 | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 5130 | return Qualifiers::GCNone; |
| 5131 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5132 | assert(getLangOpts().ObjC1); |
John McCall | ae278a3 | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 5133 | Qualifiers::GC GCAttrs = Ty.getObjCGCAttr(); |
| 5134 | |
| 5135 | // Default behaviour under objective-C's gc is for ObjC pointers |
| 5136 | // (or pointers to them) be treated as though they were declared |
| 5137 | // as __strong. |
| 5138 | if (GCAttrs == Qualifiers::GCNone) { |
| 5139 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
| 5140 | return Qualifiers::Strong; |
| 5141 | else if (Ty->isPointerType()) |
| 5142 | return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); |
| 5143 | } else { |
| 5144 | // It's not valid to set GC attributes on anything that isn't a |
| 5145 | // pointer. |
| 5146 | #ifndef NDEBUG |
| 5147 | QualType CT = Ty->getCanonicalTypeInternal(); |
| 5148 | while (const ArrayType *AT = dyn_cast<ArrayType>(CT)) |
| 5149 | CT = AT->getElementType(); |
| 5150 | assert(CT->isAnyPointerType() || CT->isBlockPointerType()); |
| 5151 | #endif |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 5152 | } |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 5153 | return GCAttrs; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 5154 | } |
| 5155 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5156 | //===----------------------------------------------------------------------===// |
| 5157 | // Type Compatibility Testing |
| 5158 | //===----------------------------------------------------------------------===// |
Chris Lattner | 770951b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 5159 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5160 | /// areCompatVectorTypes - Return true if the two specified vector types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5161 | /// compatible. |
| 5162 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 5163 | const VectorType *RHS) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 5164 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5165 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5166 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5167 | } |
| 5168 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5169 | bool ASTContext::areCompatibleVectorTypes(QualType FirstVec, |
| 5170 | QualType SecondVec) { |
| 5171 | assert(FirstVec->isVectorType() && "FirstVec should be a vector type"); |
| 5172 | assert(SecondVec->isVectorType() && "SecondVec should be a vector type"); |
| 5173 | |
| 5174 | if (hasSameUnqualifiedType(FirstVec, SecondVec)) |
| 5175 | return true; |
| 5176 | |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 5177 | // Treat Neon vector types and most AltiVec vector types as if they are the |
| 5178 | // equivalent GCC vector types. |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5179 | const VectorType *First = FirstVec->getAs<VectorType>(); |
| 5180 | const VectorType *Second = SecondVec->getAs<VectorType>(); |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 5181 | if (First->getNumElements() == Second->getNumElements() && |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5182 | hasSameType(First->getElementType(), Second->getElementType()) && |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 5183 | First->getVectorKind() != VectorType::AltiVecPixel && |
| 5184 | First->getVectorKind() != VectorType::AltiVecBool && |
| 5185 | Second->getVectorKind() != VectorType::AltiVecPixel && |
| 5186 | Second->getVectorKind() != VectorType::AltiVecBool) |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5187 | return true; |
| 5188 | |
| 5189 | return false; |
| 5190 | } |
| 5191 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5192 | //===----------------------------------------------------------------------===// |
| 5193 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 5194 | //===----------------------------------------------------------------------===// |
| 5195 | |
| 5196 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 5197 | /// inheritance hierarchy of 'rProto'. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5198 | bool |
| 5199 | ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 5200 | ObjCProtocolDecl *rProto) const { |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 5201 | if (declaresSameEntity(lProto, rProto)) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5202 | return true; |
| 5203 | for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), |
| 5204 | E = rProto->protocol_end(); PI != E; ++PI) |
| 5205 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 5206 | return true; |
| 5207 | return false; |
| 5208 | } |
| 5209 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5210 | /// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...> |
| 5211 | /// return true if lhs's protocols conform to rhs's protocol; false |
| 5212 | /// otherwise. |
| 5213 | bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) { |
| 5214 | if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType()) |
| 5215 | return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false); |
| 5216 | return false; |
| 5217 | } |
| 5218 | |
Fariborz Jahanian | a8f8dac | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 5219 | /// ObjCQualifiedClassTypesAreCompatible - compare Class<p,...> and |
| 5220 | /// Class<p1, ...>. |
| 5221 | bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs, |
| 5222 | QualType rhs) { |
| 5223 | const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>(); |
| 5224 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
| 5225 | assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible"); |
| 5226 | |
| 5227 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5228 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5229 | bool match = false; |
| 5230 | ObjCProtocolDecl *lhsProto = *I; |
| 5231 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 5232 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 5233 | ObjCProtocolDecl *rhsProto = *J; |
| 5234 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) { |
| 5235 | match = true; |
| 5236 | break; |
| 5237 | } |
| 5238 | } |
| 5239 | if (!match) |
| 5240 | return false; |
| 5241 | } |
| 5242 | return true; |
| 5243 | } |
| 5244 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5245 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 5246 | /// ObjCQualifiedIDType. |
| 5247 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 5248 | bool compare) { |
| 5249 | // Allow id<P..> and an 'id' or void* type in all cases. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5250 | if (lhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5251 | lhs->isObjCIdType() || lhs->isObjCClassType()) |
| 5252 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5253 | else if (rhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5254 | rhs->isObjCIdType() || rhs->isObjCClassType()) |
| 5255 | return true; |
| 5256 | |
| 5257 | if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5258 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
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) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5261 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5262 | if (rhsOPT->qual_empty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5263 | // If the RHS is a unqualified interface pointer "NSString*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5264 | // make sure we check the class hierarchy. |
| 5265 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 5266 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5267 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5268 | // when comparing an id<P> on lhs with a static type on rhs, |
| 5269 | // see if static class implements all of id's protocols, directly or |
| 5270 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 5271 | if (!rhsID->ClassImplementsProtocol(*I, true)) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5272 | return false; |
| 5273 | } |
| 5274 | } |
| 5275 | // If there are no qualifiers and no interface, we have an 'id'. |
| 5276 | return true; |
| 5277 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5278 | // Both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5279 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5280 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5281 | ObjCProtocolDecl *lhsProto = *I; |
| 5282 | bool match = false; |
| 5283 | |
| 5284 | // when comparing an id<P> on lhs with a static type on rhs, |
| 5285 | // see if static class implements all of id's protocols, directly or |
| 5286 | // through its super class and categories. |
| 5287 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 5288 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 5289 | ObjCProtocolDecl *rhsProto = *J; |
| 5290 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 5291 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 5292 | match = true; |
| 5293 | break; |
| 5294 | } |
| 5295 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5296 | // If the RHS is a qualified interface pointer "NSString<P>*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5297 | // make sure we check the class hierarchy. |
| 5298 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 5299 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5300 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5301 | // when comparing an id<P> on lhs with a static type on rhs, |
| 5302 | // see if static class implements all of id's protocols, directly or |
| 5303 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 5304 | if (rhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5305 | match = true; |
| 5306 | break; |
| 5307 | } |
| 5308 | } |
| 5309 | } |
| 5310 | if (!match) |
| 5311 | return false; |
| 5312 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5313 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5314 | return true; |
| 5315 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5316 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5317 | const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 5318 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 5319 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5320 | if (const ObjCObjectPointerType *lhsOPT = |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5321 | lhs->getAsObjCInterfacePointerType()) { |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5322 | // If both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5323 | for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(), |
| 5324 | E = lhsOPT->qual_end(); I != E; ++I) { |
| 5325 | ObjCProtocolDecl *lhsProto = *I; |
| 5326 | bool match = false; |
| 5327 | |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5328 | // 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] | 5329 | // see if static class implements all of id's protocols, directly or |
| 5330 | // through its super class and categories. |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5331 | // First, lhs protocols in the qualifier list must be found, direct |
| 5332 | // or indirect in rhs's qualifier list or it is a mismatch. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5333 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 5334 | E = rhsQID->qual_end(); J != E; ++J) { |
| 5335 | ObjCProtocolDecl *rhsProto = *J; |
| 5336 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 5337 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 5338 | match = true; |
| 5339 | break; |
| 5340 | } |
| 5341 | } |
| 5342 | if (!match) |
| 5343 | return false; |
| 5344 | } |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5345 | |
| 5346 | // Static class's protocols, or its super class or category protocols |
| 5347 | // must be found, direct or indirect in rhs's qualifier list or it is a mismatch. |
| 5348 | if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { |
| 5349 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
| 5350 | CollectInheritedProtocols(lhsID, LHSInheritedProtocols); |
| 5351 | // This is rather dubious but matches gcc's behavior. If lhs has |
| 5352 | // no type qualifier and its class has no static protocol(s) |
| 5353 | // assume that it is mismatch. |
| 5354 | if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty()) |
| 5355 | return false; |
| 5356 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 5357 | LHSInheritedProtocols.begin(), |
| 5358 | E = LHSInheritedProtocols.end(); I != E; ++I) { |
| 5359 | bool match = false; |
| 5360 | ObjCProtocolDecl *lhsProto = (*I); |
| 5361 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 5362 | E = rhsQID->qual_end(); J != E; ++J) { |
| 5363 | ObjCProtocolDecl *rhsProto = *J; |
| 5364 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 5365 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 5366 | match = true; |
| 5367 | break; |
| 5368 | } |
| 5369 | } |
| 5370 | if (!match) |
| 5371 | return false; |
| 5372 | } |
| 5373 | } |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5374 | return true; |
| 5375 | } |
| 5376 | return false; |
| 5377 | } |
| 5378 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5379 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5380 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 5381 | /// protocol qualifiers on the LHS or RHS. |
| 5382 | /// |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5383 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 5384 | const ObjCObjectPointerType *RHSOPT) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5385 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 5386 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 5387 | |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 5388 | // 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] | 5389 | if (LHS->isObjCUnqualifiedIdOrClass() || |
| 5390 | RHS->isObjCUnqualifiedIdOrClass()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5391 | return true; |
| 5392 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5393 | if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5394 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 5395 | QualType(RHSOPT,0), |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5396 | false); |
Fariborz Jahanian | a8f8dac | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 5397 | |
| 5398 | if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass()) |
| 5399 | return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0), |
| 5400 | QualType(RHSOPT,0)); |
| 5401 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5402 | // If we have 2 user-defined types, fall into that path. |
| 5403 | if (LHS->getInterface() && RHS->getInterface()) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5404 | return canAssignObjCInterfaces(LHS, RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5405 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5406 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5407 | } |
| 5408 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5409 | /// canAssignObjCInterfacesInBlockPointer - This routine is specifically written |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 5410 | /// for providing type-safety for objective-c pointers used to pass/return |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5411 | /// arguments in block literals. When passed as arguments, passing 'A*' where |
| 5412 | /// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is |
| 5413 | /// not OK. For the return type, the opposite is not OK. |
| 5414 | bool ASTContext::canAssignObjCInterfacesInBlockPointer( |
| 5415 | const ObjCObjectPointerType *LHSOPT, |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5416 | const ObjCObjectPointerType *RHSOPT, |
| 5417 | bool BlockReturnType) { |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 5418 | if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5419 | return true; |
| 5420 | |
| 5421 | if (LHSOPT->isObjCBuiltinType()) { |
| 5422 | return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType(); |
| 5423 | } |
| 5424 | |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 5425 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5426 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 5427 | QualType(RHSOPT,0), |
| 5428 | false); |
| 5429 | |
| 5430 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 5431 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 5432 | if (LHS && RHS) { // We have 2 user-defined types. |
| 5433 | if (LHS != RHS) { |
| 5434 | if (LHS->getDecl()->isSuperClassOf(RHS->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 | if (RHS->getDecl()->isSuperClassOf(LHS->getDecl())) |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5437 | return !BlockReturnType; |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5438 | } |
| 5439 | else |
| 5440 | return true; |
| 5441 | } |
| 5442 | return false; |
| 5443 | } |
| 5444 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5445 | /// getIntersectionOfProtocols - This routine finds the intersection of set |
| 5446 | /// of protocols inherited from two distinct objective-c pointer objects. |
| 5447 | /// It is used to build composite qualifier list of the composite type of |
| 5448 | /// the conditional expression involving two objective-c pointer objects. |
| 5449 | static |
| 5450 | void getIntersectionOfProtocols(ASTContext &Context, |
| 5451 | const ObjCObjectPointerType *LHSOPT, |
| 5452 | const ObjCObjectPointerType *RHSOPT, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5453 | SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5454 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5455 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 5456 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 5457 | assert(LHS->getInterface() && "LHS must have an interface base"); |
| 5458 | assert(RHS->getInterface() && "RHS must have an interface base"); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5459 | |
| 5460 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet; |
| 5461 | unsigned LHSNumProtocols = LHS->getNumProtocols(); |
| 5462 | if (LHSNumProtocols > 0) |
| 5463 | InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end()); |
| 5464 | else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 5465 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5466 | Context.CollectInheritedProtocols(LHS->getInterface(), |
| 5467 | LHSInheritedProtocols); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5468 | InheritedProtocolSet.insert(LHSInheritedProtocols.begin(), |
| 5469 | LHSInheritedProtocols.end()); |
| 5470 | } |
| 5471 | |
| 5472 | unsigned RHSNumProtocols = RHS->getNumProtocols(); |
| 5473 | if (RHSNumProtocols > 0) { |
Dan Gohman | cb421fa | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 5474 | ObjCProtocolDecl **RHSProtocols = |
| 5475 | const_cast<ObjCProtocolDecl **>(RHS->qual_begin()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5476 | for (unsigned i = 0; i < RHSNumProtocols; ++i) |
| 5477 | if (InheritedProtocolSet.count(RHSProtocols[i])) |
| 5478 | IntersectionOfProtocols.push_back(RHSProtocols[i]); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 5479 | } else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 5480 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5481 | Context.CollectInheritedProtocols(RHS->getInterface(), |
| 5482 | RHSInheritedProtocols); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 5483 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 5484 | RHSInheritedProtocols.begin(), |
| 5485 | E = RHSInheritedProtocols.end(); I != E; ++I) |
| 5486 | if (InheritedProtocolSet.count((*I))) |
| 5487 | IntersectionOfProtocols.push_back((*I)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5488 | } |
| 5489 | } |
| 5490 | |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 5491 | /// areCommonBaseCompatible - Returns common base class of the two classes if |
| 5492 | /// one found. Note that this is O'2 algorithm. But it will be called as the |
| 5493 | /// last type comparison in a ?-exp of ObjC pointer types before a |
| 5494 | /// warning is issued. So, its invokation is extremely rare. |
| 5495 | QualType ASTContext::areCommonBaseCompatible( |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5496 | const ObjCObjectPointerType *Lptr, |
| 5497 | const ObjCObjectPointerType *Rptr) { |
| 5498 | const ObjCObjectType *LHS = Lptr->getObjectType(); |
| 5499 | const ObjCObjectType *RHS = Rptr->getObjectType(); |
| 5500 | const ObjCInterfaceDecl* LDecl = LHS->getInterface(); |
| 5501 | const ObjCInterfaceDecl* RDecl = RHS->getInterface(); |
Douglas Gregor | 60ef308 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 5502 | if (!LDecl || !RDecl || (declaresSameEntity(LDecl, RDecl))) |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 5503 | return QualType(); |
| 5504 | |
Fariborz Jahanian | 7c2bdcb | 2011-04-18 21:16:59 +0000 | [diff] [blame] | 5505 | do { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5506 | LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5507 | if (canAssignObjCInterfaces(LHS, RHS)) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5508 | SmallVector<ObjCProtocolDecl *, 8> Protocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5509 | getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols); |
| 5510 | |
| 5511 | QualType Result = QualType(LHS, 0); |
| 5512 | if (!Protocols.empty()) |
| 5513 | Result = getObjCObjectType(Result, Protocols.data(), Protocols.size()); |
| 5514 | Result = getObjCObjectPointerType(Result); |
| 5515 | return Result; |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5516 | } |
Fariborz Jahanian | 7c2bdcb | 2011-04-18 21:16:59 +0000 | [diff] [blame] | 5517 | } while ((LDecl = LDecl->getSuperClass())); |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 5518 | |
| 5519 | return QualType(); |
| 5520 | } |
| 5521 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5522 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS, |
| 5523 | const ObjCObjectType *RHS) { |
| 5524 | assert(LHS->getInterface() && "LHS is not an interface type"); |
| 5525 | assert(RHS->getInterface() && "RHS is not an interface type"); |
| 5526 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5527 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 5528 | // the LHS. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5529 | if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface())) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5530 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5531 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5532 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 5533 | // protocol qualified at all, then we are good. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 5534 | if (LHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5535 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5536 | |
Fariborz Jahanian | b7bc34a | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 5537 | // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, |
| 5538 | // more detailed analysis is required. |
| 5539 | if (RHS->getNumProtocols() == 0) { |
| 5540 | // OK, if LHS is a superclass of RHS *and* |
| 5541 | // this superclass is assignment compatible with LHS. |
| 5542 | // false otherwise. |
Fariborz Jahanian | 627788c | 2011-04-12 16:34:14 +0000 | [diff] [blame] | 5543 | bool IsSuperClass = |
| 5544 | LHS->getInterface()->isSuperClassOf(RHS->getInterface()); |
| 5545 | if (IsSuperClass) { |
Fariborz Jahanian | b7bc34a | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 5546 | // OK if conversion of LHS to SuperClass results in narrowing of types |
| 5547 | // ; i.e., SuperClass may implement at least one of the protocols |
| 5548 | // in LHS's protocol list. Example, SuperObj<P1> = lhs<P1,P2> is ok. |
| 5549 | // But not SuperObj<P1,P2,P3> = lhs<P1,P2>. |
| 5550 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> SuperClassInheritedProtocols; |
Fariborz Jahanian | 627788c | 2011-04-12 16:34:14 +0000 | [diff] [blame] | 5551 | CollectInheritedProtocols(RHS->getInterface(), SuperClassInheritedProtocols); |
Fariborz Jahanian | b7bc34a | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 5552 | // If super class has no protocols, it is not a match. |
| 5553 | if (SuperClassInheritedProtocols.empty()) |
| 5554 | return false; |
| 5555 | |
| 5556 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 5557 | LHSPE = LHS->qual_end(); |
| 5558 | LHSPI != LHSPE; LHSPI++) { |
| 5559 | bool SuperImplementsProtocol = false; |
| 5560 | ObjCProtocolDecl *LHSProto = (*LHSPI); |
| 5561 | |
| 5562 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 5563 | SuperClassInheritedProtocols.begin(), |
| 5564 | E = SuperClassInheritedProtocols.end(); I != E; ++I) { |
| 5565 | ObjCProtocolDecl *SuperClassProto = (*I); |
| 5566 | if (SuperClassProto->lookupProtocolNamed(LHSProto->getIdentifier())) { |
| 5567 | SuperImplementsProtocol = true; |
| 5568 | break; |
| 5569 | } |
| 5570 | } |
| 5571 | if (!SuperImplementsProtocol) |
| 5572 | return false; |
| 5573 | } |
| 5574 | return true; |
| 5575 | } |
| 5576 | return false; |
| 5577 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5578 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5579 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 5580 | LHSPE = LHS->qual_end(); |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 5581 | LHSPI != LHSPE; LHSPI++) { |
| 5582 | bool RHSImplementsProtocol = false; |
| 5583 | |
| 5584 | // If the RHS doesn't implement the protocol on the left, the types |
| 5585 | // are incompatible. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5586 | for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(), |
| 5587 | RHSPE = RHS->qual_end(); |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 5588 | RHSPI != RHSPE; RHSPI++) { |
| 5589 | if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) { |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 5590 | RHSImplementsProtocol = true; |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 5591 | break; |
| 5592 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 5593 | } |
| 5594 | // FIXME: For better diagnostics, consider passing back the protocol name. |
| 5595 | if (!RHSImplementsProtocol) |
| 5596 | return false; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5597 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 5598 | // The RHS implements all protocols listed on the LHS. |
| 5599 | return true; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5600 | } |
| 5601 | |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 5602 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 5603 | // get the "pointed to" types |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5604 | const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
| 5605 | const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5606 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5607 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 5608 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5609 | |
| 5610 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 5611 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 5612 | } |
| 5613 | |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 5614 | bool ASTContext::canBindObjCObjectType(QualType To, QualType From) { |
| 5615 | return canAssignObjCInterfaces( |
| 5616 | getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(), |
| 5617 | getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>()); |
| 5618 | } |
| 5619 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5620 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5621 | /// both shall have the identically qualified version of a compatible type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5622 | /// 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] | 5623 | /// same. See 6.7.[2,3,5] for additional rules. |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5624 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS, |
| 5625 | bool CompareUnqualified) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5626 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 5627 | return hasSameType(LHS, RHS); |
| 5628 | |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5629 | return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5630 | } |
| 5631 | |
Fariborz Jahanian | c286f38 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 5632 | bool ASTContext::propertyTypesAreCompatible(QualType LHS, QualType RHS) { |
Fariborz Jahanian | 8237839 | 2011-07-12 23:20:13 +0000 | [diff] [blame] | 5633 | return typesAreCompatible(LHS, RHS); |
Fariborz Jahanian | c286f38 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 5634 | } |
| 5635 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5636 | bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) { |
| 5637 | return !mergeTypes(LHS, RHS, true).isNull(); |
| 5638 | } |
| 5639 | |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 5640 | /// mergeTransparentUnionType - if T is a transparent union type and a member |
| 5641 | /// of T is compatible with SubType, return the merged type, else return |
| 5642 | /// QualType() |
| 5643 | QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType, |
| 5644 | bool OfBlockPointer, |
| 5645 | bool Unqualified) { |
| 5646 | if (const RecordType *UT = T->getAsUnionType()) { |
| 5647 | RecordDecl *UD = UT->getDecl(); |
| 5648 | if (UD->hasAttr<TransparentUnionAttr>()) { |
| 5649 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 5650 | itend = UD->field_end(); it != itend; ++it) { |
Peter Collingbourne | f91d757 | 2010-12-02 21:00:06 +0000 | [diff] [blame] | 5651 | QualType ET = it->getType().getUnqualifiedType(); |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 5652 | QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified); |
| 5653 | if (!MT.isNull()) |
| 5654 | return MT; |
| 5655 | } |
| 5656 | } |
| 5657 | } |
| 5658 | |
| 5659 | return QualType(); |
| 5660 | } |
| 5661 | |
| 5662 | /// mergeFunctionArgumentTypes - merge two types which appear as function |
| 5663 | /// argument types |
| 5664 | QualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs, |
| 5665 | bool OfBlockPointer, |
| 5666 | bool Unqualified) { |
| 5667 | // GNU extension: two types are compatible if they appear as a function |
| 5668 | // argument, one of the types is a transparent union type and the other |
| 5669 | // type is compatible with a union member |
| 5670 | QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer, |
| 5671 | Unqualified); |
| 5672 | if (!lmerge.isNull()) |
| 5673 | return lmerge; |
| 5674 | |
| 5675 | QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer, |
| 5676 | Unqualified); |
| 5677 | if (!rmerge.isNull()) |
| 5678 | return rmerge; |
| 5679 | |
| 5680 | return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified); |
| 5681 | } |
| 5682 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5683 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs, |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5684 | bool OfBlockPointer, |
| 5685 | bool Unqualified) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5686 | const FunctionType *lbase = lhs->getAs<FunctionType>(); |
| 5687 | const FunctionType *rbase = rhs->getAs<FunctionType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5688 | const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 5689 | const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5690 | bool allLTypes = true; |
| 5691 | bool allRTypes = true; |
| 5692 | |
| 5693 | // Check return type |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5694 | QualType retType; |
Fariborz Jahanian | d263fd1 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 5695 | if (OfBlockPointer) { |
| 5696 | QualType RHS = rbase->getResultType(); |
| 5697 | QualType LHS = lbase->getResultType(); |
| 5698 | bool UnqualifiedResult = Unqualified; |
| 5699 | if (!UnqualifiedResult) |
| 5700 | UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers()); |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5701 | retType = mergeTypes(LHS, RHS, true, UnqualifiedResult, true); |
Fariborz Jahanian | d263fd1 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 5702 | } |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5703 | else |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5704 | retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), false, |
| 5705 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5706 | if (retType.isNull()) return QualType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5707 | |
| 5708 | if (Unqualified) |
| 5709 | retType = retType.getUnqualifiedType(); |
| 5710 | |
| 5711 | CanQualType LRetType = getCanonicalType(lbase->getResultType()); |
| 5712 | CanQualType RRetType = getCanonicalType(rbase->getResultType()); |
| 5713 | if (Unqualified) { |
| 5714 | LRetType = LRetType.getUnqualifiedType(); |
| 5715 | RRetType = RRetType.getUnqualifiedType(); |
| 5716 | } |
| 5717 | |
| 5718 | if (getCanonicalType(retType) != LRetType) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5719 | allLTypes = false; |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5720 | if (getCanonicalType(retType) != RRetType) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5721 | allRTypes = false; |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5722 | |
Daniel Dunbar | 6a15c85 | 2010-04-28 16:20:58 +0000 | [diff] [blame] | 5723 | // FIXME: double check this |
| 5724 | // FIXME: should we error if lbase->getRegParmAttr() != 0 && |
| 5725 | // rbase->getRegParmAttr() != 0 && |
| 5726 | // lbase->getRegParmAttr() != rbase->getRegParmAttr()? |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 5727 | FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo(); |
| 5728 | FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo(); |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5729 | |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 5730 | // Compatible functions must have compatible calling conventions |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5731 | if (!isSameCallConv(lbaseInfo.getCC(), rbaseInfo.getCC())) |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 5732 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5733 | |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5734 | // Regparm is part of the calling convention. |
Eli Friedman | a49218e | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 5735 | if (lbaseInfo.getHasRegParm() != rbaseInfo.getHasRegParm()) |
| 5736 | return QualType(); |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5737 | if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm()) |
| 5738 | return QualType(); |
| 5739 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5740 | if (lbaseInfo.getProducesResult() != rbaseInfo.getProducesResult()) |
| 5741 | return QualType(); |
| 5742 | |
Fariborz Jahanian | 53c8167 | 2011-10-05 00:05:34 +0000 | [diff] [blame] | 5743 | // functypes which return are preferred over those that do not. |
| 5744 | if (lbaseInfo.getNoReturn() && !rbaseInfo.getNoReturn()) |
| 5745 | allLTypes = false; |
| 5746 | else if (!lbaseInfo.getNoReturn() && rbaseInfo.getNoReturn()) |
| 5747 | allRTypes = false; |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5748 | // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'. |
| 5749 | bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn(); |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5750 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5751 | FunctionType::ExtInfo einfo = lbaseInfo.withNoReturn(NoReturn); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5752 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5753 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 5754 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 5755 | "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5756 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 5757 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 5758 | |
| 5759 | // Compatible functions must have the same number of arguments |
| 5760 | if (lproto_nargs != rproto_nargs) |
| 5761 | return QualType(); |
| 5762 | |
| 5763 | // Variadic and non-variadic functions aren't compatible |
| 5764 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 5765 | return QualType(); |
| 5766 | |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 5767 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 5768 | return QualType(); |
| 5769 | |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 5770 | if (LangOpts.ObjCAutoRefCount && |
| 5771 | !FunctionTypesMatchOnNSConsumedAttrs(rproto, lproto)) |
| 5772 | return QualType(); |
| 5773 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5774 | // Check argument compatibility |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5775 | SmallVector<QualType, 10> types; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5776 | for (unsigned i = 0; i < lproto_nargs; i++) { |
| 5777 | QualType largtype = lproto->getArgType(i).getUnqualifiedType(); |
| 5778 | QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 5779 | QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype, |
| 5780 | OfBlockPointer, |
| 5781 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5782 | if (argtype.isNull()) return QualType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5783 | |
| 5784 | if (Unqualified) |
| 5785 | argtype = argtype.getUnqualifiedType(); |
| 5786 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5787 | types.push_back(argtype); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5788 | if (Unqualified) { |
| 5789 | largtype = largtype.getUnqualifiedType(); |
| 5790 | rargtype = rargtype.getUnqualifiedType(); |
| 5791 | } |
| 5792 | |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5793 | if (getCanonicalType(argtype) != getCanonicalType(largtype)) |
| 5794 | allLTypes = false; |
| 5795 | if (getCanonicalType(argtype) != getCanonicalType(rargtype)) |
| 5796 | allRTypes = false; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5797 | } |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 5798 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5799 | if (allLTypes) return lhs; |
| 5800 | if (allRTypes) return rhs; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5801 | |
| 5802 | FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo(); |
| 5803 | EPI.ExtInfo = einfo; |
| 5804 | return getFunctionType(retType, types.begin(), types.size(), EPI); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5805 | } |
| 5806 | |
| 5807 | if (lproto) allRTypes = false; |
| 5808 | if (rproto) allLTypes = false; |
| 5809 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5810 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5811 | if (proto) { |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 5812 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5813 | if (proto->isVariadic()) return QualType(); |
| 5814 | // Check that the types are compatible with the types that |
| 5815 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 5816 | // The only types actually affected are promotable integer |
| 5817 | // types and floats, which would be passed as a different |
| 5818 | // type depending on whether the prototype is visible. |
| 5819 | unsigned proto_nargs = proto->getNumArgs(); |
| 5820 | for (unsigned i = 0; i < proto_nargs; ++i) { |
| 5821 | QualType argTy = proto->getArgType(i); |
Douglas Gregor | b0f8eac | 2010-02-03 19:27:29 +0000 | [diff] [blame] | 5822 | |
| 5823 | // Look at the promotion type of enum types, since that is the type used |
| 5824 | // to pass enum values. |
| 5825 | if (const EnumType *Enum = argTy->getAs<EnumType>()) |
| 5826 | argTy = Enum->getDecl()->getPromotionType(); |
| 5827 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5828 | if (argTy->isPromotableIntegerType() || |
| 5829 | getCanonicalType(argTy).getUnqualifiedType() == FloatTy) |
| 5830 | return QualType(); |
| 5831 | } |
| 5832 | |
| 5833 | if (allLTypes) return lhs; |
| 5834 | if (allRTypes) return rhs; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5835 | |
| 5836 | FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo(); |
| 5837 | EPI.ExtInfo = einfo; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5838 | return getFunctionType(retType, proto->arg_type_begin(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5839 | proto->getNumArgs(), EPI); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5840 | } |
| 5841 | |
| 5842 | if (allLTypes) return lhs; |
| 5843 | if (allRTypes) return rhs; |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 5844 | return getFunctionNoProtoType(retType, einfo); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5845 | } |
| 5846 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5847 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5848 | bool OfBlockPointer, |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5849 | bool Unqualified, bool BlockReturnType) { |
Bill Wendling | 43d6975 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 5850 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 5851 | // type is adjusted to "T" prior to any further analysis, the expression |
| 5852 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 5853 | // expression is an lvalue unless the reference is an rvalue reference and |
| 5854 | // the expression is a function call (possibly inside parentheses). |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 5855 | assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?"); |
| 5856 | assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?"); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5857 | |
| 5858 | if (Unqualified) { |
| 5859 | LHS = LHS.getUnqualifiedType(); |
| 5860 | RHS = RHS.getUnqualifiedType(); |
| 5861 | } |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 5862 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5863 | QualType LHSCan = getCanonicalType(LHS), |
| 5864 | RHSCan = getCanonicalType(RHS); |
| 5865 | |
| 5866 | // If two types are identical, they are compatible. |
| 5867 | if (LHSCan == RHSCan) |
| 5868 | return LHS; |
| 5869 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5870 | // If the qualifiers are different, the types aren't compatible... mostly. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 5871 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 5872 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5873 | if (LQuals != RQuals) { |
| 5874 | // If any of these qualifiers are different, we have a type |
| 5875 | // mismatch. |
| 5876 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5877 | LQuals.getAddressSpace() != RQuals.getAddressSpace() || |
| 5878 | LQuals.getObjCLifetime() != RQuals.getObjCLifetime()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5879 | return QualType(); |
| 5880 | |
| 5881 | // Exactly one GC qualifier difference is allowed: __strong is |
| 5882 | // okay if the other type has no GC qualifier but is an Objective |
| 5883 | // C object pointer (i.e. implicitly strong by default). We fix |
| 5884 | // this by pretending that the unqualified type was actually |
| 5885 | // qualified __strong. |
| 5886 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 5887 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 5888 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 5889 | |
| 5890 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 5891 | return QualType(); |
| 5892 | |
| 5893 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
| 5894 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); |
| 5895 | } |
| 5896 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
| 5897 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); |
| 5898 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5899 | return QualType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5900 | } |
| 5901 | |
| 5902 | // Okay, qualifiers are equal. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5903 | |
Eli Friedman | 852d63b | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 5904 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 5905 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5906 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5907 | // We want to consider the two function types to be the same for these |
| 5908 | // comparisons, just force one to the other. |
| 5909 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 5910 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 4c721d3 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 5911 | |
| 5912 | // Same as above for arrays |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 5913 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 5914 | LHSClass = Type::ConstantArray; |
| 5915 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 5916 | RHSClass = Type::ConstantArray; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5917 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5918 | // ObjCInterfaces are just specialized ObjCObjects. |
| 5919 | if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject; |
| 5920 | if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject; |
| 5921 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 5922 | // Canonicalize ExtVector -> Vector. |
| 5923 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 5924 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5925 | |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 5926 | // If the canonical type classes don't match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5927 | if (LHSClass != RHSClass) { |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5928 | // 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] | 5929 | // a signed integer type, or an unsigned integer type. |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 5930 | // Compatibility is based on the underlying type, not the promotion |
| 5931 | // type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5932 | if (const EnumType* ETy = LHS->getAs<EnumType>()) { |
Fariborz Jahanian | b918d6b | 2012-02-06 19:06:20 +0000 | [diff] [blame] | 5933 | QualType TINT = ETy->getDecl()->getIntegerType(); |
| 5934 | if (!TINT.isNull() && hasSameType(TINT, RHSCan.getUnqualifiedType())) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5935 | return RHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 5936 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5937 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
Fariborz Jahanian | b918d6b | 2012-02-06 19:06:20 +0000 | [diff] [blame] | 5938 | QualType TINT = ETy->getDecl()->getIntegerType(); |
| 5939 | if (!TINT.isNull() && hasSameType(TINT, LHSCan.getUnqualifiedType())) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5940 | return LHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 5941 | } |
Fariborz Jahanian | e7cff2c | 2012-01-26 00:45:38 +0000 | [diff] [blame] | 5942 | // allow block pointer type to match an 'id' type. |
Fariborz Jahanian | 4196363 | 2012-01-26 17:08:50 +0000 | [diff] [blame] | 5943 | if (OfBlockPointer && !BlockReturnType) { |
| 5944 | if (LHS->isObjCIdType() && RHS->isBlockPointerType()) |
| 5945 | return LHS; |
| 5946 | if (RHS->isObjCIdType() && LHS->isBlockPointerType()) |
| 5947 | return RHS; |
| 5948 | } |
Fariborz Jahanian | e7cff2c | 2012-01-26 00:45:38 +0000 | [diff] [blame] | 5949 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5950 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5951 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5952 | |
Steve Naroff | 4a74678 | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 5953 | // The canonical type classes match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5954 | switch (LHSClass) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5955 | #define TYPE(Class, Base) |
| 5956 | #define ABSTRACT_TYPE(Class, Base) |
John McCall | ad5e738 | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 5957 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5958 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 5959 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 5960 | #include "clang/AST/TypeNodes.def" |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5961 | llvm_unreachable("Non-canonical and dependent types shouldn't get here"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5962 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 5963 | case Type::LValueReference: |
| 5964 | case Type::RValueReference: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5965 | case Type::MemberPointer: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5966 | llvm_unreachable("C++ should never be in mergeTypes"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5967 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5968 | case Type::ObjCInterface: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5969 | case Type::IncompleteArray: |
| 5970 | case Type::VariableArray: |
| 5971 | case Type::FunctionProto: |
| 5972 | case Type::ExtVector: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5973 | llvm_unreachable("Types are eliminated above"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5974 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5975 | case Type::Pointer: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5976 | { |
| 5977 | // Merge two pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5978 | QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); |
| 5979 | QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5980 | if (Unqualified) { |
| 5981 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 5982 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 5983 | } |
| 5984 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false, |
| 5985 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5986 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 5987 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5988 | return LHS; |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 5989 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5990 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5991 | return getPointerType(ResultType); |
| 5992 | } |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 5993 | case Type::BlockPointer: |
| 5994 | { |
| 5995 | // Merge two block pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5996 | QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); |
| 5997 | QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5998 | if (Unqualified) { |
| 5999 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 6000 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 6001 | } |
| 6002 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer, |
| 6003 | Unqualified); |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 6004 | if (ResultType.isNull()) return QualType(); |
| 6005 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 6006 | return LHS; |
| 6007 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 6008 | return RHS; |
| 6009 | return getBlockPointerType(ResultType); |
| 6010 | } |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6011 | case Type::Atomic: |
| 6012 | { |
| 6013 | // Merge two pointer types, while trying to preserve typedef info |
| 6014 | QualType LHSValue = LHS->getAs<AtomicType>()->getValueType(); |
| 6015 | QualType RHSValue = RHS->getAs<AtomicType>()->getValueType(); |
| 6016 | if (Unqualified) { |
| 6017 | LHSValue = LHSValue.getUnqualifiedType(); |
| 6018 | RHSValue = RHSValue.getUnqualifiedType(); |
| 6019 | } |
| 6020 | QualType ResultType = mergeTypes(LHSValue, RHSValue, false, |
| 6021 | Unqualified); |
| 6022 | if (ResultType.isNull()) return QualType(); |
| 6023 | if (getCanonicalType(LHSValue) == getCanonicalType(ResultType)) |
| 6024 | return LHS; |
| 6025 | if (getCanonicalType(RHSValue) == getCanonicalType(ResultType)) |
| 6026 | return RHS; |
| 6027 | return getAtomicType(ResultType); |
| 6028 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6029 | case Type::ConstantArray: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6030 | { |
| 6031 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 6032 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 6033 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 6034 | return QualType(); |
| 6035 | |
| 6036 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 6037 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6038 | if (Unqualified) { |
| 6039 | LHSElem = LHSElem.getUnqualifiedType(); |
| 6040 | RHSElem = RHSElem.getUnqualifiedType(); |
| 6041 | } |
| 6042 | |
| 6043 | QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6044 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6045 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 6046 | return LHS; |
| 6047 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 6048 | return RHS; |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 6049 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 6050 | ArrayType::ArraySizeModifier(), 0); |
| 6051 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 6052 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6053 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 6054 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6055 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 6056 | return LHS; |
| 6057 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 6058 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6059 | if (LVAT) { |
| 6060 | // FIXME: This isn't correct! But tricky to implement because |
| 6061 | // the array's size has to be the size of LHS, but the type |
| 6062 | // has to be different. |
| 6063 | return LHS; |
| 6064 | } |
| 6065 | if (RVAT) { |
| 6066 | // FIXME: This isn't correct! But tricky to implement because |
| 6067 | // the array's size has to be the size of RHS, but the type |
| 6068 | // has to be different. |
| 6069 | return RHS; |
| 6070 | } |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 6071 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 6072 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 6073 | return getIncompleteArrayType(ResultType, |
| 6074 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6075 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6076 | case Type::FunctionNoProto: |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6077 | return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6078 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6079 | case Type::Enum: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6080 | return QualType(); |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6081 | case Type::Builtin: |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 6082 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6083 | return QualType(); |
Daniel Dunbar | 64cfdb7 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 6084 | case Type::Complex: |
| 6085 | // Distinct complex types are incompatible. |
| 6086 | return QualType(); |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 6087 | case Type::Vector: |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 6088 | // FIXME: The merged type should be an ExtVector! |
John McCall | 1c471f3 | 2010-03-12 23:14:13 +0000 | [diff] [blame] | 6089 | if (areCompatVectorTypes(LHSCan->getAs<VectorType>(), |
| 6090 | RHSCan->getAs<VectorType>())) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6091 | return LHS; |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6092 | return QualType(); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6093 | case Type::ObjCObject: { |
| 6094 | // Check if the types are assignment compatible. |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 6095 | // FIXME: This should be type compatibility, e.g. whether |
| 6096 | // "LHS x; RHS x;" at global scope is legal. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6097 | const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>(); |
| 6098 | const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>(); |
| 6099 | if (canAssignObjCInterfaces(LHSIface, RHSIface)) |
Steve Naroff | 5fd659d | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 6100 | return LHS; |
| 6101 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6102 | return QualType(); |
Cedric Venet | 61490e9 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 6103 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6104 | case Type::ObjCObjectPointer: { |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6105 | if (OfBlockPointer) { |
| 6106 | if (canAssignObjCInterfacesInBlockPointer( |
| 6107 | LHS->getAs<ObjCObjectPointerType>(), |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6108 | RHS->getAs<ObjCObjectPointerType>(), |
| 6109 | BlockReturnType)) |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 6110 | return LHS; |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6111 | return QualType(); |
| 6112 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6113 | if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(), |
| 6114 | RHS->getAs<ObjCObjectPointerType>())) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6115 | return LHS; |
| 6116 | |
Steve Naroff | bc76dd0 | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 6117 | return QualType(); |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 6118 | } |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 6119 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6120 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 6121 | llvm_unreachable("Invalid Type::Class!"); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 6122 | } |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 6123 | |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 6124 | bool ASTContext::FunctionTypesMatchOnNSConsumedAttrs( |
| 6125 | const FunctionProtoType *FromFunctionType, |
| 6126 | const FunctionProtoType *ToFunctionType) { |
| 6127 | if (FromFunctionType->hasAnyConsumedArgs() != |
| 6128 | ToFunctionType->hasAnyConsumedArgs()) |
| 6129 | return false; |
| 6130 | FunctionProtoType::ExtProtoInfo FromEPI = |
| 6131 | FromFunctionType->getExtProtoInfo(); |
| 6132 | FunctionProtoType::ExtProtoInfo ToEPI = |
| 6133 | ToFunctionType->getExtProtoInfo(); |
| 6134 | if (FromEPI.ConsumedArguments && ToEPI.ConsumedArguments) |
| 6135 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 6136 | ArgIdx != NumArgs; ++ArgIdx) { |
| 6137 | if (FromEPI.ConsumedArguments[ArgIdx] != |
| 6138 | ToEPI.ConsumedArguments[ArgIdx]) |
| 6139 | return false; |
| 6140 | } |
| 6141 | return true; |
| 6142 | } |
| 6143 | |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 6144 | /// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and |
| 6145 | /// 'RHS' attributes and returns the merged version; including for function |
| 6146 | /// return types. |
| 6147 | QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) { |
| 6148 | QualType LHSCan = getCanonicalType(LHS), |
| 6149 | RHSCan = getCanonicalType(RHS); |
| 6150 | // If two types are identical, they are compatible. |
| 6151 | if (LHSCan == RHSCan) |
| 6152 | return LHS; |
| 6153 | if (RHSCan->isFunctionType()) { |
| 6154 | if (!LHSCan->isFunctionType()) |
| 6155 | return QualType(); |
| 6156 | QualType OldReturnType = |
| 6157 | cast<FunctionType>(RHSCan.getTypePtr())->getResultType(); |
| 6158 | QualType NewReturnType = |
| 6159 | cast<FunctionType>(LHSCan.getTypePtr())->getResultType(); |
| 6160 | QualType ResReturnType = |
| 6161 | mergeObjCGCQualifiers(NewReturnType, OldReturnType); |
| 6162 | if (ResReturnType.isNull()) |
| 6163 | return QualType(); |
| 6164 | if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) { |
| 6165 | // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo(); |
| 6166 | // In either case, use OldReturnType to build the new function type. |
| 6167 | const FunctionType *F = LHS->getAs<FunctionType>(); |
| 6168 | if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6169 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 6170 | EPI.ExtInfo = getFunctionExtInfo(LHS); |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 6171 | QualType ResultType |
| 6172 | = getFunctionType(OldReturnType, FPT->arg_type_begin(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6173 | FPT->getNumArgs(), EPI); |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 6174 | return ResultType; |
| 6175 | } |
| 6176 | } |
| 6177 | return QualType(); |
| 6178 | } |
| 6179 | |
| 6180 | // If the qualifiers are different, the types can still be merged. |
| 6181 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 6182 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
| 6183 | if (LQuals != RQuals) { |
| 6184 | // If any of these qualifiers are different, we have a type mismatch. |
| 6185 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 6186 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 6187 | return QualType(); |
| 6188 | |
| 6189 | // Exactly one GC qualifier difference is allowed: __strong is |
| 6190 | // okay if the other type has no GC qualifier but is an Objective |
| 6191 | // C object pointer (i.e. implicitly strong by default). We fix |
| 6192 | // this by pretending that the unqualified type was actually |
| 6193 | // qualified __strong. |
| 6194 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 6195 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 6196 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 6197 | |
| 6198 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 6199 | return QualType(); |
| 6200 | |
| 6201 | if (GC_L == Qualifiers::Strong) |
| 6202 | return LHS; |
| 6203 | if (GC_R == Qualifiers::Strong) |
| 6204 | return RHS; |
| 6205 | return QualType(); |
| 6206 | } |
| 6207 | |
| 6208 | if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) { |
| 6209 | QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 6210 | QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 6211 | QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT); |
| 6212 | if (ResQT == LHSBaseQT) |
| 6213 | return LHS; |
| 6214 | if (ResQT == RHSBaseQT) |
| 6215 | return RHS; |
| 6216 | } |
| 6217 | return QualType(); |
| 6218 | } |
| 6219 | |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 6220 | //===----------------------------------------------------------------------===// |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6221 | // Integer Predicates |
| 6222 | //===----------------------------------------------------------------------===// |
Chris Lattner | 88054de | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 6223 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6224 | unsigned ASTContext::getIntWidth(QualType T) const { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 6225 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
Eli Friedman | 29a7f33 | 2009-12-10 22:29:29 +0000 | [diff] [blame] | 6226 | T = ET->getDecl()->getIntegerType(); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6227 | if (T->isBooleanType()) |
| 6228 | return 1; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 6229 | // For builtin types, just use the standard type sizing method |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6230 | return (unsigned)getTypeSize(T); |
| 6231 | } |
| 6232 | |
| 6233 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) { |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6234 | assert(T->hasSignedIntegerRepresentation() && "Unexpected type"); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6235 | |
| 6236 | // Turn <4 x signed int> -> <4 x unsigned int> |
| 6237 | if (const VectorType *VTy = T->getAs<VectorType>()) |
| 6238 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 6239 | VTy->getNumElements(), VTy->getVectorKind()); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6240 | |
| 6241 | // For enums, we return the unsigned version of the base type. |
| 6242 | if (const EnumType *ETy = T->getAs<EnumType>()) |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6243 | T = ETy->getDecl()->getIntegerType(); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6244 | |
| 6245 | const BuiltinType *BTy = T->getAs<BuiltinType>(); |
| 6246 | assert(BTy && "Unexpected signed integer type"); |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6247 | switch (BTy->getKind()) { |
| 6248 | case BuiltinType::Char_S: |
| 6249 | case BuiltinType::SChar: |
| 6250 | return UnsignedCharTy; |
| 6251 | case BuiltinType::Short: |
| 6252 | return UnsignedShortTy; |
| 6253 | case BuiltinType::Int: |
| 6254 | return UnsignedIntTy; |
| 6255 | case BuiltinType::Long: |
| 6256 | return UnsignedLongTy; |
| 6257 | case BuiltinType::LongLong: |
| 6258 | return UnsignedLongLongTy; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 6259 | case BuiltinType::Int128: |
| 6260 | return UnsignedInt128Ty; |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6261 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6262 | llvm_unreachable("Unexpected signed integer type"); |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6263 | } |
| 6264 | } |
| 6265 | |
Argyrios Kyrtzidis | 7b90340 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 6266 | ASTMutationListener::~ASTMutationListener() { } |
| 6267 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6268 | |
| 6269 | //===----------------------------------------------------------------------===// |
| 6270 | // Builtin Type Computation |
| 6271 | //===----------------------------------------------------------------------===// |
| 6272 | |
| 6273 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6274 | /// pointer over the consumed characters. This returns the resultant type. If |
| 6275 | /// AllowTypeModifiers is false then modifier like * are not parsed, just basic |
| 6276 | /// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of |
| 6277 | /// a vector of "i*". |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6278 | /// |
| 6279 | /// RequiresICE is filled in on return to indicate whether the value is required |
| 6280 | /// to be an Integer Constant Expression. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6281 | static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context, |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6282 | ASTContext::GetBuiltinTypeError &Error, |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6283 | bool &RequiresICE, |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6284 | bool AllowTypeModifiers) { |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6285 | // Modifiers. |
| 6286 | int HowLong = 0; |
| 6287 | bool Signed = false, Unsigned = false; |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6288 | RequiresICE = false; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6289 | |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6290 | // Read the prefixed modifiers first. |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6291 | bool Done = false; |
| 6292 | while (!Done) { |
| 6293 | switch (*Str++) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6294 | default: Done = true; --Str; break; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6295 | case 'I': |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6296 | RequiresICE = true; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6297 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6298 | case 'S': |
| 6299 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 6300 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 6301 | Signed = true; |
| 6302 | break; |
| 6303 | case 'U': |
| 6304 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
| 6305 | assert(!Unsigned && "Can't use 'S' modifier multiple times!"); |
| 6306 | Unsigned = true; |
| 6307 | break; |
| 6308 | case 'L': |
| 6309 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 6310 | ++HowLong; |
| 6311 | break; |
| 6312 | } |
| 6313 | } |
| 6314 | |
| 6315 | QualType Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6316 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6317 | // Read the base type. |
| 6318 | switch (*Str++) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6319 | default: llvm_unreachable("Unknown builtin type letter!"); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6320 | case 'v': |
| 6321 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 6322 | "Bad modifiers used with 'v'!"); |
| 6323 | Type = Context.VoidTy; |
| 6324 | break; |
| 6325 | case 'f': |
| 6326 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 6327 | "Bad modifiers used with 'f'!"); |
| 6328 | Type = Context.FloatTy; |
| 6329 | break; |
| 6330 | case 'd': |
| 6331 | assert(HowLong < 2 && !Signed && !Unsigned && |
| 6332 | "Bad modifiers used with 'd'!"); |
| 6333 | if (HowLong) |
| 6334 | Type = Context.LongDoubleTy; |
| 6335 | else |
| 6336 | Type = Context.DoubleTy; |
| 6337 | break; |
| 6338 | case 's': |
| 6339 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 6340 | if (Unsigned) |
| 6341 | Type = Context.UnsignedShortTy; |
| 6342 | else |
| 6343 | Type = Context.ShortTy; |
| 6344 | break; |
| 6345 | case 'i': |
| 6346 | if (HowLong == 3) |
| 6347 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 6348 | else if (HowLong == 2) |
| 6349 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 6350 | else if (HowLong == 1) |
| 6351 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 6352 | else |
| 6353 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 6354 | break; |
| 6355 | case 'c': |
| 6356 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 6357 | if (Signed) |
| 6358 | Type = Context.SignedCharTy; |
| 6359 | else if (Unsigned) |
| 6360 | Type = Context.UnsignedCharTy; |
| 6361 | else |
| 6362 | Type = Context.CharTy; |
| 6363 | break; |
| 6364 | case 'b': // boolean |
| 6365 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 6366 | Type = Context.BoolTy; |
| 6367 | break; |
| 6368 | case 'z': // size_t. |
| 6369 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 6370 | Type = Context.getSizeType(); |
| 6371 | break; |
| 6372 | case 'F': |
| 6373 | Type = Context.getCFConstantStringType(); |
| 6374 | break; |
Fariborz Jahanian | ba8bda0 | 2010-11-09 21:38:20 +0000 | [diff] [blame] | 6375 | case 'G': |
| 6376 | Type = Context.getObjCIdType(); |
| 6377 | break; |
| 6378 | case 'H': |
| 6379 | Type = Context.getObjCSelType(); |
| 6380 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6381 | case 'a': |
| 6382 | Type = Context.getBuiltinVaListType(); |
| 6383 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 6384 | break; |
| 6385 | case 'A': |
| 6386 | // This is a "reference" to a va_list; however, what exactly |
| 6387 | // this means depends on how va_list is defined. There are two |
| 6388 | // different kinds of va_list: ones passed by value, and ones |
| 6389 | // passed by reference. An example of a by-value va_list is |
| 6390 | // x86, where va_list is a char*. An example of by-ref va_list |
| 6391 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 6392 | // we want this argument to be a char*&; for x86-64, we want |
| 6393 | // it to be a __va_list_tag*. |
| 6394 | Type = Context.getBuiltinVaListType(); |
| 6395 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6396 | if (Type->isArrayType()) |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6397 | Type = Context.getArrayDecayedType(Type); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6398 | else |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6399 | Type = Context.getLValueReferenceType(Type); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6400 | break; |
| 6401 | case 'V': { |
| 6402 | char *End; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6403 | unsigned NumElements = strtoul(Str, &End, 10); |
| 6404 | assert(End != Str && "Missing vector size"); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6405 | Str = End; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6406 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6407 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, |
| 6408 | RequiresICE, false); |
| 6409 | assert(!RequiresICE && "Can't require vector ICE"); |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6410 | |
| 6411 | // TODO: No way to make AltiVec vectors in builtins yet. |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 6412 | Type = Context.getVectorType(ElementType, NumElements, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 6413 | VectorType::GenericVector); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6414 | break; |
| 6415 | } |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 6416 | case 'X': { |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6417 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
| 6418 | false); |
| 6419 | assert(!RequiresICE && "Can't require complex ICE"); |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 6420 | Type = Context.getComplexType(ElementType); |
| 6421 | break; |
Fariborz Jahanian | cc075e4 | 2011-08-23 23:33:09 +0000 | [diff] [blame] | 6422 | } |
| 6423 | case 'Y' : { |
| 6424 | Type = Context.getPointerDiffType(); |
| 6425 | break; |
| 6426 | } |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 6427 | case 'P': |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 6428 | Type = Context.getFILEType(); |
| 6429 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6430 | Error = ASTContext::GE_Missing_stdio; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6431 | return QualType(); |
| 6432 | } |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 6433 | break; |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 6434 | case 'J': |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6435 | if (Signed) |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 6436 | Type = Context.getsigjmp_bufType(); |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6437 | else |
| 6438 | Type = Context.getjmp_bufType(); |
| 6439 | |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 6440 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6441 | Error = ASTContext::GE_Missing_setjmp; |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 6442 | return QualType(); |
| 6443 | } |
| 6444 | break; |
Rafael Espindola | e2d4f4e | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 6445 | case 'K': |
| 6446 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'K'!"); |
| 6447 | Type = Context.getucontext_tType(); |
| 6448 | |
| 6449 | if (Type.isNull()) { |
| 6450 | Error = ASTContext::GE_Missing_ucontext; |
| 6451 | return QualType(); |
| 6452 | } |
| 6453 | break; |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 6454 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6455 | |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6456 | // If there are modifiers and if we're allowed to parse them, go for it. |
| 6457 | Done = !AllowTypeModifiers; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6458 | while (!Done) { |
John McCall | 187ab37 | 2010-03-12 04:21:28 +0000 | [diff] [blame] | 6459 | switch (char c = *Str++) { |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6460 | default: Done = true; --Str; break; |
| 6461 | case '*': |
| 6462 | case '&': { |
| 6463 | // Both pointers and references can have their pointee types |
| 6464 | // qualified with an address space. |
| 6465 | char *End; |
| 6466 | unsigned AddrSpace = strtoul(Str, &End, 10); |
| 6467 | if (End != Str && AddrSpace != 0) { |
| 6468 | Type = Context.getAddrSpaceQualType(Type, AddrSpace); |
| 6469 | Str = End; |
| 6470 | } |
| 6471 | if (c == '*') |
| 6472 | Type = Context.getPointerType(Type); |
| 6473 | else |
| 6474 | Type = Context.getLValueReferenceType(Type); |
| 6475 | break; |
| 6476 | } |
| 6477 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 6478 | case 'C': |
| 6479 | Type = Type.withConst(); |
| 6480 | break; |
| 6481 | case 'D': |
| 6482 | Type = Context.getVolatileType(Type); |
| 6483 | break; |
Ted Kremenek | 18932a0 | 2012-01-20 21:40:12 +0000 | [diff] [blame] | 6484 | case 'R': |
| 6485 | Type = Type.withRestrict(); |
| 6486 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6487 | } |
| 6488 | } |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6489 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6490 | assert((!RequiresICE || Type->isIntegralOrEnumerationType()) && |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6491 | "Integer constant 'I' type must be an integer"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6492 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6493 | return Type; |
| 6494 | } |
| 6495 | |
| 6496 | /// GetBuiltinType - Return the type for the specified builtin. |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6497 | QualType ASTContext::GetBuiltinType(unsigned Id, |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6498 | GetBuiltinTypeError &Error, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6499 | unsigned *IntegerConstantArgs) const { |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6500 | const char *TypeStr = BuiltinInfo.GetTypeString(Id); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6501 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6502 | SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6503 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6504 | bool RequiresICE = false; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6505 | Error = GE_None; |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6506 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error, |
| 6507 | RequiresICE, true); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6508 | if (Error != GE_None) |
| 6509 | return QualType(); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6510 | |
| 6511 | assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE"); |
| 6512 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6513 | while (TypeStr[0] && TypeStr[0] != '.') { |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6514 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6515 | if (Error != GE_None) |
| 6516 | return QualType(); |
| 6517 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6518 | // If this argument is required to be an IntegerConstantExpression and the |
| 6519 | // caller cares, fill in the bitmask we return. |
| 6520 | if (RequiresICE && IntegerConstantArgs) |
| 6521 | *IntegerConstantArgs |= 1 << ArgTypes.size(); |
| 6522 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6523 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 6524 | if (Ty->isArrayType()) |
| 6525 | Ty = getArrayDecayedType(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6526 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6527 | ArgTypes.push_back(Ty); |
| 6528 | } |
| 6529 | |
| 6530 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 6531 | "'.' should only occur at end of builtin type list!"); |
| 6532 | |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 6533 | FunctionType::ExtInfo EI; |
| 6534 | if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true); |
| 6535 | |
| 6536 | bool Variadic = (TypeStr[0] == '.'); |
| 6537 | |
| 6538 | // We really shouldn't be making a no-proto type here, especially in C++. |
| 6539 | if (ArgTypes.empty() && Variadic) |
| 6540 | return getFunctionNoProtoType(ResType, EI); |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 6541 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6542 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 6543 | EPI.ExtInfo = EI; |
| 6544 | EPI.Variadic = Variadic; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6545 | |
| 6546 | return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), EPI); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6547 | } |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 6548 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6549 | GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) { |
| 6550 | GVALinkage External = GVA_StrongExternal; |
| 6551 | |
| 6552 | Linkage L = FD->getLinkage(); |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6553 | switch (L) { |
| 6554 | case NoLinkage: |
| 6555 | case InternalLinkage: |
| 6556 | case UniqueExternalLinkage: |
| 6557 | return GVA_Internal; |
| 6558 | |
| 6559 | case ExternalLinkage: |
| 6560 | switch (FD->getTemplateSpecializationKind()) { |
| 6561 | case TSK_Undeclared: |
| 6562 | case TSK_ExplicitSpecialization: |
| 6563 | External = GVA_StrongExternal; |
| 6564 | break; |
| 6565 | |
| 6566 | case TSK_ExplicitInstantiationDefinition: |
| 6567 | return GVA_ExplicitTemplateInstantiation; |
| 6568 | |
| 6569 | case TSK_ExplicitInstantiationDeclaration: |
| 6570 | case TSK_ImplicitInstantiation: |
| 6571 | External = GVA_TemplateInstantiation; |
| 6572 | break; |
| 6573 | } |
| 6574 | } |
| 6575 | |
| 6576 | if (!FD->isInlined()) |
| 6577 | return External; |
| 6578 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6579 | if (!getLangOpts().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) { |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6580 | // GNU or C99 inline semantics. Determine whether this symbol should be |
| 6581 | // externally visible. |
| 6582 | if (FD->isInlineDefinitionExternallyVisible()) |
| 6583 | return External; |
| 6584 | |
| 6585 | // C99 inline semantics, where the symbol is not externally visible. |
| 6586 | return GVA_C99Inline; |
| 6587 | } |
| 6588 | |
| 6589 | // C++0x [temp.explicit]p9: |
| 6590 | // [ Note: The intent is that an inline function that is the subject of |
| 6591 | // an explicit instantiation declaration will still be implicitly |
| 6592 | // instantiated when used so that the body can be considered for |
| 6593 | // inlining, but that no out-of-line copy of the inline function would be |
| 6594 | // generated in the translation unit. -- end note ] |
| 6595 | if (FD->getTemplateSpecializationKind() |
| 6596 | == TSK_ExplicitInstantiationDeclaration) |
| 6597 | return GVA_C99Inline; |
| 6598 | |
| 6599 | return GVA_CXXInline; |
| 6600 | } |
| 6601 | |
| 6602 | GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) { |
| 6603 | // If this is a static data member, compute the kind of template |
| 6604 | // specialization. Otherwise, this variable is not part of a |
| 6605 | // template. |
| 6606 | TemplateSpecializationKind TSK = TSK_Undeclared; |
| 6607 | if (VD->isStaticDataMember()) |
| 6608 | TSK = VD->getTemplateSpecializationKind(); |
| 6609 | |
| 6610 | Linkage L = VD->getLinkage(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6611 | if (L == ExternalLinkage && getLangOpts().CPlusPlus && |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6612 | VD->getType()->getLinkage() == UniqueExternalLinkage) |
| 6613 | L = UniqueExternalLinkage; |
| 6614 | |
| 6615 | switch (L) { |
| 6616 | case NoLinkage: |
| 6617 | case InternalLinkage: |
| 6618 | case UniqueExternalLinkage: |
| 6619 | return GVA_Internal; |
| 6620 | |
| 6621 | case ExternalLinkage: |
| 6622 | switch (TSK) { |
| 6623 | case TSK_Undeclared: |
| 6624 | case TSK_ExplicitSpecialization: |
| 6625 | return GVA_StrongExternal; |
| 6626 | |
| 6627 | case TSK_ExplicitInstantiationDeclaration: |
| 6628 | llvm_unreachable("Variable should not be instantiated"); |
| 6629 | // Fall through to treat this like any other instantiation. |
| 6630 | |
| 6631 | case TSK_ExplicitInstantiationDefinition: |
| 6632 | return GVA_ExplicitTemplateInstantiation; |
| 6633 | |
| 6634 | case TSK_ImplicitInstantiation: |
| 6635 | return GVA_TemplateInstantiation; |
| 6636 | } |
| 6637 | } |
| 6638 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 6639 | llvm_unreachable("Invalid Linkage!"); |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6640 | } |
| 6641 | |
Argyrios Kyrtzidis | 4ac7c0b | 2010-07-29 20:08:05 +0000 | [diff] [blame] | 6642 | bool ASTContext::DeclMustBeEmitted(const Decl *D) { |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6643 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 6644 | if (!VD->isFileVarDecl()) |
| 6645 | return false; |
Argyrios Kyrtzidis | 144b38a | 2011-09-13 21:35:00 +0000 | [diff] [blame] | 6646 | } else if (!isa<FunctionDecl>(D)) |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6647 | return false; |
| 6648 | |
Argyrios Kyrtzidis | ab411c8 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 6649 | // Weak references don't produce any output by themselves. |
| 6650 | if (D->hasAttr<WeakRefAttr>()) |
| 6651 | return false; |
| 6652 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6653 | // Aliases and used decls are required. |
| 6654 | if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>()) |
| 6655 | return true; |
| 6656 | |
| 6657 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 6658 | // Forward declarations aren't required. |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 6659 | if (!FD->doesThisDeclarationHaveABody()) |
Nick Lewycky | dce67a7 | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 6660 | return FD->doesDeclarationForceExternallyVisibleDefinition(); |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6661 | |
| 6662 | // Constructors and destructors are required. |
| 6663 | if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>()) |
| 6664 | return true; |
| 6665 | |
| 6666 | // The key function for a class is required. |
| 6667 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 6668 | const CXXRecordDecl *RD = MD->getParent(); |
| 6669 | if (MD->isOutOfLine() && RD->isDynamicClass()) { |
| 6670 | const CXXMethodDecl *KeyFunc = getKeyFunction(RD); |
| 6671 | if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl()) |
| 6672 | return true; |
| 6673 | } |
| 6674 | } |
| 6675 | |
| 6676 | GVALinkage Linkage = GetGVALinkageForFunction(FD); |
| 6677 | |
| 6678 | // static, static inline, always_inline, and extern inline functions can |
| 6679 | // always be deferred. Normal inline functions can be deferred in C99/C++. |
| 6680 | // Implicit template instantiations can also be deferred in C++. |
| 6681 | if (Linkage == GVA_Internal || Linkage == GVA_C99Inline || |
Anton Yartsev | 3a5aca8 | 2012-02-02 06:06:34 +0000 | [diff] [blame] | 6682 | Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6683 | return false; |
| 6684 | return true; |
| 6685 | } |
Douglas Gregor | 94da158 | 2011-09-10 00:22:34 +0000 | [diff] [blame] | 6686 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6687 | const VarDecl *VD = cast<VarDecl>(D); |
| 6688 | assert(VD->isFileVarDecl() && "Expected file scoped var"); |
| 6689 | |
Argyrios Kyrtzidis | ab411c8 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 6690 | if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly) |
| 6691 | return false; |
| 6692 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6693 | // Structs that have non-trivial constructors or destructors are required. |
| 6694 | |
| 6695 | // FIXME: Handle references. |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 6696 | // FIXME: Be more selective about which constructors we care about. |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6697 | if (const RecordType *RT = VD->getType()->getAs<RecordType>()) { |
| 6698 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 6699 | if (RD->hasDefinition() && !(RD->hasTrivialDefaultConstructor() && |
| 6700 | RD->hasTrivialCopyConstructor() && |
| 6701 | RD->hasTrivialMoveConstructor() && |
| 6702 | RD->hasTrivialDestructor())) |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6703 | return true; |
| 6704 | } |
| 6705 | } |
| 6706 | |
| 6707 | GVALinkage L = GetGVALinkageForVariable(VD); |
| 6708 | if (L == GVA_Internal || L == GVA_TemplateInstantiation) { |
| 6709 | if (!(VD->getInit() && VD->getInit()->HasSideEffects(*this))) |
| 6710 | return false; |
| 6711 | } |
| 6712 | |
| 6713 | return true; |
| 6714 | } |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 6715 | |
Charles Davis | ee743f9 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 6716 | CallingConv ASTContext::getDefaultMethodCallConv() { |
| 6717 | // Pass through to the C++ ABI object |
| 6718 | return ABI->getDefaultMethodCallConv(); |
| 6719 | } |
| 6720 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6721 | bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const { |
Anders Carlsson | dae0cb5 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 6722 | // Pass through to the C++ ABI object |
| 6723 | return ABI->isNearlyEmpty(RD); |
| 6724 | } |
| 6725 | |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 6726 | MangleContext *ASTContext::createMangleContext() { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 6727 | switch (Target->getCXXABI()) { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 6728 | case CXXABI_ARM: |
| 6729 | case CXXABI_Itanium: |
| 6730 | return createItaniumMangleContext(*this, getDiagnostics()); |
| 6731 | case CXXABI_Microsoft: |
| 6732 | return createMicrosoftMangleContext(*this, getDiagnostics()); |
| 6733 | } |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6734 | llvm_unreachable("Unsupported ABI"); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 6735 | } |
| 6736 | |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 6737 | CXXABI::~CXXABI() {} |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 6738 | |
| 6739 | size_t ASTContext::getSideTableAllocatedMemory() const { |
Ted Kremenek | 0c8cd1a | 2011-07-27 18:41:12 +0000 | [diff] [blame] | 6740 | return ASTRecordLayouts.getMemorySize() |
| 6741 | + llvm::capacity_in_bytes(ObjCLayouts) |
| 6742 | + llvm::capacity_in_bytes(KeyFunctions) |
| 6743 | + llvm::capacity_in_bytes(ObjCImpls) |
| 6744 | + llvm::capacity_in_bytes(BlockVarCopyInits) |
| 6745 | + llvm::capacity_in_bytes(DeclAttrs) |
| 6746 | + llvm::capacity_in_bytes(InstantiatedFromStaticDataMember) |
| 6747 | + llvm::capacity_in_bytes(InstantiatedFromUsingDecl) |
| 6748 | + llvm::capacity_in_bytes(InstantiatedFromUsingShadowDecl) |
| 6749 | + llvm::capacity_in_bytes(InstantiatedFromUnnamedFieldDecl) |
| 6750 | + llvm::capacity_in_bytes(OverriddenMethods) |
| 6751 | + llvm::capacity_in_bytes(Types) |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 6752 | + llvm::capacity_in_bytes(VariableArrayTypes) |
Francois Pichet | 0d95f0d | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 6753 | + llvm::capacity_in_bytes(ClassScopeSpecializationPattern); |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 6754 | } |
Ted Kremenek | d211cb7 | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 6755 | |
Douglas Gregor | 9e8c92a | 2012-02-20 19:44:39 +0000 | [diff] [blame] | 6756 | unsigned ASTContext::getLambdaManglingNumber(CXXMethodDecl *CallOperator) { |
| 6757 | CXXRecordDecl *Lambda = CallOperator->getParent(); |
| 6758 | return LambdaMangleContexts[Lambda->getDeclContext()] |
| 6759 | .getManglingNumber(CallOperator); |
| 6760 | } |
| 6761 | |
| 6762 | |
Ted Kremenek | d211cb7 | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 6763 | void ASTContext::setParameterIndex(const ParmVarDecl *D, unsigned int index) { |
| 6764 | ParamIndices[D] = index; |
| 6765 | } |
| 6766 | |
| 6767 | unsigned ASTContext::getParameterIndex(const ParmVarDecl *D) const { |
| 6768 | ParameterIndexTable::const_iterator I = ParamIndices.find(D); |
| 6769 | assert(I != ParamIndices.end() && |
| 6770 | "ParmIndices lacks entry set by ParmVarDecl"); |
| 6771 | return I->second; |
| 6772 | } |