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) { |
| 139 | if (!LangOpts.CPlusPlus) return NULL; |
| 140 | if (T.getCXXABI() == "microsoft") |
| 141 | return CreateMicrosoftCXXABI(*this); |
| 142 | else |
| 143 | return CreateItaniumCXXABI(*this); |
| 144 | } |
| 145 | |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 146 | ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM, |
Daniel Dunbar | 444be73 | 2009-11-13 05:51:54 +0000 | [diff] [blame] | 147 | const TargetInfo &t, |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 148 | IdentifierTable &idents, SelectorTable &sels, |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 149 | Builtin::Context &builtins, |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 150 | unsigned size_reserve) : |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 151 | TemplateSpecializationTypes(this_()), |
| 152 | DependentTemplateSpecializationTypes(this_()), |
Argyrios Kyrtzidis | 0061138 | 2010-07-04 21:44:19 +0000 | [diff] [blame] | 153 | GlobalNestedNameSpecifier(0), IsInt128Installed(false), |
| 154 | CFConstantStringTypeDecl(0), NSConstantStringTypeDecl(0), |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 155 | ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0), |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 156 | sigjmp_bufDecl(0), BlockDescriptorType(0), BlockDescriptorExtendedType(0), |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 157 | NullTypeSourceInfo(QualType()), |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 158 | SourceMgr(SM), LangOpts(LOpts), ABI(createCXXABI(t)), Target(t), |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 159 | Idents(idents), Selectors(sels), |
Ted Kremenek | ac9590e | 2010-05-10 20:40:08 +0000 | [diff] [blame] | 160 | BuiltinInfo(builtins), |
| 161 | DeclarationNames(*this), |
| 162 | ExternalSource(0), PrintingPolicy(LOpts), |
Ted Kremenek | f057bf7 | 2010-06-18 00:31:04 +0000 | [diff] [blame] | 163 | LastSDM(0, 0), |
| 164 | UniqueBlockByRefTypeID(0), UniqueBlockParmTypeID(0) { |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 165 | ObjCIdRedefinitionType = QualType(); |
| 166 | ObjCClassRedefinitionType = QualType(); |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 167 | ObjCSelRedefinitionType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 168 | if (size_reserve > 0) Types.reserve(size_reserve); |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 169 | TUDecl = TranslationUnitDecl::Create(*this); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 170 | InitBuiltinTypes(); |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 173 | ASTContext::~ASTContext() { |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 174 | // Release the DenseMaps associated with DeclContext objects. |
| 175 | // FIXME: Is this the ideal solution? |
| 176 | ReleaseDeclContextMaps(); |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 177 | |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 178 | // Call all of the deallocation functions. |
| 179 | for (unsigned I = 0, N = Deallocations.size(); I != N; ++I) |
| 180 | Deallocations[I].first(Deallocations[I].second); |
Douglas Gregor | 0054531 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 181 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 182 | // Release all of the memory associated with overridden C++ methods. |
| 183 | for (llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::iterator |
| 184 | OM = OverriddenMethods.begin(), OMEnd = OverriddenMethods.end(); |
| 185 | OM != OMEnd; ++OM) |
| 186 | OM->second.Destroy(); |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 187 | |
Ted Kremenek | dcfcfbe | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 188 | // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 189 | // because they can contain DenseMaps. |
| 190 | for (llvm::DenseMap<const ObjCContainerDecl*, |
| 191 | const ASTRecordLayout*>::iterator |
| 192 | I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) |
| 193 | // Increment in loop to prevent using deallocated memory. |
| 194 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 195 | R->Destroy(*this); |
| 196 | |
Ted Kremenek | dcfcfbe | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 197 | for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator |
| 198 | I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.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 | bbfd68d | 2009-12-23 21:13:52 +0000 | [diff] [blame] | 203 | } |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 204 | |
Douglas Gregor | 0054531 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 205 | void ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) { |
| 206 | Deallocations.push_back(std::make_pair(Callback, Data)); |
| 207 | } |
| 208 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 209 | void |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 210 | ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) { |
| 211 | ExternalSource.reset(Source.take()); |
| 212 | } |
| 213 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 214 | void ASTContext::PrintStats() const { |
| 215 | fprintf(stderr, "*** AST Context Stats:\n"); |
| 216 | fprintf(stderr, " %d types total.\n", (int)Types.size()); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 217 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 218 | unsigned counts[] = { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 219 | #define TYPE(Name, Parent) 0, |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 220 | #define ABSTRACT_TYPE(Name, Parent) |
| 221 | #include "clang/AST/TypeNodes.def" |
| 222 | 0 // Extra |
| 223 | }; |
Douglas Gregor | c2ee10d | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 224 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 225 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 226 | Type *T = Types[i]; |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 227 | counts[(unsigned)T->getTypeClass()]++; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 230 | unsigned Idx = 0; |
| 231 | unsigned TotalBytes = 0; |
| 232 | #define TYPE(Name, Parent) \ |
| 233 | if (counts[Idx]) \ |
| 234 | fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \ |
| 235 | TotalBytes += counts[Idx] * sizeof(Name##Type); \ |
| 236 | ++Idx; |
| 237 | #define ABSTRACT_TYPE(Name, Parent) |
| 238 | #include "clang/AST/TypeNodes.def" |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 239 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 240 | fprintf(stderr, "Total bytes = %d\n", int(TotalBytes)); |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 241 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 242 | // Implicit special member functions. |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 243 | fprintf(stderr, " %u/%u implicit default constructors created\n", |
| 244 | NumImplicitDefaultConstructorsDeclared, |
| 245 | NumImplicitDefaultConstructors); |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 246 | fprintf(stderr, " %u/%u implicit copy constructors created\n", |
| 247 | NumImplicitCopyConstructorsDeclared, |
| 248 | NumImplicitCopyConstructors); |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 249 | fprintf(stderr, " %u/%u implicit copy assignment operators created\n", |
| 250 | NumImplicitCopyAssignmentOperatorsDeclared, |
| 251 | NumImplicitCopyAssignmentOperators); |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 252 | fprintf(stderr, " %u/%u implicit destructors created\n", |
| 253 | NumImplicitDestructorsDeclared, NumImplicitDestructors); |
| 254 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 255 | if (ExternalSource.get()) { |
| 256 | fprintf(stderr, "\n"); |
| 257 | ExternalSource->PrintStats(); |
| 258 | } |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 259 | |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 260 | BumpAlloc.PrintStats(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 264 | void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 265 | BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 266 | R = CanQualType::CreateUnsafe(QualType(Ty, 0)); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 267 | Types.push_back(Ty); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 270 | void ASTContext::InitBuiltinTypes() { |
| 271 | assert(VoidTy.isNull() && "Context reinitialized?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 272 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 273 | // C99 6.2.5p19. |
| 274 | InitBuiltinType(VoidTy, BuiltinType::Void); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 275 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 276 | // C99 6.2.5p2. |
| 277 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
| 278 | // C99 6.2.5p3. |
Eli Friedman | 15b9176 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 279 | if (LangOpts.CharIsSigned) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 280 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 281 | else |
| 282 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
| 283 | // C99 6.2.5p4. |
| 284 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 285 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 286 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 287 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 288 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 289 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 290 | // C99 6.2.5p6. |
| 291 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 292 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 293 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 294 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 295 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 296 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 297 | // C99 6.2.5p10. |
| 298 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 299 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 300 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 301 | |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 302 | // GNU extension, 128-bit integers. |
| 303 | InitBuiltinType(Int128Ty, BuiltinType::Int128); |
| 304 | InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128); |
| 305 | |
Chris Lattner | 3a25032 | 2009-02-26 23:43:47 +0000 | [diff] [blame] | 306 | if (LangOpts.CPlusPlus) // C++ 3.9.1p5 |
| 307 | InitBuiltinType(WCharTy, BuiltinType::WChar); |
| 308 | else // C99 |
| 309 | WCharTy = getFromTargetType(Target.getWCharType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 310 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 311 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 312 | InitBuiltinType(Char16Ty, BuiltinType::Char16); |
| 313 | else // C99 |
| 314 | Char16Ty = getFromTargetType(Target.getChar16Type()); |
| 315 | |
| 316 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 317 | InitBuiltinType(Char32Ty, BuiltinType::Char32); |
| 318 | else // C99 |
| 319 | Char32Ty = getFromTargetType(Target.getChar32Type()); |
| 320 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 321 | // Placeholder type for functions. |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 322 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
| 323 | |
| 324 | // Placeholder type for type-dependent expressions whose type is |
| 325 | // completely unknown. No code should ever check a type against |
| 326 | // DependentTy and users should never see it; however, it is here to |
| 327 | // help diagnose failures to properly check for type-dependent |
| 328 | // expressions. |
| 329 | InitBuiltinType(DependentTy, BuiltinType::Dependent); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 330 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 331 | // Placeholder type for C++0x auto declarations whose real type has |
Anders Carlsson | e89d159 | 2009-06-26 18:41:36 +0000 | [diff] [blame] | 332 | // not yet been deduced. |
| 333 | InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 334 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 335 | // C99 6.2.5p11. |
| 336 | FloatComplexTy = getComplexType(FloatTy); |
| 337 | DoubleComplexTy = getComplexType(DoubleTy); |
| 338 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 339 | |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 340 | BuiltinVaListType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 341 | |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 342 | // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope(). |
| 343 | ObjCIdTypedefType = QualType(); |
| 344 | ObjCClassTypedefType = QualType(); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 345 | ObjCSelTypedefType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 346 | |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 347 | // Builtin types for 'id', 'Class', and 'SEL'. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 348 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); |
| 349 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 350 | InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 351 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 352 | ObjCConstantStringType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 353 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 354 | // void * type |
| 355 | VoidPtrTy = getPointerType(VoidTy); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 356 | |
| 357 | // nullptr type (C++0x 2.14.7) |
| 358 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 361 | MemberSpecializationInfo * |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 362 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 363 | assert(Var->isStaticDataMember() && "Not a static data member"); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 364 | llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 365 | = InstantiatedFromStaticDataMember.find(Var); |
| 366 | if (Pos == InstantiatedFromStaticDataMember.end()) |
| 367 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 368 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 369 | return Pos->second; |
| 370 | } |
| 371 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 372 | void |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 373 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, |
Argyrios Kyrtzidis | 9421adc | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 374 | TemplateSpecializationKind TSK, |
| 375 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 376 | assert(Inst->isStaticDataMember() && "Not a static data member"); |
| 377 | assert(Tmpl->isStaticDataMember() && "Not a static data member"); |
| 378 | assert(!InstantiatedFromStaticDataMember[Inst] && |
| 379 | "Already noted what static data member was instantiated from"); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 380 | InstantiatedFromStaticDataMember[Inst] |
Argyrios Kyrtzidis | 9421adc | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 381 | = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 382 | } |
| 383 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 384 | NamedDecl * |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 385 | ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 386 | llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 387 | = InstantiatedFromUsingDecl.find(UUD); |
| 388 | if (Pos == InstantiatedFromUsingDecl.end()) |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 389 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 390 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 391 | return Pos->second; |
| 392 | } |
| 393 | |
| 394 | void |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 395 | ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) { |
| 396 | assert((isa<UsingDecl>(Pattern) || |
| 397 | isa<UnresolvedUsingValueDecl>(Pattern) || |
| 398 | isa<UnresolvedUsingTypenameDecl>(Pattern)) && |
| 399 | "pattern decl is not a using decl"); |
| 400 | assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists"); |
| 401 | InstantiatedFromUsingDecl[Inst] = Pattern; |
| 402 | } |
| 403 | |
| 404 | UsingShadowDecl * |
| 405 | ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) { |
| 406 | llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos |
| 407 | = InstantiatedFromUsingShadowDecl.find(Inst); |
| 408 | if (Pos == InstantiatedFromUsingShadowDecl.end()) |
| 409 | return 0; |
| 410 | |
| 411 | return Pos->second; |
| 412 | } |
| 413 | |
| 414 | void |
| 415 | ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, |
| 416 | UsingShadowDecl *Pattern) { |
| 417 | assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists"); |
| 418 | InstantiatedFromUsingShadowDecl[Inst] = Pattern; |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 421 | FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { |
| 422 | llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos |
| 423 | = InstantiatedFromUnnamedFieldDecl.find(Field); |
| 424 | if (Pos == InstantiatedFromUnnamedFieldDecl.end()) |
| 425 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 426 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 427 | return Pos->second; |
| 428 | } |
| 429 | |
| 430 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, |
| 431 | FieldDecl *Tmpl) { |
| 432 | assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed"); |
| 433 | assert(!Tmpl->getDeclName() && "Template field decl is not unnamed"); |
| 434 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] && |
| 435 | "Already noted what unnamed field was instantiated from"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 436 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 437 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; |
| 438 | } |
| 439 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 440 | ASTContext::overridden_cxx_method_iterator |
| 441 | ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const { |
| 442 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 443 | = OverriddenMethods.find(Method); |
| 444 | if (Pos == OverriddenMethods.end()) |
| 445 | return 0; |
| 446 | |
| 447 | return Pos->second.begin(); |
| 448 | } |
| 449 | |
| 450 | ASTContext::overridden_cxx_method_iterator |
| 451 | ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const { |
| 452 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 453 | = OverriddenMethods.find(Method); |
| 454 | if (Pos == OverriddenMethods.end()) |
| 455 | return 0; |
| 456 | |
| 457 | return Pos->second.end(); |
| 458 | } |
| 459 | |
Argyrios Kyrtzidis | c91e9f4 | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 460 | unsigned |
| 461 | ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const { |
| 462 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 463 | = OverriddenMethods.find(Method); |
| 464 | if (Pos == OverriddenMethods.end()) |
| 465 | return 0; |
| 466 | |
| 467 | return Pos->second.size(); |
| 468 | } |
| 469 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 470 | void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method, |
| 471 | const CXXMethodDecl *Overridden) { |
| 472 | OverriddenMethods[Method].push_back(Overridden); |
| 473 | } |
| 474 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 475 | //===----------------------------------------------------------------------===// |
| 476 | // Type Sizing and Analysis |
| 477 | //===----------------------------------------------------------------------===// |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 478 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 479 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 480 | /// scalar floating point type. |
| 481 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 482 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 483 | assert(BT && "Not a floating point type!"); |
| 484 | switch (BT->getKind()) { |
| 485 | default: assert(0 && "Not a floating point type!"); |
| 486 | case BuiltinType::Float: return Target.getFloatFormat(); |
| 487 | case BuiltinType::Double: return Target.getDoubleFormat(); |
| 488 | case BuiltinType::LongDouble: return Target.getLongDoubleFormat(); |
| 489 | } |
| 490 | } |
| 491 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 492 | /// getDeclAlign - Return a conservative estimate of the alignment of the |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 493 | /// specified decl. Note that bitfields do not have a valid alignment, so |
| 494 | /// this method will assert on them. |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 495 | /// If @p RefAsPointee, references are treated like their underlying type |
| 496 | /// (for alignof), else they're treated like pointers (for CodeGen). |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 497 | CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) { |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 498 | unsigned Align = Target.getCharWidth(); |
| 499 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame^] | 500 | Align = std::max(Align, D->getMaxAlignment()); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 501 | |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 502 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
| 503 | QualType T = VD->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 504 | if (const ReferenceType* RT = T->getAs<ReferenceType>()) { |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 505 | if (RefAsPointee) |
| 506 | T = RT->getPointeeType(); |
| 507 | else |
| 508 | T = getPointerType(RT->getPointeeType()); |
| 509 | } |
| 510 | if (!T->isIncompleteType() && !T->isFunctionType()) { |
Rafael Espindola | 6deecb0 | 2010-06-04 23:15:27 +0000 | [diff] [blame] | 511 | unsigned MinWidth = Target.getLargeArrayMinWidth(); |
| 512 | unsigned ArrayAlign = Target.getLargeArrayAlign(); |
| 513 | if (isa<VariableArrayType>(T) && MinWidth != 0) |
| 514 | Align = std::max(Align, ArrayAlign); |
| 515 | if (ConstantArrayType *CT = dyn_cast<ConstantArrayType>(T)) { |
| 516 | unsigned Size = getTypeSize(CT); |
| 517 | if (MinWidth != 0 && MinWidth <= Size) |
| 518 | Align = std::max(Align, ArrayAlign); |
| 519 | } |
Anders Carlsson | 4cc2cfd | 2009-04-10 04:47:03 +0000 | [diff] [blame] | 520 | // Incomplete or function types default to 1. |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 521 | while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T)) |
| 522 | T = cast<ArrayType>(T)->getElementType(); |
| 523 | |
| 524 | Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); |
| 525 | } |
Charles Davis | 05f6247 | 2010-02-23 04:52:00 +0000 | [diff] [blame] | 526 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) { |
| 527 | // In the case of a field in a packed struct, we want the minimum |
| 528 | // of the alignment of the field and the alignment of the struct. |
| 529 | Align = std::min(Align, |
| 530 | getPreferredTypeAlign(FD->getParent()->getTypeForDecl())); |
| 531 | } |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 532 | } |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 533 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 534 | return CharUnits::fromQuantity(Align / Target.getCharWidth()); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 535 | } |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 536 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 537 | std::pair<CharUnits, CharUnits> |
| 538 | ASTContext::getTypeInfoInChars(const Type *T) { |
| 539 | std::pair<uint64_t, unsigned> Info = getTypeInfo(T); |
| 540 | return std::make_pair(CharUnits::fromQuantity(Info.first / getCharWidth()), |
| 541 | CharUnits::fromQuantity(Info.second / getCharWidth())); |
| 542 | } |
| 543 | |
| 544 | std::pair<CharUnits, CharUnits> |
| 545 | ASTContext::getTypeInfoInChars(QualType T) { |
| 546 | return getTypeInfoInChars(T.getTypePtr()); |
| 547 | } |
| 548 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 549 | /// getTypeSize - Return the size of the specified type, in bits. This method |
| 550 | /// does not work on incomplete types. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 551 | /// |
| 552 | /// FIXME: Pointers into different addr spaces could have different sizes and |
| 553 | /// alignment requirements: getPointerInfo should take an AddrSpace, this |
| 554 | /// should take a QualType, &c. |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 555 | std::pair<uint64_t, unsigned> |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 556 | ASTContext::getTypeInfo(const Type *T) { |
Mike Stump | 5e30100 | 2009-02-27 18:32:39 +0000 | [diff] [blame] | 557 | uint64_t Width=0; |
| 558 | unsigned Align=8; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 559 | switch (T->getTypeClass()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 560 | #define TYPE(Class, Base) |
| 561 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 562 | #define NON_CANONICAL_TYPE(Class, Base) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 563 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 564 | #include "clang/AST/TypeNodes.def" |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 565 | assert(false && "Should not see dependent types"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 566 | break; |
| 567 | |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 568 | case Type::FunctionNoProto: |
| 569 | case Type::FunctionProto: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 570 | // GCC extension: alignof(function) = 32 bits |
| 571 | Width = 0; |
| 572 | Align = 32; |
| 573 | break; |
| 574 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 575 | case Type::IncompleteArray: |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 576 | case Type::VariableArray: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 577 | Width = 0; |
| 578 | Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); |
| 579 | break; |
| 580 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 581 | case Type::ConstantArray: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 582 | const ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 583 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 584 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 585 | Width = EltInfo.first*CAT->getSize().getZExtValue(); |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 586 | Align = EltInfo.second; |
| 587 | break; |
Christopher Lamb | 5c09a02 | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 588 | } |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 589 | case Type::ExtVector: |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 590 | case Type::Vector: { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 591 | const VectorType *VT = cast<VectorType>(T); |
| 592 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType()); |
| 593 | Width = EltInfo.first*VT->getNumElements(); |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 594 | Align = Width; |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 595 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 596 | // This happens for non-power-of-2 length vectors. |
Dan Gohman | 8eefcd3 | 2010-04-21 23:32:43 +0000 | [diff] [blame] | 597 | if (Align & (Align-1)) { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 598 | Align = llvm::NextPowerOf2(Align); |
| 599 | Width = llvm::RoundUpToAlignment(Width, Align); |
| 600 | } |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 601 | break; |
| 602 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 603 | |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 604 | case Type::Builtin: |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 605 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 606 | default: assert(0 && "Unknown builtin type!"); |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 607 | case BuiltinType::Void: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 608 | // GCC extension: alignof(void) = 8 bits. |
| 609 | Width = 0; |
| 610 | Align = 8; |
| 611 | break; |
| 612 | |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 613 | case BuiltinType::Bool: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 614 | Width = Target.getBoolWidth(); |
| 615 | Align = Target.getBoolAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 616 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 617 | case BuiltinType::Char_S: |
| 618 | case BuiltinType::Char_U: |
| 619 | case BuiltinType::UChar: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 620 | case BuiltinType::SChar: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 621 | Width = Target.getCharWidth(); |
| 622 | Align = Target.getCharAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 623 | break; |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 624 | case BuiltinType::WChar: |
| 625 | Width = Target.getWCharWidth(); |
| 626 | Align = Target.getWCharAlign(); |
| 627 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 628 | case BuiltinType::Char16: |
| 629 | Width = Target.getChar16Width(); |
| 630 | Align = Target.getChar16Align(); |
| 631 | break; |
| 632 | case BuiltinType::Char32: |
| 633 | Width = Target.getChar32Width(); |
| 634 | Align = Target.getChar32Align(); |
| 635 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 636 | case BuiltinType::UShort: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 637 | case BuiltinType::Short: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 638 | Width = Target.getShortWidth(); |
| 639 | Align = Target.getShortAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 640 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 641 | case BuiltinType::UInt: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 642 | case BuiltinType::Int: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 643 | Width = Target.getIntWidth(); |
| 644 | Align = Target.getIntAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 645 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 646 | case BuiltinType::ULong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 647 | case BuiltinType::Long: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 648 | Width = Target.getLongWidth(); |
| 649 | Align = Target.getLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 650 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 651 | case BuiltinType::ULongLong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 652 | case BuiltinType::LongLong: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 653 | Width = Target.getLongLongWidth(); |
| 654 | Align = Target.getLongLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 655 | break; |
Chris Lattner | ec16cb9 | 2009-04-30 02:55:13 +0000 | [diff] [blame] | 656 | case BuiltinType::Int128: |
| 657 | case BuiltinType::UInt128: |
| 658 | Width = 128; |
| 659 | Align = 128; // int128_t is 128-bit aligned on all targets. |
| 660 | break; |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 661 | case BuiltinType::Float: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 662 | Width = Target.getFloatWidth(); |
| 663 | Align = Target.getFloatAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 664 | break; |
| 665 | case BuiltinType::Double: |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 666 | Width = Target.getDoubleWidth(); |
| 667 | Align = Target.getDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 668 | break; |
| 669 | case BuiltinType::LongDouble: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 670 | Width = Target.getLongDoubleWidth(); |
| 671 | Align = Target.getLongDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 672 | break; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 673 | case BuiltinType::NullPtr: |
| 674 | Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) |
| 675 | Align = Target.getPointerAlign(0); // == sizeof(void*) |
Sebastian Redl | 1590d9c | 2009-05-27 19:34:06 +0000 | [diff] [blame] | 676 | break; |
Fariborz Jahanian | e04f5fc | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 677 | case BuiltinType::ObjCId: |
| 678 | case BuiltinType::ObjCClass: |
| 679 | case BuiltinType::ObjCSel: |
| 680 | Width = Target.getPointerWidth(0); |
| 681 | Align = Target.getPointerAlign(0); |
| 682 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 683 | } |
Chris Lattner | bfef6d7 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 684 | break; |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 685 | case Type::ObjCObjectPointer: |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 686 | Width = Target.getPointerWidth(0); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 687 | Align = Target.getPointerAlign(0); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 688 | break; |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 689 | case Type::BlockPointer: { |
| 690 | unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace(); |
| 691 | Width = Target.getPointerWidth(AS); |
| 692 | Align = Target.getPointerAlign(AS); |
| 693 | break; |
| 694 | } |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 695 | case Type::LValueReference: |
| 696 | case Type::RValueReference: { |
| 697 | // alignof and sizeof should never enter this code path here, so we go |
| 698 | // the pointer route. |
| 699 | unsigned AS = cast<ReferenceType>(T)->getPointeeType().getAddressSpace(); |
| 700 | Width = Target.getPointerWidth(AS); |
| 701 | Align = Target.getPointerAlign(AS); |
| 702 | break; |
| 703 | } |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 704 | case Type::Pointer: { |
| 705 | unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace(); |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 706 | Width = Target.getPointerWidth(AS); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 707 | Align = Target.getPointerAlign(AS); |
| 708 | break; |
| 709 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 710 | case Type::MemberPointer: { |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 711 | const MemberPointerType *MPT = cast<MemberPointerType>(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 712 | std::pair<uint64_t, unsigned> PtrDiffInfo = |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 713 | getTypeInfo(getPointerDiffType()); |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 714 | Width = PtrDiffInfo.first * ABI->getMemberPointerSize(MPT); |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 715 | Align = PtrDiffInfo.second; |
| 716 | break; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 717 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 718 | case Type::Complex: { |
| 719 | // Complex types have the same alignment as their elements, but twice the |
| 720 | // size. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 721 | std::pair<uint64_t, unsigned> EltInfo = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 722 | getTypeInfo(cast<ComplexType>(T)->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 723 | Width = EltInfo.first*2; |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 724 | Align = EltInfo.second; |
| 725 | break; |
| 726 | } |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 727 | case Type::ObjCObject: |
| 728 | return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr()); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 729 | case Type::ObjCInterface: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 730 | const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 731 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
| 732 | Width = Layout.getSize(); |
| 733 | Align = Layout.getAlignment(); |
| 734 | break; |
| 735 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 736 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 737 | case Type::Enum: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 738 | const TagType *TT = cast<TagType>(T); |
| 739 | |
| 740 | if (TT->getDecl()->isInvalidDecl()) { |
Chris Lattner | 8389eab | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 741 | Width = 1; |
| 742 | Align = 1; |
| 743 | break; |
| 744 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 745 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 746 | if (const EnumType *ET = dyn_cast<EnumType>(TT)) |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 747 | return getTypeInfo(ET->getDecl()->getIntegerType()); |
| 748 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 749 | const RecordType *RT = cast<RecordType>(TT); |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 750 | const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); |
| 751 | Width = Layout.getSize(); |
| 752 | Align = Layout.getAlignment(); |
Chris Lattner | dc0d73e | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 753 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 754 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 755 | |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 756 | case Type::SubstTemplateTypeParm: |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 757 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> |
| 758 | getReplacementType().getTypePtr()); |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 759 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 760 | case Type::Typedef: { |
| 761 | const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl(); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame^] | 762 | Align = std::max(Typedef->getMaxAlignment(), |
| 763 | getTypeAlign(Typedef->getUnderlyingType().getTypePtr())); |
| 764 | Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr()); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 765 | break; |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 766 | } |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 767 | |
| 768 | case Type::TypeOfExpr: |
| 769 | return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType() |
| 770 | .getTypePtr()); |
| 771 | |
| 772 | case Type::TypeOf: |
| 773 | return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr()); |
| 774 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 775 | case Type::Decltype: |
| 776 | return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType() |
| 777 | .getTypePtr()); |
| 778 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 779 | case Type::Elaborated: |
| 780 | return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 781 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 782 | case Type::TemplateSpecialization: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 783 | assert(getCanonicalType(T) != T && |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 784 | "Cannot request the size of a dependent type"); |
| 785 | // FIXME: this is likely to be wrong once we support template |
| 786 | // aliases, since a template alias could refer to a typedef that |
| 787 | // has an __aligned__ attribute on it. |
| 788 | return getTypeInfo(getCanonicalType(T)); |
| 789 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 790 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 791 | assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2"); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 792 | return std::make_pair(Width, Align); |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 793 | } |
| 794 | |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 795 | /// getTypeSizeInChars - Return the size of the specified type, in characters. |
| 796 | /// This method does not work on incomplete types. |
| 797 | CharUnits ASTContext::getTypeSizeInChars(QualType T) { |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 798 | return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth()); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 799 | } |
| 800 | CharUnits ASTContext::getTypeSizeInChars(const Type *T) { |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 801 | return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth()); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 802 | } |
| 803 | |
Ken Dyck | 16e20cc | 2010-01-26 17:25:18 +0000 | [diff] [blame] | 804 | /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 805 | /// characters. This method does not work on incomplete types. |
| 806 | CharUnits ASTContext::getTypeAlignInChars(QualType T) { |
| 807 | return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth()); |
| 808 | } |
| 809 | CharUnits ASTContext::getTypeAlignInChars(const Type *T) { |
| 810 | return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth()); |
| 811 | } |
| 812 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 813 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 814 | /// type for the current target in bits. This can be different than the ABI |
| 815 | /// alignment in cases where it is beneficial for performance to overalign |
| 816 | /// a data type. |
| 817 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) { |
| 818 | unsigned ABIAlign = getTypeAlign(T); |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 819 | |
| 820 | // Double and long long should be naturally aligned if possible. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 821 | if (const ComplexType* CT = T->getAs<ComplexType>()) |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 822 | T = CT->getElementType().getTypePtr(); |
| 823 | if (T->isSpecificBuiltinType(BuiltinType::Double) || |
| 824 | T->isSpecificBuiltinType(BuiltinType::LongLong)) |
| 825 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); |
| 826 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 827 | return ABIAlign; |
| 828 | } |
| 829 | |
Daniel Dunbar | a80a0f6 | 2009-04-22 17:43:55 +0000 | [diff] [blame] | 830 | static void CollectLocalObjCIvars(ASTContext *Ctx, |
| 831 | const ObjCInterfaceDecl *OI, |
| 832 | llvm::SmallVectorImpl<FieldDecl*> &Fields) { |
Fariborz Jahanian | a769c00 | 2008-12-17 21:40:49 +0000 | [diff] [blame] | 833 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 834 | E = OI->ivar_end(); I != E; ++I) { |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 835 | ObjCIvarDecl *IVDecl = *I; |
Fariborz Jahanian | a769c00 | 2008-12-17 21:40:49 +0000 | [diff] [blame] | 836 | if (!IVDecl->isInvalidDecl()) |
| 837 | Fields.push_back(cast<FieldDecl>(IVDecl)); |
| 838 | } |
| 839 | } |
| 840 | |
Daniel Dunbar | a80a0f6 | 2009-04-22 17:43:55 +0000 | [diff] [blame] | 841 | void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 842 | llvm::SmallVectorImpl<FieldDecl*> &Fields) { |
| 843 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
| 844 | CollectObjCIvars(SuperClass, Fields); |
| 845 | CollectLocalObjCIvars(this, OI, Fields); |
| 846 | } |
| 847 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 848 | /// ShallowCollectObjCIvars - |
| 849 | /// Collect all ivars, including those synthesized, in the current class. |
| 850 | /// |
| 851 | void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI, |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 852 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) { |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 853 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 854 | E = OI->ivar_end(); I != E; ++I) { |
| 855 | Ivars.push_back(*I); |
| 856 | } |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 857 | |
| 858 | CollectNonClassIvars(OI, Ivars); |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 859 | } |
| 860 | |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 861 | /// CollectNonClassIvars - |
| 862 | /// This routine collects all other ivars which are not declared in the class. |
Ted Kremenek | 7d2aa11 | 2010-03-11 19:44:54 +0000 | [diff] [blame] | 863 | /// This includes synthesized ivars (via @synthesize) and those in |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 864 | // class's @implementation. |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 865 | /// |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 866 | void ASTContext::CollectNonClassIvars(const ObjCInterfaceDecl *OI, |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 867 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) { |
Fariborz Jahanian | 0e5ad25 | 2010-02-23 01:26:30 +0000 | [diff] [blame] | 868 | // Find ivars declared in class extension. |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 869 | for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl; |
| 870 | CDecl = CDecl->getNextClassExtension()) { |
Fariborz Jahanian | 0e5ad25 | 2010-02-23 01:26:30 +0000 | [diff] [blame] | 871 | for (ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(), |
| 872 | E = CDecl->ivar_end(); I != E; ++I) { |
| 873 | Ivars.push_back(*I); |
| 874 | } |
| 875 | } |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 876 | |
Ted Kremenek | 7d2aa11 | 2010-03-11 19:44:54 +0000 | [diff] [blame] | 877 | // Also add any ivar defined in this class's implementation. This |
| 878 | // includes synthesized ivars. |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 879 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) { |
| 880 | for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(), |
| 881 | E = ImplDecl->ivar_end(); I != E; ++I) |
| 882 | Ivars.push_back(*I); |
| 883 | } |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 884 | } |
| 885 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 886 | /// CollectInheritedProtocols - Collect all protocols in current class and |
| 887 | /// those inherited by it. |
| 888 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 889 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 890 | if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
| 891 | for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(), |
| 892 | PE = OI->protocol_end(); P != PE; ++P) { |
| 893 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 894 | Protocols.insert(Proto); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 895 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 896 | PE = Proto->protocol_end(); P != PE; ++P) { |
| 897 | Protocols.insert(*P); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 898 | CollectInheritedProtocols(*P, Protocols); |
| 899 | } |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 900 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 901 | |
| 902 | // Categories of this Interface. |
| 903 | for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList(); |
| 904 | CDeclChain; CDeclChain = CDeclChain->getNextClassCategory()) |
| 905 | CollectInheritedProtocols(CDeclChain, Protocols); |
| 906 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) |
| 907 | while (SD) { |
| 908 | CollectInheritedProtocols(SD, Protocols); |
| 909 | SD = SD->getSuperClass(); |
| 910 | } |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 911 | } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 912 | for (ObjCInterfaceDecl::protocol_iterator P = OC->protocol_begin(), |
| 913 | PE = OC->protocol_end(); P != PE; ++P) { |
| 914 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 915 | Protocols.insert(Proto); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 916 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 917 | PE = Proto->protocol_end(); P != PE; ++P) |
| 918 | CollectInheritedProtocols(*P, Protocols); |
| 919 | } |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 920 | } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 921 | for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(), |
| 922 | PE = OP->protocol_end(); P != PE; ++P) { |
| 923 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 924 | Protocols.insert(Proto); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 925 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 926 | PE = Proto->protocol_end(); P != PE; ++P) |
| 927 | CollectInheritedProtocols(*P, Protocols); |
| 928 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 929 | } |
| 930 | } |
| 931 | |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 932 | unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) { |
| 933 | unsigned count = 0; |
| 934 | // Count ivars declared in class extension. |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 935 | for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl; |
| 936 | CDecl = CDecl->getNextClassExtension()) |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 937 | count += CDecl->ivar_size(); |
| 938 | |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 939 | // Count ivar defined in this class's implementation. This |
| 940 | // includes synthesized ivars. |
| 941 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 942 | count += ImplDecl->ivar_size(); |
| 943 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 944 | return count; |
| 945 | } |
| 946 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 947 | /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. |
| 948 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
| 949 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 950 | I = ObjCImpls.find(D); |
| 951 | if (I != ObjCImpls.end()) |
| 952 | return cast<ObjCImplementationDecl>(I->second); |
| 953 | return 0; |
| 954 | } |
| 955 | /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. |
| 956 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
| 957 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 958 | I = ObjCImpls.find(D); |
| 959 | if (I != ObjCImpls.end()) |
| 960 | return cast<ObjCCategoryImplDecl>(I->second); |
| 961 | return 0; |
| 962 | } |
| 963 | |
| 964 | /// \brief Set the implementation of ObjCInterfaceDecl. |
| 965 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
| 966 | ObjCImplementationDecl *ImplD) { |
| 967 | assert(IFaceD && ImplD && "Passed null params"); |
| 968 | ObjCImpls[IFaceD] = ImplD; |
| 969 | } |
| 970 | /// \brief Set the implementation of ObjCCategoryDecl. |
| 971 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
| 972 | ObjCCategoryImplDecl *ImplD) { |
| 973 | assert(CatD && ImplD && "Passed null params"); |
| 974 | ObjCImpls[CatD] = ImplD; |
| 975 | } |
| 976 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 977 | /// \brief Allocate an uninitialized TypeSourceInfo. |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 978 | /// |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 979 | /// The caller should initialize the memory held by TypeSourceInfo using |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 980 | /// the TypeLoc wrappers. |
| 981 | /// |
| 982 | /// \param T the type that will be the basis for type source info. This type |
| 983 | /// should refer to how the declarator was written in source code, not to |
| 984 | /// what type semantic analysis resolved the declarator to. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 985 | TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T, |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 986 | unsigned DataSize) { |
| 987 | if (!DataSize) |
| 988 | DataSize = TypeLoc::getFullDataSizeForType(T); |
| 989 | else |
| 990 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 991 | "incorrect data size provided to CreateTypeSourceInfo!"); |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 992 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 993 | TypeSourceInfo *TInfo = |
| 994 | (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8); |
| 995 | new (TInfo) TypeSourceInfo(T); |
| 996 | return TInfo; |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 997 | } |
| 998 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 999 | TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, |
John McCall | a4eb74d | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 1000 | SourceLocation L) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1001 | TypeSourceInfo *DI = CreateTypeSourceInfo(T); |
John McCall | a4eb74d | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 1002 | DI->getTypeLoc().initialize(L); |
| 1003 | return DI; |
| 1004 | } |
| 1005 | |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1006 | const ASTRecordLayout & |
| 1007 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) { |
| 1008 | return getObjCLayout(D, 0); |
| 1009 | } |
| 1010 | |
| 1011 | const ASTRecordLayout & |
| 1012 | ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) { |
| 1013 | return getObjCLayout(D->getClassInterface(), D); |
| 1014 | } |
| 1015 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1016 | //===----------------------------------------------------------------------===// |
| 1017 | // Type creation/memoization methods |
| 1018 | //===----------------------------------------------------------------------===// |
| 1019 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1020 | QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) { |
| 1021 | unsigned Fast = Quals.getFastQualifiers(); |
| 1022 | Quals.removeFastQualifiers(); |
| 1023 | |
| 1024 | // Check if we've already instantiated this type. |
| 1025 | llvm::FoldingSetNodeID ID; |
| 1026 | ExtQuals::Profile(ID, TypeNode, Quals); |
| 1027 | void *InsertPos = 0; |
| 1028 | if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) { |
| 1029 | assert(EQ->getQualifiers() == Quals); |
| 1030 | QualType T = QualType(EQ, Fast); |
| 1031 | return T; |
| 1032 | } |
| 1033 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1034 | ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1035 | ExtQualNodes.InsertNode(New, InsertPos); |
| 1036 | QualType T = QualType(New, Fast); |
| 1037 | return T; |
| 1038 | } |
| 1039 | |
| 1040 | QualType ASTContext::getVolatileType(QualType T) { |
| 1041 | QualType CanT = getCanonicalType(T); |
| 1042 | if (CanT.isVolatileQualified()) return T; |
| 1043 | |
| 1044 | QualifierCollector Quals; |
| 1045 | const Type *TypeNode = Quals.strip(T); |
| 1046 | Quals.addVolatile(); |
| 1047 | |
| 1048 | return getExtQualType(TypeNode, Quals); |
| 1049 | } |
| 1050 | |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 1051 | QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1052 | QualType CanT = getCanonicalType(T); |
| 1053 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1054 | return T; |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1055 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1056 | // If we are composing extended qualifiers together, merge together |
| 1057 | // into one ExtQuals node. |
| 1058 | QualifierCollector Quals; |
| 1059 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1060 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1061 | // If this type already has an address space specified, it cannot get |
| 1062 | // another one. |
| 1063 | assert(!Quals.hasAddressSpace() && |
| 1064 | "Type cannot be in multiple addr spaces!"); |
| 1065 | Quals.addAddressSpace(AddressSpace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1066 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1067 | return getExtQualType(TypeNode, Quals); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1070 | QualType ASTContext::getObjCGCQualType(QualType T, |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1071 | Qualifiers::GC GCAttr) { |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1072 | QualType CanT = getCanonicalType(T); |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1073 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1074 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1075 | |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1076 | if (T->isPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1077 | QualType Pointee = T->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 1078 | if (Pointee->isAnyPointerType()) { |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1079 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 1080 | return getPointerType(ResultType); |
| 1081 | } |
| 1082 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1083 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1084 | // If we are composing extended qualifiers together, merge together |
| 1085 | // into one ExtQuals node. |
| 1086 | QualifierCollector Quals; |
| 1087 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1088 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1089 | // If this type already has an ObjCGC specified, it cannot get |
| 1090 | // another one. |
| 1091 | assert(!Quals.hasObjCGCAttr() && |
| 1092 | "Type cannot have multiple ObjCGCs!"); |
| 1093 | Quals.addObjCGCAttr(GCAttr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1094 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1095 | return getExtQualType(TypeNode, Quals); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1096 | } |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1097 | |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1098 | static QualType getExtFunctionType(ASTContext& Context, QualType T, |
| 1099 | const FunctionType::ExtInfo &Info) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1100 | QualType ResultType; |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1101 | if (const PointerType *Pointer = T->getAs<PointerType>()) { |
| 1102 | QualType Pointee = Pointer->getPointeeType(); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1103 | ResultType = getExtFunctionType(Context, Pointee, Info); |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1104 | if (ResultType == Pointee) |
| 1105 | return T; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1106 | |
| 1107 | ResultType = Context.getPointerType(ResultType); |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1108 | } else if (const BlockPointerType *BlockPointer |
| 1109 | = T->getAs<BlockPointerType>()) { |
| 1110 | QualType Pointee = BlockPointer->getPointeeType(); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1111 | ResultType = getExtFunctionType(Context, Pointee, Info); |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1112 | if (ResultType == Pointee) |
| 1113 | return T; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1114 | |
| 1115 | ResultType = Context.getBlockPointerType(ResultType); |
| 1116 | } else if (const FunctionType *F = T->getAs<FunctionType>()) { |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1117 | if (F->getExtInfo() == Info) |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1118 | return T; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1119 | |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1120 | if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(F)) { |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1121 | ResultType = Context.getFunctionNoProtoType(FNPT->getResultType(), |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1122 | Info); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1123 | } else { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1124 | const FunctionProtoType *FPT = cast<FunctionProtoType>(F); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1125 | ResultType |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1126 | = Context.getFunctionType(FPT->getResultType(), FPT->arg_type_begin(), |
| 1127 | FPT->getNumArgs(), FPT->isVariadic(), |
| 1128 | FPT->getTypeQuals(), |
| 1129 | FPT->hasExceptionSpec(), |
| 1130 | FPT->hasAnyExceptionSpec(), |
| 1131 | FPT->getNumExceptions(), |
| 1132 | FPT->exception_begin(), |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1133 | Info); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1134 | } |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1135 | } else |
| 1136 | return T; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1137 | |
| 1138 | return Context.getQualifiedType(ResultType, T.getLocalQualifiers()); |
| 1139 | } |
| 1140 | |
| 1141 | QualType ASTContext::getNoReturnType(QualType T, bool AddNoReturn) { |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 1142 | FunctionType::ExtInfo Info = getFunctionExtInfo(T); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1143 | return getExtFunctionType(*this, T, |
| 1144 | Info.withNoReturn(AddNoReturn)); |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | QualType ASTContext::getCallConvType(QualType T, CallingConv CallConv) { |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 1148 | FunctionType::ExtInfo Info = getFunctionExtInfo(T); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1149 | return getExtFunctionType(*this, T, |
| 1150 | Info.withCallingConv(CallConv)); |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 1153 | QualType ASTContext::getRegParmType(QualType T, unsigned RegParm) { |
| 1154 | FunctionType::ExtInfo Info = getFunctionExtInfo(T); |
| 1155 | return getExtFunctionType(*this, T, |
| 1156 | Info.withRegParm(RegParm)); |
| 1157 | } |
| 1158 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1159 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 1160 | /// number with the specified element type. |
| 1161 | QualType ASTContext::getComplexType(QualType T) { |
| 1162 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1163 | // structure. |
| 1164 | llvm::FoldingSetNodeID ID; |
| 1165 | ComplexType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1166 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1167 | void *InsertPos = 0; |
| 1168 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1169 | return QualType(CT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1170 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1171 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1172 | // so fill in the canonical type field. |
| 1173 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1174 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1175 | Canonical = getComplexType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1176 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1177 | // Get the new insert position for the node we care about. |
| 1178 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1179 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1180 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1181 | ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1182 | Types.push_back(New); |
| 1183 | ComplexTypes.InsertNode(New, InsertPos); |
| 1184 | return QualType(New, 0); |
| 1185 | } |
| 1186 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1187 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 1188 | /// the specified type. |
| 1189 | QualType ASTContext::getPointerType(QualType T) { |
| 1190 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1191 | // structure. |
| 1192 | llvm::FoldingSetNodeID ID; |
| 1193 | PointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1194 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1195 | void *InsertPos = 0; |
| 1196 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1197 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1198 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1199 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1200 | // so fill in the canonical type field. |
| 1201 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1202 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1203 | Canonical = getPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1204 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1205 | // Get the new insert position for the node we care about. |
| 1206 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1207 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1208 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1209 | PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1210 | Types.push_back(New); |
| 1211 | PointerTypes.InsertNode(New, InsertPos); |
| 1212 | return QualType(New, 0); |
| 1213 | } |
| 1214 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1215 | /// getBlockPointerType - Return the uniqued reference to the type for |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1216 | /// a pointer to the specified block. |
| 1217 | QualType ASTContext::getBlockPointerType(QualType T) { |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1218 | assert(T->isFunctionType() && "block of function types only"); |
| 1219 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1220 | // structure. |
| 1221 | llvm::FoldingSetNodeID ID; |
| 1222 | BlockPointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1223 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1224 | void *InsertPos = 0; |
| 1225 | if (BlockPointerType *PT = |
| 1226 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1227 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1228 | |
| 1229 | // 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] | 1230 | // type either so fill in the canonical type field. |
| 1231 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1232 | if (!T.isCanonical()) { |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1233 | Canonical = getBlockPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1234 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1235 | // Get the new insert position for the node we care about. |
| 1236 | BlockPointerType *NewIP = |
| 1237 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1238 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1239 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1240 | BlockPointerType *New |
| 1241 | = new (*this, TypeAlignment) BlockPointerType(T, Canonical); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1242 | Types.push_back(New); |
| 1243 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 1244 | return QualType(New, 0); |
| 1245 | } |
| 1246 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1247 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 1248 | /// lvalue reference to the specified type. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1249 | QualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1250 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1251 | // structure. |
| 1252 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1253 | ReferenceType::Profile(ID, T, SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1254 | |
| 1255 | void *InsertPos = 0; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1256 | if (LValueReferenceType *RT = |
| 1257 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1258 | return QualType(RT, 0); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1259 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1260 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1261 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1262 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1263 | // either, so fill in the canonical type field. |
| 1264 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1265 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
| 1266 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1267 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1268 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1269 | // Get the new insert position for the node we care about. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1270 | LValueReferenceType *NewIP = |
| 1271 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1272 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1275 | LValueReferenceType *New |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1276 | = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, |
| 1277 | SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1278 | Types.push_back(New); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1279 | LValueReferenceTypes.InsertNode(New, InsertPos); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1280 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1281 | return QualType(New, 0); |
| 1282 | } |
| 1283 | |
| 1284 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 1285 | /// rvalue reference to the specified type. |
| 1286 | QualType ASTContext::getRValueReferenceType(QualType T) { |
| 1287 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1288 | // structure. |
| 1289 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1290 | ReferenceType::Profile(ID, T, false); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1291 | |
| 1292 | void *InsertPos = 0; |
| 1293 | if (RValueReferenceType *RT = |
| 1294 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1295 | return QualType(RT, 0); |
| 1296 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1297 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1298 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1299 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1300 | // either, so fill in the canonical type field. |
| 1301 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1302 | if (InnerRef || !T.isCanonical()) { |
| 1303 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1304 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1305 | |
| 1306 | // Get the new insert position for the node we care about. |
| 1307 | RValueReferenceType *NewIP = |
| 1308 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1309 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
| 1310 | } |
| 1311 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1312 | RValueReferenceType *New |
| 1313 | = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1314 | Types.push_back(New); |
| 1315 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1316 | return QualType(New, 0); |
| 1317 | } |
| 1318 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1319 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 1320 | /// member pointer to the specified type, in the specified class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1321 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1322 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1323 | // structure. |
| 1324 | llvm::FoldingSetNodeID ID; |
| 1325 | MemberPointerType::Profile(ID, T, Cls); |
| 1326 | |
| 1327 | void *InsertPos = 0; |
| 1328 | if (MemberPointerType *PT = |
| 1329 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1330 | return QualType(PT, 0); |
| 1331 | |
| 1332 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 1333 | // type either, so fill in the canonical type field. |
| 1334 | QualType Canonical; |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1335 | if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1336 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 1337 | |
| 1338 | // Get the new insert position for the node we care about. |
| 1339 | MemberPointerType *NewIP = |
| 1340 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1341 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
| 1342 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1343 | MemberPointerType *New |
| 1344 | = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1345 | Types.push_back(New); |
| 1346 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 1347 | return QualType(New, 0); |
| 1348 | } |
| 1349 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1350 | /// getConstantArrayType - Return the unique reference to the type for an |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1351 | /// array of the specified element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1352 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1353 | const llvm::APInt &ArySizeIn, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1354 | ArrayType::ArraySizeModifier ASM, |
| 1355 | unsigned EltTypeQuals) { |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1356 | assert((EltTy->isDependentType() || |
| 1357 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && |
Eli Friedman | 587cbdf | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1358 | "Constant array of VLAs is illegal!"); |
| 1359 | |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1360 | // Convert the array size into a canonical width matching the pointer size for |
| 1361 | // the target. |
| 1362 | llvm::APInt ArySize(ArySizeIn); |
| 1363 | ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1364 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1365 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 0be2ef2 | 2009-02-19 17:31:02 +0000 | [diff] [blame] | 1366 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1367 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1368 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1369 | if (ConstantArrayType *ATP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1370 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1371 | return QualType(ATP, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1372 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1373 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1374 | // so fill in the canonical type field. |
| 1375 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1376 | if (!EltTy.isCanonical()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1377 | Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1378 | ASM, EltTypeQuals); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1379 | // Get the new insert position for the node we care about. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1380 | ConstantArrayType *NewIP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1381 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1382 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1383 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1384 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1385 | ConstantArrayType *New = new(*this,TypeAlignment) |
| 1386 | ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1387 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1388 | Types.push_back(New); |
| 1389 | return QualType(New, 0); |
| 1390 | } |
| 1391 | |
Steve Naroff | bdbf7b0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 1392 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 1393 | /// variable array of the specified element type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1394 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 1395 | Expr *NumElts, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1396 | ArrayType::ArraySizeModifier ASM, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1397 | unsigned EltTypeQuals, |
| 1398 | SourceRange Brackets) { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1399 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 1400 | // that have an expression provided for their size. |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 1401 | QualType CanonType; |
| 1402 | |
| 1403 | if (!EltTy.isCanonical()) { |
| 1404 | if (NumElts) |
| 1405 | NumElts->Retain(); |
| 1406 | CanonType = getVariableArrayType(getCanonicalType(EltTy), NumElts, ASM, |
| 1407 | EltTypeQuals, Brackets); |
| 1408 | } |
| 1409 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1410 | VariableArrayType *New = new(*this, TypeAlignment) |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 1411 | VariableArrayType(EltTy, CanonType, NumElts, ASM, EltTypeQuals, Brackets); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1412 | |
| 1413 | VariableArrayTypes.push_back(New); |
| 1414 | Types.push_back(New); |
| 1415 | return QualType(New, 0); |
| 1416 | } |
| 1417 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1418 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 1419 | /// the type for a dependently-sized array of the specified element |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1420 | /// type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1421 | QualType ASTContext::getDependentSizedArrayType(QualType EltTy, |
| 1422 | Expr *NumElts, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1423 | ArrayType::ArraySizeModifier ASM, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1424 | unsigned EltTypeQuals, |
| 1425 | SourceRange Brackets) { |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1426 | assert((!NumElts || NumElts->isTypeDependent() || |
| 1427 | NumElts->isValueDependent()) && |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1428 | "Size must be type- or value-dependent!"); |
| 1429 | |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1430 | void *InsertPos = 0; |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1431 | DependentSizedArrayType *Canon = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1432 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1433 | |
| 1434 | if (NumElts) { |
| 1435 | // Dependently-sized array types that do not have a specified |
| 1436 | // number of elements will have their sizes deduced from an |
| 1437 | // initializer. |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1438 | DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM, |
| 1439 | EltTypeQuals, NumElts); |
| 1440 | |
| 1441 | Canon = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1442 | } |
| 1443 | |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1444 | DependentSizedArrayType *New; |
| 1445 | if (Canon) { |
| 1446 | // We already have a canonical version of this array type; use it as |
| 1447 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1448 | New = new (*this, TypeAlignment) |
| 1449 | DependentSizedArrayType(*this, EltTy, QualType(Canon, 0), |
| 1450 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1451 | } else { |
| 1452 | QualType CanonEltTy = getCanonicalType(EltTy); |
| 1453 | if (CanonEltTy == EltTy) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1454 | New = new (*this, TypeAlignment) |
| 1455 | DependentSizedArrayType(*this, EltTy, QualType(), |
| 1456 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1457 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1458 | if (NumElts) { |
| 1459 | DependentSizedArrayType *CanonCheck |
| 1460 | = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1461 | assert(!CanonCheck && "Dependent-sized canonical array type broken"); |
| 1462 | (void)CanonCheck; |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1463 | DependentSizedArrayTypes.InsertNode(New, InsertPos); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1464 | } |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1465 | } else { |
| 1466 | QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts, |
| 1467 | ASM, EltTypeQuals, |
| 1468 | SourceRange()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1469 | New = new (*this, TypeAlignment) |
| 1470 | DependentSizedArrayType(*this, EltTy, Canon, |
| 1471 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1472 | } |
| 1473 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1474 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1475 | Types.push_back(New); |
| 1476 | return QualType(New, 0); |
| 1477 | } |
| 1478 | |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1479 | QualType ASTContext::getIncompleteArrayType(QualType EltTy, |
| 1480 | ArrayType::ArraySizeModifier ASM, |
| 1481 | unsigned EltTypeQuals) { |
| 1482 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 0be2ef2 | 2009-02-19 17:31:02 +0000 | [diff] [blame] | 1483 | IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1484 | |
| 1485 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1486 | if (IncompleteArrayType *ATP = |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1487 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1488 | return QualType(ATP, 0); |
| 1489 | |
| 1490 | // If the element type isn't canonical, this won't be a canonical type |
| 1491 | // either, so fill in the canonical type field. |
| 1492 | QualType Canonical; |
| 1493 | |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1494 | if (!EltTy.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1495 | Canonical = getIncompleteArrayType(getCanonicalType(EltTy), |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1496 | ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1497 | |
| 1498 | // Get the new insert position for the node we care about. |
| 1499 | IncompleteArrayType *NewIP = |
| 1500 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1501 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1502 | } |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1503 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1504 | IncompleteArrayType *New = new (*this, TypeAlignment) |
| 1505 | IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1506 | |
| 1507 | IncompleteArrayTypes.InsertNode(New, InsertPos); |
| 1508 | Types.push_back(New); |
| 1509 | return QualType(New, 0); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1510 | } |
| 1511 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1512 | /// getVectorType - Return the unique reference to a vector type of |
| 1513 | /// 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] | 1514 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts, |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1515 | VectorType::AltiVecSpecific AltiVecSpec) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1516 | BuiltinType *baseType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1517 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1518 | baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1519 | assert(baseType != 0 && "getVectorType(): Expecting a built-in type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1520 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1521 | // Check if we've already instantiated a vector of this type. |
| 1522 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1523 | VectorType::Profile(ID, vecType, NumElts, Type::Vector, AltiVecSpec); |
| 1524 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1525 | void *InsertPos = 0; |
| 1526 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1527 | return QualType(VTP, 0); |
| 1528 | |
| 1529 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1530 | // so fill in the canonical type field. |
| 1531 | QualType Canonical; |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1532 | if (!vecType.isCanonical()) { |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1533 | Canonical = getVectorType(getCanonicalType(vecType), NumElts, |
| 1534 | VectorType::NotAltiVec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1535 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1536 | // Get the new insert position for the node we care about. |
| 1537 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1538 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1539 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1540 | VectorType *New = new (*this, TypeAlignment) |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1541 | VectorType(vecType, NumElts, Canonical, AltiVecSpec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1542 | VectorTypes.InsertNode(New, InsertPos); |
| 1543 | Types.push_back(New); |
| 1544 | return QualType(New, 0); |
| 1545 | } |
| 1546 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1547 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1548 | /// 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] | 1549 | QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) { |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1550 | BuiltinType *baseType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1551 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1552 | baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1553 | assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1554 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1555 | // Check if we've already instantiated a vector of this type. |
| 1556 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1557 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, |
| 1558 | VectorType::NotAltiVec); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1559 | void *InsertPos = 0; |
| 1560 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1561 | return QualType(VTP, 0); |
| 1562 | |
| 1563 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1564 | // so fill in the canonical type field. |
| 1565 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1566 | if (!vecType.isCanonical()) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1567 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1568 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1569 | // Get the new insert position for the node we care about. |
| 1570 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1571 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1572 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1573 | ExtVectorType *New = new (*this, TypeAlignment) |
| 1574 | ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1575 | VectorTypes.InsertNode(New, InsertPos); |
| 1576 | Types.push_back(New); |
| 1577 | return QualType(New, 0); |
| 1578 | } |
| 1579 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1580 | QualType ASTContext::getDependentSizedExtVectorType(QualType vecType, |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1581 | Expr *SizeExpr, |
| 1582 | SourceLocation AttrLoc) { |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1583 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1584 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1585 | SizeExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1586 | |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1587 | void *InsertPos = 0; |
| 1588 | DependentSizedExtVectorType *Canon |
| 1589 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1590 | DependentSizedExtVectorType *New; |
| 1591 | if (Canon) { |
| 1592 | // We already have a canonical version of this array type; use it as |
| 1593 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1594 | New = new (*this, TypeAlignment) |
| 1595 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), |
| 1596 | SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1597 | } else { |
| 1598 | QualType CanonVecTy = getCanonicalType(vecType); |
| 1599 | if (CanonVecTy == vecType) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1600 | New = new (*this, TypeAlignment) |
| 1601 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, |
| 1602 | AttrLoc); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1603 | |
| 1604 | DependentSizedExtVectorType *CanonCheck |
| 1605 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1606 | assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken"); |
| 1607 | (void)CanonCheck; |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1608 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); |
| 1609 | } else { |
| 1610 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 1611 | SourceLocation()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1612 | New = new (*this, TypeAlignment) |
| 1613 | DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1614 | } |
| 1615 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1616 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1617 | Types.push_back(New); |
| 1618 | return QualType(New, 0); |
| 1619 | } |
| 1620 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1621 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1622 | /// |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1623 | QualType ASTContext::getFunctionNoProtoType(QualType ResultTy, |
| 1624 | const FunctionType::ExtInfo &Info) { |
| 1625 | const CallingConv CallConv = Info.getCC(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1626 | // Unique functions, to guarantee there is only one function of a particular |
| 1627 | // structure. |
| 1628 | llvm::FoldingSetNodeID ID; |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1629 | FunctionNoProtoType::Profile(ID, ResultTy, Info); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1630 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1631 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1632 | if (FunctionNoProtoType *FT = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1633 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1634 | return QualType(FT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1635 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1636 | QualType Canonical; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1637 | if (!ResultTy.isCanonical() || |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1638 | getCanonicalCallConv(CallConv) != CallConv) { |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1639 | Canonical = |
| 1640 | getFunctionNoProtoType(getCanonicalType(ResultTy), |
| 1641 | Info.withCallingConv(getCanonicalCallConv(CallConv))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1642 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1643 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1644 | FunctionNoProtoType *NewIP = |
| 1645 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1646 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1647 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1648 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1649 | FunctionNoProtoType *New = new (*this, TypeAlignment) |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1650 | FunctionNoProtoType(ResultTy, Canonical, Info); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1651 | Types.push_back(New); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1652 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1653 | return QualType(New, 0); |
| 1654 | } |
| 1655 | |
| 1656 | /// getFunctionType - Return a normal function type with a typed argument |
| 1657 | /// list. isVariadic indicates whether the argument list includes '...'. |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 1658 | QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray, |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1659 | unsigned NumArgs, bool isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1660 | unsigned TypeQuals, bool hasExceptionSpec, |
| 1661 | bool hasAnyExceptionSpec, unsigned NumExs, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1662 | const QualType *ExArray, |
| 1663 | const FunctionType::ExtInfo &Info) { |
| 1664 | const CallingConv CallConv= Info.getCC(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1665 | // Unique functions, to guarantee there is only one function of a particular |
| 1666 | // structure. |
| 1667 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1668 | FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1669 | TypeQuals, hasExceptionSpec, hasAnyExceptionSpec, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1670 | NumExs, ExArray, Info); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1671 | |
| 1672 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1673 | if (FunctionProtoType *FTP = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1674 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1675 | return QualType(FTP, 0); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1676 | |
| 1677 | // Determine whether the type being created is already canonical or not. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1678 | bool isCanonical = !hasExceptionSpec && ResultTy.isCanonical(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1679 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1680 | if (!ArgArray[i].isCanonicalAsParam()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1681 | isCanonical = false; |
| 1682 | |
| 1683 | // If this type isn't canonical, get the canonical version of it. |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1684 | // The exception spec is not part of the canonical type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1685 | QualType Canonical; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1686 | if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1687 | llvm::SmallVector<QualType, 16> CanonicalArgs; |
| 1688 | CanonicalArgs.reserve(NumArgs); |
| 1689 | for (unsigned i = 0; i != NumArgs; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1690 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1691 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1692 | Canonical = getFunctionType(getCanonicalType(ResultTy), |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1693 | CanonicalArgs.data(), NumArgs, |
Douglas Gregor | 47259d9 | 2009-08-05 19:03:35 +0000 | [diff] [blame] | 1694 | isVariadic, TypeQuals, false, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1695 | false, 0, 0, |
| 1696 | Info.withCallingConv(getCanonicalCallConv(CallConv))); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1697 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1698 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1699 | FunctionProtoType *NewIP = |
| 1700 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1701 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1702 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1703 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1704 | // FunctionProtoType objects are allocated with extra bytes after them |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1705 | // for two variable size arrays (for parameter and exception types) at the |
| 1706 | // end of them. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1707 | FunctionProtoType *FTP = |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1708 | (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) + |
| 1709 | NumArgs*sizeof(QualType) + |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1710 | NumExs*sizeof(QualType), TypeAlignment); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1711 | new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1712 | TypeQuals, hasExceptionSpec, hasAnyExceptionSpec, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1713 | ExArray, NumExs, Canonical, Info); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1714 | Types.push_back(FTP); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1715 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1716 | return QualType(FTP, 0); |
| 1717 | } |
| 1718 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1719 | #ifndef NDEBUG |
| 1720 | static bool NeedsInjectedClassNameType(const RecordDecl *D) { |
| 1721 | if (!isa<CXXRecordDecl>(D)) return false; |
| 1722 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 1723 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) |
| 1724 | return true; |
| 1725 | if (RD->getDescribedClassTemplate() && |
| 1726 | !isa<ClassTemplateSpecializationDecl>(RD)) |
| 1727 | return true; |
| 1728 | return false; |
| 1729 | } |
| 1730 | #endif |
| 1731 | |
| 1732 | /// getInjectedClassNameType - Return the unique reference to the |
| 1733 | /// injected class name type for the specified templated declaration. |
| 1734 | QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl, |
| 1735 | QualType TST) { |
| 1736 | assert(NeedsInjectedClassNameType(Decl)); |
| 1737 | if (Decl->TypeForDecl) { |
| 1738 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
Argyrios Kyrtzidis | 37ffed3 | 2010-07-02 11:55:32 +0000 | [diff] [blame] | 1739 | } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDeclaration()) { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1740 | assert(PrevDecl->TypeForDecl && "previous declaration has no type"); |
| 1741 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 1742 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
| 1743 | } else { |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1744 | Decl->TypeForDecl = |
| 1745 | new (*this, TypeAlignment) InjectedClassNameType(Decl, TST); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1746 | Types.push_back(Decl->TypeForDecl); |
| 1747 | } |
| 1748 | return QualType(Decl->TypeForDecl, 0); |
| 1749 | } |
| 1750 | |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1751 | /// getTypeDeclType - Return the unique reference to the type for the |
| 1752 | /// specified type declaration. |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1753 | QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) { |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 1754 | assert(Decl && "Passed null for Decl param"); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1755 | assert(!Decl->TypeForDecl && "TypeForDecl present in slow case"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1756 | |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1757 | if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1758 | return getTypedefType(Typedef); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1759 | |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1760 | assert(!isa<TemplateTypeParmDecl>(Decl) && |
| 1761 | "Template type parameter types are always available."); |
| 1762 | |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1763 | if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1764 | assert(!Record->getPreviousDeclaration() && |
| 1765 | "struct/union has previous declaration"); |
| 1766 | assert(!NeedsInjectedClassNameType(Record)); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 1767 | return getRecordType(Record); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1768 | } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1769 | assert(!Enum->getPreviousDeclaration() && |
| 1770 | "enum has previous declaration"); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 1771 | return getEnumType(Enum); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1772 | } else if (const UnresolvedUsingTypenameDecl *Using = |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1773 | dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) { |
| 1774 | Decl->TypeForDecl = new (*this, TypeAlignment) UnresolvedUsingType(Using); |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 1775 | } else |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1776 | llvm_unreachable("TypeDecl without a type?"); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1777 | |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1778 | Types.push_back(Decl->TypeForDecl); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1779 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1780 | } |
| 1781 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1782 | /// getTypedefType - Return the unique reference to the type for the |
| 1783 | /// specified typename decl. |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 1784 | QualType |
| 1785 | ASTContext::getTypedefType(const TypedefDecl *Decl, QualType Canonical) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1786 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1787 | |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 1788 | if (Canonical.isNull()) |
| 1789 | Canonical = getCanonicalType(Decl->getUnderlyingType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1790 | Decl->TypeForDecl = new(*this, TypeAlignment) |
| 1791 | TypedefType(Type::Typedef, Decl, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1792 | Types.push_back(Decl->TypeForDecl); |
| 1793 | return QualType(Decl->TypeForDecl, 0); |
| 1794 | } |
| 1795 | |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 1796 | QualType ASTContext::getRecordType(const RecordDecl *Decl) { |
| 1797 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 1798 | |
| 1799 | if (const RecordDecl *PrevDecl = Decl->getPreviousDeclaration()) |
| 1800 | if (PrevDecl->TypeForDecl) |
| 1801 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 1802 | |
| 1803 | Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Decl); |
| 1804 | Types.push_back(Decl->TypeForDecl); |
| 1805 | return QualType(Decl->TypeForDecl, 0); |
| 1806 | } |
| 1807 | |
| 1808 | QualType ASTContext::getEnumType(const EnumDecl *Decl) { |
| 1809 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 1810 | |
| 1811 | if (const EnumDecl *PrevDecl = Decl->getPreviousDeclaration()) |
| 1812 | if (PrevDecl->TypeForDecl) |
| 1813 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 1814 | |
| 1815 | Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Decl); |
| 1816 | Types.push_back(Decl->TypeForDecl); |
| 1817 | return QualType(Decl->TypeForDecl, 0); |
| 1818 | } |
| 1819 | |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1820 | /// \brief Retrieve a substitution-result type. |
| 1821 | QualType |
| 1822 | ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, |
| 1823 | QualType Replacement) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1824 | assert(Replacement.isCanonical() |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1825 | && "replacement types must always be canonical"); |
| 1826 | |
| 1827 | llvm::FoldingSetNodeID ID; |
| 1828 | SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); |
| 1829 | void *InsertPos = 0; |
| 1830 | SubstTemplateTypeParmType *SubstParm |
| 1831 | = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1832 | |
| 1833 | if (!SubstParm) { |
| 1834 | SubstParm = new (*this, TypeAlignment) |
| 1835 | SubstTemplateTypeParmType(Parm, Replacement); |
| 1836 | Types.push_back(SubstParm); |
| 1837 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 1838 | } |
| 1839 | |
| 1840 | return QualType(SubstParm, 0); |
| 1841 | } |
| 1842 | |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1843 | /// \brief Retrieve the template type parameter type for a template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1844 | /// parameter or parameter pack with the given depth, index, and (optionally) |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1845 | /// name. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1846 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1847 | bool ParameterPack, |
Douglas Gregor | efed5c8 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 1848 | IdentifierInfo *Name) { |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1849 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | efed5c8 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 1850 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1851 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1852 | TemplateTypeParmType *TypeParm |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1853 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1854 | |
| 1855 | if (TypeParm) |
| 1856 | return QualType(TypeParm, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1857 | |
Douglas Gregor | efed5c8 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 1858 | if (Name) { |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1859 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | efed5c8 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 1860 | TypeParm = new (*this, TypeAlignment) |
| 1861 | TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1862 | |
| 1863 | TemplateTypeParmType *TypeCheck |
| 1864 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1865 | assert(!TypeCheck && "Template type parameter canonical type broken"); |
| 1866 | (void)TypeCheck; |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1867 | } else |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1868 | TypeParm = new (*this, TypeAlignment) |
| 1869 | TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1870 | |
| 1871 | Types.push_back(TypeParm); |
| 1872 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 1873 | |
| 1874 | return QualType(TypeParm, 0); |
| 1875 | } |
| 1876 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1877 | TypeSourceInfo * |
| 1878 | ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name, |
| 1879 | SourceLocation NameLoc, |
| 1880 | const TemplateArgumentListInfo &Args, |
| 1881 | QualType CanonType) { |
| 1882 | QualType TST = getTemplateSpecializationType(Name, Args, CanonType); |
| 1883 | |
| 1884 | TypeSourceInfo *DI = CreateTypeSourceInfo(TST); |
| 1885 | TemplateSpecializationTypeLoc TL |
| 1886 | = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc()); |
| 1887 | TL.setTemplateNameLoc(NameLoc); |
| 1888 | TL.setLAngleLoc(Args.getLAngleLoc()); |
| 1889 | TL.setRAngleLoc(Args.getRAngleLoc()); |
| 1890 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 1891 | TL.setArgLocInfo(i, Args[i].getLocInfo()); |
| 1892 | return DI; |
| 1893 | } |
| 1894 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1895 | QualType |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1896 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1897 | const TemplateArgumentListInfo &Args, |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 1898 | QualType Canon) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1899 | unsigned NumArgs = Args.size(); |
| 1900 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1901 | llvm::SmallVector<TemplateArgument, 4> ArgVec; |
| 1902 | ArgVec.reserve(NumArgs); |
| 1903 | for (unsigned i = 0; i != NumArgs; ++i) |
| 1904 | ArgVec.push_back(Args[i].getArgument()); |
| 1905 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1906 | return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 1907 | Canon); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1908 | } |
| 1909 | |
| 1910 | QualType |
| 1911 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1912 | const TemplateArgument *Args, |
| 1913 | unsigned NumArgs, |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 1914 | QualType Canon) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1915 | if (!Canon.isNull()) |
| 1916 | Canon = getCanonicalType(Canon); |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 1917 | else |
| 1918 | Canon = getCanonicalTemplateSpecializationType(Template, Args, NumArgs); |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 1919 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1920 | // Allocate the (non-canonical) template specialization type, but don't |
| 1921 | // try to unique it: these types typically have location information that |
| 1922 | // we don't unique and don't want to lose. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1923 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1924 | sizeof(TemplateArgument) * NumArgs), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1925 | TypeAlignment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1926 | TemplateSpecializationType *Spec |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 1927 | = new (Mem) TemplateSpecializationType(Template, |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1928 | Args, NumArgs, |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 1929 | Canon); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1930 | |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1931 | Types.push_back(Spec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1932 | return QualType(Spec, 0); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1933 | } |
| 1934 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1935 | QualType |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 1936 | ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template, |
| 1937 | const TemplateArgument *Args, |
| 1938 | unsigned NumArgs) { |
| 1939 | // Build the canonical template specialization type. |
| 1940 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); |
| 1941 | llvm::SmallVector<TemplateArgument, 4> CanonArgs; |
| 1942 | CanonArgs.reserve(NumArgs); |
| 1943 | for (unsigned I = 0; I != NumArgs; ++I) |
| 1944 | CanonArgs.push_back(getCanonicalTemplateArgument(Args[I])); |
| 1945 | |
| 1946 | // Determine whether this canonical template specialization type already |
| 1947 | // exists. |
| 1948 | llvm::FoldingSetNodeID ID; |
| 1949 | TemplateSpecializationType::Profile(ID, CanonTemplate, |
| 1950 | CanonArgs.data(), NumArgs, *this); |
| 1951 | |
| 1952 | void *InsertPos = 0; |
| 1953 | TemplateSpecializationType *Spec |
| 1954 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1955 | |
| 1956 | if (!Spec) { |
| 1957 | // Allocate a new canonical template specialization type. |
| 1958 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
| 1959 | sizeof(TemplateArgument) * NumArgs), |
| 1960 | TypeAlignment); |
| 1961 | Spec = new (Mem) TemplateSpecializationType(CanonTemplate, |
| 1962 | CanonArgs.data(), NumArgs, |
| 1963 | QualType()); |
| 1964 | Types.push_back(Spec); |
| 1965 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
| 1966 | } |
| 1967 | |
| 1968 | assert(Spec->isDependentType() && |
| 1969 | "Non-dependent template-id type must have a canonical type"); |
| 1970 | return QualType(Spec, 0); |
| 1971 | } |
| 1972 | |
| 1973 | QualType |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1974 | ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword, |
| 1975 | NestedNameSpecifier *NNS, |
| 1976 | QualType NamedType) { |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1977 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1978 | ElaboratedType::Profile(ID, Keyword, NNS, NamedType); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1979 | |
| 1980 | void *InsertPos = 0; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1981 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1982 | if (T) |
| 1983 | return QualType(T, 0); |
| 1984 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1985 | QualType Canon = NamedType; |
| 1986 | if (!Canon.isCanonical()) { |
| 1987 | Canon = getCanonicalType(NamedType); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1988 | ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1989 | assert(!CheckT && "Elaborated canonical type broken"); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1990 | (void)CheckT; |
| 1991 | } |
| 1992 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1993 | T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1994 | Types.push_back(T); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1995 | ElaboratedTypes.InsertNode(T, InsertPos); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1996 | return QualType(T, 0); |
| 1997 | } |
| 1998 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 1999 | QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, |
| 2000 | NestedNameSpecifier *NNS, |
| 2001 | const IdentifierInfo *Name, |
| 2002 | QualType Canon) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2003 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 2004 | |
| 2005 | if (Canon.isNull()) { |
| 2006 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2007 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 2008 | if (Keyword == ETK_None) |
| 2009 | CanonKeyword = ETK_Typename; |
| 2010 | |
| 2011 | if (CanonNNS != NNS || CanonKeyword != Keyword) |
| 2012 | Canon = getDependentNameType(CanonKeyword, CanonNNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2013 | } |
| 2014 | |
| 2015 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2016 | DependentNameType::Profile(ID, Keyword, NNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2017 | |
| 2018 | void *InsertPos = 0; |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2019 | DependentNameType *T |
| 2020 | = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2021 | if (T) |
| 2022 | return QualType(T, 0); |
| 2023 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2024 | T = new (*this) DependentNameType(Keyword, NNS, Name, Canon); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2025 | Types.push_back(T); |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2026 | DependentNameTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2027 | return QualType(T, 0); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2028 | } |
| 2029 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2030 | QualType |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2031 | ASTContext::getDependentTemplateSpecializationType( |
| 2032 | ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2033 | NestedNameSpecifier *NNS, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2034 | const IdentifierInfo *Name, |
| 2035 | const TemplateArgumentListInfo &Args) { |
| 2036 | // TODO: avoid this copy |
| 2037 | llvm::SmallVector<TemplateArgument, 16> ArgCopy; |
| 2038 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
| 2039 | ArgCopy.push_back(Args[I].getArgument()); |
| 2040 | return getDependentTemplateSpecializationType(Keyword, NNS, Name, |
| 2041 | ArgCopy.size(), |
| 2042 | ArgCopy.data()); |
| 2043 | } |
| 2044 | |
| 2045 | QualType |
| 2046 | ASTContext::getDependentTemplateSpecializationType( |
| 2047 | ElaboratedTypeKeyword Keyword, |
| 2048 | NestedNameSpecifier *NNS, |
| 2049 | const IdentifierInfo *Name, |
| 2050 | unsigned NumArgs, |
| 2051 | const TemplateArgument *Args) { |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2052 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 2053 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2054 | llvm::FoldingSetNodeID ID; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2055 | DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS, |
| 2056 | Name, NumArgs, Args); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2057 | |
| 2058 | void *InsertPos = 0; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2059 | DependentTemplateSpecializationType *T |
| 2060 | = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2061 | if (T) |
| 2062 | return QualType(T, 0); |
| 2063 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2064 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2065 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2066 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 2067 | if (Keyword == ETK_None) CanonKeyword = ETK_Typename; |
| 2068 | |
| 2069 | bool AnyNonCanonArgs = false; |
| 2070 | llvm::SmallVector<TemplateArgument, 16> CanonArgs(NumArgs); |
| 2071 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 2072 | CanonArgs[I] = getCanonicalTemplateArgument(Args[I]); |
| 2073 | if (!CanonArgs[I].structurallyEquals(Args[I])) |
| 2074 | AnyNonCanonArgs = true; |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2075 | } |
| 2076 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2077 | QualType Canon; |
| 2078 | if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) { |
| 2079 | Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS, |
| 2080 | Name, NumArgs, |
| 2081 | CanonArgs.data()); |
| 2082 | |
| 2083 | // Find the insert position again. |
| 2084 | DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2085 | } |
| 2086 | |
| 2087 | void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) + |
| 2088 | sizeof(TemplateArgument) * NumArgs), |
| 2089 | TypeAlignment); |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 2090 | T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2091 | Name, NumArgs, Args, Canon); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2092 | Types.push_back(T); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2093 | DependentTemplateSpecializationTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2094 | return QualType(T, 0); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2095 | } |
| 2096 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2097 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 2098 | /// alphabetically. |
| 2099 | static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, |
| 2100 | const ObjCProtocolDecl *RHS) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2101 | return LHS->getDeclName() < RHS->getDeclName(); |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2102 | } |
| 2103 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2104 | static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols, |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2105 | unsigned NumProtocols) { |
| 2106 | if (NumProtocols == 0) return true; |
| 2107 | |
| 2108 | for (unsigned i = 1; i != NumProtocols; ++i) |
| 2109 | if (!CmpProtocolNames(Protocols[i-1], Protocols[i])) |
| 2110 | return false; |
| 2111 | return true; |
| 2112 | } |
| 2113 | |
| 2114 | static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols, |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2115 | unsigned &NumProtocols) { |
| 2116 | ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2117 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2118 | // Sort protocols, keyed by name. |
| 2119 | std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); |
| 2120 | |
| 2121 | // Remove duplicates. |
| 2122 | ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); |
| 2123 | NumProtocols = ProtocolsEnd-Protocols; |
| 2124 | } |
| 2125 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2126 | QualType ASTContext::getObjCObjectType(QualType BaseType, |
| 2127 | ObjCProtocolDecl * const *Protocols, |
| 2128 | unsigned NumProtocols) { |
| 2129 | // If the base type is an interface and there aren't any protocols |
| 2130 | // to add, then the interface type will do just fine. |
| 2131 | if (!NumProtocols && isa<ObjCInterfaceType>(BaseType)) |
| 2132 | return BaseType; |
| 2133 | |
| 2134 | // Look in the folding set for an existing type. |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2135 | llvm::FoldingSetNodeID ID; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2136 | ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2137 | void *InsertPos = 0; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2138 | if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2139 | return QualType(QT, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2140 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2141 | // Build the canonical type, which has the canonical base type and |
| 2142 | // a sorted-and-uniqued list of protocols. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2143 | QualType Canonical; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2144 | bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols); |
| 2145 | if (!ProtocolsSorted || !BaseType.isCanonical()) { |
| 2146 | if (!ProtocolsSorted) { |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 2147 | llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols, |
| 2148 | Protocols + NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2149 | unsigned UniqueCount = NumProtocols; |
| 2150 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2151 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2152 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 2153 | &Sorted[0], UniqueCount); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2154 | } else { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2155 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 2156 | Protocols, NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2157 | } |
| 2158 | |
| 2159 | // Regenerate InsertPos. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2160 | ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2161 | } |
| 2162 | |
| 2163 | unsigned Size = sizeof(ObjCObjectTypeImpl); |
| 2164 | Size += NumProtocols * sizeof(ObjCProtocolDecl *); |
| 2165 | void *Mem = Allocate(Size, TypeAlignment); |
| 2166 | ObjCObjectTypeImpl *T = |
| 2167 | new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols); |
| 2168 | |
| 2169 | Types.push_back(T); |
| 2170 | ObjCObjectTypes.InsertNode(T, InsertPos); |
| 2171 | return QualType(T, 0); |
| 2172 | } |
| 2173 | |
| 2174 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 2175 | /// the given object type. |
| 2176 | QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) { |
| 2177 | llvm::FoldingSetNodeID ID; |
| 2178 | ObjCObjectPointerType::Profile(ID, ObjectT); |
| 2179 | |
| 2180 | void *InsertPos = 0; |
| 2181 | if (ObjCObjectPointerType *QT = |
| 2182 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2183 | return QualType(QT, 0); |
| 2184 | |
| 2185 | // Find the canonical object type. |
| 2186 | QualType Canonical; |
| 2187 | if (!ObjectT.isCanonical()) { |
| 2188 | Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT)); |
| 2189 | |
| 2190 | // Regenerate InsertPos. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2191 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2192 | } |
| 2193 | |
Douglas Gregor | fd6a088 | 2010-02-08 22:59:26 +0000 | [diff] [blame] | 2194 | // No match. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2195 | void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment); |
| 2196 | ObjCObjectPointerType *QType = |
| 2197 | new (Mem) ObjCObjectPointerType(Canonical, ObjectT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2198 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2199 | Types.push_back(QType); |
| 2200 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2201 | return QualType(QType, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2202 | } |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2203 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2204 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 2205 | /// specified ObjC interface decl. The list of protocols is optional. |
| 2206 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) { |
| 2207 | if (Decl->TypeForDecl) |
| 2208 | return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2209 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2210 | // FIXME: redeclarations? |
| 2211 | void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment); |
| 2212 | ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl); |
| 2213 | Decl->TypeForDecl = T; |
| 2214 | Types.push_back(T); |
| 2215 | return QualType(T, 0); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2216 | } |
| 2217 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2218 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 2219 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2220 | /// multiple declarations that refer to "typeof(x)" all contain different |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2221 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2222 | /// on canonical type's (which are always unique). |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2223 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2224 | TypeOfExprType *toe; |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2225 | if (tofExpr->isTypeDependent()) { |
| 2226 | llvm::FoldingSetNodeID ID; |
| 2227 | DependentTypeOfExprType::Profile(ID, *this, tofExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2228 | |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2229 | void *InsertPos = 0; |
| 2230 | DependentTypeOfExprType *Canon |
| 2231 | = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2232 | if (Canon) { |
| 2233 | // We already have a "canonical" version of an identical, dependent |
| 2234 | // typeof(expr) type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2235 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2236 | QualType((TypeOfExprType*)Canon, 0)); |
| 2237 | } |
| 2238 | else { |
| 2239 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2240 | Canon |
| 2241 | = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2242 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); |
| 2243 | toe = Canon; |
| 2244 | } |
| 2245 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2246 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2247 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2248 | } |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2249 | Types.push_back(toe); |
| 2250 | return QualType(toe, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2251 | } |
| 2252 | |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2253 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 2254 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 2255 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2256 | /// an issue. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2257 | /// on canonical type's (which are always unique). |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2258 | QualType ASTContext::getTypeOfType(QualType tofType) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2259 | QualType Canonical = getCanonicalType(tofType); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2260 | TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2261 | Types.push_back(tot); |
| 2262 | return QualType(tot, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2263 | } |
| 2264 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2265 | /// getDecltypeForExpr - Given an expr, will return the decltype for that |
| 2266 | /// expression, according to the rules in C++0x [dcl.type.simple]p4 |
| 2267 | static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) { |
Anders Carlsson | a07c33e | 2009-06-25 15:00:34 +0000 | [diff] [blame] | 2268 | if (e->isTypeDependent()) |
| 2269 | return Context.DependentTy; |
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 | // If e is an id expression or a class member access, decltype(e) is defined |
| 2272 | // as the type of the entity named by e. |
| 2273 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) { |
| 2274 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) |
| 2275 | return VD->getType(); |
| 2276 | } |
| 2277 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) { |
| 2278 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) |
| 2279 | return FD->getType(); |
| 2280 | } |
| 2281 | // If e is a function call or an invocation of an overloaded operator, |
| 2282 | // (parentheses around e are ignored), decltype(e) is defined as the |
| 2283 | // return type of that function. |
| 2284 | if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens())) |
| 2285 | return CE->getCallReturnType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2286 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2287 | QualType T = e->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2288 | |
| 2289 | // 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] | 2290 | // defined as T&, otherwise decltype(e) is defined as T. |
| 2291 | if (e->isLvalue(Context) == Expr::LV_Valid) |
| 2292 | T = Context.getLValueReferenceType(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2293 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2294 | return T; |
| 2295 | } |
| 2296 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2297 | /// getDecltypeType - Unlike many "get<Type>" functions, we don't unique |
| 2298 | /// DecltypeType AST's. The only motivation to unique these nodes would be |
| 2299 | /// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2300 | /// an issue. This doesn't effect the type checker, since it operates |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2301 | /// on canonical type's (which are always unique). |
| 2302 | QualType ASTContext::getDecltypeType(Expr *e) { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2303 | DecltypeType *dt; |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2304 | if (e->isTypeDependent()) { |
| 2305 | llvm::FoldingSetNodeID ID; |
| 2306 | DependentDecltypeType::Profile(ID, *this, e); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2307 | |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2308 | void *InsertPos = 0; |
| 2309 | DependentDecltypeType *Canon |
| 2310 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2311 | if (Canon) { |
| 2312 | // We already have a "canonical" version of an equivalent, dependent |
| 2313 | // decltype type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2314 | dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy, |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2315 | QualType((DecltypeType*)Canon, 0)); |
| 2316 | } |
| 2317 | else { |
| 2318 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2319 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2320 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); |
| 2321 | dt = Canon; |
| 2322 | } |
| 2323 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2324 | QualType T = getDecltypeForExpr(e, *this); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2325 | dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T)); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2326 | } |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2327 | Types.push_back(dt); |
| 2328 | return QualType(dt, 0); |
| 2329 | } |
| 2330 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2331 | /// getTagDeclType - Return the unique reference to the type for the |
| 2332 | /// specified TagDecl (struct/union/class/enum) decl. |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 2333 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) { |
Ted Kremenek | d778f88 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 2334 | assert (Decl); |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 2335 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 2336 | // away const? mutable? |
| 2337 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2338 | } |
| 2339 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2340 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 2341 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 2342 | /// needs to agree with the definition in <stddef.h>. |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 2343 | CanQualType ASTContext::getSizeType() const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2344 | return getFromTargetType(Target.getSizeType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2345 | } |
| 2346 | |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 2347 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 2348 | /// Used when in C++, as a GCC extension. |
| 2349 | QualType ASTContext::getSignedWCharType() const { |
| 2350 | // FIXME: derive from "Target" ? |
| 2351 | return WCharTy; |
| 2352 | } |
| 2353 | |
| 2354 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 2355 | /// Used when in C++, as a GCC extension. |
| 2356 | QualType ASTContext::getUnsignedWCharType() const { |
| 2357 | // FIXME: derive from "Target" ? |
| 2358 | return UnsignedIntTy; |
| 2359 | } |
| 2360 | |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 2361 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) |
| 2362 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 2363 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2364 | return getFromTargetType(Target.getPtrDiffType(0)); |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 2365 | } |
| 2366 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2367 | //===----------------------------------------------------------------------===// |
| 2368 | // Type Operators |
| 2369 | //===----------------------------------------------------------------------===// |
| 2370 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2371 | CanQualType ASTContext::getCanonicalParamType(QualType T) { |
| 2372 | // Push qualifiers into arrays, and then discard any remaining |
| 2373 | // qualifiers. |
| 2374 | T = getCanonicalType(T); |
| 2375 | const Type *Ty = T.getTypePtr(); |
| 2376 | |
| 2377 | QualType Result; |
| 2378 | if (isa<ArrayType>(Ty)) { |
| 2379 | Result = getArrayDecayedType(QualType(Ty,0)); |
| 2380 | } else if (isa<FunctionType>(Ty)) { |
| 2381 | Result = getPointerType(QualType(Ty, 0)); |
| 2382 | } else { |
| 2383 | Result = QualType(Ty, 0); |
| 2384 | } |
| 2385 | |
| 2386 | return CanQualType::CreateUnsafe(Result); |
| 2387 | } |
| 2388 | |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 2389 | /// getCanonicalType - Return the canonical (structural) type corresponding to |
| 2390 | /// the specified potentially non-canonical type. The non-canonical version |
| 2391 | /// of a type may have many "decorated" versions of types. Decorators can |
| 2392 | /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed |
| 2393 | /// to be free of any of these, allowing two canonical types to be compared |
| 2394 | /// for exact equality with a simple pointer comparison. |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2395 | CanQualType ASTContext::getCanonicalType(QualType T) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2396 | QualifierCollector Quals; |
| 2397 | const Type *Ptr = Quals.strip(T); |
| 2398 | QualType CanType = Ptr->getCanonicalTypeInternal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2399 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2400 | // The canonical internal type will be the canonical type *except* |
| 2401 | // that we push type qualifiers down through array types. |
| 2402 | |
| 2403 | // If there are no new qualifiers to push down, stop here. |
| 2404 | if (!Quals.hasQualifiers()) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2405 | return CanQualType::CreateUnsafe(CanType); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2406 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2407 | // If the type qualifiers are on an array type, get the canonical |
| 2408 | // type of the array with the qualifiers applied to the element |
| 2409 | // type. |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2410 | ArrayType *AT = dyn_cast<ArrayType>(CanType); |
| 2411 | if (!AT) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2412 | return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2413 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2414 | // Get the canonical version of the element with the extra qualifiers on it. |
| 2415 | // This can recursively sink qualifiers through multiple levels of arrays. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2416 | QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2417 | NewEltTy = getCanonicalType(NewEltTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2418 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2419 | if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2420 | return CanQualType::CreateUnsafe( |
| 2421 | getConstantArrayType(NewEltTy, CAT->getSize(), |
| 2422 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2423 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2424 | if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2425 | return CanQualType::CreateUnsafe( |
| 2426 | getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2427 | IAT->getIndexTypeCVRQualifiers())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2428 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2429 | if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2430 | return CanQualType::CreateUnsafe( |
| 2431 | getDependentSizedArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2432 | DSAT->getSizeExpr() ? |
| 2433 | DSAT->getSizeExpr()->Retain() : 0, |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2434 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2435 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 87a924e | 2009-10-30 22:56:57 +0000 | [diff] [blame] | 2436 | DSAT->getBracketsRange())->getCanonicalTypeInternal()); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2437 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2438 | VariableArrayType *VAT = cast<VariableArrayType>(AT); |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2439 | return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2440 | VAT->getSizeExpr() ? |
| 2441 | VAT->getSizeExpr()->Retain() : 0, |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2442 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2443 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2444 | VAT->getBracketsRange())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2445 | } |
| 2446 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2447 | QualType ASTContext::getUnqualifiedArrayType(QualType T, |
| 2448 | Qualifiers &Quals) { |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2449 | Quals = T.getQualifiers(); |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2450 | const ArrayType *AT = getAsArrayType(T); |
| 2451 | if (!AT) { |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2452 | return T.getUnqualifiedType(); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2453 | } |
| 2454 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2455 | QualType Elt = AT->getElementType(); |
Zhongxing Xu | c1ae0a8 | 2010-01-05 08:15:06 +0000 | [diff] [blame] | 2456 | QualType UnqualElt = getUnqualifiedArrayType(Elt, Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2457 | if (Elt == UnqualElt) |
| 2458 | return T; |
| 2459 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2460 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2461 | return getConstantArrayType(UnqualElt, CAT->getSize(), |
| 2462 | CAT->getSizeModifier(), 0); |
| 2463 | } |
| 2464 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2465 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2466 | return getIncompleteArrayType(UnqualElt, IAT->getSizeModifier(), 0); |
| 2467 | } |
| 2468 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2469 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) { |
| 2470 | return getVariableArrayType(UnqualElt, |
| 2471 | VAT->getSizeExpr() ? |
| 2472 | VAT->getSizeExpr()->Retain() : 0, |
| 2473 | VAT->getSizeModifier(), |
| 2474 | VAT->getIndexTypeCVRQualifiers(), |
| 2475 | VAT->getBracketsRange()); |
| 2476 | } |
| 2477 | |
| 2478 | const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2479 | return getDependentSizedArrayType(UnqualElt, DSAT->getSizeExpr()->Retain(), |
| 2480 | DSAT->getSizeModifier(), 0, |
| 2481 | SourceRange()); |
| 2482 | } |
| 2483 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2484 | /// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that |
| 2485 | /// may be similar (C++ 4.4), replaces T1 and T2 with the type that |
| 2486 | /// they point to and return true. If T1 and T2 aren't pointer types |
| 2487 | /// or pointer-to-member types, or if they are not similar at this |
| 2488 | /// level, returns false and leaves T1 and T2 unchanged. Top-level |
| 2489 | /// qualifiers on T1 and T2 are ignored. This function will typically |
| 2490 | /// be called in a loop that successively "unwraps" pointer and |
| 2491 | /// pointer-to-member types to compare them at each level. |
| 2492 | bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) { |
| 2493 | const PointerType *T1PtrType = T1->getAs<PointerType>(), |
| 2494 | *T2PtrType = T2->getAs<PointerType>(); |
| 2495 | if (T1PtrType && T2PtrType) { |
| 2496 | T1 = T1PtrType->getPointeeType(); |
| 2497 | T2 = T2PtrType->getPointeeType(); |
| 2498 | return true; |
| 2499 | } |
| 2500 | |
| 2501 | const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(), |
| 2502 | *T2MPType = T2->getAs<MemberPointerType>(); |
| 2503 | if (T1MPType && T2MPType && |
| 2504 | hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0), |
| 2505 | QualType(T2MPType->getClass(), 0))) { |
| 2506 | T1 = T1MPType->getPointeeType(); |
| 2507 | T2 = T2MPType->getPointeeType(); |
| 2508 | return true; |
| 2509 | } |
| 2510 | |
| 2511 | if (getLangOptions().ObjC1) { |
| 2512 | const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(), |
| 2513 | *T2OPType = T2->getAs<ObjCObjectPointerType>(); |
| 2514 | if (T1OPType && T2OPType) { |
| 2515 | T1 = T1OPType->getPointeeType(); |
| 2516 | T2 = T2OPType->getPointeeType(); |
| 2517 | return true; |
| 2518 | } |
| 2519 | } |
| 2520 | |
| 2521 | // FIXME: Block pointers, too? |
| 2522 | |
| 2523 | return false; |
| 2524 | } |
| 2525 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2526 | DeclarationNameInfo ASTContext::getNameForTemplate(TemplateName Name, |
| 2527 | SourceLocation NameLoc) { |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2528 | if (TemplateDecl *TD = Name.getAsTemplateDecl()) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2529 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 2530 | return DeclarationNameInfo(TD->getDeclName(), NameLoc); |
| 2531 | |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2532 | if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2533 | DeclarationName DName; |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2534 | if (DTN->isIdentifier()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2535 | DName = DeclarationNames.getIdentifier(DTN->getIdentifier()); |
| 2536 | return DeclarationNameInfo(DName, NameLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2537 | } else { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2538 | DName = DeclarationNames.getCXXOperatorName(DTN->getOperator()); |
| 2539 | // DNInfo work in progress: FIXME: source locations? |
| 2540 | DeclarationNameLoc DNLoc; |
| 2541 | DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding(); |
| 2542 | DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding(); |
| 2543 | return DeclarationNameInfo(DName, NameLoc, DNLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2544 | } |
| 2545 | } |
| 2546 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 2547 | OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate(); |
| 2548 | assert(Storage); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2549 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 2550 | return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2551 | } |
| 2552 | |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2553 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) { |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 2554 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 2555 | if (TemplateTemplateParmDecl *TTP |
| 2556 | = dyn_cast<TemplateTemplateParmDecl>(Template)) |
| 2557 | Template = getCanonicalTemplateTemplateParmDecl(TTP); |
| 2558 | |
| 2559 | // The canonical template name is the canonical template declaration. |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 2560 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 2561 | } |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2562 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 2563 | assert(!Name.getAsOverloadedTemplate()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2564 | |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2565 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 2566 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 2567 | return DTN->CanonicalTemplateName; |
| 2568 | } |
| 2569 | |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 2570 | bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) { |
| 2571 | X = getCanonicalTemplateName(X); |
| 2572 | Y = getCanonicalTemplateName(Y); |
| 2573 | return X.getAsVoidPointer() == Y.getAsVoidPointer(); |
| 2574 | } |
| 2575 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2576 | TemplateArgument |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2577 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) { |
| 2578 | switch (Arg.getKind()) { |
| 2579 | case TemplateArgument::Null: |
| 2580 | return Arg; |
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::Expression: |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2583 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2584 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2585 | case TemplateArgument::Declaration: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2586 | return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2587 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2588 | case TemplateArgument::Template: |
| 2589 | return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate())); |
| 2590 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2591 | case TemplateArgument::Integral: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2592 | return TemplateArgument(*Arg.getAsIntegral(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2593 | getCanonicalType(Arg.getIntegralType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2594 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2595 | case TemplateArgument::Type: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2596 | return TemplateArgument(getCanonicalType(Arg.getAsType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2597 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2598 | case TemplateArgument::Pack: { |
| 2599 | // FIXME: Allocate in ASTContext |
| 2600 | TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()]; |
| 2601 | unsigned Idx = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2602 | for (TemplateArgument::pack_iterator A = Arg.pack_begin(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2603 | AEnd = Arg.pack_end(); |
| 2604 | A != AEnd; (void)++A, ++Idx) |
| 2605 | CanonArgs[Idx] = getCanonicalTemplateArgument(*A); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2606 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2607 | TemplateArgument Result; |
| 2608 | Result.setArgumentPack(CanonArgs, Arg.pack_size(), false); |
| 2609 | return Result; |
| 2610 | } |
| 2611 | } |
| 2612 | |
| 2613 | // Silence GCC warning |
| 2614 | assert(false && "Unhandled template argument kind"); |
| 2615 | return TemplateArgument(); |
| 2616 | } |
| 2617 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2618 | NestedNameSpecifier * |
| 2619 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2620 | if (!NNS) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2621 | return 0; |
| 2622 | |
| 2623 | switch (NNS->getKind()) { |
| 2624 | case NestedNameSpecifier::Identifier: |
| 2625 | // Canonicalize the prefix but keep the identifier the same. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2626 | return NestedNameSpecifier::Create(*this, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2627 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 2628 | NNS->getAsIdentifier()); |
| 2629 | |
| 2630 | case NestedNameSpecifier::Namespace: |
| 2631 | // A namespace is canonical; build a nested-name-specifier with |
| 2632 | // this namespace and no prefix. |
| 2633 | return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace()); |
| 2634 | |
| 2635 | case NestedNameSpecifier::TypeSpec: |
| 2636 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 2637 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2638 | return NestedNameSpecifier::Create(*this, 0, |
| 2639 | NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2640 | T.getTypePtr()); |
| 2641 | } |
| 2642 | |
| 2643 | case NestedNameSpecifier::Global: |
| 2644 | // The global specifier is canonical and unique. |
| 2645 | return NNS; |
| 2646 | } |
| 2647 | |
| 2648 | // Required to silence a GCC warning |
| 2649 | return 0; |
| 2650 | } |
| 2651 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2652 | |
| 2653 | const ArrayType *ASTContext::getAsArrayType(QualType T) { |
| 2654 | // Handle the non-qualified case efficiently. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2655 | if (!T.hasLocalQualifiers()) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2656 | // Handle the common positive case fast. |
| 2657 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 2658 | return AT; |
| 2659 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2660 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2661 | // Handle the common negative case fast. |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2662 | QualType CType = T->getCanonicalTypeInternal(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2663 | if (!isa<ArrayType>(CType)) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2664 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2665 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2666 | // Apply any qualifiers from the array type to the element type. This |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2667 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 2668 | // 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] | 2669 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2670 | // If we get here, we either have type qualifiers on the type, or we have |
| 2671 | // 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] | 2672 | // we must propagate them down into the element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2673 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2674 | QualifierCollector Qs; |
| 2675 | const Type *Ty = Qs.strip(T.getDesugaredType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2676 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2677 | // If we have a simple case, just return now. |
| 2678 | const ArrayType *ATy = dyn_cast<ArrayType>(Ty); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2679 | if (ATy == 0 || Qs.empty()) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2680 | return ATy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2681 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2682 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 2683 | // qualifiers into the array element type and return a new array type. |
| 2684 | // Get the canonical version of the element with the extra qualifiers on it. |
| 2685 | // This can recursively sink qualifiers through multiple levels of arrays. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2686 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2687 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2688 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 2689 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 2690 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2691 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2692 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 2693 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 2694 | IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2695 | IAT->getIndexTypeCVRQualifiers())); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2696 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2697 | if (const DependentSizedArrayType *DSAT |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2698 | = dyn_cast<DependentSizedArrayType>(ATy)) |
| 2699 | return cast<ArrayType>( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2700 | getDependentSizedArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2701 | DSAT->getSizeExpr() ? |
| 2702 | DSAT->getSizeExpr()->Retain() : 0, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2703 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2704 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2705 | DSAT->getBracketsRange())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2706 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2707 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2708 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2709 | VAT->getSizeExpr() ? |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2710 | VAT->getSizeExpr()->Retain() : 0, |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2711 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2712 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2713 | VAT->getBracketsRange())); |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 2714 | } |
| 2715 | |
| 2716 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2717 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 2718 | /// specified array type to a pointer. This operation is non-trivial when |
| 2719 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 2720 | /// this returns a pointer to a properly qualified element of the array. |
| 2721 | /// |
| 2722 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
| 2723 | QualType ASTContext::getArrayDecayedType(QualType Ty) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2724 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 2725 | // typedefs in the element type of the array. This also handles propagation |
| 2726 | // of type qualifiers from the array type into the element type if present |
| 2727 | // (C99 6.7.3p8). |
| 2728 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 2729 | assert(PrettyArrayType && "Not an array type!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2730 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2731 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2732 | |
| 2733 | // int x[restrict 4] -> int *restrict |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2734 | return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2735 | } |
| 2736 | |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2737 | QualType ASTContext::getBaseElementType(QualType QT) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2738 | QualifierCollector Qs; |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 2739 | while (const ArrayType *AT = getAsArrayType(QualType(Qs.strip(QT), 0))) |
| 2740 | QT = AT->getElementType(); |
| 2741 | return Qs.apply(QT); |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2742 | } |
| 2743 | |
Anders Carlsson | fbbce49 | 2009-09-25 01:23:32 +0000 | [diff] [blame] | 2744 | QualType ASTContext::getBaseElementType(const ArrayType *AT) { |
| 2745 | QualType ElemTy = AT->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2746 | |
Anders Carlsson | fbbce49 | 2009-09-25 01:23:32 +0000 | [diff] [blame] | 2747 | if (const ArrayType *AT = getAsArrayType(ElemTy)) |
| 2748 | return getBaseElementType(AT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2749 | |
Anders Carlsson | 6183a99 | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 2750 | return ElemTy; |
| 2751 | } |
| 2752 | |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 2753 | /// getConstantArrayElementCount - Returns number of constant array elements. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2754 | uint64_t |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 2755 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
| 2756 | uint64_t ElementCount = 1; |
| 2757 | do { |
| 2758 | ElementCount *= CA->getSize().getZExtValue(); |
| 2759 | CA = dyn_cast<ConstantArrayType>(CA->getElementType()); |
| 2760 | } while (CA); |
| 2761 | return ElementCount; |
| 2762 | } |
| 2763 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2764 | /// getFloatingRank - Return a relative rank for floating point types. |
| 2765 | /// 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] | 2766 | static FloatingRank getFloatingRank(QualType T) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2767 | if (const ComplexType *CT = T->getAs<ComplexType>()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2768 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2769 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2770 | assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"); |
| 2771 | switch (T->getAs<BuiltinType>()->getKind()) { |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2772 | default: assert(0 && "getFloatingRank(): not a floating type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2773 | case BuiltinType::Float: return FloatRank; |
| 2774 | case BuiltinType::Double: return DoubleRank; |
| 2775 | case BuiltinType::LongDouble: return LongDoubleRank; |
| 2776 | } |
| 2777 | } |
| 2778 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2779 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 2780 | /// point or a complex type (based on typeDomain/typeSize). |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 2781 | /// 'typeDomain' is a real floating point or complex type. |
| 2782 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 2783 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 2784 | QualType Domain) const { |
| 2785 | FloatingRank EltRank = getFloatingRank(Size); |
| 2786 | if (Domain->isComplexType()) { |
| 2787 | switch (EltRank) { |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 2788 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 2789 | case FloatRank: return FloatComplexTy; |
| 2790 | case DoubleRank: return DoubleComplexTy; |
| 2791 | case LongDoubleRank: return LongDoubleComplexTy; |
| 2792 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2793 | } |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 2794 | |
| 2795 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 2796 | switch (EltRank) { |
| 2797 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
| 2798 | case FloatRank: return FloatTy; |
| 2799 | case DoubleRank: return DoubleTy; |
| 2800 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 2801 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2802 | } |
| 2803 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2804 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 2805 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 2806 | /// '_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] | 2807 | /// LHS < RHS, return -1. |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2808 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) { |
| 2809 | FloatingRank LHSR = getFloatingRank(LHS); |
| 2810 | FloatingRank RHSR = getFloatingRank(RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2811 | |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2812 | if (LHSR == RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 2813 | return 0; |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2814 | if (LHSR > RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 2815 | return 1; |
| 2816 | return -1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2817 | } |
| 2818 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2819 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 2820 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 2821 | /// or if it is not canonicalized. |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2822 | unsigned ASTContext::getIntegerRank(Type *T) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2823 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2824 | if (EnumType* ET = dyn_cast<EnumType>(T)) |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 2825 | T = ET->getDecl()->getPromotionType().getTypePtr(); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2826 | |
Eli Friedman | a342675 | 2009-07-05 23:44:27 +0000 | [diff] [blame] | 2827 | if (T->isSpecificBuiltinType(BuiltinType::WChar)) |
| 2828 | T = getFromTargetType(Target.getWCharType()).getTypePtr(); |
| 2829 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2830 | if (T->isSpecificBuiltinType(BuiltinType::Char16)) |
| 2831 | T = getFromTargetType(Target.getChar16Type()).getTypePtr(); |
| 2832 | |
| 2833 | if (T->isSpecificBuiltinType(BuiltinType::Char32)) |
| 2834 | T = getFromTargetType(Target.getChar32Type()).getTypePtr(); |
| 2835 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2836 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2837 | default: assert(0 && "getIntegerRank(): not a built-in integer"); |
| 2838 | case BuiltinType::Bool: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2839 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2840 | case BuiltinType::Char_S: |
| 2841 | case BuiltinType::Char_U: |
| 2842 | case BuiltinType::SChar: |
| 2843 | case BuiltinType::UChar: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2844 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2845 | case BuiltinType::Short: |
| 2846 | case BuiltinType::UShort: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2847 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2848 | case BuiltinType::Int: |
| 2849 | case BuiltinType::UInt: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2850 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2851 | case BuiltinType::Long: |
| 2852 | case BuiltinType::ULong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2853 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2854 | case BuiltinType::LongLong: |
| 2855 | case BuiltinType::ULongLong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2856 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 2857 | case BuiltinType::Int128: |
| 2858 | case BuiltinType::UInt128: |
| 2859 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2860 | } |
| 2861 | } |
| 2862 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 2863 | /// \brief Whether this is a promotable bitfield reference according |
| 2864 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
| 2865 | /// |
| 2866 | /// \returns the type this bit-field will promote to, or NULL if no |
| 2867 | /// promotion occurs. |
| 2868 | QualType ASTContext::isPromotableBitField(Expr *E) { |
Douglas Gregor | ceafbde | 2010-05-24 20:13:53 +0000 | [diff] [blame] | 2869 | if (E->isTypeDependent() || E->isValueDependent()) |
| 2870 | return QualType(); |
| 2871 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 2872 | FieldDecl *Field = E->getBitField(); |
| 2873 | if (!Field) |
| 2874 | return QualType(); |
| 2875 | |
| 2876 | QualType FT = Field->getType(); |
| 2877 | |
| 2878 | llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this); |
| 2879 | uint64_t BitWidth = BitWidthAP.getZExtValue(); |
| 2880 | uint64_t IntSize = getTypeSize(IntTy); |
| 2881 | // GCC extension compatibility: if the bit-field size is less than or equal |
| 2882 | // to the size of int, it gets promoted no matter what its type is. |
| 2883 | // For instance, unsigned long bf : 4 gets promoted to signed int. |
| 2884 | if (BitWidth < IntSize) |
| 2885 | return IntTy; |
| 2886 | |
| 2887 | if (BitWidth == IntSize) |
| 2888 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
| 2889 | |
| 2890 | // Types bigger than int are not subject to promotions, and therefore act |
| 2891 | // like the base type. |
| 2892 | // FIXME: This doesn't quite match what gcc does, but what gcc does here |
| 2893 | // is ridiculous. |
| 2894 | return QualType(); |
| 2895 | } |
| 2896 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 2897 | /// getPromotedIntegerType - Returns the type that Promotable will |
| 2898 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
| 2899 | /// integer type. |
| 2900 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) { |
| 2901 | assert(!Promotable.isNull()); |
| 2902 | assert(Promotable->isPromotableIntegerType()); |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 2903 | if (const EnumType *ET = Promotable->getAs<EnumType>()) |
| 2904 | return ET->getDecl()->getPromotionType(); |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 2905 | if (Promotable->isSignedIntegerType()) |
| 2906 | return IntTy; |
| 2907 | uint64_t PromotableSize = getTypeSize(Promotable); |
| 2908 | uint64_t IntSize = getTypeSize(IntTy); |
| 2909 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
| 2910 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
| 2911 | } |
| 2912 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2913 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2914 | /// 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] | 2915 | /// LHS < RHS, return -1. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2916 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2917 | Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 2918 | Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2919 | if (LHSC == RHSC) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2920 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2921 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 2922 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2923 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2924 | unsigned LHSRank = getIntegerRank(LHSC); |
| 2925 | unsigned RHSRank = getIntegerRank(RHSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2926 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2927 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 2928 | if (LHSRank == RHSRank) return 0; |
| 2929 | return LHSRank > RHSRank ? 1 : -1; |
| 2930 | } |
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 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 2933 | if (LHSUnsigned) { |
| 2934 | // If the unsigned [LHS] type is larger, return it. |
| 2935 | if (LHSRank >= RHSRank) |
| 2936 | return 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2937 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2938 | // If the signed type can represent all values of the unsigned type, it |
| 2939 | // 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] | 2940 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2941 | return -1; |
| 2942 | } |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2943 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2944 | // If the unsigned [RHS] type is larger, return it. |
| 2945 | if (RHSRank >= LHSRank) |
| 2946 | return -1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2947 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2948 | // If the signed type can represent all values of the unsigned type, it |
| 2949 | // 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] | 2950 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2951 | return 1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2952 | } |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2953 | |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 2954 | static RecordDecl * |
| 2955 | CreateRecordDecl(ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC, |
| 2956 | SourceLocation L, IdentifierInfo *Id) { |
| 2957 | if (Ctx.getLangOptions().CPlusPlus) |
| 2958 | return CXXRecordDecl::Create(Ctx, TK, DC, L, Id); |
| 2959 | else |
| 2960 | return RecordDecl::Create(Ctx, TK, DC, L, Id); |
| 2961 | } |
| 2962 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2963 | // getCFConstantStringType - Return the type used for constant CFStrings. |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2964 | QualType ASTContext::getCFConstantStringType() { |
| 2965 | if (!CFConstantStringTypeDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2966 | CFConstantStringTypeDecl = |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2967 | CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 2968 | &Idents.get("NSConstantString")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 2969 | CFConstantStringTypeDecl->startDefinition(); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 2970 | |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 2971 | QualType FieldTypes[4]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2972 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2973 | // const int *isa; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2974 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 2975 | // int flags; |
| 2976 | FieldTypes[1] = IntTy; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2977 | // const char *str; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2978 | FieldTypes[2] = getPointerType(CharTy.withConst()); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2979 | // long length; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2980 | FieldTypes[3] = LongTy; |
| 2981 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2982 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2983 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2984 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2985 | SourceLocation(), 0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2986 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2987 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2988 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 2989 | Field->setAccess(AS_public); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2990 | CFConstantStringTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2991 | } |
| 2992 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2993 | CFConstantStringTypeDecl->completeDefinition(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2994 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2995 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2996 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 8467583 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 2997 | } |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 2998 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 2999 | void ASTContext::setCFConstantStringType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3000 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3001 | assert(Rec && "Invalid CFConstantStringType"); |
| 3002 | CFConstantStringTypeDecl = Rec->getDecl(); |
| 3003 | } |
| 3004 | |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 3005 | // getNSConstantStringType - Return the type used for constant NSStrings. |
| 3006 | QualType ASTContext::getNSConstantStringType() { |
| 3007 | if (!NSConstantStringTypeDecl) { |
| 3008 | NSConstantStringTypeDecl = |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3009 | CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 3010 | &Idents.get("__builtin_NSString")); |
| 3011 | NSConstantStringTypeDecl->startDefinition(); |
| 3012 | |
| 3013 | QualType FieldTypes[3]; |
| 3014 | |
| 3015 | // const int *isa; |
| 3016 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
| 3017 | // const char *str; |
| 3018 | FieldTypes[1] = getPointerType(CharTy.withConst()); |
| 3019 | // unsigned int length; |
| 3020 | FieldTypes[2] = UnsignedIntTy; |
| 3021 | |
| 3022 | // Create fields |
| 3023 | for (unsigned i = 0; i < 3; ++i) { |
| 3024 | FieldDecl *Field = FieldDecl::Create(*this, NSConstantStringTypeDecl, |
| 3025 | SourceLocation(), 0, |
| 3026 | FieldTypes[i], /*TInfo=*/0, |
| 3027 | /*BitWidth=*/0, |
| 3028 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3029 | Field->setAccess(AS_public); |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 3030 | NSConstantStringTypeDecl->addDecl(Field); |
| 3031 | } |
| 3032 | |
| 3033 | NSConstantStringTypeDecl->completeDefinition(); |
| 3034 | } |
| 3035 | |
| 3036 | return getTagDeclType(NSConstantStringTypeDecl); |
| 3037 | } |
| 3038 | |
| 3039 | void ASTContext::setNSConstantStringType(QualType T) { |
| 3040 | const RecordType *Rec = T->getAs<RecordType>(); |
| 3041 | assert(Rec && "Invalid NSConstantStringType"); |
| 3042 | NSConstantStringTypeDecl = Rec->getDecl(); |
| 3043 | } |
| 3044 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3045 | QualType ASTContext::getObjCFastEnumerationStateType() { |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3046 | if (!ObjCFastEnumerationStateTypeDecl) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3047 | ObjCFastEnumerationStateTypeDecl = |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3048 | CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3049 | &Idents.get("__objcFastEnumerationState")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3050 | ObjCFastEnumerationStateTypeDecl->startDefinition(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3051 | |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3052 | QualType FieldTypes[] = { |
| 3053 | UnsignedLongTy, |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3054 | getPointerType(ObjCIdTypedefType), |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3055 | getPointerType(UnsignedLongTy), |
| 3056 | getConstantArrayType(UnsignedLongTy, |
| 3057 | llvm::APInt(32, 5), ArrayType::Normal, 0) |
| 3058 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3059 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3060 | for (size_t i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3061 | FieldDecl *Field = FieldDecl::Create(*this, |
| 3062 | ObjCFastEnumerationStateTypeDecl, |
| 3063 | SourceLocation(), 0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3064 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3065 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 3066 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3067 | Field->setAccess(AS_public); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3068 | ObjCFastEnumerationStateTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3069 | } |
Fariborz Jahanian | 38c9ab8 | 2010-05-27 16:05:06 +0000 | [diff] [blame] | 3070 | if (getLangOptions().CPlusPlus) |
Fariborz Jahanian | 81148e9 | 2010-05-27 16:35:00 +0000 | [diff] [blame] | 3071 | if (CXXRecordDecl *CXXRD = |
| 3072 | dyn_cast<CXXRecordDecl>(ObjCFastEnumerationStateTypeDecl)) |
Fariborz Jahanian | 38c9ab8 | 2010-05-27 16:05:06 +0000 | [diff] [blame] | 3073 | CXXRD->setEmpty(false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3074 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3075 | ObjCFastEnumerationStateTypeDecl->completeDefinition(); |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3076 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3077 | |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3078 | return getTagDeclType(ObjCFastEnumerationStateTypeDecl); |
| 3079 | } |
| 3080 | |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3081 | QualType ASTContext::getBlockDescriptorType() { |
| 3082 | if (BlockDescriptorType) |
| 3083 | return getTagDeclType(BlockDescriptorType); |
| 3084 | |
| 3085 | RecordDecl *T; |
| 3086 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3087 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3088 | &Idents.get("__block_descriptor")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3089 | T->startDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3090 | |
| 3091 | QualType FieldTypes[] = { |
| 3092 | UnsignedLongTy, |
| 3093 | UnsignedLongTy, |
| 3094 | }; |
| 3095 | |
| 3096 | const char *FieldNames[] = { |
| 3097 | "reserved", |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3098 | "Size" |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3099 | }; |
| 3100 | |
| 3101 | for (size_t i = 0; i < 2; ++i) { |
| 3102 | FieldDecl *Field = FieldDecl::Create(*this, |
| 3103 | T, |
| 3104 | SourceLocation(), |
| 3105 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3106 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3107 | /*BitWidth=*/0, |
| 3108 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3109 | Field->setAccess(AS_public); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3110 | T->addDecl(Field); |
| 3111 | } |
| 3112 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3113 | T->completeDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3114 | |
| 3115 | BlockDescriptorType = T; |
| 3116 | |
| 3117 | return getTagDeclType(BlockDescriptorType); |
| 3118 | } |
| 3119 | |
| 3120 | void ASTContext::setBlockDescriptorType(QualType T) { |
| 3121 | const RecordType *Rec = T->getAs<RecordType>(); |
| 3122 | assert(Rec && "Invalid BlockDescriptorType"); |
| 3123 | BlockDescriptorType = Rec->getDecl(); |
| 3124 | } |
| 3125 | |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3126 | QualType ASTContext::getBlockDescriptorExtendedType() { |
| 3127 | if (BlockDescriptorExtendedType) |
| 3128 | return getTagDeclType(BlockDescriptorExtendedType); |
| 3129 | |
| 3130 | RecordDecl *T; |
| 3131 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3132 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3133 | &Idents.get("__block_descriptor_withcopydispose")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3134 | T->startDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3135 | |
| 3136 | QualType FieldTypes[] = { |
| 3137 | UnsignedLongTy, |
| 3138 | UnsignedLongTy, |
| 3139 | getPointerType(VoidPtrTy), |
| 3140 | getPointerType(VoidPtrTy) |
| 3141 | }; |
| 3142 | |
| 3143 | const char *FieldNames[] = { |
| 3144 | "reserved", |
| 3145 | "Size", |
| 3146 | "CopyFuncPtr", |
| 3147 | "DestroyFuncPtr" |
| 3148 | }; |
| 3149 | |
| 3150 | for (size_t i = 0; i < 4; ++i) { |
| 3151 | FieldDecl *Field = FieldDecl::Create(*this, |
| 3152 | T, |
| 3153 | SourceLocation(), |
| 3154 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3155 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3156 | /*BitWidth=*/0, |
| 3157 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3158 | Field->setAccess(AS_public); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3159 | T->addDecl(Field); |
| 3160 | } |
| 3161 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3162 | T->completeDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3163 | |
| 3164 | BlockDescriptorExtendedType = T; |
| 3165 | |
| 3166 | return getTagDeclType(BlockDescriptorExtendedType); |
| 3167 | } |
| 3168 | |
| 3169 | void ASTContext::setBlockDescriptorExtendedType(QualType T) { |
| 3170 | const RecordType *Rec = T->getAs<RecordType>(); |
| 3171 | assert(Rec && "Invalid BlockDescriptorType"); |
| 3172 | BlockDescriptorExtendedType = Rec->getDecl(); |
| 3173 | } |
| 3174 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3175 | bool ASTContext::BlockRequiresCopying(QualType Ty) { |
| 3176 | if (Ty->isBlockPointerType()) |
| 3177 | return true; |
| 3178 | if (isObjCNSObjectType(Ty)) |
| 3179 | return true; |
| 3180 | if (Ty->isObjCObjectPointerType()) |
| 3181 | return true; |
| 3182 | return false; |
| 3183 | } |
| 3184 | |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3185 | QualType ASTContext::BuildByRefType(llvm::StringRef DeclName, QualType Ty) { |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3186 | // type = struct __Block_byref_1_X { |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3187 | // void *__isa; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3188 | // struct __Block_byref_1_X *__forwarding; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3189 | // unsigned int __flags; |
| 3190 | // unsigned int __size; |
Mike Stump | 38e1627 | 2009-10-21 22:01:24 +0000 | [diff] [blame] | 3191 | // void *__copy_helper; // as needed |
| 3192 | // void *__destroy_help // as needed |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3193 | // int X; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3194 | // } * |
| 3195 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3196 | bool HasCopyAndDispose = BlockRequiresCopying(Ty); |
| 3197 | |
| 3198 | // FIXME: Move up |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 3199 | llvm::SmallString<36> Name; |
| 3200 | llvm::raw_svector_ostream(Name) << "__Block_byref_" << |
| 3201 | ++UniqueBlockByRefTypeID << '_' << DeclName; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3202 | RecordDecl *T; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3203 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3204 | &Idents.get(Name.str())); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3205 | T->startDefinition(); |
| 3206 | QualType Int32Ty = IntTy; |
| 3207 | assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported"); |
| 3208 | QualType FieldTypes[] = { |
| 3209 | getPointerType(VoidPtrTy), |
| 3210 | getPointerType(getTagDeclType(T)), |
| 3211 | Int32Ty, |
| 3212 | Int32Ty, |
| 3213 | getPointerType(VoidPtrTy), |
| 3214 | getPointerType(VoidPtrTy), |
| 3215 | Ty |
| 3216 | }; |
| 3217 | |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3218 | llvm::StringRef FieldNames[] = { |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3219 | "__isa", |
| 3220 | "__forwarding", |
| 3221 | "__flags", |
| 3222 | "__size", |
| 3223 | "__copy_helper", |
| 3224 | "__destroy_helper", |
| 3225 | DeclName, |
| 3226 | }; |
| 3227 | |
| 3228 | for (size_t i = 0; i < 7; ++i) { |
| 3229 | if (!HasCopyAndDispose && i >=4 && i <= 5) |
| 3230 | continue; |
| 3231 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
| 3232 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3233 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3234 | /*BitWidth=*/0, /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3235 | Field->setAccess(AS_public); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3236 | T->addDecl(Field); |
| 3237 | } |
| 3238 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3239 | T->completeDefinition(); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3240 | |
| 3241 | return getPointerType(getTagDeclType(T)); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3242 | } |
| 3243 | |
| 3244 | |
| 3245 | QualType ASTContext::getBlockParmType( |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3246 | bool BlockHasCopyDispose, |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 3247 | llvm::SmallVectorImpl<const Expr *> &Layout) { |
| 3248 | |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3249 | // FIXME: Move up |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 3250 | llvm::SmallString<36> Name; |
| 3251 | llvm::raw_svector_ostream(Name) << "__block_literal_" |
| 3252 | << ++UniqueBlockParmTypeID; |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3253 | RecordDecl *T; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3254 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3255 | &Idents.get(Name.str())); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3256 | T->startDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3257 | QualType FieldTypes[] = { |
| 3258 | getPointerType(VoidPtrTy), |
| 3259 | IntTy, |
| 3260 | IntTy, |
| 3261 | getPointerType(VoidPtrTy), |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3262 | (BlockHasCopyDispose ? |
| 3263 | getPointerType(getBlockDescriptorExtendedType()) : |
| 3264 | getPointerType(getBlockDescriptorType())) |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3265 | }; |
| 3266 | |
| 3267 | const char *FieldNames[] = { |
| 3268 | "__isa", |
| 3269 | "__flags", |
| 3270 | "__reserved", |
| 3271 | "__FuncPtr", |
| 3272 | "__descriptor" |
| 3273 | }; |
| 3274 | |
| 3275 | for (size_t i = 0; i < 5; ++i) { |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3276 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3277 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3278 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3279 | /*BitWidth=*/0, /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3280 | Field->setAccess(AS_public); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3281 | T->addDecl(Field); |
| 3282 | } |
| 3283 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 3284 | for (unsigned i = 0; i < Layout.size(); ++i) { |
| 3285 | const Expr *E = Layout[i]; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3286 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 3287 | QualType FieldType = E->getType(); |
| 3288 | IdentifierInfo *FieldName = 0; |
| 3289 | if (isa<CXXThisExpr>(E)) { |
| 3290 | FieldName = &Idents.get("this"); |
| 3291 | } else if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E)) { |
| 3292 | const ValueDecl *D = BDRE->getDecl(); |
| 3293 | FieldName = D->getIdentifier(); |
| 3294 | if (BDRE->isByRef()) |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3295 | FieldType = BuildByRefType(D->getName(), FieldType); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 3296 | } else { |
| 3297 | // Padding. |
| 3298 | assert(isa<ConstantArrayType>(FieldType) && |
| 3299 | isa<DeclRefExpr>(E) && |
| 3300 | !cast<DeclRefExpr>(E)->getDecl()->getDeclName() && |
| 3301 | "doesn't match characteristics of padding decl"); |
| 3302 | } |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3303 | |
| 3304 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 3305 | FieldName, FieldType, /*TInfo=*/0, |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3306 | /*BitWidth=*/0, /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3307 | Field->setAccess(AS_public); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3308 | T->addDecl(Field); |
| 3309 | } |
| 3310 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3311 | T->completeDefinition(); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3312 | |
| 3313 | return getPointerType(getTagDeclType(T)); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3314 | } |
| 3315 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3316 | void ASTContext::setObjCFastEnumerationStateType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3317 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3318 | assert(Rec && "Invalid ObjCFAstEnumerationStateType"); |
| 3319 | ObjCFastEnumerationStateTypeDecl = Rec->getDecl(); |
| 3320 | } |
| 3321 | |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3322 | // This returns true if a type has been typedefed to BOOL: |
| 3323 | // typedef <type> BOOL; |
Chris Lattner | 2d99833 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 3324 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3325 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | bb49c3e | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 3326 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 3327 | return II->isStr("BOOL"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3328 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3329 | return false; |
| 3330 | } |
| 3331 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3332 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3333 | /// purpose. |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3334 | CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) { |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3335 | CharUnits sz = getTypeSizeInChars(type); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3336 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3337 | // 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] | 3338 | if (sz.isPositive() && type->isIntegralOrEnumerationType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3339 | sz = std::max(sz, getTypeSizeInChars(IntTy)); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3340 | // Treat arrays as pointers, since that's how they're passed in. |
| 3341 | else if (type->isArrayType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3342 | sz = getTypeSizeInChars(VoidPtrTy); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3343 | return sz; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3344 | } |
| 3345 | |
| 3346 | static inline |
| 3347 | std::string charUnitsToString(const CharUnits &CU) { |
| 3348 | return llvm::itostr(CU.getQuantity()); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3349 | } |
| 3350 | |
Fariborz Jahanian | 6f46c26 | 2010-04-08 18:06:22 +0000 | [diff] [blame] | 3351 | /// getObjCEncodingForBlockDecl - Return the encoded type for this block |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3352 | /// declaration. |
| 3353 | void ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr, |
| 3354 | std::string& S) { |
| 3355 | const BlockDecl *Decl = Expr->getBlockDecl(); |
| 3356 | QualType BlockTy = |
| 3357 | Expr->getType()->getAs<BlockPointerType>()->getPointeeType(); |
| 3358 | // Encode result type. |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 3359 | getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3360 | // Compute size of all parameters. |
| 3361 | // Start with computing size of a pointer in number of bytes. |
| 3362 | // FIXME: There might(should) be a better way of doing this computation! |
| 3363 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3364 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
| 3365 | CharUnits ParmOffset = PtrSize; |
Fariborz Jahanian | 6f46c26 | 2010-04-08 18:06:22 +0000 | [diff] [blame] | 3366 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3367 | E = Decl->param_end(); PI != E; ++PI) { |
| 3368 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3369 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3370 | assert (sz.isPositive() && "BlockExpr - Incomplete param type"); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3371 | ParmOffset += sz; |
| 3372 | } |
| 3373 | // Size of the argument frame |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3374 | S += charUnitsToString(ParmOffset); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3375 | // Block pointer and offset. |
| 3376 | S += "@?0"; |
| 3377 | ParmOffset = PtrSize; |
| 3378 | |
| 3379 | // Argument types. |
| 3380 | ParmOffset = PtrSize; |
| 3381 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E = |
| 3382 | Decl->param_end(); PI != E; ++PI) { |
| 3383 | ParmVarDecl *PVDecl = *PI; |
| 3384 | QualType PType = PVDecl->getOriginalType(); |
| 3385 | if (const ArrayType *AT = |
| 3386 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3387 | // Use array's original type only if it has known number of |
| 3388 | // elements. |
| 3389 | if (!isa<ConstantArrayType>(AT)) |
| 3390 | PType = PVDecl->getType(); |
| 3391 | } else if (PType->isFunctionType()) |
| 3392 | PType = PVDecl->getType(); |
| 3393 | getObjCEncodingForType(PType, S); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3394 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3395 | ParmOffset += getObjCEncodingTypeSize(PType); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3396 | } |
| 3397 | } |
| 3398 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3399 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3400 | /// declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3401 | void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Chris Lattner | e6db3b0 | 2008-11-19 07:24:05 +0000 | [diff] [blame] | 3402 | std::string& S) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3403 | // FIXME: This is not very efficient. |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3404 | // Encode type qualifer, 'in', 'inout', etc. for the return type. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3405 | getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3406 | // Encode result type. |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3407 | getObjCEncodingForType(Decl->getResultType(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3408 | // Compute size of all parameters. |
| 3409 | // Start with computing size of a pointer in number of bytes. |
| 3410 | // FIXME: There might(should) be a better way of doing this computation! |
| 3411 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3412 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3413 | // The first two arguments (self and _cmd) are pointers; account for |
| 3414 | // their size. |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3415 | CharUnits ParmOffset = 2 * PtrSize; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3416 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 3417 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3418 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3419 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3420 | assert (sz.isPositive() && |
| 3421 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3422 | ParmOffset += sz; |
| 3423 | } |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3424 | S += charUnitsToString(ParmOffset); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3425 | S += "@0:"; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3426 | S += charUnitsToString(PtrSize); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3427 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3428 | // Argument types. |
| 3429 | ParmOffset = 2 * PtrSize; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3430 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 3431 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3432 | ParmVarDecl *PVDecl = *PI; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3433 | QualType PType = PVDecl->getOriginalType(); |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 3434 | if (const ArrayType *AT = |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 3435 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3436 | // Use array's original type only if it has known number of |
| 3437 | // elements. |
Steve Naroff | bb3fde3 | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 3438 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 3439 | PType = PVDecl->getType(); |
| 3440 | } else if (PType->isFunctionType()) |
| 3441 | PType = PVDecl->getType(); |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3442 | // Process argument qualifiers for user supplied arguments; such as, |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3443 | // 'in', 'inout', etc. |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 3444 | getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3445 | getObjCEncodingForType(PType, S); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3446 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3447 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3448 | } |
| 3449 | } |
| 3450 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3451 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 3452 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3453 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 3454 | /// NULL when getting encodings for protocol properties. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3455 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 3456 | /// attributes readonly and bycopy are encoded as single characters. The |
| 3457 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 3458 | /// encoded as single characters, followed by an identifier. Property types |
| 3459 | /// are also encoded as a parametrized attribute. The characters used to encode |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 3460 | /// these attributes are defined by the following enumeration: |
| 3461 | /// @code |
| 3462 | /// enum PropertyAttributes { |
| 3463 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 3464 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 3465 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 3466 | /// kPropertyDynamic = 'D', // property is dynamic |
| 3467 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 3468 | /// kPropertySetter = 'S', // followed by setter selector name |
| 3469 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
| 3470 | /// kPropertyType = 't' // followed by old-style type encoding. |
| 3471 | /// kPropertyWeak = 'W' // 'weak' property |
| 3472 | /// kPropertyStrong = 'P' // property GC'able |
| 3473 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 3474 | /// }; |
| 3475 | /// @endcode |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3476 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3477 | const Decl *Container, |
Chris Lattner | e6db3b0 | 2008-11-19 07:24:05 +0000 | [diff] [blame] | 3478 | std::string& S) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3479 | // Collect information from the property implementation decl(s). |
| 3480 | bool Dynamic = false; |
| 3481 | ObjCPropertyImplDecl *SynthesizePID = 0; |
| 3482 | |
| 3483 | // FIXME: Duplicated code due to poor abstraction. |
| 3484 | if (Container) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3485 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3486 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
| 3487 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3488 | i = CID->propimpl_begin(), e = CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3489 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3490 | ObjCPropertyImplDecl *PID = *i; |
| 3491 | if (PID->getPropertyDecl() == PD) { |
| 3492 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 3493 | Dynamic = true; |
| 3494 | } else { |
| 3495 | SynthesizePID = PID; |
| 3496 | } |
| 3497 | } |
| 3498 | } |
| 3499 | } else { |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3500 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3501 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3502 | i = OID->propimpl_begin(), e = OID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3503 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3504 | ObjCPropertyImplDecl *PID = *i; |
| 3505 | if (PID->getPropertyDecl() == PD) { |
| 3506 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 3507 | Dynamic = true; |
| 3508 | } else { |
| 3509 | SynthesizePID = PID; |
| 3510 | } |
| 3511 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3512 | } |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3513 | } |
| 3514 | } |
| 3515 | |
| 3516 | // FIXME: This is not very efficient. |
| 3517 | S = "T"; |
| 3518 | |
| 3519 | // Encode result type. |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3520 | // GCC has some special rules regarding encoding of properties which |
| 3521 | // closely resembles encoding of ivars. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3522 | getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3523 | true /* outermost type */, |
| 3524 | true /* encoding for property */); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3525 | |
| 3526 | if (PD->isReadOnly()) { |
| 3527 | S += ",R"; |
| 3528 | } else { |
| 3529 | switch (PD->getSetterKind()) { |
| 3530 | case ObjCPropertyDecl::Assign: break; |
| 3531 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3532 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3533 | } |
| 3534 | } |
| 3535 | |
| 3536 | // It really isn't clear at all what this means, since properties |
| 3537 | // are "dynamic by default". |
| 3538 | if (Dynamic) |
| 3539 | S += ",D"; |
| 3540 | |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3541 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 3542 | S += ",N"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3543 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3544 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 3545 | S += ",G"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3546 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3547 | } |
| 3548 | |
| 3549 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 3550 | S += ",S"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3551 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3552 | } |
| 3553 | |
| 3554 | if (SynthesizePID) { |
| 3555 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 3556 | S += ",V"; |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 3557 | S += OID->getNameAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3558 | } |
| 3559 | |
| 3560 | // FIXME: OBJCGC: weak & strong |
| 3561 | } |
| 3562 | |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3563 | /// getLegacyIntegralTypeEncoding - |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3564 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
| 3565 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3566 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
| 3567 | /// |
| 3568 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3569 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3570 | if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) { |
Fariborz Jahanian | c657eba | 2009-02-11 23:59:18 +0000 | [diff] [blame] | 3571 | if (BT->getKind() == BuiltinType::ULong && |
| 3572 | ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3573 | PointeeTy = UnsignedIntTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3574 | else |
Fariborz Jahanian | c657eba | 2009-02-11 23:59:18 +0000 | [diff] [blame] | 3575 | if (BT->getKind() == BuiltinType::Long && |
| 3576 | ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3577 | PointeeTy = IntTy; |
| 3578 | } |
| 3579 | } |
| 3580 | } |
| 3581 | |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3582 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 3583 | const FieldDecl *Field) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3584 | // We follow the behavior of gcc, expanding structures which are |
| 3585 | // directly pointed to, and expanding embedded structures. Note that |
| 3586 | // these rules are sufficient to prevent recursive encoding of the |
| 3587 | // same type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3588 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
Fariborz Jahanian | 5b8c7d9 | 2008-12-22 23:22:27 +0000 | [diff] [blame] | 3589 | true /* outermost type */); |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3590 | } |
| 3591 | |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3592 | static char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) { |
| 3593 | switch (T->getAs<BuiltinType>()->getKind()) { |
| 3594 | default: assert(0 && "Unhandled builtin type kind"); |
| 3595 | case BuiltinType::Void: return 'v'; |
| 3596 | case BuiltinType::Bool: return 'B'; |
| 3597 | case BuiltinType::Char_U: |
| 3598 | case BuiltinType::UChar: return 'C'; |
| 3599 | case BuiltinType::UShort: return 'S'; |
| 3600 | case BuiltinType::UInt: return 'I'; |
| 3601 | case BuiltinType::ULong: |
| 3602 | return |
| 3603 | (const_cast<ASTContext *>(C))->getIntWidth(T) == 32 ? 'L' : 'Q'; |
| 3604 | case BuiltinType::UInt128: return 'T'; |
| 3605 | case BuiltinType::ULongLong: return 'Q'; |
| 3606 | case BuiltinType::Char_S: |
| 3607 | case BuiltinType::SChar: return 'c'; |
| 3608 | case BuiltinType::Short: return 's'; |
John McCall | 24da709 | 2010-06-11 10:11:05 +0000 | [diff] [blame] | 3609 | case BuiltinType::WChar: |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3610 | case BuiltinType::Int: return 'i'; |
| 3611 | case BuiltinType::Long: |
| 3612 | return |
| 3613 | (const_cast<ASTContext *>(C))->getIntWidth(T) == 32 ? 'l' : 'q'; |
| 3614 | case BuiltinType::LongLong: return 'q'; |
| 3615 | case BuiltinType::Int128: return 't'; |
| 3616 | case BuiltinType::Float: return 'f'; |
| 3617 | case BuiltinType::Double: return 'd'; |
| 3618 | case BuiltinType::LongDouble: return 'd'; |
| 3619 | } |
| 3620 | } |
| 3621 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3622 | static void EncodeBitField(const ASTContext *Context, std::string& S, |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3623 | QualType T, const FieldDecl *FD) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3624 | const Expr *E = FD->getBitWidth(); |
| 3625 | assert(E && "bitfield width not there - getObjCEncodingForTypeImpl"); |
| 3626 | ASTContext *Ctx = const_cast<ASTContext*>(Context); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3627 | S += 'b'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3628 | // The NeXT runtime encodes bit fields as b followed by the number of bits. |
| 3629 | // The GNU runtime requires more information; bitfields are encoded as b, |
| 3630 | // then the offset (in bits) of the first element, then the type of the |
| 3631 | // bitfield, then the size in bits. For example, in this structure: |
| 3632 | // |
| 3633 | // struct |
| 3634 | // { |
| 3635 | // int integer; |
| 3636 | // int flags:2; |
| 3637 | // }; |
| 3638 | // On a 32-bit system, the encoding for flags would be b2 for the NeXT |
| 3639 | // runtime, but b32i2 for the GNU runtime. The reason for this extra |
| 3640 | // information is not especially sensible, but we're stuck with it for |
| 3641 | // compatibility with GCC, although providing it breaks anything that |
| 3642 | // actually uses runtime introspection and wants to work on both runtimes... |
| 3643 | if (!Ctx->getLangOptions().NeXTRuntime) { |
| 3644 | const RecordDecl *RD = FD->getParent(); |
| 3645 | const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD); |
| 3646 | // FIXME: This same linear search is also used in ExprConstant - it might |
| 3647 | // be better if the FieldDecl stored its offset. We'd be increasing the |
| 3648 | // size of the object slightly, but saving some time every time it is used. |
| 3649 | unsigned i = 0; |
| 3650 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 3651 | FieldEnd = RD->field_end(); |
| 3652 | Field != FieldEnd; (void)++Field, ++i) { |
| 3653 | if (*Field == FD) |
| 3654 | break; |
| 3655 | } |
| 3656 | S += llvm::utostr(RL.getFieldOffset(i)); |
| 3657 | S += ObjCEncodingForPrimitiveKind(Context, T); |
| 3658 | } |
| 3659 | unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue(); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3660 | S += llvm::utostr(N); |
| 3661 | } |
| 3662 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 3663 | // FIXME: Use SmallString for accumulating string. |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3664 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 3665 | bool ExpandPointedToStructures, |
| 3666 | bool ExpandStructures, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 3667 | const FieldDecl *FD, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3668 | bool OutermostType, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3669 | bool EncodingProperty) { |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3670 | if (T->getAs<BuiltinType>()) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3671 | if (FD && FD->isBitField()) |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3672 | return EncodeBitField(this, S, T, FD); |
| 3673 | S += ObjCEncodingForPrimitiveKind(this, T); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3674 | return; |
| 3675 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3676 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3677 | if (const ComplexType *CT = T->getAs<ComplexType>()) { |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 3678 | S += 'j'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3679 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false, |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 3680 | false); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3681 | return; |
| 3682 | } |
Fariborz Jahanian | 60bce3e | 2009-11-23 20:40:50 +0000 | [diff] [blame] | 3683 | |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 3684 | // encoding for pointer or r3eference types. |
| 3685 | QualType PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3686 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Fariborz Jahanian | 8d2c0a9 | 2009-11-30 18:43:52 +0000 | [diff] [blame] | 3687 | if (PT->isObjCSelType()) { |
| 3688 | S += ':'; |
| 3689 | return; |
| 3690 | } |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 3691 | PointeeTy = PT->getPointeeType(); |
| 3692 | } |
| 3693 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 3694 | PointeeTy = RT->getPointeeType(); |
| 3695 | if (!PointeeTy.isNull()) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3696 | bool isReadOnly = false; |
| 3697 | // For historical/compatibility reasons, the read-only qualifier of the |
| 3698 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 3699 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3700 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3701 | if (isa<TypedefType>(T.getTypePtr())) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3702 | if (OutermostType && T.isConstQualified()) { |
| 3703 | isReadOnly = true; |
| 3704 | S += 'r'; |
| 3705 | } |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 3706 | } else if (OutermostType) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3707 | QualType P = PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3708 | while (P->getAs<PointerType>()) |
| 3709 | P = P->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3710 | if (P.isConstQualified()) { |
| 3711 | isReadOnly = true; |
| 3712 | S += 'r'; |
| 3713 | } |
| 3714 | } |
| 3715 | if (isReadOnly) { |
| 3716 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 3717 | // combinations need to be rearranged. |
| 3718 | // Rewrite "in const" from "nr" to "rn" |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 3719 | if (llvm::StringRef(S).endswith("nr")) |
| 3720 | S.replace(S.end()-2, S.end(), "rn"); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3721 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3722 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3723 | if (PointeeTy->isCharType()) { |
| 3724 | // char pointer types should be encoded as '*' unless it is a |
| 3725 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3726 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3727 | S += '*'; |
| 3728 | return; |
| 3729 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3730 | } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) { |
Steve Naroff | 9533a7f | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 3731 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 3732 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 3733 | S += '#'; |
| 3734 | return; |
| 3735 | } |
| 3736 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 3737 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 3738 | S += '@'; |
| 3739 | return; |
| 3740 | } |
| 3741 | // fall through... |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3742 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3743 | S += '^'; |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3744 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 3745 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3746 | getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3747 | NULL); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3748 | return; |
| 3749 | } |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 3750 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3751 | if (const ArrayType *AT = |
| 3752 | // Ignore type qualifiers etc. |
| 3753 | dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3754 | if (isa<IncompleteArrayType>(AT)) { |
| 3755 | // Incomplete arrays are encoded as a pointer to the array element. |
| 3756 | S += '^'; |
| 3757 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3758 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3759 | false, ExpandStructures, FD); |
| 3760 | } else { |
| 3761 | S += '['; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3762 | |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3763 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 3764 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 3765 | else { |
| 3766 | //Variable length arrays are encoded as a regular array with 0 elements. |
| 3767 | assert(isa<VariableArrayType>(AT) && "Unknown array type!"); |
| 3768 | S += '0'; |
| 3769 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3770 | |
| 3771 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3772 | false, ExpandStructures, FD); |
| 3773 | S += ']'; |
| 3774 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3775 | return; |
| 3776 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3777 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3778 | if (T->getAs<FunctionType>()) { |
Anders Carlsson | c0a87b7 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 3779 | S += '?'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3780 | return; |
| 3781 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3782 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3783 | if (const RecordType *RTy = T->getAs<RecordType>()) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3784 | RecordDecl *RDecl = RTy->getDecl(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3785 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 3786 | // Anonymous structures print as '?' |
| 3787 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 3788 | S += II->getName(); |
Fariborz Jahanian | 6fb9439 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 3789 | if (ClassTemplateSpecializationDecl *Spec |
| 3790 | = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) { |
| 3791 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 3792 | std::string TemplateArgsStr |
| 3793 | = TemplateSpecializationType::PrintTemplateArgumentList( |
| 3794 | TemplateArgs.getFlatArgumentList(), |
| 3795 | TemplateArgs.flat_size(), |
| 3796 | (*this).PrintingPolicy); |
| 3797 | |
| 3798 | S += TemplateArgsStr; |
| 3799 | } |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 3800 | } else { |
| 3801 | S += '?'; |
| 3802 | } |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3803 | if (ExpandStructures) { |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3804 | S += '='; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3805 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 3806 | FieldEnd = RDecl->field_end(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3807 | Field != FieldEnd; ++Field) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3808 | if (FD) { |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3809 | S += '"'; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3810 | S += Field->getNameAsString(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3811 | S += '"'; |
| 3812 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3813 | |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3814 | // Special case bit-fields. |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3815 | if (Field->isBitField()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3816 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3817 | (*Field)); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3818 | } else { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3819 | QualType qt = Field->getType(); |
| 3820 | getLegacyIntegralTypeEncoding(qt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3821 | getObjCEncodingForTypeImpl(qt, S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3822 | FD); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3823 | } |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3824 | } |
Fariborz Jahanian | 6de88a8 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 3825 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3826 | S += RDecl->isUnion() ? ')' : '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3827 | return; |
| 3828 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3829 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3830 | if (T->isEnumeralType()) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3831 | if (FD && FD->isBitField()) |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3832 | EncodeBitField(this, S, T, FD); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3833 | else |
| 3834 | S += 'i'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3835 | return; |
| 3836 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3837 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3838 | if (T->isBlockPointerType()) { |
Steve Naroff | 21a98b1 | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 3839 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3840 | return; |
| 3841 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3842 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3843 | // Ignore protocol qualifiers when mangling at this level. |
| 3844 | if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>()) |
| 3845 | T = OT->getBaseType(); |
| 3846 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3847 | if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3848 | // @encode(class_name) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3849 | ObjCInterfaceDecl *OI = OIT->getDecl(); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3850 | S += '{'; |
| 3851 | const IdentifierInfo *II = OI->getIdentifier(); |
| 3852 | S += II->getName(); |
| 3853 | S += '='; |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3854 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3855 | CollectObjCIvars(OI, RecFields); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3856 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3857 | if (RecFields[i]->isBitField()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3858 | getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3859 | RecFields[i]); |
| 3860 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3861 | getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3862 | FD); |
| 3863 | } |
| 3864 | S += '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3865 | return; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3866 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3867 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3868 | if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3869 | if (OPT->isObjCIdType()) { |
| 3870 | S += '@'; |
| 3871 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3872 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3873 | |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 3874 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
| 3875 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
| 3876 | // Since this is a binary compatibility issue, need to consult with runtime |
| 3877 | // folks. Fortunately, this is a *very* obsure construct. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3878 | S += '#'; |
| 3879 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3880 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3881 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3882 | if (OPT->isObjCQualifiedIdType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3883 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3884 | ExpandPointedToStructures, |
| 3885 | ExpandStructures, FD); |
| 3886 | if (FD || EncodingProperty) { |
| 3887 | // Note that we do extended encoding of protocol qualifer list |
| 3888 | // Only when doing ivar or property encoding. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3889 | S += '"'; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3890 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 3891 | E = OPT->qual_end(); I != E; ++I) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3892 | S += '<'; |
| 3893 | S += (*I)->getNameAsString(); |
| 3894 | S += '>'; |
| 3895 | } |
| 3896 | S += '"'; |
| 3897 | } |
| 3898 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3899 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3900 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3901 | QualType PointeeTy = OPT->getPointeeType(); |
| 3902 | if (!EncodingProperty && |
| 3903 | isa<TypedefType>(PointeeTy.getTypePtr())) { |
| 3904 | // Another historical/compatibility reason. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3905 | // We encode the underlying type which comes out as |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3906 | // {...}; |
| 3907 | S += '^'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3908 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 3909 | false, ExpandPointedToStructures, |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3910 | NULL); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3911 | return; |
| 3912 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3913 | |
| 3914 | S += '@'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 3915 | if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3916 | S += '"'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 3917 | S += OPT->getInterfaceDecl()->getIdentifier()->getName(); |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3918 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 3919 | E = OPT->qual_end(); I != E; ++I) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3920 | S += '<'; |
| 3921 | S += (*I)->getNameAsString(); |
| 3922 | S += '>'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3923 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3924 | S += '"'; |
| 3925 | } |
| 3926 | return; |
| 3927 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3928 | |
John McCall | 532ec7b | 2010-05-17 23:56:34 +0000 | [diff] [blame] | 3929 | // gcc just blithely ignores member pointers. |
| 3930 | // TODO: maybe there should be a mangling for these |
| 3931 | if (T->getAs<MemberPointerType>()) |
| 3932 | return; |
| 3933 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3934 | assert(0 && "@encode for type not implemented!"); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3935 | } |
| 3936 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3937 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3938 | std::string& S) const { |
| 3939 | if (QT & Decl::OBJC_TQ_In) |
| 3940 | S += 'n'; |
| 3941 | if (QT & Decl::OBJC_TQ_Inout) |
| 3942 | S += 'N'; |
| 3943 | if (QT & Decl::OBJC_TQ_Out) |
| 3944 | S += 'o'; |
| 3945 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 3946 | S += 'O'; |
| 3947 | if (QT & Decl::OBJC_TQ_Byref) |
| 3948 | S += 'R'; |
| 3949 | if (QT & Decl::OBJC_TQ_Oneway) |
| 3950 | S += 'V'; |
| 3951 | } |
| 3952 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3953 | void ASTContext::setBuiltinVaListType(QualType T) { |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 3954 | assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3955 | |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 3956 | BuiltinVaListType = T; |
| 3957 | } |
| 3958 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3959 | void ASTContext::setObjCIdType(QualType T) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3960 | ObjCIdTypedefType = T; |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 3961 | } |
| 3962 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3963 | void ASTContext::setObjCSelType(QualType T) { |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 3964 | ObjCSelTypedefType = T; |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 3965 | } |
| 3966 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3967 | void ASTContext::setObjCProtoType(QualType QT) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3968 | ObjCProtoType = QT; |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 3969 | } |
| 3970 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3971 | void ASTContext::setObjCClassType(QualType T) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3972 | ObjCClassTypedefType = T; |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 3973 | } |
| 3974 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3975 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3976 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 3977 | "'NSConstantString' type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3978 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3979 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 3980 | } |
| 3981 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 3982 | /// \brief Retrieve the template name that corresponds to a non-empty |
| 3983 | /// lookup. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3984 | TemplateName ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin, |
| 3985 | UnresolvedSetIterator End) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 3986 | unsigned size = End - Begin; |
| 3987 | assert(size > 1 && "set is not overloaded!"); |
| 3988 | |
| 3989 | void *memory = Allocate(sizeof(OverloadedTemplateStorage) + |
| 3990 | size * sizeof(FunctionTemplateDecl*)); |
| 3991 | OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size); |
| 3992 | |
| 3993 | NamedDecl **Storage = OT->getStorage(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3994 | for (UnresolvedSetIterator I = Begin; I != End; ++I) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 3995 | NamedDecl *D = *I; |
| 3996 | assert(isa<FunctionTemplateDecl>(D) || |
| 3997 | (isa<UsingShadowDecl>(D) && |
| 3998 | isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))); |
| 3999 | *Storage++ = D; |
| 4000 | } |
| 4001 | |
| 4002 | return TemplateName(OT); |
| 4003 | } |
| 4004 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4005 | /// \brief Retrieve the template name that represents a qualified |
| 4006 | /// template name such as \c std::vector. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4007 | TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4008 | bool TemplateKeyword, |
| 4009 | TemplateDecl *Template) { |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4010 | // FIXME: Canonicalization? |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4011 | llvm::FoldingSetNodeID ID; |
| 4012 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 4013 | |
| 4014 | void *InsertPos = 0; |
| 4015 | QualifiedTemplateName *QTN = |
| 4016 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4017 | if (!QTN) { |
| 4018 | QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); |
| 4019 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 4020 | } |
| 4021 | |
| 4022 | return TemplateName(QTN); |
| 4023 | } |
| 4024 | |
| 4025 | /// \brief Retrieve the template name that represents a dependent |
| 4026 | /// template name such as \c MetaFun::template apply. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4027 | TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4028 | const IdentifierInfo *Name) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4029 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 4030 | "Nested name specifier must be dependent"); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4031 | |
| 4032 | llvm::FoldingSetNodeID ID; |
| 4033 | DependentTemplateName::Profile(ID, NNS, Name); |
| 4034 | |
| 4035 | void *InsertPos = 0; |
| 4036 | DependentTemplateName *QTN = |
| 4037 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4038 | |
| 4039 | if (QTN) |
| 4040 | return TemplateName(QTN); |
| 4041 | |
| 4042 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 4043 | if (CanonNNS == NNS) { |
| 4044 | QTN = new (*this,4) DependentTemplateName(NNS, Name); |
| 4045 | } else { |
| 4046 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
| 4047 | QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4048 | DependentTemplateName *CheckQTN = |
| 4049 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4050 | assert(!CheckQTN && "Dependent type name canonicalization broken"); |
| 4051 | (void)CheckQTN; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4052 | } |
| 4053 | |
| 4054 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 4055 | return TemplateName(QTN); |
| 4056 | } |
| 4057 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4058 | /// \brief Retrieve the template name that represents a dependent |
| 4059 | /// template name such as \c MetaFun::template operator+. |
| 4060 | TemplateName |
| 4061 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
| 4062 | OverloadedOperatorKind Operator) { |
| 4063 | assert((!NNS || NNS->isDependent()) && |
| 4064 | "Nested name specifier must be dependent"); |
| 4065 | |
| 4066 | llvm::FoldingSetNodeID ID; |
| 4067 | DependentTemplateName::Profile(ID, NNS, Operator); |
| 4068 | |
| 4069 | void *InsertPos = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4070 | DependentTemplateName *QTN |
| 4071 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4072 | |
| 4073 | if (QTN) |
| 4074 | return TemplateName(QTN); |
| 4075 | |
| 4076 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 4077 | if (CanonNNS == NNS) { |
| 4078 | QTN = new (*this,4) DependentTemplateName(NNS, Operator); |
| 4079 | } else { |
| 4080 | TemplateName Canon = getDependentTemplateName(CanonNNS, Operator); |
| 4081 | QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4082 | |
| 4083 | DependentTemplateName *CheckQTN |
| 4084 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4085 | assert(!CheckQTN && "Dependent template name canonicalization broken"); |
| 4086 | (void)CheckQTN; |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4087 | } |
| 4088 | |
| 4089 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 4090 | return TemplateName(QTN); |
| 4091 | } |
| 4092 | |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4093 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | d934112 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 4094 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 4095 | /// is actually a value of type @c TargetInfo::IntType. |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 4096 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4097 | switch (Type) { |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 4098 | case TargetInfo::NoInt: return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4099 | case TargetInfo::SignedShort: return ShortTy; |
| 4100 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 4101 | case TargetInfo::SignedInt: return IntTy; |
| 4102 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 4103 | case TargetInfo::SignedLong: return LongTy; |
| 4104 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 4105 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 4106 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 4107 | } |
| 4108 | |
| 4109 | assert(false && "Unhandled TargetInfo::IntType value"); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 4110 | return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4111 | } |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 4112 | |
| 4113 | //===----------------------------------------------------------------------===// |
| 4114 | // Type Predicates. |
| 4115 | //===----------------------------------------------------------------------===// |
| 4116 | |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4117 | /// isObjCNSObjectType - Return true if this is an NSObject object using |
| 4118 | /// NSObject attribute on a c-style pointer type. |
| 4119 | /// FIXME - Make it work directly on types. |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 4120 | /// FIXME: Move to Type. |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4121 | /// |
| 4122 | bool ASTContext::isObjCNSObjectType(QualType Ty) const { |
| 4123 | if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) { |
| 4124 | if (TypedefDecl *TD = TDT->getDecl()) |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 4125 | if (TD->getAttr<ObjCNSObjectAttr>()) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4126 | return true; |
| 4127 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4128 | return false; |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4129 | } |
| 4130 | |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4131 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 4132 | /// garbage collection attribute. |
| 4133 | /// |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4134 | Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const { |
| 4135 | Qualifiers::GC GCAttrs = Qualifiers::GCNone; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4136 | if (getLangOptions().ObjC1 && |
| 4137 | getLangOptions().getGCMode() != LangOptions::NonGC) { |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 4138 | GCAttrs = Ty.getObjCGCAttr(); |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4139 | // Default behavious under objective-c's gc is for objective-c pointers |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4140 | // (or pointers to them) be treated as though they were declared |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 4141 | // as __strong. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4142 | if (GCAttrs == Qualifiers::GCNone) { |
Fariborz Jahanian | 75212ee | 2009-09-10 23:38:45 +0000 | [diff] [blame] | 4143 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4144 | GCAttrs = Qualifiers::Strong; |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 4145 | else if (Ty->isPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4146 | return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 4147 | } |
Fariborz Jahanian | c211218 | 2009-04-11 00:00:54 +0000 | [diff] [blame] | 4148 | // Non-pointers have none gc'able attribute regardless of the attribute |
| 4149 | // set on them. |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 4150 | else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4151 | return Qualifiers::GCNone; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4152 | } |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 4153 | return GCAttrs; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4154 | } |
| 4155 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4156 | //===----------------------------------------------------------------------===// |
| 4157 | // Type Compatibility Testing |
| 4158 | //===----------------------------------------------------------------------===// |
Chris Lattner | 770951b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 4159 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4160 | /// areCompatVectorTypes - Return true if the two specified vector types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4161 | /// compatible. |
| 4162 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 4163 | const VectorType *RHS) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 4164 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4165 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4166 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4167 | } |
| 4168 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 4169 | bool ASTContext::areCompatibleVectorTypes(QualType FirstVec, |
| 4170 | QualType SecondVec) { |
| 4171 | assert(FirstVec->isVectorType() && "FirstVec should be a vector type"); |
| 4172 | assert(SecondVec->isVectorType() && "SecondVec should be a vector type"); |
| 4173 | |
| 4174 | if (hasSameUnqualifiedType(FirstVec, SecondVec)) |
| 4175 | return true; |
| 4176 | |
| 4177 | // AltiVec vectors types are identical to equivalent GCC vector types |
| 4178 | const VectorType *First = FirstVec->getAs<VectorType>(); |
| 4179 | const VectorType *Second = SecondVec->getAs<VectorType>(); |
| 4180 | if ((((First->getAltiVecSpecific() == VectorType::AltiVec) && |
| 4181 | (Second->getAltiVecSpecific() == VectorType::NotAltiVec)) || |
| 4182 | ((First->getAltiVecSpecific() == VectorType::NotAltiVec) && |
| 4183 | (Second->getAltiVecSpecific() == VectorType::AltiVec))) && |
| 4184 | hasSameType(First->getElementType(), Second->getElementType()) && |
| 4185 | (First->getNumElements() == Second->getNumElements())) |
| 4186 | return true; |
| 4187 | |
| 4188 | return false; |
| 4189 | } |
| 4190 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4191 | //===----------------------------------------------------------------------===// |
| 4192 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 4193 | //===----------------------------------------------------------------------===// |
| 4194 | |
| 4195 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 4196 | /// inheritance hierarchy of 'rProto'. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 4197 | bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 4198 | ObjCProtocolDecl *rProto) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4199 | if (lProto == rProto) |
| 4200 | return true; |
| 4201 | for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), |
| 4202 | E = rProto->protocol_end(); PI != E; ++PI) |
| 4203 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 4204 | return true; |
| 4205 | return false; |
| 4206 | } |
| 4207 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4208 | /// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...> |
| 4209 | /// return true if lhs's protocols conform to rhs's protocol; false |
| 4210 | /// otherwise. |
| 4211 | bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) { |
| 4212 | if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType()) |
| 4213 | return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false); |
| 4214 | return false; |
| 4215 | } |
| 4216 | |
Fariborz Jahanian | a8f8dac | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 4217 | /// ObjCQualifiedClassTypesAreCompatible - compare Class<p,...> and |
| 4218 | /// Class<p1, ...>. |
| 4219 | bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs, |
| 4220 | QualType rhs) { |
| 4221 | const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>(); |
| 4222 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
| 4223 | assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible"); |
| 4224 | |
| 4225 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4226 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4227 | bool match = false; |
| 4228 | ObjCProtocolDecl *lhsProto = *I; |
| 4229 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 4230 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 4231 | ObjCProtocolDecl *rhsProto = *J; |
| 4232 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) { |
| 4233 | match = true; |
| 4234 | break; |
| 4235 | } |
| 4236 | } |
| 4237 | if (!match) |
| 4238 | return false; |
| 4239 | } |
| 4240 | return true; |
| 4241 | } |
| 4242 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4243 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 4244 | /// ObjCQualifiedIDType. |
| 4245 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 4246 | bool compare) { |
| 4247 | // Allow id<P..> and an 'id' or void* type in all cases. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4248 | if (lhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4249 | lhs->isObjCIdType() || lhs->isObjCClassType()) |
| 4250 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4251 | else if (rhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4252 | rhs->isObjCIdType() || rhs->isObjCClassType()) |
| 4253 | return true; |
| 4254 | |
| 4255 | if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4256 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4257 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4258 | if (!rhsOPT) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4259 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4260 | if (rhsOPT->qual_empty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4261 | // If the RHS is a unqualified interface pointer "NSString*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4262 | // make sure we check the class hierarchy. |
| 4263 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 4264 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4265 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4266 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4267 | // see if static class implements all of id's protocols, directly or |
| 4268 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 4269 | if (!rhsID->ClassImplementsProtocol(*I, true)) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4270 | return false; |
| 4271 | } |
| 4272 | } |
| 4273 | // If there are no qualifiers and no interface, we have an 'id'. |
| 4274 | return true; |
| 4275 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4276 | // Both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4277 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4278 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4279 | ObjCProtocolDecl *lhsProto = *I; |
| 4280 | bool match = false; |
| 4281 | |
| 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. |
| 4285 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 4286 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 4287 | ObjCProtocolDecl *rhsProto = *J; |
| 4288 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 4289 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 4290 | match = true; |
| 4291 | break; |
| 4292 | } |
| 4293 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4294 | // If the RHS is a qualified interface pointer "NSString<P>*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4295 | // make sure we check the class hierarchy. |
| 4296 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 4297 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4298 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4299 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4300 | // see if static class implements all of id's protocols, directly or |
| 4301 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 4302 | if (rhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4303 | match = true; |
| 4304 | break; |
| 4305 | } |
| 4306 | } |
| 4307 | } |
| 4308 | if (!match) |
| 4309 | return false; |
| 4310 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4311 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4312 | return true; |
| 4313 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4314 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4315 | const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 4316 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 4317 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4318 | if (const ObjCObjectPointerType *lhsOPT = |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4319 | lhs->getAsObjCInterfacePointerType()) { |
| 4320 | if (lhsOPT->qual_empty()) { |
| 4321 | bool match = false; |
| 4322 | if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { |
| 4323 | for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(), |
| 4324 | E = rhsQID->qual_end(); I != E; ++I) { |
Fariborz Jahanian | d1909bb | 2010-08-09 18:21:43 +0000 | [diff] [blame] | 4325 | // when comparing an id<P> on rhs with a static type on lhs, |
| 4326 | // static class must implement all of id's protocols directly or |
| 4327 | // indirectly through its super class. |
Fariborz Jahanian | 192b146 | 2010-08-12 20:46:12 +0000 | [diff] [blame] | 4328 | if (lhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4329 | match = true; |
| 4330 | break; |
| 4331 | } |
| 4332 | } |
| 4333 | if (!match) |
| 4334 | return false; |
| 4335 | } |
| 4336 | return true; |
| 4337 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4338 | // Both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4339 | for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(), |
| 4340 | E = lhsOPT->qual_end(); I != E; ++I) { |
| 4341 | ObjCProtocolDecl *lhsProto = *I; |
| 4342 | bool match = false; |
| 4343 | |
| 4344 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4345 | // see if static class implements all of id's protocols, directly or |
| 4346 | // through its super class and categories. |
| 4347 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 4348 | E = rhsQID->qual_end(); J != E; ++J) { |
| 4349 | ObjCProtocolDecl *rhsProto = *J; |
| 4350 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 4351 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 4352 | match = true; |
| 4353 | break; |
| 4354 | } |
| 4355 | } |
| 4356 | if (!match) |
| 4357 | return false; |
| 4358 | } |
| 4359 | return true; |
| 4360 | } |
| 4361 | return false; |
| 4362 | } |
| 4363 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4364 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4365 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 4366 | /// protocol qualifiers on the LHS or RHS. |
| 4367 | /// |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4368 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 4369 | const ObjCObjectPointerType *RHSOPT) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4370 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 4371 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 4372 | |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 4373 | // 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] | 4374 | if (LHS->isObjCUnqualifiedIdOrClass() || |
| 4375 | RHS->isObjCUnqualifiedIdOrClass()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4376 | return true; |
| 4377 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4378 | if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4379 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 4380 | QualType(RHSOPT,0), |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4381 | false); |
Fariborz Jahanian | a8f8dac | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 4382 | |
| 4383 | if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass()) |
| 4384 | return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0), |
| 4385 | QualType(RHSOPT,0)); |
| 4386 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4387 | // If we have 2 user-defined types, fall into that path. |
| 4388 | if (LHS->getInterface() && RHS->getInterface()) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4389 | return canAssignObjCInterfaces(LHS, RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4390 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4391 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4392 | } |
| 4393 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4394 | /// canAssignObjCInterfacesInBlockPointer - This routine is specifically written |
| 4395 | /// for providing type-safty for objective-c pointers used to pass/return |
| 4396 | /// arguments in block literals. When passed as arguments, passing 'A*' where |
| 4397 | /// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is |
| 4398 | /// not OK. For the return type, the opposite is not OK. |
| 4399 | bool ASTContext::canAssignObjCInterfacesInBlockPointer( |
| 4400 | const ObjCObjectPointerType *LHSOPT, |
| 4401 | const ObjCObjectPointerType *RHSOPT) { |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 4402 | if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4403 | return true; |
| 4404 | |
| 4405 | if (LHSOPT->isObjCBuiltinType()) { |
| 4406 | return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType(); |
| 4407 | } |
| 4408 | |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 4409 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4410 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 4411 | QualType(RHSOPT,0), |
| 4412 | false); |
| 4413 | |
| 4414 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 4415 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 4416 | if (LHS && RHS) { // We have 2 user-defined types. |
| 4417 | if (LHS != RHS) { |
| 4418 | if (LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
| 4419 | return false; |
| 4420 | if (RHS->getDecl()->isSuperClassOf(LHS->getDecl())) |
| 4421 | return true; |
| 4422 | } |
| 4423 | else |
| 4424 | return true; |
| 4425 | } |
| 4426 | return false; |
| 4427 | } |
| 4428 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4429 | /// getIntersectionOfProtocols - This routine finds the intersection of set |
| 4430 | /// of protocols inherited from two distinct objective-c pointer objects. |
| 4431 | /// It is used to build composite qualifier list of the composite type of |
| 4432 | /// the conditional expression involving two objective-c pointer objects. |
| 4433 | static |
| 4434 | void getIntersectionOfProtocols(ASTContext &Context, |
| 4435 | const ObjCObjectPointerType *LHSOPT, |
| 4436 | const ObjCObjectPointerType *RHSOPT, |
| 4437 | llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) { |
| 4438 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4439 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 4440 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 4441 | assert(LHS->getInterface() && "LHS must have an interface base"); |
| 4442 | assert(RHS->getInterface() && "RHS must have an interface base"); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4443 | |
| 4444 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet; |
| 4445 | unsigned LHSNumProtocols = LHS->getNumProtocols(); |
| 4446 | if (LHSNumProtocols > 0) |
| 4447 | InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end()); |
| 4448 | else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 4449 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4450 | Context.CollectInheritedProtocols(LHS->getInterface(), |
| 4451 | LHSInheritedProtocols); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4452 | InheritedProtocolSet.insert(LHSInheritedProtocols.begin(), |
| 4453 | LHSInheritedProtocols.end()); |
| 4454 | } |
| 4455 | |
| 4456 | unsigned RHSNumProtocols = RHS->getNumProtocols(); |
| 4457 | if (RHSNumProtocols > 0) { |
Dan Gohman | cb421fa | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 4458 | ObjCProtocolDecl **RHSProtocols = |
| 4459 | const_cast<ObjCProtocolDecl **>(RHS->qual_begin()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4460 | for (unsigned i = 0; i < RHSNumProtocols; ++i) |
| 4461 | if (InheritedProtocolSet.count(RHSProtocols[i])) |
| 4462 | IntersectionOfProtocols.push_back(RHSProtocols[i]); |
| 4463 | } |
| 4464 | else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 4465 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4466 | Context.CollectInheritedProtocols(RHS->getInterface(), |
| 4467 | RHSInheritedProtocols); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 4468 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 4469 | RHSInheritedProtocols.begin(), |
| 4470 | E = RHSInheritedProtocols.end(); I != E; ++I) |
| 4471 | if (InheritedProtocolSet.count((*I))) |
| 4472 | IntersectionOfProtocols.push_back((*I)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4473 | } |
| 4474 | } |
| 4475 | |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4476 | /// areCommonBaseCompatible - Returns common base class of the two classes if |
| 4477 | /// one found. Note that this is O'2 algorithm. But it will be called as the |
| 4478 | /// last type comparison in a ?-exp of ObjC pointer types before a |
| 4479 | /// warning is issued. So, its invokation is extremely rare. |
| 4480 | QualType ASTContext::areCommonBaseCompatible( |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4481 | const ObjCObjectPointerType *Lptr, |
| 4482 | const ObjCObjectPointerType *Rptr) { |
| 4483 | const ObjCObjectType *LHS = Lptr->getObjectType(); |
| 4484 | const ObjCObjectType *RHS = Rptr->getObjectType(); |
| 4485 | const ObjCInterfaceDecl* LDecl = LHS->getInterface(); |
| 4486 | const ObjCInterfaceDecl* RDecl = RHS->getInterface(); |
| 4487 | if (!LDecl || !RDecl) |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4488 | return QualType(); |
| 4489 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4490 | while ((LDecl = LDecl->getSuperClass())) { |
| 4491 | LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4492 | if (canAssignObjCInterfaces(LHS, RHS)) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4493 | llvm::SmallVector<ObjCProtocolDecl *, 8> Protocols; |
| 4494 | getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols); |
| 4495 | |
| 4496 | QualType Result = QualType(LHS, 0); |
| 4497 | if (!Protocols.empty()) |
| 4498 | Result = getObjCObjectType(Result, Protocols.data(), Protocols.size()); |
| 4499 | Result = getObjCObjectPointerType(Result); |
| 4500 | return Result; |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4501 | } |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4502 | } |
| 4503 | |
| 4504 | return QualType(); |
| 4505 | } |
| 4506 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4507 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS, |
| 4508 | const ObjCObjectType *RHS) { |
| 4509 | assert(LHS->getInterface() && "LHS is not an interface type"); |
| 4510 | assert(RHS->getInterface() && "RHS is not an interface type"); |
| 4511 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4512 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 4513 | // the LHS. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4514 | if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface())) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4515 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4516 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4517 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 4518 | // protocol qualified at all, then we are good. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4519 | if (LHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4520 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4521 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4522 | // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it |
| 4523 | // isn't a superset. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4524 | if (RHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4525 | return true; // FIXME: should return false! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4526 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4527 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 4528 | LHSPE = LHS->qual_end(); |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4529 | LHSPI != LHSPE; LHSPI++) { |
| 4530 | bool RHSImplementsProtocol = false; |
| 4531 | |
| 4532 | // If the RHS doesn't implement the protocol on the left, the types |
| 4533 | // are incompatible. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4534 | for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(), |
| 4535 | RHSPE = RHS->qual_end(); |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 4536 | RHSPI != RHSPE; RHSPI++) { |
| 4537 | if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) { |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4538 | RHSImplementsProtocol = true; |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 4539 | break; |
| 4540 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4541 | } |
| 4542 | // FIXME: For better diagnostics, consider passing back the protocol name. |
| 4543 | if (!RHSImplementsProtocol) |
| 4544 | return false; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4545 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4546 | // The RHS implements all protocols listed on the LHS. |
| 4547 | return true; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4548 | } |
| 4549 | |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4550 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 4551 | // get the "pointed to" types |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4552 | const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
| 4553 | const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4554 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4555 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4556 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4557 | |
| 4558 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 4559 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4560 | } |
| 4561 | |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4562 | bool ASTContext::canBindObjCObjectType(QualType To, QualType From) { |
| 4563 | return canAssignObjCInterfaces( |
| 4564 | getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(), |
| 4565 | getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>()); |
| 4566 | } |
| 4567 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4568 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4569 | /// both shall have the identically qualified version of a compatible type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4570 | /// 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] | 4571 | /// same. See 6.7.[2,3,5] for additional rules. |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4572 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS, |
| 4573 | bool CompareUnqualified) { |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 4574 | if (getLangOptions().CPlusPlus) |
| 4575 | return hasSameType(LHS, RHS); |
| 4576 | |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4577 | return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4578 | } |
| 4579 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4580 | bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) { |
| 4581 | return !mergeTypes(LHS, RHS, true).isNull(); |
| 4582 | } |
| 4583 | |
| 4584 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs, |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4585 | bool OfBlockPointer, |
| 4586 | bool Unqualified) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4587 | const FunctionType *lbase = lhs->getAs<FunctionType>(); |
| 4588 | const FunctionType *rbase = rhs->getAs<FunctionType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4589 | const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 4590 | const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4591 | bool allLTypes = true; |
| 4592 | bool allRTypes = true; |
| 4593 | |
| 4594 | // Check return type |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4595 | QualType retType; |
| 4596 | if (OfBlockPointer) |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4597 | retType = mergeTypes(rbase->getResultType(), lbase->getResultType(), true, |
| 4598 | Unqualified); |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4599 | else |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4600 | retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), |
| 4601 | false, Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4602 | if (retType.isNull()) return QualType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4603 | |
| 4604 | if (Unqualified) |
| 4605 | retType = retType.getUnqualifiedType(); |
| 4606 | |
| 4607 | CanQualType LRetType = getCanonicalType(lbase->getResultType()); |
| 4608 | CanQualType RRetType = getCanonicalType(rbase->getResultType()); |
| 4609 | if (Unqualified) { |
| 4610 | LRetType = LRetType.getUnqualifiedType(); |
| 4611 | RRetType = RRetType.getUnqualifiedType(); |
| 4612 | } |
| 4613 | |
| 4614 | if (getCanonicalType(retType) != LRetType) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4615 | allLTypes = false; |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4616 | if (getCanonicalType(retType) != RRetType) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4617 | allRTypes = false; |
Daniel Dunbar | 6a15c85 | 2010-04-28 16:20:58 +0000 | [diff] [blame] | 4618 | // FIXME: double check this |
| 4619 | // FIXME: should we error if lbase->getRegParmAttr() != 0 && |
| 4620 | // rbase->getRegParmAttr() != 0 && |
| 4621 | // lbase->getRegParmAttr() != rbase->getRegParmAttr()? |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 4622 | FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo(); |
| 4623 | FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo(); |
Daniel Dunbar | 6a15c85 | 2010-04-28 16:20:58 +0000 | [diff] [blame] | 4624 | unsigned RegParm = lbaseInfo.getRegParm() == 0 ? rbaseInfo.getRegParm() : |
| 4625 | lbaseInfo.getRegParm(); |
| 4626 | bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn(); |
| 4627 | if (NoReturn != lbaseInfo.getNoReturn() || |
| 4628 | RegParm != lbaseInfo.getRegParm()) |
| 4629 | allLTypes = false; |
| 4630 | if (NoReturn != rbaseInfo.getNoReturn() || |
| 4631 | RegParm != rbaseInfo.getRegParm()) |
| 4632 | allRTypes = false; |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 4633 | CallingConv lcc = lbaseInfo.getCC(); |
| 4634 | CallingConv rcc = rbaseInfo.getCC(); |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 4635 | // Compatible functions must have compatible calling conventions |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4636 | if (!isSameCallConv(lcc, rcc)) |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 4637 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4638 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4639 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 4640 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 4641 | "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4642 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 4643 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 4644 | |
| 4645 | // Compatible functions must have the same number of arguments |
| 4646 | if (lproto_nargs != rproto_nargs) |
| 4647 | return QualType(); |
| 4648 | |
| 4649 | // Variadic and non-variadic functions aren't compatible |
| 4650 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 4651 | return QualType(); |
| 4652 | |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 4653 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 4654 | return QualType(); |
| 4655 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4656 | // Check argument compatibility |
| 4657 | llvm::SmallVector<QualType, 10> types; |
| 4658 | for (unsigned i = 0; i < lproto_nargs; i++) { |
| 4659 | QualType largtype = lproto->getArgType(i).getUnqualifiedType(); |
| 4660 | QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4661 | QualType argtype = mergeTypes(largtype, rargtype, OfBlockPointer, |
| 4662 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4663 | if (argtype.isNull()) return QualType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4664 | |
| 4665 | if (Unqualified) |
| 4666 | argtype = argtype.getUnqualifiedType(); |
| 4667 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4668 | types.push_back(argtype); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4669 | if (Unqualified) { |
| 4670 | largtype = largtype.getUnqualifiedType(); |
| 4671 | rargtype = rargtype.getUnqualifiedType(); |
| 4672 | } |
| 4673 | |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4674 | if (getCanonicalType(argtype) != getCanonicalType(largtype)) |
| 4675 | allLTypes = false; |
| 4676 | if (getCanonicalType(argtype) != getCanonicalType(rargtype)) |
| 4677 | allRTypes = false; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4678 | } |
| 4679 | if (allLTypes) return lhs; |
| 4680 | if (allRTypes) return rhs; |
| 4681 | return getFunctionType(retType, types.begin(), types.size(), |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 4682 | lproto->isVariadic(), lproto->getTypeQuals(), |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 4683 | false, false, 0, 0, |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 4684 | FunctionType::ExtInfo(NoReturn, RegParm, lcc)); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4685 | } |
| 4686 | |
| 4687 | if (lproto) allRTypes = false; |
| 4688 | if (rproto) allLTypes = false; |
| 4689 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4690 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4691 | if (proto) { |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 4692 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4693 | if (proto->isVariadic()) return QualType(); |
| 4694 | // Check that the types are compatible with the types that |
| 4695 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 4696 | // The only types actually affected are promotable integer |
| 4697 | // types and floats, which would be passed as a different |
| 4698 | // type depending on whether the prototype is visible. |
| 4699 | unsigned proto_nargs = proto->getNumArgs(); |
| 4700 | for (unsigned i = 0; i < proto_nargs; ++i) { |
| 4701 | QualType argTy = proto->getArgType(i); |
Douglas Gregor | b0f8eac | 2010-02-03 19:27:29 +0000 | [diff] [blame] | 4702 | |
| 4703 | // Look at the promotion type of enum types, since that is the type used |
| 4704 | // to pass enum values. |
| 4705 | if (const EnumType *Enum = argTy->getAs<EnumType>()) |
| 4706 | argTy = Enum->getDecl()->getPromotionType(); |
| 4707 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4708 | if (argTy->isPromotableIntegerType() || |
| 4709 | getCanonicalType(argTy).getUnqualifiedType() == FloatTy) |
| 4710 | return QualType(); |
| 4711 | } |
| 4712 | |
| 4713 | if (allLTypes) return lhs; |
| 4714 | if (allRTypes) return rhs; |
| 4715 | return getFunctionType(retType, proto->arg_type_begin(), |
Mike Stump | 2d3c191 | 2009-07-27 00:44:23 +0000 | [diff] [blame] | 4716 | proto->getNumArgs(), proto->isVariadic(), |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 4717 | proto->getTypeQuals(), |
| 4718 | false, false, 0, 0, |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 4719 | FunctionType::ExtInfo(NoReturn, RegParm, lcc)); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4720 | } |
| 4721 | |
| 4722 | if (allLTypes) return lhs; |
| 4723 | if (allRTypes) return rhs; |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 4724 | FunctionType::ExtInfo Info(NoReturn, RegParm, lcc); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 4725 | return getFunctionNoProtoType(retType, Info); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4726 | } |
| 4727 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4728 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4729 | bool OfBlockPointer, |
| 4730 | bool Unqualified) { |
Bill Wendling | 43d6975 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 4731 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 4732 | // type is adjusted to "T" prior to any further analysis, the expression |
| 4733 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4734 | // expression is an lvalue unless the reference is an rvalue reference and |
| 4735 | // the expression is a function call (possibly inside parentheses). |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 4736 | assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?"); |
| 4737 | assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?"); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4738 | |
| 4739 | if (Unqualified) { |
| 4740 | LHS = LHS.getUnqualifiedType(); |
| 4741 | RHS = RHS.getUnqualifiedType(); |
| 4742 | } |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 4743 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4744 | QualType LHSCan = getCanonicalType(LHS), |
| 4745 | RHSCan = getCanonicalType(RHS); |
| 4746 | |
| 4747 | // If two types are identical, they are compatible. |
| 4748 | if (LHSCan == RHSCan) |
| 4749 | return LHS; |
| 4750 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4751 | // If the qualifiers are different, the types aren't compatible... mostly. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4752 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 4753 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4754 | if (LQuals != RQuals) { |
| 4755 | // If any of these qualifiers are different, we have a type |
| 4756 | // mismatch. |
| 4757 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 4758 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 4759 | return QualType(); |
| 4760 | |
| 4761 | // Exactly one GC qualifier difference is allowed: __strong is |
| 4762 | // okay if the other type has no GC qualifier but is an Objective |
| 4763 | // C object pointer (i.e. implicitly strong by default). We fix |
| 4764 | // this by pretending that the unqualified type was actually |
| 4765 | // qualified __strong. |
| 4766 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 4767 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 4768 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 4769 | |
| 4770 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 4771 | return QualType(); |
| 4772 | |
| 4773 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
| 4774 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); |
| 4775 | } |
| 4776 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
| 4777 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); |
| 4778 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4779 | return QualType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4780 | } |
| 4781 | |
| 4782 | // Okay, qualifiers are equal. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4783 | |
Eli Friedman | 852d63b | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 4784 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 4785 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4786 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4787 | // We want to consider the two function types to be the same for these |
| 4788 | // comparisons, just force one to the other. |
| 4789 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 4790 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 4c721d3 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 4791 | |
| 4792 | // Same as above for arrays |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 4793 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 4794 | LHSClass = Type::ConstantArray; |
| 4795 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 4796 | RHSClass = Type::ConstantArray; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4797 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4798 | // ObjCInterfaces are just specialized ObjCObjects. |
| 4799 | if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject; |
| 4800 | if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject; |
| 4801 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 4802 | // Canonicalize ExtVector -> Vector. |
| 4803 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 4804 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4805 | |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 4806 | // If the canonical type classes don't match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4807 | if (LHSClass != RHSClass) { |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4808 | // 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] | 4809 | // a signed integer type, or an unsigned integer type. |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 4810 | // Compatibility is based on the underlying type, not the promotion |
| 4811 | // type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4812 | if (const EnumType* ETy = LHS->getAs<EnumType>()) { |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4813 | if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType()) |
| 4814 | return RHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 4815 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4816 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4817 | if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType()) |
| 4818 | return LHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 4819 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4820 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4821 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4822 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4823 | |
Steve Naroff | 4a74678 | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 4824 | // The canonical type classes match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4825 | switch (LHSClass) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4826 | #define TYPE(Class, Base) |
| 4827 | #define ABSTRACT_TYPE(Class, Base) |
John McCall | ad5e738 | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 4828 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4829 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 4830 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 4831 | #include "clang/AST/TypeNodes.def" |
| 4832 | assert(false && "Non-canonical and dependent types shouldn't get here"); |
| 4833 | return QualType(); |
| 4834 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4835 | case Type::LValueReference: |
| 4836 | case Type::RValueReference: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4837 | case Type::MemberPointer: |
| 4838 | assert(false && "C++ should never be in mergeTypes"); |
| 4839 | return QualType(); |
| 4840 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4841 | case Type::ObjCInterface: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4842 | case Type::IncompleteArray: |
| 4843 | case Type::VariableArray: |
| 4844 | case Type::FunctionProto: |
| 4845 | case Type::ExtVector: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4846 | assert(false && "Types are eliminated above"); |
| 4847 | return QualType(); |
| 4848 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4849 | case Type::Pointer: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4850 | { |
| 4851 | // Merge two pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4852 | QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); |
| 4853 | QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4854 | if (Unqualified) { |
| 4855 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 4856 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 4857 | } |
| 4858 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false, |
| 4859 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4860 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 4861 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4862 | return LHS; |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 4863 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4864 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4865 | return getPointerType(ResultType); |
| 4866 | } |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 4867 | case Type::BlockPointer: |
| 4868 | { |
| 4869 | // Merge two block pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4870 | QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); |
| 4871 | QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4872 | if (Unqualified) { |
| 4873 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 4874 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 4875 | } |
| 4876 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer, |
| 4877 | Unqualified); |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 4878 | if (ResultType.isNull()) return QualType(); |
| 4879 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 4880 | return LHS; |
| 4881 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 4882 | return RHS; |
| 4883 | return getBlockPointerType(ResultType); |
| 4884 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4885 | case Type::ConstantArray: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4886 | { |
| 4887 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 4888 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 4889 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 4890 | return QualType(); |
| 4891 | |
| 4892 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 4893 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4894 | if (Unqualified) { |
| 4895 | LHSElem = LHSElem.getUnqualifiedType(); |
| 4896 | RHSElem = RHSElem.getUnqualifiedType(); |
| 4897 | } |
| 4898 | |
| 4899 | QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4900 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4901 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 4902 | return LHS; |
| 4903 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 4904 | return RHS; |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 4905 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 4906 | ArrayType::ArraySizeModifier(), 0); |
| 4907 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 4908 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4909 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 4910 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4911 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 4912 | return LHS; |
| 4913 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 4914 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4915 | if (LVAT) { |
| 4916 | // FIXME: This isn't correct! But tricky to implement because |
| 4917 | // the array's size has to be the size of LHS, but the type |
| 4918 | // has to be different. |
| 4919 | return LHS; |
| 4920 | } |
| 4921 | if (RVAT) { |
| 4922 | // FIXME: This isn't correct! But tricky to implement because |
| 4923 | // the array's size has to be the size of RHS, but the type |
| 4924 | // has to be different. |
| 4925 | return RHS; |
| 4926 | } |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 4927 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 4928 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4929 | return getIncompleteArrayType(ResultType, |
| 4930 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4931 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4932 | case Type::FunctionNoProto: |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4933 | return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4934 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4935 | case Type::Enum: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4936 | return QualType(); |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4937 | case Type::Builtin: |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 4938 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4939 | return QualType(); |
Daniel Dunbar | 64cfdb7 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 4940 | case Type::Complex: |
| 4941 | // Distinct complex types are incompatible. |
| 4942 | return QualType(); |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 4943 | case Type::Vector: |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 4944 | // FIXME: The merged type should be an ExtVector! |
John McCall | 1c471f3 | 2010-03-12 23:14:13 +0000 | [diff] [blame] | 4945 | if (areCompatVectorTypes(LHSCan->getAs<VectorType>(), |
| 4946 | RHSCan->getAs<VectorType>())) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4947 | return LHS; |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4948 | return QualType(); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4949 | case Type::ObjCObject: { |
| 4950 | // Check if the types are assignment compatible. |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 4951 | // FIXME: This should be type compatibility, e.g. whether |
| 4952 | // "LHS x; RHS x;" at global scope is legal. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4953 | const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>(); |
| 4954 | const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>(); |
| 4955 | if (canAssignObjCInterfaces(LHSIface, RHSIface)) |
Steve Naroff | 5fd659d | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 4956 | return LHS; |
| 4957 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4958 | return QualType(); |
Cedric Venet | 61490e9 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 4959 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4960 | case Type::ObjCObjectPointer: { |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4961 | if (OfBlockPointer) { |
| 4962 | if (canAssignObjCInterfacesInBlockPointer( |
| 4963 | LHS->getAs<ObjCObjectPointerType>(), |
| 4964 | RHS->getAs<ObjCObjectPointerType>())) |
| 4965 | return LHS; |
| 4966 | return QualType(); |
| 4967 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4968 | if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(), |
| 4969 | RHS->getAs<ObjCObjectPointerType>())) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4970 | return LHS; |
| 4971 | |
Steve Naroff | bc76dd0 | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 4972 | return QualType(); |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4973 | } |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4974 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4975 | |
| 4976 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4977 | } |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 4978 | |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 4979 | /// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and |
| 4980 | /// 'RHS' attributes and returns the merged version; including for function |
| 4981 | /// return types. |
| 4982 | QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) { |
| 4983 | QualType LHSCan = getCanonicalType(LHS), |
| 4984 | RHSCan = getCanonicalType(RHS); |
| 4985 | // If two types are identical, they are compatible. |
| 4986 | if (LHSCan == RHSCan) |
| 4987 | return LHS; |
| 4988 | if (RHSCan->isFunctionType()) { |
| 4989 | if (!LHSCan->isFunctionType()) |
| 4990 | return QualType(); |
| 4991 | QualType OldReturnType = |
| 4992 | cast<FunctionType>(RHSCan.getTypePtr())->getResultType(); |
| 4993 | QualType NewReturnType = |
| 4994 | cast<FunctionType>(LHSCan.getTypePtr())->getResultType(); |
| 4995 | QualType ResReturnType = |
| 4996 | mergeObjCGCQualifiers(NewReturnType, OldReturnType); |
| 4997 | if (ResReturnType.isNull()) |
| 4998 | return QualType(); |
| 4999 | if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) { |
| 5000 | // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo(); |
| 5001 | // In either case, use OldReturnType to build the new function type. |
| 5002 | const FunctionType *F = LHS->getAs<FunctionType>(); |
| 5003 | if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) { |
| 5004 | FunctionType::ExtInfo Info = getFunctionExtInfo(LHS); |
| 5005 | QualType ResultType |
| 5006 | = getFunctionType(OldReturnType, FPT->arg_type_begin(), |
| 5007 | FPT->getNumArgs(), FPT->isVariadic(), |
| 5008 | FPT->getTypeQuals(), |
| 5009 | FPT->hasExceptionSpec(), |
| 5010 | FPT->hasAnyExceptionSpec(), |
| 5011 | FPT->getNumExceptions(), |
| 5012 | FPT->exception_begin(), |
| 5013 | Info); |
| 5014 | return ResultType; |
| 5015 | } |
| 5016 | } |
| 5017 | return QualType(); |
| 5018 | } |
| 5019 | |
| 5020 | // If the qualifiers are different, the types can still be merged. |
| 5021 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 5022 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
| 5023 | if (LQuals != RQuals) { |
| 5024 | // If any of these qualifiers are different, we have a type mismatch. |
| 5025 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 5026 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 5027 | return QualType(); |
| 5028 | |
| 5029 | // Exactly one GC qualifier difference is allowed: __strong is |
| 5030 | // okay if the other type has no GC qualifier but is an Objective |
| 5031 | // C object pointer (i.e. implicitly strong by default). We fix |
| 5032 | // this by pretending that the unqualified type was actually |
| 5033 | // qualified __strong. |
| 5034 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 5035 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 5036 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 5037 | |
| 5038 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 5039 | return QualType(); |
| 5040 | |
| 5041 | if (GC_L == Qualifiers::Strong) |
| 5042 | return LHS; |
| 5043 | if (GC_R == Qualifiers::Strong) |
| 5044 | return RHS; |
| 5045 | return QualType(); |
| 5046 | } |
| 5047 | |
| 5048 | if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) { |
| 5049 | QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5050 | QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5051 | QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT); |
| 5052 | if (ResQT == LHSBaseQT) |
| 5053 | return LHS; |
| 5054 | if (ResQT == RHSBaseQT) |
| 5055 | return RHS; |
| 5056 | } |
| 5057 | return QualType(); |
| 5058 | } |
| 5059 | |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 5060 | //===----------------------------------------------------------------------===// |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5061 | // Integer Predicates |
| 5062 | //===----------------------------------------------------------------------===// |
Chris Lattner | 88054de | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 5063 | |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5064 | unsigned ASTContext::getIntWidth(QualType T) { |
Sebastian Redl | 632d772 | 2009-11-05 21:10:57 +0000 | [diff] [blame] | 5065 | if (T->isBooleanType()) |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5066 | return 1; |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 5067 | if (EnumType *ET = dyn_cast<EnumType>(T)) |
Eli Friedman | 29a7f33 | 2009-12-10 22:29:29 +0000 | [diff] [blame] | 5068 | T = ET->getDecl()->getIntegerType(); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 5069 | // For builtin types, just use the standard type sizing method |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5070 | return (unsigned)getTypeSize(T); |
| 5071 | } |
| 5072 | |
| 5073 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) { |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5074 | assert(T->hasSignedIntegerRepresentation() && "Unexpected type"); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5075 | |
| 5076 | // Turn <4 x signed int> -> <4 x unsigned int> |
| 5077 | if (const VectorType *VTy = T->getAs<VectorType>()) |
| 5078 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 5079 | VTy->getNumElements(), VTy->getAltiVecSpecific()); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5080 | |
| 5081 | // For enums, we return the unsigned version of the base type. |
| 5082 | if (const EnumType *ETy = T->getAs<EnumType>()) |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5083 | T = ETy->getDecl()->getIntegerType(); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5084 | |
| 5085 | const BuiltinType *BTy = T->getAs<BuiltinType>(); |
| 5086 | assert(BTy && "Unexpected signed integer type"); |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5087 | switch (BTy->getKind()) { |
| 5088 | case BuiltinType::Char_S: |
| 5089 | case BuiltinType::SChar: |
| 5090 | return UnsignedCharTy; |
| 5091 | case BuiltinType::Short: |
| 5092 | return UnsignedShortTy; |
| 5093 | case BuiltinType::Int: |
| 5094 | return UnsignedIntTy; |
| 5095 | case BuiltinType::Long: |
| 5096 | return UnsignedLongTy; |
| 5097 | case BuiltinType::LongLong: |
| 5098 | return UnsignedLongLongTy; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 5099 | case BuiltinType::Int128: |
| 5100 | return UnsignedInt128Ty; |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5101 | default: |
| 5102 | assert(0 && "Unexpected signed integer type"); |
| 5103 | return QualType(); |
| 5104 | } |
| 5105 | } |
| 5106 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 5107 | ExternalASTSource::~ExternalASTSource() { } |
| 5108 | |
| 5109 | void ExternalASTSource::PrintStats() { } |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5110 | |
| 5111 | |
| 5112 | //===----------------------------------------------------------------------===// |
| 5113 | // Builtin Type Computation |
| 5114 | //===----------------------------------------------------------------------===// |
| 5115 | |
| 5116 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
| 5117 | /// pointer over the consumed characters. This returns the resultant type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5118 | static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context, |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5119 | ASTContext::GetBuiltinTypeError &Error, |
| 5120 | bool AllowTypeModifiers = true) { |
| 5121 | // Modifiers. |
| 5122 | int HowLong = 0; |
| 5123 | bool Signed = false, Unsigned = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5124 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5125 | // Read the modifiers first. |
| 5126 | bool Done = false; |
| 5127 | while (!Done) { |
| 5128 | switch (*Str++) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5129 | default: Done = true; --Str; break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5130 | case 'S': |
| 5131 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 5132 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 5133 | Signed = true; |
| 5134 | break; |
| 5135 | case 'U': |
| 5136 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
| 5137 | assert(!Unsigned && "Can't use 'S' modifier multiple times!"); |
| 5138 | Unsigned = true; |
| 5139 | break; |
| 5140 | case 'L': |
| 5141 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 5142 | ++HowLong; |
| 5143 | break; |
| 5144 | } |
| 5145 | } |
| 5146 | |
| 5147 | QualType Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5148 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5149 | // Read the base type. |
| 5150 | switch (*Str++) { |
| 5151 | default: assert(0 && "Unknown builtin type letter!"); |
| 5152 | case 'v': |
| 5153 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 5154 | "Bad modifiers used with 'v'!"); |
| 5155 | Type = Context.VoidTy; |
| 5156 | break; |
| 5157 | case 'f': |
| 5158 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 5159 | "Bad modifiers used with 'f'!"); |
| 5160 | Type = Context.FloatTy; |
| 5161 | break; |
| 5162 | case 'd': |
| 5163 | assert(HowLong < 2 && !Signed && !Unsigned && |
| 5164 | "Bad modifiers used with 'd'!"); |
| 5165 | if (HowLong) |
| 5166 | Type = Context.LongDoubleTy; |
| 5167 | else |
| 5168 | Type = Context.DoubleTy; |
| 5169 | break; |
| 5170 | case 's': |
| 5171 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 5172 | if (Unsigned) |
| 5173 | Type = Context.UnsignedShortTy; |
| 5174 | else |
| 5175 | Type = Context.ShortTy; |
| 5176 | break; |
| 5177 | case 'i': |
| 5178 | if (HowLong == 3) |
| 5179 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 5180 | else if (HowLong == 2) |
| 5181 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 5182 | else if (HowLong == 1) |
| 5183 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 5184 | else |
| 5185 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 5186 | break; |
| 5187 | case 'c': |
| 5188 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 5189 | if (Signed) |
| 5190 | Type = Context.SignedCharTy; |
| 5191 | else if (Unsigned) |
| 5192 | Type = Context.UnsignedCharTy; |
| 5193 | else |
| 5194 | Type = Context.CharTy; |
| 5195 | break; |
| 5196 | case 'b': // boolean |
| 5197 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 5198 | Type = Context.BoolTy; |
| 5199 | break; |
| 5200 | case 'z': // size_t. |
| 5201 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 5202 | Type = Context.getSizeType(); |
| 5203 | break; |
| 5204 | case 'F': |
| 5205 | Type = Context.getCFConstantStringType(); |
| 5206 | break; |
| 5207 | case 'a': |
| 5208 | Type = Context.getBuiltinVaListType(); |
| 5209 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 5210 | break; |
| 5211 | case 'A': |
| 5212 | // This is a "reference" to a va_list; however, what exactly |
| 5213 | // this means depends on how va_list is defined. There are two |
| 5214 | // different kinds of va_list: ones passed by value, and ones |
| 5215 | // passed by reference. An example of a by-value va_list is |
| 5216 | // x86, where va_list is a char*. An example of by-ref va_list |
| 5217 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 5218 | // we want this argument to be a char*&; for x86-64, we want |
| 5219 | // it to be a __va_list_tag*. |
| 5220 | Type = Context.getBuiltinVaListType(); |
| 5221 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 5222 | if (Type->isArrayType()) { |
| 5223 | Type = Context.getArrayDecayedType(Type); |
| 5224 | } else { |
| 5225 | Type = Context.getLValueReferenceType(Type); |
| 5226 | } |
| 5227 | break; |
| 5228 | case 'V': { |
| 5229 | char *End; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5230 | unsigned NumElements = strtoul(Str, &End, 10); |
| 5231 | assert(End != Str && "Missing vector size"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5232 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5233 | Str = End; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5234 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5235 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false); |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 5236 | // FIXME: Don't know what to do about AltiVec. |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 5237 | Type = Context.getVectorType(ElementType, NumElements, |
| 5238 | VectorType::NotAltiVec); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5239 | break; |
| 5240 | } |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 5241 | case 'X': { |
| 5242 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false); |
| 5243 | Type = Context.getComplexType(ElementType); |
| 5244 | break; |
| 5245 | } |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 5246 | case 'P': |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 5247 | Type = Context.getFILEType(); |
| 5248 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 5249 | Error = ASTContext::GE_Missing_stdio; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5250 | return QualType(); |
| 5251 | } |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 5252 | break; |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 5253 | case 'J': |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 5254 | if (Signed) |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 5255 | Type = Context.getsigjmp_bufType(); |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 5256 | else |
| 5257 | Type = Context.getjmp_bufType(); |
| 5258 | |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 5259 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 5260 | Error = ASTContext::GE_Missing_setjmp; |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 5261 | return QualType(); |
| 5262 | } |
| 5263 | break; |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 5264 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5265 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5266 | if (!AllowTypeModifiers) |
| 5267 | return Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5268 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5269 | Done = false; |
| 5270 | while (!Done) { |
John McCall | 187ab37 | 2010-03-12 04:21:28 +0000 | [diff] [blame] | 5271 | switch (char c = *Str++) { |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5272 | default: Done = true; --Str; break; |
| 5273 | case '*': |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5274 | case '&': |
John McCall | 187ab37 | 2010-03-12 04:21:28 +0000 | [diff] [blame] | 5275 | { |
| 5276 | // Both pointers and references can have their pointee types |
| 5277 | // qualified with an address space. |
| 5278 | char *End; |
| 5279 | unsigned AddrSpace = strtoul(Str, &End, 10); |
| 5280 | if (End != Str && AddrSpace != 0) { |
| 5281 | Type = Context.getAddrSpaceQualType(Type, AddrSpace); |
| 5282 | Str = End; |
| 5283 | } |
| 5284 | } |
| 5285 | if (c == '*') |
| 5286 | Type = Context.getPointerType(Type); |
| 5287 | else |
| 5288 | Type = Context.getLValueReferenceType(Type); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5289 | break; |
| 5290 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 5291 | case 'C': |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5292 | Type = Type.withConst(); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5293 | break; |
Fariborz Jahanian | 013af39 | 2010-01-26 22:48:42 +0000 | [diff] [blame] | 5294 | case 'D': |
| 5295 | Type = Context.getVolatileType(Type); |
| 5296 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5297 | } |
| 5298 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5299 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5300 | return Type; |
| 5301 | } |
| 5302 | |
| 5303 | /// GetBuiltinType - Return the type for the specified builtin. |
| 5304 | QualType ASTContext::GetBuiltinType(unsigned id, |
| 5305 | GetBuiltinTypeError &Error) { |
| 5306 | const char *TypeStr = BuiltinInfo.GetTypeString(id); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5307 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5308 | llvm::SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5309 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5310 | Error = GE_None; |
| 5311 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error); |
| 5312 | if (Error != GE_None) |
| 5313 | return QualType(); |
| 5314 | while (TypeStr[0] && TypeStr[0] != '.') { |
| 5315 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error); |
| 5316 | if (Error != GE_None) |
| 5317 | return QualType(); |
| 5318 | |
| 5319 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 5320 | if (Ty->isArrayType()) |
| 5321 | Ty = getArrayDecayedType(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5322 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5323 | ArgTypes.push_back(Ty); |
| 5324 | } |
| 5325 | |
| 5326 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 5327 | "'.' should only occur at end of builtin type list!"); |
| 5328 | |
| 5329 | // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);". |
| 5330 | if (ArgTypes.size() == 0 && TypeStr[0] == '.') |
| 5331 | return getFunctionNoProtoType(ResType); |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 5332 | |
| 5333 | // FIXME: Should we create noreturn types? |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5334 | return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 5335 | TypeStr[0] == '.', 0, false, false, 0, 0, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 5336 | FunctionType::ExtInfo()); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5337 | } |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5338 | |
| 5339 | QualType |
| 5340 | ASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) { |
| 5341 | // Perform the usual unary conversions. We do this early so that |
| 5342 | // integral promotions to "int" can allow us to exit early, in the |
| 5343 | // lhs == rhs check. Also, for conversion purposes, we ignore any |
| 5344 | // qualifiers. For example, "const float" and "float" are |
| 5345 | // equivalent. |
| 5346 | if (lhs->isPromotableIntegerType()) |
| 5347 | lhs = getPromotedIntegerType(lhs); |
| 5348 | else |
| 5349 | lhs = lhs.getUnqualifiedType(); |
| 5350 | if (rhs->isPromotableIntegerType()) |
| 5351 | rhs = getPromotedIntegerType(rhs); |
| 5352 | else |
| 5353 | rhs = rhs.getUnqualifiedType(); |
| 5354 | |
| 5355 | // If both types are identical, no conversion is needed. |
| 5356 | if (lhs == rhs) |
| 5357 | return lhs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5358 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5359 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 5360 | // The caller can deal with this (e.g. pointer + int). |
| 5361 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 5362 | return lhs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5363 | |
| 5364 | // At this point, we have two different arithmetic types. |
| 5365 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5366 | // Handle complex types first (C99 6.3.1.8p1). |
| 5367 | if (lhs->isComplexType() || rhs->isComplexType()) { |
| 5368 | // if we have an integer operand, the result is the complex type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5369 | if (rhs->isIntegerType() || rhs->isComplexIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5370 | // convert the rhs to the lhs complex type. |
| 5371 | return lhs; |
| 5372 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5373 | if (lhs->isIntegerType() || lhs->isComplexIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5374 | // convert the lhs to the rhs complex type. |
| 5375 | return rhs; |
| 5376 | } |
| 5377 | // This handles complex/complex, complex/float, or float/complex. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5378 | // When both operands are complex, the shorter operand is converted to the |
| 5379 | // type of the longer, and that is the type of the result. This corresponds |
| 5380 | // to what is done when combining two real floating-point operands. |
| 5381 | // The fun begins when size promotion occur across type domains. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5382 | // 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] | 5383 | // floating-point type, the less precise type is converted, within it's |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5384 | // 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] | 5385 | // when combining a "long double" with a "double _Complex", the |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5386 | // "double _Complex" is promoted to "long double _Complex". |
| 5387 | int result = getFloatingTypeOrder(lhs, rhs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5388 | |
| 5389 | if (result > 0) { // The left side is bigger, convert rhs. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5390 | rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5391 | } else if (result < 0) { // The right side is bigger, convert lhs. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5392 | lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5393 | } |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5394 | // At this point, lhs and rhs have the same rank/size. Now, make sure the |
| 5395 | // domains match. This is a requirement for our implementation, C99 |
| 5396 | // does not require this promotion. |
| 5397 | if (lhs != rhs) { // Domains don't match, we have complex/float mix. |
| 5398 | if (lhs->isRealFloatingType()) { // handle "double, _Complex double". |
| 5399 | return rhs; |
| 5400 | } else { // handle "_Complex double, double". |
| 5401 | return lhs; |
| 5402 | } |
| 5403 | } |
| 5404 | return lhs; // The domain/size match exactly. |
| 5405 | } |
| 5406 | // Now handle "real" floating types (i.e. float, double, long double). |
| 5407 | if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) { |
| 5408 | // if we have an integer operand, the result is the real floating type. |
| 5409 | if (rhs->isIntegerType()) { |
| 5410 | // convert rhs to the lhs floating point type. |
| 5411 | return lhs; |
| 5412 | } |
| 5413 | if (rhs->isComplexIntegerType()) { |
| 5414 | // convert rhs to the complex floating point type. |
| 5415 | return getComplexType(lhs); |
| 5416 | } |
| 5417 | if (lhs->isIntegerType()) { |
| 5418 | // convert lhs to the rhs floating point type. |
| 5419 | return rhs; |
| 5420 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5421 | if (lhs->isComplexIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5422 | // convert lhs to the complex floating point type. |
| 5423 | return getComplexType(rhs); |
| 5424 | } |
| 5425 | // We have two real floating types, float/complex combos were handled above. |
| 5426 | // Convert the smaller operand to the bigger result. |
| 5427 | int result = getFloatingTypeOrder(lhs, rhs); |
| 5428 | if (result > 0) // convert the rhs |
| 5429 | return lhs; |
| 5430 | assert(result < 0 && "illegal float comparison"); |
| 5431 | return rhs; // convert the lhs |
| 5432 | } |
| 5433 | if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) { |
| 5434 | // Handle GCC complex int extension. |
| 5435 | const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType(); |
| 5436 | const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType(); |
| 5437 | |
| 5438 | if (lhsComplexInt && rhsComplexInt) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5439 | if (getIntegerTypeOrder(lhsComplexInt->getElementType(), |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5440 | rhsComplexInt->getElementType()) >= 0) |
| 5441 | return lhs; // convert the rhs |
| 5442 | return rhs; |
| 5443 | } else if (lhsComplexInt && rhs->isIntegerType()) { |
| 5444 | // convert the rhs to the lhs complex type. |
| 5445 | return lhs; |
| 5446 | } else if (rhsComplexInt && lhs->isIntegerType()) { |
| 5447 | // convert the lhs to the rhs complex type. |
| 5448 | return rhs; |
| 5449 | } |
| 5450 | } |
| 5451 | // Finally, we have two differing integer types. |
| 5452 | // The rules for this case are in C99 6.3.1.8 |
| 5453 | int compare = getIntegerTypeOrder(lhs, rhs); |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5454 | bool lhsSigned = lhs->hasSignedIntegerRepresentation(), |
| 5455 | rhsSigned = rhs->hasSignedIntegerRepresentation(); |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5456 | QualType destType; |
| 5457 | if (lhsSigned == rhsSigned) { |
| 5458 | // Same signedness; use the higher-ranked type |
| 5459 | destType = compare >= 0 ? lhs : rhs; |
| 5460 | } else if (compare != (lhsSigned ? 1 : -1)) { |
| 5461 | // The unsigned type has greater than or equal rank to the |
| 5462 | // signed type, so use the unsigned type |
| 5463 | destType = lhsSigned ? rhs : lhs; |
| 5464 | } else if (getIntWidth(lhs) != getIntWidth(rhs)) { |
| 5465 | // The two types are different widths; if we are here, that |
| 5466 | // means the signed type is larger than the unsigned type, so |
| 5467 | // use the signed type. |
| 5468 | destType = lhsSigned ? lhs : rhs; |
| 5469 | } else { |
| 5470 | // The signed type is higher-ranked than the unsigned type, |
| 5471 | // but isn't actually any bigger (like unsigned int and long |
| 5472 | // on most 32-bit systems). Use the unsigned type corresponding |
| 5473 | // to the signed type. |
| 5474 | destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs); |
| 5475 | } |
| 5476 | return destType; |
| 5477 | } |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5478 | |
| 5479 | GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) { |
| 5480 | GVALinkage External = GVA_StrongExternal; |
| 5481 | |
| 5482 | Linkage L = FD->getLinkage(); |
| 5483 | if (L == ExternalLinkage && getLangOptions().CPlusPlus && |
| 5484 | FD->getType()->getLinkage() == UniqueExternalLinkage) |
| 5485 | L = UniqueExternalLinkage; |
| 5486 | |
| 5487 | switch (L) { |
| 5488 | case NoLinkage: |
| 5489 | case InternalLinkage: |
| 5490 | case UniqueExternalLinkage: |
| 5491 | return GVA_Internal; |
| 5492 | |
| 5493 | case ExternalLinkage: |
| 5494 | switch (FD->getTemplateSpecializationKind()) { |
| 5495 | case TSK_Undeclared: |
| 5496 | case TSK_ExplicitSpecialization: |
| 5497 | External = GVA_StrongExternal; |
| 5498 | break; |
| 5499 | |
| 5500 | case TSK_ExplicitInstantiationDefinition: |
| 5501 | return GVA_ExplicitTemplateInstantiation; |
| 5502 | |
| 5503 | case TSK_ExplicitInstantiationDeclaration: |
| 5504 | case TSK_ImplicitInstantiation: |
| 5505 | External = GVA_TemplateInstantiation; |
| 5506 | break; |
| 5507 | } |
| 5508 | } |
| 5509 | |
| 5510 | if (!FD->isInlined()) |
| 5511 | return External; |
| 5512 | |
| 5513 | if (!getLangOptions().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) { |
| 5514 | // GNU or C99 inline semantics. Determine whether this symbol should be |
| 5515 | // externally visible. |
| 5516 | if (FD->isInlineDefinitionExternallyVisible()) |
| 5517 | return External; |
| 5518 | |
| 5519 | // C99 inline semantics, where the symbol is not externally visible. |
| 5520 | return GVA_C99Inline; |
| 5521 | } |
| 5522 | |
| 5523 | // C++0x [temp.explicit]p9: |
| 5524 | // [ Note: The intent is that an inline function that is the subject of |
| 5525 | // an explicit instantiation declaration will still be implicitly |
| 5526 | // instantiated when used so that the body can be considered for |
| 5527 | // inlining, but that no out-of-line copy of the inline function would be |
| 5528 | // generated in the translation unit. -- end note ] |
| 5529 | if (FD->getTemplateSpecializationKind() |
| 5530 | == TSK_ExplicitInstantiationDeclaration) |
| 5531 | return GVA_C99Inline; |
| 5532 | |
| 5533 | return GVA_CXXInline; |
| 5534 | } |
| 5535 | |
| 5536 | GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) { |
| 5537 | // If this is a static data member, compute the kind of template |
| 5538 | // specialization. Otherwise, this variable is not part of a |
| 5539 | // template. |
| 5540 | TemplateSpecializationKind TSK = TSK_Undeclared; |
| 5541 | if (VD->isStaticDataMember()) |
| 5542 | TSK = VD->getTemplateSpecializationKind(); |
| 5543 | |
| 5544 | Linkage L = VD->getLinkage(); |
| 5545 | if (L == ExternalLinkage && getLangOptions().CPlusPlus && |
| 5546 | VD->getType()->getLinkage() == UniqueExternalLinkage) |
| 5547 | L = UniqueExternalLinkage; |
| 5548 | |
| 5549 | switch (L) { |
| 5550 | case NoLinkage: |
| 5551 | case InternalLinkage: |
| 5552 | case UniqueExternalLinkage: |
| 5553 | return GVA_Internal; |
| 5554 | |
| 5555 | case ExternalLinkage: |
| 5556 | switch (TSK) { |
| 5557 | case TSK_Undeclared: |
| 5558 | case TSK_ExplicitSpecialization: |
| 5559 | return GVA_StrongExternal; |
| 5560 | |
| 5561 | case TSK_ExplicitInstantiationDeclaration: |
| 5562 | llvm_unreachable("Variable should not be instantiated"); |
| 5563 | // Fall through to treat this like any other instantiation. |
| 5564 | |
| 5565 | case TSK_ExplicitInstantiationDefinition: |
| 5566 | return GVA_ExplicitTemplateInstantiation; |
| 5567 | |
| 5568 | case TSK_ImplicitInstantiation: |
| 5569 | return GVA_TemplateInstantiation; |
| 5570 | } |
| 5571 | } |
| 5572 | |
| 5573 | return GVA_StrongExternal; |
| 5574 | } |
| 5575 | |
Argyrios Kyrtzidis | 4ac7c0b | 2010-07-29 20:08:05 +0000 | [diff] [blame] | 5576 | bool ASTContext::DeclMustBeEmitted(const Decl *D) { |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5577 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 5578 | if (!VD->isFileVarDecl()) |
| 5579 | return false; |
| 5580 | } else if (!isa<FunctionDecl>(D)) |
| 5581 | return false; |
| 5582 | |
Argyrios Kyrtzidis | ab411c8 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 5583 | // Weak references don't produce any output by themselves. |
| 5584 | if (D->hasAttr<WeakRefAttr>()) |
| 5585 | return false; |
| 5586 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5587 | // Aliases and used decls are required. |
| 5588 | if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>()) |
| 5589 | return true; |
| 5590 | |
| 5591 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 5592 | // Forward declarations aren't required. |
| 5593 | if (!FD->isThisDeclarationADefinition()) |
| 5594 | return false; |
| 5595 | |
| 5596 | // Constructors and destructors are required. |
| 5597 | if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>()) |
| 5598 | return true; |
| 5599 | |
| 5600 | // The key function for a class is required. |
| 5601 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 5602 | const CXXRecordDecl *RD = MD->getParent(); |
| 5603 | if (MD->isOutOfLine() && RD->isDynamicClass()) { |
| 5604 | const CXXMethodDecl *KeyFunc = getKeyFunction(RD); |
| 5605 | if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl()) |
| 5606 | return true; |
| 5607 | } |
| 5608 | } |
| 5609 | |
| 5610 | GVALinkage Linkage = GetGVALinkageForFunction(FD); |
| 5611 | |
| 5612 | // static, static inline, always_inline, and extern inline functions can |
| 5613 | // always be deferred. Normal inline functions can be deferred in C99/C++. |
| 5614 | // Implicit template instantiations can also be deferred in C++. |
| 5615 | if (Linkage == GVA_Internal || Linkage == GVA_C99Inline || |
| 5616 | Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) |
| 5617 | return false; |
| 5618 | return true; |
| 5619 | } |
| 5620 | |
| 5621 | const VarDecl *VD = cast<VarDecl>(D); |
| 5622 | assert(VD->isFileVarDecl() && "Expected file scoped var"); |
| 5623 | |
Argyrios Kyrtzidis | ab411c8 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 5624 | if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly) |
| 5625 | return false; |
| 5626 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5627 | // Structs that have non-trivial constructors or destructors are required. |
| 5628 | |
| 5629 | // FIXME: Handle references. |
| 5630 | if (const RecordType *RT = VD->getType()->getAs<RecordType>()) { |
| 5631 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5632 | if (RD->hasDefinition() && |
| 5633 | (!RD->hasTrivialConstructor() || !RD->hasTrivialDestructor())) |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5634 | return true; |
| 5635 | } |
| 5636 | } |
| 5637 | |
| 5638 | GVALinkage L = GetGVALinkageForVariable(VD); |
| 5639 | if (L == GVA_Internal || L == GVA_TemplateInstantiation) { |
| 5640 | if (!(VD->getInit() && VD->getInit()->HasSideEffects(*this))) |
| 5641 | return false; |
| 5642 | } |
| 5643 | |
| 5644 | return true; |
| 5645 | } |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 5646 | |
| 5647 | CXXABI::~CXXABI() {} |