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" |
Anders Carlsson | 19cc4ab | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 23 | #include "clang/AST/RecordLayout.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 24 | #include "clang/Basic/Builtins.h" |
Chris Lattner | a9376d4 | 2009-03-28 03:45:20 +0000 | [diff] [blame] | 25 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 26 | #include "clang/Basic/TargetInfo.h" |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SmallString.h" |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/StringExtras.h" |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 29 | #include "llvm/Support/MathExtras.h" |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 30 | #include "llvm/Support/raw_ostream.h" |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 31 | #include "CXXABI.h" |
Anders Carlsson | 29445a0 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 32 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 33 | using namespace clang; |
| 34 | |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 35 | unsigned ASTContext::NumImplicitDefaultConstructors; |
| 36 | unsigned ASTContext::NumImplicitDefaultConstructorsDeclared; |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 37 | unsigned ASTContext::NumImplicitCopyConstructors; |
| 38 | unsigned ASTContext::NumImplicitCopyConstructorsDeclared; |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 39 | unsigned ASTContext::NumImplicitCopyAssignmentOperators; |
| 40 | unsigned ASTContext::NumImplicitCopyAssignmentOperatorsDeclared; |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 41 | unsigned ASTContext::NumImplicitDestructors; |
| 42 | unsigned ASTContext::NumImplicitDestructorsDeclared; |
| 43 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 44 | enum FloatingRank { |
| 45 | FloatRank, DoubleRank, LongDoubleRank |
| 46 | }; |
| 47 | |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 48 | void |
| 49 | ASTContext::CanonicalTemplateTemplateParm::Profile(llvm::FoldingSetNodeID &ID, |
| 50 | TemplateTemplateParmDecl *Parm) { |
| 51 | ID.AddInteger(Parm->getDepth()); |
| 52 | ID.AddInteger(Parm->getPosition()); |
| 53 | // FIXME: Parameter pack |
| 54 | |
| 55 | TemplateParameterList *Params = Parm->getTemplateParameters(); |
| 56 | ID.AddInteger(Params->size()); |
| 57 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 58 | PEnd = Params->end(); |
| 59 | P != PEnd; ++P) { |
| 60 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
| 61 | ID.AddInteger(0); |
| 62 | ID.AddBoolean(TTP->isParameterPack()); |
| 63 | continue; |
| 64 | } |
| 65 | |
| 66 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 67 | ID.AddInteger(1); |
| 68 | // FIXME: Parameter pack |
| 69 | ID.AddPointer(NTTP->getType().getAsOpaquePtr()); |
| 70 | continue; |
| 71 | } |
| 72 | |
| 73 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); |
| 74 | ID.AddInteger(2); |
| 75 | Profile(ID, TTP); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | TemplateTemplateParmDecl * |
| 80 | ASTContext::getCanonicalTemplateTemplateParmDecl( |
| 81 | TemplateTemplateParmDecl *TTP) { |
| 82 | // Check if we already have a canonical template template parameter. |
| 83 | llvm::FoldingSetNodeID ID; |
| 84 | CanonicalTemplateTemplateParm::Profile(ID, TTP); |
| 85 | void *InsertPos = 0; |
| 86 | CanonicalTemplateTemplateParm *Canonical |
| 87 | = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
| 88 | if (Canonical) |
| 89 | return Canonical->getParam(); |
| 90 | |
| 91 | // Build a canonical template parameter list. |
| 92 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 93 | llvm::SmallVector<NamedDecl *, 4> CanonParams; |
| 94 | CanonParams.reserve(Params->size()); |
| 95 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 96 | PEnd = Params->end(); |
| 97 | P != PEnd; ++P) { |
| 98 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) |
| 99 | CanonParams.push_back( |
| 100 | TemplateTypeParmDecl::Create(*this, getTranslationUnitDecl(), |
| 101 | SourceLocation(), TTP->getDepth(), |
| 102 | TTP->getIndex(), 0, false, |
| 103 | TTP->isParameterPack())); |
| 104 | else if (NonTypeTemplateParmDecl *NTTP |
| 105 | = dyn_cast<NonTypeTemplateParmDecl>(*P)) |
| 106 | CanonParams.push_back( |
| 107 | NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
| 108 | SourceLocation(), NTTP->getDepth(), |
| 109 | NTTP->getPosition(), 0, |
| 110 | getCanonicalType(NTTP->getType()), |
| 111 | 0)); |
| 112 | else |
| 113 | CanonParams.push_back(getCanonicalTemplateTemplateParmDecl( |
| 114 | cast<TemplateTemplateParmDecl>(*P))); |
| 115 | } |
| 116 | |
| 117 | TemplateTemplateParmDecl *CanonTTP |
| 118 | = TemplateTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
| 119 | SourceLocation(), TTP->getDepth(), |
| 120 | TTP->getPosition(), 0, |
| 121 | TemplateParameterList::Create(*this, SourceLocation(), |
| 122 | SourceLocation(), |
| 123 | CanonParams.data(), |
| 124 | CanonParams.size(), |
| 125 | SourceLocation())); |
| 126 | |
| 127 | // Get the new insert position for the node we care about. |
| 128 | Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
| 129 | assert(Canonical == 0 && "Shouldn't be in the map!"); |
| 130 | (void)Canonical; |
| 131 | |
| 132 | // Create the canonical template template parameter entry. |
| 133 | Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP); |
| 134 | CanonTemplateTemplateParms.InsertNode(Canonical, InsertPos); |
| 135 | return CanonTTP; |
| 136 | } |
| 137 | |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 138 | CXXABI *ASTContext::createCXXABI(const TargetInfo &T) { |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 139 | if (!LangOpts.CPlusPlus) return 0; |
| 140 | |
Charles Davis | 20cf717 | 2010-08-19 02:18:14 +0000 | [diff] [blame] | 141 | switch (T.getCXXABI()) { |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 142 | case CXXABI_ARM: |
| 143 | return CreateARMCXXABI(*this); |
| 144 | case CXXABI_Itanium: |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 145 | return CreateItaniumCXXABI(*this); |
Charles Davis | 20cf717 | 2010-08-19 02:18:14 +0000 | [diff] [blame] | 146 | case CXXABI_Microsoft: |
| 147 | return CreateMicrosoftCXXABI(*this); |
| 148 | } |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 149 | return 0; |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 152 | ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM, |
Daniel Dunbar | 444be73 | 2009-11-13 05:51:54 +0000 | [diff] [blame] | 153 | const TargetInfo &t, |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 154 | IdentifierTable &idents, SelectorTable &sels, |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 155 | Builtin::Context &builtins, |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 156 | unsigned size_reserve) : |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 157 | TemplateSpecializationTypes(this_()), |
| 158 | DependentTemplateSpecializationTypes(this_()), |
Argyrios Kyrtzidis | 0061138 | 2010-07-04 21:44:19 +0000 | [diff] [blame] | 159 | GlobalNestedNameSpecifier(0), IsInt128Installed(false), |
| 160 | CFConstantStringTypeDecl(0), NSConstantStringTypeDecl(0), |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 161 | ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0), |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 162 | sigjmp_bufDecl(0), BlockDescriptorType(0), BlockDescriptorExtendedType(0), |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 163 | NullTypeSourceInfo(QualType()), |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 164 | SourceMgr(SM), LangOpts(LOpts), ABI(createCXXABI(t)), Target(t), |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 165 | Idents(idents), Selectors(sels), |
Ted Kremenek | ac9590e | 2010-05-10 20:40:08 +0000 | [diff] [blame] | 166 | BuiltinInfo(builtins), |
| 167 | DeclarationNames(*this), |
| 168 | ExternalSource(0), PrintingPolicy(LOpts), |
Ted Kremenek | f057bf7 | 2010-06-18 00:31:04 +0000 | [diff] [blame] | 169 | LastSDM(0, 0), |
| 170 | UniqueBlockByRefTypeID(0), UniqueBlockParmTypeID(0) { |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 171 | ObjCIdRedefinitionType = QualType(); |
| 172 | ObjCClassRedefinitionType = QualType(); |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 173 | ObjCSelRedefinitionType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 174 | if (size_reserve > 0) Types.reserve(size_reserve); |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 175 | TUDecl = TranslationUnitDecl::Create(*this); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 176 | InitBuiltinTypes(); |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 179 | ASTContext::~ASTContext() { |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 180 | // Release the DenseMaps associated with DeclContext objects. |
| 181 | // FIXME: Is this the ideal solution? |
| 182 | ReleaseDeclContextMaps(); |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 183 | |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 184 | // Call all of the deallocation functions. |
| 185 | for (unsigned I = 0, N = Deallocations.size(); I != N; ++I) |
| 186 | Deallocations[I].first(Deallocations[I].second); |
Douglas Gregor | 0054531 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 187 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 188 | // Release all of the memory associated with overridden C++ methods. |
| 189 | for (llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::iterator |
| 190 | OM = OverriddenMethods.begin(), OMEnd = OverriddenMethods.end(); |
| 191 | OM != OMEnd; ++OM) |
| 192 | OM->second.Destroy(); |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 193 | |
Ted Kremenek | dcfcfbe | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 194 | // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 195 | // because they can contain DenseMaps. |
| 196 | for (llvm::DenseMap<const ObjCContainerDecl*, |
| 197 | const ASTRecordLayout*>::iterator |
| 198 | I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) |
| 199 | // Increment in loop to prevent using deallocated memory. |
| 200 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 201 | R->Destroy(*this); |
| 202 | |
Ted Kremenek | dcfcfbe | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 203 | for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator |
| 204 | I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) { |
| 205 | // Increment in loop to prevent using deallocated memory. |
| 206 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 207 | R->Destroy(*this); |
| 208 | } |
Ted Kremenek | bbfd68d | 2009-12-23 21:13:52 +0000 | [diff] [blame] | 209 | } |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 210 | |
Douglas Gregor | 0054531 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 211 | void ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) { |
| 212 | Deallocations.push_back(std::make_pair(Callback, Data)); |
| 213 | } |
| 214 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 215 | void |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 216 | ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) { |
| 217 | ExternalSource.reset(Source.take()); |
| 218 | } |
| 219 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 220 | void ASTContext::PrintStats() const { |
| 221 | fprintf(stderr, "*** AST Context Stats:\n"); |
| 222 | fprintf(stderr, " %d types total.\n", (int)Types.size()); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 223 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 224 | unsigned counts[] = { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 225 | #define TYPE(Name, Parent) 0, |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 226 | #define ABSTRACT_TYPE(Name, Parent) |
| 227 | #include "clang/AST/TypeNodes.def" |
| 228 | 0 // Extra |
| 229 | }; |
Douglas Gregor | c2ee10d | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 230 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 231 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 232 | Type *T = Types[i]; |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 233 | counts[(unsigned)T->getTypeClass()]++; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 236 | unsigned Idx = 0; |
| 237 | unsigned TotalBytes = 0; |
| 238 | #define TYPE(Name, Parent) \ |
| 239 | if (counts[Idx]) \ |
| 240 | fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \ |
| 241 | TotalBytes += counts[Idx] * sizeof(Name##Type); \ |
| 242 | ++Idx; |
| 243 | #define ABSTRACT_TYPE(Name, Parent) |
| 244 | #include "clang/AST/TypeNodes.def" |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 245 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 246 | fprintf(stderr, "Total bytes = %d\n", int(TotalBytes)); |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 247 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 248 | // Implicit special member functions. |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 249 | fprintf(stderr, " %u/%u implicit default constructors created\n", |
| 250 | NumImplicitDefaultConstructorsDeclared, |
| 251 | NumImplicitDefaultConstructors); |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 252 | fprintf(stderr, " %u/%u implicit copy constructors created\n", |
| 253 | NumImplicitCopyConstructorsDeclared, |
| 254 | NumImplicitCopyConstructors); |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 255 | fprintf(stderr, " %u/%u implicit copy assignment operators created\n", |
| 256 | NumImplicitCopyAssignmentOperatorsDeclared, |
| 257 | NumImplicitCopyAssignmentOperators); |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 258 | fprintf(stderr, " %u/%u implicit destructors created\n", |
| 259 | NumImplicitDestructorsDeclared, NumImplicitDestructors); |
| 260 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 261 | if (ExternalSource.get()) { |
| 262 | fprintf(stderr, "\n"); |
| 263 | ExternalSource->PrintStats(); |
| 264 | } |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 265 | |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 266 | BumpAlloc.PrintStats(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 270 | void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 271 | BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 272 | R = CanQualType::CreateUnsafe(QualType(Ty, 0)); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 273 | Types.push_back(Ty); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 276 | void ASTContext::InitBuiltinTypes() { |
| 277 | assert(VoidTy.isNull() && "Context reinitialized?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 278 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 279 | // C99 6.2.5p19. |
| 280 | InitBuiltinType(VoidTy, BuiltinType::Void); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 281 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 282 | // C99 6.2.5p2. |
| 283 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
| 284 | // C99 6.2.5p3. |
Eli Friedman | 15b9176 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 285 | if (LangOpts.CharIsSigned) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 286 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 287 | else |
| 288 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
| 289 | // C99 6.2.5p4. |
| 290 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 291 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 292 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 293 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 294 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 295 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 296 | // C99 6.2.5p6. |
| 297 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 298 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 299 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 300 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 301 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 302 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 303 | // C99 6.2.5p10. |
| 304 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 305 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 306 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 307 | |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 308 | // GNU extension, 128-bit integers. |
| 309 | InitBuiltinType(Int128Ty, BuiltinType::Int128); |
| 310 | InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128); |
| 311 | |
Chris Lattner | 3a25032 | 2009-02-26 23:43:47 +0000 | [diff] [blame] | 312 | if (LangOpts.CPlusPlus) // C++ 3.9.1p5 |
| 313 | InitBuiltinType(WCharTy, BuiltinType::WChar); |
| 314 | else // C99 |
| 315 | WCharTy = getFromTargetType(Target.getWCharType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 316 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 317 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 318 | InitBuiltinType(Char16Ty, BuiltinType::Char16); |
| 319 | else // C99 |
| 320 | Char16Ty = getFromTargetType(Target.getChar16Type()); |
| 321 | |
| 322 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 323 | InitBuiltinType(Char32Ty, BuiltinType::Char32); |
| 324 | else // C99 |
| 325 | Char32Ty = getFromTargetType(Target.getChar32Type()); |
| 326 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 327 | // Placeholder type for functions. |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 328 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
| 329 | |
| 330 | // Placeholder type for type-dependent expressions whose type is |
| 331 | // completely unknown. No code should ever check a type against |
| 332 | // DependentTy and users should never see it; however, it is here to |
| 333 | // help diagnose failures to properly check for type-dependent |
| 334 | // expressions. |
| 335 | InitBuiltinType(DependentTy, BuiltinType::Dependent); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 336 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 337 | // Placeholder type for C++0x auto declarations whose real type has |
Anders Carlsson | e89d159 | 2009-06-26 18:41:36 +0000 | [diff] [blame] | 338 | // not yet been deduced. |
| 339 | InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 340 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 341 | // C99 6.2.5p11. |
| 342 | FloatComplexTy = getComplexType(FloatTy); |
| 343 | DoubleComplexTy = getComplexType(DoubleTy); |
| 344 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 345 | |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 346 | BuiltinVaListType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 347 | |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 348 | // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope(). |
| 349 | ObjCIdTypedefType = QualType(); |
| 350 | ObjCClassTypedefType = QualType(); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 351 | ObjCSelTypedefType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 352 | |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 353 | // Builtin types for 'id', 'Class', and 'SEL'. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 354 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); |
| 355 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 356 | InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 357 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 358 | ObjCConstantStringType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 359 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 360 | // void * type |
| 361 | VoidPtrTy = getPointerType(VoidTy); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 362 | |
| 363 | // nullptr type (C++0x 2.14.7) |
| 364 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 367 | MemberSpecializationInfo * |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 368 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 369 | assert(Var->isStaticDataMember() && "Not a static data member"); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 370 | llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 371 | = InstantiatedFromStaticDataMember.find(Var); |
| 372 | if (Pos == InstantiatedFromStaticDataMember.end()) |
| 373 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 374 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 375 | return Pos->second; |
| 376 | } |
| 377 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 378 | void |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 379 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, |
Argyrios Kyrtzidis | 9421adc | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 380 | TemplateSpecializationKind TSK, |
| 381 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 382 | assert(Inst->isStaticDataMember() && "Not a static data member"); |
| 383 | assert(Tmpl->isStaticDataMember() && "Not a static data member"); |
| 384 | assert(!InstantiatedFromStaticDataMember[Inst] && |
| 385 | "Already noted what static data member was instantiated from"); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 386 | InstantiatedFromStaticDataMember[Inst] |
Argyrios Kyrtzidis | 9421adc | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 387 | = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 388 | } |
| 389 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 390 | NamedDecl * |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 391 | ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 392 | llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 393 | = InstantiatedFromUsingDecl.find(UUD); |
| 394 | if (Pos == InstantiatedFromUsingDecl.end()) |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 395 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 396 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 397 | return Pos->second; |
| 398 | } |
| 399 | |
| 400 | void |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 401 | ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) { |
| 402 | assert((isa<UsingDecl>(Pattern) || |
| 403 | isa<UnresolvedUsingValueDecl>(Pattern) || |
| 404 | isa<UnresolvedUsingTypenameDecl>(Pattern)) && |
| 405 | "pattern decl is not a using decl"); |
| 406 | assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists"); |
| 407 | InstantiatedFromUsingDecl[Inst] = Pattern; |
| 408 | } |
| 409 | |
| 410 | UsingShadowDecl * |
| 411 | ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) { |
| 412 | llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos |
| 413 | = InstantiatedFromUsingShadowDecl.find(Inst); |
| 414 | if (Pos == InstantiatedFromUsingShadowDecl.end()) |
| 415 | return 0; |
| 416 | |
| 417 | return Pos->second; |
| 418 | } |
| 419 | |
| 420 | void |
| 421 | ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, |
| 422 | UsingShadowDecl *Pattern) { |
| 423 | assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists"); |
| 424 | InstantiatedFromUsingShadowDecl[Inst] = Pattern; |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 427 | FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { |
| 428 | llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos |
| 429 | = InstantiatedFromUnnamedFieldDecl.find(Field); |
| 430 | if (Pos == InstantiatedFromUnnamedFieldDecl.end()) |
| 431 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 432 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 433 | return Pos->second; |
| 434 | } |
| 435 | |
| 436 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, |
| 437 | FieldDecl *Tmpl) { |
| 438 | assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed"); |
| 439 | assert(!Tmpl->getDeclName() && "Template field decl is not unnamed"); |
| 440 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] && |
| 441 | "Already noted what unnamed field was instantiated from"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 442 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 443 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; |
| 444 | } |
| 445 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 446 | ASTContext::overridden_cxx_method_iterator |
| 447 | ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const { |
| 448 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 449 | = OverriddenMethods.find(Method); |
| 450 | if (Pos == OverriddenMethods.end()) |
| 451 | return 0; |
| 452 | |
| 453 | return Pos->second.begin(); |
| 454 | } |
| 455 | |
| 456 | ASTContext::overridden_cxx_method_iterator |
| 457 | ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const { |
| 458 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 459 | = OverriddenMethods.find(Method); |
| 460 | if (Pos == OverriddenMethods.end()) |
| 461 | return 0; |
| 462 | |
| 463 | return Pos->second.end(); |
| 464 | } |
| 465 | |
Argyrios Kyrtzidis | c91e9f4 | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 466 | unsigned |
| 467 | ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const { |
| 468 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 469 | = OverriddenMethods.find(Method); |
| 470 | if (Pos == OverriddenMethods.end()) |
| 471 | return 0; |
| 472 | |
| 473 | return Pos->second.size(); |
| 474 | } |
| 475 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 476 | void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method, |
| 477 | const CXXMethodDecl *Overridden) { |
| 478 | OverriddenMethods[Method].push_back(Overridden); |
| 479 | } |
| 480 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 481 | //===----------------------------------------------------------------------===// |
| 482 | // Type Sizing and Analysis |
| 483 | //===----------------------------------------------------------------------===// |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 484 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 485 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 486 | /// scalar floating point type. |
| 487 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 488 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 489 | assert(BT && "Not a floating point type!"); |
| 490 | switch (BT->getKind()) { |
| 491 | default: assert(0 && "Not a floating point type!"); |
| 492 | case BuiltinType::Float: return Target.getFloatFormat(); |
| 493 | case BuiltinType::Double: return Target.getDoubleFormat(); |
| 494 | case BuiltinType::LongDouble: return Target.getLongDoubleFormat(); |
| 495 | } |
| 496 | } |
| 497 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 498 | /// getDeclAlign - Return a conservative estimate of the alignment of the |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 499 | /// specified decl. Note that bitfields do not have a valid alignment, so |
| 500 | /// this method will assert on them. |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 501 | /// If @p RefAsPointee, references are treated like their underlying type |
| 502 | /// (for alignof), else they're treated like pointers (for CodeGen). |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 503 | CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) { |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 504 | unsigned Align = Target.getCharWidth(); |
| 505 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 506 | Align = std::max(Align, D->getMaxAlignment()); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 507 | |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 508 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
| 509 | QualType T = VD->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 510 | if (const ReferenceType* RT = T->getAs<ReferenceType>()) { |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 511 | if (RefAsPointee) |
| 512 | T = RT->getPointeeType(); |
| 513 | else |
| 514 | T = getPointerType(RT->getPointeeType()); |
| 515 | } |
| 516 | if (!T->isIncompleteType() && !T->isFunctionType()) { |
Rafael Espindola | 6deecb0 | 2010-06-04 23:15:27 +0000 | [diff] [blame] | 517 | unsigned MinWidth = Target.getLargeArrayMinWidth(); |
| 518 | unsigned ArrayAlign = Target.getLargeArrayAlign(); |
| 519 | if (isa<VariableArrayType>(T) && MinWidth != 0) |
| 520 | Align = std::max(Align, ArrayAlign); |
| 521 | if (ConstantArrayType *CT = dyn_cast<ConstantArrayType>(T)) { |
| 522 | unsigned Size = getTypeSize(CT); |
| 523 | if (MinWidth != 0 && MinWidth <= Size) |
| 524 | Align = std::max(Align, ArrayAlign); |
| 525 | } |
Anders Carlsson | 4cc2cfd | 2009-04-10 04:47:03 +0000 | [diff] [blame] | 526 | // Incomplete or function types default to 1. |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 527 | while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T)) |
| 528 | T = cast<ArrayType>(T)->getElementType(); |
| 529 | |
| 530 | Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); |
| 531 | } |
Charles Davis | 05f6247 | 2010-02-23 04:52:00 +0000 | [diff] [blame] | 532 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) { |
| 533 | // In the case of a field in a packed struct, we want the minimum |
| 534 | // of the alignment of the field and the alignment of the struct. |
| 535 | Align = std::min(Align, |
| 536 | getPreferredTypeAlign(FD->getParent()->getTypeForDecl())); |
| 537 | } |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 538 | } |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 539 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 540 | return CharUnits::fromQuantity(Align / Target.getCharWidth()); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 541 | } |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 542 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 543 | std::pair<CharUnits, CharUnits> |
| 544 | ASTContext::getTypeInfoInChars(const Type *T) { |
| 545 | std::pair<uint64_t, unsigned> Info = getTypeInfo(T); |
| 546 | return std::make_pair(CharUnits::fromQuantity(Info.first / getCharWidth()), |
| 547 | CharUnits::fromQuantity(Info.second / getCharWidth())); |
| 548 | } |
| 549 | |
| 550 | std::pair<CharUnits, CharUnits> |
| 551 | ASTContext::getTypeInfoInChars(QualType T) { |
| 552 | return getTypeInfoInChars(T.getTypePtr()); |
| 553 | } |
| 554 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 555 | /// getTypeSize - Return the size of the specified type, in bits. This method |
| 556 | /// does not work on incomplete types. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 557 | /// |
| 558 | /// FIXME: Pointers into different addr spaces could have different sizes and |
| 559 | /// alignment requirements: getPointerInfo should take an AddrSpace, this |
| 560 | /// should take a QualType, &c. |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 561 | std::pair<uint64_t, unsigned> |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 562 | ASTContext::getTypeInfo(const Type *T) { |
Mike Stump | 5e30100 | 2009-02-27 18:32:39 +0000 | [diff] [blame] | 563 | uint64_t Width=0; |
| 564 | unsigned Align=8; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 565 | switch (T->getTypeClass()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 566 | #define TYPE(Class, Base) |
| 567 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 568 | #define NON_CANONICAL_TYPE(Class, Base) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 569 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 570 | #include "clang/AST/TypeNodes.def" |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 571 | assert(false && "Should not see dependent types"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 572 | break; |
| 573 | |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 574 | case Type::FunctionNoProto: |
| 575 | case Type::FunctionProto: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 576 | // GCC extension: alignof(function) = 32 bits |
| 577 | Width = 0; |
| 578 | Align = 32; |
| 579 | break; |
| 580 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 581 | case Type::IncompleteArray: |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 582 | case Type::VariableArray: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 583 | Width = 0; |
| 584 | Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); |
| 585 | break; |
| 586 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 587 | case Type::ConstantArray: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 588 | const ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 589 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 590 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 591 | Width = EltInfo.first*CAT->getSize().getZExtValue(); |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 592 | Align = EltInfo.second; |
| 593 | break; |
Christopher Lamb | 5c09a02 | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 594 | } |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 595 | case Type::ExtVector: |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 596 | case Type::Vector: { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 597 | const VectorType *VT = cast<VectorType>(T); |
| 598 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType()); |
| 599 | Width = EltInfo.first*VT->getNumElements(); |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 600 | Align = Width; |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 601 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 602 | // This happens for non-power-of-2 length vectors. |
Dan Gohman | 8eefcd3 | 2010-04-21 23:32:43 +0000 | [diff] [blame] | 603 | if (Align & (Align-1)) { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 604 | Align = llvm::NextPowerOf2(Align); |
| 605 | Width = llvm::RoundUpToAlignment(Width, Align); |
| 606 | } |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 607 | break; |
| 608 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 609 | |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 610 | case Type::Builtin: |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 611 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 612 | default: assert(0 && "Unknown builtin type!"); |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 613 | case BuiltinType::Void: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 614 | // GCC extension: alignof(void) = 8 bits. |
| 615 | Width = 0; |
| 616 | Align = 8; |
| 617 | break; |
| 618 | |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 619 | case BuiltinType::Bool: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 620 | Width = Target.getBoolWidth(); |
| 621 | Align = Target.getBoolAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 622 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 623 | case BuiltinType::Char_S: |
| 624 | case BuiltinType::Char_U: |
| 625 | case BuiltinType::UChar: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 626 | case BuiltinType::SChar: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 627 | Width = Target.getCharWidth(); |
| 628 | Align = Target.getCharAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 629 | break; |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 630 | case BuiltinType::WChar: |
| 631 | Width = Target.getWCharWidth(); |
| 632 | Align = Target.getWCharAlign(); |
| 633 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 634 | case BuiltinType::Char16: |
| 635 | Width = Target.getChar16Width(); |
| 636 | Align = Target.getChar16Align(); |
| 637 | break; |
| 638 | case BuiltinType::Char32: |
| 639 | Width = Target.getChar32Width(); |
| 640 | Align = Target.getChar32Align(); |
| 641 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 642 | case BuiltinType::UShort: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 643 | case BuiltinType::Short: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 644 | Width = Target.getShortWidth(); |
| 645 | Align = Target.getShortAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 646 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 647 | case BuiltinType::UInt: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 648 | case BuiltinType::Int: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 649 | Width = Target.getIntWidth(); |
| 650 | Align = Target.getIntAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 651 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 652 | case BuiltinType::ULong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 653 | case BuiltinType::Long: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 654 | Width = Target.getLongWidth(); |
| 655 | Align = Target.getLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 656 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 657 | case BuiltinType::ULongLong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 658 | case BuiltinType::LongLong: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 659 | Width = Target.getLongLongWidth(); |
| 660 | Align = Target.getLongLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 661 | break; |
Chris Lattner | ec16cb9 | 2009-04-30 02:55:13 +0000 | [diff] [blame] | 662 | case BuiltinType::Int128: |
| 663 | case BuiltinType::UInt128: |
| 664 | Width = 128; |
| 665 | Align = 128; // int128_t is 128-bit aligned on all targets. |
| 666 | break; |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 667 | case BuiltinType::Float: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 668 | Width = Target.getFloatWidth(); |
| 669 | Align = Target.getFloatAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 670 | break; |
| 671 | case BuiltinType::Double: |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 672 | Width = Target.getDoubleWidth(); |
| 673 | Align = Target.getDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 674 | break; |
| 675 | case BuiltinType::LongDouble: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 676 | Width = Target.getLongDoubleWidth(); |
| 677 | Align = Target.getLongDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 678 | break; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 679 | case BuiltinType::NullPtr: |
| 680 | Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) |
| 681 | Align = Target.getPointerAlign(0); // == sizeof(void*) |
Sebastian Redl | 1590d9c | 2009-05-27 19:34:06 +0000 | [diff] [blame] | 682 | break; |
Fariborz Jahanian | e04f5fc | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 683 | case BuiltinType::ObjCId: |
| 684 | case BuiltinType::ObjCClass: |
| 685 | case BuiltinType::ObjCSel: |
| 686 | Width = Target.getPointerWidth(0); |
| 687 | Align = Target.getPointerAlign(0); |
| 688 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 689 | } |
Chris Lattner | bfef6d7 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 690 | break; |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 691 | case Type::ObjCObjectPointer: |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 692 | Width = Target.getPointerWidth(0); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 693 | Align = Target.getPointerAlign(0); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 694 | break; |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 695 | case Type::BlockPointer: { |
| 696 | unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace(); |
| 697 | Width = Target.getPointerWidth(AS); |
| 698 | Align = Target.getPointerAlign(AS); |
| 699 | break; |
| 700 | } |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 701 | case Type::LValueReference: |
| 702 | case Type::RValueReference: { |
| 703 | // alignof and sizeof should never enter this code path here, so we go |
| 704 | // the pointer route. |
| 705 | unsigned AS = cast<ReferenceType>(T)->getPointeeType().getAddressSpace(); |
| 706 | Width = Target.getPointerWidth(AS); |
| 707 | Align = Target.getPointerAlign(AS); |
| 708 | break; |
| 709 | } |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 710 | case Type::Pointer: { |
| 711 | unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace(); |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 712 | Width = Target.getPointerWidth(AS); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 713 | Align = Target.getPointerAlign(AS); |
| 714 | break; |
| 715 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 716 | case Type::MemberPointer: { |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 717 | const MemberPointerType *MPT = cast<MemberPointerType>(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 718 | std::pair<uint64_t, unsigned> PtrDiffInfo = |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 719 | getTypeInfo(getPointerDiffType()); |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 720 | Width = PtrDiffInfo.first * ABI->getMemberPointerSize(MPT); |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 721 | Align = PtrDiffInfo.second; |
| 722 | break; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 723 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 724 | case Type::Complex: { |
| 725 | // Complex types have the same alignment as their elements, but twice the |
| 726 | // size. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 727 | std::pair<uint64_t, unsigned> EltInfo = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 728 | getTypeInfo(cast<ComplexType>(T)->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 729 | Width = EltInfo.first*2; |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 730 | Align = EltInfo.second; |
| 731 | break; |
| 732 | } |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 733 | case Type::ObjCObject: |
| 734 | return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr()); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 735 | case Type::ObjCInterface: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 736 | const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 737 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
| 738 | Width = Layout.getSize(); |
| 739 | Align = Layout.getAlignment(); |
| 740 | break; |
| 741 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 742 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 743 | case Type::Enum: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 744 | const TagType *TT = cast<TagType>(T); |
| 745 | |
| 746 | if (TT->getDecl()->isInvalidDecl()) { |
Chris Lattner | 8389eab | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 747 | Width = 1; |
| 748 | Align = 1; |
| 749 | break; |
| 750 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 751 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 752 | if (const EnumType *ET = dyn_cast<EnumType>(TT)) |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 753 | return getTypeInfo(ET->getDecl()->getIntegerType()); |
| 754 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 755 | const RecordType *RT = cast<RecordType>(TT); |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 756 | const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); |
| 757 | Width = Layout.getSize(); |
| 758 | Align = Layout.getAlignment(); |
Chris Lattner | dc0d73e | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 759 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 760 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 761 | |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 762 | case Type::SubstTemplateTypeParm: |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 763 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> |
| 764 | getReplacementType().getTypePtr()); |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 765 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 766 | case Type::Typedef: { |
| 767 | const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl(); |
Douglas Gregor | df1367a | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 768 | std::pair<uint64_t, unsigned> Info |
| 769 | = getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); |
| 770 | Align = std::max(Typedef->getMaxAlignment(), Info.second); |
| 771 | Width = Info.first; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 772 | break; |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 773 | } |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 774 | |
| 775 | case Type::TypeOfExpr: |
| 776 | return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType() |
| 777 | .getTypePtr()); |
| 778 | |
| 779 | case Type::TypeOf: |
| 780 | return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr()); |
| 781 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 782 | case Type::Decltype: |
| 783 | return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType() |
| 784 | .getTypePtr()); |
| 785 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 786 | case Type::Elaborated: |
| 787 | return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 788 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 789 | case Type::TemplateSpecialization: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 790 | assert(getCanonicalType(T) != T && |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 791 | "Cannot request the size of a dependent type"); |
| 792 | // FIXME: this is likely to be wrong once we support template |
| 793 | // aliases, since a template alias could refer to a typedef that |
| 794 | // has an __aligned__ attribute on it. |
| 795 | return getTypeInfo(getCanonicalType(T)); |
| 796 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 797 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 798 | assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2"); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 799 | return std::make_pair(Width, Align); |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 800 | } |
| 801 | |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 802 | /// getTypeSizeInChars - Return the size of the specified type, in characters. |
| 803 | /// This method does not work on incomplete types. |
| 804 | CharUnits ASTContext::getTypeSizeInChars(QualType T) { |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 805 | return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth()); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 806 | } |
| 807 | CharUnits ASTContext::getTypeSizeInChars(const Type *T) { |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 808 | return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth()); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 809 | } |
| 810 | |
Ken Dyck | 16e20cc | 2010-01-26 17:25:18 +0000 | [diff] [blame] | 811 | /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 812 | /// characters. This method does not work on incomplete types. |
| 813 | CharUnits ASTContext::getTypeAlignInChars(QualType T) { |
| 814 | return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth()); |
| 815 | } |
| 816 | CharUnits ASTContext::getTypeAlignInChars(const Type *T) { |
| 817 | return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth()); |
| 818 | } |
| 819 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 820 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 821 | /// type for the current target in bits. This can be different than the ABI |
| 822 | /// alignment in cases where it is beneficial for performance to overalign |
| 823 | /// a data type. |
| 824 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) { |
| 825 | unsigned ABIAlign = getTypeAlign(T); |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 826 | |
| 827 | // Double and long long should be naturally aligned if possible. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 828 | if (const ComplexType* CT = T->getAs<ComplexType>()) |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 829 | T = CT->getElementType().getTypePtr(); |
| 830 | if (T->isSpecificBuiltinType(BuiltinType::Double) || |
| 831 | T->isSpecificBuiltinType(BuiltinType::LongLong)) |
| 832 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); |
| 833 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 834 | return ABIAlign; |
| 835 | } |
| 836 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 837 | /// ShallowCollectObjCIvars - |
| 838 | /// Collect all ivars, including those synthesized, in the current class. |
| 839 | /// |
| 840 | void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI, |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 841 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) { |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 842 | // FIXME. This need be removed but there are two many places which |
| 843 | // assume const-ness of ObjCInterfaceDecl |
| 844 | ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI); |
| 845 | for (ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv; |
| 846 | Iv= Iv->getNextIvar()) |
| 847 | Ivars.push_back(Iv); |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 850 | /// DeepCollectObjCIvars - |
| 851 | /// This routine first collects all declared, but not synthesized, ivars in |
| 852 | /// super class and then collects all ivars, including those synthesized for |
| 853 | /// current class. This routine is used for implementation of current class |
| 854 | /// when all ivars, declared and synthesized are known. |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 855 | /// |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 856 | void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 857 | bool leafClass, |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 858 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) { |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 859 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
| 860 | DeepCollectObjCIvars(SuperClass, false, Ivars); |
| 861 | if (!leafClass) { |
| 862 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 863 | E = OI->ivar_end(); I != E; ++I) |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 864 | Ivars.push_back(*I); |
| 865 | } |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 866 | else |
| 867 | ShallowCollectObjCIvars(OI, Ivars); |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 868 | } |
| 869 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 870 | /// CollectInheritedProtocols - Collect all protocols in current class and |
| 871 | /// those inherited by it. |
| 872 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 873 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 874 | if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
| 875 | for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(), |
| 876 | PE = OI->protocol_end(); P != PE; ++P) { |
| 877 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 878 | Protocols.insert(Proto); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 879 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 880 | PE = Proto->protocol_end(); P != PE; ++P) { |
| 881 | Protocols.insert(*P); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 882 | CollectInheritedProtocols(*P, Protocols); |
| 883 | } |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 884 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 885 | |
| 886 | // Categories of this Interface. |
| 887 | for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList(); |
| 888 | CDeclChain; CDeclChain = CDeclChain->getNextClassCategory()) |
| 889 | CollectInheritedProtocols(CDeclChain, Protocols); |
| 890 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) |
| 891 | while (SD) { |
| 892 | CollectInheritedProtocols(SD, Protocols); |
| 893 | SD = SD->getSuperClass(); |
| 894 | } |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 895 | } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 896 | for (ObjCInterfaceDecl::protocol_iterator P = OC->protocol_begin(), |
| 897 | PE = OC->protocol_end(); P != PE; ++P) { |
| 898 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 899 | Protocols.insert(Proto); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 900 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 901 | PE = Proto->protocol_end(); P != PE; ++P) |
| 902 | CollectInheritedProtocols(*P, Protocols); |
| 903 | } |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 904 | } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 905 | for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(), |
| 906 | PE = OP->protocol_end(); P != PE; ++P) { |
| 907 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 908 | Protocols.insert(Proto); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 909 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 910 | PE = Proto->protocol_end(); P != PE; ++P) |
| 911 | CollectInheritedProtocols(*P, Protocols); |
| 912 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 913 | } |
| 914 | } |
| 915 | |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 916 | unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) { |
| 917 | unsigned count = 0; |
| 918 | // Count ivars declared in class extension. |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 919 | for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl; |
| 920 | CDecl = CDecl->getNextClassExtension()) |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 921 | count += CDecl->ivar_size(); |
| 922 | |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 923 | // Count ivar defined in this class's implementation. This |
| 924 | // includes synthesized ivars. |
| 925 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 926 | count += ImplDecl->ivar_size(); |
| 927 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 928 | return count; |
| 929 | } |
| 930 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 931 | /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. |
| 932 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
| 933 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 934 | I = ObjCImpls.find(D); |
| 935 | if (I != ObjCImpls.end()) |
| 936 | return cast<ObjCImplementationDecl>(I->second); |
| 937 | return 0; |
| 938 | } |
| 939 | /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. |
| 940 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
| 941 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 942 | I = ObjCImpls.find(D); |
| 943 | if (I != ObjCImpls.end()) |
| 944 | return cast<ObjCCategoryImplDecl>(I->second); |
| 945 | return 0; |
| 946 | } |
| 947 | |
| 948 | /// \brief Set the implementation of ObjCInterfaceDecl. |
| 949 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
| 950 | ObjCImplementationDecl *ImplD) { |
| 951 | assert(IFaceD && ImplD && "Passed null params"); |
| 952 | ObjCImpls[IFaceD] = ImplD; |
| 953 | } |
| 954 | /// \brief Set the implementation of ObjCCategoryDecl. |
| 955 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
| 956 | ObjCCategoryImplDecl *ImplD) { |
| 957 | assert(CatD && ImplD && "Passed null params"); |
| 958 | ObjCImpls[CatD] = ImplD; |
| 959 | } |
| 960 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 961 | /// \brief Allocate an uninitialized TypeSourceInfo. |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 962 | /// |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 963 | /// The caller should initialize the memory held by TypeSourceInfo using |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 964 | /// the TypeLoc wrappers. |
| 965 | /// |
| 966 | /// \param T the type that will be the basis for type source info. This type |
| 967 | /// should refer to how the declarator was written in source code, not to |
| 968 | /// what type semantic analysis resolved the declarator to. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 969 | TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T, |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 970 | unsigned DataSize) { |
| 971 | if (!DataSize) |
| 972 | DataSize = TypeLoc::getFullDataSizeForType(T); |
| 973 | else |
| 974 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 975 | "incorrect data size provided to CreateTypeSourceInfo!"); |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 976 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 977 | TypeSourceInfo *TInfo = |
| 978 | (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8); |
| 979 | new (TInfo) TypeSourceInfo(T); |
| 980 | return TInfo; |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 981 | } |
| 982 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 983 | TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, |
John McCall | a4eb74d | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 984 | SourceLocation L) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 985 | TypeSourceInfo *DI = CreateTypeSourceInfo(T); |
John McCall | a4eb74d | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 986 | DI->getTypeLoc().initialize(L); |
| 987 | return DI; |
| 988 | } |
| 989 | |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 990 | const ASTRecordLayout & |
| 991 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) { |
| 992 | return getObjCLayout(D, 0); |
| 993 | } |
| 994 | |
| 995 | const ASTRecordLayout & |
| 996 | ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) { |
| 997 | return getObjCLayout(D->getClassInterface(), D); |
| 998 | } |
| 999 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1000 | //===----------------------------------------------------------------------===// |
| 1001 | // Type creation/memoization methods |
| 1002 | //===----------------------------------------------------------------------===// |
| 1003 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1004 | QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) { |
| 1005 | unsigned Fast = Quals.getFastQualifiers(); |
| 1006 | Quals.removeFastQualifiers(); |
| 1007 | |
| 1008 | // Check if we've already instantiated this type. |
| 1009 | llvm::FoldingSetNodeID ID; |
| 1010 | ExtQuals::Profile(ID, TypeNode, Quals); |
| 1011 | void *InsertPos = 0; |
| 1012 | if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) { |
| 1013 | assert(EQ->getQualifiers() == Quals); |
| 1014 | QualType T = QualType(EQ, Fast); |
| 1015 | return T; |
| 1016 | } |
| 1017 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1018 | ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1019 | ExtQualNodes.InsertNode(New, InsertPos); |
| 1020 | QualType T = QualType(New, Fast); |
| 1021 | return T; |
| 1022 | } |
| 1023 | |
| 1024 | QualType ASTContext::getVolatileType(QualType T) { |
| 1025 | QualType CanT = getCanonicalType(T); |
| 1026 | if (CanT.isVolatileQualified()) return T; |
| 1027 | |
| 1028 | QualifierCollector Quals; |
| 1029 | const Type *TypeNode = Quals.strip(T); |
| 1030 | Quals.addVolatile(); |
| 1031 | |
| 1032 | return getExtQualType(TypeNode, Quals); |
| 1033 | } |
| 1034 | |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 1035 | QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1036 | QualType CanT = getCanonicalType(T); |
| 1037 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1038 | return T; |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1039 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1040 | // If we are composing extended qualifiers together, merge together |
| 1041 | // into one ExtQuals node. |
| 1042 | QualifierCollector Quals; |
| 1043 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1044 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1045 | // If this type already has an address space specified, it cannot get |
| 1046 | // another one. |
| 1047 | assert(!Quals.hasAddressSpace() && |
| 1048 | "Type cannot be in multiple addr spaces!"); |
| 1049 | Quals.addAddressSpace(AddressSpace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1050 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1051 | return getExtQualType(TypeNode, Quals); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1054 | QualType ASTContext::getObjCGCQualType(QualType T, |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1055 | Qualifiers::GC GCAttr) { |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1056 | QualType CanT = getCanonicalType(T); |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1057 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1058 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1059 | |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1060 | if (T->isPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1061 | QualType Pointee = T->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 1062 | if (Pointee->isAnyPointerType()) { |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1063 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 1064 | return getPointerType(ResultType); |
| 1065 | } |
| 1066 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1067 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1068 | // If we are composing extended qualifiers together, merge together |
| 1069 | // into one ExtQuals node. |
| 1070 | QualifierCollector Quals; |
| 1071 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1072 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1073 | // If this type already has an ObjCGC specified, it cannot get |
| 1074 | // another one. |
| 1075 | assert(!Quals.hasObjCGCAttr() && |
| 1076 | "Type cannot have multiple ObjCGCs!"); |
| 1077 | Quals.addObjCGCAttr(GCAttr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1078 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1079 | return getExtQualType(TypeNode, Quals); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1080 | } |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1081 | |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1082 | static QualType getExtFunctionType(ASTContext& Context, QualType T, |
| 1083 | const FunctionType::ExtInfo &Info) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1084 | QualType ResultType; |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1085 | if (const PointerType *Pointer = T->getAs<PointerType>()) { |
| 1086 | QualType Pointee = Pointer->getPointeeType(); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1087 | ResultType = getExtFunctionType(Context, Pointee, Info); |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1088 | if (ResultType == Pointee) |
| 1089 | return T; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1090 | |
| 1091 | ResultType = Context.getPointerType(ResultType); |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1092 | } else if (const BlockPointerType *BlockPointer |
| 1093 | = T->getAs<BlockPointerType>()) { |
| 1094 | QualType Pointee = BlockPointer->getPointeeType(); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1095 | ResultType = getExtFunctionType(Context, Pointee, Info); |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1096 | if (ResultType == Pointee) |
| 1097 | return T; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1098 | |
| 1099 | ResultType = Context.getBlockPointerType(ResultType); |
| 1100 | } else if (const FunctionType *F = T->getAs<FunctionType>()) { |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1101 | if (F->getExtInfo() == Info) |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1102 | return T; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1103 | |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1104 | if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(F)) { |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1105 | ResultType = Context.getFunctionNoProtoType(FNPT->getResultType(), |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1106 | Info); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1107 | } else { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1108 | const FunctionProtoType *FPT = cast<FunctionProtoType>(F); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1109 | ResultType |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1110 | = Context.getFunctionType(FPT->getResultType(), FPT->arg_type_begin(), |
| 1111 | FPT->getNumArgs(), FPT->isVariadic(), |
| 1112 | FPT->getTypeQuals(), |
| 1113 | FPT->hasExceptionSpec(), |
| 1114 | FPT->hasAnyExceptionSpec(), |
| 1115 | FPT->getNumExceptions(), |
| 1116 | FPT->exception_begin(), |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1117 | Info); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1118 | } |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1119 | } else |
| 1120 | return T; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1121 | |
| 1122 | return Context.getQualifiedType(ResultType, T.getLocalQualifiers()); |
| 1123 | } |
| 1124 | |
| 1125 | QualType ASTContext::getNoReturnType(QualType T, bool AddNoReturn) { |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 1126 | FunctionType::ExtInfo Info = getFunctionExtInfo(T); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1127 | return getExtFunctionType(*this, T, |
| 1128 | Info.withNoReturn(AddNoReturn)); |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | QualType ASTContext::getCallConvType(QualType T, CallingConv CallConv) { |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 1132 | FunctionType::ExtInfo Info = getFunctionExtInfo(T); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1133 | return getExtFunctionType(*this, T, |
| 1134 | Info.withCallingConv(CallConv)); |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 1137 | QualType ASTContext::getRegParmType(QualType T, unsigned RegParm) { |
| 1138 | FunctionType::ExtInfo Info = getFunctionExtInfo(T); |
| 1139 | return getExtFunctionType(*this, T, |
| 1140 | Info.withRegParm(RegParm)); |
| 1141 | } |
| 1142 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1143 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 1144 | /// number with the specified element type. |
| 1145 | QualType ASTContext::getComplexType(QualType T) { |
| 1146 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1147 | // structure. |
| 1148 | llvm::FoldingSetNodeID ID; |
| 1149 | ComplexType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1150 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1151 | void *InsertPos = 0; |
| 1152 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1153 | return QualType(CT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1154 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1155 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1156 | // so fill in the canonical type field. |
| 1157 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1158 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1159 | Canonical = getComplexType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1160 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1161 | // Get the new insert position for the node we care about. |
| 1162 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1163 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1164 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1165 | ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1166 | Types.push_back(New); |
| 1167 | ComplexTypes.InsertNode(New, InsertPos); |
| 1168 | return QualType(New, 0); |
| 1169 | } |
| 1170 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1171 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 1172 | /// the specified type. |
| 1173 | QualType ASTContext::getPointerType(QualType T) { |
| 1174 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1175 | // structure. |
| 1176 | llvm::FoldingSetNodeID ID; |
| 1177 | PointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1178 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1179 | void *InsertPos = 0; |
| 1180 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1181 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1182 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1183 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1184 | // so fill in the canonical type field. |
| 1185 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1186 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1187 | Canonical = getPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1188 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1189 | // Get the new insert position for the node we care about. |
| 1190 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1191 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1192 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1193 | PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1194 | Types.push_back(New); |
| 1195 | PointerTypes.InsertNode(New, InsertPos); |
| 1196 | return QualType(New, 0); |
| 1197 | } |
| 1198 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1199 | /// getBlockPointerType - Return the uniqued reference to the type for |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1200 | /// a pointer to the specified block. |
| 1201 | QualType ASTContext::getBlockPointerType(QualType T) { |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1202 | assert(T->isFunctionType() && "block of function types only"); |
| 1203 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1204 | // structure. |
| 1205 | llvm::FoldingSetNodeID ID; |
| 1206 | BlockPointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1207 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1208 | void *InsertPos = 0; |
| 1209 | if (BlockPointerType *PT = |
| 1210 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1211 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1212 | |
| 1213 | // 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] | 1214 | // type either so fill in the canonical type field. |
| 1215 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1216 | if (!T.isCanonical()) { |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1217 | Canonical = getBlockPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1218 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1219 | // Get the new insert position for the node we care about. |
| 1220 | BlockPointerType *NewIP = |
| 1221 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1222 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1223 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1224 | BlockPointerType *New |
| 1225 | = new (*this, TypeAlignment) BlockPointerType(T, Canonical); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1226 | Types.push_back(New); |
| 1227 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 1228 | return QualType(New, 0); |
| 1229 | } |
| 1230 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1231 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 1232 | /// lvalue reference to the specified type. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1233 | QualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1234 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1235 | // structure. |
| 1236 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1237 | ReferenceType::Profile(ID, T, SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1238 | |
| 1239 | void *InsertPos = 0; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1240 | if (LValueReferenceType *RT = |
| 1241 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1242 | return QualType(RT, 0); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1243 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1244 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1245 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1246 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1247 | // either, so fill in the canonical type field. |
| 1248 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1249 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
| 1250 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1251 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1252 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1253 | // Get the new insert position for the node we care about. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1254 | LValueReferenceType *NewIP = |
| 1255 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1256 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1257 | } |
| 1258 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1259 | LValueReferenceType *New |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1260 | = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, |
| 1261 | SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1262 | Types.push_back(New); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1263 | LValueReferenceTypes.InsertNode(New, InsertPos); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1264 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1265 | return QualType(New, 0); |
| 1266 | } |
| 1267 | |
| 1268 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 1269 | /// rvalue reference to the specified type. |
| 1270 | QualType ASTContext::getRValueReferenceType(QualType T) { |
| 1271 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1272 | // structure. |
| 1273 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1274 | ReferenceType::Profile(ID, T, false); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1275 | |
| 1276 | void *InsertPos = 0; |
| 1277 | if (RValueReferenceType *RT = |
| 1278 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1279 | return QualType(RT, 0); |
| 1280 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1281 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1282 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1283 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1284 | // either, so fill in the canonical type field. |
| 1285 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1286 | if (InnerRef || !T.isCanonical()) { |
| 1287 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1288 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1289 | |
| 1290 | // Get the new insert position for the node we care about. |
| 1291 | RValueReferenceType *NewIP = |
| 1292 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1293 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
| 1294 | } |
| 1295 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1296 | RValueReferenceType *New |
| 1297 | = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1298 | Types.push_back(New); |
| 1299 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1300 | return QualType(New, 0); |
| 1301 | } |
| 1302 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1303 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 1304 | /// member pointer to the specified type, in the specified class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1305 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1306 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1307 | // structure. |
| 1308 | llvm::FoldingSetNodeID ID; |
| 1309 | MemberPointerType::Profile(ID, T, Cls); |
| 1310 | |
| 1311 | void *InsertPos = 0; |
| 1312 | if (MemberPointerType *PT = |
| 1313 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1314 | return QualType(PT, 0); |
| 1315 | |
| 1316 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 1317 | // type either, so fill in the canonical type field. |
| 1318 | QualType Canonical; |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1319 | if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1320 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 1321 | |
| 1322 | // Get the new insert position for the node we care about. |
| 1323 | MemberPointerType *NewIP = |
| 1324 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1325 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
| 1326 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1327 | MemberPointerType *New |
| 1328 | = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1329 | Types.push_back(New); |
| 1330 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 1331 | return QualType(New, 0); |
| 1332 | } |
| 1333 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1334 | /// getConstantArrayType - Return the unique reference to the type for an |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1335 | /// array of the specified element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1336 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1337 | const llvm::APInt &ArySizeIn, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1338 | ArrayType::ArraySizeModifier ASM, |
| 1339 | unsigned EltTypeQuals) { |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1340 | assert((EltTy->isDependentType() || |
| 1341 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && |
Eli Friedman | 587cbdf | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1342 | "Constant array of VLAs is illegal!"); |
| 1343 | |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1344 | // Convert the array size into a canonical width matching the pointer size for |
| 1345 | // the target. |
| 1346 | llvm::APInt ArySize(ArySizeIn); |
| 1347 | ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1348 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1349 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 0be2ef2 | 2009-02-19 17:31:02 +0000 | [diff] [blame] | 1350 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1351 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1352 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1353 | if (ConstantArrayType *ATP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1354 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1355 | return QualType(ATP, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1356 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1357 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1358 | // so fill in the canonical type field. |
| 1359 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1360 | if (!EltTy.isCanonical()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1361 | Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1362 | ASM, EltTypeQuals); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1363 | // Get the new insert position for the node we care about. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1364 | ConstantArrayType *NewIP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1365 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1366 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1367 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1368 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1369 | ConstantArrayType *New = new(*this,TypeAlignment) |
| 1370 | ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1371 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1372 | Types.push_back(New); |
| 1373 | return QualType(New, 0); |
| 1374 | } |
| 1375 | |
Steve Naroff | bdbf7b0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 1376 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 1377 | /// variable array of the specified element type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1378 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 1379 | Expr *NumElts, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1380 | ArrayType::ArraySizeModifier ASM, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1381 | unsigned EltTypeQuals, |
| 1382 | SourceRange Brackets) { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1383 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 1384 | // that have an expression provided for their size. |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 1385 | QualType CanonType; |
| 1386 | |
| 1387 | if (!EltTy.isCanonical()) { |
| 1388 | if (NumElts) |
| 1389 | NumElts->Retain(); |
| 1390 | CanonType = getVariableArrayType(getCanonicalType(EltTy), NumElts, ASM, |
| 1391 | EltTypeQuals, Brackets); |
| 1392 | } |
| 1393 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1394 | VariableArrayType *New = new(*this, TypeAlignment) |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 1395 | VariableArrayType(EltTy, CanonType, NumElts, ASM, EltTypeQuals, Brackets); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1396 | |
| 1397 | VariableArrayTypes.push_back(New); |
| 1398 | Types.push_back(New); |
| 1399 | return QualType(New, 0); |
| 1400 | } |
| 1401 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1402 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 1403 | /// the type for a dependently-sized array of the specified element |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1404 | /// type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1405 | QualType ASTContext::getDependentSizedArrayType(QualType EltTy, |
| 1406 | Expr *NumElts, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1407 | ArrayType::ArraySizeModifier ASM, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1408 | unsigned EltTypeQuals, |
| 1409 | SourceRange Brackets) { |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1410 | assert((!NumElts || NumElts->isTypeDependent() || |
| 1411 | NumElts->isValueDependent()) && |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1412 | "Size must be type- or value-dependent!"); |
| 1413 | |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1414 | void *InsertPos = 0; |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1415 | DependentSizedArrayType *Canon = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1416 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1417 | |
| 1418 | if (NumElts) { |
| 1419 | // Dependently-sized array types that do not have a specified |
| 1420 | // number of elements will have their sizes deduced from an |
| 1421 | // initializer. |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1422 | DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM, |
| 1423 | EltTypeQuals, NumElts); |
| 1424 | |
| 1425 | Canon = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1426 | } |
| 1427 | |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1428 | DependentSizedArrayType *New; |
| 1429 | if (Canon) { |
| 1430 | // We already have a canonical version of this array type; use it as |
| 1431 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1432 | New = new (*this, TypeAlignment) |
| 1433 | DependentSizedArrayType(*this, EltTy, QualType(Canon, 0), |
| 1434 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1435 | } else { |
| 1436 | QualType CanonEltTy = getCanonicalType(EltTy); |
| 1437 | if (CanonEltTy == EltTy) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1438 | New = new (*this, TypeAlignment) |
| 1439 | DependentSizedArrayType(*this, EltTy, QualType(), |
| 1440 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1441 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1442 | if (NumElts) { |
| 1443 | DependentSizedArrayType *CanonCheck |
| 1444 | = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1445 | assert(!CanonCheck && "Dependent-sized canonical array type broken"); |
| 1446 | (void)CanonCheck; |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1447 | DependentSizedArrayTypes.InsertNode(New, InsertPos); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1448 | } |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1449 | } else { |
| 1450 | QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts, |
| 1451 | ASM, EltTypeQuals, |
| 1452 | SourceRange()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1453 | New = new (*this, TypeAlignment) |
| 1454 | DependentSizedArrayType(*this, EltTy, Canon, |
| 1455 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1456 | } |
| 1457 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1458 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1459 | Types.push_back(New); |
| 1460 | return QualType(New, 0); |
| 1461 | } |
| 1462 | |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1463 | QualType ASTContext::getIncompleteArrayType(QualType EltTy, |
| 1464 | ArrayType::ArraySizeModifier ASM, |
| 1465 | unsigned EltTypeQuals) { |
| 1466 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 0be2ef2 | 2009-02-19 17:31:02 +0000 | [diff] [blame] | 1467 | IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1468 | |
| 1469 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1470 | if (IncompleteArrayType *ATP = |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1471 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1472 | return QualType(ATP, 0); |
| 1473 | |
| 1474 | // If the element type isn't canonical, this won't be a canonical type |
| 1475 | // either, so fill in the canonical type field. |
| 1476 | QualType Canonical; |
| 1477 | |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1478 | if (!EltTy.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1479 | Canonical = getIncompleteArrayType(getCanonicalType(EltTy), |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1480 | ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1481 | |
| 1482 | // Get the new insert position for the node we care about. |
| 1483 | IncompleteArrayType *NewIP = |
| 1484 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1485 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1486 | } |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1487 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1488 | IncompleteArrayType *New = new (*this, TypeAlignment) |
| 1489 | IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1490 | |
| 1491 | IncompleteArrayTypes.InsertNode(New, InsertPos); |
| 1492 | Types.push_back(New); |
| 1493 | return QualType(New, 0); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1494 | } |
| 1495 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1496 | /// getVectorType - Return the unique reference to a vector type of |
| 1497 | /// 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] | 1498 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts, |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1499 | VectorType::AltiVecSpecific AltiVecSpec) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1500 | BuiltinType *baseType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1501 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1502 | baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1503 | assert(baseType != 0 && "getVectorType(): Expecting a built-in type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1504 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1505 | // Check if we've already instantiated a vector of this type. |
| 1506 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1507 | VectorType::Profile(ID, vecType, NumElts, Type::Vector, AltiVecSpec); |
| 1508 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1509 | void *InsertPos = 0; |
| 1510 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1511 | return QualType(VTP, 0); |
| 1512 | |
| 1513 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1514 | // so fill in the canonical type field. |
| 1515 | QualType Canonical; |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1516 | if (!vecType.isCanonical()) { |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1517 | Canonical = getVectorType(getCanonicalType(vecType), NumElts, |
| 1518 | VectorType::NotAltiVec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1519 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1520 | // Get the new insert position for the node we care about. |
| 1521 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1522 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1523 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1524 | VectorType *New = new (*this, TypeAlignment) |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1525 | VectorType(vecType, NumElts, Canonical, AltiVecSpec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1526 | VectorTypes.InsertNode(New, InsertPos); |
| 1527 | Types.push_back(New); |
| 1528 | return QualType(New, 0); |
| 1529 | } |
| 1530 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1531 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1532 | /// the specified element type and size. VectorType must be a built-in type. |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1533 | QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) { |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1534 | BuiltinType *baseType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1535 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1536 | baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1537 | assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1538 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1539 | // Check if we've already instantiated a vector of this type. |
| 1540 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1541 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, |
| 1542 | VectorType::NotAltiVec); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1543 | void *InsertPos = 0; |
| 1544 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1545 | return QualType(VTP, 0); |
| 1546 | |
| 1547 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1548 | // so fill in the canonical type field. |
| 1549 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1550 | if (!vecType.isCanonical()) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1551 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1552 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1553 | // Get the new insert position for the node we care about. |
| 1554 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1555 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1556 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1557 | ExtVectorType *New = new (*this, TypeAlignment) |
| 1558 | ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1559 | VectorTypes.InsertNode(New, InsertPos); |
| 1560 | Types.push_back(New); |
| 1561 | return QualType(New, 0); |
| 1562 | } |
| 1563 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1564 | QualType ASTContext::getDependentSizedExtVectorType(QualType vecType, |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1565 | Expr *SizeExpr, |
| 1566 | SourceLocation AttrLoc) { |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1567 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1568 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1569 | SizeExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1570 | |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1571 | void *InsertPos = 0; |
| 1572 | DependentSizedExtVectorType *Canon |
| 1573 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1574 | DependentSizedExtVectorType *New; |
| 1575 | if (Canon) { |
| 1576 | // We already have a canonical version of this array type; use it as |
| 1577 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1578 | New = new (*this, TypeAlignment) |
| 1579 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), |
| 1580 | SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1581 | } else { |
| 1582 | QualType CanonVecTy = getCanonicalType(vecType); |
| 1583 | if (CanonVecTy == vecType) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1584 | New = new (*this, TypeAlignment) |
| 1585 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, |
| 1586 | AttrLoc); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1587 | |
| 1588 | DependentSizedExtVectorType *CanonCheck |
| 1589 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1590 | assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken"); |
| 1591 | (void)CanonCheck; |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1592 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); |
| 1593 | } else { |
| 1594 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 1595 | SourceLocation()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1596 | New = new (*this, TypeAlignment) |
| 1597 | DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1598 | } |
| 1599 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1600 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1601 | Types.push_back(New); |
| 1602 | return QualType(New, 0); |
| 1603 | } |
| 1604 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1605 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1606 | /// |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1607 | QualType ASTContext::getFunctionNoProtoType(QualType ResultTy, |
| 1608 | const FunctionType::ExtInfo &Info) { |
| 1609 | const CallingConv CallConv = Info.getCC(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1610 | // Unique functions, to guarantee there is only one function of a particular |
| 1611 | // structure. |
| 1612 | llvm::FoldingSetNodeID ID; |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1613 | FunctionNoProtoType::Profile(ID, ResultTy, Info); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1614 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1615 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1616 | if (FunctionNoProtoType *FT = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1617 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1618 | return QualType(FT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1619 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1620 | QualType Canonical; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1621 | if (!ResultTy.isCanonical() || |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1622 | getCanonicalCallConv(CallConv) != CallConv) { |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1623 | Canonical = |
| 1624 | getFunctionNoProtoType(getCanonicalType(ResultTy), |
| 1625 | Info.withCallingConv(getCanonicalCallConv(CallConv))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1626 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1627 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1628 | FunctionNoProtoType *NewIP = |
| 1629 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1630 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1631 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1632 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1633 | FunctionNoProtoType *New = new (*this, TypeAlignment) |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1634 | FunctionNoProtoType(ResultTy, Canonical, Info); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1635 | Types.push_back(New); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1636 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1637 | return QualType(New, 0); |
| 1638 | } |
| 1639 | |
| 1640 | /// getFunctionType - Return a normal function type with a typed argument |
| 1641 | /// list. isVariadic indicates whether the argument list includes '...'. |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 1642 | QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray, |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1643 | unsigned NumArgs, bool isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1644 | unsigned TypeQuals, bool hasExceptionSpec, |
| 1645 | bool hasAnyExceptionSpec, unsigned NumExs, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1646 | const QualType *ExArray, |
| 1647 | const FunctionType::ExtInfo &Info) { |
| 1648 | const CallingConv CallConv= Info.getCC(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1649 | // Unique functions, to guarantee there is only one function of a particular |
| 1650 | // structure. |
| 1651 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1652 | FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1653 | TypeQuals, hasExceptionSpec, hasAnyExceptionSpec, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1654 | NumExs, ExArray, Info); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1655 | |
| 1656 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1657 | if (FunctionProtoType *FTP = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1658 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1659 | return QualType(FTP, 0); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1660 | |
| 1661 | // Determine whether the type being created is already canonical or not. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1662 | bool isCanonical = !hasExceptionSpec && ResultTy.isCanonical(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1663 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1664 | if (!ArgArray[i].isCanonicalAsParam()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1665 | isCanonical = false; |
| 1666 | |
| 1667 | // If this type isn't canonical, get the canonical version of it. |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1668 | // The exception spec is not part of the canonical type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1669 | QualType Canonical; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1670 | if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1671 | llvm::SmallVector<QualType, 16> CanonicalArgs; |
| 1672 | CanonicalArgs.reserve(NumArgs); |
| 1673 | for (unsigned i = 0; i != NumArgs; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1674 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1675 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1676 | Canonical = getFunctionType(getCanonicalType(ResultTy), |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1677 | CanonicalArgs.data(), NumArgs, |
Douglas Gregor | 47259d9 | 2009-08-05 19:03:35 +0000 | [diff] [blame] | 1678 | isVariadic, TypeQuals, false, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1679 | false, 0, 0, |
| 1680 | Info.withCallingConv(getCanonicalCallConv(CallConv))); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1681 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1682 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1683 | FunctionProtoType *NewIP = |
| 1684 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1685 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1686 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1687 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1688 | // FunctionProtoType objects are allocated with extra bytes after them |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1689 | // for two variable size arrays (for parameter and exception types) at the |
| 1690 | // end of them. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1691 | FunctionProtoType *FTP = |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1692 | (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) + |
| 1693 | NumArgs*sizeof(QualType) + |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1694 | NumExs*sizeof(QualType), TypeAlignment); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1695 | new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1696 | TypeQuals, hasExceptionSpec, hasAnyExceptionSpec, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1697 | ExArray, NumExs, Canonical, Info); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1698 | Types.push_back(FTP); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1699 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1700 | return QualType(FTP, 0); |
| 1701 | } |
| 1702 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1703 | #ifndef NDEBUG |
| 1704 | static bool NeedsInjectedClassNameType(const RecordDecl *D) { |
| 1705 | if (!isa<CXXRecordDecl>(D)) return false; |
| 1706 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 1707 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) |
| 1708 | return true; |
| 1709 | if (RD->getDescribedClassTemplate() && |
| 1710 | !isa<ClassTemplateSpecializationDecl>(RD)) |
| 1711 | return true; |
| 1712 | return false; |
| 1713 | } |
| 1714 | #endif |
| 1715 | |
| 1716 | /// getInjectedClassNameType - Return the unique reference to the |
| 1717 | /// injected class name type for the specified templated declaration. |
| 1718 | QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl, |
| 1719 | QualType TST) { |
| 1720 | assert(NeedsInjectedClassNameType(Decl)); |
| 1721 | if (Decl->TypeForDecl) { |
| 1722 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
Argyrios Kyrtzidis | 37ffed3 | 2010-07-02 11:55:32 +0000 | [diff] [blame] | 1723 | } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDeclaration()) { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1724 | assert(PrevDecl->TypeForDecl && "previous declaration has no type"); |
| 1725 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 1726 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
| 1727 | } else { |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1728 | Decl->TypeForDecl = |
| 1729 | new (*this, TypeAlignment) InjectedClassNameType(Decl, TST); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1730 | Types.push_back(Decl->TypeForDecl); |
| 1731 | } |
| 1732 | return QualType(Decl->TypeForDecl, 0); |
| 1733 | } |
| 1734 | |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1735 | /// getTypeDeclType - Return the unique reference to the type for the |
| 1736 | /// specified type declaration. |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1737 | QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) { |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 1738 | assert(Decl && "Passed null for Decl param"); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1739 | assert(!Decl->TypeForDecl && "TypeForDecl present in slow case"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1740 | |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1741 | if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1742 | return getTypedefType(Typedef); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1743 | |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1744 | assert(!isa<TemplateTypeParmDecl>(Decl) && |
| 1745 | "Template type parameter types are always available."); |
| 1746 | |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1747 | if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1748 | assert(!Record->getPreviousDeclaration() && |
| 1749 | "struct/union has previous declaration"); |
| 1750 | assert(!NeedsInjectedClassNameType(Record)); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 1751 | return getRecordType(Record); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1752 | } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1753 | assert(!Enum->getPreviousDeclaration() && |
| 1754 | "enum has previous declaration"); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 1755 | return getEnumType(Enum); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1756 | } else if (const UnresolvedUsingTypenameDecl *Using = |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1757 | dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) { |
| 1758 | Decl->TypeForDecl = new (*this, TypeAlignment) UnresolvedUsingType(Using); |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 1759 | } else |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1760 | llvm_unreachable("TypeDecl without a type?"); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1761 | |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1762 | Types.push_back(Decl->TypeForDecl); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1763 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1764 | } |
| 1765 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1766 | /// getTypedefType - Return the unique reference to the type for the |
| 1767 | /// specified typename decl. |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 1768 | QualType |
| 1769 | ASTContext::getTypedefType(const TypedefDecl *Decl, QualType Canonical) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1770 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1771 | |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 1772 | if (Canonical.isNull()) |
| 1773 | Canonical = getCanonicalType(Decl->getUnderlyingType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1774 | Decl->TypeForDecl = new(*this, TypeAlignment) |
| 1775 | TypedefType(Type::Typedef, Decl, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1776 | Types.push_back(Decl->TypeForDecl); |
| 1777 | return QualType(Decl->TypeForDecl, 0); |
| 1778 | } |
| 1779 | |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 1780 | QualType ASTContext::getRecordType(const RecordDecl *Decl) { |
| 1781 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 1782 | |
| 1783 | if (const RecordDecl *PrevDecl = Decl->getPreviousDeclaration()) |
| 1784 | if (PrevDecl->TypeForDecl) |
| 1785 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 1786 | |
| 1787 | Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Decl); |
| 1788 | Types.push_back(Decl->TypeForDecl); |
| 1789 | return QualType(Decl->TypeForDecl, 0); |
| 1790 | } |
| 1791 | |
| 1792 | QualType ASTContext::getEnumType(const EnumDecl *Decl) { |
| 1793 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 1794 | |
| 1795 | if (const EnumDecl *PrevDecl = Decl->getPreviousDeclaration()) |
| 1796 | if (PrevDecl->TypeForDecl) |
| 1797 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 1798 | |
| 1799 | Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Decl); |
| 1800 | Types.push_back(Decl->TypeForDecl); |
| 1801 | return QualType(Decl->TypeForDecl, 0); |
| 1802 | } |
| 1803 | |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1804 | /// \brief Retrieve a substitution-result type. |
| 1805 | QualType |
| 1806 | ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, |
| 1807 | QualType Replacement) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1808 | assert(Replacement.isCanonical() |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1809 | && "replacement types must always be canonical"); |
| 1810 | |
| 1811 | llvm::FoldingSetNodeID ID; |
| 1812 | SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); |
| 1813 | void *InsertPos = 0; |
| 1814 | SubstTemplateTypeParmType *SubstParm |
| 1815 | = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1816 | |
| 1817 | if (!SubstParm) { |
| 1818 | SubstParm = new (*this, TypeAlignment) |
| 1819 | SubstTemplateTypeParmType(Parm, Replacement); |
| 1820 | Types.push_back(SubstParm); |
| 1821 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 1822 | } |
| 1823 | |
| 1824 | return QualType(SubstParm, 0); |
| 1825 | } |
| 1826 | |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1827 | /// \brief Retrieve the template type parameter type for a template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1828 | /// parameter or parameter pack with the given depth, index, and (optionally) |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1829 | /// name. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1830 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1831 | bool ParameterPack, |
Douglas Gregor | efed5c8 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 1832 | IdentifierInfo *Name) { |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1833 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | efed5c8 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 1834 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1835 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1836 | TemplateTypeParmType *TypeParm |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1837 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1838 | |
| 1839 | if (TypeParm) |
| 1840 | return QualType(TypeParm, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1841 | |
Douglas Gregor | efed5c8 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 1842 | if (Name) { |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1843 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | efed5c8 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 1844 | TypeParm = new (*this, TypeAlignment) |
| 1845 | TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1846 | |
| 1847 | TemplateTypeParmType *TypeCheck |
| 1848 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1849 | assert(!TypeCheck && "Template type parameter canonical type broken"); |
| 1850 | (void)TypeCheck; |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1851 | } else |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1852 | TypeParm = new (*this, TypeAlignment) |
| 1853 | TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1854 | |
| 1855 | Types.push_back(TypeParm); |
| 1856 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 1857 | |
| 1858 | return QualType(TypeParm, 0); |
| 1859 | } |
| 1860 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1861 | TypeSourceInfo * |
| 1862 | ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name, |
| 1863 | SourceLocation NameLoc, |
| 1864 | const TemplateArgumentListInfo &Args, |
| 1865 | QualType CanonType) { |
| 1866 | QualType TST = getTemplateSpecializationType(Name, Args, CanonType); |
| 1867 | |
| 1868 | TypeSourceInfo *DI = CreateTypeSourceInfo(TST); |
| 1869 | TemplateSpecializationTypeLoc TL |
| 1870 | = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc()); |
| 1871 | TL.setTemplateNameLoc(NameLoc); |
| 1872 | TL.setLAngleLoc(Args.getLAngleLoc()); |
| 1873 | TL.setRAngleLoc(Args.getRAngleLoc()); |
| 1874 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 1875 | TL.setArgLocInfo(i, Args[i].getLocInfo()); |
| 1876 | return DI; |
| 1877 | } |
| 1878 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1879 | QualType |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1880 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1881 | const TemplateArgumentListInfo &Args, |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 1882 | QualType Canon) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1883 | unsigned NumArgs = Args.size(); |
| 1884 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1885 | llvm::SmallVector<TemplateArgument, 4> ArgVec; |
| 1886 | ArgVec.reserve(NumArgs); |
| 1887 | for (unsigned i = 0; i != NumArgs; ++i) |
| 1888 | ArgVec.push_back(Args[i].getArgument()); |
| 1889 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1890 | return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 1891 | Canon); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1892 | } |
| 1893 | |
| 1894 | QualType |
| 1895 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1896 | const TemplateArgument *Args, |
| 1897 | unsigned NumArgs, |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 1898 | QualType Canon) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1899 | if (!Canon.isNull()) |
| 1900 | Canon = getCanonicalType(Canon); |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 1901 | else |
| 1902 | Canon = getCanonicalTemplateSpecializationType(Template, Args, NumArgs); |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 1903 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1904 | // Allocate the (non-canonical) template specialization type, but don't |
| 1905 | // try to unique it: these types typically have location information that |
| 1906 | // we don't unique and don't want to lose. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1907 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1908 | sizeof(TemplateArgument) * NumArgs), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1909 | TypeAlignment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1910 | TemplateSpecializationType *Spec |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 1911 | = new (Mem) TemplateSpecializationType(Template, |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1912 | Args, NumArgs, |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 1913 | Canon); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1914 | |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1915 | Types.push_back(Spec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1916 | return QualType(Spec, 0); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1917 | } |
| 1918 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1919 | QualType |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 1920 | ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template, |
| 1921 | const TemplateArgument *Args, |
| 1922 | unsigned NumArgs) { |
| 1923 | // Build the canonical template specialization type. |
| 1924 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); |
| 1925 | llvm::SmallVector<TemplateArgument, 4> CanonArgs; |
| 1926 | CanonArgs.reserve(NumArgs); |
| 1927 | for (unsigned I = 0; I != NumArgs; ++I) |
| 1928 | CanonArgs.push_back(getCanonicalTemplateArgument(Args[I])); |
| 1929 | |
| 1930 | // Determine whether this canonical template specialization type already |
| 1931 | // exists. |
| 1932 | llvm::FoldingSetNodeID ID; |
| 1933 | TemplateSpecializationType::Profile(ID, CanonTemplate, |
| 1934 | CanonArgs.data(), NumArgs, *this); |
| 1935 | |
| 1936 | void *InsertPos = 0; |
| 1937 | TemplateSpecializationType *Spec |
| 1938 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1939 | |
| 1940 | if (!Spec) { |
| 1941 | // Allocate a new canonical template specialization type. |
| 1942 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
| 1943 | sizeof(TemplateArgument) * NumArgs), |
| 1944 | TypeAlignment); |
| 1945 | Spec = new (Mem) TemplateSpecializationType(CanonTemplate, |
| 1946 | CanonArgs.data(), NumArgs, |
| 1947 | QualType()); |
| 1948 | Types.push_back(Spec); |
| 1949 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
| 1950 | } |
| 1951 | |
| 1952 | assert(Spec->isDependentType() && |
| 1953 | "Non-dependent template-id type must have a canonical type"); |
| 1954 | return QualType(Spec, 0); |
| 1955 | } |
| 1956 | |
| 1957 | QualType |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1958 | ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword, |
| 1959 | NestedNameSpecifier *NNS, |
| 1960 | QualType NamedType) { |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1961 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1962 | ElaboratedType::Profile(ID, Keyword, NNS, NamedType); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1963 | |
| 1964 | void *InsertPos = 0; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1965 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1966 | if (T) |
| 1967 | return QualType(T, 0); |
| 1968 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1969 | QualType Canon = NamedType; |
| 1970 | if (!Canon.isCanonical()) { |
| 1971 | Canon = getCanonicalType(NamedType); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1972 | ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1973 | assert(!CheckT && "Elaborated canonical type broken"); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1974 | (void)CheckT; |
| 1975 | } |
| 1976 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1977 | T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1978 | Types.push_back(T); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1979 | ElaboratedTypes.InsertNode(T, InsertPos); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1980 | return QualType(T, 0); |
| 1981 | } |
| 1982 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 1983 | QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, |
| 1984 | NestedNameSpecifier *NNS, |
| 1985 | const IdentifierInfo *Name, |
| 1986 | QualType Canon) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1987 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 1988 | |
| 1989 | if (Canon.isNull()) { |
| 1990 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 1991 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 1992 | if (Keyword == ETK_None) |
| 1993 | CanonKeyword = ETK_Typename; |
| 1994 | |
| 1995 | if (CanonNNS != NNS || CanonKeyword != Keyword) |
| 1996 | Canon = getDependentNameType(CanonKeyword, CanonNNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1997 | } |
| 1998 | |
| 1999 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2000 | DependentNameType::Profile(ID, Keyword, NNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2001 | |
| 2002 | void *InsertPos = 0; |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2003 | DependentNameType *T |
| 2004 | = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2005 | if (T) |
| 2006 | return QualType(T, 0); |
| 2007 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2008 | T = new (*this) DependentNameType(Keyword, NNS, Name, Canon); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2009 | Types.push_back(T); |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2010 | DependentNameTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2011 | return QualType(T, 0); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2012 | } |
| 2013 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2014 | QualType |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2015 | ASTContext::getDependentTemplateSpecializationType( |
| 2016 | ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2017 | NestedNameSpecifier *NNS, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2018 | const IdentifierInfo *Name, |
| 2019 | const TemplateArgumentListInfo &Args) { |
| 2020 | // TODO: avoid this copy |
| 2021 | llvm::SmallVector<TemplateArgument, 16> ArgCopy; |
| 2022 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
| 2023 | ArgCopy.push_back(Args[I].getArgument()); |
| 2024 | return getDependentTemplateSpecializationType(Keyword, NNS, Name, |
| 2025 | ArgCopy.size(), |
| 2026 | ArgCopy.data()); |
| 2027 | } |
| 2028 | |
| 2029 | QualType |
| 2030 | ASTContext::getDependentTemplateSpecializationType( |
| 2031 | ElaboratedTypeKeyword Keyword, |
| 2032 | NestedNameSpecifier *NNS, |
| 2033 | const IdentifierInfo *Name, |
| 2034 | unsigned NumArgs, |
| 2035 | const TemplateArgument *Args) { |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2036 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 2037 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2038 | llvm::FoldingSetNodeID ID; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2039 | DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS, |
| 2040 | Name, NumArgs, Args); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2041 | |
| 2042 | void *InsertPos = 0; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2043 | DependentTemplateSpecializationType *T |
| 2044 | = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2045 | if (T) |
| 2046 | return QualType(T, 0); |
| 2047 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2048 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2049 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2050 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 2051 | if (Keyword == ETK_None) CanonKeyword = ETK_Typename; |
| 2052 | |
| 2053 | bool AnyNonCanonArgs = false; |
| 2054 | llvm::SmallVector<TemplateArgument, 16> CanonArgs(NumArgs); |
| 2055 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 2056 | CanonArgs[I] = getCanonicalTemplateArgument(Args[I]); |
| 2057 | if (!CanonArgs[I].structurallyEquals(Args[I])) |
| 2058 | AnyNonCanonArgs = true; |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2059 | } |
| 2060 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2061 | QualType Canon; |
| 2062 | if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) { |
| 2063 | Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS, |
| 2064 | Name, NumArgs, |
| 2065 | CanonArgs.data()); |
| 2066 | |
| 2067 | // Find the insert position again. |
| 2068 | DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2069 | } |
| 2070 | |
| 2071 | void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) + |
| 2072 | sizeof(TemplateArgument) * NumArgs), |
| 2073 | TypeAlignment); |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 2074 | T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2075 | Name, NumArgs, Args, Canon); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2076 | Types.push_back(T); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2077 | DependentTemplateSpecializationTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2078 | return QualType(T, 0); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2079 | } |
| 2080 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2081 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 2082 | /// alphabetically. |
| 2083 | static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, |
| 2084 | const ObjCProtocolDecl *RHS) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2085 | return LHS->getDeclName() < RHS->getDeclName(); |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2086 | } |
| 2087 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2088 | static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols, |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2089 | unsigned NumProtocols) { |
| 2090 | if (NumProtocols == 0) return true; |
| 2091 | |
| 2092 | for (unsigned i = 1; i != NumProtocols; ++i) |
| 2093 | if (!CmpProtocolNames(Protocols[i-1], Protocols[i])) |
| 2094 | return false; |
| 2095 | return true; |
| 2096 | } |
| 2097 | |
| 2098 | static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols, |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2099 | unsigned &NumProtocols) { |
| 2100 | ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2101 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2102 | // Sort protocols, keyed by name. |
| 2103 | std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); |
| 2104 | |
| 2105 | // Remove duplicates. |
| 2106 | ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); |
| 2107 | NumProtocols = ProtocolsEnd-Protocols; |
| 2108 | } |
| 2109 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2110 | QualType ASTContext::getObjCObjectType(QualType BaseType, |
| 2111 | ObjCProtocolDecl * const *Protocols, |
| 2112 | unsigned NumProtocols) { |
| 2113 | // If the base type is an interface and there aren't any protocols |
| 2114 | // to add, then the interface type will do just fine. |
| 2115 | if (!NumProtocols && isa<ObjCInterfaceType>(BaseType)) |
| 2116 | return BaseType; |
| 2117 | |
| 2118 | // Look in the folding set for an existing type. |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2119 | llvm::FoldingSetNodeID ID; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2120 | ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2121 | void *InsertPos = 0; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2122 | if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2123 | return QualType(QT, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2124 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2125 | // Build the canonical type, which has the canonical base type and |
| 2126 | // a sorted-and-uniqued list of protocols. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2127 | QualType Canonical; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2128 | bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols); |
| 2129 | if (!ProtocolsSorted || !BaseType.isCanonical()) { |
| 2130 | if (!ProtocolsSorted) { |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 2131 | llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols, |
| 2132 | Protocols + NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2133 | unsigned UniqueCount = NumProtocols; |
| 2134 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2135 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2136 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 2137 | &Sorted[0], UniqueCount); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2138 | } else { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2139 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 2140 | Protocols, NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2141 | } |
| 2142 | |
| 2143 | // Regenerate InsertPos. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2144 | ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2145 | } |
| 2146 | |
| 2147 | unsigned Size = sizeof(ObjCObjectTypeImpl); |
| 2148 | Size += NumProtocols * sizeof(ObjCProtocolDecl *); |
| 2149 | void *Mem = Allocate(Size, TypeAlignment); |
| 2150 | ObjCObjectTypeImpl *T = |
| 2151 | new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols); |
| 2152 | |
| 2153 | Types.push_back(T); |
| 2154 | ObjCObjectTypes.InsertNode(T, InsertPos); |
| 2155 | return QualType(T, 0); |
| 2156 | } |
| 2157 | |
| 2158 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 2159 | /// the given object type. |
| 2160 | QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) { |
| 2161 | llvm::FoldingSetNodeID ID; |
| 2162 | ObjCObjectPointerType::Profile(ID, ObjectT); |
| 2163 | |
| 2164 | void *InsertPos = 0; |
| 2165 | if (ObjCObjectPointerType *QT = |
| 2166 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2167 | return QualType(QT, 0); |
| 2168 | |
| 2169 | // Find the canonical object type. |
| 2170 | QualType Canonical; |
| 2171 | if (!ObjectT.isCanonical()) { |
| 2172 | Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT)); |
| 2173 | |
| 2174 | // Regenerate InsertPos. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2175 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2176 | } |
| 2177 | |
Douglas Gregor | fd6a088 | 2010-02-08 22:59:26 +0000 | [diff] [blame] | 2178 | // No match. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2179 | void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment); |
| 2180 | ObjCObjectPointerType *QType = |
| 2181 | new (Mem) ObjCObjectPointerType(Canonical, ObjectT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2182 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2183 | Types.push_back(QType); |
| 2184 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2185 | return QualType(QType, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2186 | } |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2187 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2188 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 2189 | /// specified ObjC interface decl. The list of protocols is optional. |
| 2190 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) { |
| 2191 | if (Decl->TypeForDecl) |
| 2192 | return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2193 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2194 | // FIXME: redeclarations? |
| 2195 | void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment); |
| 2196 | ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl); |
| 2197 | Decl->TypeForDecl = T; |
| 2198 | Types.push_back(T); |
| 2199 | return QualType(T, 0); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2200 | } |
| 2201 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2202 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 2203 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2204 | /// multiple declarations that refer to "typeof(x)" all contain different |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2205 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2206 | /// on canonical type's (which are always unique). |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2207 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2208 | TypeOfExprType *toe; |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2209 | if (tofExpr->isTypeDependent()) { |
| 2210 | llvm::FoldingSetNodeID ID; |
| 2211 | DependentTypeOfExprType::Profile(ID, *this, tofExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2212 | |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2213 | void *InsertPos = 0; |
| 2214 | DependentTypeOfExprType *Canon |
| 2215 | = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2216 | if (Canon) { |
| 2217 | // We already have a "canonical" version of an identical, dependent |
| 2218 | // typeof(expr) type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2219 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2220 | QualType((TypeOfExprType*)Canon, 0)); |
| 2221 | } |
| 2222 | else { |
| 2223 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2224 | Canon |
| 2225 | = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2226 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); |
| 2227 | toe = Canon; |
| 2228 | } |
| 2229 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2230 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2231 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2232 | } |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2233 | Types.push_back(toe); |
| 2234 | return QualType(toe, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2235 | } |
| 2236 | |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2237 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 2238 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 2239 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2240 | /// an issue. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2241 | /// on canonical type's (which are always unique). |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2242 | QualType ASTContext::getTypeOfType(QualType tofType) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2243 | QualType Canonical = getCanonicalType(tofType); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2244 | TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2245 | Types.push_back(tot); |
| 2246 | return QualType(tot, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2247 | } |
| 2248 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2249 | /// getDecltypeForExpr - Given an expr, will return the decltype for that |
| 2250 | /// expression, according to the rules in C++0x [dcl.type.simple]p4 |
| 2251 | static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) { |
Anders Carlsson | a07c33e | 2009-06-25 15:00:34 +0000 | [diff] [blame] | 2252 | if (e->isTypeDependent()) |
| 2253 | return Context.DependentTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2254 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2255 | // If e is an id expression or a class member access, decltype(e) is defined |
| 2256 | // as the type of the entity named by e. |
| 2257 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) { |
| 2258 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) |
| 2259 | return VD->getType(); |
| 2260 | } |
| 2261 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) { |
| 2262 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) |
| 2263 | return FD->getType(); |
| 2264 | } |
| 2265 | // If e is a function call or an invocation of an overloaded operator, |
| 2266 | // (parentheses around e are ignored), decltype(e) is defined as the |
| 2267 | // return type of that function. |
| 2268 | if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens())) |
| 2269 | return CE->getCallReturnType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2270 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2271 | QualType T = e->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2272 | |
| 2273 | // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2274 | // defined as T&, otherwise decltype(e) is defined as T. |
| 2275 | if (e->isLvalue(Context) == Expr::LV_Valid) |
| 2276 | T = Context.getLValueReferenceType(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2277 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2278 | return T; |
| 2279 | } |
| 2280 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2281 | /// getDecltypeType - Unlike many "get<Type>" functions, we don't unique |
| 2282 | /// DecltypeType AST's. The only motivation to unique these nodes would be |
| 2283 | /// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2284 | /// an issue. This doesn't effect the type checker, since it operates |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2285 | /// on canonical type's (which are always unique). |
| 2286 | QualType ASTContext::getDecltypeType(Expr *e) { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2287 | DecltypeType *dt; |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2288 | if (e->isTypeDependent()) { |
| 2289 | llvm::FoldingSetNodeID ID; |
| 2290 | DependentDecltypeType::Profile(ID, *this, e); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2291 | |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2292 | void *InsertPos = 0; |
| 2293 | DependentDecltypeType *Canon |
| 2294 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2295 | if (Canon) { |
| 2296 | // We already have a "canonical" version of an equivalent, dependent |
| 2297 | // decltype type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2298 | dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy, |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2299 | QualType((DecltypeType*)Canon, 0)); |
| 2300 | } |
| 2301 | else { |
| 2302 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2303 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2304 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); |
| 2305 | dt = Canon; |
| 2306 | } |
| 2307 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2308 | QualType T = getDecltypeForExpr(e, *this); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2309 | dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T)); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2310 | } |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2311 | Types.push_back(dt); |
| 2312 | return QualType(dt, 0); |
| 2313 | } |
| 2314 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2315 | /// getTagDeclType - Return the unique reference to the type for the |
| 2316 | /// specified TagDecl (struct/union/class/enum) decl. |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 2317 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) { |
Ted Kremenek | d778f88 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 2318 | assert (Decl); |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 2319 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 2320 | // away const? mutable? |
| 2321 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2322 | } |
| 2323 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2324 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 2325 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 2326 | /// needs to agree with the definition in <stddef.h>. |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 2327 | CanQualType ASTContext::getSizeType() const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2328 | return getFromTargetType(Target.getSizeType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2329 | } |
| 2330 | |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 2331 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 2332 | /// Used when in C++, as a GCC extension. |
| 2333 | QualType ASTContext::getSignedWCharType() const { |
| 2334 | // FIXME: derive from "Target" ? |
| 2335 | return WCharTy; |
| 2336 | } |
| 2337 | |
| 2338 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 2339 | /// Used when in C++, as a GCC extension. |
| 2340 | QualType ASTContext::getUnsignedWCharType() const { |
| 2341 | // FIXME: derive from "Target" ? |
| 2342 | return UnsignedIntTy; |
| 2343 | } |
| 2344 | |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 2345 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) |
| 2346 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 2347 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2348 | return getFromTargetType(Target.getPtrDiffType(0)); |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 2349 | } |
| 2350 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2351 | //===----------------------------------------------------------------------===// |
| 2352 | // Type Operators |
| 2353 | //===----------------------------------------------------------------------===// |
| 2354 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2355 | CanQualType ASTContext::getCanonicalParamType(QualType T) { |
| 2356 | // Push qualifiers into arrays, and then discard any remaining |
| 2357 | // qualifiers. |
| 2358 | T = getCanonicalType(T); |
| 2359 | const Type *Ty = T.getTypePtr(); |
| 2360 | |
| 2361 | QualType Result; |
| 2362 | if (isa<ArrayType>(Ty)) { |
| 2363 | Result = getArrayDecayedType(QualType(Ty,0)); |
| 2364 | } else if (isa<FunctionType>(Ty)) { |
| 2365 | Result = getPointerType(QualType(Ty, 0)); |
| 2366 | } else { |
| 2367 | Result = QualType(Ty, 0); |
| 2368 | } |
| 2369 | |
| 2370 | return CanQualType::CreateUnsafe(Result); |
| 2371 | } |
| 2372 | |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 2373 | /// getCanonicalType - Return the canonical (structural) type corresponding to |
| 2374 | /// the specified potentially non-canonical type. The non-canonical version |
| 2375 | /// of a type may have many "decorated" versions of types. Decorators can |
| 2376 | /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed |
| 2377 | /// to be free of any of these, allowing two canonical types to be compared |
| 2378 | /// for exact equality with a simple pointer comparison. |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2379 | CanQualType ASTContext::getCanonicalType(QualType T) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2380 | QualifierCollector Quals; |
| 2381 | const Type *Ptr = Quals.strip(T); |
| 2382 | QualType CanType = Ptr->getCanonicalTypeInternal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2383 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2384 | // The canonical internal type will be the canonical type *except* |
| 2385 | // that we push type qualifiers down through array types. |
| 2386 | |
| 2387 | // If there are no new qualifiers to push down, stop here. |
| 2388 | if (!Quals.hasQualifiers()) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2389 | return CanQualType::CreateUnsafe(CanType); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2390 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2391 | // If the type qualifiers are on an array type, get the canonical |
| 2392 | // type of the array with the qualifiers applied to the element |
| 2393 | // type. |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2394 | ArrayType *AT = dyn_cast<ArrayType>(CanType); |
| 2395 | if (!AT) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2396 | return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2397 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2398 | // Get the canonical version of the element with the extra qualifiers on it. |
| 2399 | // This can recursively sink qualifiers through multiple levels of arrays. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2400 | QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2401 | NewEltTy = getCanonicalType(NewEltTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2402 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2403 | if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2404 | return CanQualType::CreateUnsafe( |
| 2405 | getConstantArrayType(NewEltTy, CAT->getSize(), |
| 2406 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2407 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2408 | if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2409 | return CanQualType::CreateUnsafe( |
| 2410 | getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2411 | IAT->getIndexTypeCVRQualifiers())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2412 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2413 | if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2414 | return CanQualType::CreateUnsafe( |
| 2415 | getDependentSizedArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2416 | DSAT->getSizeExpr() ? |
| 2417 | DSAT->getSizeExpr()->Retain() : 0, |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2418 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2419 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 87a924e | 2009-10-30 22:56:57 +0000 | [diff] [blame] | 2420 | DSAT->getBracketsRange())->getCanonicalTypeInternal()); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2421 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2422 | VariableArrayType *VAT = cast<VariableArrayType>(AT); |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2423 | return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2424 | VAT->getSizeExpr() ? |
| 2425 | VAT->getSizeExpr()->Retain() : 0, |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2426 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2427 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2428 | VAT->getBracketsRange())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2429 | } |
| 2430 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2431 | QualType ASTContext::getUnqualifiedArrayType(QualType T, |
| 2432 | Qualifiers &Quals) { |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2433 | Quals = T.getQualifiers(); |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2434 | const ArrayType *AT = getAsArrayType(T); |
| 2435 | if (!AT) { |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2436 | return T.getUnqualifiedType(); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2437 | } |
| 2438 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2439 | QualType Elt = AT->getElementType(); |
Zhongxing Xu | c1ae0a8 | 2010-01-05 08:15:06 +0000 | [diff] [blame] | 2440 | QualType UnqualElt = getUnqualifiedArrayType(Elt, Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2441 | if (Elt == UnqualElt) |
| 2442 | return T; |
| 2443 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2444 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2445 | return getConstantArrayType(UnqualElt, CAT->getSize(), |
| 2446 | CAT->getSizeModifier(), 0); |
| 2447 | } |
| 2448 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2449 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2450 | return getIncompleteArrayType(UnqualElt, IAT->getSizeModifier(), 0); |
| 2451 | } |
| 2452 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2453 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) { |
| 2454 | return getVariableArrayType(UnqualElt, |
| 2455 | VAT->getSizeExpr() ? |
| 2456 | VAT->getSizeExpr()->Retain() : 0, |
| 2457 | VAT->getSizeModifier(), |
| 2458 | VAT->getIndexTypeCVRQualifiers(), |
| 2459 | VAT->getBracketsRange()); |
| 2460 | } |
| 2461 | |
| 2462 | const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2463 | return getDependentSizedArrayType(UnqualElt, DSAT->getSizeExpr()->Retain(), |
| 2464 | DSAT->getSizeModifier(), 0, |
| 2465 | SourceRange()); |
| 2466 | } |
| 2467 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2468 | /// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that |
| 2469 | /// may be similar (C++ 4.4), replaces T1 and T2 with the type that |
| 2470 | /// they point to and return true. If T1 and T2 aren't pointer types |
| 2471 | /// or pointer-to-member types, or if they are not similar at this |
| 2472 | /// level, returns false and leaves T1 and T2 unchanged. Top-level |
| 2473 | /// qualifiers on T1 and T2 are ignored. This function will typically |
| 2474 | /// be called in a loop that successively "unwraps" pointer and |
| 2475 | /// pointer-to-member types to compare them at each level. |
| 2476 | bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) { |
| 2477 | const PointerType *T1PtrType = T1->getAs<PointerType>(), |
| 2478 | *T2PtrType = T2->getAs<PointerType>(); |
| 2479 | if (T1PtrType && T2PtrType) { |
| 2480 | T1 = T1PtrType->getPointeeType(); |
| 2481 | T2 = T2PtrType->getPointeeType(); |
| 2482 | return true; |
| 2483 | } |
| 2484 | |
| 2485 | const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(), |
| 2486 | *T2MPType = T2->getAs<MemberPointerType>(); |
| 2487 | if (T1MPType && T2MPType && |
| 2488 | hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0), |
| 2489 | QualType(T2MPType->getClass(), 0))) { |
| 2490 | T1 = T1MPType->getPointeeType(); |
| 2491 | T2 = T2MPType->getPointeeType(); |
| 2492 | return true; |
| 2493 | } |
| 2494 | |
| 2495 | if (getLangOptions().ObjC1) { |
| 2496 | const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(), |
| 2497 | *T2OPType = T2->getAs<ObjCObjectPointerType>(); |
| 2498 | if (T1OPType && T2OPType) { |
| 2499 | T1 = T1OPType->getPointeeType(); |
| 2500 | T2 = T2OPType->getPointeeType(); |
| 2501 | return true; |
| 2502 | } |
| 2503 | } |
| 2504 | |
| 2505 | // FIXME: Block pointers, too? |
| 2506 | |
| 2507 | return false; |
| 2508 | } |
| 2509 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2510 | DeclarationNameInfo ASTContext::getNameForTemplate(TemplateName Name, |
| 2511 | SourceLocation NameLoc) { |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2512 | if (TemplateDecl *TD = Name.getAsTemplateDecl()) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2513 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 2514 | return DeclarationNameInfo(TD->getDeclName(), NameLoc); |
| 2515 | |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2516 | if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2517 | DeclarationName DName; |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2518 | if (DTN->isIdentifier()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2519 | DName = DeclarationNames.getIdentifier(DTN->getIdentifier()); |
| 2520 | return DeclarationNameInfo(DName, NameLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2521 | } else { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2522 | DName = DeclarationNames.getCXXOperatorName(DTN->getOperator()); |
| 2523 | // DNInfo work in progress: FIXME: source locations? |
| 2524 | DeclarationNameLoc DNLoc; |
| 2525 | DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding(); |
| 2526 | DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding(); |
| 2527 | return DeclarationNameInfo(DName, NameLoc, DNLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2528 | } |
| 2529 | } |
| 2530 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 2531 | OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate(); |
| 2532 | assert(Storage); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2533 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 2534 | return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2535 | } |
| 2536 | |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2537 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) { |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 2538 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 2539 | if (TemplateTemplateParmDecl *TTP |
| 2540 | = dyn_cast<TemplateTemplateParmDecl>(Template)) |
| 2541 | Template = getCanonicalTemplateTemplateParmDecl(TTP); |
| 2542 | |
| 2543 | // The canonical template name is the canonical template declaration. |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 2544 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 2545 | } |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2546 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 2547 | assert(!Name.getAsOverloadedTemplate()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2548 | |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2549 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 2550 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 2551 | return DTN->CanonicalTemplateName; |
| 2552 | } |
| 2553 | |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 2554 | bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) { |
| 2555 | X = getCanonicalTemplateName(X); |
| 2556 | Y = getCanonicalTemplateName(Y); |
| 2557 | return X.getAsVoidPointer() == Y.getAsVoidPointer(); |
| 2558 | } |
| 2559 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2560 | TemplateArgument |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2561 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) { |
| 2562 | switch (Arg.getKind()) { |
| 2563 | case TemplateArgument::Null: |
| 2564 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2565 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2566 | case TemplateArgument::Expression: |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2567 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2568 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2569 | case TemplateArgument::Declaration: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2570 | return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2571 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2572 | case TemplateArgument::Template: |
| 2573 | return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate())); |
| 2574 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2575 | case TemplateArgument::Integral: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2576 | return TemplateArgument(*Arg.getAsIntegral(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2577 | getCanonicalType(Arg.getIntegralType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2578 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2579 | case TemplateArgument::Type: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2580 | return TemplateArgument(getCanonicalType(Arg.getAsType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2581 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2582 | case TemplateArgument::Pack: { |
| 2583 | // FIXME: Allocate in ASTContext |
| 2584 | TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()]; |
| 2585 | unsigned Idx = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2586 | for (TemplateArgument::pack_iterator A = Arg.pack_begin(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2587 | AEnd = Arg.pack_end(); |
| 2588 | A != AEnd; (void)++A, ++Idx) |
| 2589 | CanonArgs[Idx] = getCanonicalTemplateArgument(*A); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2590 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2591 | TemplateArgument Result; |
| 2592 | Result.setArgumentPack(CanonArgs, Arg.pack_size(), false); |
| 2593 | return Result; |
| 2594 | } |
| 2595 | } |
| 2596 | |
| 2597 | // Silence GCC warning |
| 2598 | assert(false && "Unhandled template argument kind"); |
| 2599 | return TemplateArgument(); |
| 2600 | } |
| 2601 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2602 | NestedNameSpecifier * |
| 2603 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2604 | if (!NNS) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2605 | return 0; |
| 2606 | |
| 2607 | switch (NNS->getKind()) { |
| 2608 | case NestedNameSpecifier::Identifier: |
| 2609 | // Canonicalize the prefix but keep the identifier the same. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2610 | return NestedNameSpecifier::Create(*this, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2611 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 2612 | NNS->getAsIdentifier()); |
| 2613 | |
| 2614 | case NestedNameSpecifier::Namespace: |
| 2615 | // A namespace is canonical; build a nested-name-specifier with |
| 2616 | // this namespace and no prefix. |
| 2617 | return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace()); |
| 2618 | |
| 2619 | case NestedNameSpecifier::TypeSpec: |
| 2620 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 2621 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2622 | return NestedNameSpecifier::Create(*this, 0, |
| 2623 | NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2624 | T.getTypePtr()); |
| 2625 | } |
| 2626 | |
| 2627 | case NestedNameSpecifier::Global: |
| 2628 | // The global specifier is canonical and unique. |
| 2629 | return NNS; |
| 2630 | } |
| 2631 | |
| 2632 | // Required to silence a GCC warning |
| 2633 | return 0; |
| 2634 | } |
| 2635 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2636 | |
| 2637 | const ArrayType *ASTContext::getAsArrayType(QualType T) { |
| 2638 | // Handle the non-qualified case efficiently. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2639 | if (!T.hasLocalQualifiers()) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2640 | // Handle the common positive case fast. |
| 2641 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 2642 | return AT; |
| 2643 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2644 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2645 | // Handle the common negative case fast. |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2646 | QualType CType = T->getCanonicalTypeInternal(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2647 | if (!isa<ArrayType>(CType)) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2648 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2649 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2650 | // Apply any qualifiers from the array type to the element type. This |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2651 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 2652 | // 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] | 2653 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2654 | // If we get here, we either have type qualifiers on the type, or we have |
| 2655 | // 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] | 2656 | // we must propagate them down into the element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2657 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2658 | QualifierCollector Qs; |
| 2659 | const Type *Ty = Qs.strip(T.getDesugaredType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2660 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2661 | // If we have a simple case, just return now. |
| 2662 | const ArrayType *ATy = dyn_cast<ArrayType>(Ty); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2663 | if (ATy == 0 || Qs.empty()) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2664 | return ATy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2665 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2666 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 2667 | // qualifiers into the array element type and return a new array type. |
| 2668 | // Get the canonical version of the element with the extra qualifiers on it. |
| 2669 | // This can recursively sink qualifiers through multiple levels of arrays. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2670 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2671 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2672 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 2673 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 2674 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2675 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2676 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 2677 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 2678 | IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2679 | IAT->getIndexTypeCVRQualifiers())); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2680 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2681 | if (const DependentSizedArrayType *DSAT |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2682 | = dyn_cast<DependentSizedArrayType>(ATy)) |
| 2683 | return cast<ArrayType>( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2684 | getDependentSizedArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2685 | DSAT->getSizeExpr() ? |
| 2686 | DSAT->getSizeExpr()->Retain() : 0, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2687 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2688 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2689 | DSAT->getBracketsRange())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2690 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2691 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2692 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2693 | VAT->getSizeExpr() ? |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2694 | VAT->getSizeExpr()->Retain() : 0, |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2695 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2696 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2697 | VAT->getBracketsRange())); |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 2698 | } |
| 2699 | |
| 2700 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2701 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 2702 | /// specified array type to a pointer. This operation is non-trivial when |
| 2703 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 2704 | /// this returns a pointer to a properly qualified element of the array. |
| 2705 | /// |
| 2706 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
| 2707 | QualType ASTContext::getArrayDecayedType(QualType Ty) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2708 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 2709 | // typedefs in the element type of the array. This also handles propagation |
| 2710 | // of type qualifiers from the array type into the element type if present |
| 2711 | // (C99 6.7.3p8). |
| 2712 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 2713 | assert(PrettyArrayType && "Not an array type!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2714 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2715 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2716 | |
| 2717 | // int x[restrict 4] -> int *restrict |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2718 | return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2719 | } |
| 2720 | |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2721 | QualType ASTContext::getBaseElementType(QualType QT) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2722 | QualifierCollector Qs; |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 2723 | while (const ArrayType *AT = getAsArrayType(QualType(Qs.strip(QT), 0))) |
| 2724 | QT = AT->getElementType(); |
| 2725 | return Qs.apply(QT); |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2726 | } |
| 2727 | |
Anders Carlsson | fbbce49 | 2009-09-25 01:23:32 +0000 | [diff] [blame] | 2728 | QualType ASTContext::getBaseElementType(const ArrayType *AT) { |
| 2729 | QualType ElemTy = AT->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2730 | |
Anders Carlsson | fbbce49 | 2009-09-25 01:23:32 +0000 | [diff] [blame] | 2731 | if (const ArrayType *AT = getAsArrayType(ElemTy)) |
| 2732 | return getBaseElementType(AT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2733 | |
Anders Carlsson | 6183a99 | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 2734 | return ElemTy; |
| 2735 | } |
| 2736 | |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 2737 | /// getConstantArrayElementCount - Returns number of constant array elements. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2738 | uint64_t |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 2739 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
| 2740 | uint64_t ElementCount = 1; |
| 2741 | do { |
| 2742 | ElementCount *= CA->getSize().getZExtValue(); |
| 2743 | CA = dyn_cast<ConstantArrayType>(CA->getElementType()); |
| 2744 | } while (CA); |
| 2745 | return ElementCount; |
| 2746 | } |
| 2747 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2748 | /// getFloatingRank - Return a relative rank for floating point types. |
| 2749 | /// 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] | 2750 | static FloatingRank getFloatingRank(QualType T) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2751 | if (const ComplexType *CT = T->getAs<ComplexType>()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2752 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2753 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2754 | assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"); |
| 2755 | switch (T->getAs<BuiltinType>()->getKind()) { |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2756 | default: assert(0 && "getFloatingRank(): not a floating type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2757 | case BuiltinType::Float: return FloatRank; |
| 2758 | case BuiltinType::Double: return DoubleRank; |
| 2759 | case BuiltinType::LongDouble: return LongDoubleRank; |
| 2760 | } |
| 2761 | } |
| 2762 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2763 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 2764 | /// point or a complex type (based on typeDomain/typeSize). |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 2765 | /// 'typeDomain' is a real floating point or complex type. |
| 2766 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 2767 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 2768 | QualType Domain) const { |
| 2769 | FloatingRank EltRank = getFloatingRank(Size); |
| 2770 | if (Domain->isComplexType()) { |
| 2771 | switch (EltRank) { |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 2772 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 2773 | case FloatRank: return FloatComplexTy; |
| 2774 | case DoubleRank: return DoubleComplexTy; |
| 2775 | case LongDoubleRank: return LongDoubleComplexTy; |
| 2776 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2777 | } |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 2778 | |
| 2779 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 2780 | switch (EltRank) { |
| 2781 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
| 2782 | case FloatRank: return FloatTy; |
| 2783 | case DoubleRank: return DoubleTy; |
| 2784 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 2785 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2786 | } |
| 2787 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2788 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 2789 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 2790 | /// '_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] | 2791 | /// LHS < RHS, return -1. |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2792 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) { |
| 2793 | FloatingRank LHSR = getFloatingRank(LHS); |
| 2794 | FloatingRank RHSR = getFloatingRank(RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2795 | |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2796 | if (LHSR == RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 2797 | return 0; |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2798 | if (LHSR > RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 2799 | return 1; |
| 2800 | return -1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2801 | } |
| 2802 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2803 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 2804 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 2805 | /// or if it is not canonicalized. |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2806 | unsigned ASTContext::getIntegerRank(Type *T) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2807 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2808 | if (EnumType* ET = dyn_cast<EnumType>(T)) |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 2809 | T = ET->getDecl()->getPromotionType().getTypePtr(); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2810 | |
Eli Friedman | a342675 | 2009-07-05 23:44:27 +0000 | [diff] [blame] | 2811 | if (T->isSpecificBuiltinType(BuiltinType::WChar)) |
| 2812 | T = getFromTargetType(Target.getWCharType()).getTypePtr(); |
| 2813 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2814 | if (T->isSpecificBuiltinType(BuiltinType::Char16)) |
| 2815 | T = getFromTargetType(Target.getChar16Type()).getTypePtr(); |
| 2816 | |
| 2817 | if (T->isSpecificBuiltinType(BuiltinType::Char32)) |
| 2818 | T = getFromTargetType(Target.getChar32Type()).getTypePtr(); |
| 2819 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2820 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2821 | default: assert(0 && "getIntegerRank(): not a built-in integer"); |
| 2822 | case BuiltinType::Bool: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2823 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2824 | case BuiltinType::Char_S: |
| 2825 | case BuiltinType::Char_U: |
| 2826 | case BuiltinType::SChar: |
| 2827 | case BuiltinType::UChar: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2828 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2829 | case BuiltinType::Short: |
| 2830 | case BuiltinType::UShort: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2831 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2832 | case BuiltinType::Int: |
| 2833 | case BuiltinType::UInt: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2834 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2835 | case BuiltinType::Long: |
| 2836 | case BuiltinType::ULong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2837 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2838 | case BuiltinType::LongLong: |
| 2839 | case BuiltinType::ULongLong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2840 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 2841 | case BuiltinType::Int128: |
| 2842 | case BuiltinType::UInt128: |
| 2843 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2844 | } |
| 2845 | } |
| 2846 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 2847 | /// \brief Whether this is a promotable bitfield reference according |
| 2848 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
| 2849 | /// |
| 2850 | /// \returns the type this bit-field will promote to, or NULL if no |
| 2851 | /// promotion occurs. |
| 2852 | QualType ASTContext::isPromotableBitField(Expr *E) { |
Douglas Gregor | ceafbde | 2010-05-24 20:13:53 +0000 | [diff] [blame] | 2853 | if (E->isTypeDependent() || E->isValueDependent()) |
| 2854 | return QualType(); |
| 2855 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 2856 | FieldDecl *Field = E->getBitField(); |
| 2857 | if (!Field) |
| 2858 | return QualType(); |
| 2859 | |
| 2860 | QualType FT = Field->getType(); |
| 2861 | |
| 2862 | llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this); |
| 2863 | uint64_t BitWidth = BitWidthAP.getZExtValue(); |
| 2864 | uint64_t IntSize = getTypeSize(IntTy); |
| 2865 | // GCC extension compatibility: if the bit-field size is less than or equal |
| 2866 | // to the size of int, it gets promoted no matter what its type is. |
| 2867 | // For instance, unsigned long bf : 4 gets promoted to signed int. |
| 2868 | if (BitWidth < IntSize) |
| 2869 | return IntTy; |
| 2870 | |
| 2871 | if (BitWidth == IntSize) |
| 2872 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
| 2873 | |
| 2874 | // Types bigger than int are not subject to promotions, and therefore act |
| 2875 | // like the base type. |
| 2876 | // FIXME: This doesn't quite match what gcc does, but what gcc does here |
| 2877 | // is ridiculous. |
| 2878 | return QualType(); |
| 2879 | } |
| 2880 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 2881 | /// getPromotedIntegerType - Returns the type that Promotable will |
| 2882 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
| 2883 | /// integer type. |
| 2884 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) { |
| 2885 | assert(!Promotable.isNull()); |
| 2886 | assert(Promotable->isPromotableIntegerType()); |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 2887 | if (const EnumType *ET = Promotable->getAs<EnumType>()) |
| 2888 | return ET->getDecl()->getPromotionType(); |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 2889 | if (Promotable->isSignedIntegerType()) |
| 2890 | return IntTy; |
| 2891 | uint64_t PromotableSize = getTypeSize(Promotable); |
| 2892 | uint64_t IntSize = getTypeSize(IntTy); |
| 2893 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
| 2894 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
| 2895 | } |
| 2896 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2897 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2898 | /// 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] | 2899 | /// LHS < RHS, return -1. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2900 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2901 | Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 2902 | Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2903 | if (LHSC == RHSC) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2904 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2905 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 2906 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2907 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2908 | unsigned LHSRank = getIntegerRank(LHSC); |
| 2909 | unsigned RHSRank = getIntegerRank(RHSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2910 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2911 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 2912 | if (LHSRank == RHSRank) return 0; |
| 2913 | return LHSRank > RHSRank ? 1 : -1; |
| 2914 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2915 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2916 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 2917 | if (LHSUnsigned) { |
| 2918 | // If the unsigned [LHS] type is larger, return it. |
| 2919 | if (LHSRank >= RHSRank) |
| 2920 | return 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2921 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2922 | // If the signed type can represent all values of the unsigned type, it |
| 2923 | // 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] | 2924 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2925 | return -1; |
| 2926 | } |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2927 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2928 | // If the unsigned [RHS] type is larger, return it. |
| 2929 | if (RHSRank >= LHSRank) |
| 2930 | return -1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2931 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2932 | // If the signed type can represent all values of the unsigned type, it |
| 2933 | // 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] | 2934 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2935 | return 1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2936 | } |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2937 | |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 2938 | static RecordDecl * |
| 2939 | CreateRecordDecl(ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC, |
| 2940 | SourceLocation L, IdentifierInfo *Id) { |
| 2941 | if (Ctx.getLangOptions().CPlusPlus) |
| 2942 | return CXXRecordDecl::Create(Ctx, TK, DC, L, Id); |
| 2943 | else |
| 2944 | return RecordDecl::Create(Ctx, TK, DC, L, Id); |
| 2945 | } |
| 2946 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2947 | // getCFConstantStringType - Return the type used for constant CFStrings. |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2948 | QualType ASTContext::getCFConstantStringType() { |
| 2949 | if (!CFConstantStringTypeDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2950 | CFConstantStringTypeDecl = |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2951 | CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 2952 | &Idents.get("NSConstantString")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 2953 | CFConstantStringTypeDecl->startDefinition(); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 2954 | |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 2955 | QualType FieldTypes[4]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2956 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2957 | // const int *isa; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2958 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 2959 | // int flags; |
| 2960 | FieldTypes[1] = IntTy; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2961 | // const char *str; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2962 | FieldTypes[2] = getPointerType(CharTy.withConst()); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2963 | // long length; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2964 | FieldTypes[3] = LongTy; |
| 2965 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2966 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2967 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2968 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2969 | SourceLocation(), 0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2970 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2971 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2972 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 2973 | Field->setAccess(AS_public); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2974 | CFConstantStringTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2975 | } |
| 2976 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2977 | CFConstantStringTypeDecl->completeDefinition(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2978 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2979 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2980 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 8467583 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 2981 | } |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 2982 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 2983 | void ASTContext::setCFConstantStringType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2984 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 2985 | assert(Rec && "Invalid CFConstantStringType"); |
| 2986 | CFConstantStringTypeDecl = Rec->getDecl(); |
| 2987 | } |
| 2988 | |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 2989 | // getNSConstantStringType - Return the type used for constant NSStrings. |
| 2990 | QualType ASTContext::getNSConstantStringType() { |
| 2991 | if (!NSConstantStringTypeDecl) { |
| 2992 | NSConstantStringTypeDecl = |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2993 | CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 2994 | &Idents.get("__builtin_NSString")); |
| 2995 | NSConstantStringTypeDecl->startDefinition(); |
| 2996 | |
| 2997 | QualType FieldTypes[3]; |
| 2998 | |
| 2999 | // const int *isa; |
| 3000 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
| 3001 | // const char *str; |
| 3002 | FieldTypes[1] = getPointerType(CharTy.withConst()); |
| 3003 | // unsigned int length; |
| 3004 | FieldTypes[2] = UnsignedIntTy; |
| 3005 | |
| 3006 | // Create fields |
| 3007 | for (unsigned i = 0; i < 3; ++i) { |
| 3008 | FieldDecl *Field = FieldDecl::Create(*this, NSConstantStringTypeDecl, |
| 3009 | SourceLocation(), 0, |
| 3010 | FieldTypes[i], /*TInfo=*/0, |
| 3011 | /*BitWidth=*/0, |
| 3012 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3013 | Field->setAccess(AS_public); |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 3014 | NSConstantStringTypeDecl->addDecl(Field); |
| 3015 | } |
| 3016 | |
| 3017 | NSConstantStringTypeDecl->completeDefinition(); |
| 3018 | } |
| 3019 | |
| 3020 | return getTagDeclType(NSConstantStringTypeDecl); |
| 3021 | } |
| 3022 | |
| 3023 | void ASTContext::setNSConstantStringType(QualType T) { |
| 3024 | const RecordType *Rec = T->getAs<RecordType>(); |
| 3025 | assert(Rec && "Invalid NSConstantStringType"); |
| 3026 | NSConstantStringTypeDecl = Rec->getDecl(); |
| 3027 | } |
| 3028 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3029 | QualType ASTContext::getObjCFastEnumerationStateType() { |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3030 | if (!ObjCFastEnumerationStateTypeDecl) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3031 | ObjCFastEnumerationStateTypeDecl = |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3032 | CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3033 | &Idents.get("__objcFastEnumerationState")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3034 | ObjCFastEnumerationStateTypeDecl->startDefinition(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3035 | |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3036 | QualType FieldTypes[] = { |
| 3037 | UnsignedLongTy, |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3038 | getPointerType(ObjCIdTypedefType), |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3039 | getPointerType(UnsignedLongTy), |
| 3040 | getConstantArrayType(UnsignedLongTy, |
| 3041 | llvm::APInt(32, 5), ArrayType::Normal, 0) |
| 3042 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3043 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3044 | for (size_t i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3045 | FieldDecl *Field = FieldDecl::Create(*this, |
| 3046 | ObjCFastEnumerationStateTypeDecl, |
| 3047 | SourceLocation(), 0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3048 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3049 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 3050 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3051 | Field->setAccess(AS_public); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3052 | ObjCFastEnumerationStateTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3053 | } |
Fariborz Jahanian | 38c9ab8 | 2010-05-27 16:05:06 +0000 | [diff] [blame] | 3054 | if (getLangOptions().CPlusPlus) |
Fariborz Jahanian | 81148e9 | 2010-05-27 16:35:00 +0000 | [diff] [blame] | 3055 | if (CXXRecordDecl *CXXRD = |
| 3056 | dyn_cast<CXXRecordDecl>(ObjCFastEnumerationStateTypeDecl)) |
Fariborz Jahanian | 38c9ab8 | 2010-05-27 16:05:06 +0000 | [diff] [blame] | 3057 | CXXRD->setEmpty(false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3058 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3059 | ObjCFastEnumerationStateTypeDecl->completeDefinition(); |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3060 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3061 | |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3062 | return getTagDeclType(ObjCFastEnumerationStateTypeDecl); |
| 3063 | } |
| 3064 | |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3065 | QualType ASTContext::getBlockDescriptorType() { |
| 3066 | if (BlockDescriptorType) |
| 3067 | return getTagDeclType(BlockDescriptorType); |
| 3068 | |
| 3069 | RecordDecl *T; |
| 3070 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3071 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3072 | &Idents.get("__block_descriptor")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3073 | T->startDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3074 | |
| 3075 | QualType FieldTypes[] = { |
| 3076 | UnsignedLongTy, |
| 3077 | UnsignedLongTy, |
| 3078 | }; |
| 3079 | |
| 3080 | const char *FieldNames[] = { |
| 3081 | "reserved", |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3082 | "Size" |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3083 | }; |
| 3084 | |
| 3085 | for (size_t i = 0; i < 2; ++i) { |
| 3086 | FieldDecl *Field = FieldDecl::Create(*this, |
| 3087 | T, |
| 3088 | SourceLocation(), |
| 3089 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3090 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3091 | /*BitWidth=*/0, |
| 3092 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3093 | Field->setAccess(AS_public); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3094 | T->addDecl(Field); |
| 3095 | } |
| 3096 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3097 | T->completeDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3098 | |
| 3099 | BlockDescriptorType = T; |
| 3100 | |
| 3101 | return getTagDeclType(BlockDescriptorType); |
| 3102 | } |
| 3103 | |
| 3104 | void ASTContext::setBlockDescriptorType(QualType T) { |
| 3105 | const RecordType *Rec = T->getAs<RecordType>(); |
| 3106 | assert(Rec && "Invalid BlockDescriptorType"); |
| 3107 | BlockDescriptorType = Rec->getDecl(); |
| 3108 | } |
| 3109 | |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3110 | QualType ASTContext::getBlockDescriptorExtendedType() { |
| 3111 | if (BlockDescriptorExtendedType) |
| 3112 | return getTagDeclType(BlockDescriptorExtendedType); |
| 3113 | |
| 3114 | RecordDecl *T; |
| 3115 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3116 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3117 | &Idents.get("__block_descriptor_withcopydispose")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3118 | T->startDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3119 | |
| 3120 | QualType FieldTypes[] = { |
| 3121 | UnsignedLongTy, |
| 3122 | UnsignedLongTy, |
| 3123 | getPointerType(VoidPtrTy), |
| 3124 | getPointerType(VoidPtrTy) |
| 3125 | }; |
| 3126 | |
| 3127 | const char *FieldNames[] = { |
| 3128 | "reserved", |
| 3129 | "Size", |
| 3130 | "CopyFuncPtr", |
| 3131 | "DestroyFuncPtr" |
| 3132 | }; |
| 3133 | |
| 3134 | for (size_t i = 0; i < 4; ++i) { |
| 3135 | FieldDecl *Field = FieldDecl::Create(*this, |
| 3136 | T, |
| 3137 | SourceLocation(), |
| 3138 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3139 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3140 | /*BitWidth=*/0, |
| 3141 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3142 | Field->setAccess(AS_public); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3143 | T->addDecl(Field); |
| 3144 | } |
| 3145 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3146 | T->completeDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3147 | |
| 3148 | BlockDescriptorExtendedType = T; |
| 3149 | |
| 3150 | return getTagDeclType(BlockDescriptorExtendedType); |
| 3151 | } |
| 3152 | |
| 3153 | void ASTContext::setBlockDescriptorExtendedType(QualType T) { |
| 3154 | const RecordType *Rec = T->getAs<RecordType>(); |
| 3155 | assert(Rec && "Invalid BlockDescriptorType"); |
| 3156 | BlockDescriptorExtendedType = Rec->getDecl(); |
| 3157 | } |
| 3158 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3159 | bool ASTContext::BlockRequiresCopying(QualType Ty) { |
| 3160 | if (Ty->isBlockPointerType()) |
| 3161 | return true; |
| 3162 | if (isObjCNSObjectType(Ty)) |
| 3163 | return true; |
| 3164 | if (Ty->isObjCObjectPointerType()) |
| 3165 | return true; |
| 3166 | return false; |
| 3167 | } |
| 3168 | |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3169 | QualType ASTContext::BuildByRefType(llvm::StringRef DeclName, QualType Ty) { |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3170 | // type = struct __Block_byref_1_X { |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3171 | // void *__isa; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3172 | // struct __Block_byref_1_X *__forwarding; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3173 | // unsigned int __flags; |
| 3174 | // unsigned int __size; |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 3175 | // void *__copy_helper; // as needed |
| 3176 | // void *__destroy_help // as needed |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3177 | // int X; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3178 | // } * |
| 3179 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3180 | bool HasCopyAndDispose = BlockRequiresCopying(Ty); |
| 3181 | |
| 3182 | // FIXME: Move up |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 3183 | llvm::SmallString<36> Name; |
| 3184 | llvm::raw_svector_ostream(Name) << "__Block_byref_" << |
| 3185 | ++UniqueBlockByRefTypeID << '_' << DeclName; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3186 | RecordDecl *T; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3187 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3188 | &Idents.get(Name.str())); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3189 | T->startDefinition(); |
| 3190 | QualType Int32Ty = IntTy; |
| 3191 | assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported"); |
| 3192 | QualType FieldTypes[] = { |
| 3193 | getPointerType(VoidPtrTy), |
| 3194 | getPointerType(getTagDeclType(T)), |
| 3195 | Int32Ty, |
| 3196 | Int32Ty, |
| 3197 | getPointerType(VoidPtrTy), |
| 3198 | getPointerType(VoidPtrTy), |
| 3199 | Ty |
| 3200 | }; |
| 3201 | |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3202 | llvm::StringRef FieldNames[] = { |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3203 | "__isa", |
| 3204 | "__forwarding", |
| 3205 | "__flags", |
| 3206 | "__size", |
| 3207 | "__copy_helper", |
| 3208 | "__destroy_helper", |
| 3209 | DeclName, |
| 3210 | }; |
| 3211 | |
| 3212 | for (size_t i = 0; i < 7; ++i) { |
| 3213 | if (!HasCopyAndDispose && i >=4 && i <= 5) |
| 3214 | continue; |
| 3215 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
| 3216 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3217 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3218 | /*BitWidth=*/0, /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3219 | Field->setAccess(AS_public); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3220 | T->addDecl(Field); |
| 3221 | } |
| 3222 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3223 | T->completeDefinition(); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3224 | |
| 3225 | return getPointerType(getTagDeclType(T)); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3226 | } |
| 3227 | |
| 3228 | |
| 3229 | QualType ASTContext::getBlockParmType( |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3230 | bool BlockHasCopyDispose, |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 3231 | llvm::SmallVectorImpl<const Expr *> &Layout) { |
| 3232 | |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3233 | // FIXME: Move up |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 3234 | llvm::SmallString<36> Name; |
| 3235 | llvm::raw_svector_ostream(Name) << "__block_literal_" |
| 3236 | << ++UniqueBlockParmTypeID; |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3237 | RecordDecl *T; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3238 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3239 | &Idents.get(Name.str())); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3240 | T->startDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3241 | QualType FieldTypes[] = { |
| 3242 | getPointerType(VoidPtrTy), |
| 3243 | IntTy, |
| 3244 | IntTy, |
| 3245 | getPointerType(VoidPtrTy), |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3246 | (BlockHasCopyDispose ? |
| 3247 | getPointerType(getBlockDescriptorExtendedType()) : |
| 3248 | getPointerType(getBlockDescriptorType())) |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3249 | }; |
| 3250 | |
| 3251 | const char *FieldNames[] = { |
| 3252 | "__isa", |
| 3253 | "__flags", |
| 3254 | "__reserved", |
| 3255 | "__FuncPtr", |
| 3256 | "__descriptor" |
| 3257 | }; |
| 3258 | |
| 3259 | for (size_t i = 0; i < 5; ++i) { |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3260 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3261 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3262 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3263 | /*BitWidth=*/0, /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3264 | Field->setAccess(AS_public); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3265 | T->addDecl(Field); |
| 3266 | } |
| 3267 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 3268 | for (unsigned i = 0; i < Layout.size(); ++i) { |
| 3269 | const Expr *E = Layout[i]; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3270 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 3271 | QualType FieldType = E->getType(); |
| 3272 | IdentifierInfo *FieldName = 0; |
| 3273 | if (isa<CXXThisExpr>(E)) { |
| 3274 | FieldName = &Idents.get("this"); |
| 3275 | } else if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E)) { |
| 3276 | const ValueDecl *D = BDRE->getDecl(); |
| 3277 | FieldName = D->getIdentifier(); |
| 3278 | if (BDRE->isByRef()) |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3279 | FieldType = BuildByRefType(D->getName(), FieldType); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 3280 | } else { |
| 3281 | // Padding. |
| 3282 | assert(isa<ConstantArrayType>(FieldType) && |
| 3283 | isa<DeclRefExpr>(E) && |
| 3284 | !cast<DeclRefExpr>(E)->getDecl()->getDeclName() && |
| 3285 | "doesn't match characteristics of padding decl"); |
| 3286 | } |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3287 | |
| 3288 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 3289 | FieldName, FieldType, /*TInfo=*/0, |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3290 | /*BitWidth=*/0, /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3291 | Field->setAccess(AS_public); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3292 | T->addDecl(Field); |
| 3293 | } |
| 3294 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3295 | T->completeDefinition(); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3296 | |
| 3297 | return getPointerType(getTagDeclType(T)); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3298 | } |
| 3299 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3300 | void ASTContext::setObjCFastEnumerationStateType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3301 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3302 | assert(Rec && "Invalid ObjCFAstEnumerationStateType"); |
| 3303 | ObjCFastEnumerationStateTypeDecl = Rec->getDecl(); |
| 3304 | } |
| 3305 | |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3306 | // This returns true if a type has been typedefed to BOOL: |
| 3307 | // typedef <type> BOOL; |
Chris Lattner | 2d99833 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 3308 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3309 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | bb49c3e | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 3310 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 3311 | return II->isStr("BOOL"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3312 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3313 | return false; |
| 3314 | } |
| 3315 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3316 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3317 | /// purpose. |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3318 | CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) { |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3319 | CharUnits sz = getTypeSizeInChars(type); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3320 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3321 | // 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] | 3322 | if (sz.isPositive() && type->isIntegralOrEnumerationType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3323 | sz = std::max(sz, getTypeSizeInChars(IntTy)); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3324 | // Treat arrays as pointers, since that's how they're passed in. |
| 3325 | else if (type->isArrayType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3326 | sz = getTypeSizeInChars(VoidPtrTy); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3327 | return sz; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3328 | } |
| 3329 | |
| 3330 | static inline |
| 3331 | std::string charUnitsToString(const CharUnits &CU) { |
| 3332 | return llvm::itostr(CU.getQuantity()); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3333 | } |
| 3334 | |
Fariborz Jahanian | 6f46c26 | 2010-04-08 18:06:22 +0000 | [diff] [blame] | 3335 | /// getObjCEncodingForBlockDecl - Return the encoded type for this block |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3336 | /// declaration. |
| 3337 | void ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr, |
| 3338 | std::string& S) { |
| 3339 | const BlockDecl *Decl = Expr->getBlockDecl(); |
| 3340 | QualType BlockTy = |
| 3341 | Expr->getType()->getAs<BlockPointerType>()->getPointeeType(); |
| 3342 | // Encode result type. |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 3343 | getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3344 | // Compute size of all parameters. |
| 3345 | // Start with computing size of a pointer in number of bytes. |
| 3346 | // FIXME: There might(should) be a better way of doing this computation! |
| 3347 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3348 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
| 3349 | CharUnits ParmOffset = PtrSize; |
Fariborz Jahanian | 6f46c26 | 2010-04-08 18:06:22 +0000 | [diff] [blame] | 3350 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3351 | E = Decl->param_end(); PI != E; ++PI) { |
| 3352 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3353 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3354 | assert (sz.isPositive() && "BlockExpr - Incomplete param type"); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3355 | ParmOffset += sz; |
| 3356 | } |
| 3357 | // Size of the argument frame |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3358 | S += charUnitsToString(ParmOffset); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3359 | // Block pointer and offset. |
| 3360 | S += "@?0"; |
| 3361 | ParmOffset = PtrSize; |
| 3362 | |
| 3363 | // Argument types. |
| 3364 | ParmOffset = PtrSize; |
| 3365 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E = |
| 3366 | Decl->param_end(); PI != E; ++PI) { |
| 3367 | ParmVarDecl *PVDecl = *PI; |
| 3368 | QualType PType = PVDecl->getOriginalType(); |
| 3369 | if (const ArrayType *AT = |
| 3370 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3371 | // Use array's original type only if it has known number of |
| 3372 | // elements. |
| 3373 | if (!isa<ConstantArrayType>(AT)) |
| 3374 | PType = PVDecl->getType(); |
| 3375 | } else if (PType->isFunctionType()) |
| 3376 | PType = PVDecl->getType(); |
| 3377 | getObjCEncodingForType(PType, S); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3378 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3379 | ParmOffset += getObjCEncodingTypeSize(PType); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3380 | } |
| 3381 | } |
| 3382 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3383 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3384 | /// declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3385 | void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Chris Lattner | e6db3b0 | 2008-11-19 07:24:05 +0000 | [diff] [blame] | 3386 | std::string& S) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3387 | // FIXME: This is not very efficient. |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3388 | // Encode type qualifer, 'in', 'inout', etc. for the return type. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3389 | getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3390 | // Encode result type. |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3391 | getObjCEncodingForType(Decl->getResultType(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3392 | // Compute size of all parameters. |
| 3393 | // Start with computing size of a pointer in number of bytes. |
| 3394 | // FIXME: There might(should) be a better way of doing this computation! |
| 3395 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3396 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3397 | // The first two arguments (self and _cmd) are pointers; account for |
| 3398 | // their size. |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3399 | CharUnits ParmOffset = 2 * PtrSize; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3400 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 3401 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3402 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3403 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3404 | assert (sz.isPositive() && |
| 3405 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3406 | ParmOffset += sz; |
| 3407 | } |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3408 | S += charUnitsToString(ParmOffset); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3409 | S += "@0:"; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3410 | S += charUnitsToString(PtrSize); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3411 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3412 | // Argument types. |
| 3413 | ParmOffset = 2 * PtrSize; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3414 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 3415 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3416 | ParmVarDecl *PVDecl = *PI; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3417 | QualType PType = PVDecl->getOriginalType(); |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 3418 | if (const ArrayType *AT = |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 3419 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3420 | // Use array's original type only if it has known number of |
| 3421 | // elements. |
Steve Naroff | bb3fde3 | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 3422 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 3423 | PType = PVDecl->getType(); |
| 3424 | } else if (PType->isFunctionType()) |
| 3425 | PType = PVDecl->getType(); |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3426 | // Process argument qualifiers for user supplied arguments; such as, |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3427 | // 'in', 'inout', etc. |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 3428 | getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3429 | getObjCEncodingForType(PType, S); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3430 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3431 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3432 | } |
| 3433 | } |
| 3434 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3435 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 3436 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3437 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 3438 | /// NULL when getting encodings for protocol properties. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3439 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 3440 | /// attributes readonly and bycopy are encoded as single characters. The |
| 3441 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 3442 | /// encoded as single characters, followed by an identifier. Property types |
| 3443 | /// are also encoded as a parametrized attribute. The characters used to encode |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 3444 | /// these attributes are defined by the following enumeration: |
| 3445 | /// @code |
| 3446 | /// enum PropertyAttributes { |
| 3447 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 3448 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 3449 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 3450 | /// kPropertyDynamic = 'D', // property is dynamic |
| 3451 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 3452 | /// kPropertySetter = 'S', // followed by setter selector name |
| 3453 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
| 3454 | /// kPropertyType = 't' // followed by old-style type encoding. |
| 3455 | /// kPropertyWeak = 'W' // 'weak' property |
| 3456 | /// kPropertyStrong = 'P' // property GC'able |
| 3457 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 3458 | /// }; |
| 3459 | /// @endcode |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3460 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3461 | const Decl *Container, |
Chris Lattner | e6db3b0 | 2008-11-19 07:24:05 +0000 | [diff] [blame] | 3462 | std::string& S) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3463 | // Collect information from the property implementation decl(s). |
| 3464 | bool Dynamic = false; |
| 3465 | ObjCPropertyImplDecl *SynthesizePID = 0; |
| 3466 | |
| 3467 | // FIXME: Duplicated code due to poor abstraction. |
| 3468 | if (Container) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3469 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3470 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
| 3471 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3472 | i = CID->propimpl_begin(), e = CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3473 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3474 | ObjCPropertyImplDecl *PID = *i; |
| 3475 | if (PID->getPropertyDecl() == PD) { |
| 3476 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 3477 | Dynamic = true; |
| 3478 | } else { |
| 3479 | SynthesizePID = PID; |
| 3480 | } |
| 3481 | } |
| 3482 | } |
| 3483 | } else { |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3484 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3485 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3486 | i = OID->propimpl_begin(), e = OID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3487 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3488 | ObjCPropertyImplDecl *PID = *i; |
| 3489 | if (PID->getPropertyDecl() == PD) { |
| 3490 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 3491 | Dynamic = true; |
| 3492 | } else { |
| 3493 | SynthesizePID = PID; |
| 3494 | } |
| 3495 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3496 | } |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3497 | } |
| 3498 | } |
| 3499 | |
| 3500 | // FIXME: This is not very efficient. |
| 3501 | S = "T"; |
| 3502 | |
| 3503 | // Encode result type. |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3504 | // GCC has some special rules regarding encoding of properties which |
| 3505 | // closely resembles encoding of ivars. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3506 | getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3507 | true /* outermost type */, |
| 3508 | true /* encoding for property */); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3509 | |
| 3510 | if (PD->isReadOnly()) { |
| 3511 | S += ",R"; |
| 3512 | } else { |
| 3513 | switch (PD->getSetterKind()) { |
| 3514 | case ObjCPropertyDecl::Assign: break; |
| 3515 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3516 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3517 | } |
| 3518 | } |
| 3519 | |
| 3520 | // It really isn't clear at all what this means, since properties |
| 3521 | // are "dynamic by default". |
| 3522 | if (Dynamic) |
| 3523 | S += ",D"; |
| 3524 | |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3525 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 3526 | S += ",N"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3527 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3528 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 3529 | S += ",G"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3530 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3531 | } |
| 3532 | |
| 3533 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 3534 | S += ",S"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3535 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3536 | } |
| 3537 | |
| 3538 | if (SynthesizePID) { |
| 3539 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 3540 | S += ",V"; |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 3541 | S += OID->getNameAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3542 | } |
| 3543 | |
| 3544 | // FIXME: OBJCGC: weak & strong |
| 3545 | } |
| 3546 | |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3547 | /// getLegacyIntegralTypeEncoding - |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3548 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
| 3549 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3550 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
| 3551 | /// |
| 3552 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3553 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3554 | if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) { |
Fariborz Jahanian | c657eba | 2009-02-11 23:59:18 +0000 | [diff] [blame] | 3555 | if (BT->getKind() == BuiltinType::ULong && |
| 3556 | ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3557 | PointeeTy = UnsignedIntTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3558 | else |
Fariborz Jahanian | c657eba | 2009-02-11 23:59:18 +0000 | [diff] [blame] | 3559 | if (BT->getKind() == BuiltinType::Long && |
| 3560 | ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3561 | PointeeTy = IntTy; |
| 3562 | } |
| 3563 | } |
| 3564 | } |
| 3565 | |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3566 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 3567 | const FieldDecl *Field) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3568 | // We follow the behavior of gcc, expanding structures which are |
| 3569 | // directly pointed to, and expanding embedded structures. Note that |
| 3570 | // these rules are sufficient to prevent recursive encoding of the |
| 3571 | // same type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3572 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
Fariborz Jahanian | 5b8c7d9 | 2008-12-22 23:22:27 +0000 | [diff] [blame] | 3573 | true /* outermost type */); |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3574 | } |
| 3575 | |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3576 | static char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) { |
| 3577 | switch (T->getAs<BuiltinType>()->getKind()) { |
| 3578 | default: assert(0 && "Unhandled builtin type kind"); |
| 3579 | case BuiltinType::Void: return 'v'; |
| 3580 | case BuiltinType::Bool: return 'B'; |
| 3581 | case BuiltinType::Char_U: |
| 3582 | case BuiltinType::UChar: return 'C'; |
| 3583 | case BuiltinType::UShort: return 'S'; |
| 3584 | case BuiltinType::UInt: return 'I'; |
| 3585 | case BuiltinType::ULong: |
| 3586 | return |
| 3587 | (const_cast<ASTContext *>(C))->getIntWidth(T) == 32 ? 'L' : 'Q'; |
| 3588 | case BuiltinType::UInt128: return 'T'; |
| 3589 | case BuiltinType::ULongLong: return 'Q'; |
| 3590 | case BuiltinType::Char_S: |
| 3591 | case BuiltinType::SChar: return 'c'; |
| 3592 | case BuiltinType::Short: return 's'; |
John McCall | 24da709 | 2010-06-11 10:11:05 +0000 | [diff] [blame] | 3593 | case BuiltinType::WChar: |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3594 | case BuiltinType::Int: return 'i'; |
| 3595 | case BuiltinType::Long: |
| 3596 | return |
| 3597 | (const_cast<ASTContext *>(C))->getIntWidth(T) == 32 ? 'l' : 'q'; |
| 3598 | case BuiltinType::LongLong: return 'q'; |
| 3599 | case BuiltinType::Int128: return 't'; |
| 3600 | case BuiltinType::Float: return 'f'; |
| 3601 | case BuiltinType::Double: return 'd'; |
| 3602 | case BuiltinType::LongDouble: return 'd'; |
| 3603 | } |
| 3604 | } |
| 3605 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3606 | static void EncodeBitField(const ASTContext *Context, std::string& S, |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3607 | QualType T, const FieldDecl *FD) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3608 | const Expr *E = FD->getBitWidth(); |
| 3609 | assert(E && "bitfield width not there - getObjCEncodingForTypeImpl"); |
| 3610 | ASTContext *Ctx = const_cast<ASTContext*>(Context); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3611 | S += 'b'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3612 | // The NeXT runtime encodes bit fields as b followed by the number of bits. |
| 3613 | // The GNU runtime requires more information; bitfields are encoded as b, |
| 3614 | // then the offset (in bits) of the first element, then the type of the |
| 3615 | // bitfield, then the size in bits. For example, in this structure: |
| 3616 | // |
| 3617 | // struct |
| 3618 | // { |
| 3619 | // int integer; |
| 3620 | // int flags:2; |
| 3621 | // }; |
| 3622 | // On a 32-bit system, the encoding for flags would be b2 for the NeXT |
| 3623 | // runtime, but b32i2 for the GNU runtime. The reason for this extra |
| 3624 | // information is not especially sensible, but we're stuck with it for |
| 3625 | // compatibility with GCC, although providing it breaks anything that |
| 3626 | // actually uses runtime introspection and wants to work on both runtimes... |
| 3627 | if (!Ctx->getLangOptions().NeXTRuntime) { |
| 3628 | const RecordDecl *RD = FD->getParent(); |
| 3629 | const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD); |
| 3630 | // FIXME: This same linear search is also used in ExprConstant - it might |
| 3631 | // be better if the FieldDecl stored its offset. We'd be increasing the |
| 3632 | // size of the object slightly, but saving some time every time it is used. |
| 3633 | unsigned i = 0; |
| 3634 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 3635 | FieldEnd = RD->field_end(); |
| 3636 | Field != FieldEnd; (void)++Field, ++i) { |
| 3637 | if (*Field == FD) |
| 3638 | break; |
| 3639 | } |
| 3640 | S += llvm::utostr(RL.getFieldOffset(i)); |
| 3641 | S += ObjCEncodingForPrimitiveKind(Context, T); |
| 3642 | } |
| 3643 | unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue(); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3644 | S += llvm::utostr(N); |
| 3645 | } |
| 3646 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 3647 | // FIXME: Use SmallString for accumulating string. |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3648 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 3649 | bool ExpandPointedToStructures, |
| 3650 | bool ExpandStructures, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 3651 | const FieldDecl *FD, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3652 | bool OutermostType, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3653 | bool EncodingProperty) { |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3654 | if (T->getAs<BuiltinType>()) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3655 | if (FD && FD->isBitField()) |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3656 | return EncodeBitField(this, S, T, FD); |
| 3657 | S += ObjCEncodingForPrimitiveKind(this, T); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3658 | return; |
| 3659 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3660 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3661 | if (const ComplexType *CT = T->getAs<ComplexType>()) { |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 3662 | S += 'j'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3663 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false, |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 3664 | false); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3665 | return; |
| 3666 | } |
Fariborz Jahanian | 60bce3e | 2009-11-23 20:40:50 +0000 | [diff] [blame] | 3667 | |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 3668 | // encoding for pointer or r3eference types. |
| 3669 | QualType PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3670 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Fariborz Jahanian | 8d2c0a9 | 2009-11-30 18:43:52 +0000 | [diff] [blame] | 3671 | if (PT->isObjCSelType()) { |
| 3672 | S += ':'; |
| 3673 | return; |
| 3674 | } |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 3675 | PointeeTy = PT->getPointeeType(); |
| 3676 | } |
| 3677 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 3678 | PointeeTy = RT->getPointeeType(); |
| 3679 | if (!PointeeTy.isNull()) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3680 | bool isReadOnly = false; |
| 3681 | // For historical/compatibility reasons, the read-only qualifier of the |
| 3682 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 3683 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3684 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3685 | if (isa<TypedefType>(T.getTypePtr())) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3686 | if (OutermostType && T.isConstQualified()) { |
| 3687 | isReadOnly = true; |
| 3688 | S += 'r'; |
| 3689 | } |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 3690 | } else if (OutermostType) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3691 | QualType P = PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3692 | while (P->getAs<PointerType>()) |
| 3693 | P = P->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3694 | if (P.isConstQualified()) { |
| 3695 | isReadOnly = true; |
| 3696 | S += 'r'; |
| 3697 | } |
| 3698 | } |
| 3699 | if (isReadOnly) { |
| 3700 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 3701 | // combinations need to be rearranged. |
| 3702 | // Rewrite "in const" from "nr" to "rn" |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 3703 | if (llvm::StringRef(S).endswith("nr")) |
| 3704 | S.replace(S.end()-2, S.end(), "rn"); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3705 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3706 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3707 | if (PointeeTy->isCharType()) { |
| 3708 | // char pointer types should be encoded as '*' unless it is a |
| 3709 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3710 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3711 | S += '*'; |
| 3712 | return; |
| 3713 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3714 | } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) { |
Steve Naroff | 9533a7f | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 3715 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 3716 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 3717 | S += '#'; |
| 3718 | return; |
| 3719 | } |
| 3720 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 3721 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 3722 | S += '@'; |
| 3723 | return; |
| 3724 | } |
| 3725 | // fall through... |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3726 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3727 | S += '^'; |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3728 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 3729 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3730 | getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3731 | NULL); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3732 | return; |
| 3733 | } |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 3734 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3735 | if (const ArrayType *AT = |
| 3736 | // Ignore type qualifiers etc. |
| 3737 | dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3738 | if (isa<IncompleteArrayType>(AT)) { |
| 3739 | // Incomplete arrays are encoded as a pointer to the array element. |
| 3740 | S += '^'; |
| 3741 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3742 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3743 | false, ExpandStructures, FD); |
| 3744 | } else { |
| 3745 | S += '['; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3746 | |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3747 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 3748 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 3749 | else { |
| 3750 | //Variable length arrays are encoded as a regular array with 0 elements. |
| 3751 | assert(isa<VariableArrayType>(AT) && "Unknown array type!"); |
| 3752 | S += '0'; |
| 3753 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3754 | |
| 3755 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3756 | false, ExpandStructures, FD); |
| 3757 | S += ']'; |
| 3758 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3759 | return; |
| 3760 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3761 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3762 | if (T->getAs<FunctionType>()) { |
Anders Carlsson | c0a87b7 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 3763 | S += '?'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3764 | return; |
| 3765 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3766 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3767 | if (const RecordType *RTy = T->getAs<RecordType>()) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3768 | RecordDecl *RDecl = RTy->getDecl(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3769 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 3770 | // Anonymous structures print as '?' |
| 3771 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 3772 | S += II->getName(); |
Fariborz Jahanian | 6fb9439 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 3773 | if (ClassTemplateSpecializationDecl *Spec |
| 3774 | = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) { |
| 3775 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 3776 | std::string TemplateArgsStr |
| 3777 | = TemplateSpecializationType::PrintTemplateArgumentList( |
| 3778 | TemplateArgs.getFlatArgumentList(), |
| 3779 | TemplateArgs.flat_size(), |
| 3780 | (*this).PrintingPolicy); |
| 3781 | |
| 3782 | S += TemplateArgsStr; |
| 3783 | } |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 3784 | } else { |
| 3785 | S += '?'; |
| 3786 | } |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3787 | if (ExpandStructures) { |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3788 | S += '='; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3789 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 3790 | FieldEnd = RDecl->field_end(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3791 | Field != FieldEnd; ++Field) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3792 | if (FD) { |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3793 | S += '"'; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3794 | S += Field->getNameAsString(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3795 | S += '"'; |
| 3796 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3797 | |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3798 | // Special case bit-fields. |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3799 | if (Field->isBitField()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3800 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3801 | (*Field)); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3802 | } else { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3803 | QualType qt = Field->getType(); |
| 3804 | getLegacyIntegralTypeEncoding(qt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3805 | getObjCEncodingForTypeImpl(qt, S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3806 | FD); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3807 | } |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3808 | } |
Fariborz Jahanian | 6de88a8 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 3809 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3810 | S += RDecl->isUnion() ? ')' : '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3811 | return; |
| 3812 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3813 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3814 | if (T->isEnumeralType()) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3815 | if (FD && FD->isBitField()) |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3816 | EncodeBitField(this, S, T, FD); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3817 | else |
| 3818 | S += 'i'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3819 | return; |
| 3820 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3821 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3822 | if (T->isBlockPointerType()) { |
Steve Naroff | 21a98b1 | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 3823 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3824 | return; |
| 3825 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3826 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3827 | // Ignore protocol qualifiers when mangling at this level. |
| 3828 | if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>()) |
| 3829 | T = OT->getBaseType(); |
| 3830 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3831 | if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3832 | // @encode(class_name) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3833 | ObjCInterfaceDecl *OI = OIT->getDecl(); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3834 | S += '{'; |
| 3835 | const IdentifierInfo *II = OI->getIdentifier(); |
| 3836 | S += II->getName(); |
| 3837 | S += '='; |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 3838 | llvm::SmallVector<ObjCIvarDecl*, 32> Ivars; |
| 3839 | DeepCollectObjCIvars(OI, true, Ivars); |
| 3840 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) { |
| 3841 | FieldDecl *Field = cast<FieldDecl>(Ivars[i]); |
| 3842 | if (Field->isBitField()) |
| 3843 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3844 | else |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 3845 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3846 | } |
| 3847 | S += '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3848 | return; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3849 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3850 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3851 | if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3852 | if (OPT->isObjCIdType()) { |
| 3853 | S += '@'; |
| 3854 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3855 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3856 | |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 3857 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
| 3858 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
| 3859 | // Since this is a binary compatibility issue, need to consult with runtime |
| 3860 | // folks. Fortunately, this is a *very* obsure construct. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3861 | S += '#'; |
| 3862 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3863 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3864 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3865 | if (OPT->isObjCQualifiedIdType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3866 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3867 | ExpandPointedToStructures, |
| 3868 | ExpandStructures, FD); |
| 3869 | if (FD || EncodingProperty) { |
| 3870 | // Note that we do extended encoding of protocol qualifer list |
| 3871 | // Only when doing ivar or property encoding. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3872 | S += '"'; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3873 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 3874 | E = OPT->qual_end(); I != E; ++I) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3875 | S += '<'; |
| 3876 | S += (*I)->getNameAsString(); |
| 3877 | S += '>'; |
| 3878 | } |
| 3879 | S += '"'; |
| 3880 | } |
| 3881 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3882 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3883 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3884 | QualType PointeeTy = OPT->getPointeeType(); |
| 3885 | if (!EncodingProperty && |
| 3886 | isa<TypedefType>(PointeeTy.getTypePtr())) { |
| 3887 | // Another historical/compatibility reason. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3888 | // We encode the underlying type which comes out as |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3889 | // {...}; |
| 3890 | S += '^'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3891 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 3892 | false, ExpandPointedToStructures, |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3893 | NULL); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3894 | return; |
| 3895 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3896 | |
| 3897 | S += '@'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 3898 | if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3899 | S += '"'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 3900 | S += OPT->getInterfaceDecl()->getIdentifier()->getName(); |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3901 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 3902 | E = OPT->qual_end(); I != E; ++I) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3903 | S += '<'; |
| 3904 | S += (*I)->getNameAsString(); |
| 3905 | S += '>'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3906 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3907 | S += '"'; |
| 3908 | } |
| 3909 | return; |
| 3910 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3911 | |
John McCall | 532ec7b | 2010-05-17 23:56:34 +0000 | [diff] [blame] | 3912 | // gcc just blithely ignores member pointers. |
| 3913 | // TODO: maybe there should be a mangling for these |
| 3914 | if (T->getAs<MemberPointerType>()) |
| 3915 | return; |
| 3916 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3917 | assert(0 && "@encode for type not implemented!"); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3918 | } |
| 3919 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3920 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3921 | std::string& S) const { |
| 3922 | if (QT & Decl::OBJC_TQ_In) |
| 3923 | S += 'n'; |
| 3924 | if (QT & Decl::OBJC_TQ_Inout) |
| 3925 | S += 'N'; |
| 3926 | if (QT & Decl::OBJC_TQ_Out) |
| 3927 | S += 'o'; |
| 3928 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 3929 | S += 'O'; |
| 3930 | if (QT & Decl::OBJC_TQ_Byref) |
| 3931 | S += 'R'; |
| 3932 | if (QT & Decl::OBJC_TQ_Oneway) |
| 3933 | S += 'V'; |
| 3934 | } |
| 3935 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3936 | void ASTContext::setBuiltinVaListType(QualType T) { |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 3937 | assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3938 | |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 3939 | BuiltinVaListType = T; |
| 3940 | } |
| 3941 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3942 | void ASTContext::setObjCIdType(QualType T) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3943 | ObjCIdTypedefType = T; |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 3944 | } |
| 3945 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3946 | void ASTContext::setObjCSelType(QualType T) { |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 3947 | ObjCSelTypedefType = T; |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 3948 | } |
| 3949 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3950 | void ASTContext::setObjCProtoType(QualType QT) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3951 | ObjCProtoType = QT; |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 3952 | } |
| 3953 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3954 | void ASTContext::setObjCClassType(QualType T) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3955 | ObjCClassTypedefType = T; |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 3956 | } |
| 3957 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3958 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3959 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 3960 | "'NSConstantString' type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3961 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3962 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 3963 | } |
| 3964 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 3965 | /// \brief Retrieve the template name that corresponds to a non-empty |
| 3966 | /// lookup. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3967 | TemplateName ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin, |
| 3968 | UnresolvedSetIterator End) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 3969 | unsigned size = End - Begin; |
| 3970 | assert(size > 1 && "set is not overloaded!"); |
| 3971 | |
| 3972 | void *memory = Allocate(sizeof(OverloadedTemplateStorage) + |
| 3973 | size * sizeof(FunctionTemplateDecl*)); |
| 3974 | OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size); |
| 3975 | |
| 3976 | NamedDecl **Storage = OT->getStorage(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3977 | for (UnresolvedSetIterator I = Begin; I != End; ++I) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 3978 | NamedDecl *D = *I; |
| 3979 | assert(isa<FunctionTemplateDecl>(D) || |
| 3980 | (isa<UsingShadowDecl>(D) && |
| 3981 | isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))); |
| 3982 | *Storage++ = D; |
| 3983 | } |
| 3984 | |
| 3985 | return TemplateName(OT); |
| 3986 | } |
| 3987 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3988 | /// \brief Retrieve the template name that represents a qualified |
| 3989 | /// template name such as \c std::vector. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3990 | TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3991 | bool TemplateKeyword, |
| 3992 | TemplateDecl *Template) { |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3993 | // FIXME: Canonicalization? |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3994 | llvm::FoldingSetNodeID ID; |
| 3995 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 3996 | |
| 3997 | void *InsertPos = 0; |
| 3998 | QualifiedTemplateName *QTN = |
| 3999 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4000 | if (!QTN) { |
| 4001 | QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); |
| 4002 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 4003 | } |
| 4004 | |
| 4005 | return TemplateName(QTN); |
| 4006 | } |
| 4007 | |
| 4008 | /// \brief Retrieve the template name that represents a dependent |
| 4009 | /// template name such as \c MetaFun::template apply. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4010 | TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4011 | const IdentifierInfo *Name) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4012 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 4013 | "Nested name specifier must be dependent"); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4014 | |
| 4015 | llvm::FoldingSetNodeID ID; |
| 4016 | DependentTemplateName::Profile(ID, NNS, Name); |
| 4017 | |
| 4018 | void *InsertPos = 0; |
| 4019 | DependentTemplateName *QTN = |
| 4020 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4021 | |
| 4022 | if (QTN) |
| 4023 | return TemplateName(QTN); |
| 4024 | |
| 4025 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 4026 | if (CanonNNS == NNS) { |
| 4027 | QTN = new (*this,4) DependentTemplateName(NNS, Name); |
| 4028 | } else { |
| 4029 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
| 4030 | QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4031 | DependentTemplateName *CheckQTN = |
| 4032 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4033 | assert(!CheckQTN && "Dependent type name canonicalization broken"); |
| 4034 | (void)CheckQTN; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4035 | } |
| 4036 | |
| 4037 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 4038 | return TemplateName(QTN); |
| 4039 | } |
| 4040 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4041 | /// \brief Retrieve the template name that represents a dependent |
| 4042 | /// template name such as \c MetaFun::template operator+. |
| 4043 | TemplateName |
| 4044 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
| 4045 | OverloadedOperatorKind Operator) { |
| 4046 | assert((!NNS || NNS->isDependent()) && |
| 4047 | "Nested name specifier must be dependent"); |
| 4048 | |
| 4049 | llvm::FoldingSetNodeID ID; |
| 4050 | DependentTemplateName::Profile(ID, NNS, Operator); |
| 4051 | |
| 4052 | void *InsertPos = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4053 | DependentTemplateName *QTN |
| 4054 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4055 | |
| 4056 | if (QTN) |
| 4057 | return TemplateName(QTN); |
| 4058 | |
| 4059 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 4060 | if (CanonNNS == NNS) { |
| 4061 | QTN = new (*this,4) DependentTemplateName(NNS, Operator); |
| 4062 | } else { |
| 4063 | TemplateName Canon = getDependentTemplateName(CanonNNS, Operator); |
| 4064 | QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4065 | |
| 4066 | DependentTemplateName *CheckQTN |
| 4067 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4068 | assert(!CheckQTN && "Dependent template name canonicalization broken"); |
| 4069 | (void)CheckQTN; |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4070 | } |
| 4071 | |
| 4072 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 4073 | return TemplateName(QTN); |
| 4074 | } |
| 4075 | |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4076 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | d934112 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 4077 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 4078 | /// is actually a value of type @c TargetInfo::IntType. |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 4079 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4080 | switch (Type) { |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 4081 | case TargetInfo::NoInt: return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4082 | case TargetInfo::SignedShort: return ShortTy; |
| 4083 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 4084 | case TargetInfo::SignedInt: return IntTy; |
| 4085 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 4086 | case TargetInfo::SignedLong: return LongTy; |
| 4087 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 4088 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 4089 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 4090 | } |
| 4091 | |
| 4092 | assert(false && "Unhandled TargetInfo::IntType value"); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 4093 | return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4094 | } |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 4095 | |
| 4096 | //===----------------------------------------------------------------------===// |
| 4097 | // Type Predicates. |
| 4098 | //===----------------------------------------------------------------------===// |
| 4099 | |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4100 | /// isObjCNSObjectType - Return true if this is an NSObject object using |
| 4101 | /// NSObject attribute on a c-style pointer type. |
| 4102 | /// FIXME - Make it work directly on types. |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 4103 | /// FIXME: Move to Type. |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4104 | /// |
| 4105 | bool ASTContext::isObjCNSObjectType(QualType Ty) const { |
| 4106 | if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) { |
| 4107 | if (TypedefDecl *TD = TDT->getDecl()) |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 4108 | if (TD->getAttr<ObjCNSObjectAttr>()) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4109 | return true; |
| 4110 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4111 | return false; |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4112 | } |
| 4113 | |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4114 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 4115 | /// garbage collection attribute. |
| 4116 | /// |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4117 | Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const { |
| 4118 | Qualifiers::GC GCAttrs = Qualifiers::GCNone; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4119 | if (getLangOptions().ObjC1 && |
| 4120 | getLangOptions().getGCMode() != LangOptions::NonGC) { |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 4121 | GCAttrs = Ty.getObjCGCAttr(); |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4122 | // Default behavious under objective-c's gc is for objective-c pointers |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4123 | // (or pointers to them) be treated as though they were declared |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 4124 | // as __strong. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4125 | if (GCAttrs == Qualifiers::GCNone) { |
Fariborz Jahanian | 75212ee | 2009-09-10 23:38:45 +0000 | [diff] [blame] | 4126 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4127 | GCAttrs = Qualifiers::Strong; |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 4128 | else if (Ty->isPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4129 | return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 4130 | } |
Fariborz Jahanian | c211218 | 2009-04-11 00:00:54 +0000 | [diff] [blame] | 4131 | // Non-pointers have none gc'able attribute regardless of the attribute |
| 4132 | // set on them. |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 4133 | else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4134 | return Qualifiers::GCNone; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4135 | } |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 4136 | return GCAttrs; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4137 | } |
| 4138 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4139 | //===----------------------------------------------------------------------===// |
| 4140 | // Type Compatibility Testing |
| 4141 | //===----------------------------------------------------------------------===// |
Chris Lattner | 770951b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 4142 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4143 | /// areCompatVectorTypes - Return true if the two specified vector types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4144 | /// compatible. |
| 4145 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 4146 | const VectorType *RHS) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 4147 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4148 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4149 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4150 | } |
| 4151 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 4152 | bool ASTContext::areCompatibleVectorTypes(QualType FirstVec, |
| 4153 | QualType SecondVec) { |
| 4154 | assert(FirstVec->isVectorType() && "FirstVec should be a vector type"); |
| 4155 | assert(SecondVec->isVectorType() && "SecondVec should be a vector type"); |
| 4156 | |
| 4157 | if (hasSameUnqualifiedType(FirstVec, SecondVec)) |
| 4158 | return true; |
| 4159 | |
| 4160 | // AltiVec vectors types are identical to equivalent GCC vector types |
| 4161 | const VectorType *First = FirstVec->getAs<VectorType>(); |
| 4162 | const VectorType *Second = SecondVec->getAs<VectorType>(); |
| 4163 | if ((((First->getAltiVecSpecific() == VectorType::AltiVec) && |
| 4164 | (Second->getAltiVecSpecific() == VectorType::NotAltiVec)) || |
| 4165 | ((First->getAltiVecSpecific() == VectorType::NotAltiVec) && |
| 4166 | (Second->getAltiVecSpecific() == VectorType::AltiVec))) && |
| 4167 | hasSameType(First->getElementType(), Second->getElementType()) && |
| 4168 | (First->getNumElements() == Second->getNumElements())) |
| 4169 | return true; |
| 4170 | |
| 4171 | return false; |
| 4172 | } |
| 4173 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4174 | //===----------------------------------------------------------------------===// |
| 4175 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 4176 | //===----------------------------------------------------------------------===// |
| 4177 | |
| 4178 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 4179 | /// inheritance hierarchy of 'rProto'. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 4180 | bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 4181 | ObjCProtocolDecl *rProto) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4182 | if (lProto == rProto) |
| 4183 | return true; |
| 4184 | for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), |
| 4185 | E = rProto->protocol_end(); PI != E; ++PI) |
| 4186 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 4187 | return true; |
| 4188 | return false; |
| 4189 | } |
| 4190 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4191 | /// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...> |
| 4192 | /// return true if lhs's protocols conform to rhs's protocol; false |
| 4193 | /// otherwise. |
| 4194 | bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) { |
| 4195 | if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType()) |
| 4196 | return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false); |
| 4197 | return false; |
| 4198 | } |
| 4199 | |
Fariborz Jahanian | a8f8dac | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 4200 | /// ObjCQualifiedClassTypesAreCompatible - compare Class<p,...> and |
| 4201 | /// Class<p1, ...>. |
| 4202 | bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs, |
| 4203 | QualType rhs) { |
| 4204 | const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>(); |
| 4205 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
| 4206 | assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible"); |
| 4207 | |
| 4208 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4209 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4210 | bool match = false; |
| 4211 | ObjCProtocolDecl *lhsProto = *I; |
| 4212 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 4213 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 4214 | ObjCProtocolDecl *rhsProto = *J; |
| 4215 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) { |
| 4216 | match = true; |
| 4217 | break; |
| 4218 | } |
| 4219 | } |
| 4220 | if (!match) |
| 4221 | return false; |
| 4222 | } |
| 4223 | return true; |
| 4224 | } |
| 4225 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4226 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 4227 | /// ObjCQualifiedIDType. |
| 4228 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 4229 | bool compare) { |
| 4230 | // Allow id<P..> and an 'id' or void* type in all cases. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4231 | if (lhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4232 | lhs->isObjCIdType() || lhs->isObjCClassType()) |
| 4233 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4234 | else if (rhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4235 | rhs->isObjCIdType() || rhs->isObjCClassType()) |
| 4236 | return true; |
| 4237 | |
| 4238 | if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4239 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4240 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4241 | if (!rhsOPT) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4242 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4243 | if (rhsOPT->qual_empty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4244 | // If the RHS is a unqualified interface pointer "NSString*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4245 | // make sure we check the class hierarchy. |
| 4246 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 4247 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4248 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4249 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4250 | // see if static class implements all of id's protocols, directly or |
| 4251 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 4252 | if (!rhsID->ClassImplementsProtocol(*I, true)) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4253 | return false; |
| 4254 | } |
| 4255 | } |
| 4256 | // If there are no qualifiers and no interface, we have an 'id'. |
| 4257 | return true; |
| 4258 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4259 | // Both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4260 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4261 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4262 | ObjCProtocolDecl *lhsProto = *I; |
| 4263 | bool match = false; |
| 4264 | |
| 4265 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4266 | // see if static class implements all of id's protocols, directly or |
| 4267 | // through its super class and categories. |
| 4268 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 4269 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 4270 | ObjCProtocolDecl *rhsProto = *J; |
| 4271 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 4272 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 4273 | match = true; |
| 4274 | break; |
| 4275 | } |
| 4276 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4277 | // If the RHS is a qualified interface pointer "NSString<P>*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4278 | // make sure we check the class hierarchy. |
| 4279 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 4280 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4281 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4282 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4283 | // see if static class implements all of id's protocols, directly or |
| 4284 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 4285 | if (rhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4286 | match = true; |
| 4287 | break; |
| 4288 | } |
| 4289 | } |
| 4290 | } |
| 4291 | if (!match) |
| 4292 | return false; |
| 4293 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4294 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4295 | return true; |
| 4296 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4297 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4298 | const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 4299 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 4300 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4301 | if (const ObjCObjectPointerType *lhsOPT = |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4302 | lhs->getAsObjCInterfacePointerType()) { |
| 4303 | if (lhsOPT->qual_empty()) { |
| 4304 | bool match = false; |
| 4305 | if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { |
| 4306 | for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(), |
| 4307 | E = rhsQID->qual_end(); I != E; ++I) { |
Fariborz Jahanian | d1909bb | 2010-08-09 18:21:43 +0000 | [diff] [blame] | 4308 | // when comparing an id<P> on rhs with a static type on lhs, |
| 4309 | // static class must implement all of id's protocols directly or |
| 4310 | // indirectly through its super class. |
Fariborz Jahanian | 192b146 | 2010-08-12 20:46:12 +0000 | [diff] [blame] | 4311 | if (lhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4312 | match = true; |
| 4313 | break; |
| 4314 | } |
| 4315 | } |
| 4316 | if (!match) |
| 4317 | return false; |
| 4318 | } |
| 4319 | return true; |
| 4320 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4321 | // Both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4322 | for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(), |
| 4323 | E = lhsOPT->qual_end(); I != E; ++I) { |
| 4324 | ObjCProtocolDecl *lhsProto = *I; |
| 4325 | bool match = false; |
| 4326 | |
| 4327 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4328 | // see if static class implements all of id's protocols, directly or |
| 4329 | // through its super class and categories. |
| 4330 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 4331 | E = rhsQID->qual_end(); J != E; ++J) { |
| 4332 | ObjCProtocolDecl *rhsProto = *J; |
| 4333 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 4334 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 4335 | match = true; |
| 4336 | break; |
| 4337 | } |
| 4338 | } |
| 4339 | if (!match) |
| 4340 | return false; |
| 4341 | } |
| 4342 | return true; |
| 4343 | } |
| 4344 | return false; |
| 4345 | } |
| 4346 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4347 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4348 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 4349 | /// protocol qualifiers on the LHS or RHS. |
| 4350 | /// |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4351 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 4352 | const ObjCObjectPointerType *RHSOPT) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4353 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 4354 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 4355 | |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 4356 | // 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] | 4357 | if (LHS->isObjCUnqualifiedIdOrClass() || |
| 4358 | RHS->isObjCUnqualifiedIdOrClass()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4359 | return true; |
| 4360 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4361 | if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4362 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 4363 | QualType(RHSOPT,0), |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4364 | false); |
Fariborz Jahanian | a8f8dac | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 4365 | |
| 4366 | if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass()) |
| 4367 | return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0), |
| 4368 | QualType(RHSOPT,0)); |
| 4369 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4370 | // If we have 2 user-defined types, fall into that path. |
| 4371 | if (LHS->getInterface() && RHS->getInterface()) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4372 | return canAssignObjCInterfaces(LHS, RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4373 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4374 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4375 | } |
| 4376 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4377 | /// canAssignObjCInterfacesInBlockPointer - This routine is specifically written |
| 4378 | /// for providing type-safty for objective-c pointers used to pass/return |
| 4379 | /// arguments in block literals. When passed as arguments, passing 'A*' where |
| 4380 | /// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is |
| 4381 | /// not OK. For the return type, the opposite is not OK. |
| 4382 | bool ASTContext::canAssignObjCInterfacesInBlockPointer( |
| 4383 | const ObjCObjectPointerType *LHSOPT, |
| 4384 | const ObjCObjectPointerType *RHSOPT) { |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 4385 | if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4386 | return true; |
| 4387 | |
| 4388 | if (LHSOPT->isObjCBuiltinType()) { |
| 4389 | return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType(); |
| 4390 | } |
| 4391 | |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 4392 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4393 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 4394 | QualType(RHSOPT,0), |
| 4395 | false); |
| 4396 | |
| 4397 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 4398 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 4399 | if (LHS && RHS) { // We have 2 user-defined types. |
| 4400 | if (LHS != RHS) { |
| 4401 | if (LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
| 4402 | return false; |
| 4403 | if (RHS->getDecl()->isSuperClassOf(LHS->getDecl())) |
| 4404 | return true; |
| 4405 | } |
| 4406 | else |
| 4407 | return true; |
| 4408 | } |
| 4409 | return false; |
| 4410 | } |
| 4411 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4412 | /// getIntersectionOfProtocols - This routine finds the intersection of set |
| 4413 | /// of protocols inherited from two distinct objective-c pointer objects. |
| 4414 | /// It is used to build composite qualifier list of the composite type of |
| 4415 | /// the conditional expression involving two objective-c pointer objects. |
| 4416 | static |
| 4417 | void getIntersectionOfProtocols(ASTContext &Context, |
| 4418 | const ObjCObjectPointerType *LHSOPT, |
| 4419 | const ObjCObjectPointerType *RHSOPT, |
| 4420 | llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) { |
| 4421 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4422 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 4423 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 4424 | assert(LHS->getInterface() && "LHS must have an interface base"); |
| 4425 | assert(RHS->getInterface() && "RHS must have an interface base"); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4426 | |
| 4427 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet; |
| 4428 | unsigned LHSNumProtocols = LHS->getNumProtocols(); |
| 4429 | if (LHSNumProtocols > 0) |
| 4430 | InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end()); |
| 4431 | else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 4432 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4433 | Context.CollectInheritedProtocols(LHS->getInterface(), |
| 4434 | LHSInheritedProtocols); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4435 | InheritedProtocolSet.insert(LHSInheritedProtocols.begin(), |
| 4436 | LHSInheritedProtocols.end()); |
| 4437 | } |
| 4438 | |
| 4439 | unsigned RHSNumProtocols = RHS->getNumProtocols(); |
| 4440 | if (RHSNumProtocols > 0) { |
Dan Gohman | cb421fa | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 4441 | ObjCProtocolDecl **RHSProtocols = |
| 4442 | const_cast<ObjCProtocolDecl **>(RHS->qual_begin()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4443 | for (unsigned i = 0; i < RHSNumProtocols; ++i) |
| 4444 | if (InheritedProtocolSet.count(RHSProtocols[i])) |
| 4445 | IntersectionOfProtocols.push_back(RHSProtocols[i]); |
| 4446 | } |
| 4447 | else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 4448 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4449 | Context.CollectInheritedProtocols(RHS->getInterface(), |
| 4450 | RHSInheritedProtocols); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 4451 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 4452 | RHSInheritedProtocols.begin(), |
| 4453 | E = RHSInheritedProtocols.end(); I != E; ++I) |
| 4454 | if (InheritedProtocolSet.count((*I))) |
| 4455 | IntersectionOfProtocols.push_back((*I)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4456 | } |
| 4457 | } |
| 4458 | |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4459 | /// areCommonBaseCompatible - Returns common base class of the two classes if |
| 4460 | /// one found. Note that this is O'2 algorithm. But it will be called as the |
| 4461 | /// last type comparison in a ?-exp of ObjC pointer types before a |
| 4462 | /// warning is issued. So, its invokation is extremely rare. |
| 4463 | QualType ASTContext::areCommonBaseCompatible( |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4464 | const ObjCObjectPointerType *Lptr, |
| 4465 | const ObjCObjectPointerType *Rptr) { |
| 4466 | const ObjCObjectType *LHS = Lptr->getObjectType(); |
| 4467 | const ObjCObjectType *RHS = Rptr->getObjectType(); |
| 4468 | const ObjCInterfaceDecl* LDecl = LHS->getInterface(); |
| 4469 | const ObjCInterfaceDecl* RDecl = RHS->getInterface(); |
| 4470 | if (!LDecl || !RDecl) |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4471 | return QualType(); |
| 4472 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4473 | while ((LDecl = LDecl->getSuperClass())) { |
| 4474 | LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4475 | if (canAssignObjCInterfaces(LHS, RHS)) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4476 | llvm::SmallVector<ObjCProtocolDecl *, 8> Protocols; |
| 4477 | getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols); |
| 4478 | |
| 4479 | QualType Result = QualType(LHS, 0); |
| 4480 | if (!Protocols.empty()) |
| 4481 | Result = getObjCObjectType(Result, Protocols.data(), Protocols.size()); |
| 4482 | Result = getObjCObjectPointerType(Result); |
| 4483 | return Result; |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4484 | } |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4485 | } |
| 4486 | |
| 4487 | return QualType(); |
| 4488 | } |
| 4489 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4490 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS, |
| 4491 | const ObjCObjectType *RHS) { |
| 4492 | assert(LHS->getInterface() && "LHS is not an interface type"); |
| 4493 | assert(RHS->getInterface() && "RHS is not an interface type"); |
| 4494 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4495 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 4496 | // the LHS. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4497 | if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface())) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4498 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4499 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4500 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 4501 | // protocol qualified at all, then we are good. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4502 | if (LHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4503 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4504 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4505 | // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it |
| 4506 | // isn't a superset. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4507 | if (RHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4508 | return true; // FIXME: should return false! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4509 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4510 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 4511 | LHSPE = LHS->qual_end(); |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4512 | LHSPI != LHSPE; LHSPI++) { |
| 4513 | bool RHSImplementsProtocol = false; |
| 4514 | |
| 4515 | // If the RHS doesn't implement the protocol on the left, the types |
| 4516 | // are incompatible. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4517 | for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(), |
| 4518 | RHSPE = RHS->qual_end(); |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 4519 | RHSPI != RHSPE; RHSPI++) { |
| 4520 | if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) { |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4521 | RHSImplementsProtocol = true; |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 4522 | break; |
| 4523 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4524 | } |
| 4525 | // FIXME: For better diagnostics, consider passing back the protocol name. |
| 4526 | if (!RHSImplementsProtocol) |
| 4527 | return false; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4528 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4529 | // The RHS implements all protocols listed on the LHS. |
| 4530 | return true; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4531 | } |
| 4532 | |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4533 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 4534 | // get the "pointed to" types |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4535 | const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
| 4536 | const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4537 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4538 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4539 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4540 | |
| 4541 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 4542 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4543 | } |
| 4544 | |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4545 | bool ASTContext::canBindObjCObjectType(QualType To, QualType From) { |
| 4546 | return canAssignObjCInterfaces( |
| 4547 | getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(), |
| 4548 | getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>()); |
| 4549 | } |
| 4550 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4551 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4552 | /// both shall have the identically qualified version of a compatible type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4553 | /// 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] | 4554 | /// same. See 6.7.[2,3,5] for additional rules. |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4555 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS, |
| 4556 | bool CompareUnqualified) { |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 4557 | if (getLangOptions().CPlusPlus) |
| 4558 | return hasSameType(LHS, RHS); |
| 4559 | |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4560 | return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4561 | } |
| 4562 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4563 | bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) { |
| 4564 | return !mergeTypes(LHS, RHS, true).isNull(); |
| 4565 | } |
| 4566 | |
| 4567 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs, |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4568 | bool OfBlockPointer, |
| 4569 | bool Unqualified) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4570 | const FunctionType *lbase = lhs->getAs<FunctionType>(); |
| 4571 | const FunctionType *rbase = rhs->getAs<FunctionType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4572 | const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 4573 | const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4574 | bool allLTypes = true; |
| 4575 | bool allRTypes = true; |
| 4576 | |
| 4577 | // Check return type |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4578 | QualType retType; |
| 4579 | if (OfBlockPointer) |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4580 | retType = mergeTypes(rbase->getResultType(), lbase->getResultType(), true, |
| 4581 | Unqualified); |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4582 | else |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4583 | retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), |
| 4584 | false, Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4585 | if (retType.isNull()) return QualType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4586 | |
| 4587 | if (Unqualified) |
| 4588 | retType = retType.getUnqualifiedType(); |
| 4589 | |
| 4590 | CanQualType LRetType = getCanonicalType(lbase->getResultType()); |
| 4591 | CanQualType RRetType = getCanonicalType(rbase->getResultType()); |
| 4592 | if (Unqualified) { |
| 4593 | LRetType = LRetType.getUnqualifiedType(); |
| 4594 | RRetType = RRetType.getUnqualifiedType(); |
| 4595 | } |
| 4596 | |
| 4597 | if (getCanonicalType(retType) != LRetType) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4598 | allLTypes = false; |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4599 | if (getCanonicalType(retType) != RRetType) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4600 | allRTypes = false; |
Daniel Dunbar | 6a15c85 | 2010-04-28 16:20:58 +0000 | [diff] [blame] | 4601 | // FIXME: double check this |
| 4602 | // FIXME: should we error if lbase->getRegParmAttr() != 0 && |
| 4603 | // rbase->getRegParmAttr() != 0 && |
| 4604 | // lbase->getRegParmAttr() != rbase->getRegParmAttr()? |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 4605 | FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo(); |
| 4606 | FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo(); |
Daniel Dunbar | 6a15c85 | 2010-04-28 16:20:58 +0000 | [diff] [blame] | 4607 | unsigned RegParm = lbaseInfo.getRegParm() == 0 ? rbaseInfo.getRegParm() : |
| 4608 | lbaseInfo.getRegParm(); |
| 4609 | bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn(); |
| 4610 | if (NoReturn != lbaseInfo.getNoReturn() || |
| 4611 | RegParm != lbaseInfo.getRegParm()) |
| 4612 | allLTypes = false; |
| 4613 | if (NoReturn != rbaseInfo.getNoReturn() || |
| 4614 | RegParm != rbaseInfo.getRegParm()) |
| 4615 | allRTypes = false; |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 4616 | CallingConv lcc = lbaseInfo.getCC(); |
| 4617 | CallingConv rcc = rbaseInfo.getCC(); |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 4618 | // Compatible functions must have compatible calling conventions |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4619 | if (!isSameCallConv(lcc, rcc)) |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 4620 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4621 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4622 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 4623 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 4624 | "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4625 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 4626 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 4627 | |
| 4628 | // Compatible functions must have the same number of arguments |
| 4629 | if (lproto_nargs != rproto_nargs) |
| 4630 | return QualType(); |
| 4631 | |
| 4632 | // Variadic and non-variadic functions aren't compatible |
| 4633 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 4634 | return QualType(); |
| 4635 | |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 4636 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 4637 | return QualType(); |
| 4638 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4639 | // Check argument compatibility |
| 4640 | llvm::SmallVector<QualType, 10> types; |
| 4641 | for (unsigned i = 0; i < lproto_nargs; i++) { |
| 4642 | QualType largtype = lproto->getArgType(i).getUnqualifiedType(); |
| 4643 | QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4644 | QualType argtype = mergeTypes(largtype, rargtype, OfBlockPointer, |
| 4645 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4646 | if (argtype.isNull()) return QualType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4647 | |
| 4648 | if (Unqualified) |
| 4649 | argtype = argtype.getUnqualifiedType(); |
| 4650 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4651 | types.push_back(argtype); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4652 | if (Unqualified) { |
| 4653 | largtype = largtype.getUnqualifiedType(); |
| 4654 | rargtype = rargtype.getUnqualifiedType(); |
| 4655 | } |
| 4656 | |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4657 | if (getCanonicalType(argtype) != getCanonicalType(largtype)) |
| 4658 | allLTypes = false; |
| 4659 | if (getCanonicalType(argtype) != getCanonicalType(rargtype)) |
| 4660 | allRTypes = false; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4661 | } |
| 4662 | if (allLTypes) return lhs; |
| 4663 | if (allRTypes) return rhs; |
| 4664 | return getFunctionType(retType, types.begin(), types.size(), |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 4665 | lproto->isVariadic(), lproto->getTypeQuals(), |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 4666 | false, false, 0, 0, |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 4667 | FunctionType::ExtInfo(NoReturn, RegParm, lcc)); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4668 | } |
| 4669 | |
| 4670 | if (lproto) allRTypes = false; |
| 4671 | if (rproto) allLTypes = false; |
| 4672 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4673 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4674 | if (proto) { |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 4675 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4676 | if (proto->isVariadic()) return QualType(); |
| 4677 | // Check that the types are compatible with the types that |
| 4678 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 4679 | // The only types actually affected are promotable integer |
| 4680 | // types and floats, which would be passed as a different |
| 4681 | // type depending on whether the prototype is visible. |
| 4682 | unsigned proto_nargs = proto->getNumArgs(); |
| 4683 | for (unsigned i = 0; i < proto_nargs; ++i) { |
| 4684 | QualType argTy = proto->getArgType(i); |
Douglas Gregor | b0f8eac | 2010-02-03 19:27:29 +0000 | [diff] [blame] | 4685 | |
| 4686 | // Look at the promotion type of enum types, since that is the type used |
| 4687 | // to pass enum values. |
| 4688 | if (const EnumType *Enum = argTy->getAs<EnumType>()) |
| 4689 | argTy = Enum->getDecl()->getPromotionType(); |
| 4690 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4691 | if (argTy->isPromotableIntegerType() || |
| 4692 | getCanonicalType(argTy).getUnqualifiedType() == FloatTy) |
| 4693 | return QualType(); |
| 4694 | } |
| 4695 | |
| 4696 | if (allLTypes) return lhs; |
| 4697 | if (allRTypes) return rhs; |
| 4698 | return getFunctionType(retType, proto->arg_type_begin(), |
Mike Stump | 2d3c191 | 2009-07-27 00:44:23 +0000 | [diff] [blame] | 4699 | proto->getNumArgs(), proto->isVariadic(), |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 4700 | proto->getTypeQuals(), |
| 4701 | false, false, 0, 0, |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 4702 | FunctionType::ExtInfo(NoReturn, RegParm, lcc)); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4703 | } |
| 4704 | |
| 4705 | if (allLTypes) return lhs; |
| 4706 | if (allRTypes) return rhs; |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 4707 | FunctionType::ExtInfo Info(NoReturn, RegParm, lcc); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 4708 | return getFunctionNoProtoType(retType, Info); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4709 | } |
| 4710 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4711 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4712 | bool OfBlockPointer, |
| 4713 | bool Unqualified) { |
Bill Wendling | 43d6975 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 4714 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 4715 | // type is adjusted to "T" prior to any further analysis, the expression |
| 4716 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4717 | // expression is an lvalue unless the reference is an rvalue reference and |
| 4718 | // the expression is a function call (possibly inside parentheses). |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 4719 | assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?"); |
| 4720 | assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?"); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4721 | |
| 4722 | if (Unqualified) { |
| 4723 | LHS = LHS.getUnqualifiedType(); |
| 4724 | RHS = RHS.getUnqualifiedType(); |
| 4725 | } |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 4726 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4727 | QualType LHSCan = getCanonicalType(LHS), |
| 4728 | RHSCan = getCanonicalType(RHS); |
| 4729 | |
| 4730 | // If two types are identical, they are compatible. |
| 4731 | if (LHSCan == RHSCan) |
| 4732 | return LHS; |
| 4733 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4734 | // If the qualifiers are different, the types aren't compatible... mostly. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4735 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 4736 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4737 | if (LQuals != RQuals) { |
| 4738 | // If any of these qualifiers are different, we have a type |
| 4739 | // mismatch. |
| 4740 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 4741 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 4742 | return QualType(); |
| 4743 | |
| 4744 | // Exactly one GC qualifier difference is allowed: __strong is |
| 4745 | // okay if the other type has no GC qualifier but is an Objective |
| 4746 | // C object pointer (i.e. implicitly strong by default). We fix |
| 4747 | // this by pretending that the unqualified type was actually |
| 4748 | // qualified __strong. |
| 4749 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 4750 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 4751 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 4752 | |
| 4753 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 4754 | return QualType(); |
| 4755 | |
| 4756 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
| 4757 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); |
| 4758 | } |
| 4759 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
| 4760 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); |
| 4761 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4762 | return QualType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4763 | } |
| 4764 | |
| 4765 | // Okay, qualifiers are equal. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4766 | |
Eli Friedman | 852d63b | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 4767 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 4768 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4769 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4770 | // We want to consider the two function types to be the same for these |
| 4771 | // comparisons, just force one to the other. |
| 4772 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 4773 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 4c721d3 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 4774 | |
| 4775 | // Same as above for arrays |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 4776 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 4777 | LHSClass = Type::ConstantArray; |
| 4778 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 4779 | RHSClass = Type::ConstantArray; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4780 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4781 | // ObjCInterfaces are just specialized ObjCObjects. |
| 4782 | if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject; |
| 4783 | if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject; |
| 4784 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 4785 | // Canonicalize ExtVector -> Vector. |
| 4786 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 4787 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4788 | |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 4789 | // If the canonical type classes don't match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4790 | if (LHSClass != RHSClass) { |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4791 | // 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] | 4792 | // a signed integer type, or an unsigned integer type. |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 4793 | // Compatibility is based on the underlying type, not the promotion |
| 4794 | // type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4795 | if (const EnumType* ETy = LHS->getAs<EnumType>()) { |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4796 | if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType()) |
| 4797 | return RHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 4798 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4799 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4800 | if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType()) |
| 4801 | return LHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 4802 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4803 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4804 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4805 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4806 | |
Steve Naroff | 4a74678 | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 4807 | // The canonical type classes match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4808 | switch (LHSClass) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4809 | #define TYPE(Class, Base) |
| 4810 | #define ABSTRACT_TYPE(Class, Base) |
John McCall | ad5e738 | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 4811 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4812 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 4813 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 4814 | #include "clang/AST/TypeNodes.def" |
| 4815 | assert(false && "Non-canonical and dependent types shouldn't get here"); |
| 4816 | return QualType(); |
| 4817 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4818 | case Type::LValueReference: |
| 4819 | case Type::RValueReference: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4820 | case Type::MemberPointer: |
| 4821 | assert(false && "C++ should never be in mergeTypes"); |
| 4822 | return QualType(); |
| 4823 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4824 | case Type::ObjCInterface: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4825 | case Type::IncompleteArray: |
| 4826 | case Type::VariableArray: |
| 4827 | case Type::FunctionProto: |
| 4828 | case Type::ExtVector: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4829 | assert(false && "Types are eliminated above"); |
| 4830 | return QualType(); |
| 4831 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4832 | case Type::Pointer: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4833 | { |
| 4834 | // Merge two pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4835 | QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); |
| 4836 | QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4837 | if (Unqualified) { |
| 4838 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 4839 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 4840 | } |
| 4841 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false, |
| 4842 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4843 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 4844 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4845 | return LHS; |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 4846 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4847 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4848 | return getPointerType(ResultType); |
| 4849 | } |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 4850 | case Type::BlockPointer: |
| 4851 | { |
| 4852 | // Merge two block pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4853 | QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); |
| 4854 | QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4855 | if (Unqualified) { |
| 4856 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 4857 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 4858 | } |
| 4859 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer, |
| 4860 | Unqualified); |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 4861 | if (ResultType.isNull()) return QualType(); |
| 4862 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 4863 | return LHS; |
| 4864 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 4865 | return RHS; |
| 4866 | return getBlockPointerType(ResultType); |
| 4867 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4868 | case Type::ConstantArray: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4869 | { |
| 4870 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 4871 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 4872 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 4873 | return QualType(); |
| 4874 | |
| 4875 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 4876 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4877 | if (Unqualified) { |
| 4878 | LHSElem = LHSElem.getUnqualifiedType(); |
| 4879 | RHSElem = RHSElem.getUnqualifiedType(); |
| 4880 | } |
| 4881 | |
| 4882 | QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4883 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4884 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 4885 | return LHS; |
| 4886 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 4887 | return RHS; |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 4888 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 4889 | ArrayType::ArraySizeModifier(), 0); |
| 4890 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 4891 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4892 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 4893 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4894 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 4895 | return LHS; |
| 4896 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 4897 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4898 | if (LVAT) { |
| 4899 | // FIXME: This isn't correct! But tricky to implement because |
| 4900 | // the array's size has to be the size of LHS, but the type |
| 4901 | // has to be different. |
| 4902 | return LHS; |
| 4903 | } |
| 4904 | if (RVAT) { |
| 4905 | // FIXME: This isn't correct! But tricky to implement because |
| 4906 | // the array's size has to be the size of RHS, but the type |
| 4907 | // has to be different. |
| 4908 | return RHS; |
| 4909 | } |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 4910 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 4911 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4912 | return getIncompleteArrayType(ResultType, |
| 4913 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4914 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4915 | case Type::FunctionNoProto: |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4916 | return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4917 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4918 | case Type::Enum: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4919 | return QualType(); |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4920 | case Type::Builtin: |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 4921 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4922 | return QualType(); |
Daniel Dunbar | 64cfdb7 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 4923 | case Type::Complex: |
| 4924 | // Distinct complex types are incompatible. |
| 4925 | return QualType(); |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 4926 | case Type::Vector: |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 4927 | // FIXME: The merged type should be an ExtVector! |
John McCall | 1c471f3 | 2010-03-12 23:14:13 +0000 | [diff] [blame] | 4928 | if (areCompatVectorTypes(LHSCan->getAs<VectorType>(), |
| 4929 | RHSCan->getAs<VectorType>())) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4930 | return LHS; |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4931 | return QualType(); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4932 | case Type::ObjCObject: { |
| 4933 | // Check if the types are assignment compatible. |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 4934 | // FIXME: This should be type compatibility, e.g. whether |
| 4935 | // "LHS x; RHS x;" at global scope is legal. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4936 | const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>(); |
| 4937 | const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>(); |
| 4938 | if (canAssignObjCInterfaces(LHSIface, RHSIface)) |
Steve Naroff | 5fd659d | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 4939 | return LHS; |
| 4940 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4941 | return QualType(); |
Cedric Venet | 61490e9 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 4942 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4943 | case Type::ObjCObjectPointer: { |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4944 | if (OfBlockPointer) { |
| 4945 | if (canAssignObjCInterfacesInBlockPointer( |
| 4946 | LHS->getAs<ObjCObjectPointerType>(), |
| 4947 | RHS->getAs<ObjCObjectPointerType>())) |
| 4948 | return LHS; |
| 4949 | return QualType(); |
| 4950 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4951 | if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(), |
| 4952 | RHS->getAs<ObjCObjectPointerType>())) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4953 | return LHS; |
| 4954 | |
Steve Naroff | bc76dd0 | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 4955 | return QualType(); |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4956 | } |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4957 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4958 | |
| 4959 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4960 | } |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 4961 | |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 4962 | /// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and |
| 4963 | /// 'RHS' attributes and returns the merged version; including for function |
| 4964 | /// return types. |
| 4965 | QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) { |
| 4966 | QualType LHSCan = getCanonicalType(LHS), |
| 4967 | RHSCan = getCanonicalType(RHS); |
| 4968 | // If two types are identical, they are compatible. |
| 4969 | if (LHSCan == RHSCan) |
| 4970 | return LHS; |
| 4971 | if (RHSCan->isFunctionType()) { |
| 4972 | if (!LHSCan->isFunctionType()) |
| 4973 | return QualType(); |
| 4974 | QualType OldReturnType = |
| 4975 | cast<FunctionType>(RHSCan.getTypePtr())->getResultType(); |
| 4976 | QualType NewReturnType = |
| 4977 | cast<FunctionType>(LHSCan.getTypePtr())->getResultType(); |
| 4978 | QualType ResReturnType = |
| 4979 | mergeObjCGCQualifiers(NewReturnType, OldReturnType); |
| 4980 | if (ResReturnType.isNull()) |
| 4981 | return QualType(); |
| 4982 | if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) { |
| 4983 | // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo(); |
| 4984 | // In either case, use OldReturnType to build the new function type. |
| 4985 | const FunctionType *F = LHS->getAs<FunctionType>(); |
| 4986 | if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) { |
| 4987 | FunctionType::ExtInfo Info = getFunctionExtInfo(LHS); |
| 4988 | QualType ResultType |
| 4989 | = getFunctionType(OldReturnType, FPT->arg_type_begin(), |
| 4990 | FPT->getNumArgs(), FPT->isVariadic(), |
| 4991 | FPT->getTypeQuals(), |
| 4992 | FPT->hasExceptionSpec(), |
| 4993 | FPT->hasAnyExceptionSpec(), |
| 4994 | FPT->getNumExceptions(), |
| 4995 | FPT->exception_begin(), |
| 4996 | Info); |
| 4997 | return ResultType; |
| 4998 | } |
| 4999 | } |
| 5000 | return QualType(); |
| 5001 | } |
| 5002 | |
| 5003 | // If the qualifiers are different, the types can still be merged. |
| 5004 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 5005 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
| 5006 | if (LQuals != RQuals) { |
| 5007 | // If any of these qualifiers are different, we have a type mismatch. |
| 5008 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 5009 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 5010 | return QualType(); |
| 5011 | |
| 5012 | // Exactly one GC qualifier difference is allowed: __strong is |
| 5013 | // okay if the other type has no GC qualifier but is an Objective |
| 5014 | // C object pointer (i.e. implicitly strong by default). We fix |
| 5015 | // this by pretending that the unqualified type was actually |
| 5016 | // qualified __strong. |
| 5017 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 5018 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 5019 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 5020 | |
| 5021 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 5022 | return QualType(); |
| 5023 | |
| 5024 | if (GC_L == Qualifiers::Strong) |
| 5025 | return LHS; |
| 5026 | if (GC_R == Qualifiers::Strong) |
| 5027 | return RHS; |
| 5028 | return QualType(); |
| 5029 | } |
| 5030 | |
| 5031 | if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) { |
| 5032 | QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5033 | QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5034 | QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT); |
| 5035 | if (ResQT == LHSBaseQT) |
| 5036 | return LHS; |
| 5037 | if (ResQT == RHSBaseQT) |
| 5038 | return RHS; |
| 5039 | } |
| 5040 | return QualType(); |
| 5041 | } |
| 5042 | |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 5043 | //===----------------------------------------------------------------------===// |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5044 | // Integer Predicates |
| 5045 | //===----------------------------------------------------------------------===// |
Chris Lattner | 88054de | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 5046 | |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5047 | unsigned ASTContext::getIntWidth(QualType T) { |
Sebastian Redl | 632d772 | 2009-11-05 21:10:57 +0000 | [diff] [blame] | 5048 | if (T->isBooleanType()) |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5049 | return 1; |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 5050 | if (EnumType *ET = dyn_cast<EnumType>(T)) |
Eli Friedman | 29a7f33 | 2009-12-10 22:29:29 +0000 | [diff] [blame] | 5051 | T = ET->getDecl()->getIntegerType(); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 5052 | // For builtin types, just use the standard type sizing method |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5053 | return (unsigned)getTypeSize(T); |
| 5054 | } |
| 5055 | |
| 5056 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) { |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5057 | assert(T->hasSignedIntegerRepresentation() && "Unexpected type"); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5058 | |
| 5059 | // Turn <4 x signed int> -> <4 x unsigned int> |
| 5060 | if (const VectorType *VTy = T->getAs<VectorType>()) |
| 5061 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 5062 | VTy->getNumElements(), VTy->getAltiVecSpecific()); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5063 | |
| 5064 | // For enums, we return the unsigned version of the base type. |
| 5065 | if (const EnumType *ETy = T->getAs<EnumType>()) |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5066 | T = ETy->getDecl()->getIntegerType(); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5067 | |
| 5068 | const BuiltinType *BTy = T->getAs<BuiltinType>(); |
| 5069 | assert(BTy && "Unexpected signed integer type"); |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5070 | switch (BTy->getKind()) { |
| 5071 | case BuiltinType::Char_S: |
| 5072 | case BuiltinType::SChar: |
| 5073 | return UnsignedCharTy; |
| 5074 | case BuiltinType::Short: |
| 5075 | return UnsignedShortTy; |
| 5076 | case BuiltinType::Int: |
| 5077 | return UnsignedIntTy; |
| 5078 | case BuiltinType::Long: |
| 5079 | return UnsignedLongTy; |
| 5080 | case BuiltinType::LongLong: |
| 5081 | return UnsignedLongLongTy; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 5082 | case BuiltinType::Int128: |
| 5083 | return UnsignedInt128Ty; |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5084 | default: |
| 5085 | assert(0 && "Unexpected signed integer type"); |
| 5086 | return QualType(); |
| 5087 | } |
| 5088 | } |
| 5089 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 5090 | ExternalASTSource::~ExternalASTSource() { } |
| 5091 | |
| 5092 | void ExternalASTSource::PrintStats() { } |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5093 | |
| 5094 | |
| 5095 | //===----------------------------------------------------------------------===// |
| 5096 | // Builtin Type Computation |
| 5097 | //===----------------------------------------------------------------------===// |
| 5098 | |
| 5099 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
| 5100 | /// pointer over the consumed characters. This returns the resultant type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5101 | static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context, |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5102 | ASTContext::GetBuiltinTypeError &Error, |
| 5103 | bool AllowTypeModifiers = true) { |
| 5104 | // Modifiers. |
| 5105 | int HowLong = 0; |
| 5106 | bool Signed = false, Unsigned = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5107 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5108 | // Read the modifiers first. |
| 5109 | bool Done = false; |
| 5110 | while (!Done) { |
| 5111 | switch (*Str++) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5112 | default: Done = true; --Str; break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5113 | case 'S': |
| 5114 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 5115 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 5116 | Signed = true; |
| 5117 | break; |
| 5118 | case 'U': |
| 5119 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
| 5120 | assert(!Unsigned && "Can't use 'S' modifier multiple times!"); |
| 5121 | Unsigned = true; |
| 5122 | break; |
| 5123 | case 'L': |
| 5124 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 5125 | ++HowLong; |
| 5126 | break; |
| 5127 | } |
| 5128 | } |
| 5129 | |
| 5130 | QualType Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5131 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5132 | // Read the base type. |
| 5133 | switch (*Str++) { |
| 5134 | default: assert(0 && "Unknown builtin type letter!"); |
| 5135 | case 'v': |
| 5136 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 5137 | "Bad modifiers used with 'v'!"); |
| 5138 | Type = Context.VoidTy; |
| 5139 | break; |
| 5140 | case 'f': |
| 5141 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 5142 | "Bad modifiers used with 'f'!"); |
| 5143 | Type = Context.FloatTy; |
| 5144 | break; |
| 5145 | case 'd': |
| 5146 | assert(HowLong < 2 && !Signed && !Unsigned && |
| 5147 | "Bad modifiers used with 'd'!"); |
| 5148 | if (HowLong) |
| 5149 | Type = Context.LongDoubleTy; |
| 5150 | else |
| 5151 | Type = Context.DoubleTy; |
| 5152 | break; |
| 5153 | case 's': |
| 5154 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 5155 | if (Unsigned) |
| 5156 | Type = Context.UnsignedShortTy; |
| 5157 | else |
| 5158 | Type = Context.ShortTy; |
| 5159 | break; |
| 5160 | case 'i': |
| 5161 | if (HowLong == 3) |
| 5162 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 5163 | else if (HowLong == 2) |
| 5164 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 5165 | else if (HowLong == 1) |
| 5166 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 5167 | else |
| 5168 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 5169 | break; |
| 5170 | case 'c': |
| 5171 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 5172 | if (Signed) |
| 5173 | Type = Context.SignedCharTy; |
| 5174 | else if (Unsigned) |
| 5175 | Type = Context.UnsignedCharTy; |
| 5176 | else |
| 5177 | Type = Context.CharTy; |
| 5178 | break; |
| 5179 | case 'b': // boolean |
| 5180 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 5181 | Type = Context.BoolTy; |
| 5182 | break; |
| 5183 | case 'z': // size_t. |
| 5184 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 5185 | Type = Context.getSizeType(); |
| 5186 | break; |
| 5187 | case 'F': |
| 5188 | Type = Context.getCFConstantStringType(); |
| 5189 | break; |
| 5190 | case 'a': |
| 5191 | Type = Context.getBuiltinVaListType(); |
| 5192 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 5193 | break; |
| 5194 | case 'A': |
| 5195 | // This is a "reference" to a va_list; however, what exactly |
| 5196 | // this means depends on how va_list is defined. There are two |
| 5197 | // different kinds of va_list: ones passed by value, and ones |
| 5198 | // passed by reference. An example of a by-value va_list is |
| 5199 | // x86, where va_list is a char*. An example of by-ref va_list |
| 5200 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 5201 | // we want this argument to be a char*&; for x86-64, we want |
| 5202 | // it to be a __va_list_tag*. |
| 5203 | Type = Context.getBuiltinVaListType(); |
| 5204 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 5205 | if (Type->isArrayType()) { |
| 5206 | Type = Context.getArrayDecayedType(Type); |
| 5207 | } else { |
| 5208 | Type = Context.getLValueReferenceType(Type); |
| 5209 | } |
| 5210 | break; |
| 5211 | case 'V': { |
| 5212 | char *End; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5213 | unsigned NumElements = strtoul(Str, &End, 10); |
| 5214 | assert(End != Str && "Missing vector size"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5215 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5216 | Str = End; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5217 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5218 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false); |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 5219 | // FIXME: Don't know what to do about AltiVec. |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 5220 | Type = Context.getVectorType(ElementType, NumElements, |
| 5221 | VectorType::NotAltiVec); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5222 | break; |
| 5223 | } |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 5224 | case 'X': { |
| 5225 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false); |
| 5226 | Type = Context.getComplexType(ElementType); |
| 5227 | break; |
| 5228 | } |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 5229 | case 'P': |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 5230 | Type = Context.getFILEType(); |
| 5231 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 5232 | Error = ASTContext::GE_Missing_stdio; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5233 | return QualType(); |
| 5234 | } |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 5235 | break; |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 5236 | case 'J': |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 5237 | if (Signed) |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 5238 | Type = Context.getsigjmp_bufType(); |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 5239 | else |
| 5240 | Type = Context.getjmp_bufType(); |
| 5241 | |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 5242 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 5243 | Error = ASTContext::GE_Missing_setjmp; |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 5244 | return QualType(); |
| 5245 | } |
| 5246 | break; |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 5247 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5248 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5249 | if (!AllowTypeModifiers) |
| 5250 | return Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5251 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5252 | Done = false; |
| 5253 | while (!Done) { |
John McCall | 187ab37 | 2010-03-12 04:21:28 +0000 | [diff] [blame] | 5254 | switch (char c = *Str++) { |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5255 | default: Done = true; --Str; break; |
| 5256 | case '*': |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5257 | case '&': |
John McCall | 187ab37 | 2010-03-12 04:21:28 +0000 | [diff] [blame] | 5258 | { |
| 5259 | // Both pointers and references can have their pointee types |
| 5260 | // qualified with an address space. |
| 5261 | char *End; |
| 5262 | unsigned AddrSpace = strtoul(Str, &End, 10); |
| 5263 | if (End != Str && AddrSpace != 0) { |
| 5264 | Type = Context.getAddrSpaceQualType(Type, AddrSpace); |
| 5265 | Str = End; |
| 5266 | } |
| 5267 | } |
| 5268 | if (c == '*') |
| 5269 | Type = Context.getPointerType(Type); |
| 5270 | else |
| 5271 | Type = Context.getLValueReferenceType(Type); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5272 | break; |
| 5273 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 5274 | case 'C': |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5275 | Type = Type.withConst(); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5276 | break; |
Fariborz Jahanian | 013af39 | 2010-01-26 22:48:42 +0000 | [diff] [blame] | 5277 | case 'D': |
| 5278 | Type = Context.getVolatileType(Type); |
| 5279 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5280 | } |
| 5281 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5282 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5283 | return Type; |
| 5284 | } |
| 5285 | |
| 5286 | /// GetBuiltinType - Return the type for the specified builtin. |
| 5287 | QualType ASTContext::GetBuiltinType(unsigned id, |
| 5288 | GetBuiltinTypeError &Error) { |
| 5289 | const char *TypeStr = BuiltinInfo.GetTypeString(id); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5290 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5291 | llvm::SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5292 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5293 | Error = GE_None; |
| 5294 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error); |
| 5295 | if (Error != GE_None) |
| 5296 | return QualType(); |
| 5297 | while (TypeStr[0] && TypeStr[0] != '.') { |
| 5298 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error); |
| 5299 | if (Error != GE_None) |
| 5300 | return QualType(); |
| 5301 | |
| 5302 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 5303 | if (Ty->isArrayType()) |
| 5304 | Ty = getArrayDecayedType(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5305 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5306 | ArgTypes.push_back(Ty); |
| 5307 | } |
| 5308 | |
| 5309 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 5310 | "'.' should only occur at end of builtin type list!"); |
| 5311 | |
| 5312 | // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);". |
| 5313 | if (ArgTypes.size() == 0 && TypeStr[0] == '.') |
| 5314 | return getFunctionNoProtoType(ResType); |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 5315 | |
| 5316 | // FIXME: Should we create noreturn types? |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5317 | return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 5318 | TypeStr[0] == '.', 0, false, false, 0, 0, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 5319 | FunctionType::ExtInfo()); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5320 | } |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5321 | |
| 5322 | QualType |
| 5323 | ASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) { |
| 5324 | // Perform the usual unary conversions. We do this early so that |
| 5325 | // integral promotions to "int" can allow us to exit early, in the |
| 5326 | // lhs == rhs check. Also, for conversion purposes, we ignore any |
| 5327 | // qualifiers. For example, "const float" and "float" are |
| 5328 | // equivalent. |
| 5329 | if (lhs->isPromotableIntegerType()) |
| 5330 | lhs = getPromotedIntegerType(lhs); |
| 5331 | else |
| 5332 | lhs = lhs.getUnqualifiedType(); |
| 5333 | if (rhs->isPromotableIntegerType()) |
| 5334 | rhs = getPromotedIntegerType(rhs); |
| 5335 | else |
| 5336 | rhs = rhs.getUnqualifiedType(); |
| 5337 | |
| 5338 | // If both types are identical, no conversion is needed. |
| 5339 | if (lhs == rhs) |
| 5340 | return lhs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5341 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5342 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 5343 | // The caller can deal with this (e.g. pointer + int). |
| 5344 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 5345 | return lhs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5346 | |
| 5347 | // At this point, we have two different arithmetic types. |
| 5348 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5349 | // Handle complex types first (C99 6.3.1.8p1). |
| 5350 | if (lhs->isComplexType() || rhs->isComplexType()) { |
| 5351 | // if we have an integer operand, the result is the complex type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5352 | if (rhs->isIntegerType() || rhs->isComplexIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5353 | // convert the rhs to the lhs complex type. |
| 5354 | return lhs; |
| 5355 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5356 | if (lhs->isIntegerType() || lhs->isComplexIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5357 | // convert the lhs to the rhs complex type. |
| 5358 | return rhs; |
| 5359 | } |
| 5360 | // This handles complex/complex, complex/float, or float/complex. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5361 | // When both operands are complex, the shorter operand is converted to the |
| 5362 | // type of the longer, and that is the type of the result. This corresponds |
| 5363 | // to what is done when combining two real floating-point operands. |
| 5364 | // The fun begins when size promotion occur across type domains. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5365 | // From H&S 6.3.4: When one operand is complex and the other is a real |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5366 | // floating-point type, the less precise type is converted, within it's |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5367 | // real or complex domain, to the precision of the other type. For example, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5368 | // when combining a "long double" with a "double _Complex", the |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5369 | // "double _Complex" is promoted to "long double _Complex". |
| 5370 | int result = getFloatingTypeOrder(lhs, rhs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5371 | |
| 5372 | if (result > 0) { // The left side is bigger, convert rhs. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5373 | rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5374 | } else if (result < 0) { // The right side is bigger, convert lhs. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5375 | lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5376 | } |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5377 | // At this point, lhs and rhs have the same rank/size. Now, make sure the |
| 5378 | // domains match. This is a requirement for our implementation, C99 |
| 5379 | // does not require this promotion. |
| 5380 | if (lhs != rhs) { // Domains don't match, we have complex/float mix. |
| 5381 | if (lhs->isRealFloatingType()) { // handle "double, _Complex double". |
| 5382 | return rhs; |
| 5383 | } else { // handle "_Complex double, double". |
| 5384 | return lhs; |
| 5385 | } |
| 5386 | } |
| 5387 | return lhs; // The domain/size match exactly. |
| 5388 | } |
| 5389 | // Now handle "real" floating types (i.e. float, double, long double). |
| 5390 | if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) { |
| 5391 | // if we have an integer operand, the result is the real floating type. |
| 5392 | if (rhs->isIntegerType()) { |
| 5393 | // convert rhs to the lhs floating point type. |
| 5394 | return lhs; |
| 5395 | } |
| 5396 | if (rhs->isComplexIntegerType()) { |
| 5397 | // convert rhs to the complex floating point type. |
| 5398 | return getComplexType(lhs); |
| 5399 | } |
| 5400 | if (lhs->isIntegerType()) { |
| 5401 | // convert lhs to the rhs floating point type. |
| 5402 | return rhs; |
| 5403 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5404 | if (lhs->isComplexIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5405 | // convert lhs to the complex floating point type. |
| 5406 | return getComplexType(rhs); |
| 5407 | } |
| 5408 | // We have two real floating types, float/complex combos were handled above. |
| 5409 | // Convert the smaller operand to the bigger result. |
| 5410 | int result = getFloatingTypeOrder(lhs, rhs); |
| 5411 | if (result > 0) // convert the rhs |
| 5412 | return lhs; |
| 5413 | assert(result < 0 && "illegal float comparison"); |
| 5414 | return rhs; // convert the lhs |
| 5415 | } |
| 5416 | if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) { |
| 5417 | // Handle GCC complex int extension. |
| 5418 | const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType(); |
| 5419 | const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType(); |
| 5420 | |
| 5421 | if (lhsComplexInt && rhsComplexInt) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5422 | if (getIntegerTypeOrder(lhsComplexInt->getElementType(), |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5423 | rhsComplexInt->getElementType()) >= 0) |
| 5424 | return lhs; // convert the rhs |
| 5425 | return rhs; |
| 5426 | } else if (lhsComplexInt && rhs->isIntegerType()) { |
| 5427 | // convert the rhs to the lhs complex type. |
| 5428 | return lhs; |
| 5429 | } else if (rhsComplexInt && lhs->isIntegerType()) { |
| 5430 | // convert the lhs to the rhs complex type. |
| 5431 | return rhs; |
| 5432 | } |
| 5433 | } |
| 5434 | // Finally, we have two differing integer types. |
| 5435 | // The rules for this case are in C99 6.3.1.8 |
| 5436 | int compare = getIntegerTypeOrder(lhs, rhs); |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5437 | bool lhsSigned = lhs->hasSignedIntegerRepresentation(), |
| 5438 | rhsSigned = rhs->hasSignedIntegerRepresentation(); |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5439 | QualType destType; |
| 5440 | if (lhsSigned == rhsSigned) { |
| 5441 | // Same signedness; use the higher-ranked type |
| 5442 | destType = compare >= 0 ? lhs : rhs; |
| 5443 | } else if (compare != (lhsSigned ? 1 : -1)) { |
| 5444 | // The unsigned type has greater than or equal rank to the |
| 5445 | // signed type, so use the unsigned type |
| 5446 | destType = lhsSigned ? rhs : lhs; |
| 5447 | } else if (getIntWidth(lhs) != getIntWidth(rhs)) { |
| 5448 | // The two types are different widths; if we are here, that |
| 5449 | // means the signed type is larger than the unsigned type, so |
| 5450 | // use the signed type. |
| 5451 | destType = lhsSigned ? lhs : rhs; |
| 5452 | } else { |
| 5453 | // The signed type is higher-ranked than the unsigned type, |
| 5454 | // but isn't actually any bigger (like unsigned int and long |
| 5455 | // on most 32-bit systems). Use the unsigned type corresponding |
| 5456 | // to the signed type. |
| 5457 | destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs); |
| 5458 | } |
| 5459 | return destType; |
| 5460 | } |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5461 | |
| 5462 | GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) { |
| 5463 | GVALinkage External = GVA_StrongExternal; |
| 5464 | |
| 5465 | Linkage L = FD->getLinkage(); |
| 5466 | if (L == ExternalLinkage && getLangOptions().CPlusPlus && |
| 5467 | FD->getType()->getLinkage() == UniqueExternalLinkage) |
| 5468 | L = UniqueExternalLinkage; |
| 5469 | |
| 5470 | switch (L) { |
| 5471 | case NoLinkage: |
| 5472 | case InternalLinkage: |
| 5473 | case UniqueExternalLinkage: |
| 5474 | return GVA_Internal; |
| 5475 | |
| 5476 | case ExternalLinkage: |
| 5477 | switch (FD->getTemplateSpecializationKind()) { |
| 5478 | case TSK_Undeclared: |
| 5479 | case TSK_ExplicitSpecialization: |
| 5480 | External = GVA_StrongExternal; |
| 5481 | break; |
| 5482 | |
| 5483 | case TSK_ExplicitInstantiationDefinition: |
| 5484 | return GVA_ExplicitTemplateInstantiation; |
| 5485 | |
| 5486 | case TSK_ExplicitInstantiationDeclaration: |
| 5487 | case TSK_ImplicitInstantiation: |
| 5488 | External = GVA_TemplateInstantiation; |
| 5489 | break; |
| 5490 | } |
| 5491 | } |
| 5492 | |
| 5493 | if (!FD->isInlined()) |
| 5494 | return External; |
| 5495 | |
| 5496 | if (!getLangOptions().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) { |
| 5497 | // GNU or C99 inline semantics. Determine whether this symbol should be |
| 5498 | // externally visible. |
| 5499 | if (FD->isInlineDefinitionExternallyVisible()) |
| 5500 | return External; |
| 5501 | |
| 5502 | // C99 inline semantics, where the symbol is not externally visible. |
| 5503 | return GVA_C99Inline; |
| 5504 | } |
| 5505 | |
| 5506 | // C++0x [temp.explicit]p9: |
| 5507 | // [ Note: The intent is that an inline function that is the subject of |
| 5508 | // an explicit instantiation declaration will still be implicitly |
| 5509 | // instantiated when used so that the body can be considered for |
| 5510 | // inlining, but that no out-of-line copy of the inline function would be |
| 5511 | // generated in the translation unit. -- end note ] |
| 5512 | if (FD->getTemplateSpecializationKind() |
| 5513 | == TSK_ExplicitInstantiationDeclaration) |
| 5514 | return GVA_C99Inline; |
| 5515 | |
| 5516 | return GVA_CXXInline; |
| 5517 | } |
| 5518 | |
| 5519 | GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) { |
| 5520 | // If this is a static data member, compute the kind of template |
| 5521 | // specialization. Otherwise, this variable is not part of a |
| 5522 | // template. |
| 5523 | TemplateSpecializationKind TSK = TSK_Undeclared; |
| 5524 | if (VD->isStaticDataMember()) |
| 5525 | TSK = VD->getTemplateSpecializationKind(); |
| 5526 | |
| 5527 | Linkage L = VD->getLinkage(); |
| 5528 | if (L == ExternalLinkage && getLangOptions().CPlusPlus && |
| 5529 | VD->getType()->getLinkage() == UniqueExternalLinkage) |
| 5530 | L = UniqueExternalLinkage; |
| 5531 | |
| 5532 | switch (L) { |
| 5533 | case NoLinkage: |
| 5534 | case InternalLinkage: |
| 5535 | case UniqueExternalLinkage: |
| 5536 | return GVA_Internal; |
| 5537 | |
| 5538 | case ExternalLinkage: |
| 5539 | switch (TSK) { |
| 5540 | case TSK_Undeclared: |
| 5541 | case TSK_ExplicitSpecialization: |
| 5542 | return GVA_StrongExternal; |
| 5543 | |
| 5544 | case TSK_ExplicitInstantiationDeclaration: |
| 5545 | llvm_unreachable("Variable should not be instantiated"); |
| 5546 | // Fall through to treat this like any other instantiation. |
| 5547 | |
| 5548 | case TSK_ExplicitInstantiationDefinition: |
| 5549 | return GVA_ExplicitTemplateInstantiation; |
| 5550 | |
| 5551 | case TSK_ImplicitInstantiation: |
| 5552 | return GVA_TemplateInstantiation; |
| 5553 | } |
| 5554 | } |
| 5555 | |
| 5556 | return GVA_StrongExternal; |
| 5557 | } |
| 5558 | |
Argyrios Kyrtzidis | 4ac7c0b | 2010-07-29 20:08:05 +0000 | [diff] [blame] | 5559 | bool ASTContext::DeclMustBeEmitted(const Decl *D) { |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5560 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 5561 | if (!VD->isFileVarDecl()) |
| 5562 | return false; |
| 5563 | } else if (!isa<FunctionDecl>(D)) |
| 5564 | return false; |
| 5565 | |
Argyrios Kyrtzidis | ab411c8 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 5566 | // Weak references don't produce any output by themselves. |
| 5567 | if (D->hasAttr<WeakRefAttr>()) |
| 5568 | return false; |
| 5569 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5570 | // Aliases and used decls are required. |
| 5571 | if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>()) |
| 5572 | return true; |
| 5573 | |
| 5574 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 5575 | // Forward declarations aren't required. |
| 5576 | if (!FD->isThisDeclarationADefinition()) |
| 5577 | return false; |
| 5578 | |
| 5579 | // Constructors and destructors are required. |
| 5580 | if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>()) |
| 5581 | return true; |
| 5582 | |
| 5583 | // The key function for a class is required. |
| 5584 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 5585 | const CXXRecordDecl *RD = MD->getParent(); |
| 5586 | if (MD->isOutOfLine() && RD->isDynamicClass()) { |
| 5587 | const CXXMethodDecl *KeyFunc = getKeyFunction(RD); |
| 5588 | if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl()) |
| 5589 | return true; |
| 5590 | } |
| 5591 | } |
| 5592 | |
| 5593 | GVALinkage Linkage = GetGVALinkageForFunction(FD); |
| 5594 | |
| 5595 | // static, static inline, always_inline, and extern inline functions can |
| 5596 | // always be deferred. Normal inline functions can be deferred in C99/C++. |
| 5597 | // Implicit template instantiations can also be deferred in C++. |
| 5598 | if (Linkage == GVA_Internal || Linkage == GVA_C99Inline || |
| 5599 | Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) |
| 5600 | return false; |
| 5601 | return true; |
| 5602 | } |
| 5603 | |
| 5604 | const VarDecl *VD = cast<VarDecl>(D); |
| 5605 | assert(VD->isFileVarDecl() && "Expected file scoped var"); |
| 5606 | |
Argyrios Kyrtzidis | ab411c8 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 5607 | if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly) |
| 5608 | return false; |
| 5609 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5610 | // Structs that have non-trivial constructors or destructors are required. |
| 5611 | |
| 5612 | // FIXME: Handle references. |
| 5613 | if (const RecordType *RT = VD->getType()->getAs<RecordType>()) { |
| 5614 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5615 | if (RD->hasDefinition() && |
| 5616 | (!RD->hasTrivialConstructor() || !RD->hasTrivialDestructor())) |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5617 | return true; |
| 5618 | } |
| 5619 | } |
| 5620 | |
| 5621 | GVALinkage L = GetGVALinkageForVariable(VD); |
| 5622 | if (L == GVA_Internal || L == GVA_TemplateInstantiation) { |
| 5623 | if (!(VD->getInit() && VD->getInit()->HasSideEffects(*this))) |
| 5624 | return false; |
| 5625 | } |
| 5626 | |
| 5627 | return true; |
| 5628 | } |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 5629 | |
| 5630 | CXXABI::~CXXABI() {} |