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" |
Anders Carlsson | 29445a0 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 31 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 32 | using namespace clang; |
| 33 | |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 34 | unsigned ASTContext::NumImplicitDefaultConstructors; |
| 35 | unsigned ASTContext::NumImplicitDefaultConstructorsDeclared; |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 36 | unsigned ASTContext::NumImplicitCopyConstructors; |
| 37 | unsigned ASTContext::NumImplicitCopyConstructorsDeclared; |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 38 | unsigned ASTContext::NumImplicitCopyAssignmentOperators; |
| 39 | unsigned ASTContext::NumImplicitCopyAssignmentOperatorsDeclared; |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 40 | unsigned ASTContext::NumImplicitDestructors; |
| 41 | unsigned ASTContext::NumImplicitDestructorsDeclared; |
| 42 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 43 | enum FloatingRank { |
| 44 | FloatRank, DoubleRank, LongDoubleRank |
| 45 | }; |
| 46 | |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 47 | void |
| 48 | ASTContext::CanonicalTemplateTemplateParm::Profile(llvm::FoldingSetNodeID &ID, |
| 49 | TemplateTemplateParmDecl *Parm) { |
| 50 | ID.AddInteger(Parm->getDepth()); |
| 51 | ID.AddInteger(Parm->getPosition()); |
| 52 | // FIXME: Parameter pack |
| 53 | |
| 54 | TemplateParameterList *Params = Parm->getTemplateParameters(); |
| 55 | ID.AddInteger(Params->size()); |
| 56 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 57 | PEnd = Params->end(); |
| 58 | P != PEnd; ++P) { |
| 59 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
| 60 | ID.AddInteger(0); |
| 61 | ID.AddBoolean(TTP->isParameterPack()); |
| 62 | continue; |
| 63 | } |
| 64 | |
| 65 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 66 | ID.AddInteger(1); |
| 67 | // FIXME: Parameter pack |
| 68 | ID.AddPointer(NTTP->getType().getAsOpaquePtr()); |
| 69 | continue; |
| 70 | } |
| 71 | |
| 72 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); |
| 73 | ID.AddInteger(2); |
| 74 | Profile(ID, TTP); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | TemplateTemplateParmDecl * |
| 79 | ASTContext::getCanonicalTemplateTemplateParmDecl( |
| 80 | TemplateTemplateParmDecl *TTP) { |
| 81 | // Check if we already have a canonical template template parameter. |
| 82 | llvm::FoldingSetNodeID ID; |
| 83 | CanonicalTemplateTemplateParm::Profile(ID, TTP); |
| 84 | void *InsertPos = 0; |
| 85 | CanonicalTemplateTemplateParm *Canonical |
| 86 | = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
| 87 | if (Canonical) |
| 88 | return Canonical->getParam(); |
| 89 | |
| 90 | // Build a canonical template parameter list. |
| 91 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 92 | llvm::SmallVector<NamedDecl *, 4> CanonParams; |
| 93 | CanonParams.reserve(Params->size()); |
| 94 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 95 | PEnd = Params->end(); |
| 96 | P != PEnd; ++P) { |
| 97 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) |
| 98 | CanonParams.push_back( |
| 99 | TemplateTypeParmDecl::Create(*this, getTranslationUnitDecl(), |
| 100 | SourceLocation(), TTP->getDepth(), |
| 101 | TTP->getIndex(), 0, false, |
| 102 | TTP->isParameterPack())); |
| 103 | else if (NonTypeTemplateParmDecl *NTTP |
| 104 | = dyn_cast<NonTypeTemplateParmDecl>(*P)) |
| 105 | CanonParams.push_back( |
| 106 | NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
| 107 | SourceLocation(), NTTP->getDepth(), |
| 108 | NTTP->getPosition(), 0, |
| 109 | getCanonicalType(NTTP->getType()), |
| 110 | 0)); |
| 111 | else |
| 112 | CanonParams.push_back(getCanonicalTemplateTemplateParmDecl( |
| 113 | cast<TemplateTemplateParmDecl>(*P))); |
| 114 | } |
| 115 | |
| 116 | TemplateTemplateParmDecl *CanonTTP |
| 117 | = TemplateTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
| 118 | SourceLocation(), TTP->getDepth(), |
| 119 | TTP->getPosition(), 0, |
| 120 | TemplateParameterList::Create(*this, SourceLocation(), |
| 121 | SourceLocation(), |
| 122 | CanonParams.data(), |
| 123 | CanonParams.size(), |
| 124 | SourceLocation())); |
| 125 | |
| 126 | // Get the new insert position for the node we care about. |
| 127 | Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
| 128 | assert(Canonical == 0 && "Shouldn't be in the map!"); |
| 129 | (void)Canonical; |
| 130 | |
| 131 | // Create the canonical template template parameter entry. |
| 132 | Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP); |
| 133 | CanonTemplateTemplateParms.InsertNode(Canonical, InsertPos); |
| 134 | return CanonTTP; |
| 135 | } |
| 136 | |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 137 | ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM, |
Daniel Dunbar | 444be73 | 2009-11-13 05:51:54 +0000 | [diff] [blame] | 138 | const TargetInfo &t, |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 139 | IdentifierTable &idents, SelectorTable &sels, |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 140 | Builtin::Context &builtins, |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 141 | unsigned size_reserve) : |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 142 | TemplateSpecializationTypes(this_()), |
| 143 | DependentTemplateSpecializationTypes(this_()), |
Argyrios Kyrtzidis | 0061138 | 2010-07-04 21:44:19 +0000 | [diff] [blame] | 144 | GlobalNestedNameSpecifier(0), IsInt128Installed(false), |
| 145 | CFConstantStringTypeDecl(0), NSConstantStringTypeDecl(0), |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 146 | ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0), |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 147 | sigjmp_bufDecl(0), BlockDescriptorType(0), BlockDescriptorExtendedType(0), |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 148 | NullTypeSourceInfo(QualType()), |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 149 | SourceMgr(SM), LangOpts(LOpts), Target(t), |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 150 | Idents(idents), Selectors(sels), |
Ted Kremenek | ac9590e | 2010-05-10 20:40:08 +0000 | [diff] [blame] | 151 | BuiltinInfo(builtins), |
| 152 | DeclarationNames(*this), |
| 153 | ExternalSource(0), PrintingPolicy(LOpts), |
Ted Kremenek | f057bf7 | 2010-06-18 00:31:04 +0000 | [diff] [blame] | 154 | LastSDM(0, 0), |
| 155 | UniqueBlockByRefTypeID(0), UniqueBlockParmTypeID(0) { |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 156 | ObjCIdRedefinitionType = QualType(); |
| 157 | ObjCClassRedefinitionType = QualType(); |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 158 | ObjCSelRedefinitionType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 159 | if (size_reserve > 0) Types.reserve(size_reserve); |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 160 | TUDecl = TranslationUnitDecl::Create(*this); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 161 | InitBuiltinTypes(); |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 164 | ASTContext::~ASTContext() { |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 165 | // Release the DenseMaps associated with DeclContext objects. |
| 166 | // FIXME: Is this the ideal solution? |
| 167 | ReleaseDeclContextMaps(); |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 168 | |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 169 | // Call all of the deallocation functions. |
| 170 | for (unsigned I = 0, N = Deallocations.size(); I != N; ++I) |
| 171 | Deallocations[I].first(Deallocations[I].second); |
Douglas Gregor | 0054531 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 172 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 173 | // Release all of the memory associated with overridden C++ methods. |
| 174 | for (llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::iterator |
| 175 | OM = OverriddenMethods.begin(), OMEnd = OverriddenMethods.end(); |
| 176 | OM != OMEnd; ++OM) |
| 177 | OM->second.Destroy(); |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 178 | |
Ted Kremenek | dcfcfbe | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 179 | // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 180 | // because they can contain DenseMaps. |
| 181 | for (llvm::DenseMap<const ObjCContainerDecl*, |
| 182 | const ASTRecordLayout*>::iterator |
| 183 | I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) |
| 184 | // Increment in loop to prevent using deallocated memory. |
| 185 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 186 | R->Destroy(*this); |
| 187 | |
Ted Kremenek | dcfcfbe | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 188 | for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator |
| 189 | I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) { |
| 190 | // Increment in loop to prevent using deallocated memory. |
| 191 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 192 | R->Destroy(*this); |
| 193 | } |
Ted Kremenek | bbfd68d | 2009-12-23 21:13:52 +0000 | [diff] [blame] | 194 | } |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 195 | |
Douglas Gregor | 0054531 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 196 | void ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) { |
| 197 | Deallocations.push_back(std::make_pair(Callback, Data)); |
| 198 | } |
| 199 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 200 | void |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 201 | ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) { |
| 202 | ExternalSource.reset(Source.take()); |
| 203 | } |
| 204 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 205 | void ASTContext::PrintStats() const { |
| 206 | fprintf(stderr, "*** AST Context Stats:\n"); |
| 207 | fprintf(stderr, " %d types total.\n", (int)Types.size()); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 208 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 209 | unsigned counts[] = { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 210 | #define TYPE(Name, Parent) 0, |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 211 | #define ABSTRACT_TYPE(Name, Parent) |
| 212 | #include "clang/AST/TypeNodes.def" |
| 213 | 0 // Extra |
| 214 | }; |
Douglas Gregor | c2ee10d | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 215 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 216 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 217 | Type *T = Types[i]; |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 218 | counts[(unsigned)T->getTypeClass()]++; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 221 | unsigned Idx = 0; |
| 222 | unsigned TotalBytes = 0; |
| 223 | #define TYPE(Name, Parent) \ |
| 224 | if (counts[Idx]) \ |
| 225 | fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \ |
| 226 | TotalBytes += counts[Idx] * sizeof(Name##Type); \ |
| 227 | ++Idx; |
| 228 | #define ABSTRACT_TYPE(Name, Parent) |
| 229 | #include "clang/AST/TypeNodes.def" |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 230 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 231 | fprintf(stderr, "Total bytes = %d\n", int(TotalBytes)); |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 232 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 233 | // Implicit special member functions. |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 234 | fprintf(stderr, " %u/%u implicit default constructors created\n", |
| 235 | NumImplicitDefaultConstructorsDeclared, |
| 236 | NumImplicitDefaultConstructors); |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 237 | fprintf(stderr, " %u/%u implicit copy constructors created\n", |
| 238 | NumImplicitCopyConstructorsDeclared, |
| 239 | NumImplicitCopyConstructors); |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 240 | fprintf(stderr, " %u/%u implicit copy assignment operators created\n", |
| 241 | NumImplicitCopyAssignmentOperatorsDeclared, |
| 242 | NumImplicitCopyAssignmentOperators); |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 243 | fprintf(stderr, " %u/%u implicit destructors created\n", |
| 244 | NumImplicitDestructorsDeclared, NumImplicitDestructors); |
| 245 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 246 | if (ExternalSource.get()) { |
| 247 | fprintf(stderr, "\n"); |
| 248 | ExternalSource->PrintStats(); |
| 249 | } |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 250 | |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 251 | BumpAlloc.PrintStats(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 255 | void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 256 | BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 257 | R = CanQualType::CreateUnsafe(QualType(Ty, 0)); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 258 | Types.push_back(Ty); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 261 | void ASTContext::InitBuiltinTypes() { |
| 262 | assert(VoidTy.isNull() && "Context reinitialized?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 263 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 264 | // C99 6.2.5p19. |
| 265 | InitBuiltinType(VoidTy, BuiltinType::Void); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 266 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 267 | // C99 6.2.5p2. |
| 268 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
| 269 | // C99 6.2.5p3. |
Eli Friedman | 15b9176 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 270 | if (LangOpts.CharIsSigned) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 271 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 272 | else |
| 273 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
| 274 | // C99 6.2.5p4. |
| 275 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 276 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 277 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 278 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 279 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 280 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 281 | // C99 6.2.5p6. |
| 282 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 283 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 284 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 285 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 286 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 287 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 288 | // C99 6.2.5p10. |
| 289 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 290 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 291 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 292 | |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 293 | // GNU extension, 128-bit integers. |
| 294 | InitBuiltinType(Int128Ty, BuiltinType::Int128); |
| 295 | InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128); |
| 296 | |
Chris Lattner | 3a25032 | 2009-02-26 23:43:47 +0000 | [diff] [blame] | 297 | if (LangOpts.CPlusPlus) // C++ 3.9.1p5 |
| 298 | InitBuiltinType(WCharTy, BuiltinType::WChar); |
| 299 | else // C99 |
| 300 | WCharTy = getFromTargetType(Target.getWCharType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 301 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 302 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 303 | InitBuiltinType(Char16Ty, BuiltinType::Char16); |
| 304 | else // C99 |
| 305 | Char16Ty = getFromTargetType(Target.getChar16Type()); |
| 306 | |
| 307 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 308 | InitBuiltinType(Char32Ty, BuiltinType::Char32); |
| 309 | else // C99 |
| 310 | Char32Ty = getFromTargetType(Target.getChar32Type()); |
| 311 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 312 | // Placeholder type for functions. |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 313 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
| 314 | |
| 315 | // Placeholder type for type-dependent expressions whose type is |
| 316 | // completely unknown. No code should ever check a type against |
| 317 | // DependentTy and users should never see it; however, it is here to |
| 318 | // help diagnose failures to properly check for type-dependent |
| 319 | // expressions. |
| 320 | InitBuiltinType(DependentTy, BuiltinType::Dependent); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 321 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 322 | // Placeholder type for C++0x auto declarations whose real type has |
Anders Carlsson | e89d159 | 2009-06-26 18:41:36 +0000 | [diff] [blame] | 323 | // not yet been deduced. |
| 324 | InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 325 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 326 | // C99 6.2.5p11. |
| 327 | FloatComplexTy = getComplexType(FloatTy); |
| 328 | DoubleComplexTy = getComplexType(DoubleTy); |
| 329 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 330 | |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 331 | BuiltinVaListType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 332 | |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 333 | // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope(). |
| 334 | ObjCIdTypedefType = QualType(); |
| 335 | ObjCClassTypedefType = QualType(); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 336 | ObjCSelTypedefType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 337 | |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 338 | // Builtin types for 'id', 'Class', and 'SEL'. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 339 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); |
| 340 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 341 | InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 342 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 343 | ObjCConstantStringType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 344 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 345 | // void * type |
| 346 | VoidPtrTy = getPointerType(VoidTy); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 347 | |
| 348 | // nullptr type (C++0x 2.14.7) |
| 349 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 352 | MemberSpecializationInfo * |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 353 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 354 | assert(Var->isStaticDataMember() && "Not a static data member"); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 355 | llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 356 | = InstantiatedFromStaticDataMember.find(Var); |
| 357 | if (Pos == InstantiatedFromStaticDataMember.end()) |
| 358 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 359 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 360 | return Pos->second; |
| 361 | } |
| 362 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 363 | void |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 364 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, |
Argyrios Kyrtzidis | 9421adc | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 365 | TemplateSpecializationKind TSK, |
| 366 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 367 | assert(Inst->isStaticDataMember() && "Not a static data member"); |
| 368 | assert(Tmpl->isStaticDataMember() && "Not a static data member"); |
| 369 | assert(!InstantiatedFromStaticDataMember[Inst] && |
| 370 | "Already noted what static data member was instantiated from"); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 371 | InstantiatedFromStaticDataMember[Inst] |
Argyrios Kyrtzidis | 9421adc | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 372 | = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 373 | } |
| 374 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 375 | NamedDecl * |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 376 | ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 377 | llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 378 | = InstantiatedFromUsingDecl.find(UUD); |
| 379 | if (Pos == InstantiatedFromUsingDecl.end()) |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 380 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 381 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 382 | return Pos->second; |
| 383 | } |
| 384 | |
| 385 | void |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 386 | ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) { |
| 387 | assert((isa<UsingDecl>(Pattern) || |
| 388 | isa<UnresolvedUsingValueDecl>(Pattern) || |
| 389 | isa<UnresolvedUsingTypenameDecl>(Pattern)) && |
| 390 | "pattern decl is not a using decl"); |
| 391 | assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists"); |
| 392 | InstantiatedFromUsingDecl[Inst] = Pattern; |
| 393 | } |
| 394 | |
| 395 | UsingShadowDecl * |
| 396 | ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) { |
| 397 | llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos |
| 398 | = InstantiatedFromUsingShadowDecl.find(Inst); |
| 399 | if (Pos == InstantiatedFromUsingShadowDecl.end()) |
| 400 | return 0; |
| 401 | |
| 402 | return Pos->second; |
| 403 | } |
| 404 | |
| 405 | void |
| 406 | ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, |
| 407 | UsingShadowDecl *Pattern) { |
| 408 | assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists"); |
| 409 | InstantiatedFromUsingShadowDecl[Inst] = Pattern; |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 412 | FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { |
| 413 | llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos |
| 414 | = InstantiatedFromUnnamedFieldDecl.find(Field); |
| 415 | if (Pos == InstantiatedFromUnnamedFieldDecl.end()) |
| 416 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 417 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 418 | return Pos->second; |
| 419 | } |
| 420 | |
| 421 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, |
| 422 | FieldDecl *Tmpl) { |
| 423 | assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed"); |
| 424 | assert(!Tmpl->getDeclName() && "Template field decl is not unnamed"); |
| 425 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] && |
| 426 | "Already noted what unnamed field was instantiated from"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 427 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 428 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; |
| 429 | } |
| 430 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 431 | ASTContext::overridden_cxx_method_iterator |
| 432 | ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const { |
| 433 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 434 | = OverriddenMethods.find(Method); |
| 435 | if (Pos == OverriddenMethods.end()) |
| 436 | return 0; |
| 437 | |
| 438 | return Pos->second.begin(); |
| 439 | } |
| 440 | |
| 441 | ASTContext::overridden_cxx_method_iterator |
| 442 | ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const { |
| 443 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 444 | = OverriddenMethods.find(Method); |
| 445 | if (Pos == OverriddenMethods.end()) |
| 446 | return 0; |
| 447 | |
| 448 | return Pos->second.end(); |
| 449 | } |
| 450 | |
Argyrios Kyrtzidis | c91e9f4 | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 451 | unsigned |
| 452 | ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const { |
| 453 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 454 | = OverriddenMethods.find(Method); |
| 455 | if (Pos == OverriddenMethods.end()) |
| 456 | return 0; |
| 457 | |
| 458 | return Pos->second.size(); |
| 459 | } |
| 460 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 461 | void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method, |
| 462 | const CXXMethodDecl *Overridden) { |
| 463 | OverriddenMethods[Method].push_back(Overridden); |
| 464 | } |
| 465 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 466 | namespace { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 467 | class BeforeInTranslationUnit |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 468 | : std::binary_function<SourceRange, SourceRange, bool> { |
| 469 | SourceManager *SourceMgr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 470 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 471 | public: |
| 472 | explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 473 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 474 | bool operator()(SourceRange X, SourceRange Y) { |
| 475 | return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin()); |
| 476 | } |
| 477 | }; |
| 478 | } |
| 479 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 480 | //===----------------------------------------------------------------------===// |
| 481 | // Type Sizing and Analysis |
| 482 | //===----------------------------------------------------------------------===// |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 483 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 484 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 485 | /// scalar floating point type. |
| 486 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 487 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 488 | assert(BT && "Not a floating point type!"); |
| 489 | switch (BT->getKind()) { |
| 490 | default: assert(0 && "Not a floating point type!"); |
| 491 | case BuiltinType::Float: return Target.getFloatFormat(); |
| 492 | case BuiltinType::Double: return Target.getDoubleFormat(); |
| 493 | case BuiltinType::LongDouble: return Target.getLongDoubleFormat(); |
| 494 | } |
| 495 | } |
| 496 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 497 | /// getDeclAlign - Return a conservative estimate of the alignment of the |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 498 | /// specified decl. Note that bitfields do not have a valid alignment, so |
| 499 | /// this method will assert on them. |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 500 | /// If @p RefAsPointee, references are treated like their underlying type |
| 501 | /// (for alignof), else they're treated like pointers (for CodeGen). |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 502 | CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) { |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 503 | unsigned Align = Target.getCharWidth(); |
| 504 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 505 | if (const AlignedAttr* AA = D->getAttr<AlignedAttr>()) |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 506 | Align = std::max(Align, AA->getMaxAlignment()); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 507 | |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 508 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
| 509 | QualType T = VD->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 510 | if (const ReferenceType* RT = T->getAs<ReferenceType>()) { |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 511 | if (RefAsPointee) |
| 512 | T = RT->getPointeeType(); |
| 513 | else |
| 514 | T = getPointerType(RT->getPointeeType()); |
| 515 | } |
| 516 | if (!T->isIncompleteType() && !T->isFunctionType()) { |
Rafael Espindola | 6deecb0 | 2010-06-04 23:15:27 +0000 | [diff] [blame] | 517 | unsigned MinWidth = Target.getLargeArrayMinWidth(); |
| 518 | unsigned ArrayAlign = Target.getLargeArrayAlign(); |
| 519 | if (isa<VariableArrayType>(T) && MinWidth != 0) |
| 520 | Align = std::max(Align, ArrayAlign); |
| 521 | if (ConstantArrayType *CT = dyn_cast<ConstantArrayType>(T)) { |
| 522 | unsigned Size = getTypeSize(CT); |
| 523 | if (MinWidth != 0 && MinWidth <= Size) |
| 524 | Align = std::max(Align, ArrayAlign); |
| 525 | } |
Anders Carlsson | 4cc2cfd | 2009-04-10 04:47:03 +0000 | [diff] [blame] | 526 | // Incomplete or function types default to 1. |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 527 | while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T)) |
| 528 | T = cast<ArrayType>(T)->getElementType(); |
| 529 | |
| 530 | Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); |
| 531 | } |
Charles Davis | 05f6247 | 2010-02-23 04:52:00 +0000 | [diff] [blame] | 532 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) { |
| 533 | // In the case of a field in a packed struct, we want the minimum |
| 534 | // of the alignment of the field and the alignment of the struct. |
| 535 | Align = std::min(Align, |
| 536 | getPreferredTypeAlign(FD->getParent()->getTypeForDecl())); |
| 537 | } |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 538 | } |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 539 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 540 | return CharUnits::fromQuantity(Align / Target.getCharWidth()); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 541 | } |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 542 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 543 | std::pair<CharUnits, CharUnits> |
| 544 | ASTContext::getTypeInfoInChars(const Type *T) { |
| 545 | std::pair<uint64_t, unsigned> Info = getTypeInfo(T); |
| 546 | return std::make_pair(CharUnits::fromQuantity(Info.first / getCharWidth()), |
| 547 | CharUnits::fromQuantity(Info.second / getCharWidth())); |
| 548 | } |
| 549 | |
| 550 | std::pair<CharUnits, CharUnits> |
| 551 | ASTContext::getTypeInfoInChars(QualType T) { |
| 552 | return getTypeInfoInChars(T.getTypePtr()); |
| 553 | } |
| 554 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 555 | /// getTypeSize - Return the size of the specified type, in bits. This method |
| 556 | /// does not work on incomplete types. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 557 | /// |
| 558 | /// FIXME: Pointers into different addr spaces could have different sizes and |
| 559 | /// alignment requirements: getPointerInfo should take an AddrSpace, this |
| 560 | /// should take a QualType, &c. |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 561 | std::pair<uint64_t, unsigned> |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 562 | ASTContext::getTypeInfo(const Type *T) { |
Mike Stump | 5e30100 | 2009-02-27 18:32:39 +0000 | [diff] [blame] | 563 | uint64_t Width=0; |
| 564 | unsigned Align=8; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 565 | switch (T->getTypeClass()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 566 | #define TYPE(Class, Base) |
| 567 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 568 | #define NON_CANONICAL_TYPE(Class, Base) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 569 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 570 | #include "clang/AST/TypeNodes.def" |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 571 | assert(false && "Should not see dependent types"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 572 | break; |
| 573 | |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 574 | case Type::FunctionNoProto: |
| 575 | case Type::FunctionProto: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 576 | // GCC extension: alignof(function) = 32 bits |
| 577 | Width = 0; |
| 578 | Align = 32; |
| 579 | break; |
| 580 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 581 | case Type::IncompleteArray: |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 582 | case Type::VariableArray: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 583 | Width = 0; |
| 584 | Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); |
| 585 | break; |
| 586 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 587 | case Type::ConstantArray: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 588 | const ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 589 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 590 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 591 | Width = EltInfo.first*CAT->getSize().getZExtValue(); |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 592 | Align = EltInfo.second; |
| 593 | break; |
Christopher Lamb | 5c09a02 | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 594 | } |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 595 | case Type::ExtVector: |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 596 | case Type::Vector: { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 597 | const VectorType *VT = cast<VectorType>(T); |
| 598 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType()); |
| 599 | Width = EltInfo.first*VT->getNumElements(); |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 600 | Align = Width; |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 601 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 602 | // This happens for non-power-of-2 length vectors. |
Dan Gohman | 8eefcd3 | 2010-04-21 23:32:43 +0000 | [diff] [blame] | 603 | if (Align & (Align-1)) { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 604 | Align = llvm::NextPowerOf2(Align); |
| 605 | Width = llvm::RoundUpToAlignment(Width, Align); |
| 606 | } |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 607 | break; |
| 608 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 609 | |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 610 | case Type::Builtin: |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 611 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 612 | default: assert(0 && "Unknown builtin type!"); |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 613 | case BuiltinType::Void: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 614 | // GCC extension: alignof(void) = 8 bits. |
| 615 | Width = 0; |
| 616 | Align = 8; |
| 617 | break; |
| 618 | |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 619 | case BuiltinType::Bool: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 620 | Width = Target.getBoolWidth(); |
| 621 | Align = Target.getBoolAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 622 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 623 | case BuiltinType::Char_S: |
| 624 | case BuiltinType::Char_U: |
| 625 | case BuiltinType::UChar: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 626 | case BuiltinType::SChar: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 627 | Width = Target.getCharWidth(); |
| 628 | Align = Target.getCharAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 629 | break; |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 630 | case BuiltinType::WChar: |
| 631 | Width = Target.getWCharWidth(); |
| 632 | Align = Target.getWCharAlign(); |
| 633 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 634 | case BuiltinType::Char16: |
| 635 | Width = Target.getChar16Width(); |
| 636 | Align = Target.getChar16Align(); |
| 637 | break; |
| 638 | case BuiltinType::Char32: |
| 639 | Width = Target.getChar32Width(); |
| 640 | Align = Target.getChar32Align(); |
| 641 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 642 | case BuiltinType::UShort: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 643 | case BuiltinType::Short: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 644 | Width = Target.getShortWidth(); |
| 645 | Align = Target.getShortAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 646 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 647 | case BuiltinType::UInt: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 648 | case BuiltinType::Int: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 649 | Width = Target.getIntWidth(); |
| 650 | Align = Target.getIntAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 651 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 652 | case BuiltinType::ULong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 653 | case BuiltinType::Long: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 654 | Width = Target.getLongWidth(); |
| 655 | Align = Target.getLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 656 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 657 | case BuiltinType::ULongLong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 658 | case BuiltinType::LongLong: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 659 | Width = Target.getLongLongWidth(); |
| 660 | Align = Target.getLongLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 661 | break; |
Chris Lattner | ec16cb9 | 2009-04-30 02:55:13 +0000 | [diff] [blame] | 662 | case BuiltinType::Int128: |
| 663 | case BuiltinType::UInt128: |
| 664 | Width = 128; |
| 665 | Align = 128; // int128_t is 128-bit aligned on all targets. |
| 666 | break; |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 667 | case BuiltinType::Float: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 668 | Width = Target.getFloatWidth(); |
| 669 | Align = Target.getFloatAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 670 | break; |
| 671 | case BuiltinType::Double: |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 672 | Width = Target.getDoubleWidth(); |
| 673 | Align = Target.getDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 674 | break; |
| 675 | case BuiltinType::LongDouble: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 676 | Width = Target.getLongDoubleWidth(); |
| 677 | Align = Target.getLongDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 678 | break; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 679 | case BuiltinType::NullPtr: |
| 680 | Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) |
| 681 | Align = Target.getPointerAlign(0); // == sizeof(void*) |
Sebastian Redl | 1590d9c | 2009-05-27 19:34:06 +0000 | [diff] [blame] | 682 | break; |
Fariborz Jahanian | e04f5fc | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 683 | case BuiltinType::ObjCId: |
| 684 | case BuiltinType::ObjCClass: |
| 685 | case BuiltinType::ObjCSel: |
| 686 | Width = Target.getPointerWidth(0); |
| 687 | Align = Target.getPointerAlign(0); |
| 688 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 689 | } |
Chris Lattner | bfef6d7 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 690 | break; |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 691 | case Type::ObjCObjectPointer: |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 692 | Width = Target.getPointerWidth(0); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 693 | Align = Target.getPointerAlign(0); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 694 | break; |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 695 | case Type::BlockPointer: { |
| 696 | unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace(); |
| 697 | Width = Target.getPointerWidth(AS); |
| 698 | Align = Target.getPointerAlign(AS); |
| 699 | break; |
| 700 | } |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 701 | case Type::LValueReference: |
| 702 | case Type::RValueReference: { |
| 703 | // alignof and sizeof should never enter this code path here, so we go |
| 704 | // the pointer route. |
| 705 | unsigned AS = cast<ReferenceType>(T)->getPointeeType().getAddressSpace(); |
| 706 | Width = Target.getPointerWidth(AS); |
| 707 | Align = Target.getPointerAlign(AS); |
| 708 | break; |
| 709 | } |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 710 | case Type::Pointer: { |
| 711 | unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace(); |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 712 | Width = Target.getPointerWidth(AS); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 713 | Align = Target.getPointerAlign(AS); |
| 714 | break; |
| 715 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 716 | case Type::MemberPointer: { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 717 | QualType Pointee = cast<MemberPointerType>(T)->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 718 | std::pair<uint64_t, unsigned> PtrDiffInfo = |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 719 | getTypeInfo(getPointerDiffType()); |
| 720 | Width = PtrDiffInfo.first; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 721 | if (Pointee->isFunctionType()) |
| 722 | Width *= 2; |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 723 | Align = PtrDiffInfo.second; |
| 724 | break; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 725 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 726 | case Type::Complex: { |
| 727 | // Complex types have the same alignment as their elements, but twice the |
| 728 | // size. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 729 | std::pair<uint64_t, unsigned> EltInfo = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 730 | getTypeInfo(cast<ComplexType>(T)->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 731 | Width = EltInfo.first*2; |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 732 | Align = EltInfo.second; |
| 733 | break; |
| 734 | } |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 735 | case Type::ObjCObject: |
| 736 | return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr()); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 737 | case Type::ObjCInterface: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 738 | const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 739 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
| 740 | Width = Layout.getSize(); |
| 741 | Align = Layout.getAlignment(); |
| 742 | break; |
| 743 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 744 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 745 | case Type::Enum: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 746 | const TagType *TT = cast<TagType>(T); |
| 747 | |
| 748 | if (TT->getDecl()->isInvalidDecl()) { |
Chris Lattner | 8389eab | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 749 | Width = 1; |
| 750 | Align = 1; |
| 751 | break; |
| 752 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 753 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 754 | if (const EnumType *ET = dyn_cast<EnumType>(TT)) |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 755 | return getTypeInfo(ET->getDecl()->getIntegerType()); |
| 756 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 757 | const RecordType *RT = cast<RecordType>(TT); |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 758 | const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); |
| 759 | Width = Layout.getSize(); |
| 760 | Align = Layout.getAlignment(); |
Chris Lattner | dc0d73e | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 761 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 762 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 763 | |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 764 | case Type::SubstTemplateTypeParm: |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 765 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> |
| 766 | getReplacementType().getTypePtr()); |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 767 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 768 | case Type::Typedef: { |
| 769 | const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl(); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 770 | if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 771 | Align = std::max(Aligned->getMaxAlignment(), |
| 772 | getTypeAlign(Typedef->getUnderlyingType().getTypePtr())); |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 773 | Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr()); |
| 774 | } else |
| 775 | return getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 776 | break; |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 777 | } |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 778 | |
| 779 | case Type::TypeOfExpr: |
| 780 | return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType() |
| 781 | .getTypePtr()); |
| 782 | |
| 783 | case Type::TypeOf: |
| 784 | return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr()); |
| 785 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 786 | case Type::Decltype: |
| 787 | return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType() |
| 788 | .getTypePtr()); |
| 789 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 790 | case Type::Elaborated: |
| 791 | return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 792 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 793 | case Type::TemplateSpecialization: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 794 | assert(getCanonicalType(T) != T && |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 795 | "Cannot request the size of a dependent type"); |
| 796 | // FIXME: this is likely to be wrong once we support template |
| 797 | // aliases, since a template alias could refer to a typedef that |
| 798 | // has an __aligned__ attribute on it. |
| 799 | return getTypeInfo(getCanonicalType(T)); |
| 800 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 801 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 802 | assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2"); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 803 | return std::make_pair(Width, Align); |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 804 | } |
| 805 | |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 806 | /// getTypeSizeInChars - Return the size of the specified type, in characters. |
| 807 | /// This method does not work on incomplete types. |
| 808 | CharUnits ASTContext::getTypeSizeInChars(QualType T) { |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 809 | return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth()); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 810 | } |
| 811 | CharUnits ASTContext::getTypeSizeInChars(const Type *T) { |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 812 | return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth()); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 813 | } |
| 814 | |
Ken Dyck | 16e20cc | 2010-01-26 17:25:18 +0000 | [diff] [blame] | 815 | /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 816 | /// characters. This method does not work on incomplete types. |
| 817 | CharUnits ASTContext::getTypeAlignInChars(QualType T) { |
| 818 | return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth()); |
| 819 | } |
| 820 | CharUnits ASTContext::getTypeAlignInChars(const Type *T) { |
| 821 | return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth()); |
| 822 | } |
| 823 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 824 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 825 | /// type for the current target in bits. This can be different than the ABI |
| 826 | /// alignment in cases where it is beneficial for performance to overalign |
| 827 | /// a data type. |
| 828 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) { |
| 829 | unsigned ABIAlign = getTypeAlign(T); |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 830 | |
| 831 | // Double and long long should be naturally aligned if possible. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 832 | if (const ComplexType* CT = T->getAs<ComplexType>()) |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 833 | T = CT->getElementType().getTypePtr(); |
| 834 | if (T->isSpecificBuiltinType(BuiltinType::Double) || |
| 835 | T->isSpecificBuiltinType(BuiltinType::LongLong)) |
| 836 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); |
| 837 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 838 | return ABIAlign; |
| 839 | } |
| 840 | |
Daniel Dunbar | a80a0f6 | 2009-04-22 17:43:55 +0000 | [diff] [blame] | 841 | static void CollectLocalObjCIvars(ASTContext *Ctx, |
| 842 | const ObjCInterfaceDecl *OI, |
| 843 | llvm::SmallVectorImpl<FieldDecl*> &Fields) { |
Fariborz Jahanian | a769c00 | 2008-12-17 21:40:49 +0000 | [diff] [blame] | 844 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 845 | E = OI->ivar_end(); I != E; ++I) { |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 846 | ObjCIvarDecl *IVDecl = *I; |
Fariborz Jahanian | a769c00 | 2008-12-17 21:40:49 +0000 | [diff] [blame] | 847 | if (!IVDecl->isInvalidDecl()) |
| 848 | Fields.push_back(cast<FieldDecl>(IVDecl)); |
| 849 | } |
| 850 | } |
| 851 | |
Daniel Dunbar | a80a0f6 | 2009-04-22 17:43:55 +0000 | [diff] [blame] | 852 | void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 853 | llvm::SmallVectorImpl<FieldDecl*> &Fields) { |
| 854 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
| 855 | CollectObjCIvars(SuperClass, Fields); |
| 856 | CollectLocalObjCIvars(this, OI, Fields); |
| 857 | } |
| 858 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 859 | /// ShallowCollectObjCIvars - |
| 860 | /// Collect all ivars, including those synthesized, in the current class. |
| 861 | /// |
| 862 | void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI, |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 863 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) { |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 864 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 865 | E = OI->ivar_end(); I != E; ++I) { |
| 866 | Ivars.push_back(*I); |
| 867 | } |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 868 | |
| 869 | CollectNonClassIvars(OI, Ivars); |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 870 | } |
| 871 | |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 872 | /// CollectNonClassIvars - |
| 873 | /// 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] | 874 | /// This includes synthesized ivars (via @synthesize) and those in |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 875 | // class's @implementation. |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 876 | /// |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 877 | void ASTContext::CollectNonClassIvars(const ObjCInterfaceDecl *OI, |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 878 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) { |
Fariborz Jahanian | 0e5ad25 | 2010-02-23 01:26:30 +0000 | [diff] [blame] | 879 | // Find ivars declared in class extension. |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 880 | for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl; |
| 881 | CDecl = CDecl->getNextClassExtension()) { |
Fariborz Jahanian | 0e5ad25 | 2010-02-23 01:26:30 +0000 | [diff] [blame] | 882 | for (ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(), |
| 883 | E = CDecl->ivar_end(); I != E; ++I) { |
| 884 | Ivars.push_back(*I); |
| 885 | } |
| 886 | } |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 887 | |
Ted Kremenek | 7d2aa11 | 2010-03-11 19:44:54 +0000 | [diff] [blame] | 888 | // Also add any ivar defined in this class's implementation. This |
| 889 | // includes synthesized ivars. |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 890 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) { |
| 891 | for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(), |
| 892 | E = ImplDecl->ivar_end(); I != E; ++I) |
| 893 | Ivars.push_back(*I); |
| 894 | } |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 895 | } |
| 896 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 897 | /// CollectInheritedProtocols - Collect all protocols in current class and |
| 898 | /// those inherited by it. |
| 899 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 900 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 901 | if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
| 902 | for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(), |
| 903 | PE = OI->protocol_end(); P != PE; ++P) { |
| 904 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 905 | Protocols.insert(Proto); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 906 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 907 | PE = Proto->protocol_end(); P != PE; ++P) { |
| 908 | Protocols.insert(*P); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 909 | CollectInheritedProtocols(*P, Protocols); |
| 910 | } |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 911 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 912 | |
| 913 | // Categories of this Interface. |
| 914 | for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList(); |
| 915 | CDeclChain; CDeclChain = CDeclChain->getNextClassCategory()) |
| 916 | CollectInheritedProtocols(CDeclChain, Protocols); |
| 917 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) |
| 918 | while (SD) { |
| 919 | CollectInheritedProtocols(SD, Protocols); |
| 920 | SD = SD->getSuperClass(); |
| 921 | } |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 922 | } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 923 | for (ObjCInterfaceDecl::protocol_iterator P = OC->protocol_begin(), |
| 924 | PE = OC->protocol_end(); P != PE; ++P) { |
| 925 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 926 | Protocols.insert(Proto); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 927 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 928 | PE = Proto->protocol_end(); P != PE; ++P) |
| 929 | CollectInheritedProtocols(*P, Protocols); |
| 930 | } |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 931 | } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 932 | for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(), |
| 933 | PE = OP->protocol_end(); P != PE; ++P) { |
| 934 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 935 | Protocols.insert(Proto); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 936 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 937 | PE = Proto->protocol_end(); P != PE; ++P) |
| 938 | CollectInheritedProtocols(*P, Protocols); |
| 939 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 940 | } |
| 941 | } |
| 942 | |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 943 | unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) { |
| 944 | unsigned count = 0; |
| 945 | // Count ivars declared in class extension. |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 946 | for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl; |
| 947 | CDecl = CDecl->getNextClassExtension()) |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 948 | count += CDecl->ivar_size(); |
| 949 | |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 950 | // Count ivar defined in this class's implementation. This |
| 951 | // includes synthesized ivars. |
| 952 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 953 | count += ImplDecl->ivar_size(); |
| 954 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 955 | return count; |
| 956 | } |
| 957 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 958 | /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. |
| 959 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
| 960 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 961 | I = ObjCImpls.find(D); |
| 962 | if (I != ObjCImpls.end()) |
| 963 | return cast<ObjCImplementationDecl>(I->second); |
| 964 | return 0; |
| 965 | } |
| 966 | /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. |
| 967 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
| 968 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 969 | I = ObjCImpls.find(D); |
| 970 | if (I != ObjCImpls.end()) |
| 971 | return cast<ObjCCategoryImplDecl>(I->second); |
| 972 | return 0; |
| 973 | } |
| 974 | |
| 975 | /// \brief Set the implementation of ObjCInterfaceDecl. |
| 976 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
| 977 | ObjCImplementationDecl *ImplD) { |
| 978 | assert(IFaceD && ImplD && "Passed null params"); |
| 979 | ObjCImpls[IFaceD] = ImplD; |
| 980 | } |
| 981 | /// \brief Set the implementation of ObjCCategoryDecl. |
| 982 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
| 983 | ObjCCategoryImplDecl *ImplD) { |
| 984 | assert(CatD && ImplD && "Passed null params"); |
| 985 | ObjCImpls[CatD] = ImplD; |
| 986 | } |
| 987 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 988 | /// \brief Allocate an uninitialized TypeSourceInfo. |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 989 | /// |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 990 | /// The caller should initialize the memory held by TypeSourceInfo using |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 991 | /// the TypeLoc wrappers. |
| 992 | /// |
| 993 | /// \param T the type that will be the basis for type source info. This type |
| 994 | /// should refer to how the declarator was written in source code, not to |
| 995 | /// what type semantic analysis resolved the declarator to. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 996 | TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T, |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 997 | unsigned DataSize) { |
| 998 | if (!DataSize) |
| 999 | DataSize = TypeLoc::getFullDataSizeForType(T); |
| 1000 | else |
| 1001 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1002 | "incorrect data size provided to CreateTypeSourceInfo!"); |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1003 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1004 | TypeSourceInfo *TInfo = |
| 1005 | (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8); |
| 1006 | new (TInfo) TypeSourceInfo(T); |
| 1007 | return TInfo; |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1010 | TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, |
John McCall | a4eb74d | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 1011 | SourceLocation L) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1012 | TypeSourceInfo *DI = CreateTypeSourceInfo(T); |
John McCall | a4eb74d | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 1013 | DI->getTypeLoc().initialize(L); |
| 1014 | return DI; |
| 1015 | } |
| 1016 | |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1017 | const ASTRecordLayout & |
| 1018 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) { |
| 1019 | return getObjCLayout(D, 0); |
| 1020 | } |
| 1021 | |
| 1022 | const ASTRecordLayout & |
| 1023 | ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) { |
| 1024 | return getObjCLayout(D->getClassInterface(), D); |
| 1025 | } |
| 1026 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1027 | //===----------------------------------------------------------------------===// |
| 1028 | // Type creation/memoization methods |
| 1029 | //===----------------------------------------------------------------------===// |
| 1030 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1031 | QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) { |
| 1032 | unsigned Fast = Quals.getFastQualifiers(); |
| 1033 | Quals.removeFastQualifiers(); |
| 1034 | |
| 1035 | // Check if we've already instantiated this type. |
| 1036 | llvm::FoldingSetNodeID ID; |
| 1037 | ExtQuals::Profile(ID, TypeNode, Quals); |
| 1038 | void *InsertPos = 0; |
| 1039 | if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) { |
| 1040 | assert(EQ->getQualifiers() == Quals); |
| 1041 | QualType T = QualType(EQ, Fast); |
| 1042 | return T; |
| 1043 | } |
| 1044 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1045 | ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1046 | ExtQualNodes.InsertNode(New, InsertPos); |
| 1047 | QualType T = QualType(New, Fast); |
| 1048 | return T; |
| 1049 | } |
| 1050 | |
| 1051 | QualType ASTContext::getVolatileType(QualType T) { |
| 1052 | QualType CanT = getCanonicalType(T); |
| 1053 | if (CanT.isVolatileQualified()) return T; |
| 1054 | |
| 1055 | QualifierCollector Quals; |
| 1056 | const Type *TypeNode = Quals.strip(T); |
| 1057 | Quals.addVolatile(); |
| 1058 | |
| 1059 | return getExtQualType(TypeNode, Quals); |
| 1060 | } |
| 1061 | |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 1062 | QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1063 | QualType CanT = getCanonicalType(T); |
| 1064 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1065 | return T; |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1066 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1067 | // If we are composing extended qualifiers together, merge together |
| 1068 | // into one ExtQuals node. |
| 1069 | QualifierCollector Quals; |
| 1070 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1071 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1072 | // If this type already has an address space specified, it cannot get |
| 1073 | // another one. |
| 1074 | assert(!Quals.hasAddressSpace() && |
| 1075 | "Type cannot be in multiple addr spaces!"); |
| 1076 | Quals.addAddressSpace(AddressSpace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1077 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1078 | return getExtQualType(TypeNode, Quals); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1081 | QualType ASTContext::getObjCGCQualType(QualType T, |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1082 | Qualifiers::GC GCAttr) { |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1083 | QualType CanT = getCanonicalType(T); |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1084 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1085 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1086 | |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1087 | if (T->isPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1088 | QualType Pointee = T->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 1089 | if (Pointee->isAnyPointerType()) { |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1090 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 1091 | return getPointerType(ResultType); |
| 1092 | } |
| 1093 | } |
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 | // If we are composing extended qualifiers together, merge together |
| 1096 | // into one ExtQuals node. |
| 1097 | QualifierCollector Quals; |
| 1098 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1099 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1100 | // If this type already has an ObjCGC specified, it cannot get |
| 1101 | // another one. |
| 1102 | assert(!Quals.hasObjCGCAttr() && |
| 1103 | "Type cannot have multiple ObjCGCs!"); |
| 1104 | Quals.addObjCGCAttr(GCAttr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1105 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1106 | return getExtQualType(TypeNode, Quals); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1107 | } |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1108 | |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1109 | static QualType getExtFunctionType(ASTContext& Context, QualType T, |
| 1110 | const FunctionType::ExtInfo &Info) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1111 | QualType ResultType; |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1112 | if (const PointerType *Pointer = T->getAs<PointerType>()) { |
| 1113 | QualType Pointee = Pointer->getPointeeType(); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1114 | ResultType = getExtFunctionType(Context, Pointee, Info); |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1115 | if (ResultType == Pointee) |
| 1116 | return T; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1117 | |
| 1118 | ResultType = Context.getPointerType(ResultType); |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1119 | } else if (const BlockPointerType *BlockPointer |
| 1120 | = T->getAs<BlockPointerType>()) { |
| 1121 | QualType Pointee = BlockPointer->getPointeeType(); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1122 | ResultType = getExtFunctionType(Context, Pointee, Info); |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1123 | if (ResultType == Pointee) |
| 1124 | return T; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1125 | |
| 1126 | ResultType = Context.getBlockPointerType(ResultType); |
| 1127 | } else if (const FunctionType *F = T->getAs<FunctionType>()) { |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1128 | if (F->getExtInfo() == Info) |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1129 | return T; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1130 | |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1131 | if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(F)) { |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1132 | ResultType = Context.getFunctionNoProtoType(FNPT->getResultType(), |
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 | } else { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1135 | const FunctionProtoType *FPT = cast<FunctionProtoType>(F); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1136 | ResultType |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1137 | = Context.getFunctionType(FPT->getResultType(), FPT->arg_type_begin(), |
| 1138 | FPT->getNumArgs(), FPT->isVariadic(), |
| 1139 | FPT->getTypeQuals(), |
| 1140 | FPT->hasExceptionSpec(), |
| 1141 | FPT->hasAnyExceptionSpec(), |
| 1142 | FPT->getNumExceptions(), |
| 1143 | FPT->exception_begin(), |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1144 | Info); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1145 | } |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1146 | } else |
| 1147 | return T; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1148 | |
| 1149 | return Context.getQualifiedType(ResultType, T.getLocalQualifiers()); |
| 1150 | } |
| 1151 | |
| 1152 | QualType ASTContext::getNoReturnType(QualType T, bool AddNoReturn) { |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 1153 | FunctionType::ExtInfo Info = getFunctionExtInfo(T); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1154 | return getExtFunctionType(*this, T, |
| 1155 | Info.withNoReturn(AddNoReturn)); |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | QualType ASTContext::getCallConvType(QualType T, CallingConv CallConv) { |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 1159 | FunctionType::ExtInfo Info = getFunctionExtInfo(T); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1160 | return getExtFunctionType(*this, T, |
| 1161 | Info.withCallingConv(CallConv)); |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 1164 | QualType ASTContext::getRegParmType(QualType T, unsigned RegParm) { |
| 1165 | FunctionType::ExtInfo Info = getFunctionExtInfo(T); |
| 1166 | return getExtFunctionType(*this, T, |
| 1167 | Info.withRegParm(RegParm)); |
| 1168 | } |
| 1169 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1170 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 1171 | /// number with the specified element type. |
| 1172 | QualType ASTContext::getComplexType(QualType T) { |
| 1173 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1174 | // structure. |
| 1175 | llvm::FoldingSetNodeID ID; |
| 1176 | ComplexType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1177 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1178 | void *InsertPos = 0; |
| 1179 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1180 | return QualType(CT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1181 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1182 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1183 | // so fill in the canonical type field. |
| 1184 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1185 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1186 | Canonical = getComplexType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1187 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1188 | // Get the new insert position for the node we care about. |
| 1189 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1190 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1191 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1192 | ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1193 | Types.push_back(New); |
| 1194 | ComplexTypes.InsertNode(New, InsertPos); |
| 1195 | return QualType(New, 0); |
| 1196 | } |
| 1197 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1198 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 1199 | /// the specified type. |
| 1200 | QualType ASTContext::getPointerType(QualType T) { |
| 1201 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1202 | // structure. |
| 1203 | llvm::FoldingSetNodeID ID; |
| 1204 | PointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1205 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1206 | void *InsertPos = 0; |
| 1207 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1208 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1209 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1210 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1211 | // so fill in the canonical type field. |
| 1212 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1213 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1214 | Canonical = getPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1215 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1216 | // Get the new insert position for the node we care about. |
| 1217 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1218 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1219 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1220 | PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1221 | Types.push_back(New); |
| 1222 | PointerTypes.InsertNode(New, InsertPos); |
| 1223 | return QualType(New, 0); |
| 1224 | } |
| 1225 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1226 | /// getBlockPointerType - Return the uniqued reference to the type for |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1227 | /// a pointer to the specified block. |
| 1228 | QualType ASTContext::getBlockPointerType(QualType T) { |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1229 | assert(T->isFunctionType() && "block of function types only"); |
| 1230 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1231 | // structure. |
| 1232 | llvm::FoldingSetNodeID ID; |
| 1233 | BlockPointerType::Profile(ID, 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 | void *InsertPos = 0; |
| 1236 | if (BlockPointerType *PT = |
| 1237 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1238 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1239 | |
| 1240 | // 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] | 1241 | // type either so fill in the canonical type field. |
| 1242 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1243 | if (!T.isCanonical()) { |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1244 | Canonical = getBlockPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1245 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1246 | // Get the new insert position for the node we care about. |
| 1247 | BlockPointerType *NewIP = |
| 1248 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1249 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1250 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1251 | BlockPointerType *New |
| 1252 | = new (*this, TypeAlignment) BlockPointerType(T, Canonical); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1253 | Types.push_back(New); |
| 1254 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 1255 | return QualType(New, 0); |
| 1256 | } |
| 1257 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1258 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 1259 | /// lvalue reference to the specified type. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1260 | QualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1261 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1262 | // structure. |
| 1263 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1264 | ReferenceType::Profile(ID, T, SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1265 | |
| 1266 | void *InsertPos = 0; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1267 | if (LValueReferenceType *RT = |
| 1268 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1269 | return QualType(RT, 0); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1270 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1271 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1272 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1273 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1274 | // either, so fill in the canonical type field. |
| 1275 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1276 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
| 1277 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1278 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1279 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1280 | // Get the new insert position for the node we care about. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1281 | LValueReferenceType *NewIP = |
| 1282 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1283 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1284 | } |
| 1285 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1286 | LValueReferenceType *New |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1287 | = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, |
| 1288 | SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1289 | Types.push_back(New); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1290 | LValueReferenceTypes.InsertNode(New, InsertPos); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1291 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1292 | return QualType(New, 0); |
| 1293 | } |
| 1294 | |
| 1295 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 1296 | /// rvalue reference to the specified type. |
| 1297 | QualType ASTContext::getRValueReferenceType(QualType T) { |
| 1298 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1299 | // structure. |
| 1300 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1301 | ReferenceType::Profile(ID, T, false); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1302 | |
| 1303 | void *InsertPos = 0; |
| 1304 | if (RValueReferenceType *RT = |
| 1305 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1306 | return QualType(RT, 0); |
| 1307 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1308 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1309 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1310 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1311 | // either, so fill in the canonical type field. |
| 1312 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1313 | if (InnerRef || !T.isCanonical()) { |
| 1314 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1315 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1316 | |
| 1317 | // Get the new insert position for the node we care about. |
| 1318 | RValueReferenceType *NewIP = |
| 1319 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1320 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
| 1321 | } |
| 1322 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1323 | RValueReferenceType *New |
| 1324 | = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1325 | Types.push_back(New); |
| 1326 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1327 | return QualType(New, 0); |
| 1328 | } |
| 1329 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1330 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 1331 | /// member pointer to the specified type, in the specified class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1332 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1333 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1334 | // structure. |
| 1335 | llvm::FoldingSetNodeID ID; |
| 1336 | MemberPointerType::Profile(ID, T, Cls); |
| 1337 | |
| 1338 | void *InsertPos = 0; |
| 1339 | if (MemberPointerType *PT = |
| 1340 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1341 | return QualType(PT, 0); |
| 1342 | |
| 1343 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 1344 | // type either, so fill in the canonical type field. |
| 1345 | QualType Canonical; |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1346 | if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1347 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 1348 | |
| 1349 | // Get the new insert position for the node we care about. |
| 1350 | MemberPointerType *NewIP = |
| 1351 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1352 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
| 1353 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1354 | MemberPointerType *New |
| 1355 | = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1356 | Types.push_back(New); |
| 1357 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 1358 | return QualType(New, 0); |
| 1359 | } |
| 1360 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1361 | /// getConstantArrayType - Return the unique reference to the type for an |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1362 | /// array of the specified element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1363 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1364 | const llvm::APInt &ArySizeIn, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1365 | ArrayType::ArraySizeModifier ASM, |
| 1366 | unsigned EltTypeQuals) { |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1367 | assert((EltTy->isDependentType() || |
| 1368 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && |
Eli Friedman | 587cbdf | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1369 | "Constant array of VLAs is illegal!"); |
| 1370 | |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1371 | // Convert the array size into a canonical width matching the pointer size for |
| 1372 | // the target. |
| 1373 | llvm::APInt ArySize(ArySizeIn); |
| 1374 | ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1375 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1376 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 0be2ef2 | 2009-02-19 17:31:02 +0000 | [diff] [blame] | 1377 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1378 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1379 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1380 | if (ConstantArrayType *ATP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1381 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1382 | return QualType(ATP, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1383 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1384 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1385 | // so fill in the canonical type field. |
| 1386 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1387 | if (!EltTy.isCanonical()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1388 | Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1389 | ASM, EltTypeQuals); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1390 | // Get the new insert position for the node we care about. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1391 | ConstantArrayType *NewIP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1392 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1393 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1394 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1395 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1396 | ConstantArrayType *New = new(*this,TypeAlignment) |
| 1397 | ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1398 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1399 | Types.push_back(New); |
| 1400 | return QualType(New, 0); |
| 1401 | } |
| 1402 | |
Steve Naroff | bdbf7b0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 1403 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 1404 | /// variable array of the specified element type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1405 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 1406 | Expr *NumElts, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1407 | ArrayType::ArraySizeModifier ASM, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1408 | unsigned EltTypeQuals, |
| 1409 | SourceRange Brackets) { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1410 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 1411 | // that have an expression provided for their size. |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 1412 | QualType CanonType; |
| 1413 | |
| 1414 | if (!EltTy.isCanonical()) { |
| 1415 | if (NumElts) |
| 1416 | NumElts->Retain(); |
| 1417 | CanonType = getVariableArrayType(getCanonicalType(EltTy), NumElts, ASM, |
| 1418 | EltTypeQuals, Brackets); |
| 1419 | } |
| 1420 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1421 | VariableArrayType *New = new(*this, TypeAlignment) |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 1422 | VariableArrayType(EltTy, CanonType, NumElts, ASM, EltTypeQuals, Brackets); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1423 | |
| 1424 | VariableArrayTypes.push_back(New); |
| 1425 | Types.push_back(New); |
| 1426 | return QualType(New, 0); |
| 1427 | } |
| 1428 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1429 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 1430 | /// the type for a dependently-sized array of the specified element |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1431 | /// type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1432 | QualType ASTContext::getDependentSizedArrayType(QualType EltTy, |
| 1433 | Expr *NumElts, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1434 | ArrayType::ArraySizeModifier ASM, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1435 | unsigned EltTypeQuals, |
| 1436 | SourceRange Brackets) { |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1437 | assert((!NumElts || NumElts->isTypeDependent() || |
| 1438 | NumElts->isValueDependent()) && |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1439 | "Size must be type- or value-dependent!"); |
| 1440 | |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1441 | void *InsertPos = 0; |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1442 | DependentSizedArrayType *Canon = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1443 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1444 | |
| 1445 | if (NumElts) { |
| 1446 | // Dependently-sized array types that do not have a specified |
| 1447 | // number of elements will have their sizes deduced from an |
| 1448 | // initializer. |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1449 | DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM, |
| 1450 | EltTypeQuals, NumElts); |
| 1451 | |
| 1452 | Canon = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1453 | } |
| 1454 | |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1455 | DependentSizedArrayType *New; |
| 1456 | if (Canon) { |
| 1457 | // We already have a canonical version of this array type; use it as |
| 1458 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1459 | New = new (*this, TypeAlignment) |
| 1460 | DependentSizedArrayType(*this, EltTy, QualType(Canon, 0), |
| 1461 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1462 | } else { |
| 1463 | QualType CanonEltTy = getCanonicalType(EltTy); |
| 1464 | if (CanonEltTy == EltTy) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1465 | New = new (*this, TypeAlignment) |
| 1466 | DependentSizedArrayType(*this, EltTy, QualType(), |
| 1467 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1468 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1469 | if (NumElts) { |
| 1470 | DependentSizedArrayType *CanonCheck |
| 1471 | = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1472 | assert(!CanonCheck && "Dependent-sized canonical array type broken"); |
| 1473 | (void)CanonCheck; |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1474 | DependentSizedArrayTypes.InsertNode(New, InsertPos); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1475 | } |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1476 | } else { |
| 1477 | QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts, |
| 1478 | ASM, EltTypeQuals, |
| 1479 | SourceRange()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1480 | New = new (*this, TypeAlignment) |
| 1481 | DependentSizedArrayType(*this, EltTy, Canon, |
| 1482 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1483 | } |
| 1484 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1485 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1486 | Types.push_back(New); |
| 1487 | return QualType(New, 0); |
| 1488 | } |
| 1489 | |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1490 | QualType ASTContext::getIncompleteArrayType(QualType EltTy, |
| 1491 | ArrayType::ArraySizeModifier ASM, |
| 1492 | unsigned EltTypeQuals) { |
| 1493 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 0be2ef2 | 2009-02-19 17:31:02 +0000 | [diff] [blame] | 1494 | IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1495 | |
| 1496 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1497 | if (IncompleteArrayType *ATP = |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1498 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1499 | return QualType(ATP, 0); |
| 1500 | |
| 1501 | // If the element type isn't canonical, this won't be a canonical type |
| 1502 | // either, so fill in the canonical type field. |
| 1503 | QualType Canonical; |
| 1504 | |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1505 | if (!EltTy.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1506 | Canonical = getIncompleteArrayType(getCanonicalType(EltTy), |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1507 | ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1508 | |
| 1509 | // Get the new insert position for the node we care about. |
| 1510 | IncompleteArrayType *NewIP = |
| 1511 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1512 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1513 | } |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1514 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1515 | IncompleteArrayType *New = new (*this, TypeAlignment) |
| 1516 | IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1517 | |
| 1518 | IncompleteArrayTypes.InsertNode(New, InsertPos); |
| 1519 | Types.push_back(New); |
| 1520 | return QualType(New, 0); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1523 | /// getVectorType - Return the unique reference to a vector type of |
| 1524 | /// 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] | 1525 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts, |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1526 | VectorType::AltiVecSpecific AltiVecSpec) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1527 | BuiltinType *baseType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1528 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1529 | baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1530 | assert(baseType != 0 && "getVectorType(): Expecting a built-in type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1531 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1532 | // Check if we've already instantiated a vector of this type. |
| 1533 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1534 | VectorType::Profile(ID, vecType, NumElts, Type::Vector, AltiVecSpec); |
| 1535 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1536 | void *InsertPos = 0; |
| 1537 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1538 | return QualType(VTP, 0); |
| 1539 | |
| 1540 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1541 | // so fill in the canonical type field. |
| 1542 | QualType Canonical; |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1543 | if (!vecType.isCanonical()) { |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1544 | Canonical = getVectorType(getCanonicalType(vecType), NumElts, |
| 1545 | VectorType::NotAltiVec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1546 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1547 | // Get the new insert position for the node we care about. |
| 1548 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1549 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1550 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1551 | VectorType *New = new (*this, TypeAlignment) |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1552 | VectorType(vecType, NumElts, Canonical, AltiVecSpec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1553 | VectorTypes.InsertNode(New, InsertPos); |
| 1554 | Types.push_back(New); |
| 1555 | return QualType(New, 0); |
| 1556 | } |
| 1557 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1558 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1559 | /// 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] | 1560 | QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) { |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1561 | BuiltinType *baseType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1562 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1563 | baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1564 | assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1565 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1566 | // Check if we've already instantiated a vector of this type. |
| 1567 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1568 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, |
| 1569 | VectorType::NotAltiVec); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1570 | void *InsertPos = 0; |
| 1571 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1572 | return QualType(VTP, 0); |
| 1573 | |
| 1574 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1575 | // so fill in the canonical type field. |
| 1576 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1577 | if (!vecType.isCanonical()) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1578 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1579 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1580 | // Get the new insert position for the node we care about. |
| 1581 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1582 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1583 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1584 | ExtVectorType *New = new (*this, TypeAlignment) |
| 1585 | ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1586 | VectorTypes.InsertNode(New, InsertPos); |
| 1587 | Types.push_back(New); |
| 1588 | return QualType(New, 0); |
| 1589 | } |
| 1590 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1591 | QualType ASTContext::getDependentSizedExtVectorType(QualType vecType, |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1592 | Expr *SizeExpr, |
| 1593 | SourceLocation AttrLoc) { |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1594 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1595 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1596 | SizeExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1597 | |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1598 | void *InsertPos = 0; |
| 1599 | DependentSizedExtVectorType *Canon |
| 1600 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1601 | DependentSizedExtVectorType *New; |
| 1602 | if (Canon) { |
| 1603 | // We already have a canonical version of this array type; use it as |
| 1604 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1605 | New = new (*this, TypeAlignment) |
| 1606 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), |
| 1607 | SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1608 | } else { |
| 1609 | QualType CanonVecTy = getCanonicalType(vecType); |
| 1610 | if (CanonVecTy == vecType) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1611 | New = new (*this, TypeAlignment) |
| 1612 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, |
| 1613 | AttrLoc); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1614 | |
| 1615 | DependentSizedExtVectorType *CanonCheck |
| 1616 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1617 | assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken"); |
| 1618 | (void)CanonCheck; |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1619 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); |
| 1620 | } else { |
| 1621 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 1622 | SourceLocation()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1623 | New = new (*this, TypeAlignment) |
| 1624 | DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1625 | } |
| 1626 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1627 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1628 | Types.push_back(New); |
| 1629 | return QualType(New, 0); |
| 1630 | } |
| 1631 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1632 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1633 | /// |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1634 | QualType ASTContext::getFunctionNoProtoType(QualType ResultTy, |
| 1635 | const FunctionType::ExtInfo &Info) { |
| 1636 | const CallingConv CallConv = Info.getCC(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1637 | // Unique functions, to guarantee there is only one function of a particular |
| 1638 | // structure. |
| 1639 | llvm::FoldingSetNodeID ID; |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1640 | FunctionNoProtoType::Profile(ID, ResultTy, Info); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1641 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1642 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1643 | if (FunctionNoProtoType *FT = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1644 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1645 | return QualType(FT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1646 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1647 | QualType Canonical; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1648 | if (!ResultTy.isCanonical() || |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1649 | getCanonicalCallConv(CallConv) != CallConv) { |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1650 | Canonical = |
| 1651 | getFunctionNoProtoType(getCanonicalType(ResultTy), |
| 1652 | Info.withCallingConv(getCanonicalCallConv(CallConv))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1653 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1654 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1655 | FunctionNoProtoType *NewIP = |
| 1656 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1657 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1658 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1659 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1660 | FunctionNoProtoType *New = new (*this, TypeAlignment) |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1661 | FunctionNoProtoType(ResultTy, Canonical, Info); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1662 | Types.push_back(New); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1663 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1664 | return QualType(New, 0); |
| 1665 | } |
| 1666 | |
| 1667 | /// getFunctionType - Return a normal function type with a typed argument |
| 1668 | /// list. isVariadic indicates whether the argument list includes '...'. |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 1669 | QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray, |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1670 | unsigned NumArgs, bool isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1671 | unsigned TypeQuals, bool hasExceptionSpec, |
| 1672 | bool hasAnyExceptionSpec, unsigned NumExs, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1673 | const QualType *ExArray, |
| 1674 | const FunctionType::ExtInfo &Info) { |
| 1675 | const CallingConv CallConv= Info.getCC(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1676 | // Unique functions, to guarantee there is only one function of a particular |
| 1677 | // structure. |
| 1678 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1679 | FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1680 | TypeQuals, hasExceptionSpec, hasAnyExceptionSpec, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1681 | NumExs, ExArray, Info); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1682 | |
| 1683 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1684 | if (FunctionProtoType *FTP = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1685 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1686 | return QualType(FTP, 0); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1687 | |
| 1688 | // Determine whether the type being created is already canonical or not. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1689 | bool isCanonical = !hasExceptionSpec && ResultTy.isCanonical(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1690 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1691 | if (!ArgArray[i].isCanonicalAsParam()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1692 | isCanonical = false; |
| 1693 | |
| 1694 | // If this type isn't canonical, get the canonical version of it. |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1695 | // The exception spec is not part of the canonical type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1696 | QualType Canonical; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1697 | if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1698 | llvm::SmallVector<QualType, 16> CanonicalArgs; |
| 1699 | CanonicalArgs.reserve(NumArgs); |
| 1700 | for (unsigned i = 0; i != NumArgs; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1701 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1702 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1703 | Canonical = getFunctionType(getCanonicalType(ResultTy), |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1704 | CanonicalArgs.data(), NumArgs, |
Douglas Gregor | 47259d9 | 2009-08-05 19:03:35 +0000 | [diff] [blame] | 1705 | isVariadic, TypeQuals, false, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1706 | false, 0, 0, |
| 1707 | Info.withCallingConv(getCanonicalCallConv(CallConv))); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1708 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1709 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1710 | FunctionProtoType *NewIP = |
| 1711 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1712 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1713 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1714 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1715 | // FunctionProtoType objects are allocated with extra bytes after them |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1716 | // for two variable size arrays (for parameter and exception types) at the |
| 1717 | // end of them. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1718 | FunctionProtoType *FTP = |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1719 | (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) + |
| 1720 | NumArgs*sizeof(QualType) + |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1721 | NumExs*sizeof(QualType), TypeAlignment); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1722 | new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1723 | TypeQuals, hasExceptionSpec, hasAnyExceptionSpec, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1724 | ExArray, NumExs, Canonical, Info); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1725 | Types.push_back(FTP); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1726 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1727 | return QualType(FTP, 0); |
| 1728 | } |
| 1729 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1730 | #ifndef NDEBUG |
| 1731 | static bool NeedsInjectedClassNameType(const RecordDecl *D) { |
| 1732 | if (!isa<CXXRecordDecl>(D)) return false; |
| 1733 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 1734 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) |
| 1735 | return true; |
| 1736 | if (RD->getDescribedClassTemplate() && |
| 1737 | !isa<ClassTemplateSpecializationDecl>(RD)) |
| 1738 | return true; |
| 1739 | return false; |
| 1740 | } |
| 1741 | #endif |
| 1742 | |
| 1743 | /// getInjectedClassNameType - Return the unique reference to the |
| 1744 | /// injected class name type for the specified templated declaration. |
| 1745 | QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl, |
| 1746 | QualType TST) { |
| 1747 | assert(NeedsInjectedClassNameType(Decl)); |
| 1748 | if (Decl->TypeForDecl) { |
| 1749 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
Argyrios Kyrtzidis | 37ffed3 | 2010-07-02 11:55:32 +0000 | [diff] [blame] | 1750 | } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDeclaration()) { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1751 | assert(PrevDecl->TypeForDecl && "previous declaration has no type"); |
| 1752 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 1753 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
| 1754 | } else { |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1755 | Decl->TypeForDecl = |
| 1756 | new (*this, TypeAlignment) InjectedClassNameType(Decl, TST); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1757 | Types.push_back(Decl->TypeForDecl); |
| 1758 | } |
| 1759 | return QualType(Decl->TypeForDecl, 0); |
| 1760 | } |
| 1761 | |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1762 | /// getTypeDeclType - Return the unique reference to the type for the |
| 1763 | /// specified type declaration. |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1764 | QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) { |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 1765 | assert(Decl && "Passed null for Decl param"); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1766 | assert(!Decl->TypeForDecl && "TypeForDecl present in slow case"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1767 | |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1768 | if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1769 | return getTypedefType(Typedef); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1770 | |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1771 | assert(!isa<TemplateTypeParmDecl>(Decl) && |
| 1772 | "Template type parameter types are always available."); |
| 1773 | |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1774 | if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1775 | assert(!Record->getPreviousDeclaration() && |
| 1776 | "struct/union has previous declaration"); |
| 1777 | assert(!NeedsInjectedClassNameType(Record)); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 1778 | return getRecordType(Record); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1779 | } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1780 | assert(!Enum->getPreviousDeclaration() && |
| 1781 | "enum has previous declaration"); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 1782 | return getEnumType(Enum); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1783 | } else if (const UnresolvedUsingTypenameDecl *Using = |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1784 | dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) { |
| 1785 | Decl->TypeForDecl = new (*this, TypeAlignment) UnresolvedUsingType(Using); |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 1786 | } else |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1787 | llvm_unreachable("TypeDecl without a type?"); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1788 | |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1789 | Types.push_back(Decl->TypeForDecl); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1790 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1791 | } |
| 1792 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1793 | /// getTypedefType - Return the unique reference to the type for the |
| 1794 | /// specified typename decl. |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 1795 | QualType |
| 1796 | ASTContext::getTypedefType(const TypedefDecl *Decl, QualType Canonical) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1797 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1798 | |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 1799 | if (Canonical.isNull()) |
| 1800 | Canonical = getCanonicalType(Decl->getUnderlyingType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1801 | Decl->TypeForDecl = new(*this, TypeAlignment) |
| 1802 | TypedefType(Type::Typedef, Decl, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1803 | Types.push_back(Decl->TypeForDecl); |
| 1804 | return QualType(Decl->TypeForDecl, 0); |
| 1805 | } |
| 1806 | |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 1807 | QualType ASTContext::getRecordType(const RecordDecl *Decl) { |
| 1808 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 1809 | |
| 1810 | if (const RecordDecl *PrevDecl = Decl->getPreviousDeclaration()) |
| 1811 | if (PrevDecl->TypeForDecl) |
| 1812 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 1813 | |
| 1814 | Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Decl); |
| 1815 | Types.push_back(Decl->TypeForDecl); |
| 1816 | return QualType(Decl->TypeForDecl, 0); |
| 1817 | } |
| 1818 | |
| 1819 | QualType ASTContext::getEnumType(const EnumDecl *Decl) { |
| 1820 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 1821 | |
| 1822 | if (const EnumDecl *PrevDecl = Decl->getPreviousDeclaration()) |
| 1823 | if (PrevDecl->TypeForDecl) |
| 1824 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 1825 | |
| 1826 | Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Decl); |
| 1827 | Types.push_back(Decl->TypeForDecl); |
| 1828 | return QualType(Decl->TypeForDecl, 0); |
| 1829 | } |
| 1830 | |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1831 | /// \brief Retrieve a substitution-result type. |
| 1832 | QualType |
| 1833 | ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, |
| 1834 | QualType Replacement) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1835 | assert(Replacement.isCanonical() |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1836 | && "replacement types must always be canonical"); |
| 1837 | |
| 1838 | llvm::FoldingSetNodeID ID; |
| 1839 | SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); |
| 1840 | void *InsertPos = 0; |
| 1841 | SubstTemplateTypeParmType *SubstParm |
| 1842 | = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1843 | |
| 1844 | if (!SubstParm) { |
| 1845 | SubstParm = new (*this, TypeAlignment) |
| 1846 | SubstTemplateTypeParmType(Parm, Replacement); |
| 1847 | Types.push_back(SubstParm); |
| 1848 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 1849 | } |
| 1850 | |
| 1851 | return QualType(SubstParm, 0); |
| 1852 | } |
| 1853 | |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1854 | /// \brief Retrieve the template type parameter type for a template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1855 | /// parameter or parameter pack with the given depth, index, and (optionally) |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1856 | /// name. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1857 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1858 | bool ParameterPack, |
Douglas Gregor | efed5c8 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 1859 | IdentifierInfo *Name) { |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1860 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | efed5c8 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 1861 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1862 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1863 | TemplateTypeParmType *TypeParm |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1864 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1865 | |
| 1866 | if (TypeParm) |
| 1867 | return QualType(TypeParm, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1868 | |
Douglas Gregor | efed5c8 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 1869 | if (Name) { |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1870 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | efed5c8 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 1871 | TypeParm = new (*this, TypeAlignment) |
| 1872 | TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1873 | |
| 1874 | TemplateTypeParmType *TypeCheck |
| 1875 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1876 | assert(!TypeCheck && "Template type parameter canonical type broken"); |
| 1877 | (void)TypeCheck; |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1878 | } else |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1879 | TypeParm = new (*this, TypeAlignment) |
| 1880 | TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1881 | |
| 1882 | Types.push_back(TypeParm); |
| 1883 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 1884 | |
| 1885 | return QualType(TypeParm, 0); |
| 1886 | } |
| 1887 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1888 | TypeSourceInfo * |
| 1889 | ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name, |
| 1890 | SourceLocation NameLoc, |
| 1891 | const TemplateArgumentListInfo &Args, |
| 1892 | QualType CanonType) { |
| 1893 | QualType TST = getTemplateSpecializationType(Name, Args, CanonType); |
| 1894 | |
| 1895 | TypeSourceInfo *DI = CreateTypeSourceInfo(TST); |
| 1896 | TemplateSpecializationTypeLoc TL |
| 1897 | = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc()); |
| 1898 | TL.setTemplateNameLoc(NameLoc); |
| 1899 | TL.setLAngleLoc(Args.getLAngleLoc()); |
| 1900 | TL.setRAngleLoc(Args.getRAngleLoc()); |
| 1901 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 1902 | TL.setArgLocInfo(i, Args[i].getLocInfo()); |
| 1903 | return DI; |
| 1904 | } |
| 1905 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1906 | QualType |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1907 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1908 | const TemplateArgumentListInfo &Args, |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 1909 | QualType Canon) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1910 | unsigned NumArgs = Args.size(); |
| 1911 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1912 | llvm::SmallVector<TemplateArgument, 4> ArgVec; |
| 1913 | ArgVec.reserve(NumArgs); |
| 1914 | for (unsigned i = 0; i != NumArgs; ++i) |
| 1915 | ArgVec.push_back(Args[i].getArgument()); |
| 1916 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1917 | return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 1918 | Canon); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1919 | } |
| 1920 | |
| 1921 | QualType |
| 1922 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1923 | const TemplateArgument *Args, |
| 1924 | unsigned NumArgs, |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 1925 | QualType Canon) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1926 | if (!Canon.isNull()) |
| 1927 | Canon = getCanonicalType(Canon); |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 1928 | else |
| 1929 | Canon = getCanonicalTemplateSpecializationType(Template, Args, NumArgs); |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 1930 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1931 | // Allocate the (non-canonical) template specialization type, but don't |
| 1932 | // try to unique it: these types typically have location information that |
| 1933 | // we don't unique and don't want to lose. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1934 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1935 | sizeof(TemplateArgument) * NumArgs), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1936 | TypeAlignment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1937 | TemplateSpecializationType *Spec |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 1938 | = new (Mem) TemplateSpecializationType(Template, |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1939 | Args, NumArgs, |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 1940 | Canon); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1941 | |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1942 | Types.push_back(Spec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1943 | return QualType(Spec, 0); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1944 | } |
| 1945 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1946 | QualType |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 1947 | ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template, |
| 1948 | const TemplateArgument *Args, |
| 1949 | unsigned NumArgs) { |
| 1950 | // Build the canonical template specialization type. |
| 1951 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); |
| 1952 | llvm::SmallVector<TemplateArgument, 4> CanonArgs; |
| 1953 | CanonArgs.reserve(NumArgs); |
| 1954 | for (unsigned I = 0; I != NumArgs; ++I) |
| 1955 | CanonArgs.push_back(getCanonicalTemplateArgument(Args[I])); |
| 1956 | |
| 1957 | // Determine whether this canonical template specialization type already |
| 1958 | // exists. |
| 1959 | llvm::FoldingSetNodeID ID; |
| 1960 | TemplateSpecializationType::Profile(ID, CanonTemplate, |
| 1961 | CanonArgs.data(), NumArgs, *this); |
| 1962 | |
| 1963 | void *InsertPos = 0; |
| 1964 | TemplateSpecializationType *Spec |
| 1965 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1966 | |
| 1967 | if (!Spec) { |
| 1968 | // Allocate a new canonical template specialization type. |
| 1969 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
| 1970 | sizeof(TemplateArgument) * NumArgs), |
| 1971 | TypeAlignment); |
| 1972 | Spec = new (Mem) TemplateSpecializationType(CanonTemplate, |
| 1973 | CanonArgs.data(), NumArgs, |
| 1974 | QualType()); |
| 1975 | Types.push_back(Spec); |
| 1976 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
| 1977 | } |
| 1978 | |
| 1979 | assert(Spec->isDependentType() && |
| 1980 | "Non-dependent template-id type must have a canonical type"); |
| 1981 | return QualType(Spec, 0); |
| 1982 | } |
| 1983 | |
| 1984 | QualType |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1985 | ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword, |
| 1986 | NestedNameSpecifier *NNS, |
| 1987 | QualType NamedType) { |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1988 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1989 | ElaboratedType::Profile(ID, Keyword, NNS, NamedType); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1990 | |
| 1991 | void *InsertPos = 0; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1992 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1993 | if (T) |
| 1994 | return QualType(T, 0); |
| 1995 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1996 | QualType Canon = NamedType; |
| 1997 | if (!Canon.isCanonical()) { |
| 1998 | Canon = getCanonicalType(NamedType); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1999 | ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2000 | assert(!CheckT && "Elaborated canonical type broken"); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2001 | (void)CheckT; |
| 2002 | } |
| 2003 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2004 | T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2005 | Types.push_back(T); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2006 | ElaboratedTypes.InsertNode(T, InsertPos); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2007 | return QualType(T, 0); |
| 2008 | } |
| 2009 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2010 | QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, |
| 2011 | NestedNameSpecifier *NNS, |
| 2012 | const IdentifierInfo *Name, |
| 2013 | QualType Canon) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2014 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 2015 | |
| 2016 | if (Canon.isNull()) { |
| 2017 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2018 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 2019 | if (Keyword == ETK_None) |
| 2020 | CanonKeyword = ETK_Typename; |
| 2021 | |
| 2022 | if (CanonNNS != NNS || CanonKeyword != Keyword) |
| 2023 | Canon = getDependentNameType(CanonKeyword, CanonNNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2024 | } |
| 2025 | |
| 2026 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2027 | DependentNameType::Profile(ID, Keyword, NNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2028 | |
| 2029 | void *InsertPos = 0; |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2030 | DependentNameType *T |
| 2031 | = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2032 | if (T) |
| 2033 | return QualType(T, 0); |
| 2034 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2035 | T = new (*this) DependentNameType(Keyword, NNS, Name, Canon); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2036 | Types.push_back(T); |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2037 | DependentNameTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2038 | return QualType(T, 0); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2039 | } |
| 2040 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2041 | QualType |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2042 | ASTContext::getDependentTemplateSpecializationType( |
| 2043 | ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2044 | NestedNameSpecifier *NNS, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2045 | const IdentifierInfo *Name, |
| 2046 | const TemplateArgumentListInfo &Args) { |
| 2047 | // TODO: avoid this copy |
| 2048 | llvm::SmallVector<TemplateArgument, 16> ArgCopy; |
| 2049 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
| 2050 | ArgCopy.push_back(Args[I].getArgument()); |
| 2051 | return getDependentTemplateSpecializationType(Keyword, NNS, Name, |
| 2052 | ArgCopy.size(), |
| 2053 | ArgCopy.data()); |
| 2054 | } |
| 2055 | |
| 2056 | QualType |
| 2057 | ASTContext::getDependentTemplateSpecializationType( |
| 2058 | ElaboratedTypeKeyword Keyword, |
| 2059 | NestedNameSpecifier *NNS, |
| 2060 | const IdentifierInfo *Name, |
| 2061 | unsigned NumArgs, |
| 2062 | const TemplateArgument *Args) { |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2063 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 2064 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2065 | llvm::FoldingSetNodeID ID; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2066 | DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS, |
| 2067 | Name, NumArgs, Args); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2068 | |
| 2069 | void *InsertPos = 0; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2070 | DependentTemplateSpecializationType *T |
| 2071 | = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2072 | if (T) |
| 2073 | return QualType(T, 0); |
| 2074 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2075 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2076 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2077 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 2078 | if (Keyword == ETK_None) CanonKeyword = ETK_Typename; |
| 2079 | |
| 2080 | bool AnyNonCanonArgs = false; |
| 2081 | llvm::SmallVector<TemplateArgument, 16> CanonArgs(NumArgs); |
| 2082 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 2083 | CanonArgs[I] = getCanonicalTemplateArgument(Args[I]); |
| 2084 | if (!CanonArgs[I].structurallyEquals(Args[I])) |
| 2085 | AnyNonCanonArgs = true; |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2086 | } |
| 2087 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2088 | QualType Canon; |
| 2089 | if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) { |
| 2090 | Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS, |
| 2091 | Name, NumArgs, |
| 2092 | CanonArgs.data()); |
| 2093 | |
| 2094 | // Find the insert position again. |
| 2095 | DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2096 | } |
| 2097 | |
| 2098 | void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) + |
| 2099 | sizeof(TemplateArgument) * NumArgs), |
| 2100 | TypeAlignment); |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 2101 | T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2102 | Name, NumArgs, Args, Canon); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2103 | Types.push_back(T); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2104 | DependentTemplateSpecializationTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2105 | return QualType(T, 0); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2106 | } |
| 2107 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2108 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 2109 | /// alphabetically. |
| 2110 | static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, |
| 2111 | const ObjCProtocolDecl *RHS) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2112 | return LHS->getDeclName() < RHS->getDeclName(); |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2113 | } |
| 2114 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2115 | static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols, |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2116 | unsigned NumProtocols) { |
| 2117 | if (NumProtocols == 0) return true; |
| 2118 | |
| 2119 | for (unsigned i = 1; i != NumProtocols; ++i) |
| 2120 | if (!CmpProtocolNames(Protocols[i-1], Protocols[i])) |
| 2121 | return false; |
| 2122 | return true; |
| 2123 | } |
| 2124 | |
| 2125 | static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols, |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2126 | unsigned &NumProtocols) { |
| 2127 | ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2128 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2129 | // Sort protocols, keyed by name. |
| 2130 | std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); |
| 2131 | |
| 2132 | // Remove duplicates. |
| 2133 | ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); |
| 2134 | NumProtocols = ProtocolsEnd-Protocols; |
| 2135 | } |
| 2136 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2137 | QualType ASTContext::getObjCObjectType(QualType BaseType, |
| 2138 | ObjCProtocolDecl * const *Protocols, |
| 2139 | unsigned NumProtocols) { |
| 2140 | // If the base type is an interface and there aren't any protocols |
| 2141 | // to add, then the interface type will do just fine. |
| 2142 | if (!NumProtocols && isa<ObjCInterfaceType>(BaseType)) |
| 2143 | return BaseType; |
| 2144 | |
| 2145 | // Look in the folding set for an existing type. |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2146 | llvm::FoldingSetNodeID ID; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2147 | ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2148 | void *InsertPos = 0; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2149 | if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2150 | return QualType(QT, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2151 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2152 | // Build the canonical type, which has the canonical base type and |
| 2153 | // a sorted-and-uniqued list of protocols. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2154 | QualType Canonical; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2155 | bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols); |
| 2156 | if (!ProtocolsSorted || !BaseType.isCanonical()) { |
| 2157 | if (!ProtocolsSorted) { |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 2158 | llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols, |
| 2159 | Protocols + NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2160 | unsigned UniqueCount = NumProtocols; |
| 2161 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2162 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2163 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 2164 | &Sorted[0], UniqueCount); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2165 | } else { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2166 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 2167 | Protocols, NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2168 | } |
| 2169 | |
| 2170 | // Regenerate InsertPos. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2171 | ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2172 | } |
| 2173 | |
| 2174 | unsigned Size = sizeof(ObjCObjectTypeImpl); |
| 2175 | Size += NumProtocols * sizeof(ObjCProtocolDecl *); |
| 2176 | void *Mem = Allocate(Size, TypeAlignment); |
| 2177 | ObjCObjectTypeImpl *T = |
| 2178 | new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols); |
| 2179 | |
| 2180 | Types.push_back(T); |
| 2181 | ObjCObjectTypes.InsertNode(T, InsertPos); |
| 2182 | return QualType(T, 0); |
| 2183 | } |
| 2184 | |
| 2185 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 2186 | /// the given object type. |
| 2187 | QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) { |
| 2188 | llvm::FoldingSetNodeID ID; |
| 2189 | ObjCObjectPointerType::Profile(ID, ObjectT); |
| 2190 | |
| 2191 | void *InsertPos = 0; |
| 2192 | if (ObjCObjectPointerType *QT = |
| 2193 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2194 | return QualType(QT, 0); |
| 2195 | |
| 2196 | // Find the canonical object type. |
| 2197 | QualType Canonical; |
| 2198 | if (!ObjectT.isCanonical()) { |
| 2199 | Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT)); |
| 2200 | |
| 2201 | // Regenerate InsertPos. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2202 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2203 | } |
| 2204 | |
Douglas Gregor | fd6a088 | 2010-02-08 22:59:26 +0000 | [diff] [blame] | 2205 | // No match. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2206 | void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment); |
| 2207 | ObjCObjectPointerType *QType = |
| 2208 | new (Mem) ObjCObjectPointerType(Canonical, ObjectT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2209 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2210 | Types.push_back(QType); |
| 2211 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2212 | return QualType(QType, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2213 | } |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2214 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2215 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 2216 | /// specified ObjC interface decl. The list of protocols is optional. |
| 2217 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) { |
| 2218 | if (Decl->TypeForDecl) |
| 2219 | return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2220 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2221 | // FIXME: redeclarations? |
| 2222 | void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment); |
| 2223 | ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl); |
| 2224 | Decl->TypeForDecl = T; |
| 2225 | Types.push_back(T); |
| 2226 | return QualType(T, 0); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2227 | } |
| 2228 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2229 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 2230 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2231 | /// multiple declarations that refer to "typeof(x)" all contain different |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2232 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2233 | /// on canonical type's (which are always unique). |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2234 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2235 | TypeOfExprType *toe; |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2236 | if (tofExpr->isTypeDependent()) { |
| 2237 | llvm::FoldingSetNodeID ID; |
| 2238 | DependentTypeOfExprType::Profile(ID, *this, tofExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2239 | |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2240 | void *InsertPos = 0; |
| 2241 | DependentTypeOfExprType *Canon |
| 2242 | = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2243 | if (Canon) { |
| 2244 | // We already have a "canonical" version of an identical, dependent |
| 2245 | // typeof(expr) type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2246 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2247 | QualType((TypeOfExprType*)Canon, 0)); |
| 2248 | } |
| 2249 | else { |
| 2250 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2251 | Canon |
| 2252 | = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2253 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); |
| 2254 | toe = Canon; |
| 2255 | } |
| 2256 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2257 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2258 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2259 | } |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2260 | Types.push_back(toe); |
| 2261 | return QualType(toe, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2262 | } |
| 2263 | |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2264 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 2265 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 2266 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2267 | /// an issue. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2268 | /// on canonical type's (which are always unique). |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2269 | QualType ASTContext::getTypeOfType(QualType tofType) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2270 | QualType Canonical = getCanonicalType(tofType); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2271 | TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2272 | Types.push_back(tot); |
| 2273 | return QualType(tot, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2274 | } |
| 2275 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2276 | /// getDecltypeForExpr - Given an expr, will return the decltype for that |
| 2277 | /// expression, according to the rules in C++0x [dcl.type.simple]p4 |
| 2278 | static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) { |
Anders Carlsson | a07c33e | 2009-06-25 15:00:34 +0000 | [diff] [blame] | 2279 | if (e->isTypeDependent()) |
| 2280 | return Context.DependentTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2281 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2282 | // If e is an id expression or a class member access, decltype(e) is defined |
| 2283 | // as the type of the entity named by e. |
| 2284 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) { |
| 2285 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) |
| 2286 | return VD->getType(); |
| 2287 | } |
| 2288 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) { |
| 2289 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) |
| 2290 | return FD->getType(); |
| 2291 | } |
| 2292 | // If e is a function call or an invocation of an overloaded operator, |
| 2293 | // (parentheses around e are ignored), decltype(e) is defined as the |
| 2294 | // return type of that function. |
| 2295 | if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens())) |
| 2296 | return CE->getCallReturnType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2297 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2298 | QualType T = e->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2299 | |
| 2300 | // 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] | 2301 | // defined as T&, otherwise decltype(e) is defined as T. |
| 2302 | if (e->isLvalue(Context) == Expr::LV_Valid) |
| 2303 | T = Context.getLValueReferenceType(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2304 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2305 | return T; |
| 2306 | } |
| 2307 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2308 | /// getDecltypeType - Unlike many "get<Type>" functions, we don't unique |
| 2309 | /// DecltypeType AST's. The only motivation to unique these nodes would be |
| 2310 | /// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2311 | /// an issue. This doesn't effect the type checker, since it operates |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2312 | /// on canonical type's (which are always unique). |
| 2313 | QualType ASTContext::getDecltypeType(Expr *e) { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2314 | DecltypeType *dt; |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2315 | if (e->isTypeDependent()) { |
| 2316 | llvm::FoldingSetNodeID ID; |
| 2317 | DependentDecltypeType::Profile(ID, *this, e); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2318 | |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2319 | void *InsertPos = 0; |
| 2320 | DependentDecltypeType *Canon |
| 2321 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2322 | if (Canon) { |
| 2323 | // We already have a "canonical" version of an equivalent, dependent |
| 2324 | // decltype type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2325 | dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy, |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2326 | QualType((DecltypeType*)Canon, 0)); |
| 2327 | } |
| 2328 | else { |
| 2329 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2330 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2331 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); |
| 2332 | dt = Canon; |
| 2333 | } |
| 2334 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2335 | QualType T = getDecltypeForExpr(e, *this); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2336 | dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T)); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2337 | } |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2338 | Types.push_back(dt); |
| 2339 | return QualType(dt, 0); |
| 2340 | } |
| 2341 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2342 | /// getTagDeclType - Return the unique reference to the type for the |
| 2343 | /// specified TagDecl (struct/union/class/enum) decl. |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 2344 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) { |
Ted Kremenek | d778f88 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 2345 | assert (Decl); |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 2346 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 2347 | // away const? mutable? |
| 2348 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2349 | } |
| 2350 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2351 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 2352 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 2353 | /// needs to agree with the definition in <stddef.h>. |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 2354 | CanQualType ASTContext::getSizeType() const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2355 | return getFromTargetType(Target.getSizeType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2356 | } |
| 2357 | |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 2358 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 2359 | /// Used when in C++, as a GCC extension. |
| 2360 | QualType ASTContext::getSignedWCharType() const { |
| 2361 | // FIXME: derive from "Target" ? |
| 2362 | return WCharTy; |
| 2363 | } |
| 2364 | |
| 2365 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 2366 | /// Used when in C++, as a GCC extension. |
| 2367 | QualType ASTContext::getUnsignedWCharType() const { |
| 2368 | // FIXME: derive from "Target" ? |
| 2369 | return UnsignedIntTy; |
| 2370 | } |
| 2371 | |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 2372 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) |
| 2373 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 2374 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2375 | return getFromTargetType(Target.getPtrDiffType(0)); |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 2376 | } |
| 2377 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2378 | //===----------------------------------------------------------------------===// |
| 2379 | // Type Operators |
| 2380 | //===----------------------------------------------------------------------===// |
| 2381 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2382 | CanQualType ASTContext::getCanonicalParamType(QualType T) { |
| 2383 | // Push qualifiers into arrays, and then discard any remaining |
| 2384 | // qualifiers. |
| 2385 | T = getCanonicalType(T); |
| 2386 | const Type *Ty = T.getTypePtr(); |
| 2387 | |
| 2388 | QualType Result; |
| 2389 | if (isa<ArrayType>(Ty)) { |
| 2390 | Result = getArrayDecayedType(QualType(Ty,0)); |
| 2391 | } else if (isa<FunctionType>(Ty)) { |
| 2392 | Result = getPointerType(QualType(Ty, 0)); |
| 2393 | } else { |
| 2394 | Result = QualType(Ty, 0); |
| 2395 | } |
| 2396 | |
| 2397 | return CanQualType::CreateUnsafe(Result); |
| 2398 | } |
| 2399 | |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 2400 | /// getCanonicalType - Return the canonical (structural) type corresponding to |
| 2401 | /// the specified potentially non-canonical type. The non-canonical version |
| 2402 | /// of a type may have many "decorated" versions of types. Decorators can |
| 2403 | /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed |
| 2404 | /// to be free of any of these, allowing two canonical types to be compared |
| 2405 | /// for exact equality with a simple pointer comparison. |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2406 | CanQualType ASTContext::getCanonicalType(QualType T) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2407 | QualifierCollector Quals; |
| 2408 | const Type *Ptr = Quals.strip(T); |
| 2409 | QualType CanType = Ptr->getCanonicalTypeInternal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2410 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2411 | // The canonical internal type will be the canonical type *except* |
| 2412 | // that we push type qualifiers down through array types. |
| 2413 | |
| 2414 | // If there are no new qualifiers to push down, stop here. |
| 2415 | if (!Quals.hasQualifiers()) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2416 | return CanQualType::CreateUnsafe(CanType); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2417 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2418 | // If the type qualifiers are on an array type, get the canonical |
| 2419 | // type of the array with the qualifiers applied to the element |
| 2420 | // type. |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2421 | ArrayType *AT = dyn_cast<ArrayType>(CanType); |
| 2422 | if (!AT) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2423 | return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2424 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2425 | // Get the canonical version of the element with the extra qualifiers on it. |
| 2426 | // This can recursively sink qualifiers through multiple levels of arrays. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2427 | QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2428 | NewEltTy = getCanonicalType(NewEltTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2429 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2430 | if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2431 | return CanQualType::CreateUnsafe( |
| 2432 | getConstantArrayType(NewEltTy, CAT->getSize(), |
| 2433 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2434 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2435 | if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2436 | return CanQualType::CreateUnsafe( |
| 2437 | getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2438 | IAT->getIndexTypeCVRQualifiers())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2439 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2440 | if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2441 | return CanQualType::CreateUnsafe( |
| 2442 | getDependentSizedArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2443 | DSAT->getSizeExpr() ? |
| 2444 | DSAT->getSizeExpr()->Retain() : 0, |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2445 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2446 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 87a924e | 2009-10-30 22:56:57 +0000 | [diff] [blame] | 2447 | DSAT->getBracketsRange())->getCanonicalTypeInternal()); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2448 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2449 | VariableArrayType *VAT = cast<VariableArrayType>(AT); |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2450 | return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2451 | VAT->getSizeExpr() ? |
| 2452 | VAT->getSizeExpr()->Retain() : 0, |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2453 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2454 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2455 | VAT->getBracketsRange())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2456 | } |
| 2457 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2458 | QualType ASTContext::getUnqualifiedArrayType(QualType T, |
| 2459 | Qualifiers &Quals) { |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2460 | Quals = T.getQualifiers(); |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2461 | const ArrayType *AT = getAsArrayType(T); |
| 2462 | if (!AT) { |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2463 | return T.getUnqualifiedType(); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2464 | } |
| 2465 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2466 | QualType Elt = AT->getElementType(); |
Zhongxing Xu | c1ae0a8 | 2010-01-05 08:15:06 +0000 | [diff] [blame] | 2467 | QualType UnqualElt = getUnqualifiedArrayType(Elt, Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2468 | if (Elt == UnqualElt) |
| 2469 | return T; |
| 2470 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2471 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2472 | return getConstantArrayType(UnqualElt, CAT->getSize(), |
| 2473 | CAT->getSizeModifier(), 0); |
| 2474 | } |
| 2475 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2476 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2477 | return getIncompleteArrayType(UnqualElt, IAT->getSizeModifier(), 0); |
| 2478 | } |
| 2479 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2480 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) { |
| 2481 | return getVariableArrayType(UnqualElt, |
| 2482 | VAT->getSizeExpr() ? |
| 2483 | VAT->getSizeExpr()->Retain() : 0, |
| 2484 | VAT->getSizeModifier(), |
| 2485 | VAT->getIndexTypeCVRQualifiers(), |
| 2486 | VAT->getBracketsRange()); |
| 2487 | } |
| 2488 | |
| 2489 | const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2490 | return getDependentSizedArrayType(UnqualElt, DSAT->getSizeExpr()->Retain(), |
| 2491 | DSAT->getSizeModifier(), 0, |
| 2492 | SourceRange()); |
| 2493 | } |
| 2494 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2495 | /// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that |
| 2496 | /// may be similar (C++ 4.4), replaces T1 and T2 with the type that |
| 2497 | /// they point to and return true. If T1 and T2 aren't pointer types |
| 2498 | /// or pointer-to-member types, or if they are not similar at this |
| 2499 | /// level, returns false and leaves T1 and T2 unchanged. Top-level |
| 2500 | /// qualifiers on T1 and T2 are ignored. This function will typically |
| 2501 | /// be called in a loop that successively "unwraps" pointer and |
| 2502 | /// pointer-to-member types to compare them at each level. |
| 2503 | bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) { |
| 2504 | const PointerType *T1PtrType = T1->getAs<PointerType>(), |
| 2505 | *T2PtrType = T2->getAs<PointerType>(); |
| 2506 | if (T1PtrType && T2PtrType) { |
| 2507 | T1 = T1PtrType->getPointeeType(); |
| 2508 | T2 = T2PtrType->getPointeeType(); |
| 2509 | return true; |
| 2510 | } |
| 2511 | |
| 2512 | const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(), |
| 2513 | *T2MPType = T2->getAs<MemberPointerType>(); |
| 2514 | if (T1MPType && T2MPType && |
| 2515 | hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0), |
| 2516 | QualType(T2MPType->getClass(), 0))) { |
| 2517 | T1 = T1MPType->getPointeeType(); |
| 2518 | T2 = T2MPType->getPointeeType(); |
| 2519 | return true; |
| 2520 | } |
| 2521 | |
| 2522 | if (getLangOptions().ObjC1) { |
| 2523 | const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(), |
| 2524 | *T2OPType = T2->getAs<ObjCObjectPointerType>(); |
| 2525 | if (T1OPType && T2OPType) { |
| 2526 | T1 = T1OPType->getPointeeType(); |
| 2527 | T2 = T2OPType->getPointeeType(); |
| 2528 | return true; |
| 2529 | } |
| 2530 | } |
| 2531 | |
| 2532 | // FIXME: Block pointers, too? |
| 2533 | |
| 2534 | return false; |
| 2535 | } |
| 2536 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2537 | DeclarationNameInfo ASTContext::getNameForTemplate(TemplateName Name, |
| 2538 | SourceLocation NameLoc) { |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2539 | if (TemplateDecl *TD = Name.getAsTemplateDecl()) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2540 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 2541 | return DeclarationNameInfo(TD->getDeclName(), NameLoc); |
| 2542 | |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2543 | if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2544 | DeclarationName DName; |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2545 | if (DTN->isIdentifier()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2546 | DName = DeclarationNames.getIdentifier(DTN->getIdentifier()); |
| 2547 | return DeclarationNameInfo(DName, NameLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2548 | } else { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2549 | DName = DeclarationNames.getCXXOperatorName(DTN->getOperator()); |
| 2550 | // DNInfo work in progress: FIXME: source locations? |
| 2551 | DeclarationNameLoc DNLoc; |
| 2552 | DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding(); |
| 2553 | DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding(); |
| 2554 | return DeclarationNameInfo(DName, NameLoc, DNLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2555 | } |
| 2556 | } |
| 2557 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 2558 | OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate(); |
| 2559 | assert(Storage); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2560 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 2561 | return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2562 | } |
| 2563 | |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2564 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) { |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 2565 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 2566 | if (TemplateTemplateParmDecl *TTP |
| 2567 | = dyn_cast<TemplateTemplateParmDecl>(Template)) |
| 2568 | Template = getCanonicalTemplateTemplateParmDecl(TTP); |
| 2569 | |
| 2570 | // The canonical template name is the canonical template declaration. |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 2571 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 2572 | } |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2573 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 2574 | assert(!Name.getAsOverloadedTemplate()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2575 | |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2576 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 2577 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 2578 | return DTN->CanonicalTemplateName; |
| 2579 | } |
| 2580 | |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 2581 | bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) { |
| 2582 | X = getCanonicalTemplateName(X); |
| 2583 | Y = getCanonicalTemplateName(Y); |
| 2584 | return X.getAsVoidPointer() == Y.getAsVoidPointer(); |
| 2585 | } |
| 2586 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2587 | TemplateArgument |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2588 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) { |
| 2589 | switch (Arg.getKind()) { |
| 2590 | case TemplateArgument::Null: |
| 2591 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2592 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2593 | case TemplateArgument::Expression: |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2594 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2595 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2596 | case TemplateArgument::Declaration: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2597 | return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2598 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2599 | case TemplateArgument::Template: |
| 2600 | return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate())); |
| 2601 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2602 | case TemplateArgument::Integral: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2603 | return TemplateArgument(*Arg.getAsIntegral(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2604 | getCanonicalType(Arg.getIntegralType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2605 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2606 | case TemplateArgument::Type: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2607 | return TemplateArgument(getCanonicalType(Arg.getAsType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2608 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2609 | case TemplateArgument::Pack: { |
| 2610 | // FIXME: Allocate in ASTContext |
| 2611 | TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()]; |
| 2612 | unsigned Idx = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2613 | for (TemplateArgument::pack_iterator A = Arg.pack_begin(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2614 | AEnd = Arg.pack_end(); |
| 2615 | A != AEnd; (void)++A, ++Idx) |
| 2616 | CanonArgs[Idx] = getCanonicalTemplateArgument(*A); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2617 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2618 | TemplateArgument Result; |
| 2619 | Result.setArgumentPack(CanonArgs, Arg.pack_size(), false); |
| 2620 | return Result; |
| 2621 | } |
| 2622 | } |
| 2623 | |
| 2624 | // Silence GCC warning |
| 2625 | assert(false && "Unhandled template argument kind"); |
| 2626 | return TemplateArgument(); |
| 2627 | } |
| 2628 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2629 | NestedNameSpecifier * |
| 2630 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2631 | if (!NNS) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2632 | return 0; |
| 2633 | |
| 2634 | switch (NNS->getKind()) { |
| 2635 | case NestedNameSpecifier::Identifier: |
| 2636 | // Canonicalize the prefix but keep the identifier the same. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2637 | return NestedNameSpecifier::Create(*this, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2638 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 2639 | NNS->getAsIdentifier()); |
| 2640 | |
| 2641 | case NestedNameSpecifier::Namespace: |
| 2642 | // A namespace is canonical; build a nested-name-specifier with |
| 2643 | // this namespace and no prefix. |
| 2644 | return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace()); |
| 2645 | |
| 2646 | case NestedNameSpecifier::TypeSpec: |
| 2647 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 2648 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2649 | return NestedNameSpecifier::Create(*this, 0, |
| 2650 | NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2651 | T.getTypePtr()); |
| 2652 | } |
| 2653 | |
| 2654 | case NestedNameSpecifier::Global: |
| 2655 | // The global specifier is canonical and unique. |
| 2656 | return NNS; |
| 2657 | } |
| 2658 | |
| 2659 | // Required to silence a GCC warning |
| 2660 | return 0; |
| 2661 | } |
| 2662 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2663 | |
| 2664 | const ArrayType *ASTContext::getAsArrayType(QualType T) { |
| 2665 | // Handle the non-qualified case efficiently. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2666 | if (!T.hasLocalQualifiers()) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2667 | // Handle the common positive case fast. |
| 2668 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 2669 | return AT; |
| 2670 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2671 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2672 | // Handle the common negative case fast. |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2673 | QualType CType = T->getCanonicalTypeInternal(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2674 | if (!isa<ArrayType>(CType)) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2675 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2676 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2677 | // Apply any qualifiers from the array type to the element type. This |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2678 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 2679 | // 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] | 2680 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2681 | // If we get here, we either have type qualifiers on the type, or we have |
| 2682 | // 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] | 2683 | // we must propagate them down into the element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2684 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2685 | QualifierCollector Qs; |
| 2686 | const Type *Ty = Qs.strip(T.getDesugaredType()); |
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 we have a simple case, just return now. |
| 2689 | const ArrayType *ATy = dyn_cast<ArrayType>(Ty); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2690 | if (ATy == 0 || Qs.empty()) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2691 | return ATy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2692 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2693 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 2694 | // qualifiers into the array element type and return a new array type. |
| 2695 | // Get the canonical version of the element with the extra qualifiers on it. |
| 2696 | // This can recursively sink qualifiers through multiple levels of arrays. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2697 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2698 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2699 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 2700 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 2701 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2702 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2703 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 2704 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 2705 | IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2706 | IAT->getIndexTypeCVRQualifiers())); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2707 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2708 | if (const DependentSizedArrayType *DSAT |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2709 | = dyn_cast<DependentSizedArrayType>(ATy)) |
| 2710 | return cast<ArrayType>( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2711 | getDependentSizedArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2712 | DSAT->getSizeExpr() ? |
| 2713 | DSAT->getSizeExpr()->Retain() : 0, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2714 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2715 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2716 | DSAT->getBracketsRange())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2717 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2718 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2719 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2720 | VAT->getSizeExpr() ? |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2721 | VAT->getSizeExpr()->Retain() : 0, |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2722 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2723 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2724 | VAT->getBracketsRange())); |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 2725 | } |
| 2726 | |
| 2727 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2728 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 2729 | /// specified array type to a pointer. This operation is non-trivial when |
| 2730 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 2731 | /// this returns a pointer to a properly qualified element of the array. |
| 2732 | /// |
| 2733 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
| 2734 | QualType ASTContext::getArrayDecayedType(QualType Ty) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2735 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 2736 | // typedefs in the element type of the array. This also handles propagation |
| 2737 | // of type qualifiers from the array type into the element type if present |
| 2738 | // (C99 6.7.3p8). |
| 2739 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 2740 | assert(PrettyArrayType && "Not an array type!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2741 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2742 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2743 | |
| 2744 | // int x[restrict 4] -> int *restrict |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2745 | return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2746 | } |
| 2747 | |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2748 | QualType ASTContext::getBaseElementType(QualType QT) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2749 | QualifierCollector Qs; |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 2750 | while (const ArrayType *AT = getAsArrayType(QualType(Qs.strip(QT), 0))) |
| 2751 | QT = AT->getElementType(); |
| 2752 | return Qs.apply(QT); |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2753 | } |
| 2754 | |
Anders Carlsson | fbbce49 | 2009-09-25 01:23:32 +0000 | [diff] [blame] | 2755 | QualType ASTContext::getBaseElementType(const ArrayType *AT) { |
| 2756 | QualType ElemTy = AT->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2757 | |
Anders Carlsson | fbbce49 | 2009-09-25 01:23:32 +0000 | [diff] [blame] | 2758 | if (const ArrayType *AT = getAsArrayType(ElemTy)) |
| 2759 | return getBaseElementType(AT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2760 | |
Anders Carlsson | 6183a99 | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 2761 | return ElemTy; |
| 2762 | } |
| 2763 | |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 2764 | /// getConstantArrayElementCount - Returns number of constant array elements. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2765 | uint64_t |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 2766 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
| 2767 | uint64_t ElementCount = 1; |
| 2768 | do { |
| 2769 | ElementCount *= CA->getSize().getZExtValue(); |
| 2770 | CA = dyn_cast<ConstantArrayType>(CA->getElementType()); |
| 2771 | } while (CA); |
| 2772 | return ElementCount; |
| 2773 | } |
| 2774 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2775 | /// getFloatingRank - Return a relative rank for floating point types. |
| 2776 | /// 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] | 2777 | static FloatingRank getFloatingRank(QualType T) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2778 | if (const ComplexType *CT = T->getAs<ComplexType>()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2779 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2780 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2781 | assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"); |
| 2782 | switch (T->getAs<BuiltinType>()->getKind()) { |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2783 | default: assert(0 && "getFloatingRank(): not a floating type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2784 | case BuiltinType::Float: return FloatRank; |
| 2785 | case BuiltinType::Double: return DoubleRank; |
| 2786 | case BuiltinType::LongDouble: return LongDoubleRank; |
| 2787 | } |
| 2788 | } |
| 2789 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2790 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 2791 | /// point or a complex type (based on typeDomain/typeSize). |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 2792 | /// 'typeDomain' is a real floating point or complex type. |
| 2793 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 2794 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 2795 | QualType Domain) const { |
| 2796 | FloatingRank EltRank = getFloatingRank(Size); |
| 2797 | if (Domain->isComplexType()) { |
| 2798 | switch (EltRank) { |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 2799 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 2800 | case FloatRank: return FloatComplexTy; |
| 2801 | case DoubleRank: return DoubleComplexTy; |
| 2802 | case LongDoubleRank: return LongDoubleComplexTy; |
| 2803 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2804 | } |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 2805 | |
| 2806 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 2807 | switch (EltRank) { |
| 2808 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
| 2809 | case FloatRank: return FloatTy; |
| 2810 | case DoubleRank: return DoubleTy; |
| 2811 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 2812 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2813 | } |
| 2814 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2815 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 2816 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 2817 | /// '_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] | 2818 | /// LHS < RHS, return -1. |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2819 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) { |
| 2820 | FloatingRank LHSR = getFloatingRank(LHS); |
| 2821 | FloatingRank RHSR = getFloatingRank(RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2822 | |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2823 | if (LHSR == RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 2824 | return 0; |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2825 | if (LHSR > RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 2826 | return 1; |
| 2827 | return -1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2828 | } |
| 2829 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2830 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 2831 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 2832 | /// or if it is not canonicalized. |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2833 | unsigned ASTContext::getIntegerRank(Type *T) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2834 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2835 | if (EnumType* ET = dyn_cast<EnumType>(T)) |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 2836 | T = ET->getDecl()->getPromotionType().getTypePtr(); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2837 | |
Eli Friedman | a342675 | 2009-07-05 23:44:27 +0000 | [diff] [blame] | 2838 | if (T->isSpecificBuiltinType(BuiltinType::WChar)) |
| 2839 | T = getFromTargetType(Target.getWCharType()).getTypePtr(); |
| 2840 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2841 | if (T->isSpecificBuiltinType(BuiltinType::Char16)) |
| 2842 | T = getFromTargetType(Target.getChar16Type()).getTypePtr(); |
| 2843 | |
| 2844 | if (T->isSpecificBuiltinType(BuiltinType::Char32)) |
| 2845 | T = getFromTargetType(Target.getChar32Type()).getTypePtr(); |
| 2846 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2847 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2848 | default: assert(0 && "getIntegerRank(): not a built-in integer"); |
| 2849 | case BuiltinType::Bool: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2850 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2851 | case BuiltinType::Char_S: |
| 2852 | case BuiltinType::Char_U: |
| 2853 | case BuiltinType::SChar: |
| 2854 | case BuiltinType::UChar: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2855 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2856 | case BuiltinType::Short: |
| 2857 | case BuiltinType::UShort: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2858 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2859 | case BuiltinType::Int: |
| 2860 | case BuiltinType::UInt: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2861 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2862 | case BuiltinType::Long: |
| 2863 | case BuiltinType::ULong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2864 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2865 | case BuiltinType::LongLong: |
| 2866 | case BuiltinType::ULongLong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2867 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 2868 | case BuiltinType::Int128: |
| 2869 | case BuiltinType::UInt128: |
| 2870 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2871 | } |
| 2872 | } |
| 2873 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 2874 | /// \brief Whether this is a promotable bitfield reference according |
| 2875 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
| 2876 | /// |
| 2877 | /// \returns the type this bit-field will promote to, or NULL if no |
| 2878 | /// promotion occurs. |
| 2879 | QualType ASTContext::isPromotableBitField(Expr *E) { |
Douglas Gregor | ceafbde | 2010-05-24 20:13:53 +0000 | [diff] [blame] | 2880 | if (E->isTypeDependent() || E->isValueDependent()) |
| 2881 | return QualType(); |
| 2882 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 2883 | FieldDecl *Field = E->getBitField(); |
| 2884 | if (!Field) |
| 2885 | return QualType(); |
| 2886 | |
| 2887 | QualType FT = Field->getType(); |
| 2888 | |
| 2889 | llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this); |
| 2890 | uint64_t BitWidth = BitWidthAP.getZExtValue(); |
| 2891 | uint64_t IntSize = getTypeSize(IntTy); |
| 2892 | // GCC extension compatibility: if the bit-field size is less than or equal |
| 2893 | // to the size of int, it gets promoted no matter what its type is. |
| 2894 | // For instance, unsigned long bf : 4 gets promoted to signed int. |
| 2895 | if (BitWidth < IntSize) |
| 2896 | return IntTy; |
| 2897 | |
| 2898 | if (BitWidth == IntSize) |
| 2899 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
| 2900 | |
| 2901 | // Types bigger than int are not subject to promotions, and therefore act |
| 2902 | // like the base type. |
| 2903 | // FIXME: This doesn't quite match what gcc does, but what gcc does here |
| 2904 | // is ridiculous. |
| 2905 | return QualType(); |
| 2906 | } |
| 2907 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 2908 | /// getPromotedIntegerType - Returns the type that Promotable will |
| 2909 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
| 2910 | /// integer type. |
| 2911 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) { |
| 2912 | assert(!Promotable.isNull()); |
| 2913 | assert(Promotable->isPromotableIntegerType()); |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 2914 | if (const EnumType *ET = Promotable->getAs<EnumType>()) |
| 2915 | return ET->getDecl()->getPromotionType(); |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 2916 | if (Promotable->isSignedIntegerType()) |
| 2917 | return IntTy; |
| 2918 | uint64_t PromotableSize = getTypeSize(Promotable); |
| 2919 | uint64_t IntSize = getTypeSize(IntTy); |
| 2920 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
| 2921 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
| 2922 | } |
| 2923 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2924 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2925 | /// 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] | 2926 | /// LHS < RHS, return -1. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2927 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2928 | Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 2929 | Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2930 | if (LHSC == RHSC) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2931 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2932 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 2933 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2934 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2935 | unsigned LHSRank = getIntegerRank(LHSC); |
| 2936 | unsigned RHSRank = getIntegerRank(RHSC); |
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 (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 2939 | if (LHSRank == RHSRank) return 0; |
| 2940 | return LHSRank > RHSRank ? 1 : -1; |
| 2941 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2942 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2943 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 2944 | if (LHSUnsigned) { |
| 2945 | // If the unsigned [LHS] type is larger, return it. |
| 2946 | if (LHSRank >= RHSRank) |
| 2947 | return 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2948 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2949 | // If the signed type can represent all values of the unsigned type, it |
| 2950 | // 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] | 2951 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2952 | return -1; |
| 2953 | } |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2954 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2955 | // If the unsigned [RHS] type is larger, return it. |
| 2956 | if (RHSRank >= LHSRank) |
| 2957 | return -1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2958 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2959 | // If the signed type can represent all values of the unsigned type, it |
| 2960 | // 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] | 2961 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2962 | return 1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2963 | } |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2964 | |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 2965 | static RecordDecl * |
| 2966 | CreateRecordDecl(ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC, |
| 2967 | SourceLocation L, IdentifierInfo *Id) { |
| 2968 | if (Ctx.getLangOptions().CPlusPlus) |
| 2969 | return CXXRecordDecl::Create(Ctx, TK, DC, L, Id); |
| 2970 | else |
| 2971 | return RecordDecl::Create(Ctx, TK, DC, L, Id); |
| 2972 | } |
| 2973 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2974 | // getCFConstantStringType - Return the type used for constant CFStrings. |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2975 | QualType ASTContext::getCFConstantStringType() { |
| 2976 | if (!CFConstantStringTypeDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2977 | CFConstantStringTypeDecl = |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2978 | CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 2979 | &Idents.get("NSConstantString")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 2980 | CFConstantStringTypeDecl->startDefinition(); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 2981 | |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 2982 | QualType FieldTypes[4]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2983 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2984 | // const int *isa; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2985 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 2986 | // int flags; |
| 2987 | FieldTypes[1] = IntTy; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2988 | // const char *str; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2989 | FieldTypes[2] = getPointerType(CharTy.withConst()); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2990 | // long length; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2991 | FieldTypes[3] = LongTy; |
| 2992 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2993 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2994 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2995 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2996 | SourceLocation(), 0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2997 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2998 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2999 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3000 | Field->setAccess(AS_public); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3001 | CFConstantStringTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3002 | } |
| 3003 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3004 | CFConstantStringTypeDecl->completeDefinition(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3005 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3006 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3007 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 8467583 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 3008 | } |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 3009 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3010 | void ASTContext::setCFConstantStringType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3011 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3012 | assert(Rec && "Invalid CFConstantStringType"); |
| 3013 | CFConstantStringTypeDecl = Rec->getDecl(); |
| 3014 | } |
| 3015 | |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 3016 | // getNSConstantStringType - Return the type used for constant NSStrings. |
| 3017 | QualType ASTContext::getNSConstantStringType() { |
| 3018 | if (!NSConstantStringTypeDecl) { |
| 3019 | NSConstantStringTypeDecl = |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3020 | CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 3021 | &Idents.get("__builtin_NSString")); |
| 3022 | NSConstantStringTypeDecl->startDefinition(); |
| 3023 | |
| 3024 | QualType FieldTypes[3]; |
| 3025 | |
| 3026 | // const int *isa; |
| 3027 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
| 3028 | // const char *str; |
| 3029 | FieldTypes[1] = getPointerType(CharTy.withConst()); |
| 3030 | // unsigned int length; |
| 3031 | FieldTypes[2] = UnsignedIntTy; |
| 3032 | |
| 3033 | // Create fields |
| 3034 | for (unsigned i = 0; i < 3; ++i) { |
| 3035 | FieldDecl *Field = FieldDecl::Create(*this, NSConstantStringTypeDecl, |
| 3036 | SourceLocation(), 0, |
| 3037 | FieldTypes[i], /*TInfo=*/0, |
| 3038 | /*BitWidth=*/0, |
| 3039 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3040 | Field->setAccess(AS_public); |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 3041 | NSConstantStringTypeDecl->addDecl(Field); |
| 3042 | } |
| 3043 | |
| 3044 | NSConstantStringTypeDecl->completeDefinition(); |
| 3045 | } |
| 3046 | |
| 3047 | return getTagDeclType(NSConstantStringTypeDecl); |
| 3048 | } |
| 3049 | |
| 3050 | void ASTContext::setNSConstantStringType(QualType T) { |
| 3051 | const RecordType *Rec = T->getAs<RecordType>(); |
| 3052 | assert(Rec && "Invalid NSConstantStringType"); |
| 3053 | NSConstantStringTypeDecl = Rec->getDecl(); |
| 3054 | } |
| 3055 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3056 | QualType ASTContext::getObjCFastEnumerationStateType() { |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3057 | if (!ObjCFastEnumerationStateTypeDecl) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3058 | ObjCFastEnumerationStateTypeDecl = |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3059 | CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3060 | &Idents.get("__objcFastEnumerationState")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3061 | ObjCFastEnumerationStateTypeDecl->startDefinition(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3062 | |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3063 | QualType FieldTypes[] = { |
| 3064 | UnsignedLongTy, |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3065 | getPointerType(ObjCIdTypedefType), |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3066 | getPointerType(UnsignedLongTy), |
| 3067 | getConstantArrayType(UnsignedLongTy, |
| 3068 | llvm::APInt(32, 5), ArrayType::Normal, 0) |
| 3069 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3070 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3071 | for (size_t i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3072 | FieldDecl *Field = FieldDecl::Create(*this, |
| 3073 | ObjCFastEnumerationStateTypeDecl, |
| 3074 | SourceLocation(), 0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3075 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3076 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 3077 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3078 | Field->setAccess(AS_public); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3079 | ObjCFastEnumerationStateTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3080 | } |
Fariborz Jahanian | 38c9ab8 | 2010-05-27 16:05:06 +0000 | [diff] [blame] | 3081 | if (getLangOptions().CPlusPlus) |
Fariborz Jahanian | 81148e9 | 2010-05-27 16:35:00 +0000 | [diff] [blame] | 3082 | if (CXXRecordDecl *CXXRD = |
| 3083 | dyn_cast<CXXRecordDecl>(ObjCFastEnumerationStateTypeDecl)) |
Fariborz Jahanian | 38c9ab8 | 2010-05-27 16:05:06 +0000 | [diff] [blame] | 3084 | CXXRD->setEmpty(false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3085 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3086 | ObjCFastEnumerationStateTypeDecl->completeDefinition(); |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3087 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3088 | |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3089 | return getTagDeclType(ObjCFastEnumerationStateTypeDecl); |
| 3090 | } |
| 3091 | |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3092 | QualType ASTContext::getBlockDescriptorType() { |
| 3093 | if (BlockDescriptorType) |
| 3094 | return getTagDeclType(BlockDescriptorType); |
| 3095 | |
| 3096 | RecordDecl *T; |
| 3097 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3098 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3099 | &Idents.get("__block_descriptor")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3100 | T->startDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3101 | |
| 3102 | QualType FieldTypes[] = { |
| 3103 | UnsignedLongTy, |
| 3104 | UnsignedLongTy, |
| 3105 | }; |
| 3106 | |
| 3107 | const char *FieldNames[] = { |
| 3108 | "reserved", |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3109 | "Size" |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3110 | }; |
| 3111 | |
| 3112 | for (size_t i = 0; i < 2; ++i) { |
| 3113 | FieldDecl *Field = FieldDecl::Create(*this, |
| 3114 | T, |
| 3115 | SourceLocation(), |
| 3116 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3117 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3118 | /*BitWidth=*/0, |
| 3119 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3120 | Field->setAccess(AS_public); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3121 | T->addDecl(Field); |
| 3122 | } |
| 3123 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3124 | T->completeDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3125 | |
| 3126 | BlockDescriptorType = T; |
| 3127 | |
| 3128 | return getTagDeclType(BlockDescriptorType); |
| 3129 | } |
| 3130 | |
| 3131 | void ASTContext::setBlockDescriptorType(QualType T) { |
| 3132 | const RecordType *Rec = T->getAs<RecordType>(); |
| 3133 | assert(Rec && "Invalid BlockDescriptorType"); |
| 3134 | BlockDescriptorType = Rec->getDecl(); |
| 3135 | } |
| 3136 | |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3137 | QualType ASTContext::getBlockDescriptorExtendedType() { |
| 3138 | if (BlockDescriptorExtendedType) |
| 3139 | return getTagDeclType(BlockDescriptorExtendedType); |
| 3140 | |
| 3141 | RecordDecl *T; |
| 3142 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3143 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3144 | &Idents.get("__block_descriptor_withcopydispose")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3145 | T->startDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3146 | |
| 3147 | QualType FieldTypes[] = { |
| 3148 | UnsignedLongTy, |
| 3149 | UnsignedLongTy, |
| 3150 | getPointerType(VoidPtrTy), |
| 3151 | getPointerType(VoidPtrTy) |
| 3152 | }; |
| 3153 | |
| 3154 | const char *FieldNames[] = { |
| 3155 | "reserved", |
| 3156 | "Size", |
| 3157 | "CopyFuncPtr", |
| 3158 | "DestroyFuncPtr" |
| 3159 | }; |
| 3160 | |
| 3161 | for (size_t i = 0; i < 4; ++i) { |
| 3162 | FieldDecl *Field = FieldDecl::Create(*this, |
| 3163 | T, |
| 3164 | SourceLocation(), |
| 3165 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3166 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3167 | /*BitWidth=*/0, |
| 3168 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3169 | Field->setAccess(AS_public); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3170 | T->addDecl(Field); |
| 3171 | } |
| 3172 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3173 | T->completeDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3174 | |
| 3175 | BlockDescriptorExtendedType = T; |
| 3176 | |
| 3177 | return getTagDeclType(BlockDescriptorExtendedType); |
| 3178 | } |
| 3179 | |
| 3180 | void ASTContext::setBlockDescriptorExtendedType(QualType T) { |
| 3181 | const RecordType *Rec = T->getAs<RecordType>(); |
| 3182 | assert(Rec && "Invalid BlockDescriptorType"); |
| 3183 | BlockDescriptorExtendedType = Rec->getDecl(); |
| 3184 | } |
| 3185 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3186 | bool ASTContext::BlockRequiresCopying(QualType Ty) { |
| 3187 | if (Ty->isBlockPointerType()) |
| 3188 | return true; |
| 3189 | if (isObjCNSObjectType(Ty)) |
| 3190 | return true; |
| 3191 | if (Ty->isObjCObjectPointerType()) |
| 3192 | return true; |
| 3193 | return false; |
| 3194 | } |
| 3195 | |
| 3196 | QualType ASTContext::BuildByRefType(const char *DeclName, QualType Ty) { |
| 3197 | // type = struct __Block_byref_1_X { |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3198 | // void *__isa; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3199 | // struct __Block_byref_1_X *__forwarding; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3200 | // unsigned int __flags; |
| 3201 | // unsigned int __size; |
Mike Stump | 38e1627 | 2009-10-21 22:01:24 +0000 | [diff] [blame] | 3202 | // void *__copy_helper; // as needed |
| 3203 | // void *__destroy_help // as needed |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3204 | // int X; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3205 | // } * |
| 3206 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3207 | bool HasCopyAndDispose = BlockRequiresCopying(Ty); |
| 3208 | |
| 3209 | // FIXME: Move up |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 3210 | llvm::SmallString<36> Name; |
| 3211 | llvm::raw_svector_ostream(Name) << "__Block_byref_" << |
| 3212 | ++UniqueBlockByRefTypeID << '_' << DeclName; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3213 | RecordDecl *T; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3214 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3215 | &Idents.get(Name.str())); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3216 | T->startDefinition(); |
| 3217 | QualType Int32Ty = IntTy; |
| 3218 | assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported"); |
| 3219 | QualType FieldTypes[] = { |
| 3220 | getPointerType(VoidPtrTy), |
| 3221 | getPointerType(getTagDeclType(T)), |
| 3222 | Int32Ty, |
| 3223 | Int32Ty, |
| 3224 | getPointerType(VoidPtrTy), |
| 3225 | getPointerType(VoidPtrTy), |
| 3226 | Ty |
| 3227 | }; |
| 3228 | |
| 3229 | const char *FieldNames[] = { |
| 3230 | "__isa", |
| 3231 | "__forwarding", |
| 3232 | "__flags", |
| 3233 | "__size", |
| 3234 | "__copy_helper", |
| 3235 | "__destroy_helper", |
| 3236 | DeclName, |
| 3237 | }; |
| 3238 | |
| 3239 | for (size_t i = 0; i < 7; ++i) { |
| 3240 | if (!HasCopyAndDispose && i >=4 && i <= 5) |
| 3241 | continue; |
| 3242 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
| 3243 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3244 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3245 | /*BitWidth=*/0, /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3246 | Field->setAccess(AS_public); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3247 | T->addDecl(Field); |
| 3248 | } |
| 3249 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3250 | T->completeDefinition(); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3251 | |
| 3252 | return getPointerType(getTagDeclType(T)); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3253 | } |
| 3254 | |
| 3255 | |
| 3256 | QualType ASTContext::getBlockParmType( |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3257 | bool BlockHasCopyDispose, |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 3258 | llvm::SmallVectorImpl<const Expr *> &Layout) { |
| 3259 | |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3260 | // FIXME: Move up |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 3261 | llvm::SmallString<36> Name; |
| 3262 | llvm::raw_svector_ostream(Name) << "__block_literal_" |
| 3263 | << ++UniqueBlockParmTypeID; |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3264 | RecordDecl *T; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3265 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3266 | &Idents.get(Name.str())); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3267 | T->startDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3268 | QualType FieldTypes[] = { |
| 3269 | getPointerType(VoidPtrTy), |
| 3270 | IntTy, |
| 3271 | IntTy, |
| 3272 | getPointerType(VoidPtrTy), |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3273 | (BlockHasCopyDispose ? |
| 3274 | getPointerType(getBlockDescriptorExtendedType()) : |
| 3275 | getPointerType(getBlockDescriptorType())) |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3276 | }; |
| 3277 | |
| 3278 | const char *FieldNames[] = { |
| 3279 | "__isa", |
| 3280 | "__flags", |
| 3281 | "__reserved", |
| 3282 | "__FuncPtr", |
| 3283 | "__descriptor" |
| 3284 | }; |
| 3285 | |
| 3286 | for (size_t i = 0; i < 5; ++i) { |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3287 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3288 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3289 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3290 | /*BitWidth=*/0, /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3291 | Field->setAccess(AS_public); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3292 | T->addDecl(Field); |
| 3293 | } |
| 3294 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 3295 | for (unsigned i = 0; i < Layout.size(); ++i) { |
| 3296 | const Expr *E = Layout[i]; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3297 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 3298 | QualType FieldType = E->getType(); |
| 3299 | IdentifierInfo *FieldName = 0; |
| 3300 | if (isa<CXXThisExpr>(E)) { |
| 3301 | FieldName = &Idents.get("this"); |
| 3302 | } else if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E)) { |
| 3303 | const ValueDecl *D = BDRE->getDecl(); |
| 3304 | FieldName = D->getIdentifier(); |
| 3305 | if (BDRE->isByRef()) |
| 3306 | FieldType = BuildByRefType(D->getNameAsCString(), FieldType); |
| 3307 | } else { |
| 3308 | // Padding. |
| 3309 | assert(isa<ConstantArrayType>(FieldType) && |
| 3310 | isa<DeclRefExpr>(E) && |
| 3311 | !cast<DeclRefExpr>(E)->getDecl()->getDeclName() && |
| 3312 | "doesn't match characteristics of padding decl"); |
| 3313 | } |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3314 | |
| 3315 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 3316 | FieldName, FieldType, /*TInfo=*/0, |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3317 | /*BitWidth=*/0, /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3318 | Field->setAccess(AS_public); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3319 | T->addDecl(Field); |
| 3320 | } |
| 3321 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3322 | T->completeDefinition(); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3323 | |
| 3324 | return getPointerType(getTagDeclType(T)); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3325 | } |
| 3326 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3327 | void ASTContext::setObjCFastEnumerationStateType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3328 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3329 | assert(Rec && "Invalid ObjCFAstEnumerationStateType"); |
| 3330 | ObjCFastEnumerationStateTypeDecl = Rec->getDecl(); |
| 3331 | } |
| 3332 | |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3333 | // This returns true if a type has been typedefed to BOOL: |
| 3334 | // typedef <type> BOOL; |
Chris Lattner | 2d99833 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 3335 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3336 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | bb49c3e | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 3337 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 3338 | return II->isStr("BOOL"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3339 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3340 | return false; |
| 3341 | } |
| 3342 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3343 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3344 | /// purpose. |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3345 | CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) { |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3346 | CharUnits sz = getTypeSizeInChars(type); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3347 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3348 | // 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] | 3349 | if (sz.isPositive() && type->isIntegralOrEnumerationType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3350 | sz = std::max(sz, getTypeSizeInChars(IntTy)); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3351 | // Treat arrays as pointers, since that's how they're passed in. |
| 3352 | else if (type->isArrayType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3353 | sz = getTypeSizeInChars(VoidPtrTy); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3354 | return sz; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3355 | } |
| 3356 | |
| 3357 | static inline |
| 3358 | std::string charUnitsToString(const CharUnits &CU) { |
| 3359 | return llvm::itostr(CU.getQuantity()); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3360 | } |
| 3361 | |
Fariborz Jahanian | 6f46c26 | 2010-04-08 18:06:22 +0000 | [diff] [blame] | 3362 | /// getObjCEncodingForBlockDecl - Return the encoded type for this block |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3363 | /// declaration. |
| 3364 | void ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr, |
| 3365 | std::string& S) { |
| 3366 | const BlockDecl *Decl = Expr->getBlockDecl(); |
| 3367 | QualType BlockTy = |
| 3368 | Expr->getType()->getAs<BlockPointerType>()->getPointeeType(); |
| 3369 | // Encode result type. |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 3370 | getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3371 | // Compute size of all parameters. |
| 3372 | // Start with computing size of a pointer in number of bytes. |
| 3373 | // FIXME: There might(should) be a better way of doing this computation! |
| 3374 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3375 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
| 3376 | CharUnits ParmOffset = PtrSize; |
Fariborz Jahanian | 6f46c26 | 2010-04-08 18:06:22 +0000 | [diff] [blame] | 3377 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3378 | E = Decl->param_end(); PI != E; ++PI) { |
| 3379 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3380 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3381 | assert (sz.isPositive() && "BlockExpr - Incomplete param type"); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3382 | ParmOffset += sz; |
| 3383 | } |
| 3384 | // Size of the argument frame |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3385 | S += charUnitsToString(ParmOffset); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3386 | // Block pointer and offset. |
| 3387 | S += "@?0"; |
| 3388 | ParmOffset = PtrSize; |
| 3389 | |
| 3390 | // Argument types. |
| 3391 | ParmOffset = PtrSize; |
| 3392 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E = |
| 3393 | Decl->param_end(); PI != E; ++PI) { |
| 3394 | ParmVarDecl *PVDecl = *PI; |
| 3395 | QualType PType = PVDecl->getOriginalType(); |
| 3396 | if (const ArrayType *AT = |
| 3397 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3398 | // Use array's original type only if it has known number of |
| 3399 | // elements. |
| 3400 | if (!isa<ConstantArrayType>(AT)) |
| 3401 | PType = PVDecl->getType(); |
| 3402 | } else if (PType->isFunctionType()) |
| 3403 | PType = PVDecl->getType(); |
| 3404 | getObjCEncodingForType(PType, S); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3405 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3406 | ParmOffset += getObjCEncodingTypeSize(PType); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3407 | } |
| 3408 | } |
| 3409 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3410 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3411 | /// declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3412 | void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Chris Lattner | e6db3b0 | 2008-11-19 07:24:05 +0000 | [diff] [blame] | 3413 | std::string& S) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3414 | // FIXME: This is not very efficient. |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3415 | // Encode type qualifer, 'in', 'inout', etc. for the return type. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3416 | getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3417 | // Encode result type. |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3418 | getObjCEncodingForType(Decl->getResultType(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3419 | // Compute size of all parameters. |
| 3420 | // Start with computing size of a pointer in number of bytes. |
| 3421 | // FIXME: There might(should) be a better way of doing this computation! |
| 3422 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3423 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3424 | // The first two arguments (self and _cmd) are pointers; account for |
| 3425 | // their size. |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3426 | CharUnits ParmOffset = 2 * PtrSize; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3427 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 3428 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3429 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3430 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3431 | assert (sz.isPositive() && |
| 3432 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3433 | ParmOffset += sz; |
| 3434 | } |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3435 | S += charUnitsToString(ParmOffset); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3436 | S += "@0:"; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3437 | S += charUnitsToString(PtrSize); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3438 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3439 | // Argument types. |
| 3440 | ParmOffset = 2 * PtrSize; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3441 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 3442 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3443 | ParmVarDecl *PVDecl = *PI; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3444 | QualType PType = PVDecl->getOriginalType(); |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 3445 | if (const ArrayType *AT = |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 3446 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3447 | // Use array's original type only if it has known number of |
| 3448 | // elements. |
Steve Naroff | bb3fde3 | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 3449 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 3450 | PType = PVDecl->getType(); |
| 3451 | } else if (PType->isFunctionType()) |
| 3452 | PType = PVDecl->getType(); |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3453 | // Process argument qualifiers for user supplied arguments; such as, |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3454 | // 'in', 'inout', etc. |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 3455 | getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3456 | getObjCEncodingForType(PType, S); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3457 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3458 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3459 | } |
| 3460 | } |
| 3461 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3462 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 3463 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3464 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 3465 | /// NULL when getting encodings for protocol properties. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3466 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 3467 | /// attributes readonly and bycopy are encoded as single characters. The |
| 3468 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 3469 | /// encoded as single characters, followed by an identifier. Property types |
| 3470 | /// are also encoded as a parametrized attribute. The characters used to encode |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 3471 | /// these attributes are defined by the following enumeration: |
| 3472 | /// @code |
| 3473 | /// enum PropertyAttributes { |
| 3474 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 3475 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 3476 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 3477 | /// kPropertyDynamic = 'D', // property is dynamic |
| 3478 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 3479 | /// kPropertySetter = 'S', // followed by setter selector name |
| 3480 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
| 3481 | /// kPropertyType = 't' // followed by old-style type encoding. |
| 3482 | /// kPropertyWeak = 'W' // 'weak' property |
| 3483 | /// kPropertyStrong = 'P' // property GC'able |
| 3484 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 3485 | /// }; |
| 3486 | /// @endcode |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3487 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3488 | const Decl *Container, |
Chris Lattner | e6db3b0 | 2008-11-19 07:24:05 +0000 | [diff] [blame] | 3489 | std::string& S) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3490 | // Collect information from the property implementation decl(s). |
| 3491 | bool Dynamic = false; |
| 3492 | ObjCPropertyImplDecl *SynthesizePID = 0; |
| 3493 | |
| 3494 | // FIXME: Duplicated code due to poor abstraction. |
| 3495 | if (Container) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3496 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3497 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
| 3498 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3499 | i = CID->propimpl_begin(), e = CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3500 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3501 | ObjCPropertyImplDecl *PID = *i; |
| 3502 | if (PID->getPropertyDecl() == PD) { |
| 3503 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 3504 | Dynamic = true; |
| 3505 | } else { |
| 3506 | SynthesizePID = PID; |
| 3507 | } |
| 3508 | } |
| 3509 | } |
| 3510 | } else { |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3511 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3512 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3513 | i = OID->propimpl_begin(), e = OID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3514 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3515 | ObjCPropertyImplDecl *PID = *i; |
| 3516 | if (PID->getPropertyDecl() == PD) { |
| 3517 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 3518 | Dynamic = true; |
| 3519 | } else { |
| 3520 | SynthesizePID = PID; |
| 3521 | } |
| 3522 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3523 | } |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3524 | } |
| 3525 | } |
| 3526 | |
| 3527 | // FIXME: This is not very efficient. |
| 3528 | S = "T"; |
| 3529 | |
| 3530 | // Encode result type. |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3531 | // GCC has some special rules regarding encoding of properties which |
| 3532 | // closely resembles encoding of ivars. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3533 | getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3534 | true /* outermost type */, |
| 3535 | true /* encoding for property */); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3536 | |
| 3537 | if (PD->isReadOnly()) { |
| 3538 | S += ",R"; |
| 3539 | } else { |
| 3540 | switch (PD->getSetterKind()) { |
| 3541 | case ObjCPropertyDecl::Assign: break; |
| 3542 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3543 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3544 | } |
| 3545 | } |
| 3546 | |
| 3547 | // It really isn't clear at all what this means, since properties |
| 3548 | // are "dynamic by default". |
| 3549 | if (Dynamic) |
| 3550 | S += ",D"; |
| 3551 | |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3552 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 3553 | S += ",N"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3554 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3555 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 3556 | S += ",G"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3557 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3558 | } |
| 3559 | |
| 3560 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 3561 | S += ",S"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3562 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3563 | } |
| 3564 | |
| 3565 | if (SynthesizePID) { |
| 3566 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 3567 | S += ",V"; |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 3568 | S += OID->getNameAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3569 | } |
| 3570 | |
| 3571 | // FIXME: OBJCGC: weak & strong |
| 3572 | } |
| 3573 | |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3574 | /// getLegacyIntegralTypeEncoding - |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3575 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
| 3576 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3577 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
| 3578 | /// |
| 3579 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3580 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3581 | if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) { |
Fariborz Jahanian | c657eba | 2009-02-11 23:59:18 +0000 | [diff] [blame] | 3582 | if (BT->getKind() == BuiltinType::ULong && |
| 3583 | ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3584 | PointeeTy = UnsignedIntTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3585 | else |
Fariborz Jahanian | c657eba | 2009-02-11 23:59:18 +0000 | [diff] [blame] | 3586 | if (BT->getKind() == BuiltinType::Long && |
| 3587 | ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3588 | PointeeTy = IntTy; |
| 3589 | } |
| 3590 | } |
| 3591 | } |
| 3592 | |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3593 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 3594 | const FieldDecl *Field) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3595 | // We follow the behavior of gcc, expanding structures which are |
| 3596 | // directly pointed to, and expanding embedded structures. Note that |
| 3597 | // these rules are sufficient to prevent recursive encoding of the |
| 3598 | // same type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3599 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
Fariborz Jahanian | 5b8c7d9 | 2008-12-22 23:22:27 +0000 | [diff] [blame] | 3600 | true /* outermost type */); |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3601 | } |
| 3602 | |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3603 | static char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) { |
| 3604 | switch (T->getAs<BuiltinType>()->getKind()) { |
| 3605 | default: assert(0 && "Unhandled builtin type kind"); |
| 3606 | case BuiltinType::Void: return 'v'; |
| 3607 | case BuiltinType::Bool: return 'B'; |
| 3608 | case BuiltinType::Char_U: |
| 3609 | case BuiltinType::UChar: return 'C'; |
| 3610 | case BuiltinType::UShort: return 'S'; |
| 3611 | case BuiltinType::UInt: return 'I'; |
| 3612 | case BuiltinType::ULong: |
| 3613 | return |
| 3614 | (const_cast<ASTContext *>(C))->getIntWidth(T) == 32 ? 'L' : 'Q'; |
| 3615 | case BuiltinType::UInt128: return 'T'; |
| 3616 | case BuiltinType::ULongLong: return 'Q'; |
| 3617 | case BuiltinType::Char_S: |
| 3618 | case BuiltinType::SChar: return 'c'; |
| 3619 | case BuiltinType::Short: return 's'; |
John McCall | 24da709 | 2010-06-11 10:11:05 +0000 | [diff] [blame] | 3620 | case BuiltinType::WChar: |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3621 | case BuiltinType::Int: return 'i'; |
| 3622 | case BuiltinType::Long: |
| 3623 | return |
| 3624 | (const_cast<ASTContext *>(C))->getIntWidth(T) == 32 ? 'l' : 'q'; |
| 3625 | case BuiltinType::LongLong: return 'q'; |
| 3626 | case BuiltinType::Int128: return 't'; |
| 3627 | case BuiltinType::Float: return 'f'; |
| 3628 | case BuiltinType::Double: return 'd'; |
| 3629 | case BuiltinType::LongDouble: return 'd'; |
| 3630 | } |
| 3631 | } |
| 3632 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3633 | static void EncodeBitField(const ASTContext *Context, std::string& S, |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3634 | QualType T, const FieldDecl *FD) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3635 | const Expr *E = FD->getBitWidth(); |
| 3636 | assert(E && "bitfield width not there - getObjCEncodingForTypeImpl"); |
| 3637 | ASTContext *Ctx = const_cast<ASTContext*>(Context); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3638 | S += 'b'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3639 | // The NeXT runtime encodes bit fields as b followed by the number of bits. |
| 3640 | // The GNU runtime requires more information; bitfields are encoded as b, |
| 3641 | // then the offset (in bits) of the first element, then the type of the |
| 3642 | // bitfield, then the size in bits. For example, in this structure: |
| 3643 | // |
| 3644 | // struct |
| 3645 | // { |
| 3646 | // int integer; |
| 3647 | // int flags:2; |
| 3648 | // }; |
| 3649 | // On a 32-bit system, the encoding for flags would be b2 for the NeXT |
| 3650 | // runtime, but b32i2 for the GNU runtime. The reason for this extra |
| 3651 | // information is not especially sensible, but we're stuck with it for |
| 3652 | // compatibility with GCC, although providing it breaks anything that |
| 3653 | // actually uses runtime introspection and wants to work on both runtimes... |
| 3654 | if (!Ctx->getLangOptions().NeXTRuntime) { |
| 3655 | const RecordDecl *RD = FD->getParent(); |
| 3656 | const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD); |
| 3657 | // FIXME: This same linear search is also used in ExprConstant - it might |
| 3658 | // be better if the FieldDecl stored its offset. We'd be increasing the |
| 3659 | // size of the object slightly, but saving some time every time it is used. |
| 3660 | unsigned i = 0; |
| 3661 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 3662 | FieldEnd = RD->field_end(); |
| 3663 | Field != FieldEnd; (void)++Field, ++i) { |
| 3664 | if (*Field == FD) |
| 3665 | break; |
| 3666 | } |
| 3667 | S += llvm::utostr(RL.getFieldOffset(i)); |
| 3668 | S += ObjCEncodingForPrimitiveKind(Context, T); |
| 3669 | } |
| 3670 | unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue(); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3671 | S += llvm::utostr(N); |
| 3672 | } |
| 3673 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 3674 | // FIXME: Use SmallString for accumulating string. |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3675 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 3676 | bool ExpandPointedToStructures, |
| 3677 | bool ExpandStructures, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 3678 | const FieldDecl *FD, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3679 | bool OutermostType, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3680 | bool EncodingProperty) { |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3681 | if (T->getAs<BuiltinType>()) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3682 | if (FD && FD->isBitField()) |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3683 | return EncodeBitField(this, S, T, FD); |
| 3684 | S += ObjCEncodingForPrimitiveKind(this, T); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3685 | return; |
| 3686 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3687 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3688 | if (const ComplexType *CT = T->getAs<ComplexType>()) { |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 3689 | S += 'j'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3690 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false, |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 3691 | false); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3692 | return; |
| 3693 | } |
Fariborz Jahanian | 60bce3e | 2009-11-23 20:40:50 +0000 | [diff] [blame] | 3694 | |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 3695 | // encoding for pointer or r3eference types. |
| 3696 | QualType PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3697 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Fariborz Jahanian | 8d2c0a9 | 2009-11-30 18:43:52 +0000 | [diff] [blame] | 3698 | if (PT->isObjCSelType()) { |
| 3699 | S += ':'; |
| 3700 | return; |
| 3701 | } |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 3702 | PointeeTy = PT->getPointeeType(); |
| 3703 | } |
| 3704 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 3705 | PointeeTy = RT->getPointeeType(); |
| 3706 | if (!PointeeTy.isNull()) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3707 | bool isReadOnly = false; |
| 3708 | // For historical/compatibility reasons, the read-only qualifier of the |
| 3709 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 3710 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3711 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3712 | if (isa<TypedefType>(T.getTypePtr())) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3713 | if (OutermostType && T.isConstQualified()) { |
| 3714 | isReadOnly = true; |
| 3715 | S += 'r'; |
| 3716 | } |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 3717 | } else if (OutermostType) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3718 | QualType P = PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3719 | while (P->getAs<PointerType>()) |
| 3720 | P = P->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3721 | if (P.isConstQualified()) { |
| 3722 | isReadOnly = true; |
| 3723 | S += 'r'; |
| 3724 | } |
| 3725 | } |
| 3726 | if (isReadOnly) { |
| 3727 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 3728 | // combinations need to be rearranged. |
| 3729 | // Rewrite "in const" from "nr" to "rn" |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 3730 | if (llvm::StringRef(S).endswith("nr")) |
| 3731 | S.replace(S.end()-2, S.end(), "rn"); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3732 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3733 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3734 | if (PointeeTy->isCharType()) { |
| 3735 | // char pointer types should be encoded as '*' unless it is a |
| 3736 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3737 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3738 | S += '*'; |
| 3739 | return; |
| 3740 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3741 | } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) { |
Steve Naroff | 9533a7f | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 3742 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 3743 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 3744 | S += '#'; |
| 3745 | return; |
| 3746 | } |
| 3747 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 3748 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 3749 | S += '@'; |
| 3750 | return; |
| 3751 | } |
| 3752 | // fall through... |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3753 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3754 | S += '^'; |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3755 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 3756 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3757 | getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3758 | NULL); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3759 | return; |
| 3760 | } |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 3761 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3762 | if (const ArrayType *AT = |
| 3763 | // Ignore type qualifiers etc. |
| 3764 | dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3765 | if (isa<IncompleteArrayType>(AT)) { |
| 3766 | // Incomplete arrays are encoded as a pointer to the array element. |
| 3767 | S += '^'; |
| 3768 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3769 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3770 | false, ExpandStructures, FD); |
| 3771 | } else { |
| 3772 | S += '['; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3773 | |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3774 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 3775 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 3776 | else { |
| 3777 | //Variable length arrays are encoded as a regular array with 0 elements. |
| 3778 | assert(isa<VariableArrayType>(AT) && "Unknown array type!"); |
| 3779 | S += '0'; |
| 3780 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3781 | |
| 3782 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3783 | false, ExpandStructures, FD); |
| 3784 | S += ']'; |
| 3785 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3786 | return; |
| 3787 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3788 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3789 | if (T->getAs<FunctionType>()) { |
Anders Carlsson | c0a87b7 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 3790 | S += '?'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3791 | return; |
| 3792 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3793 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3794 | if (const RecordType *RTy = T->getAs<RecordType>()) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3795 | RecordDecl *RDecl = RTy->getDecl(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3796 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 3797 | // Anonymous structures print as '?' |
| 3798 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 3799 | S += II->getName(); |
Fariborz Jahanian | 6fb9439 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 3800 | if (ClassTemplateSpecializationDecl *Spec |
| 3801 | = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) { |
| 3802 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 3803 | std::string TemplateArgsStr |
| 3804 | = TemplateSpecializationType::PrintTemplateArgumentList( |
| 3805 | TemplateArgs.getFlatArgumentList(), |
| 3806 | TemplateArgs.flat_size(), |
| 3807 | (*this).PrintingPolicy); |
| 3808 | |
| 3809 | S += TemplateArgsStr; |
| 3810 | } |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 3811 | } else { |
| 3812 | S += '?'; |
| 3813 | } |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3814 | if (ExpandStructures) { |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3815 | S += '='; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3816 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 3817 | FieldEnd = RDecl->field_end(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3818 | Field != FieldEnd; ++Field) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3819 | if (FD) { |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3820 | S += '"'; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3821 | S += Field->getNameAsString(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3822 | S += '"'; |
| 3823 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3824 | |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3825 | // Special case bit-fields. |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3826 | if (Field->isBitField()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3827 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3828 | (*Field)); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3829 | } else { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3830 | QualType qt = Field->getType(); |
| 3831 | getLegacyIntegralTypeEncoding(qt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3832 | getObjCEncodingForTypeImpl(qt, S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3833 | FD); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3834 | } |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3835 | } |
Fariborz Jahanian | 6de88a8 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 3836 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3837 | S += RDecl->isUnion() ? ')' : '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3838 | return; |
| 3839 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3840 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3841 | if (T->isEnumeralType()) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3842 | if (FD && FD->isBitField()) |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3843 | EncodeBitField(this, S, T, FD); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3844 | else |
| 3845 | S += 'i'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3846 | return; |
| 3847 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3848 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3849 | if (T->isBlockPointerType()) { |
Steve Naroff | 21a98b1 | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 3850 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3851 | return; |
| 3852 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3853 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3854 | // Ignore protocol qualifiers when mangling at this level. |
| 3855 | if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>()) |
| 3856 | T = OT->getBaseType(); |
| 3857 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3858 | if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3859 | // @encode(class_name) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3860 | ObjCInterfaceDecl *OI = OIT->getDecl(); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3861 | S += '{'; |
| 3862 | const IdentifierInfo *II = OI->getIdentifier(); |
| 3863 | S += II->getName(); |
| 3864 | S += '='; |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3865 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3866 | CollectObjCIvars(OI, RecFields); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3867 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3868 | if (RecFields[i]->isBitField()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3869 | getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3870 | RecFields[i]); |
| 3871 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3872 | getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3873 | FD); |
| 3874 | } |
| 3875 | S += '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3876 | return; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3877 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3878 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3879 | if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3880 | if (OPT->isObjCIdType()) { |
| 3881 | S += '@'; |
| 3882 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3883 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3884 | |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 3885 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
| 3886 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
| 3887 | // Since this is a binary compatibility issue, need to consult with runtime |
| 3888 | // folks. Fortunately, this is a *very* obsure construct. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3889 | S += '#'; |
| 3890 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3891 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3892 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3893 | if (OPT->isObjCQualifiedIdType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3894 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3895 | ExpandPointedToStructures, |
| 3896 | ExpandStructures, FD); |
| 3897 | if (FD || EncodingProperty) { |
| 3898 | // Note that we do extended encoding of protocol qualifer list |
| 3899 | // Only when doing ivar or property encoding. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3900 | S += '"'; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3901 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 3902 | E = OPT->qual_end(); I != E; ++I) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3903 | S += '<'; |
| 3904 | S += (*I)->getNameAsString(); |
| 3905 | S += '>'; |
| 3906 | } |
| 3907 | S += '"'; |
| 3908 | } |
| 3909 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3910 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3911 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3912 | QualType PointeeTy = OPT->getPointeeType(); |
| 3913 | if (!EncodingProperty && |
| 3914 | isa<TypedefType>(PointeeTy.getTypePtr())) { |
| 3915 | // Another historical/compatibility reason. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3916 | // We encode the underlying type which comes out as |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3917 | // {...}; |
| 3918 | S += '^'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3919 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 3920 | false, ExpandPointedToStructures, |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3921 | NULL); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3922 | return; |
| 3923 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3924 | |
| 3925 | S += '@'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 3926 | if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3927 | S += '"'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 3928 | S += OPT->getInterfaceDecl()->getIdentifier()->getName(); |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3929 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 3930 | E = OPT->qual_end(); I != E; ++I) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3931 | S += '<'; |
| 3932 | S += (*I)->getNameAsString(); |
| 3933 | S += '>'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3934 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3935 | S += '"'; |
| 3936 | } |
| 3937 | return; |
| 3938 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3939 | |
John McCall | 532ec7b | 2010-05-17 23:56:34 +0000 | [diff] [blame] | 3940 | // gcc just blithely ignores member pointers. |
| 3941 | // TODO: maybe there should be a mangling for these |
| 3942 | if (T->getAs<MemberPointerType>()) |
| 3943 | return; |
| 3944 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3945 | assert(0 && "@encode for type not implemented!"); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3946 | } |
| 3947 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3948 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3949 | std::string& S) const { |
| 3950 | if (QT & Decl::OBJC_TQ_In) |
| 3951 | S += 'n'; |
| 3952 | if (QT & Decl::OBJC_TQ_Inout) |
| 3953 | S += 'N'; |
| 3954 | if (QT & Decl::OBJC_TQ_Out) |
| 3955 | S += 'o'; |
| 3956 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 3957 | S += 'O'; |
| 3958 | if (QT & Decl::OBJC_TQ_Byref) |
| 3959 | S += 'R'; |
| 3960 | if (QT & Decl::OBJC_TQ_Oneway) |
| 3961 | S += 'V'; |
| 3962 | } |
| 3963 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3964 | void ASTContext::setBuiltinVaListType(QualType T) { |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 3965 | assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3966 | |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 3967 | BuiltinVaListType = T; |
| 3968 | } |
| 3969 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3970 | void ASTContext::setObjCIdType(QualType T) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3971 | ObjCIdTypedefType = T; |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 3972 | } |
| 3973 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3974 | void ASTContext::setObjCSelType(QualType T) { |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 3975 | ObjCSelTypedefType = T; |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 3976 | } |
| 3977 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3978 | void ASTContext::setObjCProtoType(QualType QT) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3979 | ObjCProtoType = QT; |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 3980 | } |
| 3981 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3982 | void ASTContext::setObjCClassType(QualType T) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3983 | ObjCClassTypedefType = T; |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 3984 | } |
| 3985 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3986 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3987 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 3988 | "'NSConstantString' type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3989 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3990 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 3991 | } |
| 3992 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 3993 | /// \brief Retrieve the template name that corresponds to a non-empty |
| 3994 | /// lookup. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3995 | TemplateName ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin, |
| 3996 | UnresolvedSetIterator End) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 3997 | unsigned size = End - Begin; |
| 3998 | assert(size > 1 && "set is not overloaded!"); |
| 3999 | |
| 4000 | void *memory = Allocate(sizeof(OverloadedTemplateStorage) + |
| 4001 | size * sizeof(FunctionTemplateDecl*)); |
| 4002 | OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size); |
| 4003 | |
| 4004 | NamedDecl **Storage = OT->getStorage(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4005 | for (UnresolvedSetIterator I = Begin; I != End; ++I) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4006 | NamedDecl *D = *I; |
| 4007 | assert(isa<FunctionTemplateDecl>(D) || |
| 4008 | (isa<UsingShadowDecl>(D) && |
| 4009 | isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))); |
| 4010 | *Storage++ = D; |
| 4011 | } |
| 4012 | |
| 4013 | return TemplateName(OT); |
| 4014 | } |
| 4015 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4016 | /// \brief Retrieve the template name that represents a qualified |
| 4017 | /// template name such as \c std::vector. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4018 | TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4019 | bool TemplateKeyword, |
| 4020 | TemplateDecl *Template) { |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4021 | // FIXME: Canonicalization? |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4022 | llvm::FoldingSetNodeID ID; |
| 4023 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 4024 | |
| 4025 | void *InsertPos = 0; |
| 4026 | QualifiedTemplateName *QTN = |
| 4027 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4028 | if (!QTN) { |
| 4029 | QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); |
| 4030 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 4031 | } |
| 4032 | |
| 4033 | return TemplateName(QTN); |
| 4034 | } |
| 4035 | |
| 4036 | /// \brief Retrieve the template name that represents a dependent |
| 4037 | /// template name such as \c MetaFun::template apply. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4038 | TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4039 | const IdentifierInfo *Name) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4040 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 4041 | "Nested name specifier must be dependent"); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4042 | |
| 4043 | llvm::FoldingSetNodeID ID; |
| 4044 | DependentTemplateName::Profile(ID, NNS, Name); |
| 4045 | |
| 4046 | void *InsertPos = 0; |
| 4047 | DependentTemplateName *QTN = |
| 4048 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4049 | |
| 4050 | if (QTN) |
| 4051 | return TemplateName(QTN); |
| 4052 | |
| 4053 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 4054 | if (CanonNNS == NNS) { |
| 4055 | QTN = new (*this,4) DependentTemplateName(NNS, Name); |
| 4056 | } else { |
| 4057 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
| 4058 | QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4059 | DependentTemplateName *CheckQTN = |
| 4060 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4061 | assert(!CheckQTN && "Dependent type name canonicalization broken"); |
| 4062 | (void)CheckQTN; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4063 | } |
| 4064 | |
| 4065 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 4066 | return TemplateName(QTN); |
| 4067 | } |
| 4068 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4069 | /// \brief Retrieve the template name that represents a dependent |
| 4070 | /// template name such as \c MetaFun::template operator+. |
| 4071 | TemplateName |
| 4072 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
| 4073 | OverloadedOperatorKind Operator) { |
| 4074 | assert((!NNS || NNS->isDependent()) && |
| 4075 | "Nested name specifier must be dependent"); |
| 4076 | |
| 4077 | llvm::FoldingSetNodeID ID; |
| 4078 | DependentTemplateName::Profile(ID, NNS, Operator); |
| 4079 | |
| 4080 | void *InsertPos = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4081 | DependentTemplateName *QTN |
| 4082 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4083 | |
| 4084 | if (QTN) |
| 4085 | return TemplateName(QTN); |
| 4086 | |
| 4087 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 4088 | if (CanonNNS == NNS) { |
| 4089 | QTN = new (*this,4) DependentTemplateName(NNS, Operator); |
| 4090 | } else { |
| 4091 | TemplateName Canon = getDependentTemplateName(CanonNNS, Operator); |
| 4092 | QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4093 | |
| 4094 | DependentTemplateName *CheckQTN |
| 4095 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4096 | assert(!CheckQTN && "Dependent template name canonicalization broken"); |
| 4097 | (void)CheckQTN; |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4098 | } |
| 4099 | |
| 4100 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 4101 | return TemplateName(QTN); |
| 4102 | } |
| 4103 | |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4104 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | d934112 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 4105 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 4106 | /// is actually a value of type @c TargetInfo::IntType. |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 4107 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4108 | switch (Type) { |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 4109 | case TargetInfo::NoInt: return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4110 | case TargetInfo::SignedShort: return ShortTy; |
| 4111 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 4112 | case TargetInfo::SignedInt: return IntTy; |
| 4113 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 4114 | case TargetInfo::SignedLong: return LongTy; |
| 4115 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 4116 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 4117 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 4118 | } |
| 4119 | |
| 4120 | assert(false && "Unhandled TargetInfo::IntType value"); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 4121 | return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4122 | } |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 4123 | |
| 4124 | //===----------------------------------------------------------------------===// |
| 4125 | // Type Predicates. |
| 4126 | //===----------------------------------------------------------------------===// |
| 4127 | |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4128 | /// isObjCNSObjectType - Return true if this is an NSObject object using |
| 4129 | /// NSObject attribute on a c-style pointer type. |
| 4130 | /// FIXME - Make it work directly on types. |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 4131 | /// FIXME: Move to Type. |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4132 | /// |
| 4133 | bool ASTContext::isObjCNSObjectType(QualType Ty) const { |
| 4134 | if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) { |
| 4135 | if (TypedefDecl *TD = TDT->getDecl()) |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 4136 | if (TD->getAttr<ObjCNSObjectAttr>()) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4137 | return true; |
| 4138 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4139 | return false; |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4140 | } |
| 4141 | |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4142 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 4143 | /// garbage collection attribute. |
| 4144 | /// |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4145 | Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const { |
| 4146 | Qualifiers::GC GCAttrs = Qualifiers::GCNone; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4147 | if (getLangOptions().ObjC1 && |
| 4148 | getLangOptions().getGCMode() != LangOptions::NonGC) { |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 4149 | GCAttrs = Ty.getObjCGCAttr(); |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4150 | // Default behavious under objective-c's gc is for objective-c pointers |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4151 | // (or pointers to them) be treated as though they were declared |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 4152 | // as __strong. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4153 | if (GCAttrs == Qualifiers::GCNone) { |
Fariborz Jahanian | 75212ee | 2009-09-10 23:38:45 +0000 | [diff] [blame] | 4154 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4155 | GCAttrs = Qualifiers::Strong; |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 4156 | else if (Ty->isPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4157 | return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 4158 | } |
Fariborz Jahanian | c211218 | 2009-04-11 00:00:54 +0000 | [diff] [blame] | 4159 | // Non-pointers have none gc'able attribute regardless of the attribute |
| 4160 | // set on them. |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 4161 | else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4162 | return Qualifiers::GCNone; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4163 | } |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 4164 | return GCAttrs; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4165 | } |
| 4166 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4167 | //===----------------------------------------------------------------------===// |
| 4168 | // Type Compatibility Testing |
| 4169 | //===----------------------------------------------------------------------===// |
Chris Lattner | 770951b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 4170 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4171 | /// areCompatVectorTypes - Return true if the two specified vector types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4172 | /// compatible. |
| 4173 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 4174 | const VectorType *RHS) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 4175 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4176 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4177 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4178 | } |
| 4179 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 4180 | bool ASTContext::areCompatibleVectorTypes(QualType FirstVec, |
| 4181 | QualType SecondVec) { |
| 4182 | assert(FirstVec->isVectorType() && "FirstVec should be a vector type"); |
| 4183 | assert(SecondVec->isVectorType() && "SecondVec should be a vector type"); |
| 4184 | |
| 4185 | if (hasSameUnqualifiedType(FirstVec, SecondVec)) |
| 4186 | return true; |
| 4187 | |
| 4188 | // AltiVec vectors types are identical to equivalent GCC vector types |
| 4189 | const VectorType *First = FirstVec->getAs<VectorType>(); |
| 4190 | const VectorType *Second = SecondVec->getAs<VectorType>(); |
| 4191 | if ((((First->getAltiVecSpecific() == VectorType::AltiVec) && |
| 4192 | (Second->getAltiVecSpecific() == VectorType::NotAltiVec)) || |
| 4193 | ((First->getAltiVecSpecific() == VectorType::NotAltiVec) && |
| 4194 | (Second->getAltiVecSpecific() == VectorType::AltiVec))) && |
| 4195 | hasSameType(First->getElementType(), Second->getElementType()) && |
| 4196 | (First->getNumElements() == Second->getNumElements())) |
| 4197 | return true; |
| 4198 | |
| 4199 | return false; |
| 4200 | } |
| 4201 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4202 | //===----------------------------------------------------------------------===// |
| 4203 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 4204 | //===----------------------------------------------------------------------===// |
| 4205 | |
| 4206 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 4207 | /// inheritance hierarchy of 'rProto'. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 4208 | bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 4209 | ObjCProtocolDecl *rProto) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4210 | if (lProto == rProto) |
| 4211 | return true; |
| 4212 | for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), |
| 4213 | E = rProto->protocol_end(); PI != E; ++PI) |
| 4214 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 4215 | return true; |
| 4216 | return false; |
| 4217 | } |
| 4218 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4219 | /// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...> |
| 4220 | /// return true if lhs's protocols conform to rhs's protocol; false |
| 4221 | /// otherwise. |
| 4222 | bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) { |
| 4223 | if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType()) |
| 4224 | return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false); |
| 4225 | return false; |
| 4226 | } |
| 4227 | |
Fariborz Jahanian | a8f8dac | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 4228 | /// ObjCQualifiedClassTypesAreCompatible - compare Class<p,...> and |
| 4229 | /// Class<p1, ...>. |
| 4230 | bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs, |
| 4231 | QualType rhs) { |
| 4232 | const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>(); |
| 4233 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
| 4234 | assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible"); |
| 4235 | |
| 4236 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4237 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4238 | bool match = false; |
| 4239 | ObjCProtocolDecl *lhsProto = *I; |
| 4240 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 4241 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 4242 | ObjCProtocolDecl *rhsProto = *J; |
| 4243 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) { |
| 4244 | match = true; |
| 4245 | break; |
| 4246 | } |
| 4247 | } |
| 4248 | if (!match) |
| 4249 | return false; |
| 4250 | } |
| 4251 | return true; |
| 4252 | } |
| 4253 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4254 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 4255 | /// ObjCQualifiedIDType. |
| 4256 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 4257 | bool compare) { |
| 4258 | // Allow id<P..> and an 'id' or void* type in all cases. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4259 | if (lhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4260 | lhs->isObjCIdType() || lhs->isObjCClassType()) |
| 4261 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4262 | else if (rhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4263 | rhs->isObjCIdType() || rhs->isObjCClassType()) |
| 4264 | return true; |
| 4265 | |
| 4266 | if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4267 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4268 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4269 | if (!rhsOPT) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4270 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4271 | if (rhsOPT->qual_empty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4272 | // If the RHS is a unqualified interface pointer "NSString*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4273 | // make sure we check the class hierarchy. |
| 4274 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 4275 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4276 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4277 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4278 | // see if static class implements all of id's protocols, directly or |
| 4279 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 4280 | if (!rhsID->ClassImplementsProtocol(*I, true)) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4281 | return false; |
| 4282 | } |
| 4283 | } |
| 4284 | // If there are no qualifiers and no interface, we have an 'id'. |
| 4285 | return true; |
| 4286 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4287 | // Both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4288 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4289 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4290 | ObjCProtocolDecl *lhsProto = *I; |
| 4291 | bool match = false; |
| 4292 | |
| 4293 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4294 | // see if static class implements all of id's protocols, directly or |
| 4295 | // through its super class and categories. |
| 4296 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 4297 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 4298 | ObjCProtocolDecl *rhsProto = *J; |
| 4299 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 4300 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 4301 | match = true; |
| 4302 | break; |
| 4303 | } |
| 4304 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4305 | // If the RHS is a qualified interface pointer "NSString<P>*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4306 | // make sure we check the class hierarchy. |
| 4307 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 4308 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4309 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4310 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4311 | // see if static class implements all of id's protocols, directly or |
| 4312 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 4313 | if (rhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4314 | match = true; |
| 4315 | break; |
| 4316 | } |
| 4317 | } |
| 4318 | } |
| 4319 | if (!match) |
| 4320 | return false; |
| 4321 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4322 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4323 | return true; |
| 4324 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4325 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4326 | const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 4327 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 4328 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4329 | if (const ObjCObjectPointerType *lhsOPT = |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4330 | lhs->getAsObjCInterfacePointerType()) { |
| 4331 | if (lhsOPT->qual_empty()) { |
| 4332 | bool match = false; |
| 4333 | if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { |
| 4334 | for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(), |
| 4335 | E = rhsQID->qual_end(); I != E; ++I) { |
Fariborz Jahanian | d1909bb | 2010-08-09 18:21:43 +0000 | [diff] [blame] | 4336 | // when comparing an id<P> on rhs with a static type on lhs, |
| 4337 | // static class must implement all of id's protocols directly or |
| 4338 | // indirectly through its super class. |
Fariborz Jahanian | 192b146 | 2010-08-12 20:46:12 +0000 | [diff] [blame^] | 4339 | if (lhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4340 | match = true; |
| 4341 | break; |
| 4342 | } |
| 4343 | } |
| 4344 | if (!match) |
| 4345 | return false; |
| 4346 | } |
| 4347 | return true; |
| 4348 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4349 | // Both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4350 | for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(), |
| 4351 | E = lhsOPT->qual_end(); I != E; ++I) { |
| 4352 | ObjCProtocolDecl *lhsProto = *I; |
| 4353 | bool match = false; |
| 4354 | |
| 4355 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4356 | // see if static class implements all of id's protocols, directly or |
| 4357 | // through its super class and categories. |
| 4358 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 4359 | E = rhsQID->qual_end(); J != E; ++J) { |
| 4360 | ObjCProtocolDecl *rhsProto = *J; |
| 4361 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 4362 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 4363 | match = true; |
| 4364 | break; |
| 4365 | } |
| 4366 | } |
| 4367 | if (!match) |
| 4368 | return false; |
| 4369 | } |
| 4370 | return true; |
| 4371 | } |
| 4372 | return false; |
| 4373 | } |
| 4374 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4375 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4376 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 4377 | /// protocol qualifiers on the LHS or RHS. |
| 4378 | /// |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4379 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 4380 | const ObjCObjectPointerType *RHSOPT) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4381 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 4382 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 4383 | |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 4384 | // 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] | 4385 | if (LHS->isObjCUnqualifiedIdOrClass() || |
| 4386 | RHS->isObjCUnqualifiedIdOrClass()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4387 | return true; |
| 4388 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4389 | if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4390 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 4391 | QualType(RHSOPT,0), |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4392 | false); |
Fariborz Jahanian | a8f8dac | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 4393 | |
| 4394 | if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass()) |
| 4395 | return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0), |
| 4396 | QualType(RHSOPT,0)); |
| 4397 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4398 | // If we have 2 user-defined types, fall into that path. |
| 4399 | if (LHS->getInterface() && RHS->getInterface()) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4400 | return canAssignObjCInterfaces(LHS, RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4401 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4402 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4403 | } |
| 4404 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4405 | /// canAssignObjCInterfacesInBlockPointer - This routine is specifically written |
| 4406 | /// for providing type-safty for objective-c pointers used to pass/return |
| 4407 | /// arguments in block literals. When passed as arguments, passing 'A*' where |
| 4408 | /// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is |
| 4409 | /// not OK. For the return type, the opposite is not OK. |
| 4410 | bool ASTContext::canAssignObjCInterfacesInBlockPointer( |
| 4411 | const ObjCObjectPointerType *LHSOPT, |
| 4412 | const ObjCObjectPointerType *RHSOPT) { |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 4413 | if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4414 | return true; |
| 4415 | |
| 4416 | if (LHSOPT->isObjCBuiltinType()) { |
| 4417 | return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType(); |
| 4418 | } |
| 4419 | |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 4420 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4421 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 4422 | QualType(RHSOPT,0), |
| 4423 | false); |
| 4424 | |
| 4425 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 4426 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 4427 | if (LHS && RHS) { // We have 2 user-defined types. |
| 4428 | if (LHS != RHS) { |
| 4429 | if (LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
| 4430 | return false; |
| 4431 | if (RHS->getDecl()->isSuperClassOf(LHS->getDecl())) |
| 4432 | return true; |
| 4433 | } |
| 4434 | else |
| 4435 | return true; |
| 4436 | } |
| 4437 | return false; |
| 4438 | } |
| 4439 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4440 | /// getIntersectionOfProtocols - This routine finds the intersection of set |
| 4441 | /// of protocols inherited from two distinct objective-c pointer objects. |
| 4442 | /// It is used to build composite qualifier list of the composite type of |
| 4443 | /// the conditional expression involving two objective-c pointer objects. |
| 4444 | static |
| 4445 | void getIntersectionOfProtocols(ASTContext &Context, |
| 4446 | const ObjCObjectPointerType *LHSOPT, |
| 4447 | const ObjCObjectPointerType *RHSOPT, |
| 4448 | llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) { |
| 4449 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4450 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 4451 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 4452 | assert(LHS->getInterface() && "LHS must have an interface base"); |
| 4453 | assert(RHS->getInterface() && "RHS must have an interface base"); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4454 | |
| 4455 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet; |
| 4456 | unsigned LHSNumProtocols = LHS->getNumProtocols(); |
| 4457 | if (LHSNumProtocols > 0) |
| 4458 | InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end()); |
| 4459 | else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 4460 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4461 | Context.CollectInheritedProtocols(LHS->getInterface(), |
| 4462 | LHSInheritedProtocols); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4463 | InheritedProtocolSet.insert(LHSInheritedProtocols.begin(), |
| 4464 | LHSInheritedProtocols.end()); |
| 4465 | } |
| 4466 | |
| 4467 | unsigned RHSNumProtocols = RHS->getNumProtocols(); |
| 4468 | if (RHSNumProtocols > 0) { |
Dan Gohman | cb421fa | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 4469 | ObjCProtocolDecl **RHSProtocols = |
| 4470 | const_cast<ObjCProtocolDecl **>(RHS->qual_begin()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4471 | for (unsigned i = 0; i < RHSNumProtocols; ++i) |
| 4472 | if (InheritedProtocolSet.count(RHSProtocols[i])) |
| 4473 | IntersectionOfProtocols.push_back(RHSProtocols[i]); |
| 4474 | } |
| 4475 | else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 4476 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4477 | Context.CollectInheritedProtocols(RHS->getInterface(), |
| 4478 | RHSInheritedProtocols); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 4479 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 4480 | RHSInheritedProtocols.begin(), |
| 4481 | E = RHSInheritedProtocols.end(); I != E; ++I) |
| 4482 | if (InheritedProtocolSet.count((*I))) |
| 4483 | IntersectionOfProtocols.push_back((*I)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4484 | } |
| 4485 | } |
| 4486 | |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4487 | /// areCommonBaseCompatible - Returns common base class of the two classes if |
| 4488 | /// one found. Note that this is O'2 algorithm. But it will be called as the |
| 4489 | /// last type comparison in a ?-exp of ObjC pointer types before a |
| 4490 | /// warning is issued. So, its invokation is extremely rare. |
| 4491 | QualType ASTContext::areCommonBaseCompatible( |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4492 | const ObjCObjectPointerType *Lptr, |
| 4493 | const ObjCObjectPointerType *Rptr) { |
| 4494 | const ObjCObjectType *LHS = Lptr->getObjectType(); |
| 4495 | const ObjCObjectType *RHS = Rptr->getObjectType(); |
| 4496 | const ObjCInterfaceDecl* LDecl = LHS->getInterface(); |
| 4497 | const ObjCInterfaceDecl* RDecl = RHS->getInterface(); |
| 4498 | if (!LDecl || !RDecl) |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4499 | return QualType(); |
| 4500 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4501 | while ((LDecl = LDecl->getSuperClass())) { |
| 4502 | LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4503 | if (canAssignObjCInterfaces(LHS, RHS)) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4504 | llvm::SmallVector<ObjCProtocolDecl *, 8> Protocols; |
| 4505 | getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols); |
| 4506 | |
| 4507 | QualType Result = QualType(LHS, 0); |
| 4508 | if (!Protocols.empty()) |
| 4509 | Result = getObjCObjectType(Result, Protocols.data(), Protocols.size()); |
| 4510 | Result = getObjCObjectPointerType(Result); |
| 4511 | return Result; |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4512 | } |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4513 | } |
| 4514 | |
| 4515 | return QualType(); |
| 4516 | } |
| 4517 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4518 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS, |
| 4519 | const ObjCObjectType *RHS) { |
| 4520 | assert(LHS->getInterface() && "LHS is not an interface type"); |
| 4521 | assert(RHS->getInterface() && "RHS is not an interface type"); |
| 4522 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4523 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 4524 | // the LHS. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4525 | if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface())) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4526 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4527 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4528 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 4529 | // protocol qualified at all, then we are good. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4530 | if (LHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4531 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4532 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4533 | // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it |
| 4534 | // isn't a superset. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4535 | if (RHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4536 | return true; // FIXME: should return false! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4537 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4538 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 4539 | LHSPE = LHS->qual_end(); |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4540 | LHSPI != LHSPE; LHSPI++) { |
| 4541 | bool RHSImplementsProtocol = false; |
| 4542 | |
| 4543 | // If the RHS doesn't implement the protocol on the left, the types |
| 4544 | // are incompatible. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4545 | for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(), |
| 4546 | RHSPE = RHS->qual_end(); |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 4547 | RHSPI != RHSPE; RHSPI++) { |
| 4548 | if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) { |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4549 | RHSImplementsProtocol = true; |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 4550 | break; |
| 4551 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4552 | } |
| 4553 | // FIXME: For better diagnostics, consider passing back the protocol name. |
| 4554 | if (!RHSImplementsProtocol) |
| 4555 | return false; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4556 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4557 | // The RHS implements all protocols listed on the LHS. |
| 4558 | return true; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4559 | } |
| 4560 | |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4561 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 4562 | // get the "pointed to" types |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4563 | const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
| 4564 | const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4565 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4566 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4567 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4568 | |
| 4569 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 4570 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4571 | } |
| 4572 | |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4573 | bool ASTContext::canBindObjCObjectType(QualType To, QualType From) { |
| 4574 | return canAssignObjCInterfaces( |
| 4575 | getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(), |
| 4576 | getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>()); |
| 4577 | } |
| 4578 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4579 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4580 | /// both shall have the identically qualified version of a compatible type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4581 | /// 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] | 4582 | /// same. See 6.7.[2,3,5] for additional rules. |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4583 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS, |
| 4584 | bool CompareUnqualified) { |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 4585 | if (getLangOptions().CPlusPlus) |
| 4586 | return hasSameType(LHS, RHS); |
| 4587 | |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4588 | return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4589 | } |
| 4590 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4591 | bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) { |
| 4592 | return !mergeTypes(LHS, RHS, true).isNull(); |
| 4593 | } |
| 4594 | |
| 4595 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs, |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4596 | bool OfBlockPointer, |
| 4597 | bool Unqualified) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4598 | const FunctionType *lbase = lhs->getAs<FunctionType>(); |
| 4599 | const FunctionType *rbase = rhs->getAs<FunctionType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4600 | const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 4601 | const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4602 | bool allLTypes = true; |
| 4603 | bool allRTypes = true; |
| 4604 | |
| 4605 | // Check return type |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4606 | QualType retType; |
| 4607 | if (OfBlockPointer) |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4608 | retType = mergeTypes(rbase->getResultType(), lbase->getResultType(), true, |
| 4609 | Unqualified); |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4610 | else |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4611 | retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), |
| 4612 | false, Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4613 | if (retType.isNull()) return QualType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4614 | |
| 4615 | if (Unqualified) |
| 4616 | retType = retType.getUnqualifiedType(); |
| 4617 | |
| 4618 | CanQualType LRetType = getCanonicalType(lbase->getResultType()); |
| 4619 | CanQualType RRetType = getCanonicalType(rbase->getResultType()); |
| 4620 | if (Unqualified) { |
| 4621 | LRetType = LRetType.getUnqualifiedType(); |
| 4622 | RRetType = RRetType.getUnqualifiedType(); |
| 4623 | } |
| 4624 | |
| 4625 | if (getCanonicalType(retType) != LRetType) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4626 | allLTypes = false; |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4627 | if (getCanonicalType(retType) != RRetType) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4628 | allRTypes = false; |
Daniel Dunbar | 6a15c85 | 2010-04-28 16:20:58 +0000 | [diff] [blame] | 4629 | // FIXME: double check this |
| 4630 | // FIXME: should we error if lbase->getRegParmAttr() != 0 && |
| 4631 | // rbase->getRegParmAttr() != 0 && |
| 4632 | // lbase->getRegParmAttr() != rbase->getRegParmAttr()? |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 4633 | FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo(); |
| 4634 | FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo(); |
Daniel Dunbar | 6a15c85 | 2010-04-28 16:20:58 +0000 | [diff] [blame] | 4635 | unsigned RegParm = lbaseInfo.getRegParm() == 0 ? rbaseInfo.getRegParm() : |
| 4636 | lbaseInfo.getRegParm(); |
| 4637 | bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn(); |
| 4638 | if (NoReturn != lbaseInfo.getNoReturn() || |
| 4639 | RegParm != lbaseInfo.getRegParm()) |
| 4640 | allLTypes = false; |
| 4641 | if (NoReturn != rbaseInfo.getNoReturn() || |
| 4642 | RegParm != rbaseInfo.getRegParm()) |
| 4643 | allRTypes = false; |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 4644 | CallingConv lcc = lbaseInfo.getCC(); |
| 4645 | CallingConv rcc = rbaseInfo.getCC(); |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 4646 | // Compatible functions must have compatible calling conventions |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4647 | if (!isSameCallConv(lcc, rcc)) |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 4648 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4649 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4650 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 4651 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 4652 | "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4653 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 4654 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 4655 | |
| 4656 | // Compatible functions must have the same number of arguments |
| 4657 | if (lproto_nargs != rproto_nargs) |
| 4658 | return QualType(); |
| 4659 | |
| 4660 | // Variadic and non-variadic functions aren't compatible |
| 4661 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 4662 | return QualType(); |
| 4663 | |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 4664 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 4665 | return QualType(); |
| 4666 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4667 | // Check argument compatibility |
| 4668 | llvm::SmallVector<QualType, 10> types; |
| 4669 | for (unsigned i = 0; i < lproto_nargs; i++) { |
| 4670 | QualType largtype = lproto->getArgType(i).getUnqualifiedType(); |
| 4671 | QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4672 | QualType argtype = mergeTypes(largtype, rargtype, OfBlockPointer, |
| 4673 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4674 | if (argtype.isNull()) return QualType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4675 | |
| 4676 | if (Unqualified) |
| 4677 | argtype = argtype.getUnqualifiedType(); |
| 4678 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4679 | types.push_back(argtype); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4680 | if (Unqualified) { |
| 4681 | largtype = largtype.getUnqualifiedType(); |
| 4682 | rargtype = rargtype.getUnqualifiedType(); |
| 4683 | } |
| 4684 | |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4685 | if (getCanonicalType(argtype) != getCanonicalType(largtype)) |
| 4686 | allLTypes = false; |
| 4687 | if (getCanonicalType(argtype) != getCanonicalType(rargtype)) |
| 4688 | allRTypes = false; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4689 | } |
| 4690 | if (allLTypes) return lhs; |
| 4691 | if (allRTypes) return rhs; |
| 4692 | return getFunctionType(retType, types.begin(), types.size(), |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 4693 | lproto->isVariadic(), lproto->getTypeQuals(), |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 4694 | false, false, 0, 0, |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 4695 | FunctionType::ExtInfo(NoReturn, RegParm, lcc)); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4696 | } |
| 4697 | |
| 4698 | if (lproto) allRTypes = false; |
| 4699 | if (rproto) allLTypes = false; |
| 4700 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4701 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4702 | if (proto) { |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 4703 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4704 | if (proto->isVariadic()) return QualType(); |
| 4705 | // Check that the types are compatible with the types that |
| 4706 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 4707 | // The only types actually affected are promotable integer |
| 4708 | // types and floats, which would be passed as a different |
| 4709 | // type depending on whether the prototype is visible. |
| 4710 | unsigned proto_nargs = proto->getNumArgs(); |
| 4711 | for (unsigned i = 0; i < proto_nargs; ++i) { |
| 4712 | QualType argTy = proto->getArgType(i); |
Douglas Gregor | b0f8eac | 2010-02-03 19:27:29 +0000 | [diff] [blame] | 4713 | |
| 4714 | // Look at the promotion type of enum types, since that is the type used |
| 4715 | // to pass enum values. |
| 4716 | if (const EnumType *Enum = argTy->getAs<EnumType>()) |
| 4717 | argTy = Enum->getDecl()->getPromotionType(); |
| 4718 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4719 | if (argTy->isPromotableIntegerType() || |
| 4720 | getCanonicalType(argTy).getUnqualifiedType() == FloatTy) |
| 4721 | return QualType(); |
| 4722 | } |
| 4723 | |
| 4724 | if (allLTypes) return lhs; |
| 4725 | if (allRTypes) return rhs; |
| 4726 | return getFunctionType(retType, proto->arg_type_begin(), |
Mike Stump | 2d3c191 | 2009-07-27 00:44:23 +0000 | [diff] [blame] | 4727 | proto->getNumArgs(), proto->isVariadic(), |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 4728 | proto->getTypeQuals(), |
| 4729 | false, false, 0, 0, |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 4730 | FunctionType::ExtInfo(NoReturn, RegParm, lcc)); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4731 | } |
| 4732 | |
| 4733 | if (allLTypes) return lhs; |
| 4734 | if (allRTypes) return rhs; |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 4735 | FunctionType::ExtInfo Info(NoReturn, RegParm, lcc); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 4736 | return getFunctionNoProtoType(retType, Info); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4737 | } |
| 4738 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4739 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4740 | bool OfBlockPointer, |
| 4741 | bool Unqualified) { |
Bill Wendling | 43d6975 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 4742 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 4743 | // type is adjusted to "T" prior to any further analysis, the expression |
| 4744 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4745 | // expression is an lvalue unless the reference is an rvalue reference and |
| 4746 | // the expression is a function call (possibly inside parentheses). |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 4747 | assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?"); |
| 4748 | assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?"); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4749 | |
| 4750 | if (Unqualified) { |
| 4751 | LHS = LHS.getUnqualifiedType(); |
| 4752 | RHS = RHS.getUnqualifiedType(); |
| 4753 | } |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 4754 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4755 | QualType LHSCan = getCanonicalType(LHS), |
| 4756 | RHSCan = getCanonicalType(RHS); |
| 4757 | |
| 4758 | // If two types are identical, they are compatible. |
| 4759 | if (LHSCan == RHSCan) |
| 4760 | return LHS; |
| 4761 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4762 | // If the qualifiers are different, the types aren't compatible... mostly. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4763 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 4764 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4765 | if (LQuals != RQuals) { |
| 4766 | // If any of these qualifiers are different, we have a type |
| 4767 | // mismatch. |
| 4768 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 4769 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 4770 | return QualType(); |
| 4771 | |
| 4772 | // Exactly one GC qualifier difference is allowed: __strong is |
| 4773 | // okay if the other type has no GC qualifier but is an Objective |
| 4774 | // C object pointer (i.e. implicitly strong by default). We fix |
| 4775 | // this by pretending that the unqualified type was actually |
| 4776 | // qualified __strong. |
| 4777 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 4778 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 4779 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 4780 | |
| 4781 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 4782 | return QualType(); |
| 4783 | |
| 4784 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
| 4785 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); |
| 4786 | } |
| 4787 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
| 4788 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); |
| 4789 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4790 | return QualType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4791 | } |
| 4792 | |
| 4793 | // Okay, qualifiers are equal. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4794 | |
Eli Friedman | 852d63b | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 4795 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 4796 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4797 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4798 | // We want to consider the two function types to be the same for these |
| 4799 | // comparisons, just force one to the other. |
| 4800 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 4801 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 4c721d3 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 4802 | |
| 4803 | // Same as above for arrays |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 4804 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 4805 | LHSClass = Type::ConstantArray; |
| 4806 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 4807 | RHSClass = Type::ConstantArray; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4808 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4809 | // ObjCInterfaces are just specialized ObjCObjects. |
| 4810 | if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject; |
| 4811 | if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject; |
| 4812 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 4813 | // Canonicalize ExtVector -> Vector. |
| 4814 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 4815 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4816 | |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 4817 | // If the canonical type classes don't match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4818 | if (LHSClass != RHSClass) { |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4819 | // 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] | 4820 | // a signed integer type, or an unsigned integer type. |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 4821 | // Compatibility is based on the underlying type, not the promotion |
| 4822 | // type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4823 | if (const EnumType* ETy = LHS->getAs<EnumType>()) { |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4824 | if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType()) |
| 4825 | return RHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 4826 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4827 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4828 | if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType()) |
| 4829 | return LHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 4830 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4831 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4832 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4833 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4834 | |
Steve Naroff | 4a74678 | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 4835 | // The canonical type classes match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4836 | switch (LHSClass) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4837 | #define TYPE(Class, Base) |
| 4838 | #define ABSTRACT_TYPE(Class, Base) |
John McCall | ad5e738 | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 4839 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4840 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 4841 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 4842 | #include "clang/AST/TypeNodes.def" |
| 4843 | assert(false && "Non-canonical and dependent types shouldn't get here"); |
| 4844 | return QualType(); |
| 4845 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4846 | case Type::LValueReference: |
| 4847 | case Type::RValueReference: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4848 | case Type::MemberPointer: |
| 4849 | assert(false && "C++ should never be in mergeTypes"); |
| 4850 | return QualType(); |
| 4851 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4852 | case Type::ObjCInterface: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4853 | case Type::IncompleteArray: |
| 4854 | case Type::VariableArray: |
| 4855 | case Type::FunctionProto: |
| 4856 | case Type::ExtVector: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4857 | assert(false && "Types are eliminated above"); |
| 4858 | return QualType(); |
| 4859 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4860 | case Type::Pointer: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4861 | { |
| 4862 | // Merge two pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4863 | QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); |
| 4864 | QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4865 | if (Unqualified) { |
| 4866 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 4867 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 4868 | } |
| 4869 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false, |
| 4870 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4871 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 4872 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4873 | return LHS; |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 4874 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4875 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4876 | return getPointerType(ResultType); |
| 4877 | } |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 4878 | case Type::BlockPointer: |
| 4879 | { |
| 4880 | // Merge two block pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4881 | QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); |
| 4882 | QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4883 | if (Unqualified) { |
| 4884 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 4885 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 4886 | } |
| 4887 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer, |
| 4888 | Unqualified); |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 4889 | if (ResultType.isNull()) return QualType(); |
| 4890 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 4891 | return LHS; |
| 4892 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 4893 | return RHS; |
| 4894 | return getBlockPointerType(ResultType); |
| 4895 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4896 | case Type::ConstantArray: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4897 | { |
| 4898 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 4899 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 4900 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 4901 | return QualType(); |
| 4902 | |
| 4903 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 4904 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4905 | if (Unqualified) { |
| 4906 | LHSElem = LHSElem.getUnqualifiedType(); |
| 4907 | RHSElem = RHSElem.getUnqualifiedType(); |
| 4908 | } |
| 4909 | |
| 4910 | QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4911 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4912 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 4913 | return LHS; |
| 4914 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 4915 | return RHS; |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 4916 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 4917 | ArrayType::ArraySizeModifier(), 0); |
| 4918 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 4919 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4920 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 4921 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4922 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 4923 | return LHS; |
| 4924 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 4925 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4926 | if (LVAT) { |
| 4927 | // FIXME: This isn't correct! But tricky to implement because |
| 4928 | // the array's size has to be the size of LHS, but the type |
| 4929 | // has to be different. |
| 4930 | return LHS; |
| 4931 | } |
| 4932 | if (RVAT) { |
| 4933 | // FIXME: This isn't correct! But tricky to implement because |
| 4934 | // the array's size has to be the size of RHS, but the type |
| 4935 | // has to be different. |
| 4936 | return RHS; |
| 4937 | } |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 4938 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 4939 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4940 | return getIncompleteArrayType(ResultType, |
| 4941 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4942 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4943 | case Type::FunctionNoProto: |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4944 | return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4945 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4946 | case Type::Enum: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4947 | return QualType(); |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4948 | case Type::Builtin: |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 4949 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4950 | return QualType(); |
Daniel Dunbar | 64cfdb7 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 4951 | case Type::Complex: |
| 4952 | // Distinct complex types are incompatible. |
| 4953 | return QualType(); |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 4954 | case Type::Vector: |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 4955 | // FIXME: The merged type should be an ExtVector! |
John McCall | 1c471f3 | 2010-03-12 23:14:13 +0000 | [diff] [blame] | 4956 | if (areCompatVectorTypes(LHSCan->getAs<VectorType>(), |
| 4957 | RHSCan->getAs<VectorType>())) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4958 | return LHS; |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4959 | return QualType(); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4960 | case Type::ObjCObject: { |
| 4961 | // Check if the types are assignment compatible. |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 4962 | // FIXME: This should be type compatibility, e.g. whether |
| 4963 | // "LHS x; RHS x;" at global scope is legal. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4964 | const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>(); |
| 4965 | const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>(); |
| 4966 | if (canAssignObjCInterfaces(LHSIface, RHSIface)) |
Steve Naroff | 5fd659d | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 4967 | return LHS; |
| 4968 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4969 | return QualType(); |
Cedric Venet | 61490e9 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 4970 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4971 | case Type::ObjCObjectPointer: { |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4972 | if (OfBlockPointer) { |
| 4973 | if (canAssignObjCInterfacesInBlockPointer( |
| 4974 | LHS->getAs<ObjCObjectPointerType>(), |
| 4975 | RHS->getAs<ObjCObjectPointerType>())) |
| 4976 | return LHS; |
| 4977 | return QualType(); |
| 4978 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4979 | if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(), |
| 4980 | RHS->getAs<ObjCObjectPointerType>())) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4981 | return LHS; |
| 4982 | |
Steve Naroff | bc76dd0 | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 4983 | return QualType(); |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4984 | } |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4985 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4986 | |
| 4987 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4988 | } |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 4989 | |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 4990 | /// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and |
| 4991 | /// 'RHS' attributes and returns the merged version; including for function |
| 4992 | /// return types. |
| 4993 | QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) { |
| 4994 | QualType LHSCan = getCanonicalType(LHS), |
| 4995 | RHSCan = getCanonicalType(RHS); |
| 4996 | // If two types are identical, they are compatible. |
| 4997 | if (LHSCan == RHSCan) |
| 4998 | return LHS; |
| 4999 | if (RHSCan->isFunctionType()) { |
| 5000 | if (!LHSCan->isFunctionType()) |
| 5001 | return QualType(); |
| 5002 | QualType OldReturnType = |
| 5003 | cast<FunctionType>(RHSCan.getTypePtr())->getResultType(); |
| 5004 | QualType NewReturnType = |
| 5005 | cast<FunctionType>(LHSCan.getTypePtr())->getResultType(); |
| 5006 | QualType ResReturnType = |
| 5007 | mergeObjCGCQualifiers(NewReturnType, OldReturnType); |
| 5008 | if (ResReturnType.isNull()) |
| 5009 | return QualType(); |
| 5010 | if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) { |
| 5011 | // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo(); |
| 5012 | // In either case, use OldReturnType to build the new function type. |
| 5013 | const FunctionType *F = LHS->getAs<FunctionType>(); |
| 5014 | if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) { |
| 5015 | FunctionType::ExtInfo Info = getFunctionExtInfo(LHS); |
| 5016 | QualType ResultType |
| 5017 | = getFunctionType(OldReturnType, FPT->arg_type_begin(), |
| 5018 | FPT->getNumArgs(), FPT->isVariadic(), |
| 5019 | FPT->getTypeQuals(), |
| 5020 | FPT->hasExceptionSpec(), |
| 5021 | FPT->hasAnyExceptionSpec(), |
| 5022 | FPT->getNumExceptions(), |
| 5023 | FPT->exception_begin(), |
| 5024 | Info); |
| 5025 | return ResultType; |
| 5026 | } |
| 5027 | } |
| 5028 | return QualType(); |
| 5029 | } |
| 5030 | |
| 5031 | // If the qualifiers are different, the types can still be merged. |
| 5032 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 5033 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
| 5034 | if (LQuals != RQuals) { |
| 5035 | // If any of these qualifiers are different, we have a type mismatch. |
| 5036 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 5037 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 5038 | return QualType(); |
| 5039 | |
| 5040 | // Exactly one GC qualifier difference is allowed: __strong is |
| 5041 | // okay if the other type has no GC qualifier but is an Objective |
| 5042 | // C object pointer (i.e. implicitly strong by default). We fix |
| 5043 | // this by pretending that the unqualified type was actually |
| 5044 | // qualified __strong. |
| 5045 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 5046 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 5047 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 5048 | |
| 5049 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 5050 | return QualType(); |
| 5051 | |
| 5052 | if (GC_L == Qualifiers::Strong) |
| 5053 | return LHS; |
| 5054 | if (GC_R == Qualifiers::Strong) |
| 5055 | return RHS; |
| 5056 | return QualType(); |
| 5057 | } |
| 5058 | |
| 5059 | if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) { |
| 5060 | QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5061 | QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5062 | QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT); |
| 5063 | if (ResQT == LHSBaseQT) |
| 5064 | return LHS; |
| 5065 | if (ResQT == RHSBaseQT) |
| 5066 | return RHS; |
| 5067 | } |
| 5068 | return QualType(); |
| 5069 | } |
| 5070 | |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 5071 | //===----------------------------------------------------------------------===// |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5072 | // Integer Predicates |
| 5073 | //===----------------------------------------------------------------------===// |
Chris Lattner | 88054de | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 5074 | |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5075 | unsigned ASTContext::getIntWidth(QualType T) { |
Sebastian Redl | 632d772 | 2009-11-05 21:10:57 +0000 | [diff] [blame] | 5076 | if (T->isBooleanType()) |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5077 | return 1; |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 5078 | if (EnumType *ET = dyn_cast<EnumType>(T)) |
Eli Friedman | 29a7f33 | 2009-12-10 22:29:29 +0000 | [diff] [blame] | 5079 | T = ET->getDecl()->getIntegerType(); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 5080 | // For builtin types, just use the standard type sizing method |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5081 | return (unsigned)getTypeSize(T); |
| 5082 | } |
| 5083 | |
| 5084 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) { |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5085 | assert(T->hasSignedIntegerRepresentation() && "Unexpected type"); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5086 | |
| 5087 | // Turn <4 x signed int> -> <4 x unsigned int> |
| 5088 | if (const VectorType *VTy = T->getAs<VectorType>()) |
| 5089 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 5090 | VTy->getNumElements(), VTy->getAltiVecSpecific()); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5091 | |
| 5092 | // For enums, we return the unsigned version of the base type. |
| 5093 | if (const EnumType *ETy = T->getAs<EnumType>()) |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5094 | T = ETy->getDecl()->getIntegerType(); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5095 | |
| 5096 | const BuiltinType *BTy = T->getAs<BuiltinType>(); |
| 5097 | assert(BTy && "Unexpected signed integer type"); |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5098 | switch (BTy->getKind()) { |
| 5099 | case BuiltinType::Char_S: |
| 5100 | case BuiltinType::SChar: |
| 5101 | return UnsignedCharTy; |
| 5102 | case BuiltinType::Short: |
| 5103 | return UnsignedShortTy; |
| 5104 | case BuiltinType::Int: |
| 5105 | return UnsignedIntTy; |
| 5106 | case BuiltinType::Long: |
| 5107 | return UnsignedLongTy; |
| 5108 | case BuiltinType::LongLong: |
| 5109 | return UnsignedLongLongTy; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 5110 | case BuiltinType::Int128: |
| 5111 | return UnsignedInt128Ty; |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5112 | default: |
| 5113 | assert(0 && "Unexpected signed integer type"); |
| 5114 | return QualType(); |
| 5115 | } |
| 5116 | } |
| 5117 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 5118 | ExternalASTSource::~ExternalASTSource() { } |
| 5119 | |
| 5120 | void ExternalASTSource::PrintStats() { } |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5121 | |
| 5122 | |
| 5123 | //===----------------------------------------------------------------------===// |
| 5124 | // Builtin Type Computation |
| 5125 | //===----------------------------------------------------------------------===// |
| 5126 | |
| 5127 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
| 5128 | /// pointer over the consumed characters. This returns the resultant type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5129 | static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context, |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5130 | ASTContext::GetBuiltinTypeError &Error, |
| 5131 | bool AllowTypeModifiers = true) { |
| 5132 | // Modifiers. |
| 5133 | int HowLong = 0; |
| 5134 | bool Signed = false, Unsigned = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5135 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5136 | // Read the modifiers first. |
| 5137 | bool Done = false; |
| 5138 | while (!Done) { |
| 5139 | switch (*Str++) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5140 | default: Done = true; --Str; break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5141 | case 'S': |
| 5142 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 5143 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 5144 | Signed = true; |
| 5145 | break; |
| 5146 | case 'U': |
| 5147 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
| 5148 | assert(!Unsigned && "Can't use 'S' modifier multiple times!"); |
| 5149 | Unsigned = true; |
| 5150 | break; |
| 5151 | case 'L': |
| 5152 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 5153 | ++HowLong; |
| 5154 | break; |
| 5155 | } |
| 5156 | } |
| 5157 | |
| 5158 | QualType Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5159 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5160 | // Read the base type. |
| 5161 | switch (*Str++) { |
| 5162 | default: assert(0 && "Unknown builtin type letter!"); |
| 5163 | case 'v': |
| 5164 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 5165 | "Bad modifiers used with 'v'!"); |
| 5166 | Type = Context.VoidTy; |
| 5167 | break; |
| 5168 | case 'f': |
| 5169 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 5170 | "Bad modifiers used with 'f'!"); |
| 5171 | Type = Context.FloatTy; |
| 5172 | break; |
| 5173 | case 'd': |
| 5174 | assert(HowLong < 2 && !Signed && !Unsigned && |
| 5175 | "Bad modifiers used with 'd'!"); |
| 5176 | if (HowLong) |
| 5177 | Type = Context.LongDoubleTy; |
| 5178 | else |
| 5179 | Type = Context.DoubleTy; |
| 5180 | break; |
| 5181 | case 's': |
| 5182 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 5183 | if (Unsigned) |
| 5184 | Type = Context.UnsignedShortTy; |
| 5185 | else |
| 5186 | Type = Context.ShortTy; |
| 5187 | break; |
| 5188 | case 'i': |
| 5189 | if (HowLong == 3) |
| 5190 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 5191 | else if (HowLong == 2) |
| 5192 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 5193 | else if (HowLong == 1) |
| 5194 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 5195 | else |
| 5196 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 5197 | break; |
| 5198 | case 'c': |
| 5199 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 5200 | if (Signed) |
| 5201 | Type = Context.SignedCharTy; |
| 5202 | else if (Unsigned) |
| 5203 | Type = Context.UnsignedCharTy; |
| 5204 | else |
| 5205 | Type = Context.CharTy; |
| 5206 | break; |
| 5207 | case 'b': // boolean |
| 5208 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 5209 | Type = Context.BoolTy; |
| 5210 | break; |
| 5211 | case 'z': // size_t. |
| 5212 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 5213 | Type = Context.getSizeType(); |
| 5214 | break; |
| 5215 | case 'F': |
| 5216 | Type = Context.getCFConstantStringType(); |
| 5217 | break; |
| 5218 | case 'a': |
| 5219 | Type = Context.getBuiltinVaListType(); |
| 5220 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 5221 | break; |
| 5222 | case 'A': |
| 5223 | // This is a "reference" to a va_list; however, what exactly |
| 5224 | // this means depends on how va_list is defined. There are two |
| 5225 | // different kinds of va_list: ones passed by value, and ones |
| 5226 | // passed by reference. An example of a by-value va_list is |
| 5227 | // x86, where va_list is a char*. An example of by-ref va_list |
| 5228 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 5229 | // we want this argument to be a char*&; for x86-64, we want |
| 5230 | // it to be a __va_list_tag*. |
| 5231 | Type = Context.getBuiltinVaListType(); |
| 5232 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 5233 | if (Type->isArrayType()) { |
| 5234 | Type = Context.getArrayDecayedType(Type); |
| 5235 | } else { |
| 5236 | Type = Context.getLValueReferenceType(Type); |
| 5237 | } |
| 5238 | break; |
| 5239 | case 'V': { |
| 5240 | char *End; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5241 | unsigned NumElements = strtoul(Str, &End, 10); |
| 5242 | assert(End != Str && "Missing vector size"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5243 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5244 | Str = End; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5245 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5246 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false); |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 5247 | // FIXME: Don't know what to do about AltiVec. |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 5248 | Type = Context.getVectorType(ElementType, NumElements, |
| 5249 | VectorType::NotAltiVec); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5250 | break; |
| 5251 | } |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 5252 | case 'X': { |
| 5253 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false); |
| 5254 | Type = Context.getComplexType(ElementType); |
| 5255 | break; |
| 5256 | } |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 5257 | case 'P': |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 5258 | Type = Context.getFILEType(); |
| 5259 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 5260 | Error = ASTContext::GE_Missing_stdio; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5261 | return QualType(); |
| 5262 | } |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 5263 | break; |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 5264 | case 'J': |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 5265 | if (Signed) |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 5266 | Type = Context.getsigjmp_bufType(); |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 5267 | else |
| 5268 | Type = Context.getjmp_bufType(); |
| 5269 | |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 5270 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 5271 | Error = ASTContext::GE_Missing_setjmp; |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 5272 | return QualType(); |
| 5273 | } |
| 5274 | break; |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 5275 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5276 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5277 | if (!AllowTypeModifiers) |
| 5278 | return Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5279 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5280 | Done = false; |
| 5281 | while (!Done) { |
John McCall | 187ab37 | 2010-03-12 04:21:28 +0000 | [diff] [blame] | 5282 | switch (char c = *Str++) { |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5283 | default: Done = true; --Str; break; |
| 5284 | case '*': |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5285 | case '&': |
John McCall | 187ab37 | 2010-03-12 04:21:28 +0000 | [diff] [blame] | 5286 | { |
| 5287 | // Both pointers and references can have their pointee types |
| 5288 | // qualified with an address space. |
| 5289 | char *End; |
| 5290 | unsigned AddrSpace = strtoul(Str, &End, 10); |
| 5291 | if (End != Str && AddrSpace != 0) { |
| 5292 | Type = Context.getAddrSpaceQualType(Type, AddrSpace); |
| 5293 | Str = End; |
| 5294 | } |
| 5295 | } |
| 5296 | if (c == '*') |
| 5297 | Type = Context.getPointerType(Type); |
| 5298 | else |
| 5299 | Type = Context.getLValueReferenceType(Type); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5300 | break; |
| 5301 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 5302 | case 'C': |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5303 | Type = Type.withConst(); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5304 | break; |
Fariborz Jahanian | 013af39 | 2010-01-26 22:48:42 +0000 | [diff] [blame] | 5305 | case 'D': |
| 5306 | Type = Context.getVolatileType(Type); |
| 5307 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5308 | } |
| 5309 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5310 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5311 | return Type; |
| 5312 | } |
| 5313 | |
| 5314 | /// GetBuiltinType - Return the type for the specified builtin. |
| 5315 | QualType ASTContext::GetBuiltinType(unsigned id, |
| 5316 | GetBuiltinTypeError &Error) { |
| 5317 | const char *TypeStr = BuiltinInfo.GetTypeString(id); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5318 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5319 | llvm::SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5320 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5321 | Error = GE_None; |
| 5322 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error); |
| 5323 | if (Error != GE_None) |
| 5324 | return QualType(); |
| 5325 | while (TypeStr[0] && TypeStr[0] != '.') { |
| 5326 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error); |
| 5327 | if (Error != GE_None) |
| 5328 | return QualType(); |
| 5329 | |
| 5330 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 5331 | if (Ty->isArrayType()) |
| 5332 | Ty = getArrayDecayedType(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5333 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5334 | ArgTypes.push_back(Ty); |
| 5335 | } |
| 5336 | |
| 5337 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 5338 | "'.' should only occur at end of builtin type list!"); |
| 5339 | |
| 5340 | // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);". |
| 5341 | if (ArgTypes.size() == 0 && TypeStr[0] == '.') |
| 5342 | return getFunctionNoProtoType(ResType); |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 5343 | |
| 5344 | // FIXME: Should we create noreturn types? |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5345 | return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 5346 | TypeStr[0] == '.', 0, false, false, 0, 0, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 5347 | FunctionType::ExtInfo()); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5348 | } |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5349 | |
| 5350 | QualType |
| 5351 | ASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) { |
| 5352 | // Perform the usual unary conversions. We do this early so that |
| 5353 | // integral promotions to "int" can allow us to exit early, in the |
| 5354 | // lhs == rhs check. Also, for conversion purposes, we ignore any |
| 5355 | // qualifiers. For example, "const float" and "float" are |
| 5356 | // equivalent. |
| 5357 | if (lhs->isPromotableIntegerType()) |
| 5358 | lhs = getPromotedIntegerType(lhs); |
| 5359 | else |
| 5360 | lhs = lhs.getUnqualifiedType(); |
| 5361 | if (rhs->isPromotableIntegerType()) |
| 5362 | rhs = getPromotedIntegerType(rhs); |
| 5363 | else |
| 5364 | rhs = rhs.getUnqualifiedType(); |
| 5365 | |
| 5366 | // If both types are identical, no conversion is needed. |
| 5367 | if (lhs == rhs) |
| 5368 | return lhs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5369 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5370 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 5371 | // The caller can deal with this (e.g. pointer + int). |
| 5372 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 5373 | return lhs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5374 | |
| 5375 | // At this point, we have two different arithmetic types. |
| 5376 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5377 | // Handle complex types first (C99 6.3.1.8p1). |
| 5378 | if (lhs->isComplexType() || rhs->isComplexType()) { |
| 5379 | // if we have an integer operand, the result is the complex type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5380 | if (rhs->isIntegerType() || rhs->isComplexIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5381 | // convert the rhs to the lhs complex type. |
| 5382 | return lhs; |
| 5383 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5384 | if (lhs->isIntegerType() || lhs->isComplexIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5385 | // convert the lhs to the rhs complex type. |
| 5386 | return rhs; |
| 5387 | } |
| 5388 | // This handles complex/complex, complex/float, or float/complex. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5389 | // When both operands are complex, the shorter operand is converted to the |
| 5390 | // type of the longer, and that is the type of the result. This corresponds |
| 5391 | // to what is done when combining two real floating-point operands. |
| 5392 | // The fun begins when size promotion occur across type domains. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5393 | // 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] | 5394 | // floating-point type, the less precise type is converted, within it's |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5395 | // 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] | 5396 | // when combining a "long double" with a "double _Complex", the |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5397 | // "double _Complex" is promoted to "long double _Complex". |
| 5398 | int result = getFloatingTypeOrder(lhs, rhs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5399 | |
| 5400 | if (result > 0) { // The left side is bigger, convert rhs. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5401 | rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5402 | } else if (result < 0) { // The right side is bigger, convert lhs. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5403 | lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5404 | } |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5405 | // At this point, lhs and rhs have the same rank/size. Now, make sure the |
| 5406 | // domains match. This is a requirement for our implementation, C99 |
| 5407 | // does not require this promotion. |
| 5408 | if (lhs != rhs) { // Domains don't match, we have complex/float mix. |
| 5409 | if (lhs->isRealFloatingType()) { // handle "double, _Complex double". |
| 5410 | return rhs; |
| 5411 | } else { // handle "_Complex double, double". |
| 5412 | return lhs; |
| 5413 | } |
| 5414 | } |
| 5415 | return lhs; // The domain/size match exactly. |
| 5416 | } |
| 5417 | // Now handle "real" floating types (i.e. float, double, long double). |
| 5418 | if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) { |
| 5419 | // if we have an integer operand, the result is the real floating type. |
| 5420 | if (rhs->isIntegerType()) { |
| 5421 | // convert rhs to the lhs floating point type. |
| 5422 | return lhs; |
| 5423 | } |
| 5424 | if (rhs->isComplexIntegerType()) { |
| 5425 | // convert rhs to the complex floating point type. |
| 5426 | return getComplexType(lhs); |
| 5427 | } |
| 5428 | if (lhs->isIntegerType()) { |
| 5429 | // convert lhs to the rhs floating point type. |
| 5430 | return rhs; |
| 5431 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5432 | if (lhs->isComplexIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5433 | // convert lhs to the complex floating point type. |
| 5434 | return getComplexType(rhs); |
| 5435 | } |
| 5436 | // We have two real floating types, float/complex combos were handled above. |
| 5437 | // Convert the smaller operand to the bigger result. |
| 5438 | int result = getFloatingTypeOrder(lhs, rhs); |
| 5439 | if (result > 0) // convert the rhs |
| 5440 | return lhs; |
| 5441 | assert(result < 0 && "illegal float comparison"); |
| 5442 | return rhs; // convert the lhs |
| 5443 | } |
| 5444 | if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) { |
| 5445 | // Handle GCC complex int extension. |
| 5446 | const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType(); |
| 5447 | const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType(); |
| 5448 | |
| 5449 | if (lhsComplexInt && rhsComplexInt) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5450 | if (getIntegerTypeOrder(lhsComplexInt->getElementType(), |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5451 | rhsComplexInt->getElementType()) >= 0) |
| 5452 | return lhs; // convert the rhs |
| 5453 | return rhs; |
| 5454 | } else if (lhsComplexInt && rhs->isIntegerType()) { |
| 5455 | // convert the rhs to the lhs complex type. |
| 5456 | return lhs; |
| 5457 | } else if (rhsComplexInt && lhs->isIntegerType()) { |
| 5458 | // convert the lhs to the rhs complex type. |
| 5459 | return rhs; |
| 5460 | } |
| 5461 | } |
| 5462 | // Finally, we have two differing integer types. |
| 5463 | // The rules for this case are in C99 6.3.1.8 |
| 5464 | int compare = getIntegerTypeOrder(lhs, rhs); |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5465 | bool lhsSigned = lhs->hasSignedIntegerRepresentation(), |
| 5466 | rhsSigned = rhs->hasSignedIntegerRepresentation(); |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5467 | QualType destType; |
| 5468 | if (lhsSigned == rhsSigned) { |
| 5469 | // Same signedness; use the higher-ranked type |
| 5470 | destType = compare >= 0 ? lhs : rhs; |
| 5471 | } else if (compare != (lhsSigned ? 1 : -1)) { |
| 5472 | // The unsigned type has greater than or equal rank to the |
| 5473 | // signed type, so use the unsigned type |
| 5474 | destType = lhsSigned ? rhs : lhs; |
| 5475 | } else if (getIntWidth(lhs) != getIntWidth(rhs)) { |
| 5476 | // The two types are different widths; if we are here, that |
| 5477 | // means the signed type is larger than the unsigned type, so |
| 5478 | // use the signed type. |
| 5479 | destType = lhsSigned ? lhs : rhs; |
| 5480 | } else { |
| 5481 | // The signed type is higher-ranked than the unsigned type, |
| 5482 | // but isn't actually any bigger (like unsigned int and long |
| 5483 | // on most 32-bit systems). Use the unsigned type corresponding |
| 5484 | // to the signed type. |
| 5485 | destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs); |
| 5486 | } |
| 5487 | return destType; |
| 5488 | } |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5489 | |
| 5490 | GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) { |
| 5491 | GVALinkage External = GVA_StrongExternal; |
| 5492 | |
| 5493 | Linkage L = FD->getLinkage(); |
| 5494 | if (L == ExternalLinkage && getLangOptions().CPlusPlus && |
| 5495 | FD->getType()->getLinkage() == UniqueExternalLinkage) |
| 5496 | L = UniqueExternalLinkage; |
| 5497 | |
| 5498 | switch (L) { |
| 5499 | case NoLinkage: |
| 5500 | case InternalLinkage: |
| 5501 | case UniqueExternalLinkage: |
| 5502 | return GVA_Internal; |
| 5503 | |
| 5504 | case ExternalLinkage: |
| 5505 | switch (FD->getTemplateSpecializationKind()) { |
| 5506 | case TSK_Undeclared: |
| 5507 | case TSK_ExplicitSpecialization: |
| 5508 | External = GVA_StrongExternal; |
| 5509 | break; |
| 5510 | |
| 5511 | case TSK_ExplicitInstantiationDefinition: |
| 5512 | return GVA_ExplicitTemplateInstantiation; |
| 5513 | |
| 5514 | case TSK_ExplicitInstantiationDeclaration: |
| 5515 | case TSK_ImplicitInstantiation: |
| 5516 | External = GVA_TemplateInstantiation; |
| 5517 | break; |
| 5518 | } |
| 5519 | } |
| 5520 | |
| 5521 | if (!FD->isInlined()) |
| 5522 | return External; |
| 5523 | |
| 5524 | if (!getLangOptions().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) { |
| 5525 | // GNU or C99 inline semantics. Determine whether this symbol should be |
| 5526 | // externally visible. |
| 5527 | if (FD->isInlineDefinitionExternallyVisible()) |
| 5528 | return External; |
| 5529 | |
| 5530 | // C99 inline semantics, where the symbol is not externally visible. |
| 5531 | return GVA_C99Inline; |
| 5532 | } |
| 5533 | |
| 5534 | // C++0x [temp.explicit]p9: |
| 5535 | // [ Note: The intent is that an inline function that is the subject of |
| 5536 | // an explicit instantiation declaration will still be implicitly |
| 5537 | // instantiated when used so that the body can be considered for |
| 5538 | // inlining, but that no out-of-line copy of the inline function would be |
| 5539 | // generated in the translation unit. -- end note ] |
| 5540 | if (FD->getTemplateSpecializationKind() |
| 5541 | == TSK_ExplicitInstantiationDeclaration) |
| 5542 | return GVA_C99Inline; |
| 5543 | |
| 5544 | return GVA_CXXInline; |
| 5545 | } |
| 5546 | |
| 5547 | GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) { |
| 5548 | // If this is a static data member, compute the kind of template |
| 5549 | // specialization. Otherwise, this variable is not part of a |
| 5550 | // template. |
| 5551 | TemplateSpecializationKind TSK = TSK_Undeclared; |
| 5552 | if (VD->isStaticDataMember()) |
| 5553 | TSK = VD->getTemplateSpecializationKind(); |
| 5554 | |
| 5555 | Linkage L = VD->getLinkage(); |
| 5556 | if (L == ExternalLinkage && getLangOptions().CPlusPlus && |
| 5557 | VD->getType()->getLinkage() == UniqueExternalLinkage) |
| 5558 | L = UniqueExternalLinkage; |
| 5559 | |
| 5560 | switch (L) { |
| 5561 | case NoLinkage: |
| 5562 | case InternalLinkage: |
| 5563 | case UniqueExternalLinkage: |
| 5564 | return GVA_Internal; |
| 5565 | |
| 5566 | case ExternalLinkage: |
| 5567 | switch (TSK) { |
| 5568 | case TSK_Undeclared: |
| 5569 | case TSK_ExplicitSpecialization: |
| 5570 | return GVA_StrongExternal; |
| 5571 | |
| 5572 | case TSK_ExplicitInstantiationDeclaration: |
| 5573 | llvm_unreachable("Variable should not be instantiated"); |
| 5574 | // Fall through to treat this like any other instantiation. |
| 5575 | |
| 5576 | case TSK_ExplicitInstantiationDefinition: |
| 5577 | return GVA_ExplicitTemplateInstantiation; |
| 5578 | |
| 5579 | case TSK_ImplicitInstantiation: |
| 5580 | return GVA_TemplateInstantiation; |
| 5581 | } |
| 5582 | } |
| 5583 | |
| 5584 | return GVA_StrongExternal; |
| 5585 | } |
| 5586 | |
Argyrios Kyrtzidis | 4ac7c0b | 2010-07-29 20:08:05 +0000 | [diff] [blame] | 5587 | bool ASTContext::DeclMustBeEmitted(const Decl *D) { |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5588 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 5589 | if (!VD->isFileVarDecl()) |
| 5590 | return false; |
| 5591 | } else if (!isa<FunctionDecl>(D)) |
| 5592 | return false; |
| 5593 | |
Argyrios Kyrtzidis | ab411c8 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 5594 | // Weak references don't produce any output by themselves. |
| 5595 | if (D->hasAttr<WeakRefAttr>()) |
| 5596 | return false; |
| 5597 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5598 | // Aliases and used decls are required. |
| 5599 | if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>()) |
| 5600 | return true; |
| 5601 | |
| 5602 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 5603 | // Forward declarations aren't required. |
| 5604 | if (!FD->isThisDeclarationADefinition()) |
| 5605 | return false; |
| 5606 | |
| 5607 | // Constructors and destructors are required. |
| 5608 | if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>()) |
| 5609 | return true; |
| 5610 | |
| 5611 | // The key function for a class is required. |
| 5612 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 5613 | const CXXRecordDecl *RD = MD->getParent(); |
| 5614 | if (MD->isOutOfLine() && RD->isDynamicClass()) { |
| 5615 | const CXXMethodDecl *KeyFunc = getKeyFunction(RD); |
| 5616 | if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl()) |
| 5617 | return true; |
| 5618 | } |
| 5619 | } |
| 5620 | |
| 5621 | GVALinkage Linkage = GetGVALinkageForFunction(FD); |
| 5622 | |
| 5623 | // static, static inline, always_inline, and extern inline functions can |
| 5624 | // always be deferred. Normal inline functions can be deferred in C99/C++. |
| 5625 | // Implicit template instantiations can also be deferred in C++. |
| 5626 | if (Linkage == GVA_Internal || Linkage == GVA_C99Inline || |
| 5627 | Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) |
| 5628 | return false; |
| 5629 | return true; |
| 5630 | } |
| 5631 | |
| 5632 | const VarDecl *VD = cast<VarDecl>(D); |
| 5633 | assert(VD->isFileVarDecl() && "Expected file scoped var"); |
| 5634 | |
Argyrios Kyrtzidis | ab411c8 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 5635 | if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly) |
| 5636 | return false; |
| 5637 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5638 | // Structs that have non-trivial constructors or destructors are required. |
| 5639 | |
| 5640 | // FIXME: Handle references. |
| 5641 | if (const RecordType *RT = VD->getType()->getAs<RecordType>()) { |
| 5642 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 5643 | if (!RD->hasTrivialConstructor() || !RD->hasTrivialDestructor()) |
| 5644 | return true; |
| 5645 | } |
| 5646 | } |
| 5647 | |
| 5648 | GVALinkage L = GetGVALinkageForVariable(VD); |
| 5649 | if (L == GVA_Internal || L == GVA_TemplateInstantiation) { |
| 5650 | if (!(VD->getInit() && VD->getInit()->HasSideEffects(*this))) |
| 5651 | return false; |
| 5652 | } |
| 5653 | |
| 5654 | return true; |
| 5655 | } |