Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the ASTContext interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/ASTContext.h" |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 15 | #include "clang/AST/CharUnits.h" |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclCXX.h" |
Steve Naroff | 980e508 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclTemplate.h" |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeLoc.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 20 | #include "clang/AST/Expr.h" |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 21 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExternalASTSource.h" |
Argyrios Kyrtzidis | 7b90340 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 23 | #include "clang/AST/ASTMutationListener.h" |
Anders Carlsson | 19cc4ab | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 24 | #include "clang/AST/RecordLayout.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 25 | #include "clang/Basic/Builtins.h" |
Chris Lattner | a9376d4 | 2009-03-28 03:45:20 +0000 | [diff] [blame] | 26 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 27 | #include "clang/Basic/TargetInfo.h" |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SmallString.h" |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/StringExtras.h" |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 30 | #include "llvm/Support/MathExtras.h" |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 31 | #include "llvm/Support/raw_ostream.h" |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 32 | #include "CXXABI.h" |
Anders Carlsson | 29445a0 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 33 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 34 | using namespace clang; |
| 35 | |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 36 | unsigned ASTContext::NumImplicitDefaultConstructors; |
| 37 | unsigned ASTContext::NumImplicitDefaultConstructorsDeclared; |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 38 | unsigned ASTContext::NumImplicitCopyConstructors; |
| 39 | unsigned ASTContext::NumImplicitCopyConstructorsDeclared; |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 40 | unsigned ASTContext::NumImplicitCopyAssignmentOperators; |
| 41 | unsigned ASTContext::NumImplicitCopyAssignmentOperatorsDeclared; |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 42 | unsigned ASTContext::NumImplicitDestructors; |
| 43 | unsigned ASTContext::NumImplicitDestructorsDeclared; |
| 44 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 45 | enum FloatingRank { |
| 46 | FloatRank, DoubleRank, LongDoubleRank |
| 47 | }; |
| 48 | |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 49 | void |
| 50 | ASTContext::CanonicalTemplateTemplateParm::Profile(llvm::FoldingSetNodeID &ID, |
| 51 | TemplateTemplateParmDecl *Parm) { |
| 52 | ID.AddInteger(Parm->getDepth()); |
| 53 | ID.AddInteger(Parm->getPosition()); |
| 54 | // FIXME: Parameter pack |
| 55 | |
| 56 | TemplateParameterList *Params = Parm->getTemplateParameters(); |
| 57 | ID.AddInteger(Params->size()); |
| 58 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 59 | PEnd = Params->end(); |
| 60 | P != PEnd; ++P) { |
| 61 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
| 62 | ID.AddInteger(0); |
| 63 | ID.AddBoolean(TTP->isParameterPack()); |
| 64 | continue; |
| 65 | } |
| 66 | |
| 67 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 68 | ID.AddInteger(1); |
| 69 | // FIXME: Parameter pack |
| 70 | ID.AddPointer(NTTP->getType().getAsOpaquePtr()); |
| 71 | continue; |
| 72 | } |
| 73 | |
| 74 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); |
| 75 | ID.AddInteger(2); |
| 76 | Profile(ID, TTP); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | TemplateTemplateParmDecl * |
| 81 | ASTContext::getCanonicalTemplateTemplateParmDecl( |
| 82 | TemplateTemplateParmDecl *TTP) { |
| 83 | // Check if we already have a canonical template template parameter. |
| 84 | llvm::FoldingSetNodeID ID; |
| 85 | CanonicalTemplateTemplateParm::Profile(ID, TTP); |
| 86 | void *InsertPos = 0; |
| 87 | CanonicalTemplateTemplateParm *Canonical |
| 88 | = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
| 89 | if (Canonical) |
| 90 | return Canonical->getParam(); |
| 91 | |
| 92 | // Build a canonical template parameter list. |
| 93 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 94 | llvm::SmallVector<NamedDecl *, 4> CanonParams; |
| 95 | CanonParams.reserve(Params->size()); |
| 96 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 97 | PEnd = Params->end(); |
| 98 | P != PEnd; ++P) { |
| 99 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) |
| 100 | CanonParams.push_back( |
| 101 | TemplateTypeParmDecl::Create(*this, getTranslationUnitDecl(), |
| 102 | SourceLocation(), TTP->getDepth(), |
| 103 | TTP->getIndex(), 0, false, |
| 104 | TTP->isParameterPack())); |
| 105 | else if (NonTypeTemplateParmDecl *NTTP |
| 106 | = dyn_cast<NonTypeTemplateParmDecl>(*P)) |
| 107 | CanonParams.push_back( |
| 108 | NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
| 109 | SourceLocation(), NTTP->getDepth(), |
| 110 | NTTP->getPosition(), 0, |
| 111 | getCanonicalType(NTTP->getType()), |
| 112 | 0)); |
| 113 | else |
| 114 | CanonParams.push_back(getCanonicalTemplateTemplateParmDecl( |
| 115 | cast<TemplateTemplateParmDecl>(*P))); |
| 116 | } |
| 117 | |
| 118 | TemplateTemplateParmDecl *CanonTTP |
| 119 | = TemplateTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
| 120 | SourceLocation(), TTP->getDepth(), |
| 121 | TTP->getPosition(), 0, |
| 122 | TemplateParameterList::Create(*this, SourceLocation(), |
| 123 | SourceLocation(), |
| 124 | CanonParams.data(), |
| 125 | CanonParams.size(), |
| 126 | SourceLocation())); |
| 127 | |
| 128 | // Get the new insert position for the node we care about. |
| 129 | Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
| 130 | assert(Canonical == 0 && "Shouldn't be in the map!"); |
| 131 | (void)Canonical; |
| 132 | |
| 133 | // Create the canonical template template parameter entry. |
| 134 | Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP); |
| 135 | CanonTemplateTemplateParms.InsertNode(Canonical, InsertPos); |
| 136 | return CanonTTP; |
| 137 | } |
| 138 | |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 139 | CXXABI *ASTContext::createCXXABI(const TargetInfo &T) { |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 140 | if (!LangOpts.CPlusPlus) return 0; |
| 141 | |
Charles Davis | 20cf717 | 2010-08-19 02:18:14 +0000 | [diff] [blame] | 142 | switch (T.getCXXABI()) { |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 143 | case CXXABI_ARM: |
| 144 | return CreateARMCXXABI(*this); |
| 145 | case CXXABI_Itanium: |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 146 | return CreateItaniumCXXABI(*this); |
Charles Davis | 20cf717 | 2010-08-19 02:18:14 +0000 | [diff] [blame] | 147 | case CXXABI_Microsoft: |
| 148 | return CreateMicrosoftCXXABI(*this); |
| 149 | } |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 150 | return 0; |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 153 | ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM, |
Daniel Dunbar | 444be73 | 2009-11-13 05:51:54 +0000 | [diff] [blame] | 154 | const TargetInfo &t, |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 155 | IdentifierTable &idents, SelectorTable &sels, |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 156 | Builtin::Context &builtins, |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 157 | unsigned size_reserve) : |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 158 | TemplateSpecializationTypes(this_()), |
| 159 | DependentTemplateSpecializationTypes(this_()), |
Argyrios Kyrtzidis | 0061138 | 2010-07-04 21:44:19 +0000 | [diff] [blame] | 160 | GlobalNestedNameSpecifier(0), IsInt128Installed(false), |
| 161 | CFConstantStringTypeDecl(0), NSConstantStringTypeDecl(0), |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 162 | ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0), |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 163 | sigjmp_bufDecl(0), BlockDescriptorType(0), BlockDescriptorExtendedType(0), |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 164 | NullTypeSourceInfo(QualType()), |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 165 | SourceMgr(SM), LangOpts(LOpts), ABI(createCXXABI(t)), Target(t), |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 166 | Idents(idents), Selectors(sels), |
Ted Kremenek | ac9590e | 2010-05-10 20:40:08 +0000 | [diff] [blame] | 167 | BuiltinInfo(builtins), |
| 168 | DeclarationNames(*this), |
Argyrios Kyrtzidis | 9a44b5f | 2010-10-28 09:29:35 +0000 | [diff] [blame] | 169 | ExternalSource(0), Listener(0), PrintingPolicy(LOpts), |
Ted Kremenek | f057bf7 | 2010-06-18 00:31:04 +0000 | [diff] [blame] | 170 | LastSDM(0, 0), |
| 171 | UniqueBlockByRefTypeID(0), UniqueBlockParmTypeID(0) { |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 172 | ObjCIdRedefinitionType = QualType(); |
| 173 | ObjCClassRedefinitionType = QualType(); |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 174 | ObjCSelRedefinitionType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 175 | if (size_reserve > 0) Types.reserve(size_reserve); |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 176 | TUDecl = TranslationUnitDecl::Create(*this); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 177 | InitBuiltinTypes(); |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 180 | ASTContext::~ASTContext() { |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 181 | // Release the DenseMaps associated with DeclContext objects. |
| 182 | // FIXME: Is this the ideal solution? |
| 183 | ReleaseDeclContextMaps(); |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 184 | |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 185 | // Call all of the deallocation functions. |
| 186 | for (unsigned I = 0, N = Deallocations.size(); I != N; ++I) |
| 187 | Deallocations[I].first(Deallocations[I].second); |
Douglas Gregor | 0054531 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 188 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 189 | // Release all of the memory associated with overridden C++ methods. |
| 190 | for (llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::iterator |
| 191 | OM = OverriddenMethods.begin(), OMEnd = OverriddenMethods.end(); |
| 192 | OM != OMEnd; ++OM) |
| 193 | OM->second.Destroy(); |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 194 | |
Ted Kremenek | dcfcfbe | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 195 | // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 196 | // because they can contain DenseMaps. |
| 197 | for (llvm::DenseMap<const ObjCContainerDecl*, |
| 198 | const ASTRecordLayout*>::iterator |
| 199 | I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) |
| 200 | // Increment in loop to prevent using deallocated memory. |
| 201 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 202 | R->Destroy(*this); |
| 203 | |
Ted Kremenek | dcfcfbe | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 204 | for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator |
| 205 | I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) { |
| 206 | // Increment in loop to prevent using deallocated memory. |
| 207 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 208 | R->Destroy(*this); |
| 209 | } |
Douglas Gregor | 6320064 | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 210 | |
| 211 | for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(), |
| 212 | AEnd = DeclAttrs.end(); |
| 213 | A != AEnd; ++A) |
| 214 | A->second->~AttrVec(); |
| 215 | } |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 216 | |
Douglas Gregor | 0054531 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 217 | void ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) { |
| 218 | Deallocations.push_back(std::make_pair(Callback, Data)); |
| 219 | } |
| 220 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 221 | void |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 222 | ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) { |
| 223 | ExternalSource.reset(Source.take()); |
| 224 | } |
| 225 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 226 | void ASTContext::PrintStats() const { |
| 227 | fprintf(stderr, "*** AST Context Stats:\n"); |
| 228 | fprintf(stderr, " %d types total.\n", (int)Types.size()); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 229 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 230 | unsigned counts[] = { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 231 | #define TYPE(Name, Parent) 0, |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 232 | #define ABSTRACT_TYPE(Name, Parent) |
| 233 | #include "clang/AST/TypeNodes.def" |
| 234 | 0 // Extra |
| 235 | }; |
Douglas Gregor | c2ee10d | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 236 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 237 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 238 | Type *T = Types[i]; |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 239 | counts[(unsigned)T->getTypeClass()]++; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 242 | unsigned Idx = 0; |
| 243 | unsigned TotalBytes = 0; |
| 244 | #define TYPE(Name, Parent) \ |
| 245 | if (counts[Idx]) \ |
| 246 | fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \ |
| 247 | TotalBytes += counts[Idx] * sizeof(Name##Type); \ |
| 248 | ++Idx; |
| 249 | #define ABSTRACT_TYPE(Name, Parent) |
| 250 | #include "clang/AST/TypeNodes.def" |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 251 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 252 | fprintf(stderr, "Total bytes = %d\n", int(TotalBytes)); |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 253 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 254 | // Implicit special member functions. |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 255 | fprintf(stderr, " %u/%u implicit default constructors created\n", |
| 256 | NumImplicitDefaultConstructorsDeclared, |
| 257 | NumImplicitDefaultConstructors); |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 258 | fprintf(stderr, " %u/%u implicit copy constructors created\n", |
| 259 | NumImplicitCopyConstructorsDeclared, |
| 260 | NumImplicitCopyConstructors); |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 261 | fprintf(stderr, " %u/%u implicit copy assignment operators created\n", |
| 262 | NumImplicitCopyAssignmentOperatorsDeclared, |
| 263 | NumImplicitCopyAssignmentOperators); |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 264 | fprintf(stderr, " %u/%u implicit destructors created\n", |
| 265 | NumImplicitDestructorsDeclared, NumImplicitDestructors); |
| 266 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 267 | if (ExternalSource.get()) { |
| 268 | fprintf(stderr, "\n"); |
| 269 | ExternalSource->PrintStats(); |
| 270 | } |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 271 | |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 272 | BumpAlloc.PrintStats(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 276 | void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 277 | BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 278 | R = CanQualType::CreateUnsafe(QualType(Ty, 0)); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 279 | Types.push_back(Ty); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 282 | void ASTContext::InitBuiltinTypes() { |
| 283 | assert(VoidTy.isNull() && "Context reinitialized?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 284 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 285 | // C99 6.2.5p19. |
| 286 | InitBuiltinType(VoidTy, BuiltinType::Void); |
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.5p2. |
| 289 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
| 290 | // C99 6.2.5p3. |
Eli Friedman | 15b9176 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 291 | if (LangOpts.CharIsSigned) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 292 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 293 | else |
| 294 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
| 295 | // C99 6.2.5p4. |
| 296 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 297 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 298 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 299 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 300 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 301 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 302 | // C99 6.2.5p6. |
| 303 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 304 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 305 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 306 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 307 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 308 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 309 | // C99 6.2.5p10. |
| 310 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 311 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 312 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 313 | |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 314 | // GNU extension, 128-bit integers. |
| 315 | InitBuiltinType(Int128Ty, BuiltinType::Int128); |
| 316 | InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128); |
| 317 | |
Chris Lattner | 3a25032 | 2009-02-26 23:43:47 +0000 | [diff] [blame] | 318 | if (LangOpts.CPlusPlus) // C++ 3.9.1p5 |
| 319 | InitBuiltinType(WCharTy, BuiltinType::WChar); |
| 320 | else // C99 |
| 321 | WCharTy = getFromTargetType(Target.getWCharType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 322 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 323 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 324 | InitBuiltinType(Char16Ty, BuiltinType::Char16); |
| 325 | else // C99 |
| 326 | Char16Ty = getFromTargetType(Target.getChar16Type()); |
| 327 | |
| 328 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 329 | InitBuiltinType(Char32Ty, BuiltinType::Char32); |
| 330 | else // C99 |
| 331 | Char32Ty = getFromTargetType(Target.getChar32Type()); |
| 332 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 333 | // Placeholder type for type-dependent expressions whose type is |
| 334 | // completely unknown. No code should ever check a type against |
| 335 | // DependentTy and users should never see it; however, it is here to |
| 336 | // help diagnose failures to properly check for type-dependent |
| 337 | // expressions. |
| 338 | InitBuiltinType(DependentTy, BuiltinType::Dependent); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 339 | |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 340 | // Placeholder type for functions. |
| 341 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
| 342 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 343 | // Placeholder type for C++0x auto declarations whose real type has |
Anders Carlsson | e89d159 | 2009-06-26 18:41:36 +0000 | [diff] [blame] | 344 | // not yet been deduced. |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 345 | InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 346 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 347 | // C99 6.2.5p11. |
| 348 | FloatComplexTy = getComplexType(FloatTy); |
| 349 | DoubleComplexTy = getComplexType(DoubleTy); |
| 350 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 351 | |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 352 | BuiltinVaListType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 353 | |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 354 | // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope(). |
| 355 | ObjCIdTypedefType = QualType(); |
| 356 | ObjCClassTypedefType = QualType(); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 357 | ObjCSelTypedefType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 358 | |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 359 | // Builtin types for 'id', 'Class', and 'SEL'. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 360 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); |
| 361 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 362 | InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 363 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 364 | ObjCConstantStringType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 365 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 366 | // void * type |
| 367 | VoidPtrTy = getPointerType(VoidTy); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 368 | |
| 369 | // nullptr type (C++0x 2.14.7) |
| 370 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 373 | Diagnostic &ASTContext::getDiagnostics() const { |
| 374 | return SourceMgr.getDiagnostics(); |
| 375 | } |
| 376 | |
Douglas Gregor | 6320064 | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 377 | AttrVec& ASTContext::getDeclAttrs(const Decl *D) { |
| 378 | AttrVec *&Result = DeclAttrs[D]; |
| 379 | if (!Result) { |
| 380 | void *Mem = Allocate(sizeof(AttrVec)); |
| 381 | Result = new (Mem) AttrVec; |
| 382 | } |
| 383 | |
| 384 | return *Result; |
| 385 | } |
| 386 | |
| 387 | /// \brief Erase the attributes corresponding to the given declaration. |
| 388 | void ASTContext::eraseDeclAttrs(const Decl *D) { |
| 389 | llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D); |
| 390 | if (Pos != DeclAttrs.end()) { |
| 391 | Pos->second->~AttrVec(); |
| 392 | DeclAttrs.erase(Pos); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 397 | MemberSpecializationInfo * |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 398 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 399 | assert(Var->isStaticDataMember() && "Not a static data member"); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 400 | llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 401 | = InstantiatedFromStaticDataMember.find(Var); |
| 402 | if (Pos == InstantiatedFromStaticDataMember.end()) |
| 403 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 404 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 405 | return Pos->second; |
| 406 | } |
| 407 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 408 | void |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 409 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, |
Argyrios Kyrtzidis | 9421adc | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 410 | TemplateSpecializationKind TSK, |
| 411 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 412 | assert(Inst->isStaticDataMember() && "Not a static data member"); |
| 413 | assert(Tmpl->isStaticDataMember() && "Not a static data member"); |
| 414 | assert(!InstantiatedFromStaticDataMember[Inst] && |
| 415 | "Already noted what static data member was instantiated from"); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 416 | InstantiatedFromStaticDataMember[Inst] |
Argyrios Kyrtzidis | 9421adc | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 417 | = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 418 | } |
| 419 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 420 | NamedDecl * |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 421 | ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 422 | llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 423 | = InstantiatedFromUsingDecl.find(UUD); |
| 424 | if (Pos == InstantiatedFromUsingDecl.end()) |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 425 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 426 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 427 | return Pos->second; |
| 428 | } |
| 429 | |
| 430 | void |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 431 | ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) { |
| 432 | assert((isa<UsingDecl>(Pattern) || |
| 433 | isa<UnresolvedUsingValueDecl>(Pattern) || |
| 434 | isa<UnresolvedUsingTypenameDecl>(Pattern)) && |
| 435 | "pattern decl is not a using decl"); |
| 436 | assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists"); |
| 437 | InstantiatedFromUsingDecl[Inst] = Pattern; |
| 438 | } |
| 439 | |
| 440 | UsingShadowDecl * |
| 441 | ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) { |
| 442 | llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos |
| 443 | = InstantiatedFromUsingShadowDecl.find(Inst); |
| 444 | if (Pos == InstantiatedFromUsingShadowDecl.end()) |
| 445 | return 0; |
| 446 | |
| 447 | return Pos->second; |
| 448 | } |
| 449 | |
| 450 | void |
| 451 | ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, |
| 452 | UsingShadowDecl *Pattern) { |
| 453 | assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists"); |
| 454 | InstantiatedFromUsingShadowDecl[Inst] = Pattern; |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 457 | FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { |
| 458 | llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos |
| 459 | = InstantiatedFromUnnamedFieldDecl.find(Field); |
| 460 | if (Pos == InstantiatedFromUnnamedFieldDecl.end()) |
| 461 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 462 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 463 | return Pos->second; |
| 464 | } |
| 465 | |
| 466 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, |
| 467 | FieldDecl *Tmpl) { |
| 468 | assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed"); |
| 469 | assert(!Tmpl->getDeclName() && "Template field decl is not unnamed"); |
| 470 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] && |
| 471 | "Already noted what unnamed field was instantiated from"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 472 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 473 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; |
| 474 | } |
| 475 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 476 | ASTContext::overridden_cxx_method_iterator |
| 477 | ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const { |
| 478 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 479 | = OverriddenMethods.find(Method); |
| 480 | if (Pos == OverriddenMethods.end()) |
| 481 | return 0; |
| 482 | |
| 483 | return Pos->second.begin(); |
| 484 | } |
| 485 | |
| 486 | ASTContext::overridden_cxx_method_iterator |
| 487 | ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const { |
| 488 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 489 | = OverriddenMethods.find(Method); |
| 490 | if (Pos == OverriddenMethods.end()) |
| 491 | return 0; |
| 492 | |
| 493 | return Pos->second.end(); |
| 494 | } |
| 495 | |
Argyrios Kyrtzidis | c91e9f4 | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 496 | unsigned |
| 497 | ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const { |
| 498 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 499 | = OverriddenMethods.find(Method); |
| 500 | if (Pos == OverriddenMethods.end()) |
| 501 | return 0; |
| 502 | |
| 503 | return Pos->second.size(); |
| 504 | } |
| 505 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 506 | void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method, |
| 507 | const CXXMethodDecl *Overridden) { |
| 508 | OverriddenMethods[Method].push_back(Overridden); |
| 509 | } |
| 510 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 511 | //===----------------------------------------------------------------------===// |
| 512 | // Type Sizing and Analysis |
| 513 | //===----------------------------------------------------------------------===// |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 514 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 515 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 516 | /// scalar floating point type. |
| 517 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 518 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 519 | assert(BT && "Not a floating point type!"); |
| 520 | switch (BT->getKind()) { |
| 521 | default: assert(0 && "Not a floating point type!"); |
| 522 | case BuiltinType::Float: return Target.getFloatFormat(); |
| 523 | case BuiltinType::Double: return Target.getDoubleFormat(); |
| 524 | case BuiltinType::LongDouble: return Target.getLongDoubleFormat(); |
| 525 | } |
| 526 | } |
| 527 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 528 | /// getDeclAlign - Return a conservative estimate of the alignment of the |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 529 | /// specified decl. Note that bitfields do not have a valid alignment, so |
| 530 | /// this method will assert on them. |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 531 | /// If @p RefAsPointee, references are treated like their underlying type |
| 532 | /// (for alignof), else they're treated like pointers (for CodeGen). |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 533 | CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) { |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 534 | unsigned Align = Target.getCharWidth(); |
| 535 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 536 | bool UseAlignAttrOnly = false; |
| 537 | if (unsigned AlignFromAttr = D->getMaxAlignment()) { |
| 538 | Align = AlignFromAttr; |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 539 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 540 | // __attribute__((aligned)) can increase or decrease alignment |
| 541 | // *except* on a struct or struct member, where it only increases |
| 542 | // alignment unless 'packed' is also specified. |
| 543 | // |
| 544 | // It is an error for [[align]] to decrease alignment, so we can |
| 545 | // ignore that possibility; Sema should diagnose it. |
| 546 | if (isa<FieldDecl>(D)) { |
| 547 | UseAlignAttrOnly = D->hasAttr<PackedAttr>() || |
| 548 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
| 549 | } else { |
| 550 | UseAlignAttrOnly = true; |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | if (UseAlignAttrOnly) { |
| 555 | // ignore type of value |
| 556 | } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 557 | QualType T = VD->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 558 | if (const ReferenceType* RT = T->getAs<ReferenceType>()) { |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 559 | if (RefAsPointee) |
| 560 | T = RT->getPointeeType(); |
| 561 | else |
| 562 | T = getPointerType(RT->getPointeeType()); |
| 563 | } |
| 564 | if (!T->isIncompleteType() && !T->isFunctionType()) { |
Rafael Espindola | 6deecb0 | 2010-06-04 23:15:27 +0000 | [diff] [blame] | 565 | unsigned MinWidth = Target.getLargeArrayMinWidth(); |
| 566 | unsigned ArrayAlign = Target.getLargeArrayAlign(); |
| 567 | if (isa<VariableArrayType>(T) && MinWidth != 0) |
| 568 | Align = std::max(Align, ArrayAlign); |
| 569 | if (ConstantArrayType *CT = dyn_cast<ConstantArrayType>(T)) { |
| 570 | unsigned Size = getTypeSize(CT); |
| 571 | if (MinWidth != 0 && MinWidth <= Size) |
| 572 | Align = std::max(Align, ArrayAlign); |
| 573 | } |
Anders Carlsson | 4cc2cfd | 2009-04-10 04:47:03 +0000 | [diff] [blame] | 574 | // Incomplete or function types default to 1. |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 575 | while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T)) |
| 576 | T = cast<ArrayType>(T)->getElementType(); |
| 577 | |
| 578 | Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); |
| 579 | } |
Charles Davis | 05f6247 | 2010-02-23 04:52:00 +0000 | [diff] [blame] | 580 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) { |
| 581 | // In the case of a field in a packed struct, we want the minimum |
| 582 | // of the alignment of the field and the alignment of the struct. |
| 583 | Align = std::min(Align, |
| 584 | getPreferredTypeAlign(FD->getParent()->getTypeForDecl())); |
| 585 | } |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 586 | } |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 587 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 588 | return CharUnits::fromQuantity(Align / Target.getCharWidth()); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 589 | } |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 590 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 591 | std::pair<CharUnits, CharUnits> |
| 592 | ASTContext::getTypeInfoInChars(const Type *T) { |
| 593 | std::pair<uint64_t, unsigned> Info = getTypeInfo(T); |
| 594 | return std::make_pair(CharUnits::fromQuantity(Info.first / getCharWidth()), |
| 595 | CharUnits::fromQuantity(Info.second / getCharWidth())); |
| 596 | } |
| 597 | |
| 598 | std::pair<CharUnits, CharUnits> |
| 599 | ASTContext::getTypeInfoInChars(QualType T) { |
| 600 | return getTypeInfoInChars(T.getTypePtr()); |
| 601 | } |
| 602 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 603 | /// getTypeSize - Return the size of the specified type, in bits. This method |
| 604 | /// does not work on incomplete types. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 605 | /// |
| 606 | /// FIXME: Pointers into different addr spaces could have different sizes and |
| 607 | /// alignment requirements: getPointerInfo should take an AddrSpace, this |
| 608 | /// should take a QualType, &c. |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 609 | std::pair<uint64_t, unsigned> |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 610 | ASTContext::getTypeInfo(const Type *T) { |
Mike Stump | 5e30100 | 2009-02-27 18:32:39 +0000 | [diff] [blame] | 611 | uint64_t Width=0; |
| 612 | unsigned Align=8; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 613 | switch (T->getTypeClass()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 614 | #define TYPE(Class, Base) |
| 615 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 616 | #define NON_CANONICAL_TYPE(Class, Base) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 617 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 618 | #include "clang/AST/TypeNodes.def" |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 619 | assert(false && "Should not see dependent types"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 620 | break; |
| 621 | |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 622 | case Type::FunctionNoProto: |
| 623 | case Type::FunctionProto: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 624 | // GCC extension: alignof(function) = 32 bits |
| 625 | Width = 0; |
| 626 | Align = 32; |
| 627 | break; |
| 628 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 629 | case Type::IncompleteArray: |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 630 | case Type::VariableArray: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 631 | Width = 0; |
| 632 | Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); |
| 633 | break; |
| 634 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 635 | case Type::ConstantArray: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 636 | const ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 637 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 638 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 639 | Width = EltInfo.first*CAT->getSize().getZExtValue(); |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 640 | Align = EltInfo.second; |
| 641 | break; |
Christopher Lamb | 5c09a02 | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 642 | } |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 643 | case Type::ExtVector: |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 644 | case Type::Vector: { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 645 | const VectorType *VT = cast<VectorType>(T); |
| 646 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType()); |
| 647 | Width = EltInfo.first*VT->getNumElements(); |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 648 | Align = Width; |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 649 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 650 | // This happens for non-power-of-2 length vectors. |
Dan Gohman | 8eefcd3 | 2010-04-21 23:32:43 +0000 | [diff] [blame] | 651 | if (Align & (Align-1)) { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 652 | Align = llvm::NextPowerOf2(Align); |
| 653 | Width = llvm::RoundUpToAlignment(Width, Align); |
| 654 | } |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 655 | break; |
| 656 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 657 | |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 658 | case Type::Builtin: |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 659 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 660 | default: assert(0 && "Unknown builtin type!"); |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 661 | case BuiltinType::Void: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 662 | // GCC extension: alignof(void) = 8 bits. |
| 663 | Width = 0; |
| 664 | Align = 8; |
| 665 | break; |
| 666 | |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 667 | case BuiltinType::Bool: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 668 | Width = Target.getBoolWidth(); |
| 669 | Align = Target.getBoolAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 670 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 671 | case BuiltinType::Char_S: |
| 672 | case BuiltinType::Char_U: |
| 673 | case BuiltinType::UChar: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 674 | case BuiltinType::SChar: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 675 | Width = Target.getCharWidth(); |
| 676 | Align = Target.getCharAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 677 | break; |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 678 | case BuiltinType::WChar: |
| 679 | Width = Target.getWCharWidth(); |
| 680 | Align = Target.getWCharAlign(); |
| 681 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 682 | case BuiltinType::Char16: |
| 683 | Width = Target.getChar16Width(); |
| 684 | Align = Target.getChar16Align(); |
| 685 | break; |
| 686 | case BuiltinType::Char32: |
| 687 | Width = Target.getChar32Width(); |
| 688 | Align = Target.getChar32Align(); |
| 689 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 690 | case BuiltinType::UShort: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 691 | case BuiltinType::Short: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 692 | Width = Target.getShortWidth(); |
| 693 | Align = Target.getShortAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 694 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 695 | case BuiltinType::UInt: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 696 | case BuiltinType::Int: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 697 | Width = Target.getIntWidth(); |
| 698 | Align = Target.getIntAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 699 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 700 | case BuiltinType::ULong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 701 | case BuiltinType::Long: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 702 | Width = Target.getLongWidth(); |
| 703 | Align = Target.getLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 704 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 705 | case BuiltinType::ULongLong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 706 | case BuiltinType::LongLong: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 707 | Width = Target.getLongLongWidth(); |
| 708 | Align = Target.getLongLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 709 | break; |
Chris Lattner | ec16cb9 | 2009-04-30 02:55:13 +0000 | [diff] [blame] | 710 | case BuiltinType::Int128: |
| 711 | case BuiltinType::UInt128: |
| 712 | Width = 128; |
| 713 | Align = 128; // int128_t is 128-bit aligned on all targets. |
| 714 | break; |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 715 | case BuiltinType::Float: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 716 | Width = Target.getFloatWidth(); |
| 717 | Align = Target.getFloatAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 718 | break; |
| 719 | case BuiltinType::Double: |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 720 | Width = Target.getDoubleWidth(); |
| 721 | Align = Target.getDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 722 | break; |
| 723 | case BuiltinType::LongDouble: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 724 | Width = Target.getLongDoubleWidth(); |
| 725 | Align = Target.getLongDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 726 | break; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 727 | case BuiltinType::NullPtr: |
| 728 | Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) |
| 729 | Align = Target.getPointerAlign(0); // == sizeof(void*) |
Sebastian Redl | 1590d9c | 2009-05-27 19:34:06 +0000 | [diff] [blame] | 730 | break; |
Fariborz Jahanian | e04f5fc | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 731 | case BuiltinType::ObjCId: |
| 732 | case BuiltinType::ObjCClass: |
| 733 | case BuiltinType::ObjCSel: |
| 734 | Width = Target.getPointerWidth(0); |
| 735 | Align = Target.getPointerAlign(0); |
| 736 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 737 | } |
Chris Lattner | bfef6d7 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 738 | break; |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 739 | case Type::ObjCObjectPointer: |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 740 | Width = Target.getPointerWidth(0); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 741 | Align = Target.getPointerAlign(0); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 742 | break; |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 743 | case Type::BlockPointer: { |
| 744 | unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace(); |
| 745 | Width = Target.getPointerWidth(AS); |
| 746 | Align = Target.getPointerAlign(AS); |
| 747 | break; |
| 748 | } |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 749 | case Type::LValueReference: |
| 750 | case Type::RValueReference: { |
| 751 | // alignof and sizeof should never enter this code path here, so we go |
| 752 | // the pointer route. |
| 753 | unsigned AS = cast<ReferenceType>(T)->getPointeeType().getAddressSpace(); |
| 754 | Width = Target.getPointerWidth(AS); |
| 755 | Align = Target.getPointerAlign(AS); |
| 756 | break; |
| 757 | } |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 758 | case Type::Pointer: { |
| 759 | unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace(); |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 760 | Width = Target.getPointerWidth(AS); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 761 | Align = Target.getPointerAlign(AS); |
| 762 | break; |
| 763 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 764 | case Type::MemberPointer: { |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 765 | const MemberPointerType *MPT = cast<MemberPointerType>(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 766 | std::pair<uint64_t, unsigned> PtrDiffInfo = |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 767 | getTypeInfo(getPointerDiffType()); |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 768 | Width = PtrDiffInfo.first * ABI->getMemberPointerSize(MPT); |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 769 | Align = PtrDiffInfo.second; |
| 770 | break; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 771 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 772 | case Type::Complex: { |
| 773 | // Complex types have the same alignment as their elements, but twice the |
| 774 | // size. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 775 | std::pair<uint64_t, unsigned> EltInfo = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 776 | getTypeInfo(cast<ComplexType>(T)->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 777 | Width = EltInfo.first*2; |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 778 | Align = EltInfo.second; |
| 779 | break; |
| 780 | } |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 781 | case Type::ObjCObject: |
| 782 | return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr()); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 783 | case Type::ObjCInterface: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 784 | const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 785 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
| 786 | Width = Layout.getSize(); |
| 787 | Align = Layout.getAlignment(); |
| 788 | break; |
| 789 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 790 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 791 | case Type::Enum: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 792 | const TagType *TT = cast<TagType>(T); |
| 793 | |
| 794 | if (TT->getDecl()->isInvalidDecl()) { |
Chris Lattner | 8389eab | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 795 | Width = 1; |
| 796 | Align = 1; |
| 797 | break; |
| 798 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 799 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 800 | if (const EnumType *ET = dyn_cast<EnumType>(TT)) |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 801 | return getTypeInfo(ET->getDecl()->getIntegerType()); |
| 802 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 803 | const RecordType *RT = cast<RecordType>(TT); |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 804 | const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); |
| 805 | Width = Layout.getSize(); |
| 806 | Align = Layout.getAlignment(); |
Chris Lattner | dc0d73e | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 807 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 808 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 809 | |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 810 | case Type::SubstTemplateTypeParm: |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 811 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> |
| 812 | getReplacementType().getTypePtr()); |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 813 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 814 | case Type::Paren: |
| 815 | return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr()); |
| 816 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 817 | case Type::Typedef: { |
| 818 | const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl(); |
Douglas Gregor | df1367a | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 819 | std::pair<uint64_t, unsigned> Info |
| 820 | = getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); |
| 821 | Align = std::max(Typedef->getMaxAlignment(), Info.second); |
| 822 | Width = Info.first; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 823 | break; |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 824 | } |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 825 | |
| 826 | case Type::TypeOfExpr: |
| 827 | return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType() |
| 828 | .getTypePtr()); |
| 829 | |
| 830 | case Type::TypeOf: |
| 831 | return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr()); |
| 832 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 833 | case Type::Decltype: |
| 834 | return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType() |
| 835 | .getTypePtr()); |
| 836 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 837 | case Type::Elaborated: |
| 838 | return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 839 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 840 | case Type::TemplateSpecialization: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 841 | assert(getCanonicalType(T) != T && |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 842 | "Cannot request the size of a dependent type"); |
| 843 | // FIXME: this is likely to be wrong once we support template |
| 844 | // aliases, since a template alias could refer to a typedef that |
| 845 | // has an __aligned__ attribute on it. |
| 846 | return getTypeInfo(getCanonicalType(T)); |
| 847 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 848 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 849 | assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2"); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 850 | return std::make_pair(Width, Align); |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 851 | } |
| 852 | |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 853 | /// getTypeSizeInChars - Return the size of the specified type, in characters. |
| 854 | /// This method does not work on incomplete types. |
| 855 | CharUnits ASTContext::getTypeSizeInChars(QualType T) { |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 856 | return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth()); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 857 | } |
| 858 | CharUnits ASTContext::getTypeSizeInChars(const Type *T) { |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 859 | return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth()); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Ken Dyck | 16e20cc | 2010-01-26 17:25:18 +0000 | [diff] [blame] | 862 | /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 863 | /// characters. This method does not work on incomplete types. |
| 864 | CharUnits ASTContext::getTypeAlignInChars(QualType T) { |
| 865 | return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth()); |
| 866 | } |
| 867 | CharUnits ASTContext::getTypeAlignInChars(const Type *T) { |
| 868 | return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth()); |
| 869 | } |
| 870 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 871 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 872 | /// type for the current target in bits. This can be different than the ABI |
| 873 | /// alignment in cases where it is beneficial for performance to overalign |
| 874 | /// a data type. |
| 875 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) { |
| 876 | unsigned ABIAlign = getTypeAlign(T); |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 877 | |
| 878 | // Double and long long should be naturally aligned if possible. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 879 | if (const ComplexType* CT = T->getAs<ComplexType>()) |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 880 | T = CT->getElementType().getTypePtr(); |
| 881 | if (T->isSpecificBuiltinType(BuiltinType::Double) || |
| 882 | T->isSpecificBuiltinType(BuiltinType::LongLong)) |
| 883 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); |
| 884 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 885 | return ABIAlign; |
| 886 | } |
| 887 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 888 | /// ShallowCollectObjCIvars - |
| 889 | /// Collect all ivars, including those synthesized, in the current class. |
| 890 | /// |
| 891 | void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI, |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 892 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) { |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 893 | // FIXME. This need be removed but there are two many places which |
| 894 | // assume const-ness of ObjCInterfaceDecl |
| 895 | ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI); |
| 896 | for (ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv; |
| 897 | Iv= Iv->getNextIvar()) |
| 898 | Ivars.push_back(Iv); |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 899 | } |
| 900 | |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 901 | /// DeepCollectObjCIvars - |
| 902 | /// This routine first collects all declared, but not synthesized, ivars in |
| 903 | /// super class and then collects all ivars, including those synthesized for |
| 904 | /// current class. This routine is used for implementation of current class |
| 905 | /// when all ivars, declared and synthesized are known. |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 906 | /// |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 907 | void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 908 | bool leafClass, |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 909 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) { |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 910 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
| 911 | DeepCollectObjCIvars(SuperClass, false, Ivars); |
| 912 | if (!leafClass) { |
| 913 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 914 | E = OI->ivar_end(); I != E; ++I) |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 915 | Ivars.push_back(*I); |
| 916 | } |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 917 | else |
| 918 | ShallowCollectObjCIvars(OI, Ivars); |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 919 | } |
| 920 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 921 | /// CollectInheritedProtocols - Collect all protocols in current class and |
| 922 | /// those inherited by it. |
| 923 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 924 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 925 | if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 926 | // We can use protocol_iterator here instead of |
| 927 | // all_referenced_protocol_iterator since we are walking all categories. |
| 928 | for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(), |
| 929 | PE = OI->all_referenced_protocol_end(); P != PE; ++P) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 930 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 931 | Protocols.insert(Proto); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 932 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 933 | PE = Proto->protocol_end(); P != PE; ++P) { |
| 934 | Protocols.insert(*P); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 935 | CollectInheritedProtocols(*P, Protocols); |
| 936 | } |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 937 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 938 | |
| 939 | // Categories of this Interface. |
| 940 | for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList(); |
| 941 | CDeclChain; CDeclChain = CDeclChain->getNextClassCategory()) |
| 942 | CollectInheritedProtocols(CDeclChain, Protocols); |
| 943 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) |
| 944 | while (SD) { |
| 945 | CollectInheritedProtocols(SD, Protocols); |
| 946 | SD = SD->getSuperClass(); |
| 947 | } |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 948 | } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 949 | for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(), |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 950 | PE = OC->protocol_end(); P != PE; ++P) { |
| 951 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 952 | Protocols.insert(Proto); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 953 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 954 | PE = Proto->protocol_end(); P != PE; ++P) |
| 955 | CollectInheritedProtocols(*P, Protocols); |
| 956 | } |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 957 | } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 958 | for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(), |
| 959 | PE = OP->protocol_end(); P != PE; ++P) { |
| 960 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 961 | Protocols.insert(Proto); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 962 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 963 | PE = Proto->protocol_end(); P != PE; ++P) |
| 964 | CollectInheritedProtocols(*P, Protocols); |
| 965 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 966 | } |
| 967 | } |
| 968 | |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 969 | unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) { |
| 970 | unsigned count = 0; |
| 971 | // Count ivars declared in class extension. |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 972 | for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl; |
| 973 | CDecl = CDecl->getNextClassExtension()) |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 974 | count += CDecl->ivar_size(); |
| 975 | |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 976 | // Count ivar defined in this class's implementation. This |
| 977 | // includes synthesized ivars. |
| 978 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 979 | count += ImplDecl->ivar_size(); |
| 980 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 981 | return count; |
| 982 | } |
| 983 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 984 | /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. |
| 985 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
| 986 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 987 | I = ObjCImpls.find(D); |
| 988 | if (I != ObjCImpls.end()) |
| 989 | return cast<ObjCImplementationDecl>(I->second); |
| 990 | return 0; |
| 991 | } |
| 992 | /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. |
| 993 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
| 994 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 995 | I = ObjCImpls.find(D); |
| 996 | if (I != ObjCImpls.end()) |
| 997 | return cast<ObjCCategoryImplDecl>(I->second); |
| 998 | return 0; |
| 999 | } |
| 1000 | |
| 1001 | /// \brief Set the implementation of ObjCInterfaceDecl. |
| 1002 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
| 1003 | ObjCImplementationDecl *ImplD) { |
| 1004 | assert(IFaceD && ImplD && "Passed null params"); |
| 1005 | ObjCImpls[IFaceD] = ImplD; |
| 1006 | } |
| 1007 | /// \brief Set the implementation of ObjCCategoryDecl. |
| 1008 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
| 1009 | ObjCCategoryImplDecl *ImplD) { |
| 1010 | assert(CatD && ImplD && "Passed null params"); |
| 1011 | ObjCImpls[CatD] = ImplD; |
| 1012 | } |
| 1013 | |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1014 | /// \brief Get the copy initialization expression of VarDecl,or NULL if |
| 1015 | /// none exists. |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1016 | Expr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) { |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1017 | assert(VD && "Passed null params"); |
| 1018 | assert(VD->hasAttr<BlocksAttr>() && |
| 1019 | "getBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1020 | llvm::DenseMap<const VarDecl*, Expr*>::iterator |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1021 | I = BlockVarCopyInits.find(VD); |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1022 | return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0; |
| 1023 | } |
| 1024 | |
| 1025 | /// \brief Set the copy inialization expression of a block var decl. |
| 1026 | void ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) { |
| 1027 | assert(VD && Init && "Passed null params"); |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1028 | assert(VD->hasAttr<BlocksAttr>() && |
| 1029 | "setBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1030 | BlockVarCopyInits[VD] = Init; |
| 1031 | } |
| 1032 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1033 | /// \brief Allocate an uninitialized TypeSourceInfo. |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1034 | /// |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1035 | /// The caller should initialize the memory held by TypeSourceInfo using |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1036 | /// the TypeLoc wrappers. |
| 1037 | /// |
| 1038 | /// \param T the type that will be the basis for type source info. This type |
| 1039 | /// should refer to how the declarator was written in source code, not to |
| 1040 | /// what type semantic analysis resolved the declarator to. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1041 | TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T, |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1042 | unsigned DataSize) { |
| 1043 | if (!DataSize) |
| 1044 | DataSize = TypeLoc::getFullDataSizeForType(T); |
| 1045 | else |
| 1046 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1047 | "incorrect data size provided to CreateTypeSourceInfo!"); |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1048 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1049 | TypeSourceInfo *TInfo = |
| 1050 | (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8); |
| 1051 | new (TInfo) TypeSourceInfo(T); |
| 1052 | return TInfo; |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1055 | TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, |
John McCall | a4eb74d | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 1056 | SourceLocation L) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1057 | TypeSourceInfo *DI = CreateTypeSourceInfo(T); |
John McCall | a4eb74d | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 1058 | DI->getTypeLoc().initialize(L); |
| 1059 | return DI; |
| 1060 | } |
| 1061 | |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1062 | const ASTRecordLayout & |
| 1063 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) { |
| 1064 | return getObjCLayout(D, 0); |
| 1065 | } |
| 1066 | |
| 1067 | const ASTRecordLayout & |
| 1068 | ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) { |
| 1069 | return getObjCLayout(D->getClassInterface(), D); |
| 1070 | } |
| 1071 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1072 | //===----------------------------------------------------------------------===// |
| 1073 | // Type creation/memoization methods |
| 1074 | //===----------------------------------------------------------------------===// |
| 1075 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1076 | QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) { |
| 1077 | unsigned Fast = Quals.getFastQualifiers(); |
| 1078 | Quals.removeFastQualifiers(); |
| 1079 | |
| 1080 | // Check if we've already instantiated this type. |
| 1081 | llvm::FoldingSetNodeID ID; |
| 1082 | ExtQuals::Profile(ID, TypeNode, Quals); |
| 1083 | void *InsertPos = 0; |
| 1084 | if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) { |
| 1085 | assert(EQ->getQualifiers() == Quals); |
| 1086 | QualType T = QualType(EQ, Fast); |
| 1087 | return T; |
| 1088 | } |
| 1089 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 1090 | ExtQuals *New = new (*this, TypeAlignment) ExtQuals(TypeNode, Quals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1091 | ExtQualNodes.InsertNode(New, InsertPos); |
| 1092 | QualType T = QualType(New, Fast); |
| 1093 | return T; |
| 1094 | } |
| 1095 | |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 1096 | QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1097 | QualType CanT = getCanonicalType(T); |
| 1098 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1099 | return T; |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1100 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1101 | // If we are composing extended qualifiers together, merge together |
| 1102 | // into one ExtQuals node. |
| 1103 | QualifierCollector Quals; |
| 1104 | const Type *TypeNode = Quals.strip(T); |
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 | // If this type already has an address space specified, it cannot get |
| 1107 | // another one. |
| 1108 | assert(!Quals.hasAddressSpace() && |
| 1109 | "Type cannot be in multiple addr spaces!"); |
| 1110 | Quals.addAddressSpace(AddressSpace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1111 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1112 | return getExtQualType(TypeNode, Quals); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1115 | QualType ASTContext::getObjCGCQualType(QualType T, |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1116 | Qualifiers::GC GCAttr) { |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1117 | QualType CanT = getCanonicalType(T); |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1118 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1119 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1120 | |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1121 | if (T->isPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1122 | QualType Pointee = T->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 1123 | if (Pointee->isAnyPointerType()) { |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1124 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 1125 | return getPointerType(ResultType); |
| 1126 | } |
| 1127 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1128 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1129 | // If we are composing extended qualifiers together, merge together |
| 1130 | // into one ExtQuals node. |
| 1131 | QualifierCollector Quals; |
| 1132 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1133 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1134 | // If this type already has an ObjCGC specified, it cannot get |
| 1135 | // another one. |
| 1136 | assert(!Quals.hasObjCGCAttr() && |
| 1137 | "Type cannot have multiple ObjCGCs!"); |
| 1138 | Quals.addObjCGCAttr(GCAttr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1139 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1140 | return getExtQualType(TypeNode, Quals); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1141 | } |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1142 | |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 1143 | const FunctionType *ASTContext::adjustFunctionType(const FunctionType *T, |
| 1144 | FunctionType::ExtInfo Info) { |
| 1145 | if (T->getExtInfo() == Info) |
| 1146 | return T; |
| 1147 | |
| 1148 | QualType Result; |
| 1149 | if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) { |
| 1150 | Result = getFunctionNoProtoType(FNPT->getResultType(), Info); |
| 1151 | } else { |
| 1152 | const FunctionProtoType *FPT = cast<FunctionProtoType>(T); |
| 1153 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 1154 | EPI.ExtInfo = Info; |
| 1155 | Result = getFunctionType(FPT->getResultType(), FPT->arg_type_begin(), |
| 1156 | FPT->getNumArgs(), EPI); |
| 1157 | } |
| 1158 | |
| 1159 | return cast<FunctionType>(Result.getTypePtr()); |
| 1160 | } |
| 1161 | |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1162 | static QualType getExtFunctionType(ASTContext& Context, QualType T, |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 1163 | FunctionType::ExtInfo Info) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1164 | QualType ResultType; |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1165 | if (const PointerType *Pointer = T->getAs<PointerType>()) { |
| 1166 | QualType Pointee = Pointer->getPointeeType(); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1167 | ResultType = getExtFunctionType(Context, Pointee, Info); |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1168 | if (ResultType == Pointee) |
| 1169 | return T; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1170 | |
| 1171 | ResultType = Context.getPointerType(ResultType); |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1172 | } else if (const ParenType *Paren = T->getAs<ParenType>()) { |
| 1173 | QualType Inner = Paren->getInnerType(); |
| 1174 | ResultType = getExtFunctionType(Context, Inner, Info); |
| 1175 | if (ResultType == Inner) |
| 1176 | return T; |
| 1177 | |
| 1178 | ResultType = Context.getParenType(ResultType); |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1179 | } else if (const BlockPointerType *BlockPointer |
| 1180 | = T->getAs<BlockPointerType>()) { |
| 1181 | QualType Pointee = BlockPointer->getPointeeType(); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1182 | ResultType = getExtFunctionType(Context, Pointee, Info); |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1183 | if (ResultType == Pointee) |
| 1184 | return T; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1185 | |
| 1186 | ResultType = Context.getBlockPointerType(ResultType); |
Douglas Gregor | afac01d | 2010-09-01 16:29:03 +0000 | [diff] [blame] | 1187 | } else if (const MemberPointerType *MemberPointer |
| 1188 | = T->getAs<MemberPointerType>()) { |
| 1189 | QualType Pointee = MemberPointer->getPointeeType(); |
| 1190 | ResultType = getExtFunctionType(Context, Pointee, Info); |
| 1191 | if (ResultType == Pointee) |
| 1192 | return T; |
| 1193 | |
| 1194 | ResultType = Context.getMemberPointerType(ResultType, |
| 1195 | MemberPointer->getClass()); |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1196 | } else if (const FunctionType *F = T->getAs<FunctionType>()) { |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 1197 | ResultType = QualType(Context.adjustFunctionType(F, Info), 0); |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1198 | } else |
| 1199 | return T; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1200 | |
| 1201 | return Context.getQualifiedType(ResultType, T.getLocalQualifiers()); |
| 1202 | } |
| 1203 | |
| 1204 | QualType ASTContext::getNoReturnType(QualType T, bool AddNoReturn) { |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 1205 | FunctionType::ExtInfo Info = getFunctionExtInfo(T); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1206 | return getExtFunctionType(*this, T, Info.withNoReturn(AddNoReturn)); |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1209 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 1210 | /// number with the specified element type. |
| 1211 | QualType ASTContext::getComplexType(QualType T) { |
| 1212 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1213 | // structure. |
| 1214 | llvm::FoldingSetNodeID ID; |
| 1215 | ComplexType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1216 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1217 | void *InsertPos = 0; |
| 1218 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1219 | return QualType(CT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1220 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1221 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1222 | // so fill in the canonical type field. |
| 1223 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1224 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1225 | Canonical = getComplexType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1226 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1227 | // Get the new insert position for the node we care about. |
| 1228 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1229 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1230 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1231 | ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1232 | Types.push_back(New); |
| 1233 | ComplexTypes.InsertNode(New, InsertPos); |
| 1234 | return QualType(New, 0); |
| 1235 | } |
| 1236 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1237 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 1238 | /// the specified type. |
| 1239 | QualType ASTContext::getPointerType(QualType T) { |
| 1240 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1241 | // structure. |
| 1242 | llvm::FoldingSetNodeID ID; |
| 1243 | PointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1244 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1245 | void *InsertPos = 0; |
| 1246 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1247 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1248 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1249 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1250 | // so fill in the canonical type field. |
| 1251 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1252 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1253 | Canonical = getPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1254 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1255 | // Get the new insert position for the node we care about. |
| 1256 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1257 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1258 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1259 | PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1260 | Types.push_back(New); |
| 1261 | PointerTypes.InsertNode(New, InsertPos); |
| 1262 | return QualType(New, 0); |
| 1263 | } |
| 1264 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1265 | /// getBlockPointerType - Return the uniqued reference to the type for |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1266 | /// a pointer to the specified block. |
| 1267 | QualType ASTContext::getBlockPointerType(QualType T) { |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1268 | assert(T->isFunctionType() && "block of function types only"); |
| 1269 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1270 | // structure. |
| 1271 | llvm::FoldingSetNodeID ID; |
| 1272 | BlockPointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1273 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1274 | void *InsertPos = 0; |
| 1275 | if (BlockPointerType *PT = |
| 1276 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1277 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1278 | |
| 1279 | // 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] | 1280 | // type either so fill in the canonical type field. |
| 1281 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1282 | if (!T.isCanonical()) { |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1283 | Canonical = getBlockPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1284 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1285 | // Get the new insert position for the node we care about. |
| 1286 | BlockPointerType *NewIP = |
| 1287 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1288 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1289 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1290 | BlockPointerType *New |
| 1291 | = new (*this, TypeAlignment) BlockPointerType(T, Canonical); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1292 | Types.push_back(New); |
| 1293 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 1294 | return QualType(New, 0); |
| 1295 | } |
| 1296 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1297 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 1298 | /// lvalue reference to the specified type. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1299 | QualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1300 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1301 | // structure. |
| 1302 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1303 | ReferenceType::Profile(ID, T, SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1304 | |
| 1305 | void *InsertPos = 0; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1306 | if (LValueReferenceType *RT = |
| 1307 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1308 | return QualType(RT, 0); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1309 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1310 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1311 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1312 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1313 | // either, so fill in the canonical type field. |
| 1314 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1315 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
| 1316 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1317 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1318 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1319 | // Get the new insert position for the node we care about. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1320 | LValueReferenceType *NewIP = |
| 1321 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1322 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1325 | LValueReferenceType *New |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1326 | = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, |
| 1327 | SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1328 | Types.push_back(New); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1329 | LValueReferenceTypes.InsertNode(New, InsertPos); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1330 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1331 | return QualType(New, 0); |
| 1332 | } |
| 1333 | |
| 1334 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 1335 | /// rvalue reference to the specified type. |
| 1336 | QualType ASTContext::getRValueReferenceType(QualType T) { |
| 1337 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1338 | // structure. |
| 1339 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1340 | ReferenceType::Profile(ID, T, false); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1341 | |
| 1342 | void *InsertPos = 0; |
| 1343 | if (RValueReferenceType *RT = |
| 1344 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1345 | return QualType(RT, 0); |
| 1346 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1347 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1348 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1349 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1350 | // either, so fill in the canonical type field. |
| 1351 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1352 | if (InnerRef || !T.isCanonical()) { |
| 1353 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1354 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1355 | |
| 1356 | // Get the new insert position for the node we care about. |
| 1357 | RValueReferenceType *NewIP = |
| 1358 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1359 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
| 1360 | } |
| 1361 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1362 | RValueReferenceType *New |
| 1363 | = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1364 | Types.push_back(New); |
| 1365 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1366 | return QualType(New, 0); |
| 1367 | } |
| 1368 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1369 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 1370 | /// member pointer to the specified type, in the specified class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1371 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1372 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1373 | // structure. |
| 1374 | llvm::FoldingSetNodeID ID; |
| 1375 | MemberPointerType::Profile(ID, T, Cls); |
| 1376 | |
| 1377 | void *InsertPos = 0; |
| 1378 | if (MemberPointerType *PT = |
| 1379 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1380 | return QualType(PT, 0); |
| 1381 | |
| 1382 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 1383 | // type either, so fill in the canonical type field. |
| 1384 | QualType Canonical; |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1385 | if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1386 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 1387 | |
| 1388 | // Get the new insert position for the node we care about. |
| 1389 | MemberPointerType *NewIP = |
| 1390 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1391 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
| 1392 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1393 | MemberPointerType *New |
| 1394 | = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1395 | Types.push_back(New); |
| 1396 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 1397 | return QualType(New, 0); |
| 1398 | } |
| 1399 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1400 | /// getConstantArrayType - Return the unique reference to the type for an |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1401 | /// array of the specified element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1402 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1403 | const llvm::APInt &ArySizeIn, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1404 | ArrayType::ArraySizeModifier ASM, |
| 1405 | unsigned EltTypeQuals) { |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1406 | assert((EltTy->isDependentType() || |
| 1407 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && |
Eli Friedman | 587cbdf | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1408 | "Constant array of VLAs is illegal!"); |
| 1409 | |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1410 | // Convert the array size into a canonical width matching the pointer size for |
| 1411 | // the target. |
| 1412 | llvm::APInt ArySize(ArySizeIn); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1413 | ArySize = |
| 1414 | ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1415 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1416 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 0be2ef2 | 2009-02-19 17:31:02 +0000 | [diff] [blame] | 1417 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1418 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1419 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1420 | if (ConstantArrayType *ATP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1421 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1422 | return QualType(ATP, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1423 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1424 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1425 | // so fill in the canonical type field. |
| 1426 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1427 | if (!EltTy.isCanonical()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1428 | Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1429 | ASM, EltTypeQuals); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1430 | // Get the new insert position for the node we care about. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1431 | ConstantArrayType *NewIP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1432 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1433 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1434 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1435 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1436 | ConstantArrayType *New = new(*this,TypeAlignment) |
| 1437 | ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1438 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1439 | Types.push_back(New); |
| 1440 | return QualType(New, 0); |
| 1441 | } |
| 1442 | |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1443 | /// getIncompleteArrayType - Returns a unique reference to the type for a |
| 1444 | /// incomplete array of the specified element type. |
| 1445 | QualType ASTContext::getUnknownSizeVariableArrayType(QualType Ty) { |
| 1446 | QualType ElemTy = getBaseElementType(Ty); |
| 1447 | DeclarationName Name; |
| 1448 | llvm::SmallVector<QualType, 8> ATypes; |
| 1449 | QualType ATy = Ty; |
| 1450 | while (const ArrayType *AT = getAsArrayType(ATy)) { |
| 1451 | ATypes.push_back(ATy); |
| 1452 | ATy = AT->getElementType(); |
| 1453 | } |
| 1454 | for (int i = ATypes.size() - 1; i >= 0; i--) { |
| 1455 | if (const VariableArrayType *VAT = getAsVariableArrayType(ATypes[i])) { |
| 1456 | ElemTy = getVariableArrayType(ElemTy, /*ArraySize*/0, ArrayType::Star, |
| 1457 | 0, VAT->getBracketsRange()); |
| 1458 | } |
| 1459 | else if (const ConstantArrayType *CAT = getAsConstantArrayType(ATypes[i])) { |
| 1460 | llvm::APSInt ConstVal(CAT->getSize()); |
| 1461 | ElemTy = getConstantArrayType(ElemTy, ConstVal, ArrayType::Normal, 0); |
| 1462 | } |
| 1463 | else if (getAsIncompleteArrayType(ATypes[i])) { |
| 1464 | ElemTy = getVariableArrayType(ElemTy, /*ArraySize*/0, ArrayType::Normal, |
| 1465 | 0, SourceRange()); |
| 1466 | } |
| 1467 | else |
| 1468 | assert(false && "DependentArrayType is seen"); |
| 1469 | } |
| 1470 | return ElemTy; |
| 1471 | } |
| 1472 | |
| 1473 | /// getVariableArrayDecayedType - Returns a vla type where known sizes |
| 1474 | /// are replaced with [*] |
| 1475 | QualType ASTContext::getVariableArrayDecayedType(QualType Ty) { |
| 1476 | if (Ty->isPointerType()) { |
| 1477 | QualType BaseType = Ty->getAs<PointerType>()->getPointeeType(); |
| 1478 | if (isa<VariableArrayType>(BaseType)) { |
| 1479 | ArrayType *AT = dyn_cast<ArrayType>(BaseType); |
| 1480 | VariableArrayType *VAT = cast<VariableArrayType>(AT); |
| 1481 | if (VAT->getSizeExpr()) { |
| 1482 | Ty = getUnknownSizeVariableArrayType(BaseType); |
| 1483 | Ty = getPointerType(Ty); |
| 1484 | } |
| 1485 | } |
| 1486 | } |
| 1487 | return Ty; |
| 1488 | } |
| 1489 | |
| 1490 | |
Steve Naroff | bdbf7b0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 1491 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 1492 | /// variable array of the specified element type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1493 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 1494 | Expr *NumElts, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1495 | ArrayType::ArraySizeModifier ASM, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1496 | unsigned EltTypeQuals, |
| 1497 | SourceRange Brackets) { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1498 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 1499 | // that have an expression provided for their size. |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 1500 | QualType CanonType; |
| 1501 | |
| 1502 | if (!EltTy.isCanonical()) { |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 1503 | CanonType = getVariableArrayType(getCanonicalType(EltTy), NumElts, ASM, |
| 1504 | EltTypeQuals, Brackets); |
| 1505 | } |
| 1506 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1507 | VariableArrayType *New = new(*this, TypeAlignment) |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 1508 | VariableArrayType(EltTy, CanonType, NumElts, ASM, EltTypeQuals, Brackets); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1509 | |
| 1510 | VariableArrayTypes.push_back(New); |
| 1511 | Types.push_back(New); |
| 1512 | return QualType(New, 0); |
| 1513 | } |
| 1514 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1515 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 1516 | /// the type for a dependently-sized array of the specified element |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1517 | /// type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1518 | QualType ASTContext::getDependentSizedArrayType(QualType EltTy, |
| 1519 | Expr *NumElts, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1520 | ArrayType::ArraySizeModifier ASM, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1521 | unsigned EltTypeQuals, |
| 1522 | SourceRange Brackets) { |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1523 | assert((!NumElts || NumElts->isTypeDependent() || |
| 1524 | NumElts->isValueDependent()) && |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1525 | "Size must be type- or value-dependent!"); |
| 1526 | |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1527 | void *InsertPos = 0; |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1528 | DependentSizedArrayType *Canon = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1529 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1530 | |
Douglas Gregor | 32adc8b | 2010-10-26 00:51:02 +0000 | [diff] [blame] | 1531 | QualType CanonicalEltTy = getCanonicalType(EltTy); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1532 | if (NumElts) { |
| 1533 | // Dependently-sized array types that do not have a specified |
| 1534 | // number of elements will have their sizes deduced from an |
| 1535 | // initializer. |
Douglas Gregor | 32adc8b | 2010-10-26 00:51:02 +0000 | [diff] [blame] | 1536 | DependentSizedArrayType::Profile(ID, *this, CanonicalEltTy, ASM, |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1537 | EltTypeQuals, NumElts); |
| 1538 | |
| 1539 | Canon = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1540 | } |
| 1541 | |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1542 | DependentSizedArrayType *New; |
| 1543 | if (Canon) { |
| 1544 | // We already have a canonical version of this array type; use it as |
| 1545 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1546 | New = new (*this, TypeAlignment) |
| 1547 | DependentSizedArrayType(*this, EltTy, QualType(Canon, 0), |
| 1548 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | 32adc8b | 2010-10-26 00:51:02 +0000 | [diff] [blame] | 1549 | } else if (CanonicalEltTy == EltTy) { |
| 1550 | // This is a canonical type. Record it. |
| 1551 | New = new (*this, TypeAlignment) |
| 1552 | DependentSizedArrayType(*this, EltTy, QualType(), |
| 1553 | NumElts, ASM, EltTypeQuals, Brackets); |
| 1554 | |
| 1555 | if (NumElts) { |
| 1556 | #ifndef NDEBUG |
| 1557 | DependentSizedArrayType *CanonCheck |
| 1558 | = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1559 | assert(!CanonCheck && "Dependent-sized canonical array type broken"); |
| 1560 | (void)CanonCheck; |
| 1561 | #endif |
| 1562 | DependentSizedArrayTypes.InsertNode(New, InsertPos); |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1563 | } |
Douglas Gregor | 32adc8b | 2010-10-26 00:51:02 +0000 | [diff] [blame] | 1564 | } else { |
| 1565 | QualType Canon = getDependentSizedArrayType(CanonicalEltTy, NumElts, |
| 1566 | ASM, EltTypeQuals, |
| 1567 | SourceRange()); |
| 1568 | New = new (*this, TypeAlignment) |
| 1569 | DependentSizedArrayType(*this, EltTy, Canon, |
| 1570 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1571 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1572 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1573 | Types.push_back(New); |
| 1574 | return QualType(New, 0); |
| 1575 | } |
| 1576 | |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1577 | QualType ASTContext::getIncompleteArrayType(QualType EltTy, |
| 1578 | ArrayType::ArraySizeModifier ASM, |
| 1579 | unsigned EltTypeQuals) { |
| 1580 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 0be2ef2 | 2009-02-19 17:31:02 +0000 | [diff] [blame] | 1581 | IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1582 | |
| 1583 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1584 | if (IncompleteArrayType *ATP = |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1585 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1586 | return QualType(ATP, 0); |
| 1587 | |
| 1588 | // If the element type isn't canonical, this won't be a canonical type |
| 1589 | // either, so fill in the canonical type field. |
| 1590 | QualType Canonical; |
| 1591 | |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1592 | if (!EltTy.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1593 | Canonical = getIncompleteArrayType(getCanonicalType(EltTy), |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1594 | ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1595 | |
| 1596 | // Get the new insert position for the node we care about. |
| 1597 | IncompleteArrayType *NewIP = |
| 1598 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1599 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1600 | } |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1601 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1602 | IncompleteArrayType *New = new (*this, TypeAlignment) |
| 1603 | IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1604 | |
| 1605 | IncompleteArrayTypes.InsertNode(New, InsertPos); |
| 1606 | Types.push_back(New); |
| 1607 | return QualType(New, 0); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1608 | } |
| 1609 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1610 | /// getVectorType - Return the unique reference to a vector type of |
| 1611 | /// 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] | 1612 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1613 | VectorType::VectorKind VecKind) { |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 1614 | BuiltinType *BaseType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1615 | |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 1616 | BaseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); |
| 1617 | assert(BaseType != 0 && "getVectorType(): Expecting a built-in type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1618 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1619 | // Check if we've already instantiated a vector of this type. |
| 1620 | llvm::FoldingSetNodeID ID; |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1621 | VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind); |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1622 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1623 | void *InsertPos = 0; |
| 1624 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1625 | return QualType(VTP, 0); |
| 1626 | |
| 1627 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1628 | // so fill in the canonical type field. |
| 1629 | QualType Canonical; |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1630 | if (!vecType.isCanonical()) { |
Bob Wilson | 231da7e | 2010-11-16 00:32:20 +0000 | [diff] [blame] | 1631 | Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1632 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1633 | // Get the new insert position for the node we care about. |
| 1634 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1635 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1636 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1637 | VectorType *New = new (*this, TypeAlignment) |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1638 | VectorType(vecType, NumElts, Canonical, VecKind); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1639 | VectorTypes.InsertNode(New, InsertPos); |
| 1640 | Types.push_back(New); |
| 1641 | return QualType(New, 0); |
| 1642 | } |
| 1643 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1644 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1645 | /// 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] | 1646 | QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) { |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1647 | BuiltinType *baseType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1648 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1649 | baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1650 | assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1651 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1652 | // Check if we've already instantiated a vector of this type. |
| 1653 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1654 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1655 | VectorType::GenericVector); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1656 | void *InsertPos = 0; |
| 1657 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1658 | return QualType(VTP, 0); |
| 1659 | |
| 1660 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1661 | // so fill in the canonical type field. |
| 1662 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1663 | if (!vecType.isCanonical()) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1664 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1665 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1666 | // Get the new insert position for the node we care about. |
| 1667 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1668 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1669 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1670 | ExtVectorType *New = new (*this, TypeAlignment) |
| 1671 | ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1672 | VectorTypes.InsertNode(New, InsertPos); |
| 1673 | Types.push_back(New); |
| 1674 | return QualType(New, 0); |
| 1675 | } |
| 1676 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1677 | QualType ASTContext::getDependentSizedExtVectorType(QualType vecType, |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1678 | Expr *SizeExpr, |
| 1679 | SourceLocation AttrLoc) { |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1680 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1681 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1682 | SizeExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1683 | |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1684 | void *InsertPos = 0; |
| 1685 | DependentSizedExtVectorType *Canon |
| 1686 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1687 | DependentSizedExtVectorType *New; |
| 1688 | if (Canon) { |
| 1689 | // We already have a canonical version of this array type; use it as |
| 1690 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1691 | New = new (*this, TypeAlignment) |
| 1692 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), |
| 1693 | SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1694 | } else { |
| 1695 | QualType CanonVecTy = getCanonicalType(vecType); |
| 1696 | if (CanonVecTy == vecType) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1697 | New = new (*this, TypeAlignment) |
| 1698 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, |
| 1699 | AttrLoc); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1700 | |
| 1701 | DependentSizedExtVectorType *CanonCheck |
| 1702 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1703 | assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken"); |
| 1704 | (void)CanonCheck; |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1705 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); |
| 1706 | } else { |
| 1707 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 1708 | SourceLocation()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1709 | New = new (*this, TypeAlignment) |
| 1710 | DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1711 | } |
| 1712 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1713 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1714 | Types.push_back(New); |
| 1715 | return QualType(New, 0); |
| 1716 | } |
| 1717 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1718 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1719 | /// |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1720 | QualType ASTContext::getFunctionNoProtoType(QualType ResultTy, |
| 1721 | const FunctionType::ExtInfo &Info) { |
| 1722 | const CallingConv CallConv = Info.getCC(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1723 | // Unique functions, to guarantee there is only one function of a particular |
| 1724 | // structure. |
| 1725 | llvm::FoldingSetNodeID ID; |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1726 | FunctionNoProtoType::Profile(ID, ResultTy, Info); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1727 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1728 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1729 | if (FunctionNoProtoType *FT = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1730 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1731 | return QualType(FT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1732 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1733 | QualType Canonical; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1734 | if (!ResultTy.isCanonical() || |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1735 | getCanonicalCallConv(CallConv) != CallConv) { |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1736 | Canonical = |
| 1737 | getFunctionNoProtoType(getCanonicalType(ResultTy), |
| 1738 | Info.withCallingConv(getCanonicalCallConv(CallConv))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1739 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1740 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1741 | FunctionNoProtoType *NewIP = |
| 1742 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1743 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1744 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1745 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1746 | FunctionNoProtoType *New = new (*this, TypeAlignment) |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1747 | FunctionNoProtoType(ResultTy, Canonical, Info); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1748 | Types.push_back(New); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1749 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1750 | return QualType(New, 0); |
| 1751 | } |
| 1752 | |
| 1753 | /// getFunctionType - Return a normal function type with a typed argument |
| 1754 | /// list. isVariadic indicates whether the argument list includes '...'. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1755 | QualType ASTContext::getFunctionType(QualType ResultTy, |
| 1756 | const QualType *ArgArray, unsigned NumArgs, |
| 1757 | const FunctionProtoType::ExtProtoInfo &EPI) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1758 | // Unique functions, to guarantee there is only one function of a particular |
| 1759 | // structure. |
| 1760 | llvm::FoldingSetNodeID ID; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1761 | FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, EPI); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1762 | |
| 1763 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1764 | if (FunctionProtoType *FTP = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1765 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1766 | return QualType(FTP, 0); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1767 | |
| 1768 | // Determine whether the type being created is already canonical or not. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1769 | bool isCanonical = !EPI.HasExceptionSpec && ResultTy.isCanonical(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1770 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1771 | if (!ArgArray[i].isCanonicalAsParam()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1772 | isCanonical = false; |
| 1773 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1774 | const CallingConv CallConv = EPI.ExtInfo.getCC(); |
| 1775 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1776 | // If this type isn't canonical, get the canonical version of it. |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1777 | // The exception spec is not part of the canonical type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1778 | QualType Canonical; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1779 | if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1780 | llvm::SmallVector<QualType, 16> CanonicalArgs; |
| 1781 | CanonicalArgs.reserve(NumArgs); |
| 1782 | for (unsigned i = 0; i != NumArgs; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1783 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1784 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1785 | FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI; |
| 1786 | if (CanonicalEPI.HasExceptionSpec) { |
| 1787 | CanonicalEPI.HasExceptionSpec = false; |
| 1788 | CanonicalEPI.HasAnyExceptionSpec = false; |
| 1789 | CanonicalEPI.NumExceptions = 0; |
| 1790 | } |
| 1791 | CanonicalEPI.ExtInfo |
| 1792 | = CanonicalEPI.ExtInfo.withCallingConv(getCanonicalCallConv(CallConv)); |
| 1793 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1794 | Canonical = getFunctionType(getCanonicalType(ResultTy), |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1795 | CanonicalArgs.data(), NumArgs, |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1796 | CanonicalEPI); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1797 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1798 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1799 | FunctionProtoType *NewIP = |
| 1800 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1801 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1802 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1803 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1804 | // FunctionProtoType objects are allocated with extra bytes after them |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1805 | // for two variable size arrays (for parameter and exception types) at the |
| 1806 | // end of them. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1807 | size_t Size = sizeof(FunctionProtoType) + |
| 1808 | NumArgs * sizeof(QualType) + |
| 1809 | EPI.NumExceptions * sizeof(QualType); |
| 1810 | FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment); |
| 1811 | new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, Canonical, EPI); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1812 | Types.push_back(FTP); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1813 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1814 | return QualType(FTP, 0); |
| 1815 | } |
| 1816 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1817 | #ifndef NDEBUG |
| 1818 | static bool NeedsInjectedClassNameType(const RecordDecl *D) { |
| 1819 | if (!isa<CXXRecordDecl>(D)) return false; |
| 1820 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 1821 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) |
| 1822 | return true; |
| 1823 | if (RD->getDescribedClassTemplate() && |
| 1824 | !isa<ClassTemplateSpecializationDecl>(RD)) |
| 1825 | return true; |
| 1826 | return false; |
| 1827 | } |
| 1828 | #endif |
| 1829 | |
| 1830 | /// getInjectedClassNameType - Return the unique reference to the |
| 1831 | /// injected class name type for the specified templated declaration. |
| 1832 | QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl, |
| 1833 | QualType TST) { |
| 1834 | assert(NeedsInjectedClassNameType(Decl)); |
| 1835 | if (Decl->TypeForDecl) { |
| 1836 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
Argyrios Kyrtzidis | 37ffed3 | 2010-07-02 11:55:32 +0000 | [diff] [blame] | 1837 | } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDeclaration()) { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1838 | assert(PrevDecl->TypeForDecl && "previous declaration has no type"); |
| 1839 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 1840 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
| 1841 | } else { |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1842 | Decl->TypeForDecl = |
| 1843 | new (*this, TypeAlignment) InjectedClassNameType(Decl, TST); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1844 | Types.push_back(Decl->TypeForDecl); |
| 1845 | } |
| 1846 | return QualType(Decl->TypeForDecl, 0); |
| 1847 | } |
| 1848 | |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1849 | /// getTypeDeclType - Return the unique reference to the type for the |
| 1850 | /// specified type declaration. |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1851 | QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) { |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 1852 | assert(Decl && "Passed null for Decl param"); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1853 | assert(!Decl->TypeForDecl && "TypeForDecl present in slow case"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1854 | |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1855 | if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1856 | return getTypedefType(Typedef); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1857 | |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1858 | assert(!isa<TemplateTypeParmDecl>(Decl) && |
| 1859 | "Template type parameter types are always available."); |
| 1860 | |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1861 | if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1862 | assert(!Record->getPreviousDeclaration() && |
| 1863 | "struct/union has previous declaration"); |
| 1864 | assert(!NeedsInjectedClassNameType(Record)); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 1865 | return getRecordType(Record); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1866 | } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1867 | assert(!Enum->getPreviousDeclaration() && |
| 1868 | "enum has previous declaration"); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 1869 | return getEnumType(Enum); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1870 | } else if (const UnresolvedUsingTypenameDecl *Using = |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1871 | dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) { |
| 1872 | Decl->TypeForDecl = new (*this, TypeAlignment) UnresolvedUsingType(Using); |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 1873 | } else |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1874 | llvm_unreachable("TypeDecl without a type?"); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1875 | |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1876 | Types.push_back(Decl->TypeForDecl); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1877 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1878 | } |
| 1879 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1880 | /// getTypedefType - Return the unique reference to the type for the |
| 1881 | /// specified typename decl. |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 1882 | QualType |
| 1883 | ASTContext::getTypedefType(const TypedefDecl *Decl, QualType Canonical) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1884 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1885 | |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 1886 | if (Canonical.isNull()) |
| 1887 | Canonical = getCanonicalType(Decl->getUnderlyingType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1888 | Decl->TypeForDecl = new(*this, TypeAlignment) |
| 1889 | TypedefType(Type::Typedef, Decl, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1890 | Types.push_back(Decl->TypeForDecl); |
| 1891 | return QualType(Decl->TypeForDecl, 0); |
| 1892 | } |
| 1893 | |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 1894 | QualType ASTContext::getRecordType(const RecordDecl *Decl) { |
| 1895 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 1896 | |
| 1897 | if (const RecordDecl *PrevDecl = Decl->getPreviousDeclaration()) |
| 1898 | if (PrevDecl->TypeForDecl) |
| 1899 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 1900 | |
| 1901 | Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Decl); |
| 1902 | Types.push_back(Decl->TypeForDecl); |
| 1903 | return QualType(Decl->TypeForDecl, 0); |
| 1904 | } |
| 1905 | |
| 1906 | QualType ASTContext::getEnumType(const EnumDecl *Decl) { |
| 1907 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 1908 | |
| 1909 | if (const EnumDecl *PrevDecl = Decl->getPreviousDeclaration()) |
| 1910 | if (PrevDecl->TypeForDecl) |
| 1911 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 1912 | |
| 1913 | Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Decl); |
| 1914 | Types.push_back(Decl->TypeForDecl); |
| 1915 | return QualType(Decl->TypeForDecl, 0); |
| 1916 | } |
| 1917 | |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1918 | /// \brief Retrieve a substitution-result type. |
| 1919 | QualType |
| 1920 | ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, |
| 1921 | QualType Replacement) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1922 | assert(Replacement.isCanonical() |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1923 | && "replacement types must always be canonical"); |
| 1924 | |
| 1925 | llvm::FoldingSetNodeID ID; |
| 1926 | SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); |
| 1927 | void *InsertPos = 0; |
| 1928 | SubstTemplateTypeParmType *SubstParm |
| 1929 | = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1930 | |
| 1931 | if (!SubstParm) { |
| 1932 | SubstParm = new (*this, TypeAlignment) |
| 1933 | SubstTemplateTypeParmType(Parm, Replacement); |
| 1934 | Types.push_back(SubstParm); |
| 1935 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 1936 | } |
| 1937 | |
| 1938 | return QualType(SubstParm, 0); |
| 1939 | } |
| 1940 | |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1941 | /// \brief Retrieve the template type parameter type for a template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1942 | /// parameter or parameter pack with the given depth, index, and (optionally) |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1943 | /// name. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1944 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1945 | bool ParameterPack, |
Douglas Gregor | efed5c8 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 1946 | IdentifierInfo *Name) { |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1947 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | efed5c8 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 1948 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1949 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1950 | TemplateTypeParmType *TypeParm |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1951 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1952 | |
| 1953 | if (TypeParm) |
| 1954 | return QualType(TypeParm, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1955 | |
Douglas Gregor | efed5c8 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 1956 | if (Name) { |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1957 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | efed5c8 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 1958 | TypeParm = new (*this, TypeAlignment) |
| 1959 | TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1960 | |
| 1961 | TemplateTypeParmType *TypeCheck |
| 1962 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1963 | assert(!TypeCheck && "Template type parameter canonical type broken"); |
| 1964 | (void)TypeCheck; |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1965 | } else |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1966 | TypeParm = new (*this, TypeAlignment) |
| 1967 | TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1968 | |
| 1969 | Types.push_back(TypeParm); |
| 1970 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 1971 | |
| 1972 | return QualType(TypeParm, 0); |
| 1973 | } |
| 1974 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1975 | TypeSourceInfo * |
| 1976 | ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name, |
| 1977 | SourceLocation NameLoc, |
| 1978 | const TemplateArgumentListInfo &Args, |
| 1979 | QualType CanonType) { |
| 1980 | QualType TST = getTemplateSpecializationType(Name, Args, CanonType); |
| 1981 | |
| 1982 | TypeSourceInfo *DI = CreateTypeSourceInfo(TST); |
| 1983 | TemplateSpecializationTypeLoc TL |
| 1984 | = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc()); |
| 1985 | TL.setTemplateNameLoc(NameLoc); |
| 1986 | TL.setLAngleLoc(Args.getLAngleLoc()); |
| 1987 | TL.setRAngleLoc(Args.getRAngleLoc()); |
| 1988 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 1989 | TL.setArgLocInfo(i, Args[i].getLocInfo()); |
| 1990 | return DI; |
| 1991 | } |
| 1992 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1993 | QualType |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1994 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1995 | const TemplateArgumentListInfo &Args, |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 1996 | QualType Canon) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1997 | unsigned NumArgs = Args.size(); |
| 1998 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1999 | llvm::SmallVector<TemplateArgument, 4> ArgVec; |
| 2000 | ArgVec.reserve(NumArgs); |
| 2001 | for (unsigned i = 0; i != NumArgs; ++i) |
| 2002 | ArgVec.push_back(Args[i].getArgument()); |
| 2003 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2004 | return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 2005 | Canon); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2006 | } |
| 2007 | |
| 2008 | QualType |
| 2009 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2010 | const TemplateArgument *Args, |
| 2011 | unsigned NumArgs, |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 2012 | QualType Canon) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 2013 | if (!Canon.isNull()) |
| 2014 | Canon = getCanonicalType(Canon); |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2015 | else |
| 2016 | Canon = getCanonicalTemplateSpecializationType(Template, Args, NumArgs); |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 2017 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2018 | // Allocate the (non-canonical) template specialization type, but don't |
| 2019 | // try to unique it: these types typically have location information that |
| 2020 | // we don't unique and don't want to lose. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2021 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2022 | sizeof(TemplateArgument) * NumArgs), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2023 | TypeAlignment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2024 | TemplateSpecializationType *Spec |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 2025 | = new (Mem) TemplateSpecializationType(Template, |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2026 | Args, NumArgs, |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 2027 | Canon); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2028 | |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2029 | Types.push_back(Spec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2030 | return QualType(Spec, 0); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2031 | } |
| 2032 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2033 | QualType |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2034 | ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template, |
| 2035 | const TemplateArgument *Args, |
| 2036 | unsigned NumArgs) { |
| 2037 | // Build the canonical template specialization type. |
| 2038 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); |
| 2039 | llvm::SmallVector<TemplateArgument, 4> CanonArgs; |
| 2040 | CanonArgs.reserve(NumArgs); |
| 2041 | for (unsigned I = 0; I != NumArgs; ++I) |
| 2042 | CanonArgs.push_back(getCanonicalTemplateArgument(Args[I])); |
| 2043 | |
| 2044 | // Determine whether this canonical template specialization type already |
| 2045 | // exists. |
| 2046 | llvm::FoldingSetNodeID ID; |
| 2047 | TemplateSpecializationType::Profile(ID, CanonTemplate, |
| 2048 | CanonArgs.data(), NumArgs, *this); |
| 2049 | |
| 2050 | void *InsertPos = 0; |
| 2051 | TemplateSpecializationType *Spec |
| 2052 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2053 | |
| 2054 | if (!Spec) { |
| 2055 | // Allocate a new canonical template specialization type. |
| 2056 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
| 2057 | sizeof(TemplateArgument) * NumArgs), |
| 2058 | TypeAlignment); |
| 2059 | Spec = new (Mem) TemplateSpecializationType(CanonTemplate, |
| 2060 | CanonArgs.data(), NumArgs, |
| 2061 | QualType()); |
| 2062 | Types.push_back(Spec); |
| 2063 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
| 2064 | } |
| 2065 | |
| 2066 | assert(Spec->isDependentType() && |
| 2067 | "Non-dependent template-id type must have a canonical type"); |
| 2068 | return QualType(Spec, 0); |
| 2069 | } |
| 2070 | |
| 2071 | QualType |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2072 | ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword, |
| 2073 | NestedNameSpecifier *NNS, |
| 2074 | QualType NamedType) { |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2075 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2076 | ElaboratedType::Profile(ID, Keyword, NNS, NamedType); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2077 | |
| 2078 | void *InsertPos = 0; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2079 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2080 | if (T) |
| 2081 | return QualType(T, 0); |
| 2082 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2083 | QualType Canon = NamedType; |
| 2084 | if (!Canon.isCanonical()) { |
| 2085 | Canon = getCanonicalType(NamedType); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2086 | ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2087 | assert(!CheckT && "Elaborated canonical type broken"); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2088 | (void)CheckT; |
| 2089 | } |
| 2090 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2091 | T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2092 | Types.push_back(T); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2093 | ElaboratedTypes.InsertNode(T, InsertPos); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2094 | return QualType(T, 0); |
| 2095 | } |
| 2096 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 2097 | QualType |
| 2098 | ASTContext::getParenType(QualType InnerType) { |
| 2099 | llvm::FoldingSetNodeID ID; |
| 2100 | ParenType::Profile(ID, InnerType); |
| 2101 | |
| 2102 | void *InsertPos = 0; |
| 2103 | ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2104 | if (T) |
| 2105 | return QualType(T, 0); |
| 2106 | |
| 2107 | QualType Canon = InnerType; |
| 2108 | if (!Canon.isCanonical()) { |
| 2109 | Canon = getCanonicalType(InnerType); |
| 2110 | ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2111 | assert(!CheckT && "Paren canonical type broken"); |
| 2112 | (void)CheckT; |
| 2113 | } |
| 2114 | |
| 2115 | T = new (*this) ParenType(InnerType, Canon); |
| 2116 | Types.push_back(T); |
| 2117 | ParenTypes.InsertNode(T, InsertPos); |
| 2118 | return QualType(T, 0); |
| 2119 | } |
| 2120 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2121 | QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, |
| 2122 | NestedNameSpecifier *NNS, |
| 2123 | const IdentifierInfo *Name, |
| 2124 | QualType Canon) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2125 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 2126 | |
| 2127 | if (Canon.isNull()) { |
| 2128 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2129 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 2130 | if (Keyword == ETK_None) |
| 2131 | CanonKeyword = ETK_Typename; |
| 2132 | |
| 2133 | if (CanonNNS != NNS || CanonKeyword != Keyword) |
| 2134 | Canon = getDependentNameType(CanonKeyword, CanonNNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2135 | } |
| 2136 | |
| 2137 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2138 | DependentNameType::Profile(ID, Keyword, NNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2139 | |
| 2140 | void *InsertPos = 0; |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2141 | DependentNameType *T |
| 2142 | = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2143 | if (T) |
| 2144 | return QualType(T, 0); |
| 2145 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2146 | T = new (*this) DependentNameType(Keyword, NNS, Name, Canon); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2147 | Types.push_back(T); |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2148 | DependentNameTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2149 | return QualType(T, 0); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2150 | } |
| 2151 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2152 | QualType |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2153 | ASTContext::getDependentTemplateSpecializationType( |
| 2154 | ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2155 | NestedNameSpecifier *NNS, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2156 | const IdentifierInfo *Name, |
| 2157 | const TemplateArgumentListInfo &Args) { |
| 2158 | // TODO: avoid this copy |
| 2159 | llvm::SmallVector<TemplateArgument, 16> ArgCopy; |
| 2160 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
| 2161 | ArgCopy.push_back(Args[I].getArgument()); |
| 2162 | return getDependentTemplateSpecializationType(Keyword, NNS, Name, |
| 2163 | ArgCopy.size(), |
| 2164 | ArgCopy.data()); |
| 2165 | } |
| 2166 | |
| 2167 | QualType |
| 2168 | ASTContext::getDependentTemplateSpecializationType( |
| 2169 | ElaboratedTypeKeyword Keyword, |
| 2170 | NestedNameSpecifier *NNS, |
| 2171 | const IdentifierInfo *Name, |
| 2172 | unsigned NumArgs, |
| 2173 | const TemplateArgument *Args) { |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2174 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 2175 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2176 | llvm::FoldingSetNodeID ID; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2177 | DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS, |
| 2178 | Name, NumArgs, Args); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2179 | |
| 2180 | void *InsertPos = 0; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2181 | DependentTemplateSpecializationType *T |
| 2182 | = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2183 | if (T) |
| 2184 | return QualType(T, 0); |
| 2185 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2186 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2187 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2188 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 2189 | if (Keyword == ETK_None) CanonKeyword = ETK_Typename; |
| 2190 | |
| 2191 | bool AnyNonCanonArgs = false; |
| 2192 | llvm::SmallVector<TemplateArgument, 16> CanonArgs(NumArgs); |
| 2193 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 2194 | CanonArgs[I] = getCanonicalTemplateArgument(Args[I]); |
| 2195 | if (!CanonArgs[I].structurallyEquals(Args[I])) |
| 2196 | AnyNonCanonArgs = true; |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2197 | } |
| 2198 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2199 | QualType Canon; |
| 2200 | if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) { |
| 2201 | Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS, |
| 2202 | Name, NumArgs, |
| 2203 | CanonArgs.data()); |
| 2204 | |
| 2205 | // Find the insert position again. |
| 2206 | DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2207 | } |
| 2208 | |
| 2209 | void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) + |
| 2210 | sizeof(TemplateArgument) * NumArgs), |
| 2211 | TypeAlignment); |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 2212 | T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2213 | Name, NumArgs, Args, Canon); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2214 | Types.push_back(T); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2215 | DependentTemplateSpecializationTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2216 | return QualType(T, 0); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2217 | } |
| 2218 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2219 | QualType ASTContext::getPackExpansionType(QualType Pattern) { |
| 2220 | llvm::FoldingSetNodeID ID; |
| 2221 | PackExpansionType::Profile(ID, Pattern); |
| 2222 | |
| 2223 | assert(Pattern->containsUnexpandedParameterPack() && |
| 2224 | "Pack expansions must expand one or more parameter packs"); |
| 2225 | void *InsertPos = 0; |
| 2226 | PackExpansionType *T |
| 2227 | = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2228 | if (T) |
| 2229 | return QualType(T, 0); |
| 2230 | |
| 2231 | QualType Canon; |
| 2232 | if (!Pattern.isCanonical()) { |
| 2233 | Canon = getPackExpansionType(getCanonicalType(Pattern)); |
| 2234 | |
| 2235 | // Find the insert position again. |
| 2236 | PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2237 | } |
| 2238 | |
| 2239 | T = new (*this) PackExpansionType(Pattern, Canon); |
| 2240 | Types.push_back(T); |
| 2241 | PackExpansionTypes.InsertNode(T, InsertPos); |
| 2242 | return QualType(T, 0); |
| 2243 | } |
| 2244 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2245 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 2246 | /// alphabetically. |
| 2247 | static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, |
| 2248 | const ObjCProtocolDecl *RHS) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2249 | return LHS->getDeclName() < RHS->getDeclName(); |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2250 | } |
| 2251 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2252 | static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols, |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2253 | unsigned NumProtocols) { |
| 2254 | if (NumProtocols == 0) return true; |
| 2255 | |
| 2256 | for (unsigned i = 1; i != NumProtocols; ++i) |
| 2257 | if (!CmpProtocolNames(Protocols[i-1], Protocols[i])) |
| 2258 | return false; |
| 2259 | return true; |
| 2260 | } |
| 2261 | |
| 2262 | static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols, |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2263 | unsigned &NumProtocols) { |
| 2264 | ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2265 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2266 | // Sort protocols, keyed by name. |
| 2267 | std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); |
| 2268 | |
| 2269 | // Remove duplicates. |
| 2270 | ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); |
| 2271 | NumProtocols = ProtocolsEnd-Protocols; |
| 2272 | } |
| 2273 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2274 | QualType ASTContext::getObjCObjectType(QualType BaseType, |
| 2275 | ObjCProtocolDecl * const *Protocols, |
| 2276 | unsigned NumProtocols) { |
| 2277 | // If the base type is an interface and there aren't any protocols |
| 2278 | // to add, then the interface type will do just fine. |
| 2279 | if (!NumProtocols && isa<ObjCInterfaceType>(BaseType)) |
| 2280 | return BaseType; |
| 2281 | |
| 2282 | // Look in the folding set for an existing type. |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2283 | llvm::FoldingSetNodeID ID; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2284 | ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2285 | void *InsertPos = 0; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2286 | if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2287 | return QualType(QT, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2288 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2289 | // Build the canonical type, which has the canonical base type and |
| 2290 | // a sorted-and-uniqued list of protocols. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2291 | QualType Canonical; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2292 | bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols); |
| 2293 | if (!ProtocolsSorted || !BaseType.isCanonical()) { |
| 2294 | if (!ProtocolsSorted) { |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 2295 | llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols, |
| 2296 | Protocols + NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2297 | unsigned UniqueCount = NumProtocols; |
| 2298 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2299 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2300 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 2301 | &Sorted[0], UniqueCount); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2302 | } else { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2303 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 2304 | Protocols, NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2305 | } |
| 2306 | |
| 2307 | // Regenerate InsertPos. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2308 | ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2309 | } |
| 2310 | |
| 2311 | unsigned Size = sizeof(ObjCObjectTypeImpl); |
| 2312 | Size += NumProtocols * sizeof(ObjCProtocolDecl *); |
| 2313 | void *Mem = Allocate(Size, TypeAlignment); |
| 2314 | ObjCObjectTypeImpl *T = |
| 2315 | new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols); |
| 2316 | |
| 2317 | Types.push_back(T); |
| 2318 | ObjCObjectTypes.InsertNode(T, InsertPos); |
| 2319 | return QualType(T, 0); |
| 2320 | } |
| 2321 | |
| 2322 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 2323 | /// the given object type. |
| 2324 | QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) { |
| 2325 | llvm::FoldingSetNodeID ID; |
| 2326 | ObjCObjectPointerType::Profile(ID, ObjectT); |
| 2327 | |
| 2328 | void *InsertPos = 0; |
| 2329 | if (ObjCObjectPointerType *QT = |
| 2330 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2331 | return QualType(QT, 0); |
| 2332 | |
| 2333 | // Find the canonical object type. |
| 2334 | QualType Canonical; |
| 2335 | if (!ObjectT.isCanonical()) { |
| 2336 | Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT)); |
| 2337 | |
| 2338 | // Regenerate InsertPos. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2339 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2340 | } |
| 2341 | |
Douglas Gregor | fd6a088 | 2010-02-08 22:59:26 +0000 | [diff] [blame] | 2342 | // No match. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2343 | void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment); |
| 2344 | ObjCObjectPointerType *QType = |
| 2345 | new (Mem) ObjCObjectPointerType(Canonical, ObjectT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2346 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2347 | Types.push_back(QType); |
| 2348 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2349 | return QualType(QType, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2350 | } |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2351 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2352 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 2353 | /// specified ObjC interface decl. The list of protocols is optional. |
| 2354 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) { |
| 2355 | if (Decl->TypeForDecl) |
| 2356 | return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2357 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2358 | // FIXME: redeclarations? |
| 2359 | void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment); |
| 2360 | ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl); |
| 2361 | Decl->TypeForDecl = T; |
| 2362 | Types.push_back(T); |
| 2363 | return QualType(T, 0); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2364 | } |
| 2365 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2366 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 2367 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2368 | /// multiple declarations that refer to "typeof(x)" all contain different |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2369 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2370 | /// on canonical type's (which are always unique). |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2371 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2372 | TypeOfExprType *toe; |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2373 | if (tofExpr->isTypeDependent()) { |
| 2374 | llvm::FoldingSetNodeID ID; |
| 2375 | DependentTypeOfExprType::Profile(ID, *this, tofExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2376 | |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2377 | void *InsertPos = 0; |
| 2378 | DependentTypeOfExprType *Canon |
| 2379 | = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2380 | if (Canon) { |
| 2381 | // We already have a "canonical" version of an identical, dependent |
| 2382 | // typeof(expr) type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2383 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2384 | QualType((TypeOfExprType*)Canon, 0)); |
| 2385 | } |
| 2386 | else { |
| 2387 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2388 | Canon |
| 2389 | = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2390 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); |
| 2391 | toe = Canon; |
| 2392 | } |
| 2393 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2394 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2395 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2396 | } |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2397 | Types.push_back(toe); |
| 2398 | return QualType(toe, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2399 | } |
| 2400 | |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2401 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 2402 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 2403 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2404 | /// an issue. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2405 | /// on canonical type's (which are always unique). |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2406 | QualType ASTContext::getTypeOfType(QualType tofType) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2407 | QualType Canonical = getCanonicalType(tofType); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2408 | TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2409 | Types.push_back(tot); |
| 2410 | return QualType(tot, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2411 | } |
| 2412 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2413 | /// getDecltypeForExpr - Given an expr, will return the decltype for that |
| 2414 | /// expression, according to the rules in C++0x [dcl.type.simple]p4 |
| 2415 | static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) { |
Anders Carlsson | a07c33e | 2009-06-25 15:00:34 +0000 | [diff] [blame] | 2416 | if (e->isTypeDependent()) |
| 2417 | return Context.DependentTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2418 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2419 | // If e is an id expression or a class member access, decltype(e) is defined |
| 2420 | // as the type of the entity named by e. |
| 2421 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) { |
| 2422 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) |
| 2423 | return VD->getType(); |
| 2424 | } |
| 2425 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) { |
| 2426 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) |
| 2427 | return FD->getType(); |
| 2428 | } |
| 2429 | // If e is a function call or an invocation of an overloaded operator, |
| 2430 | // (parentheses around e are ignored), decltype(e) is defined as the |
| 2431 | // return type of that function. |
| 2432 | if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens())) |
| 2433 | return CE->getCallReturnType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2434 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2435 | QualType T = e->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2436 | |
| 2437 | // 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] | 2438 | // defined as T&, otherwise decltype(e) is defined as T. |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 2439 | if (e->isLValue()) |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2440 | T = Context.getLValueReferenceType(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2441 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2442 | return T; |
| 2443 | } |
| 2444 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2445 | /// getDecltypeType - Unlike many "get<Type>" functions, we don't unique |
| 2446 | /// DecltypeType AST's. The only motivation to unique these nodes would be |
| 2447 | /// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2448 | /// an issue. This doesn't effect the type checker, since it operates |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2449 | /// on canonical type's (which are always unique). |
| 2450 | QualType ASTContext::getDecltypeType(Expr *e) { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2451 | DecltypeType *dt; |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2452 | if (e->isTypeDependent()) { |
| 2453 | llvm::FoldingSetNodeID ID; |
| 2454 | DependentDecltypeType::Profile(ID, *this, e); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2455 | |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2456 | void *InsertPos = 0; |
| 2457 | DependentDecltypeType *Canon |
| 2458 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2459 | if (Canon) { |
| 2460 | // We already have a "canonical" version of an equivalent, dependent |
| 2461 | // decltype type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2462 | dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy, |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2463 | QualType((DecltypeType*)Canon, 0)); |
| 2464 | } |
| 2465 | else { |
| 2466 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2467 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2468 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); |
| 2469 | dt = Canon; |
| 2470 | } |
| 2471 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2472 | QualType T = getDecltypeForExpr(e, *this); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2473 | dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T)); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2474 | } |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2475 | Types.push_back(dt); |
| 2476 | return QualType(dt, 0); |
| 2477 | } |
| 2478 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2479 | /// getTagDeclType - Return the unique reference to the type for the |
| 2480 | /// specified TagDecl (struct/union/class/enum) decl. |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 2481 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) { |
Ted Kremenek | d778f88 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 2482 | assert (Decl); |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 2483 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 2484 | // away const? mutable? |
| 2485 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2486 | } |
| 2487 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2488 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 2489 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 2490 | /// needs to agree with the definition in <stddef.h>. |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 2491 | CanQualType ASTContext::getSizeType() const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2492 | return getFromTargetType(Target.getSizeType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2493 | } |
| 2494 | |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 2495 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 2496 | /// Used when in C++, as a GCC extension. |
| 2497 | QualType ASTContext::getSignedWCharType() const { |
| 2498 | // FIXME: derive from "Target" ? |
| 2499 | return WCharTy; |
| 2500 | } |
| 2501 | |
| 2502 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 2503 | /// Used when in C++, as a GCC extension. |
| 2504 | QualType ASTContext::getUnsignedWCharType() const { |
| 2505 | // FIXME: derive from "Target" ? |
| 2506 | return UnsignedIntTy; |
| 2507 | } |
| 2508 | |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 2509 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) |
| 2510 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 2511 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2512 | return getFromTargetType(Target.getPtrDiffType(0)); |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 2513 | } |
| 2514 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2515 | //===----------------------------------------------------------------------===// |
| 2516 | // Type Operators |
| 2517 | //===----------------------------------------------------------------------===// |
| 2518 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2519 | CanQualType ASTContext::getCanonicalParamType(QualType T) { |
| 2520 | // Push qualifiers into arrays, and then discard any remaining |
| 2521 | // qualifiers. |
| 2522 | T = getCanonicalType(T); |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2523 | T = getVariableArrayDecayedType(T); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2524 | const Type *Ty = T.getTypePtr(); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2525 | QualType Result; |
| 2526 | if (isa<ArrayType>(Ty)) { |
| 2527 | Result = getArrayDecayedType(QualType(Ty,0)); |
| 2528 | } else if (isa<FunctionType>(Ty)) { |
| 2529 | Result = getPointerType(QualType(Ty, 0)); |
| 2530 | } else { |
| 2531 | Result = QualType(Ty, 0); |
| 2532 | } |
| 2533 | |
| 2534 | return CanQualType::CreateUnsafe(Result); |
| 2535 | } |
| 2536 | |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 2537 | /// getCanonicalType - Return the canonical (structural) type corresponding to |
| 2538 | /// the specified potentially non-canonical type. The non-canonical version |
| 2539 | /// of a type may have many "decorated" versions of types. Decorators can |
| 2540 | /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed |
| 2541 | /// to be free of any of these, allowing two canonical types to be compared |
| 2542 | /// for exact equality with a simple pointer comparison. |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2543 | CanQualType ASTContext::getCanonicalType(QualType T) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2544 | QualifierCollector Quals; |
| 2545 | const Type *Ptr = Quals.strip(T); |
| 2546 | QualType CanType = Ptr->getCanonicalTypeInternal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2547 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2548 | // The canonical internal type will be the canonical type *except* |
| 2549 | // that we push type qualifiers down through array types. |
| 2550 | |
| 2551 | // If there are no new qualifiers to push down, stop here. |
| 2552 | if (!Quals.hasQualifiers()) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2553 | return CanQualType::CreateUnsafe(CanType); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2554 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2555 | // If the type qualifiers are on an array type, get the canonical |
| 2556 | // type of the array with the qualifiers applied to the element |
| 2557 | // type. |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2558 | ArrayType *AT = dyn_cast<ArrayType>(CanType); |
| 2559 | if (!AT) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2560 | return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2561 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2562 | // Get the canonical version of the element with the extra qualifiers on it. |
| 2563 | // This can recursively sink qualifiers through multiple levels of arrays. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2564 | QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2565 | NewEltTy = getCanonicalType(NewEltTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2566 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2567 | if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2568 | return CanQualType::CreateUnsafe( |
| 2569 | getConstantArrayType(NewEltTy, CAT->getSize(), |
| 2570 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2571 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2572 | if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2573 | return CanQualType::CreateUnsafe( |
| 2574 | getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2575 | IAT->getIndexTypeCVRQualifiers())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2576 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2577 | if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2578 | return CanQualType::CreateUnsafe( |
| 2579 | getDependentSizedArrayType(NewEltTy, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 2580 | DSAT->getSizeExpr(), |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2581 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2582 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 87a924e | 2009-10-30 22:56:57 +0000 | [diff] [blame] | 2583 | DSAT->getBracketsRange())->getCanonicalTypeInternal()); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2584 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2585 | VariableArrayType *VAT = cast<VariableArrayType>(AT); |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2586 | return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 2587 | VAT->getSizeExpr(), |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2588 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2589 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2590 | VAT->getBracketsRange())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2591 | } |
| 2592 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2593 | QualType ASTContext::getUnqualifiedArrayType(QualType T, |
| 2594 | Qualifiers &Quals) { |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2595 | Quals = T.getQualifiers(); |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2596 | const ArrayType *AT = getAsArrayType(T); |
| 2597 | if (!AT) { |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2598 | return T.getUnqualifiedType(); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2599 | } |
| 2600 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2601 | QualType Elt = AT->getElementType(); |
Zhongxing Xu | c1ae0a8 | 2010-01-05 08:15:06 +0000 | [diff] [blame] | 2602 | QualType UnqualElt = getUnqualifiedArrayType(Elt, Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2603 | if (Elt == UnqualElt) |
| 2604 | return T; |
| 2605 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2606 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2607 | return getConstantArrayType(UnqualElt, CAT->getSize(), |
| 2608 | CAT->getSizeModifier(), 0); |
| 2609 | } |
| 2610 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2611 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2612 | return getIncompleteArrayType(UnqualElt, IAT->getSizeModifier(), 0); |
| 2613 | } |
| 2614 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2615 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) { |
| 2616 | return getVariableArrayType(UnqualElt, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 2617 | VAT->getSizeExpr(), |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2618 | VAT->getSizeModifier(), |
| 2619 | VAT->getIndexTypeCVRQualifiers(), |
| 2620 | VAT->getBracketsRange()); |
| 2621 | } |
| 2622 | |
| 2623 | const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT); |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 2624 | return getDependentSizedArrayType(UnqualElt, DSAT->getSizeExpr(), |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2625 | DSAT->getSizeModifier(), 0, |
| 2626 | SourceRange()); |
| 2627 | } |
| 2628 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2629 | /// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that |
| 2630 | /// may be similar (C++ 4.4), replaces T1 and T2 with the type that |
| 2631 | /// they point to and return true. If T1 and T2 aren't pointer types |
| 2632 | /// or pointer-to-member types, or if they are not similar at this |
| 2633 | /// level, returns false and leaves T1 and T2 unchanged. Top-level |
| 2634 | /// qualifiers on T1 and T2 are ignored. This function will typically |
| 2635 | /// be called in a loop that successively "unwraps" pointer and |
| 2636 | /// pointer-to-member types to compare them at each level. |
| 2637 | bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) { |
| 2638 | const PointerType *T1PtrType = T1->getAs<PointerType>(), |
| 2639 | *T2PtrType = T2->getAs<PointerType>(); |
| 2640 | if (T1PtrType && T2PtrType) { |
| 2641 | T1 = T1PtrType->getPointeeType(); |
| 2642 | T2 = T2PtrType->getPointeeType(); |
| 2643 | return true; |
| 2644 | } |
| 2645 | |
| 2646 | const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(), |
| 2647 | *T2MPType = T2->getAs<MemberPointerType>(); |
| 2648 | if (T1MPType && T2MPType && |
| 2649 | hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0), |
| 2650 | QualType(T2MPType->getClass(), 0))) { |
| 2651 | T1 = T1MPType->getPointeeType(); |
| 2652 | T2 = T2MPType->getPointeeType(); |
| 2653 | return true; |
| 2654 | } |
| 2655 | |
| 2656 | if (getLangOptions().ObjC1) { |
| 2657 | const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(), |
| 2658 | *T2OPType = T2->getAs<ObjCObjectPointerType>(); |
| 2659 | if (T1OPType && T2OPType) { |
| 2660 | T1 = T1OPType->getPointeeType(); |
| 2661 | T2 = T2OPType->getPointeeType(); |
| 2662 | return true; |
| 2663 | } |
| 2664 | } |
| 2665 | |
| 2666 | // FIXME: Block pointers, too? |
| 2667 | |
| 2668 | return false; |
| 2669 | } |
| 2670 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2671 | DeclarationNameInfo ASTContext::getNameForTemplate(TemplateName Name, |
| 2672 | SourceLocation NameLoc) { |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2673 | if (TemplateDecl *TD = Name.getAsTemplateDecl()) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2674 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 2675 | return DeclarationNameInfo(TD->getDeclName(), NameLoc); |
| 2676 | |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2677 | if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2678 | DeclarationName DName; |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2679 | if (DTN->isIdentifier()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2680 | DName = DeclarationNames.getIdentifier(DTN->getIdentifier()); |
| 2681 | return DeclarationNameInfo(DName, NameLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2682 | } else { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2683 | DName = DeclarationNames.getCXXOperatorName(DTN->getOperator()); |
| 2684 | // DNInfo work in progress: FIXME: source locations? |
| 2685 | DeclarationNameLoc DNLoc; |
| 2686 | DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding(); |
| 2687 | DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding(); |
| 2688 | return DeclarationNameInfo(DName, NameLoc, DNLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2689 | } |
| 2690 | } |
| 2691 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 2692 | OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate(); |
| 2693 | assert(Storage); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2694 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 2695 | return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2696 | } |
| 2697 | |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2698 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) { |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 2699 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 2700 | if (TemplateTemplateParmDecl *TTP |
| 2701 | = dyn_cast<TemplateTemplateParmDecl>(Template)) |
| 2702 | Template = getCanonicalTemplateTemplateParmDecl(TTP); |
| 2703 | |
| 2704 | // The canonical template name is the canonical template declaration. |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 2705 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 2706 | } |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2707 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 2708 | assert(!Name.getAsOverloadedTemplate()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2709 | |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2710 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 2711 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 2712 | return DTN->CanonicalTemplateName; |
| 2713 | } |
| 2714 | |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 2715 | bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) { |
| 2716 | X = getCanonicalTemplateName(X); |
| 2717 | Y = getCanonicalTemplateName(Y); |
| 2718 | return X.getAsVoidPointer() == Y.getAsVoidPointer(); |
| 2719 | } |
| 2720 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2721 | TemplateArgument |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2722 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) { |
| 2723 | switch (Arg.getKind()) { |
| 2724 | case TemplateArgument::Null: |
| 2725 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2726 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2727 | case TemplateArgument::Expression: |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2728 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2729 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2730 | case TemplateArgument::Declaration: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2731 | return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2732 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2733 | case TemplateArgument::Template: |
| 2734 | return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate())); |
| 2735 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2736 | case TemplateArgument::Integral: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2737 | return TemplateArgument(*Arg.getAsIntegral(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2738 | getCanonicalType(Arg.getIntegralType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2739 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2740 | case TemplateArgument::Type: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2741 | return TemplateArgument(getCanonicalType(Arg.getAsType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2742 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2743 | case TemplateArgument::Pack: { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 2744 | if (Arg.pack_size() == 0) |
| 2745 | return Arg; |
| 2746 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2747 | TemplateArgument *CanonArgs |
| 2748 | = new (*this) TemplateArgument[Arg.pack_size()]; |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2749 | unsigned Idx = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2750 | for (TemplateArgument::pack_iterator A = Arg.pack_begin(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2751 | AEnd = Arg.pack_end(); |
| 2752 | A != AEnd; (void)++A, ++Idx) |
| 2753 | CanonArgs[Idx] = getCanonicalTemplateArgument(*A); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2754 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2755 | return TemplateArgument(CanonArgs, Arg.pack_size()); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2756 | } |
| 2757 | } |
| 2758 | |
| 2759 | // Silence GCC warning |
| 2760 | assert(false && "Unhandled template argument kind"); |
| 2761 | return TemplateArgument(); |
| 2762 | } |
| 2763 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2764 | NestedNameSpecifier * |
| 2765 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2766 | if (!NNS) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2767 | return 0; |
| 2768 | |
| 2769 | switch (NNS->getKind()) { |
| 2770 | case NestedNameSpecifier::Identifier: |
| 2771 | // Canonicalize the prefix but keep the identifier the same. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2772 | return NestedNameSpecifier::Create(*this, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2773 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 2774 | NNS->getAsIdentifier()); |
| 2775 | |
| 2776 | case NestedNameSpecifier::Namespace: |
| 2777 | // A namespace is canonical; build a nested-name-specifier with |
| 2778 | // this namespace and no prefix. |
| 2779 | return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace()); |
| 2780 | |
| 2781 | case NestedNameSpecifier::TypeSpec: |
| 2782 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 2783 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 2784 | |
| 2785 | // If we have some kind of dependent-named type (e.g., "typename T::type"), |
| 2786 | // break it apart into its prefix and identifier, then reconsititute those |
| 2787 | // as the canonical nested-name-specifier. This is required to canonicalize |
| 2788 | // a dependent nested-name-specifier involving typedefs of dependent-name |
| 2789 | // types, e.g., |
| 2790 | // typedef typename T::type T1; |
| 2791 | // typedef typename T1::type T2; |
| 2792 | if (const DependentNameType *DNT = T->getAs<DependentNameType>()) { |
| 2793 | NestedNameSpecifier *Prefix |
| 2794 | = getCanonicalNestedNameSpecifier(DNT->getQualifier()); |
| 2795 | return NestedNameSpecifier::Create(*this, Prefix, |
| 2796 | const_cast<IdentifierInfo *>(DNT->getIdentifier())); |
| 2797 | } |
| 2798 | |
Douglas Gregor | 643f843 | 2010-11-04 00:14:23 +0000 | [diff] [blame] | 2799 | // Do the same thing as above, but with dependent-named specializations. |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 2800 | if (const DependentTemplateSpecializationType *DTST |
| 2801 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 2802 | NestedNameSpecifier *Prefix |
| 2803 | = getCanonicalNestedNameSpecifier(DTST->getQualifier()); |
| 2804 | TemplateName Name |
| 2805 | = getDependentTemplateName(Prefix, DTST->getIdentifier()); |
| 2806 | T = getTemplateSpecializationType(Name, |
| 2807 | DTST->getArgs(), DTST->getNumArgs()); |
| 2808 | T = getCanonicalType(T); |
| 2809 | } |
| 2810 | |
| 2811 | return NestedNameSpecifier::Create(*this, 0, false, T.getTypePtr()); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2812 | } |
| 2813 | |
| 2814 | case NestedNameSpecifier::Global: |
| 2815 | // The global specifier is canonical and unique. |
| 2816 | return NNS; |
| 2817 | } |
| 2818 | |
| 2819 | // Required to silence a GCC warning |
| 2820 | return 0; |
| 2821 | } |
| 2822 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2823 | |
| 2824 | const ArrayType *ASTContext::getAsArrayType(QualType T) { |
| 2825 | // Handle the non-qualified case efficiently. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2826 | if (!T.hasLocalQualifiers()) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2827 | // Handle the common positive case fast. |
| 2828 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 2829 | return AT; |
| 2830 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2831 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2832 | // Handle the common negative case fast. |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2833 | QualType CType = T->getCanonicalTypeInternal(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2834 | if (!isa<ArrayType>(CType)) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2835 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2836 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2837 | // Apply any qualifiers from the array type to the element type. This |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2838 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 2839 | // 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] | 2840 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2841 | // If we get here, we either have type qualifiers on the type, or we have |
| 2842 | // 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] | 2843 | // we must propagate them down into the element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2844 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2845 | QualifierCollector Qs; |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 2846 | const Type *Ty = Qs.strip(T.getDesugaredType(*this)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2847 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2848 | // If we have a simple case, just return now. |
| 2849 | const ArrayType *ATy = dyn_cast<ArrayType>(Ty); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2850 | if (ATy == 0 || Qs.empty()) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2851 | return ATy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2852 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2853 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 2854 | // qualifiers into the array element type and return a new array type. |
| 2855 | // Get the canonical version of the element with the extra qualifiers on it. |
| 2856 | // This can recursively sink qualifiers through multiple levels of arrays. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2857 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2858 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2859 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 2860 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 2861 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2862 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2863 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 2864 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 2865 | IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2866 | IAT->getIndexTypeCVRQualifiers())); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2867 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2868 | if (const DependentSizedArrayType *DSAT |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2869 | = dyn_cast<DependentSizedArrayType>(ATy)) |
| 2870 | return cast<ArrayType>( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2871 | getDependentSizedArrayType(NewEltTy, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 2872 | DSAT->getSizeExpr(), |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2873 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2874 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2875 | DSAT->getBracketsRange())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2876 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2877 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2878 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 2879 | VAT->getSizeExpr(), |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2880 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2881 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2882 | VAT->getBracketsRange())); |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 2883 | } |
| 2884 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2885 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 2886 | /// specified array type to a pointer. This operation is non-trivial when |
| 2887 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 2888 | /// this returns a pointer to a properly qualified element of the array. |
| 2889 | /// |
| 2890 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
| 2891 | QualType ASTContext::getArrayDecayedType(QualType Ty) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2892 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 2893 | // typedefs in the element type of the array. This also handles propagation |
| 2894 | // of type qualifiers from the array type into the element type if present |
| 2895 | // (C99 6.7.3p8). |
| 2896 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 2897 | assert(PrettyArrayType && "Not an array type!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2898 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2899 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2900 | |
| 2901 | // int x[restrict 4] -> int *restrict |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2902 | return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2903 | } |
| 2904 | |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2905 | QualType ASTContext::getBaseElementType(QualType QT) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2906 | QualifierCollector Qs; |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 2907 | while (const ArrayType *AT = getAsArrayType(QualType(Qs.strip(QT), 0))) |
| 2908 | QT = AT->getElementType(); |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 2909 | return Qs.apply(*this, QT); |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2910 | } |
| 2911 | |
Anders Carlsson | fbbce49 | 2009-09-25 01:23:32 +0000 | [diff] [blame] | 2912 | QualType ASTContext::getBaseElementType(const ArrayType *AT) { |
| 2913 | QualType ElemTy = AT->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2914 | |
Anders Carlsson | fbbce49 | 2009-09-25 01:23:32 +0000 | [diff] [blame] | 2915 | if (const ArrayType *AT = getAsArrayType(ElemTy)) |
| 2916 | return getBaseElementType(AT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2917 | |
Anders Carlsson | 6183a99 | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 2918 | return ElemTy; |
| 2919 | } |
| 2920 | |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 2921 | /// getConstantArrayElementCount - Returns number of constant array elements. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2922 | uint64_t |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 2923 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
| 2924 | uint64_t ElementCount = 1; |
| 2925 | do { |
| 2926 | ElementCount *= CA->getSize().getZExtValue(); |
| 2927 | CA = dyn_cast<ConstantArrayType>(CA->getElementType()); |
| 2928 | } while (CA); |
| 2929 | return ElementCount; |
| 2930 | } |
| 2931 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2932 | /// getFloatingRank - Return a relative rank for floating point types. |
| 2933 | /// 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] | 2934 | static FloatingRank getFloatingRank(QualType T) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2935 | if (const ComplexType *CT = T->getAs<ComplexType>()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2936 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2937 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2938 | assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"); |
| 2939 | switch (T->getAs<BuiltinType>()->getKind()) { |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2940 | default: assert(0 && "getFloatingRank(): not a floating type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2941 | case BuiltinType::Float: return FloatRank; |
| 2942 | case BuiltinType::Double: return DoubleRank; |
| 2943 | case BuiltinType::LongDouble: return LongDoubleRank; |
| 2944 | } |
| 2945 | } |
| 2946 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2947 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 2948 | /// point or a complex type (based on typeDomain/typeSize). |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 2949 | /// 'typeDomain' is a real floating point or complex type. |
| 2950 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 2951 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 2952 | QualType Domain) const { |
| 2953 | FloatingRank EltRank = getFloatingRank(Size); |
| 2954 | if (Domain->isComplexType()) { |
| 2955 | switch (EltRank) { |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 2956 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 2957 | case FloatRank: return FloatComplexTy; |
| 2958 | case DoubleRank: return DoubleComplexTy; |
| 2959 | case LongDoubleRank: return LongDoubleComplexTy; |
| 2960 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2961 | } |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 2962 | |
| 2963 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 2964 | switch (EltRank) { |
| 2965 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
| 2966 | case FloatRank: return FloatTy; |
| 2967 | case DoubleRank: return DoubleTy; |
| 2968 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 2969 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2970 | } |
| 2971 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2972 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 2973 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 2974 | /// '_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] | 2975 | /// LHS < RHS, return -1. |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2976 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) { |
| 2977 | FloatingRank LHSR = getFloatingRank(LHS); |
| 2978 | FloatingRank RHSR = getFloatingRank(RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2979 | |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2980 | if (LHSR == RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 2981 | return 0; |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2982 | if (LHSR > RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 2983 | return 1; |
| 2984 | return -1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2985 | } |
| 2986 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2987 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 2988 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 2989 | /// or if it is not canonicalized. |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2990 | unsigned ASTContext::getIntegerRank(Type *T) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2991 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2992 | if (EnumType* ET = dyn_cast<EnumType>(T)) |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 2993 | T = ET->getDecl()->getPromotionType().getTypePtr(); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2994 | |
Eli Friedman | a342675 | 2009-07-05 23:44:27 +0000 | [diff] [blame] | 2995 | if (T->isSpecificBuiltinType(BuiltinType::WChar)) |
| 2996 | T = getFromTargetType(Target.getWCharType()).getTypePtr(); |
| 2997 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2998 | if (T->isSpecificBuiltinType(BuiltinType::Char16)) |
| 2999 | T = getFromTargetType(Target.getChar16Type()).getTypePtr(); |
| 3000 | |
| 3001 | if (T->isSpecificBuiltinType(BuiltinType::Char32)) |
| 3002 | T = getFromTargetType(Target.getChar32Type()).getTypePtr(); |
| 3003 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3004 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3005 | default: assert(0 && "getIntegerRank(): not a built-in integer"); |
| 3006 | case BuiltinType::Bool: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3007 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3008 | case BuiltinType::Char_S: |
| 3009 | case BuiltinType::Char_U: |
| 3010 | case BuiltinType::SChar: |
| 3011 | case BuiltinType::UChar: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3012 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3013 | case BuiltinType::Short: |
| 3014 | case BuiltinType::UShort: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3015 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3016 | case BuiltinType::Int: |
| 3017 | case BuiltinType::UInt: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3018 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3019 | case BuiltinType::Long: |
| 3020 | case BuiltinType::ULong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3021 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3022 | case BuiltinType::LongLong: |
| 3023 | case BuiltinType::ULongLong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3024 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 3025 | case BuiltinType::Int128: |
| 3026 | case BuiltinType::UInt128: |
| 3027 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3028 | } |
| 3029 | } |
| 3030 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3031 | /// \brief Whether this is a promotable bitfield reference according |
| 3032 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
| 3033 | /// |
| 3034 | /// \returns the type this bit-field will promote to, or NULL if no |
| 3035 | /// promotion occurs. |
| 3036 | QualType ASTContext::isPromotableBitField(Expr *E) { |
Douglas Gregor | ceafbde | 2010-05-24 20:13:53 +0000 | [diff] [blame] | 3037 | if (E->isTypeDependent() || E->isValueDependent()) |
| 3038 | return QualType(); |
| 3039 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3040 | FieldDecl *Field = E->getBitField(); |
| 3041 | if (!Field) |
| 3042 | return QualType(); |
| 3043 | |
| 3044 | QualType FT = Field->getType(); |
| 3045 | |
| 3046 | llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this); |
| 3047 | uint64_t BitWidth = BitWidthAP.getZExtValue(); |
| 3048 | uint64_t IntSize = getTypeSize(IntTy); |
| 3049 | // GCC extension compatibility: if the bit-field size is less than or equal |
| 3050 | // to the size of int, it gets promoted no matter what its type is. |
| 3051 | // For instance, unsigned long bf : 4 gets promoted to signed int. |
| 3052 | if (BitWidth < IntSize) |
| 3053 | return IntTy; |
| 3054 | |
| 3055 | if (BitWidth == IntSize) |
| 3056 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
| 3057 | |
| 3058 | // Types bigger than int are not subject to promotions, and therefore act |
| 3059 | // like the base type. |
| 3060 | // FIXME: This doesn't quite match what gcc does, but what gcc does here |
| 3061 | // is ridiculous. |
| 3062 | return QualType(); |
| 3063 | } |
| 3064 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3065 | /// getPromotedIntegerType - Returns the type that Promotable will |
| 3066 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
| 3067 | /// integer type. |
| 3068 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) { |
| 3069 | assert(!Promotable.isNull()); |
| 3070 | assert(Promotable->isPromotableIntegerType()); |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 3071 | if (const EnumType *ET = Promotable->getAs<EnumType>()) |
| 3072 | return ET->getDecl()->getPromotionType(); |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3073 | if (Promotable->isSignedIntegerType()) |
| 3074 | return IntTy; |
| 3075 | uint64_t PromotableSize = getTypeSize(Promotable); |
| 3076 | uint64_t IntSize = getTypeSize(IntTy); |
| 3077 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
| 3078 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
| 3079 | } |
| 3080 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3081 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3082 | /// 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] | 3083 | /// LHS < RHS, return -1. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3084 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3085 | Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 3086 | Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3087 | if (LHSC == RHSC) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3088 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3089 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 3090 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3091 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3092 | unsigned LHSRank = getIntegerRank(LHSC); |
| 3093 | unsigned RHSRank = getIntegerRank(RHSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3094 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3095 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 3096 | if (LHSRank == RHSRank) return 0; |
| 3097 | return LHSRank > RHSRank ? 1 : -1; |
| 3098 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3099 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3100 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 3101 | if (LHSUnsigned) { |
| 3102 | // If the unsigned [LHS] type is larger, return it. |
| 3103 | if (LHSRank >= RHSRank) |
| 3104 | return 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3105 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3106 | // If the signed type can represent all values of the unsigned type, it |
| 3107 | // 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] | 3108 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3109 | return -1; |
| 3110 | } |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3111 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3112 | // If the unsigned [RHS] type is larger, return it. |
| 3113 | if (RHSRank >= LHSRank) |
| 3114 | return -1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3115 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3116 | // If the signed type can represent all values of the unsigned type, it |
| 3117 | // 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] | 3118 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3119 | return 1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3120 | } |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3121 | |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3122 | static RecordDecl * |
| 3123 | CreateRecordDecl(ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC, |
| 3124 | SourceLocation L, IdentifierInfo *Id) { |
| 3125 | if (Ctx.getLangOptions().CPlusPlus) |
| 3126 | return CXXRecordDecl::Create(Ctx, TK, DC, L, Id); |
| 3127 | else |
| 3128 | return RecordDecl::Create(Ctx, TK, DC, L, Id); |
| 3129 | } |
| 3130 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3131 | // getCFConstantStringType - Return the type used for constant CFStrings. |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3132 | QualType ASTContext::getCFConstantStringType() { |
| 3133 | if (!CFConstantStringTypeDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3134 | CFConstantStringTypeDecl = |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3135 | CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3136 | &Idents.get("NSConstantString")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3137 | CFConstantStringTypeDecl->startDefinition(); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3138 | |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 3139 | QualType FieldTypes[4]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3140 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3141 | // const int *isa; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3142 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 3143 | // int flags; |
| 3144 | FieldTypes[1] = IntTy; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3145 | // const char *str; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3146 | FieldTypes[2] = getPointerType(CharTy.withConst()); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3147 | // long length; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3148 | FieldTypes[3] = LongTy; |
| 3149 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3150 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3151 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3152 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3153 | SourceLocation(), 0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3154 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3155 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 3156 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3157 | Field->setAccess(AS_public); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3158 | CFConstantStringTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3159 | } |
| 3160 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3161 | CFConstantStringTypeDecl->completeDefinition(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3162 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3163 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3164 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 8467583 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 3165 | } |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 3166 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3167 | void ASTContext::setCFConstantStringType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3168 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3169 | assert(Rec && "Invalid CFConstantStringType"); |
| 3170 | CFConstantStringTypeDecl = Rec->getDecl(); |
| 3171 | } |
| 3172 | |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 3173 | // getNSConstantStringType - Return the type used for constant NSStrings. |
| 3174 | QualType ASTContext::getNSConstantStringType() { |
| 3175 | if (!NSConstantStringTypeDecl) { |
| 3176 | NSConstantStringTypeDecl = |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3177 | CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 3178 | &Idents.get("__builtin_NSString")); |
| 3179 | NSConstantStringTypeDecl->startDefinition(); |
| 3180 | |
| 3181 | QualType FieldTypes[3]; |
| 3182 | |
| 3183 | // const int *isa; |
| 3184 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
| 3185 | // const char *str; |
| 3186 | FieldTypes[1] = getPointerType(CharTy.withConst()); |
| 3187 | // unsigned int length; |
| 3188 | FieldTypes[2] = UnsignedIntTy; |
| 3189 | |
| 3190 | // Create fields |
| 3191 | for (unsigned i = 0; i < 3; ++i) { |
| 3192 | FieldDecl *Field = FieldDecl::Create(*this, NSConstantStringTypeDecl, |
| 3193 | SourceLocation(), 0, |
| 3194 | FieldTypes[i], /*TInfo=*/0, |
| 3195 | /*BitWidth=*/0, |
| 3196 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3197 | Field->setAccess(AS_public); |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 3198 | NSConstantStringTypeDecl->addDecl(Field); |
| 3199 | } |
| 3200 | |
| 3201 | NSConstantStringTypeDecl->completeDefinition(); |
| 3202 | } |
| 3203 | |
| 3204 | return getTagDeclType(NSConstantStringTypeDecl); |
| 3205 | } |
| 3206 | |
| 3207 | void ASTContext::setNSConstantStringType(QualType T) { |
| 3208 | const RecordType *Rec = T->getAs<RecordType>(); |
| 3209 | assert(Rec && "Invalid NSConstantStringType"); |
| 3210 | NSConstantStringTypeDecl = Rec->getDecl(); |
| 3211 | } |
| 3212 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3213 | QualType ASTContext::getObjCFastEnumerationStateType() { |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3214 | if (!ObjCFastEnumerationStateTypeDecl) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3215 | ObjCFastEnumerationStateTypeDecl = |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3216 | CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3217 | &Idents.get("__objcFastEnumerationState")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3218 | ObjCFastEnumerationStateTypeDecl->startDefinition(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3219 | |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3220 | QualType FieldTypes[] = { |
| 3221 | UnsignedLongTy, |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3222 | getPointerType(ObjCIdTypedefType), |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3223 | getPointerType(UnsignedLongTy), |
| 3224 | getConstantArrayType(UnsignedLongTy, |
| 3225 | llvm::APInt(32, 5), ArrayType::Normal, 0) |
| 3226 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3227 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3228 | for (size_t i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3229 | FieldDecl *Field = FieldDecl::Create(*this, |
| 3230 | ObjCFastEnumerationStateTypeDecl, |
| 3231 | SourceLocation(), 0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3232 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3233 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 3234 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3235 | Field->setAccess(AS_public); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3236 | ObjCFastEnumerationStateTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3237 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3238 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3239 | ObjCFastEnumerationStateTypeDecl->completeDefinition(); |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3240 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3241 | |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3242 | return getTagDeclType(ObjCFastEnumerationStateTypeDecl); |
| 3243 | } |
| 3244 | |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3245 | QualType ASTContext::getBlockDescriptorType() { |
| 3246 | if (BlockDescriptorType) |
| 3247 | return getTagDeclType(BlockDescriptorType); |
| 3248 | |
| 3249 | RecordDecl *T; |
| 3250 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3251 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3252 | &Idents.get("__block_descriptor")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3253 | T->startDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3254 | |
| 3255 | QualType FieldTypes[] = { |
| 3256 | UnsignedLongTy, |
| 3257 | UnsignedLongTy, |
| 3258 | }; |
| 3259 | |
| 3260 | const char *FieldNames[] = { |
| 3261 | "reserved", |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3262 | "Size" |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3263 | }; |
| 3264 | |
| 3265 | for (size_t i = 0; i < 2; ++i) { |
| 3266 | FieldDecl *Field = FieldDecl::Create(*this, |
| 3267 | T, |
| 3268 | SourceLocation(), |
| 3269 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3270 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3271 | /*BitWidth=*/0, |
| 3272 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3273 | Field->setAccess(AS_public); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3274 | T->addDecl(Field); |
| 3275 | } |
| 3276 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3277 | T->completeDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3278 | |
| 3279 | BlockDescriptorType = T; |
| 3280 | |
| 3281 | return getTagDeclType(BlockDescriptorType); |
| 3282 | } |
| 3283 | |
| 3284 | void ASTContext::setBlockDescriptorType(QualType T) { |
| 3285 | const RecordType *Rec = T->getAs<RecordType>(); |
| 3286 | assert(Rec && "Invalid BlockDescriptorType"); |
| 3287 | BlockDescriptorType = Rec->getDecl(); |
| 3288 | } |
| 3289 | |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3290 | QualType ASTContext::getBlockDescriptorExtendedType() { |
| 3291 | if (BlockDescriptorExtendedType) |
| 3292 | return getTagDeclType(BlockDescriptorExtendedType); |
| 3293 | |
| 3294 | RecordDecl *T; |
| 3295 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3296 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3297 | &Idents.get("__block_descriptor_withcopydispose")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3298 | T->startDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3299 | |
| 3300 | QualType FieldTypes[] = { |
| 3301 | UnsignedLongTy, |
| 3302 | UnsignedLongTy, |
| 3303 | getPointerType(VoidPtrTy), |
| 3304 | getPointerType(VoidPtrTy) |
| 3305 | }; |
| 3306 | |
| 3307 | const char *FieldNames[] = { |
| 3308 | "reserved", |
| 3309 | "Size", |
| 3310 | "CopyFuncPtr", |
| 3311 | "DestroyFuncPtr" |
| 3312 | }; |
| 3313 | |
| 3314 | for (size_t i = 0; i < 4; ++i) { |
| 3315 | FieldDecl *Field = FieldDecl::Create(*this, |
| 3316 | T, |
| 3317 | SourceLocation(), |
| 3318 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3319 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3320 | /*BitWidth=*/0, |
| 3321 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3322 | Field->setAccess(AS_public); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3323 | T->addDecl(Field); |
| 3324 | } |
| 3325 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3326 | T->completeDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3327 | |
| 3328 | BlockDescriptorExtendedType = T; |
| 3329 | |
| 3330 | return getTagDeclType(BlockDescriptorExtendedType); |
| 3331 | } |
| 3332 | |
| 3333 | void ASTContext::setBlockDescriptorExtendedType(QualType T) { |
| 3334 | const RecordType *Rec = T->getAs<RecordType>(); |
| 3335 | assert(Rec && "Invalid BlockDescriptorType"); |
| 3336 | BlockDescriptorExtendedType = Rec->getDecl(); |
| 3337 | } |
| 3338 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3339 | bool ASTContext::BlockRequiresCopying(QualType Ty) { |
| 3340 | if (Ty->isBlockPointerType()) |
| 3341 | return true; |
| 3342 | if (isObjCNSObjectType(Ty)) |
| 3343 | return true; |
| 3344 | if (Ty->isObjCObjectPointerType()) |
| 3345 | return true; |
Fariborz Jahanian | e38be61 | 2010-11-17 00:21:28 +0000 | [diff] [blame] | 3346 | if (getLangOptions().CPlusPlus) { |
| 3347 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3348 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 3349 | return RD->hasConstCopyConstructor(*this); |
| 3350 | |
| 3351 | } |
| 3352 | } |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3353 | return false; |
| 3354 | } |
| 3355 | |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3356 | QualType ASTContext::BuildByRefType(llvm::StringRef DeclName, QualType Ty) { |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3357 | // type = struct __Block_byref_1_X { |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3358 | // void *__isa; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3359 | // struct __Block_byref_1_X *__forwarding; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3360 | // unsigned int __flags; |
| 3361 | // unsigned int __size; |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 3362 | // void *__copy_helper; // as needed |
| 3363 | // void *__destroy_help // as needed |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3364 | // int X; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3365 | // } * |
| 3366 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3367 | bool HasCopyAndDispose = BlockRequiresCopying(Ty); |
| 3368 | |
| 3369 | // FIXME: Move up |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 3370 | llvm::SmallString<36> Name; |
| 3371 | llvm::raw_svector_ostream(Name) << "__Block_byref_" << |
| 3372 | ++UniqueBlockByRefTypeID << '_' << DeclName; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3373 | RecordDecl *T; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3374 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3375 | &Idents.get(Name.str())); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3376 | T->startDefinition(); |
| 3377 | QualType Int32Ty = IntTy; |
| 3378 | assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported"); |
| 3379 | QualType FieldTypes[] = { |
| 3380 | getPointerType(VoidPtrTy), |
| 3381 | getPointerType(getTagDeclType(T)), |
| 3382 | Int32Ty, |
| 3383 | Int32Ty, |
| 3384 | getPointerType(VoidPtrTy), |
| 3385 | getPointerType(VoidPtrTy), |
| 3386 | Ty |
| 3387 | }; |
| 3388 | |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3389 | llvm::StringRef FieldNames[] = { |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3390 | "__isa", |
| 3391 | "__forwarding", |
| 3392 | "__flags", |
| 3393 | "__size", |
| 3394 | "__copy_helper", |
| 3395 | "__destroy_helper", |
| 3396 | DeclName, |
| 3397 | }; |
| 3398 | |
| 3399 | for (size_t i = 0; i < 7; ++i) { |
| 3400 | if (!HasCopyAndDispose && i >=4 && i <= 5) |
| 3401 | continue; |
| 3402 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
| 3403 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3404 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3405 | /*BitWidth=*/0, /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3406 | Field->setAccess(AS_public); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3407 | T->addDecl(Field); |
| 3408 | } |
| 3409 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3410 | T->completeDefinition(); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3411 | |
| 3412 | return getPointerType(getTagDeclType(T)); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3413 | } |
| 3414 | |
| 3415 | |
| 3416 | QualType ASTContext::getBlockParmType( |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3417 | bool BlockHasCopyDispose, |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 3418 | llvm::SmallVectorImpl<const Expr *> &Layout) { |
| 3419 | |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3420 | // FIXME: Move up |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 3421 | llvm::SmallString<36> Name; |
| 3422 | llvm::raw_svector_ostream(Name) << "__block_literal_" |
| 3423 | << ++UniqueBlockParmTypeID; |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3424 | RecordDecl *T; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3425 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3426 | &Idents.get(Name.str())); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3427 | T->startDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3428 | QualType FieldTypes[] = { |
| 3429 | getPointerType(VoidPtrTy), |
| 3430 | IntTy, |
| 3431 | IntTy, |
| 3432 | getPointerType(VoidPtrTy), |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3433 | (BlockHasCopyDispose ? |
| 3434 | getPointerType(getBlockDescriptorExtendedType()) : |
| 3435 | getPointerType(getBlockDescriptorType())) |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3436 | }; |
| 3437 | |
| 3438 | const char *FieldNames[] = { |
| 3439 | "__isa", |
| 3440 | "__flags", |
| 3441 | "__reserved", |
| 3442 | "__FuncPtr", |
| 3443 | "__descriptor" |
| 3444 | }; |
| 3445 | |
| 3446 | for (size_t i = 0; i < 5; ++i) { |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3447 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3448 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3449 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3450 | /*BitWidth=*/0, /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3451 | Field->setAccess(AS_public); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3452 | T->addDecl(Field); |
| 3453 | } |
| 3454 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 3455 | for (unsigned i = 0; i < Layout.size(); ++i) { |
| 3456 | const Expr *E = Layout[i]; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3457 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 3458 | QualType FieldType = E->getType(); |
| 3459 | IdentifierInfo *FieldName = 0; |
| 3460 | if (isa<CXXThisExpr>(E)) { |
| 3461 | FieldName = &Idents.get("this"); |
| 3462 | } else if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E)) { |
| 3463 | const ValueDecl *D = BDRE->getDecl(); |
| 3464 | FieldName = D->getIdentifier(); |
| 3465 | if (BDRE->isByRef()) |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3466 | FieldType = BuildByRefType(D->getName(), FieldType); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 3467 | } else { |
| 3468 | // Padding. |
| 3469 | assert(isa<ConstantArrayType>(FieldType) && |
| 3470 | isa<DeclRefExpr>(E) && |
| 3471 | !cast<DeclRefExpr>(E)->getDecl()->getDeclName() && |
| 3472 | "doesn't match characteristics of padding decl"); |
| 3473 | } |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3474 | |
| 3475 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 3476 | FieldName, FieldType, /*TInfo=*/0, |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3477 | /*BitWidth=*/0, /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3478 | Field->setAccess(AS_public); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3479 | T->addDecl(Field); |
| 3480 | } |
| 3481 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3482 | T->completeDefinition(); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3483 | |
| 3484 | return getPointerType(getTagDeclType(T)); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3485 | } |
| 3486 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3487 | void ASTContext::setObjCFastEnumerationStateType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3488 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3489 | assert(Rec && "Invalid ObjCFAstEnumerationStateType"); |
| 3490 | ObjCFastEnumerationStateTypeDecl = Rec->getDecl(); |
| 3491 | } |
| 3492 | |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3493 | // This returns true if a type has been typedefed to BOOL: |
| 3494 | // typedef <type> BOOL; |
Chris Lattner | 2d99833 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 3495 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3496 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | bb49c3e | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 3497 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 3498 | return II->isStr("BOOL"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3499 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3500 | return false; |
| 3501 | } |
| 3502 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3503 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3504 | /// purpose. |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3505 | CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) { |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3506 | CharUnits sz = getTypeSizeInChars(type); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3507 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3508 | // 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] | 3509 | if (sz.isPositive() && type->isIntegralOrEnumerationType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3510 | sz = std::max(sz, getTypeSizeInChars(IntTy)); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3511 | // Treat arrays as pointers, since that's how they're passed in. |
| 3512 | else if (type->isArrayType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3513 | sz = getTypeSizeInChars(VoidPtrTy); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3514 | return sz; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3515 | } |
| 3516 | |
| 3517 | static inline |
| 3518 | std::string charUnitsToString(const CharUnits &CU) { |
| 3519 | return llvm::itostr(CU.getQuantity()); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3520 | } |
| 3521 | |
Fariborz Jahanian | 6f46c26 | 2010-04-08 18:06:22 +0000 | [diff] [blame] | 3522 | /// getObjCEncodingForBlockDecl - Return the encoded type for this block |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3523 | /// declaration. |
| 3524 | void ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr, |
| 3525 | std::string& S) { |
| 3526 | const BlockDecl *Decl = Expr->getBlockDecl(); |
| 3527 | QualType BlockTy = |
| 3528 | Expr->getType()->getAs<BlockPointerType>()->getPointeeType(); |
| 3529 | // Encode result type. |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 3530 | getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3531 | // Compute size of all parameters. |
| 3532 | // Start with computing size of a pointer in number of bytes. |
| 3533 | // FIXME: There might(should) be a better way of doing this computation! |
| 3534 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3535 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
| 3536 | CharUnits ParmOffset = PtrSize; |
Fariborz Jahanian | 6f46c26 | 2010-04-08 18:06:22 +0000 | [diff] [blame] | 3537 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3538 | E = Decl->param_end(); PI != E; ++PI) { |
| 3539 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3540 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3541 | assert (sz.isPositive() && "BlockExpr - Incomplete param type"); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3542 | ParmOffset += sz; |
| 3543 | } |
| 3544 | // Size of the argument frame |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3545 | S += charUnitsToString(ParmOffset); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3546 | // Block pointer and offset. |
| 3547 | S += "@?0"; |
| 3548 | ParmOffset = PtrSize; |
| 3549 | |
| 3550 | // Argument types. |
| 3551 | ParmOffset = PtrSize; |
| 3552 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E = |
| 3553 | Decl->param_end(); PI != E; ++PI) { |
| 3554 | ParmVarDecl *PVDecl = *PI; |
| 3555 | QualType PType = PVDecl->getOriginalType(); |
| 3556 | if (const ArrayType *AT = |
| 3557 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3558 | // Use array's original type only if it has known number of |
| 3559 | // elements. |
| 3560 | if (!isa<ConstantArrayType>(AT)) |
| 3561 | PType = PVDecl->getType(); |
| 3562 | } else if (PType->isFunctionType()) |
| 3563 | PType = PVDecl->getType(); |
| 3564 | getObjCEncodingForType(PType, S); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3565 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3566 | ParmOffset += getObjCEncodingTypeSize(PType); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3567 | } |
| 3568 | } |
| 3569 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3570 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3571 | /// declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3572 | void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Chris Lattner | e6db3b0 | 2008-11-19 07:24:05 +0000 | [diff] [blame] | 3573 | std::string& S) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3574 | // FIXME: This is not very efficient. |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3575 | // Encode type qualifer, 'in', 'inout', etc. for the return type. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3576 | getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3577 | // Encode result type. |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3578 | getObjCEncodingForType(Decl->getResultType(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3579 | // Compute size of all parameters. |
| 3580 | // Start with computing size of a pointer in number of bytes. |
| 3581 | // FIXME: There might(should) be a better way of doing this computation! |
| 3582 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3583 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3584 | // The first two arguments (self and _cmd) are pointers; account for |
| 3585 | // their size. |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3586 | CharUnits ParmOffset = 2 * PtrSize; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3587 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 3588 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3589 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3590 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3591 | assert (sz.isPositive() && |
| 3592 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3593 | ParmOffset += sz; |
| 3594 | } |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3595 | S += charUnitsToString(ParmOffset); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3596 | S += "@0:"; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3597 | S += charUnitsToString(PtrSize); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3598 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3599 | // Argument types. |
| 3600 | ParmOffset = 2 * PtrSize; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3601 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 3602 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3603 | ParmVarDecl *PVDecl = *PI; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3604 | QualType PType = PVDecl->getOriginalType(); |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 3605 | if (const ArrayType *AT = |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 3606 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3607 | // Use array's original type only if it has known number of |
| 3608 | // elements. |
Steve Naroff | bb3fde3 | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 3609 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 3610 | PType = PVDecl->getType(); |
| 3611 | } else if (PType->isFunctionType()) |
| 3612 | PType = PVDecl->getType(); |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3613 | // Process argument qualifiers for user supplied arguments; such as, |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3614 | // 'in', 'inout', etc. |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 3615 | getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3616 | getObjCEncodingForType(PType, S); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3617 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3618 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3619 | } |
| 3620 | } |
| 3621 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3622 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 3623 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3624 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 3625 | /// NULL when getting encodings for protocol properties. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3626 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 3627 | /// attributes readonly and bycopy are encoded as single characters. The |
| 3628 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 3629 | /// encoded as single characters, followed by an identifier. Property types |
| 3630 | /// are also encoded as a parametrized attribute. The characters used to encode |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 3631 | /// these attributes are defined by the following enumeration: |
| 3632 | /// @code |
| 3633 | /// enum PropertyAttributes { |
| 3634 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 3635 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 3636 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 3637 | /// kPropertyDynamic = 'D', // property is dynamic |
| 3638 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 3639 | /// kPropertySetter = 'S', // followed by setter selector name |
| 3640 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
| 3641 | /// kPropertyType = 't' // followed by old-style type encoding. |
| 3642 | /// kPropertyWeak = 'W' // 'weak' property |
| 3643 | /// kPropertyStrong = 'P' // property GC'able |
| 3644 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 3645 | /// }; |
| 3646 | /// @endcode |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3647 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3648 | const Decl *Container, |
Chris Lattner | e6db3b0 | 2008-11-19 07:24:05 +0000 | [diff] [blame] | 3649 | std::string& S) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3650 | // Collect information from the property implementation decl(s). |
| 3651 | bool Dynamic = false; |
| 3652 | ObjCPropertyImplDecl *SynthesizePID = 0; |
| 3653 | |
| 3654 | // FIXME: Duplicated code due to poor abstraction. |
| 3655 | if (Container) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3656 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3657 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
| 3658 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3659 | i = CID->propimpl_begin(), e = CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3660 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3661 | ObjCPropertyImplDecl *PID = *i; |
| 3662 | if (PID->getPropertyDecl() == PD) { |
| 3663 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 3664 | Dynamic = true; |
| 3665 | } else { |
| 3666 | SynthesizePID = PID; |
| 3667 | } |
| 3668 | } |
| 3669 | } |
| 3670 | } else { |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3671 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3672 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3673 | i = OID->propimpl_begin(), e = OID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3674 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3675 | ObjCPropertyImplDecl *PID = *i; |
| 3676 | if (PID->getPropertyDecl() == PD) { |
| 3677 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 3678 | Dynamic = true; |
| 3679 | } else { |
| 3680 | SynthesizePID = PID; |
| 3681 | } |
| 3682 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3683 | } |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3684 | } |
| 3685 | } |
| 3686 | |
| 3687 | // FIXME: This is not very efficient. |
| 3688 | S = "T"; |
| 3689 | |
| 3690 | // Encode result type. |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3691 | // GCC has some special rules regarding encoding of properties which |
| 3692 | // closely resembles encoding of ivars. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3693 | getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3694 | true /* outermost type */, |
| 3695 | true /* encoding for property */); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3696 | |
| 3697 | if (PD->isReadOnly()) { |
| 3698 | S += ",R"; |
| 3699 | } else { |
| 3700 | switch (PD->getSetterKind()) { |
| 3701 | case ObjCPropertyDecl::Assign: break; |
| 3702 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3703 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3704 | } |
| 3705 | } |
| 3706 | |
| 3707 | // It really isn't clear at all what this means, since properties |
| 3708 | // are "dynamic by default". |
| 3709 | if (Dynamic) |
| 3710 | S += ",D"; |
| 3711 | |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3712 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 3713 | S += ",N"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3714 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3715 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 3716 | S += ",G"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3717 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3718 | } |
| 3719 | |
| 3720 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 3721 | S += ",S"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3722 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3723 | } |
| 3724 | |
| 3725 | if (SynthesizePID) { |
| 3726 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 3727 | S += ",V"; |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 3728 | S += OID->getNameAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3729 | } |
| 3730 | |
| 3731 | // FIXME: OBJCGC: weak & strong |
| 3732 | } |
| 3733 | |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3734 | /// getLegacyIntegralTypeEncoding - |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3735 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
| 3736 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3737 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
| 3738 | /// |
| 3739 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3740 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3741 | if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) { |
Fariborz Jahanian | c657eba | 2009-02-11 23:59:18 +0000 | [diff] [blame] | 3742 | if (BT->getKind() == BuiltinType::ULong && |
| 3743 | ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3744 | PointeeTy = UnsignedIntTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3745 | else |
Fariborz Jahanian | c657eba | 2009-02-11 23:59:18 +0000 | [diff] [blame] | 3746 | if (BT->getKind() == BuiltinType::Long && |
| 3747 | ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3748 | PointeeTy = IntTy; |
| 3749 | } |
| 3750 | } |
| 3751 | } |
| 3752 | |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3753 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 3754 | const FieldDecl *Field) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3755 | // We follow the behavior of gcc, expanding structures which are |
| 3756 | // directly pointed to, and expanding embedded structures. Note that |
| 3757 | // these rules are sufficient to prevent recursive encoding of the |
| 3758 | // same type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3759 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
Fariborz Jahanian | 5b8c7d9 | 2008-12-22 23:22:27 +0000 | [diff] [blame] | 3760 | true /* outermost type */); |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3761 | } |
| 3762 | |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3763 | static char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) { |
| 3764 | switch (T->getAs<BuiltinType>()->getKind()) { |
| 3765 | default: assert(0 && "Unhandled builtin type kind"); |
| 3766 | case BuiltinType::Void: return 'v'; |
| 3767 | case BuiltinType::Bool: return 'B'; |
| 3768 | case BuiltinType::Char_U: |
| 3769 | case BuiltinType::UChar: return 'C'; |
| 3770 | case BuiltinType::UShort: return 'S'; |
| 3771 | case BuiltinType::UInt: return 'I'; |
| 3772 | case BuiltinType::ULong: |
| 3773 | return |
| 3774 | (const_cast<ASTContext *>(C))->getIntWidth(T) == 32 ? 'L' : 'Q'; |
| 3775 | case BuiltinType::UInt128: return 'T'; |
| 3776 | case BuiltinType::ULongLong: return 'Q'; |
| 3777 | case BuiltinType::Char_S: |
| 3778 | case BuiltinType::SChar: return 'c'; |
| 3779 | case BuiltinType::Short: return 's'; |
John McCall | 24da709 | 2010-06-11 10:11:05 +0000 | [diff] [blame] | 3780 | case BuiltinType::WChar: |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3781 | case BuiltinType::Int: return 'i'; |
| 3782 | case BuiltinType::Long: |
| 3783 | return |
| 3784 | (const_cast<ASTContext *>(C))->getIntWidth(T) == 32 ? 'l' : 'q'; |
| 3785 | case BuiltinType::LongLong: return 'q'; |
| 3786 | case BuiltinType::Int128: return 't'; |
| 3787 | case BuiltinType::Float: return 'f'; |
| 3788 | case BuiltinType::Double: return 'd'; |
Daniel Dunbar | 3a0be84 | 2010-10-11 21:13:48 +0000 | [diff] [blame] | 3789 | case BuiltinType::LongDouble: return 'D'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3790 | } |
| 3791 | } |
| 3792 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3793 | static void EncodeBitField(const ASTContext *Context, std::string& S, |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3794 | QualType T, const FieldDecl *FD) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3795 | const Expr *E = FD->getBitWidth(); |
| 3796 | assert(E && "bitfield width not there - getObjCEncodingForTypeImpl"); |
| 3797 | ASTContext *Ctx = const_cast<ASTContext*>(Context); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3798 | S += 'b'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3799 | // The NeXT runtime encodes bit fields as b followed by the number of bits. |
| 3800 | // The GNU runtime requires more information; bitfields are encoded as b, |
| 3801 | // then the offset (in bits) of the first element, then the type of the |
| 3802 | // bitfield, then the size in bits. For example, in this structure: |
| 3803 | // |
| 3804 | // struct |
| 3805 | // { |
| 3806 | // int integer; |
| 3807 | // int flags:2; |
| 3808 | // }; |
| 3809 | // On a 32-bit system, the encoding for flags would be b2 for the NeXT |
| 3810 | // runtime, but b32i2 for the GNU runtime. The reason for this extra |
| 3811 | // information is not especially sensible, but we're stuck with it for |
| 3812 | // compatibility with GCC, although providing it breaks anything that |
| 3813 | // actually uses runtime introspection and wants to work on both runtimes... |
| 3814 | if (!Ctx->getLangOptions().NeXTRuntime) { |
| 3815 | const RecordDecl *RD = FD->getParent(); |
| 3816 | const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD); |
| 3817 | // FIXME: This same linear search is also used in ExprConstant - it might |
| 3818 | // be better if the FieldDecl stored its offset. We'd be increasing the |
| 3819 | // size of the object slightly, but saving some time every time it is used. |
| 3820 | unsigned i = 0; |
| 3821 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 3822 | FieldEnd = RD->field_end(); |
| 3823 | Field != FieldEnd; (void)++Field, ++i) { |
| 3824 | if (*Field == FD) |
| 3825 | break; |
| 3826 | } |
| 3827 | S += llvm::utostr(RL.getFieldOffset(i)); |
| 3828 | S += ObjCEncodingForPrimitiveKind(Context, T); |
| 3829 | } |
| 3830 | unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue(); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3831 | S += llvm::utostr(N); |
| 3832 | } |
| 3833 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 3834 | // FIXME: Use SmallString for accumulating string. |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3835 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 3836 | bool ExpandPointedToStructures, |
| 3837 | bool ExpandStructures, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 3838 | const FieldDecl *FD, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3839 | bool OutermostType, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3840 | bool EncodingProperty) { |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3841 | if (T->getAs<BuiltinType>()) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3842 | if (FD && FD->isBitField()) |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3843 | return EncodeBitField(this, S, T, FD); |
| 3844 | S += ObjCEncodingForPrimitiveKind(this, T); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3845 | return; |
| 3846 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3847 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3848 | if (const ComplexType *CT = T->getAs<ComplexType>()) { |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 3849 | S += 'j'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3850 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false, |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 3851 | false); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3852 | return; |
| 3853 | } |
Fariborz Jahanian | 60bce3e | 2009-11-23 20:40:50 +0000 | [diff] [blame] | 3854 | |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 3855 | // encoding for pointer or r3eference types. |
| 3856 | QualType PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3857 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Fariborz Jahanian | 8d2c0a9 | 2009-11-30 18:43:52 +0000 | [diff] [blame] | 3858 | if (PT->isObjCSelType()) { |
| 3859 | S += ':'; |
| 3860 | return; |
| 3861 | } |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 3862 | PointeeTy = PT->getPointeeType(); |
| 3863 | } |
| 3864 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 3865 | PointeeTy = RT->getPointeeType(); |
| 3866 | if (!PointeeTy.isNull()) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3867 | bool isReadOnly = false; |
| 3868 | // For historical/compatibility reasons, the read-only qualifier of the |
| 3869 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 3870 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3871 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3872 | if (isa<TypedefType>(T.getTypePtr())) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3873 | if (OutermostType && T.isConstQualified()) { |
| 3874 | isReadOnly = true; |
| 3875 | S += 'r'; |
| 3876 | } |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 3877 | } else if (OutermostType) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3878 | QualType P = PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3879 | while (P->getAs<PointerType>()) |
| 3880 | P = P->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3881 | if (P.isConstQualified()) { |
| 3882 | isReadOnly = true; |
| 3883 | S += 'r'; |
| 3884 | } |
| 3885 | } |
| 3886 | if (isReadOnly) { |
| 3887 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 3888 | // combinations need to be rearranged. |
| 3889 | // Rewrite "in const" from "nr" to "rn" |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 3890 | if (llvm::StringRef(S).endswith("nr")) |
| 3891 | S.replace(S.end()-2, S.end(), "rn"); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3892 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3893 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3894 | if (PointeeTy->isCharType()) { |
| 3895 | // char pointer types should be encoded as '*' unless it is a |
| 3896 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3897 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3898 | S += '*'; |
| 3899 | return; |
| 3900 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3901 | } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) { |
Steve Naroff | 9533a7f | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 3902 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 3903 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 3904 | S += '#'; |
| 3905 | return; |
| 3906 | } |
| 3907 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 3908 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 3909 | S += '@'; |
| 3910 | return; |
| 3911 | } |
| 3912 | // fall through... |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3913 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3914 | S += '^'; |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3915 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 3916 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3917 | getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3918 | NULL); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3919 | return; |
| 3920 | } |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 3921 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3922 | if (const ArrayType *AT = |
| 3923 | // Ignore type qualifiers etc. |
| 3924 | dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3925 | if (isa<IncompleteArrayType>(AT)) { |
| 3926 | // Incomplete arrays are encoded as a pointer to the array element. |
| 3927 | S += '^'; |
| 3928 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3929 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3930 | false, ExpandStructures, FD); |
| 3931 | } else { |
| 3932 | S += '['; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3933 | |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3934 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 3935 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 3936 | else { |
| 3937 | //Variable length arrays are encoded as a regular array with 0 elements. |
| 3938 | assert(isa<VariableArrayType>(AT) && "Unknown array type!"); |
| 3939 | S += '0'; |
| 3940 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3941 | |
| 3942 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3943 | false, ExpandStructures, FD); |
| 3944 | S += ']'; |
| 3945 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3946 | return; |
| 3947 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3948 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3949 | if (T->getAs<FunctionType>()) { |
Anders Carlsson | c0a87b7 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 3950 | S += '?'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3951 | return; |
| 3952 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3953 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3954 | if (const RecordType *RTy = T->getAs<RecordType>()) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3955 | RecordDecl *RDecl = RTy->getDecl(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3956 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 3957 | // Anonymous structures print as '?' |
| 3958 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 3959 | S += II->getName(); |
Fariborz Jahanian | 6fb9439 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 3960 | if (ClassTemplateSpecializationDecl *Spec |
| 3961 | = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) { |
| 3962 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 3963 | std::string TemplateArgsStr |
| 3964 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3965 | TemplateArgs.data(), |
| 3966 | TemplateArgs.size(), |
Fariborz Jahanian | 6fb9439 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 3967 | (*this).PrintingPolicy); |
| 3968 | |
| 3969 | S += TemplateArgsStr; |
| 3970 | } |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 3971 | } else { |
| 3972 | S += '?'; |
| 3973 | } |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3974 | if (ExpandStructures) { |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3975 | S += '='; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3976 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 3977 | FieldEnd = RDecl->field_end(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3978 | Field != FieldEnd; ++Field) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3979 | if (FD) { |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3980 | S += '"'; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3981 | S += Field->getNameAsString(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3982 | S += '"'; |
| 3983 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3984 | |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3985 | // Special case bit-fields. |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3986 | if (Field->isBitField()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3987 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3988 | (*Field)); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3989 | } else { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3990 | QualType qt = Field->getType(); |
| 3991 | getLegacyIntegralTypeEncoding(qt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3992 | getObjCEncodingForTypeImpl(qt, S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3993 | FD); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3994 | } |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3995 | } |
Fariborz Jahanian | 6de88a8 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 3996 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3997 | S += RDecl->isUnion() ? ')' : '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3998 | return; |
| 3999 | } |
Fariborz Jahanian | e6012c7 | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 4000 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4001 | if (T->isEnumeralType()) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4002 | if (FD && FD->isBitField()) |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4003 | EncodeBitField(this, S, T, FD); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4004 | else |
| 4005 | S += 'i'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4006 | return; |
| 4007 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4008 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4009 | if (T->isBlockPointerType()) { |
Steve Naroff | 21a98b1 | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 4010 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4011 | return; |
| 4012 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4013 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4014 | // Ignore protocol qualifiers when mangling at this level. |
| 4015 | if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>()) |
| 4016 | T = OT->getBaseType(); |
| 4017 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4018 | if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4019 | // @encode(class_name) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4020 | ObjCInterfaceDecl *OI = OIT->getDecl(); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4021 | S += '{'; |
| 4022 | const IdentifierInfo *II = OI->getIdentifier(); |
| 4023 | S += II->getName(); |
| 4024 | S += '='; |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4025 | llvm::SmallVector<ObjCIvarDecl*, 32> Ivars; |
| 4026 | DeepCollectObjCIvars(OI, true, Ivars); |
| 4027 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) { |
| 4028 | FieldDecl *Field = cast<FieldDecl>(Ivars[i]); |
| 4029 | if (Field->isBitField()) |
| 4030 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4031 | else |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4032 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4033 | } |
| 4034 | S += '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4035 | return; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4036 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4037 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4038 | if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4039 | if (OPT->isObjCIdType()) { |
| 4040 | S += '@'; |
| 4041 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4042 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4043 | |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4044 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
| 4045 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
| 4046 | // Since this is a binary compatibility issue, need to consult with runtime |
| 4047 | // folks. Fortunately, this is a *very* obsure construct. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4048 | S += '#'; |
| 4049 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4050 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4051 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4052 | if (OPT->isObjCQualifiedIdType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4053 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4054 | ExpandPointedToStructures, |
| 4055 | ExpandStructures, FD); |
| 4056 | if (FD || EncodingProperty) { |
| 4057 | // Note that we do extended encoding of protocol qualifer list |
| 4058 | // Only when doing ivar or property encoding. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4059 | S += '"'; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4060 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 4061 | E = OPT->qual_end(); I != E; ++I) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4062 | S += '<'; |
| 4063 | S += (*I)->getNameAsString(); |
| 4064 | S += '>'; |
| 4065 | } |
| 4066 | S += '"'; |
| 4067 | } |
| 4068 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4069 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4070 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4071 | QualType PointeeTy = OPT->getPointeeType(); |
| 4072 | if (!EncodingProperty && |
| 4073 | isa<TypedefType>(PointeeTy.getTypePtr())) { |
| 4074 | // Another historical/compatibility reason. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4075 | // We encode the underlying type which comes out as |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4076 | // {...}; |
| 4077 | S += '^'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4078 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 4079 | false, ExpandPointedToStructures, |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4080 | NULL); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4081 | return; |
| 4082 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4083 | |
| 4084 | S += '@'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4085 | if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4086 | S += '"'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4087 | S += OPT->getInterfaceDecl()->getIdentifier()->getName(); |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4088 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 4089 | E = OPT->qual_end(); I != E; ++I) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4090 | S += '<'; |
| 4091 | S += (*I)->getNameAsString(); |
| 4092 | S += '>'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4093 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4094 | S += '"'; |
| 4095 | } |
| 4096 | return; |
| 4097 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4098 | |
John McCall | 532ec7b | 2010-05-17 23:56:34 +0000 | [diff] [blame] | 4099 | // gcc just blithely ignores member pointers. |
| 4100 | // TODO: maybe there should be a mangling for these |
| 4101 | if (T->getAs<MemberPointerType>()) |
| 4102 | return; |
Fariborz Jahanian | e6012c7 | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 4103 | |
| 4104 | if (T->isVectorType()) { |
| 4105 | // This matches gcc's encoding, even though technically it is |
| 4106 | // insufficient. |
| 4107 | // FIXME. We should do a better job than gcc. |
| 4108 | return; |
| 4109 | } |
| 4110 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4111 | assert(0 && "@encode for type not implemented!"); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4112 | } |
| 4113 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4114 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 4115 | std::string& S) const { |
| 4116 | if (QT & Decl::OBJC_TQ_In) |
| 4117 | S += 'n'; |
| 4118 | if (QT & Decl::OBJC_TQ_Inout) |
| 4119 | S += 'N'; |
| 4120 | if (QT & Decl::OBJC_TQ_Out) |
| 4121 | S += 'o'; |
| 4122 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 4123 | S += 'O'; |
| 4124 | if (QT & Decl::OBJC_TQ_Byref) |
| 4125 | S += 'R'; |
| 4126 | if (QT & Decl::OBJC_TQ_Oneway) |
| 4127 | S += 'V'; |
| 4128 | } |
| 4129 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4130 | void ASTContext::setBuiltinVaListType(QualType T) { |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 4131 | assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4132 | |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 4133 | BuiltinVaListType = T; |
| 4134 | } |
| 4135 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4136 | void ASTContext::setObjCIdType(QualType T) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 4137 | ObjCIdTypedefType = T; |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 4138 | } |
| 4139 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4140 | void ASTContext::setObjCSelType(QualType T) { |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 4141 | ObjCSelTypedefType = T; |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 4142 | } |
| 4143 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4144 | void ASTContext::setObjCProtoType(QualType QT) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4145 | ObjCProtoType = QT; |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 4146 | } |
| 4147 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4148 | void ASTContext::setObjCClassType(QualType T) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 4149 | ObjCClassTypedefType = T; |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 4150 | } |
| 4151 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4152 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4153 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 4154 | "'NSConstantString' type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4155 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4156 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 4157 | } |
| 4158 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4159 | /// \brief Retrieve the template name that corresponds to a non-empty |
| 4160 | /// lookup. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4161 | TemplateName ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin, |
| 4162 | UnresolvedSetIterator End) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4163 | unsigned size = End - Begin; |
| 4164 | assert(size > 1 && "set is not overloaded!"); |
| 4165 | |
| 4166 | void *memory = Allocate(sizeof(OverloadedTemplateStorage) + |
| 4167 | size * sizeof(FunctionTemplateDecl*)); |
| 4168 | OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size); |
| 4169 | |
| 4170 | NamedDecl **Storage = OT->getStorage(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4171 | for (UnresolvedSetIterator I = Begin; I != End; ++I) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4172 | NamedDecl *D = *I; |
| 4173 | assert(isa<FunctionTemplateDecl>(D) || |
| 4174 | (isa<UsingShadowDecl>(D) && |
| 4175 | isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))); |
| 4176 | *Storage++ = D; |
| 4177 | } |
| 4178 | |
| 4179 | return TemplateName(OT); |
| 4180 | } |
| 4181 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4182 | /// \brief Retrieve the template name that represents a qualified |
| 4183 | /// template name such as \c std::vector. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4184 | TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4185 | bool TemplateKeyword, |
| 4186 | TemplateDecl *Template) { |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4187 | // FIXME: Canonicalization? |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4188 | llvm::FoldingSetNodeID ID; |
| 4189 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 4190 | |
| 4191 | void *InsertPos = 0; |
| 4192 | QualifiedTemplateName *QTN = |
| 4193 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4194 | if (!QTN) { |
| 4195 | QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); |
| 4196 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 4197 | } |
| 4198 | |
| 4199 | return TemplateName(QTN); |
| 4200 | } |
| 4201 | |
| 4202 | /// \brief Retrieve the template name that represents a dependent |
| 4203 | /// template name such as \c MetaFun::template apply. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4204 | TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4205 | const IdentifierInfo *Name) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4206 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 4207 | "Nested name specifier must be dependent"); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4208 | |
| 4209 | llvm::FoldingSetNodeID ID; |
| 4210 | DependentTemplateName::Profile(ID, NNS, Name); |
| 4211 | |
| 4212 | void *InsertPos = 0; |
| 4213 | DependentTemplateName *QTN = |
| 4214 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4215 | |
| 4216 | if (QTN) |
| 4217 | return TemplateName(QTN); |
| 4218 | |
| 4219 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 4220 | if (CanonNNS == NNS) { |
| 4221 | QTN = new (*this,4) DependentTemplateName(NNS, Name); |
| 4222 | } else { |
| 4223 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
| 4224 | QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4225 | DependentTemplateName *CheckQTN = |
| 4226 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4227 | assert(!CheckQTN && "Dependent type name canonicalization broken"); |
| 4228 | (void)CheckQTN; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4229 | } |
| 4230 | |
| 4231 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 4232 | return TemplateName(QTN); |
| 4233 | } |
| 4234 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4235 | /// \brief Retrieve the template name that represents a dependent |
| 4236 | /// template name such as \c MetaFun::template operator+. |
| 4237 | TemplateName |
| 4238 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
| 4239 | OverloadedOperatorKind Operator) { |
| 4240 | assert((!NNS || NNS->isDependent()) && |
| 4241 | "Nested name specifier must be dependent"); |
| 4242 | |
| 4243 | llvm::FoldingSetNodeID ID; |
| 4244 | DependentTemplateName::Profile(ID, NNS, Operator); |
| 4245 | |
| 4246 | void *InsertPos = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4247 | DependentTemplateName *QTN |
| 4248 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4249 | |
| 4250 | if (QTN) |
| 4251 | return TemplateName(QTN); |
| 4252 | |
| 4253 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 4254 | if (CanonNNS == NNS) { |
| 4255 | QTN = new (*this,4) DependentTemplateName(NNS, Operator); |
| 4256 | } else { |
| 4257 | TemplateName Canon = getDependentTemplateName(CanonNNS, Operator); |
| 4258 | QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4259 | |
| 4260 | DependentTemplateName *CheckQTN |
| 4261 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4262 | assert(!CheckQTN && "Dependent template name canonicalization broken"); |
| 4263 | (void)CheckQTN; |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4264 | } |
| 4265 | |
| 4266 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 4267 | return TemplateName(QTN); |
| 4268 | } |
| 4269 | |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4270 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | d934112 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 4271 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 4272 | /// is actually a value of type @c TargetInfo::IntType. |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 4273 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4274 | switch (Type) { |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 4275 | case TargetInfo::NoInt: return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4276 | case TargetInfo::SignedShort: return ShortTy; |
| 4277 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 4278 | case TargetInfo::SignedInt: return IntTy; |
| 4279 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 4280 | case TargetInfo::SignedLong: return LongTy; |
| 4281 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 4282 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 4283 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 4284 | } |
| 4285 | |
| 4286 | assert(false && "Unhandled TargetInfo::IntType value"); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 4287 | return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4288 | } |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 4289 | |
| 4290 | //===----------------------------------------------------------------------===// |
| 4291 | // Type Predicates. |
| 4292 | //===----------------------------------------------------------------------===// |
| 4293 | |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4294 | /// isObjCNSObjectType - Return true if this is an NSObject object using |
| 4295 | /// NSObject attribute on a c-style pointer type. |
| 4296 | /// FIXME - Make it work directly on types. |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 4297 | /// FIXME: Move to Type. |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4298 | /// |
| 4299 | bool ASTContext::isObjCNSObjectType(QualType Ty) const { |
| 4300 | if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) { |
| 4301 | if (TypedefDecl *TD = TDT->getDecl()) |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 4302 | if (TD->getAttr<ObjCNSObjectAttr>()) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4303 | return true; |
| 4304 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4305 | return false; |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4306 | } |
| 4307 | |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4308 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 4309 | /// garbage collection attribute. |
| 4310 | /// |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4311 | Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const { |
| 4312 | Qualifiers::GC GCAttrs = Qualifiers::GCNone; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4313 | if (getLangOptions().ObjC1 && |
| 4314 | getLangOptions().getGCMode() != LangOptions::NonGC) { |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 4315 | GCAttrs = Ty.getObjCGCAttr(); |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4316 | // Default behavious under objective-c's gc is for objective-c pointers |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4317 | // (or pointers to them) be treated as though they were declared |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 4318 | // as __strong. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4319 | if (GCAttrs == Qualifiers::GCNone) { |
Fariborz Jahanian | 75212ee | 2009-09-10 23:38:45 +0000 | [diff] [blame] | 4320 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4321 | GCAttrs = Qualifiers::Strong; |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 4322 | else if (Ty->isPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4323 | return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 4324 | } |
Fariborz Jahanian | c211218 | 2009-04-11 00:00:54 +0000 | [diff] [blame] | 4325 | // Non-pointers have none gc'able attribute regardless of the attribute |
| 4326 | // set on them. |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 4327 | else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4328 | return Qualifiers::GCNone; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4329 | } |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 4330 | return GCAttrs; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4331 | } |
| 4332 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4333 | //===----------------------------------------------------------------------===// |
| 4334 | // Type Compatibility Testing |
| 4335 | //===----------------------------------------------------------------------===// |
Chris Lattner | 770951b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 4336 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4337 | /// areCompatVectorTypes - Return true if the two specified vector types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4338 | /// compatible. |
| 4339 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 4340 | const VectorType *RHS) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 4341 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4342 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4343 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4344 | } |
| 4345 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 4346 | bool ASTContext::areCompatibleVectorTypes(QualType FirstVec, |
| 4347 | QualType SecondVec) { |
| 4348 | assert(FirstVec->isVectorType() && "FirstVec should be a vector type"); |
| 4349 | assert(SecondVec->isVectorType() && "SecondVec should be a vector type"); |
| 4350 | |
| 4351 | if (hasSameUnqualifiedType(FirstVec, SecondVec)) |
| 4352 | return true; |
| 4353 | |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 4354 | // Treat Neon vector types and most AltiVec vector types as if they are the |
| 4355 | // equivalent GCC vector types. |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 4356 | const VectorType *First = FirstVec->getAs<VectorType>(); |
| 4357 | const VectorType *Second = SecondVec->getAs<VectorType>(); |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 4358 | if (First->getNumElements() == Second->getNumElements() && |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 4359 | hasSameType(First->getElementType(), Second->getElementType()) && |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 4360 | First->getVectorKind() != VectorType::AltiVecPixel && |
| 4361 | First->getVectorKind() != VectorType::AltiVecBool && |
| 4362 | Second->getVectorKind() != VectorType::AltiVecPixel && |
| 4363 | Second->getVectorKind() != VectorType::AltiVecBool) |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 4364 | return true; |
| 4365 | |
| 4366 | return false; |
| 4367 | } |
| 4368 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4369 | //===----------------------------------------------------------------------===// |
| 4370 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 4371 | //===----------------------------------------------------------------------===// |
| 4372 | |
| 4373 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 4374 | /// inheritance hierarchy of 'rProto'. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 4375 | bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 4376 | ObjCProtocolDecl *rProto) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4377 | if (lProto == rProto) |
| 4378 | return true; |
| 4379 | for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), |
| 4380 | E = rProto->protocol_end(); PI != E; ++PI) |
| 4381 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 4382 | return true; |
| 4383 | return false; |
| 4384 | } |
| 4385 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4386 | /// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...> |
| 4387 | /// return true if lhs's protocols conform to rhs's protocol; false |
| 4388 | /// otherwise. |
| 4389 | bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) { |
| 4390 | if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType()) |
| 4391 | return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false); |
| 4392 | return false; |
| 4393 | } |
| 4394 | |
Fariborz Jahanian | a8f8dac | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 4395 | /// ObjCQualifiedClassTypesAreCompatible - compare Class<p,...> and |
| 4396 | /// Class<p1, ...>. |
| 4397 | bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs, |
| 4398 | QualType rhs) { |
| 4399 | const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>(); |
| 4400 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
| 4401 | assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible"); |
| 4402 | |
| 4403 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4404 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4405 | bool match = false; |
| 4406 | ObjCProtocolDecl *lhsProto = *I; |
| 4407 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 4408 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 4409 | ObjCProtocolDecl *rhsProto = *J; |
| 4410 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) { |
| 4411 | match = true; |
| 4412 | break; |
| 4413 | } |
| 4414 | } |
| 4415 | if (!match) |
| 4416 | return false; |
| 4417 | } |
| 4418 | return true; |
| 4419 | } |
| 4420 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4421 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 4422 | /// ObjCQualifiedIDType. |
| 4423 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 4424 | bool compare) { |
| 4425 | // Allow id<P..> and an 'id' or void* type in all cases. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4426 | if (lhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4427 | lhs->isObjCIdType() || lhs->isObjCClassType()) |
| 4428 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4429 | else if (rhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4430 | rhs->isObjCIdType() || rhs->isObjCClassType()) |
| 4431 | return true; |
| 4432 | |
| 4433 | if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4434 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4435 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4436 | if (!rhsOPT) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4437 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4438 | if (rhsOPT->qual_empty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4439 | // If the RHS is a unqualified interface pointer "NSString*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4440 | // make sure we check the class hierarchy. |
| 4441 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 4442 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4443 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4444 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4445 | // see if static class implements all of id's protocols, directly or |
| 4446 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 4447 | if (!rhsID->ClassImplementsProtocol(*I, true)) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4448 | return false; |
| 4449 | } |
| 4450 | } |
| 4451 | // If there are no qualifiers and no interface, we have an 'id'. |
| 4452 | return true; |
| 4453 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4454 | // Both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4455 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4456 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4457 | ObjCProtocolDecl *lhsProto = *I; |
| 4458 | bool match = false; |
| 4459 | |
| 4460 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4461 | // see if static class implements all of id's protocols, directly or |
| 4462 | // through its super class and categories. |
| 4463 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 4464 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 4465 | ObjCProtocolDecl *rhsProto = *J; |
| 4466 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 4467 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 4468 | match = true; |
| 4469 | break; |
| 4470 | } |
| 4471 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4472 | // If the RHS is a qualified interface pointer "NSString<P>*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4473 | // make sure we check the class hierarchy. |
| 4474 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 4475 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4476 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4477 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4478 | // see if static class implements all of id's protocols, directly or |
| 4479 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 4480 | if (rhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4481 | match = true; |
| 4482 | break; |
| 4483 | } |
| 4484 | } |
| 4485 | } |
| 4486 | if (!match) |
| 4487 | return false; |
| 4488 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4489 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4490 | return true; |
| 4491 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4492 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4493 | const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 4494 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 4495 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4496 | if (const ObjCObjectPointerType *lhsOPT = |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4497 | lhs->getAsObjCInterfacePointerType()) { |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 4498 | // If both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4499 | for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(), |
| 4500 | E = lhsOPT->qual_end(); I != E; ++I) { |
| 4501 | ObjCProtocolDecl *lhsProto = *I; |
| 4502 | bool match = false; |
| 4503 | |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 4504 | // when comparing an id<P> on rhs with a static type on lhs, |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4505 | // see if static class implements all of id's protocols, directly or |
| 4506 | // through its super class and categories. |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 4507 | // First, lhs protocols in the qualifier list must be found, direct |
| 4508 | // or indirect in rhs's qualifier list or it is a mismatch. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4509 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 4510 | E = rhsQID->qual_end(); J != E; ++J) { |
| 4511 | ObjCProtocolDecl *rhsProto = *J; |
| 4512 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 4513 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 4514 | match = true; |
| 4515 | break; |
| 4516 | } |
| 4517 | } |
| 4518 | if (!match) |
| 4519 | return false; |
| 4520 | } |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 4521 | |
| 4522 | // Static class's protocols, or its super class or category protocols |
| 4523 | // must be found, direct or indirect in rhs's qualifier list or it is a mismatch. |
| 4524 | if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { |
| 4525 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
| 4526 | CollectInheritedProtocols(lhsID, LHSInheritedProtocols); |
| 4527 | // This is rather dubious but matches gcc's behavior. If lhs has |
| 4528 | // no type qualifier and its class has no static protocol(s) |
| 4529 | // assume that it is mismatch. |
| 4530 | if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty()) |
| 4531 | return false; |
| 4532 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 4533 | LHSInheritedProtocols.begin(), |
| 4534 | E = LHSInheritedProtocols.end(); I != E; ++I) { |
| 4535 | bool match = false; |
| 4536 | ObjCProtocolDecl *lhsProto = (*I); |
| 4537 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 4538 | E = rhsQID->qual_end(); J != E; ++J) { |
| 4539 | ObjCProtocolDecl *rhsProto = *J; |
| 4540 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 4541 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 4542 | match = true; |
| 4543 | break; |
| 4544 | } |
| 4545 | } |
| 4546 | if (!match) |
| 4547 | return false; |
| 4548 | } |
| 4549 | } |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4550 | return true; |
| 4551 | } |
| 4552 | return false; |
| 4553 | } |
| 4554 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4555 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4556 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 4557 | /// protocol qualifiers on the LHS or RHS. |
| 4558 | /// |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4559 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 4560 | const ObjCObjectPointerType *RHSOPT) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4561 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 4562 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 4563 | |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 4564 | // 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] | 4565 | if (LHS->isObjCUnqualifiedIdOrClass() || |
| 4566 | RHS->isObjCUnqualifiedIdOrClass()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4567 | return true; |
| 4568 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4569 | if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4570 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 4571 | QualType(RHSOPT,0), |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4572 | false); |
Fariborz Jahanian | a8f8dac | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 4573 | |
| 4574 | if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass()) |
| 4575 | return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0), |
| 4576 | QualType(RHSOPT,0)); |
| 4577 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4578 | // If we have 2 user-defined types, fall into that path. |
| 4579 | if (LHS->getInterface() && RHS->getInterface()) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4580 | return canAssignObjCInterfaces(LHS, RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4581 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4582 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4583 | } |
| 4584 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4585 | /// canAssignObjCInterfacesInBlockPointer - This routine is specifically written |
| 4586 | /// for providing type-safty for objective-c pointers used to pass/return |
| 4587 | /// arguments in block literals. When passed as arguments, passing 'A*' where |
| 4588 | /// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is |
| 4589 | /// not OK. For the return type, the opposite is not OK. |
| 4590 | bool ASTContext::canAssignObjCInterfacesInBlockPointer( |
| 4591 | const ObjCObjectPointerType *LHSOPT, |
| 4592 | const ObjCObjectPointerType *RHSOPT) { |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 4593 | if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4594 | return true; |
| 4595 | |
| 4596 | if (LHSOPT->isObjCBuiltinType()) { |
| 4597 | return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType(); |
| 4598 | } |
| 4599 | |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 4600 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4601 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 4602 | QualType(RHSOPT,0), |
| 4603 | false); |
| 4604 | |
| 4605 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 4606 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 4607 | if (LHS && RHS) { // We have 2 user-defined types. |
| 4608 | if (LHS != RHS) { |
| 4609 | if (LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
| 4610 | return false; |
| 4611 | if (RHS->getDecl()->isSuperClassOf(LHS->getDecl())) |
| 4612 | return true; |
| 4613 | } |
| 4614 | else |
| 4615 | return true; |
| 4616 | } |
| 4617 | return false; |
| 4618 | } |
| 4619 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4620 | /// getIntersectionOfProtocols - This routine finds the intersection of set |
| 4621 | /// of protocols inherited from two distinct objective-c pointer objects. |
| 4622 | /// It is used to build composite qualifier list of the composite type of |
| 4623 | /// the conditional expression involving two objective-c pointer objects. |
| 4624 | static |
| 4625 | void getIntersectionOfProtocols(ASTContext &Context, |
| 4626 | const ObjCObjectPointerType *LHSOPT, |
| 4627 | const ObjCObjectPointerType *RHSOPT, |
| 4628 | llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) { |
| 4629 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4630 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 4631 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 4632 | assert(LHS->getInterface() && "LHS must have an interface base"); |
| 4633 | assert(RHS->getInterface() && "RHS must have an interface base"); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4634 | |
| 4635 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet; |
| 4636 | unsigned LHSNumProtocols = LHS->getNumProtocols(); |
| 4637 | if (LHSNumProtocols > 0) |
| 4638 | InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end()); |
| 4639 | else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 4640 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4641 | Context.CollectInheritedProtocols(LHS->getInterface(), |
| 4642 | LHSInheritedProtocols); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4643 | InheritedProtocolSet.insert(LHSInheritedProtocols.begin(), |
| 4644 | LHSInheritedProtocols.end()); |
| 4645 | } |
| 4646 | |
| 4647 | unsigned RHSNumProtocols = RHS->getNumProtocols(); |
| 4648 | if (RHSNumProtocols > 0) { |
Dan Gohman | cb421fa | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 4649 | ObjCProtocolDecl **RHSProtocols = |
| 4650 | const_cast<ObjCProtocolDecl **>(RHS->qual_begin()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4651 | for (unsigned i = 0; i < RHSNumProtocols; ++i) |
| 4652 | if (InheritedProtocolSet.count(RHSProtocols[i])) |
| 4653 | IntersectionOfProtocols.push_back(RHSProtocols[i]); |
| 4654 | } |
| 4655 | else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 4656 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4657 | Context.CollectInheritedProtocols(RHS->getInterface(), |
| 4658 | RHSInheritedProtocols); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 4659 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 4660 | RHSInheritedProtocols.begin(), |
| 4661 | E = RHSInheritedProtocols.end(); I != E; ++I) |
| 4662 | if (InheritedProtocolSet.count((*I))) |
| 4663 | IntersectionOfProtocols.push_back((*I)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4664 | } |
| 4665 | } |
| 4666 | |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4667 | /// areCommonBaseCompatible - Returns common base class of the two classes if |
| 4668 | /// one found. Note that this is O'2 algorithm. But it will be called as the |
| 4669 | /// last type comparison in a ?-exp of ObjC pointer types before a |
| 4670 | /// warning is issued. So, its invokation is extremely rare. |
| 4671 | QualType ASTContext::areCommonBaseCompatible( |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4672 | const ObjCObjectPointerType *Lptr, |
| 4673 | const ObjCObjectPointerType *Rptr) { |
| 4674 | const ObjCObjectType *LHS = Lptr->getObjectType(); |
| 4675 | const ObjCObjectType *RHS = Rptr->getObjectType(); |
| 4676 | const ObjCInterfaceDecl* LDecl = LHS->getInterface(); |
| 4677 | const ObjCInterfaceDecl* RDecl = RHS->getInterface(); |
| 4678 | if (!LDecl || !RDecl) |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4679 | return QualType(); |
| 4680 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4681 | while ((LDecl = LDecl->getSuperClass())) { |
| 4682 | LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4683 | if (canAssignObjCInterfaces(LHS, RHS)) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4684 | llvm::SmallVector<ObjCProtocolDecl *, 8> Protocols; |
| 4685 | getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols); |
| 4686 | |
| 4687 | QualType Result = QualType(LHS, 0); |
| 4688 | if (!Protocols.empty()) |
| 4689 | Result = getObjCObjectType(Result, Protocols.data(), Protocols.size()); |
| 4690 | Result = getObjCObjectPointerType(Result); |
| 4691 | return Result; |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4692 | } |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4693 | } |
| 4694 | |
| 4695 | return QualType(); |
| 4696 | } |
| 4697 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4698 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS, |
| 4699 | const ObjCObjectType *RHS) { |
| 4700 | assert(LHS->getInterface() && "LHS is not an interface type"); |
| 4701 | assert(RHS->getInterface() && "RHS is not an interface type"); |
| 4702 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4703 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 4704 | // the LHS. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4705 | if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface())) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4706 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4707 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4708 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 4709 | // protocol qualified at all, then we are good. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4710 | if (LHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4711 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4712 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4713 | // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it |
| 4714 | // isn't a superset. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4715 | if (RHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4716 | return true; // FIXME: should return false! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4717 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4718 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 4719 | LHSPE = LHS->qual_end(); |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4720 | LHSPI != LHSPE; LHSPI++) { |
| 4721 | bool RHSImplementsProtocol = false; |
| 4722 | |
| 4723 | // If the RHS doesn't implement the protocol on the left, the types |
| 4724 | // are incompatible. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4725 | for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(), |
| 4726 | RHSPE = RHS->qual_end(); |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 4727 | RHSPI != RHSPE; RHSPI++) { |
| 4728 | if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) { |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4729 | RHSImplementsProtocol = true; |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 4730 | break; |
| 4731 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4732 | } |
| 4733 | // FIXME: For better diagnostics, consider passing back the protocol name. |
| 4734 | if (!RHSImplementsProtocol) |
| 4735 | return false; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4736 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4737 | // The RHS implements all protocols listed on the LHS. |
| 4738 | return true; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4739 | } |
| 4740 | |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4741 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 4742 | // get the "pointed to" types |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4743 | const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
| 4744 | const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4745 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4746 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4747 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4748 | |
| 4749 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 4750 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4751 | } |
| 4752 | |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4753 | bool ASTContext::canBindObjCObjectType(QualType To, QualType From) { |
| 4754 | return canAssignObjCInterfaces( |
| 4755 | getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(), |
| 4756 | getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>()); |
| 4757 | } |
| 4758 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4759 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4760 | /// both shall have the identically qualified version of a compatible type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4761 | /// 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] | 4762 | /// same. See 6.7.[2,3,5] for additional rules. |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4763 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS, |
| 4764 | bool CompareUnqualified) { |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 4765 | if (getLangOptions().CPlusPlus) |
| 4766 | return hasSameType(LHS, RHS); |
| 4767 | |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4768 | return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4769 | } |
| 4770 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4771 | bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) { |
| 4772 | return !mergeTypes(LHS, RHS, true).isNull(); |
| 4773 | } |
| 4774 | |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 4775 | /// mergeTransparentUnionType - if T is a transparent union type and a member |
| 4776 | /// of T is compatible with SubType, return the merged type, else return |
| 4777 | /// QualType() |
| 4778 | QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType, |
| 4779 | bool OfBlockPointer, |
| 4780 | bool Unqualified) { |
| 4781 | if (const RecordType *UT = T->getAsUnionType()) { |
| 4782 | RecordDecl *UD = UT->getDecl(); |
| 4783 | if (UD->hasAttr<TransparentUnionAttr>()) { |
| 4784 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 4785 | itend = UD->field_end(); it != itend; ++it) { |
Peter Collingbourne | f91d757 | 2010-12-02 21:00:06 +0000 | [diff] [blame] | 4786 | QualType ET = it->getType().getUnqualifiedType(); |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 4787 | QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified); |
| 4788 | if (!MT.isNull()) |
| 4789 | return MT; |
| 4790 | } |
| 4791 | } |
| 4792 | } |
| 4793 | |
| 4794 | return QualType(); |
| 4795 | } |
| 4796 | |
| 4797 | /// mergeFunctionArgumentTypes - merge two types which appear as function |
| 4798 | /// argument types |
| 4799 | QualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs, |
| 4800 | bool OfBlockPointer, |
| 4801 | bool Unqualified) { |
| 4802 | // GNU extension: two types are compatible if they appear as a function |
| 4803 | // argument, one of the types is a transparent union type and the other |
| 4804 | // type is compatible with a union member |
| 4805 | QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer, |
| 4806 | Unqualified); |
| 4807 | if (!lmerge.isNull()) |
| 4808 | return lmerge; |
| 4809 | |
| 4810 | QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer, |
| 4811 | Unqualified); |
| 4812 | if (!rmerge.isNull()) |
| 4813 | return rmerge; |
| 4814 | |
| 4815 | return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified); |
| 4816 | } |
| 4817 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4818 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs, |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4819 | bool OfBlockPointer, |
| 4820 | bool Unqualified) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4821 | const FunctionType *lbase = lhs->getAs<FunctionType>(); |
| 4822 | const FunctionType *rbase = rhs->getAs<FunctionType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4823 | const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 4824 | const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4825 | bool allLTypes = true; |
| 4826 | bool allRTypes = true; |
| 4827 | |
| 4828 | // Check return type |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4829 | QualType retType; |
| 4830 | if (OfBlockPointer) |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4831 | retType = mergeTypes(rbase->getResultType(), lbase->getResultType(), true, |
| 4832 | Unqualified); |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4833 | else |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 4834 | retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), false, |
| 4835 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4836 | if (retType.isNull()) return QualType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4837 | |
| 4838 | if (Unqualified) |
| 4839 | retType = retType.getUnqualifiedType(); |
| 4840 | |
| 4841 | CanQualType LRetType = getCanonicalType(lbase->getResultType()); |
| 4842 | CanQualType RRetType = getCanonicalType(rbase->getResultType()); |
| 4843 | if (Unqualified) { |
| 4844 | LRetType = LRetType.getUnqualifiedType(); |
| 4845 | RRetType = RRetType.getUnqualifiedType(); |
| 4846 | } |
| 4847 | |
| 4848 | if (getCanonicalType(retType) != LRetType) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4849 | allLTypes = false; |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4850 | if (getCanonicalType(retType) != RRetType) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4851 | allRTypes = false; |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 4852 | |
Daniel Dunbar | 6a15c85 | 2010-04-28 16:20:58 +0000 | [diff] [blame] | 4853 | // FIXME: double check this |
| 4854 | // FIXME: should we error if lbase->getRegParmAttr() != 0 && |
| 4855 | // rbase->getRegParmAttr() != 0 && |
| 4856 | // lbase->getRegParmAttr() != rbase->getRegParmAttr()? |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 4857 | FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo(); |
| 4858 | FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo(); |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 4859 | |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 4860 | // Compatible functions must have compatible calling conventions |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 4861 | if (!isSameCallConv(lbaseInfo.getCC(), rbaseInfo.getCC())) |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 4862 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4863 | |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 4864 | // Regparm is part of the calling convention. |
| 4865 | if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm()) |
| 4866 | return QualType(); |
| 4867 | |
| 4868 | // It's noreturn if either type is. |
| 4869 | // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'. |
| 4870 | bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn(); |
| 4871 | if (NoReturn != lbaseInfo.getNoReturn()) |
| 4872 | allLTypes = false; |
| 4873 | if (NoReturn != rbaseInfo.getNoReturn()) |
| 4874 | allRTypes = false; |
| 4875 | |
| 4876 | FunctionType::ExtInfo einfo(NoReturn, |
| 4877 | lbaseInfo.getRegParm(), |
| 4878 | lbaseInfo.getCC()); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 4879 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4880 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 4881 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 4882 | "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4883 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 4884 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 4885 | |
| 4886 | // Compatible functions must have the same number of arguments |
| 4887 | if (lproto_nargs != rproto_nargs) |
| 4888 | return QualType(); |
| 4889 | |
| 4890 | // Variadic and non-variadic functions aren't compatible |
| 4891 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 4892 | return QualType(); |
| 4893 | |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 4894 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 4895 | return QualType(); |
| 4896 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4897 | // Check argument compatibility |
| 4898 | llvm::SmallVector<QualType, 10> types; |
| 4899 | for (unsigned i = 0; i < lproto_nargs; i++) { |
| 4900 | QualType largtype = lproto->getArgType(i).getUnqualifiedType(); |
| 4901 | QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 4902 | QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype, |
| 4903 | OfBlockPointer, |
| 4904 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4905 | if (argtype.isNull()) return QualType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4906 | |
| 4907 | if (Unqualified) |
| 4908 | argtype = argtype.getUnqualifiedType(); |
| 4909 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4910 | types.push_back(argtype); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4911 | if (Unqualified) { |
| 4912 | largtype = largtype.getUnqualifiedType(); |
| 4913 | rargtype = rargtype.getUnqualifiedType(); |
| 4914 | } |
| 4915 | |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4916 | if (getCanonicalType(argtype) != getCanonicalType(largtype)) |
| 4917 | allLTypes = false; |
| 4918 | if (getCanonicalType(argtype) != getCanonicalType(rargtype)) |
| 4919 | allRTypes = false; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4920 | } |
| 4921 | if (allLTypes) return lhs; |
| 4922 | if (allRTypes) return rhs; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 4923 | |
| 4924 | FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo(); |
| 4925 | EPI.ExtInfo = einfo; |
| 4926 | return getFunctionType(retType, types.begin(), types.size(), EPI); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4927 | } |
| 4928 | |
| 4929 | if (lproto) allRTypes = false; |
| 4930 | if (rproto) allLTypes = false; |
| 4931 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4932 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4933 | if (proto) { |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 4934 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4935 | if (proto->isVariadic()) return QualType(); |
| 4936 | // Check that the types are compatible with the types that |
| 4937 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 4938 | // The only types actually affected are promotable integer |
| 4939 | // types and floats, which would be passed as a different |
| 4940 | // type depending on whether the prototype is visible. |
| 4941 | unsigned proto_nargs = proto->getNumArgs(); |
| 4942 | for (unsigned i = 0; i < proto_nargs; ++i) { |
| 4943 | QualType argTy = proto->getArgType(i); |
Douglas Gregor | b0f8eac | 2010-02-03 19:27:29 +0000 | [diff] [blame] | 4944 | |
| 4945 | // Look at the promotion type of enum types, since that is the type used |
| 4946 | // to pass enum values. |
| 4947 | if (const EnumType *Enum = argTy->getAs<EnumType>()) |
| 4948 | argTy = Enum->getDecl()->getPromotionType(); |
| 4949 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4950 | if (argTy->isPromotableIntegerType() || |
| 4951 | getCanonicalType(argTy).getUnqualifiedType() == FloatTy) |
| 4952 | return QualType(); |
| 4953 | } |
| 4954 | |
| 4955 | if (allLTypes) return lhs; |
| 4956 | if (allRTypes) return rhs; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 4957 | |
| 4958 | FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo(); |
| 4959 | EPI.ExtInfo = einfo; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4960 | return getFunctionType(retType, proto->arg_type_begin(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 4961 | proto->getNumArgs(), EPI); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4962 | } |
| 4963 | |
| 4964 | if (allLTypes) return lhs; |
| 4965 | if (allRTypes) return rhs; |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 4966 | return getFunctionNoProtoType(retType, einfo); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4967 | } |
| 4968 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4969 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4970 | bool OfBlockPointer, |
| 4971 | bool Unqualified) { |
Bill Wendling | 43d6975 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 4972 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 4973 | // type is adjusted to "T" prior to any further analysis, the expression |
| 4974 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4975 | // expression is an lvalue unless the reference is an rvalue reference and |
| 4976 | // the expression is a function call (possibly inside parentheses). |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 4977 | assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?"); |
| 4978 | assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?"); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4979 | |
| 4980 | if (Unqualified) { |
| 4981 | LHS = LHS.getUnqualifiedType(); |
| 4982 | RHS = RHS.getUnqualifiedType(); |
| 4983 | } |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 4984 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4985 | QualType LHSCan = getCanonicalType(LHS), |
| 4986 | RHSCan = getCanonicalType(RHS); |
| 4987 | |
| 4988 | // If two types are identical, they are compatible. |
| 4989 | if (LHSCan == RHSCan) |
| 4990 | return LHS; |
| 4991 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4992 | // If the qualifiers are different, the types aren't compatible... mostly. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4993 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 4994 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4995 | if (LQuals != RQuals) { |
| 4996 | // If any of these qualifiers are different, we have a type |
| 4997 | // mismatch. |
| 4998 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 4999 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 5000 | return QualType(); |
| 5001 | |
| 5002 | // Exactly one GC qualifier difference is allowed: __strong is |
| 5003 | // okay if the other type has no GC qualifier but is an Objective |
| 5004 | // C object pointer (i.e. implicitly strong by default). We fix |
| 5005 | // this by pretending that the unqualified type was actually |
| 5006 | // qualified __strong. |
| 5007 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 5008 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 5009 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 5010 | |
| 5011 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 5012 | return QualType(); |
| 5013 | |
| 5014 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
| 5015 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); |
| 5016 | } |
| 5017 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
| 5018 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); |
| 5019 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5020 | return QualType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5021 | } |
| 5022 | |
| 5023 | // Okay, qualifiers are equal. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5024 | |
Eli Friedman | 852d63b | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 5025 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 5026 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5027 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5028 | // We want to consider the two function types to be the same for these |
| 5029 | // comparisons, just force one to the other. |
| 5030 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 5031 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 4c721d3 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 5032 | |
| 5033 | // Same as above for arrays |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 5034 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 5035 | LHSClass = Type::ConstantArray; |
| 5036 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 5037 | RHSClass = Type::ConstantArray; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5038 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5039 | // ObjCInterfaces are just specialized ObjCObjects. |
| 5040 | if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject; |
| 5041 | if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject; |
| 5042 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 5043 | // Canonicalize ExtVector -> Vector. |
| 5044 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 5045 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5046 | |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 5047 | // If the canonical type classes don't match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5048 | if (LHSClass != RHSClass) { |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5049 | // 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] | 5050 | // a signed integer type, or an unsigned integer type. |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 5051 | // Compatibility is based on the underlying type, not the promotion |
| 5052 | // type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5053 | if (const EnumType* ETy = LHS->getAs<EnumType>()) { |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5054 | if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType()) |
| 5055 | return RHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 5056 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5057 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5058 | if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType()) |
| 5059 | return LHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 5060 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5061 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5062 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5063 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5064 | |
Steve Naroff | 4a74678 | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 5065 | // The canonical type classes match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5066 | switch (LHSClass) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5067 | #define TYPE(Class, Base) |
| 5068 | #define ABSTRACT_TYPE(Class, Base) |
John McCall | ad5e738 | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 5069 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5070 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 5071 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 5072 | #include "clang/AST/TypeNodes.def" |
| 5073 | assert(false && "Non-canonical and dependent types shouldn't get here"); |
| 5074 | return QualType(); |
| 5075 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 5076 | case Type::LValueReference: |
| 5077 | case Type::RValueReference: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5078 | case Type::MemberPointer: |
| 5079 | assert(false && "C++ should never be in mergeTypes"); |
| 5080 | return QualType(); |
| 5081 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5082 | case Type::ObjCInterface: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5083 | case Type::IncompleteArray: |
| 5084 | case Type::VariableArray: |
| 5085 | case Type::FunctionProto: |
| 5086 | case Type::ExtVector: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5087 | assert(false && "Types are eliminated above"); |
| 5088 | return QualType(); |
| 5089 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5090 | case Type::Pointer: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5091 | { |
| 5092 | // Merge two pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5093 | QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); |
| 5094 | QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5095 | if (Unqualified) { |
| 5096 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 5097 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 5098 | } |
| 5099 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false, |
| 5100 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5101 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 5102 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5103 | return LHS; |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 5104 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5105 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5106 | return getPointerType(ResultType); |
| 5107 | } |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 5108 | case Type::BlockPointer: |
| 5109 | { |
| 5110 | // Merge two block pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5111 | QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); |
| 5112 | QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5113 | if (Unqualified) { |
| 5114 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 5115 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 5116 | } |
| 5117 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer, |
| 5118 | Unqualified); |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 5119 | if (ResultType.isNull()) return QualType(); |
| 5120 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 5121 | return LHS; |
| 5122 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 5123 | return RHS; |
| 5124 | return getBlockPointerType(ResultType); |
| 5125 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5126 | case Type::ConstantArray: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5127 | { |
| 5128 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 5129 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 5130 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 5131 | return QualType(); |
| 5132 | |
| 5133 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 5134 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5135 | if (Unqualified) { |
| 5136 | LHSElem = LHSElem.getUnqualifiedType(); |
| 5137 | RHSElem = RHSElem.getUnqualifiedType(); |
| 5138 | } |
| 5139 | |
| 5140 | QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5141 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5142 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 5143 | return LHS; |
| 5144 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 5145 | return RHS; |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 5146 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 5147 | ArrayType::ArraySizeModifier(), 0); |
| 5148 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 5149 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5150 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 5151 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5152 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 5153 | return LHS; |
| 5154 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 5155 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5156 | if (LVAT) { |
| 5157 | // FIXME: This isn't correct! But tricky to implement because |
| 5158 | // the array's size has to be the size of LHS, but the type |
| 5159 | // has to be different. |
| 5160 | return LHS; |
| 5161 | } |
| 5162 | if (RVAT) { |
| 5163 | // FIXME: This isn't correct! But tricky to implement because |
| 5164 | // the array's size has to be the size of RHS, but the type |
| 5165 | // has to be different. |
| 5166 | return RHS; |
| 5167 | } |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 5168 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 5169 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 5170 | return getIncompleteArrayType(ResultType, |
| 5171 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5172 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5173 | case Type::FunctionNoProto: |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5174 | return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5175 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5176 | case Type::Enum: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5177 | return QualType(); |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5178 | case Type::Builtin: |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 5179 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5180 | return QualType(); |
Daniel Dunbar | 64cfdb7 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 5181 | case Type::Complex: |
| 5182 | // Distinct complex types are incompatible. |
| 5183 | return QualType(); |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 5184 | case Type::Vector: |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 5185 | // FIXME: The merged type should be an ExtVector! |
John McCall | 1c471f3 | 2010-03-12 23:14:13 +0000 | [diff] [blame] | 5186 | if (areCompatVectorTypes(LHSCan->getAs<VectorType>(), |
| 5187 | RHSCan->getAs<VectorType>())) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5188 | return LHS; |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5189 | return QualType(); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5190 | case Type::ObjCObject: { |
| 5191 | // Check if the types are assignment compatible. |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 5192 | // FIXME: This should be type compatibility, e.g. whether |
| 5193 | // "LHS x; RHS x;" at global scope is legal. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5194 | const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>(); |
| 5195 | const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>(); |
| 5196 | if (canAssignObjCInterfaces(LHSIface, RHSIface)) |
Steve Naroff | 5fd659d | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 5197 | return LHS; |
| 5198 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5199 | return QualType(); |
Cedric Venet | 61490e9 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 5200 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5201 | case Type::ObjCObjectPointer: { |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5202 | if (OfBlockPointer) { |
| 5203 | if (canAssignObjCInterfacesInBlockPointer( |
| 5204 | LHS->getAs<ObjCObjectPointerType>(), |
| 5205 | RHS->getAs<ObjCObjectPointerType>())) |
| 5206 | return LHS; |
| 5207 | return QualType(); |
| 5208 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5209 | if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(), |
| 5210 | RHS->getAs<ObjCObjectPointerType>())) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5211 | return LHS; |
| 5212 | |
Steve Naroff | bc76dd0 | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 5213 | return QualType(); |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5214 | } |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5215 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5216 | |
| 5217 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5218 | } |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 5219 | |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 5220 | /// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and |
| 5221 | /// 'RHS' attributes and returns the merged version; including for function |
| 5222 | /// return types. |
| 5223 | QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) { |
| 5224 | QualType LHSCan = getCanonicalType(LHS), |
| 5225 | RHSCan = getCanonicalType(RHS); |
| 5226 | // If two types are identical, they are compatible. |
| 5227 | if (LHSCan == RHSCan) |
| 5228 | return LHS; |
| 5229 | if (RHSCan->isFunctionType()) { |
| 5230 | if (!LHSCan->isFunctionType()) |
| 5231 | return QualType(); |
| 5232 | QualType OldReturnType = |
| 5233 | cast<FunctionType>(RHSCan.getTypePtr())->getResultType(); |
| 5234 | QualType NewReturnType = |
| 5235 | cast<FunctionType>(LHSCan.getTypePtr())->getResultType(); |
| 5236 | QualType ResReturnType = |
| 5237 | mergeObjCGCQualifiers(NewReturnType, OldReturnType); |
| 5238 | if (ResReturnType.isNull()) |
| 5239 | return QualType(); |
| 5240 | if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) { |
| 5241 | // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo(); |
| 5242 | // In either case, use OldReturnType to build the new function type. |
| 5243 | const FunctionType *F = LHS->getAs<FunctionType>(); |
| 5244 | if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5245 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 5246 | EPI.ExtInfo = getFunctionExtInfo(LHS); |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 5247 | QualType ResultType |
| 5248 | = getFunctionType(OldReturnType, FPT->arg_type_begin(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5249 | FPT->getNumArgs(), EPI); |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 5250 | return ResultType; |
| 5251 | } |
| 5252 | } |
| 5253 | return QualType(); |
| 5254 | } |
| 5255 | |
| 5256 | // If the qualifiers are different, the types can still be merged. |
| 5257 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 5258 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
| 5259 | if (LQuals != RQuals) { |
| 5260 | // If any of these qualifiers are different, we have a type mismatch. |
| 5261 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 5262 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 5263 | return QualType(); |
| 5264 | |
| 5265 | // Exactly one GC qualifier difference is allowed: __strong is |
| 5266 | // okay if the other type has no GC qualifier but is an Objective |
| 5267 | // C object pointer (i.e. implicitly strong by default). We fix |
| 5268 | // this by pretending that the unqualified type was actually |
| 5269 | // qualified __strong. |
| 5270 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 5271 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 5272 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 5273 | |
| 5274 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 5275 | return QualType(); |
| 5276 | |
| 5277 | if (GC_L == Qualifiers::Strong) |
| 5278 | return LHS; |
| 5279 | if (GC_R == Qualifiers::Strong) |
| 5280 | return RHS; |
| 5281 | return QualType(); |
| 5282 | } |
| 5283 | |
| 5284 | if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) { |
| 5285 | QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5286 | QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5287 | QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT); |
| 5288 | if (ResQT == LHSBaseQT) |
| 5289 | return LHS; |
| 5290 | if (ResQT == RHSBaseQT) |
| 5291 | return RHS; |
| 5292 | } |
| 5293 | return QualType(); |
| 5294 | } |
| 5295 | |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 5296 | //===----------------------------------------------------------------------===// |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5297 | // Integer Predicates |
| 5298 | //===----------------------------------------------------------------------===// |
Chris Lattner | 88054de | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 5299 | |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5300 | unsigned ASTContext::getIntWidth(QualType T) { |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 5301 | if (EnumType *ET = dyn_cast<EnumType>(T)) |
Eli Friedman | 29a7f33 | 2009-12-10 22:29:29 +0000 | [diff] [blame] | 5302 | T = ET->getDecl()->getIntegerType(); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 5303 | if (T->isBooleanType()) |
| 5304 | return 1; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 5305 | // For builtin types, just use the standard type sizing method |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5306 | return (unsigned)getTypeSize(T); |
| 5307 | } |
| 5308 | |
| 5309 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) { |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5310 | assert(T->hasSignedIntegerRepresentation() && "Unexpected type"); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5311 | |
| 5312 | // Turn <4 x signed int> -> <4 x unsigned int> |
| 5313 | if (const VectorType *VTy = T->getAs<VectorType>()) |
| 5314 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 5315 | VTy->getNumElements(), VTy->getVectorKind()); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5316 | |
| 5317 | // For enums, we return the unsigned version of the base type. |
| 5318 | if (const EnumType *ETy = T->getAs<EnumType>()) |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5319 | T = ETy->getDecl()->getIntegerType(); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5320 | |
| 5321 | const BuiltinType *BTy = T->getAs<BuiltinType>(); |
| 5322 | assert(BTy && "Unexpected signed integer type"); |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5323 | switch (BTy->getKind()) { |
| 5324 | case BuiltinType::Char_S: |
| 5325 | case BuiltinType::SChar: |
| 5326 | return UnsignedCharTy; |
| 5327 | case BuiltinType::Short: |
| 5328 | return UnsignedShortTy; |
| 5329 | case BuiltinType::Int: |
| 5330 | return UnsignedIntTy; |
| 5331 | case BuiltinType::Long: |
| 5332 | return UnsignedLongTy; |
| 5333 | case BuiltinType::LongLong: |
| 5334 | return UnsignedLongLongTy; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 5335 | case BuiltinType::Int128: |
| 5336 | return UnsignedInt128Ty; |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5337 | default: |
| 5338 | assert(0 && "Unexpected signed integer type"); |
| 5339 | return QualType(); |
| 5340 | } |
| 5341 | } |
| 5342 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 5343 | ExternalASTSource::~ExternalASTSource() { } |
| 5344 | |
| 5345 | void ExternalASTSource::PrintStats() { } |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5346 | |
Argyrios Kyrtzidis | 7b90340 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 5347 | ASTMutationListener::~ASTMutationListener() { } |
| 5348 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5349 | |
| 5350 | //===----------------------------------------------------------------------===// |
| 5351 | // Builtin Type Computation |
| 5352 | //===----------------------------------------------------------------------===// |
| 5353 | |
| 5354 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5355 | /// pointer over the consumed characters. This returns the resultant type. If |
| 5356 | /// AllowTypeModifiers is false then modifier like * are not parsed, just basic |
| 5357 | /// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of |
| 5358 | /// a vector of "i*". |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5359 | /// |
| 5360 | /// RequiresICE is filled in on return to indicate whether the value is required |
| 5361 | /// to be an Integer Constant Expression. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5362 | static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context, |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5363 | ASTContext::GetBuiltinTypeError &Error, |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5364 | bool &RequiresICE, |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5365 | bool AllowTypeModifiers) { |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5366 | // Modifiers. |
| 5367 | int HowLong = 0; |
| 5368 | bool Signed = false, Unsigned = false; |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5369 | RequiresICE = false; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 5370 | |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5371 | // Read the prefixed modifiers first. |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5372 | bool Done = false; |
| 5373 | while (!Done) { |
| 5374 | switch (*Str++) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5375 | default: Done = true; --Str; break; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 5376 | case 'I': |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5377 | RequiresICE = true; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 5378 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5379 | case 'S': |
| 5380 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 5381 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 5382 | Signed = true; |
| 5383 | break; |
| 5384 | case 'U': |
| 5385 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
| 5386 | assert(!Unsigned && "Can't use 'S' modifier multiple times!"); |
| 5387 | Unsigned = true; |
| 5388 | break; |
| 5389 | case 'L': |
| 5390 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 5391 | ++HowLong; |
| 5392 | break; |
| 5393 | } |
| 5394 | } |
| 5395 | |
| 5396 | QualType Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5397 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5398 | // Read the base type. |
| 5399 | switch (*Str++) { |
| 5400 | default: assert(0 && "Unknown builtin type letter!"); |
| 5401 | case 'v': |
| 5402 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 5403 | "Bad modifiers used with 'v'!"); |
| 5404 | Type = Context.VoidTy; |
| 5405 | break; |
| 5406 | case 'f': |
| 5407 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 5408 | "Bad modifiers used with 'f'!"); |
| 5409 | Type = Context.FloatTy; |
| 5410 | break; |
| 5411 | case 'd': |
| 5412 | assert(HowLong < 2 && !Signed && !Unsigned && |
| 5413 | "Bad modifiers used with 'd'!"); |
| 5414 | if (HowLong) |
| 5415 | Type = Context.LongDoubleTy; |
| 5416 | else |
| 5417 | Type = Context.DoubleTy; |
| 5418 | break; |
| 5419 | case 's': |
| 5420 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 5421 | if (Unsigned) |
| 5422 | Type = Context.UnsignedShortTy; |
| 5423 | else |
| 5424 | Type = Context.ShortTy; |
| 5425 | break; |
| 5426 | case 'i': |
| 5427 | if (HowLong == 3) |
| 5428 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 5429 | else if (HowLong == 2) |
| 5430 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 5431 | else if (HowLong == 1) |
| 5432 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 5433 | else |
| 5434 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 5435 | break; |
| 5436 | case 'c': |
| 5437 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 5438 | if (Signed) |
| 5439 | Type = Context.SignedCharTy; |
| 5440 | else if (Unsigned) |
| 5441 | Type = Context.UnsignedCharTy; |
| 5442 | else |
| 5443 | Type = Context.CharTy; |
| 5444 | break; |
| 5445 | case 'b': // boolean |
| 5446 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 5447 | Type = Context.BoolTy; |
| 5448 | break; |
| 5449 | case 'z': // size_t. |
| 5450 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 5451 | Type = Context.getSizeType(); |
| 5452 | break; |
| 5453 | case 'F': |
| 5454 | Type = Context.getCFConstantStringType(); |
| 5455 | break; |
Fariborz Jahanian | ba8bda0 | 2010-11-09 21:38:20 +0000 | [diff] [blame] | 5456 | case 'G': |
| 5457 | Type = Context.getObjCIdType(); |
| 5458 | break; |
| 5459 | case 'H': |
| 5460 | Type = Context.getObjCSelType(); |
| 5461 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5462 | case 'a': |
| 5463 | Type = Context.getBuiltinVaListType(); |
| 5464 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 5465 | break; |
| 5466 | case 'A': |
| 5467 | // This is a "reference" to a va_list; however, what exactly |
| 5468 | // this means depends on how va_list is defined. There are two |
| 5469 | // different kinds of va_list: ones passed by value, and ones |
| 5470 | // passed by reference. An example of a by-value va_list is |
| 5471 | // x86, where va_list is a char*. An example of by-ref va_list |
| 5472 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 5473 | // we want this argument to be a char*&; for x86-64, we want |
| 5474 | // it to be a __va_list_tag*. |
| 5475 | Type = Context.getBuiltinVaListType(); |
| 5476 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5477 | if (Type->isArrayType()) |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5478 | Type = Context.getArrayDecayedType(Type); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5479 | else |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5480 | Type = Context.getLValueReferenceType(Type); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5481 | break; |
| 5482 | case 'V': { |
| 5483 | char *End; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5484 | unsigned NumElements = strtoul(Str, &End, 10); |
| 5485 | assert(End != Str && "Missing vector size"); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5486 | Str = End; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5487 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5488 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, |
| 5489 | RequiresICE, false); |
| 5490 | assert(!RequiresICE && "Can't require vector ICE"); |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5491 | |
| 5492 | // TODO: No way to make AltiVec vectors in builtins yet. |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 5493 | Type = Context.getVectorType(ElementType, NumElements, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 5494 | VectorType::GenericVector); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5495 | break; |
| 5496 | } |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 5497 | case 'X': { |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5498 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
| 5499 | false); |
| 5500 | assert(!RequiresICE && "Can't require complex ICE"); |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 5501 | Type = Context.getComplexType(ElementType); |
| 5502 | break; |
| 5503 | } |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 5504 | case 'P': |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 5505 | Type = Context.getFILEType(); |
| 5506 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 5507 | Error = ASTContext::GE_Missing_stdio; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5508 | return QualType(); |
| 5509 | } |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 5510 | break; |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 5511 | case 'J': |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 5512 | if (Signed) |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 5513 | Type = Context.getsigjmp_bufType(); |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 5514 | else |
| 5515 | Type = Context.getjmp_bufType(); |
| 5516 | |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 5517 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 5518 | Error = ASTContext::GE_Missing_setjmp; |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 5519 | return QualType(); |
| 5520 | } |
| 5521 | break; |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 5522 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5523 | |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5524 | // If there are modifiers and if we're allowed to parse them, go for it. |
| 5525 | Done = !AllowTypeModifiers; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5526 | while (!Done) { |
John McCall | 187ab37 | 2010-03-12 04:21:28 +0000 | [diff] [blame] | 5527 | switch (char c = *Str++) { |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5528 | default: Done = true; --Str; break; |
| 5529 | case '*': |
| 5530 | case '&': { |
| 5531 | // Both pointers and references can have their pointee types |
| 5532 | // qualified with an address space. |
| 5533 | char *End; |
| 5534 | unsigned AddrSpace = strtoul(Str, &End, 10); |
| 5535 | if (End != Str && AddrSpace != 0) { |
| 5536 | Type = Context.getAddrSpaceQualType(Type, AddrSpace); |
| 5537 | Str = End; |
| 5538 | } |
| 5539 | if (c == '*') |
| 5540 | Type = Context.getPointerType(Type); |
| 5541 | else |
| 5542 | Type = Context.getLValueReferenceType(Type); |
| 5543 | break; |
| 5544 | } |
| 5545 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 5546 | case 'C': |
| 5547 | Type = Type.withConst(); |
| 5548 | break; |
| 5549 | case 'D': |
| 5550 | Type = Context.getVolatileType(Type); |
| 5551 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5552 | } |
| 5553 | } |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 5554 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5555 | assert((!RequiresICE || Type->isIntegralOrEnumerationType()) && |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 5556 | "Integer constant 'I' type must be an integer"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5557 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5558 | return Type; |
| 5559 | } |
| 5560 | |
| 5561 | /// GetBuiltinType - Return the type for the specified builtin. |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5562 | QualType ASTContext::GetBuiltinType(unsigned Id, |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5563 | GetBuiltinTypeError &Error, |
| 5564 | unsigned *IntegerConstantArgs) { |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5565 | const char *TypeStr = BuiltinInfo.GetTypeString(Id); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5566 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5567 | llvm::SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5568 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5569 | bool RequiresICE = false; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5570 | Error = GE_None; |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5571 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error, |
| 5572 | RequiresICE, true); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5573 | if (Error != GE_None) |
| 5574 | return QualType(); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5575 | |
| 5576 | assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE"); |
| 5577 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5578 | while (TypeStr[0] && TypeStr[0] != '.') { |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5579 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5580 | if (Error != GE_None) |
| 5581 | return QualType(); |
| 5582 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5583 | // If this argument is required to be an IntegerConstantExpression and the |
| 5584 | // caller cares, fill in the bitmask we return. |
| 5585 | if (RequiresICE && IntegerConstantArgs) |
| 5586 | *IntegerConstantArgs |= 1 << ArgTypes.size(); |
| 5587 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5588 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 5589 | if (Ty->isArrayType()) |
| 5590 | Ty = getArrayDecayedType(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5591 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5592 | ArgTypes.push_back(Ty); |
| 5593 | } |
| 5594 | |
| 5595 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 5596 | "'.' should only occur at end of builtin type list!"); |
| 5597 | |
| 5598 | // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);". |
| 5599 | if (ArgTypes.size() == 0 && TypeStr[0] == '.') |
| 5600 | return getFunctionNoProtoType(ResType); |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 5601 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5602 | FunctionProtoType::ExtProtoInfo EPI; |
| 5603 | EPI.Variadic = (TypeStr[0] == '.'); |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 5604 | // FIXME: Should we create noreturn types? |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5605 | |
| 5606 | return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), EPI); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5607 | } |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5608 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5609 | GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) { |
| 5610 | GVALinkage External = GVA_StrongExternal; |
| 5611 | |
| 5612 | Linkage L = FD->getLinkage(); |
| 5613 | if (L == ExternalLinkage && getLangOptions().CPlusPlus && |
| 5614 | FD->getType()->getLinkage() == UniqueExternalLinkage) |
| 5615 | L = UniqueExternalLinkage; |
| 5616 | |
| 5617 | switch (L) { |
| 5618 | case NoLinkage: |
| 5619 | case InternalLinkage: |
| 5620 | case UniqueExternalLinkage: |
| 5621 | return GVA_Internal; |
| 5622 | |
| 5623 | case ExternalLinkage: |
| 5624 | switch (FD->getTemplateSpecializationKind()) { |
| 5625 | case TSK_Undeclared: |
| 5626 | case TSK_ExplicitSpecialization: |
| 5627 | External = GVA_StrongExternal; |
| 5628 | break; |
| 5629 | |
| 5630 | case TSK_ExplicitInstantiationDefinition: |
| 5631 | return GVA_ExplicitTemplateInstantiation; |
| 5632 | |
| 5633 | case TSK_ExplicitInstantiationDeclaration: |
| 5634 | case TSK_ImplicitInstantiation: |
| 5635 | External = GVA_TemplateInstantiation; |
| 5636 | break; |
| 5637 | } |
| 5638 | } |
| 5639 | |
| 5640 | if (!FD->isInlined()) |
| 5641 | return External; |
| 5642 | |
| 5643 | if (!getLangOptions().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) { |
| 5644 | // GNU or C99 inline semantics. Determine whether this symbol should be |
| 5645 | // externally visible. |
| 5646 | if (FD->isInlineDefinitionExternallyVisible()) |
| 5647 | return External; |
| 5648 | |
| 5649 | // C99 inline semantics, where the symbol is not externally visible. |
| 5650 | return GVA_C99Inline; |
| 5651 | } |
| 5652 | |
| 5653 | // C++0x [temp.explicit]p9: |
| 5654 | // [ Note: The intent is that an inline function that is the subject of |
| 5655 | // an explicit instantiation declaration will still be implicitly |
| 5656 | // instantiated when used so that the body can be considered for |
| 5657 | // inlining, but that no out-of-line copy of the inline function would be |
| 5658 | // generated in the translation unit. -- end note ] |
| 5659 | if (FD->getTemplateSpecializationKind() |
| 5660 | == TSK_ExplicitInstantiationDeclaration) |
| 5661 | return GVA_C99Inline; |
| 5662 | |
| 5663 | return GVA_CXXInline; |
| 5664 | } |
| 5665 | |
| 5666 | GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) { |
| 5667 | // If this is a static data member, compute the kind of template |
| 5668 | // specialization. Otherwise, this variable is not part of a |
| 5669 | // template. |
| 5670 | TemplateSpecializationKind TSK = TSK_Undeclared; |
| 5671 | if (VD->isStaticDataMember()) |
| 5672 | TSK = VD->getTemplateSpecializationKind(); |
| 5673 | |
| 5674 | Linkage L = VD->getLinkage(); |
| 5675 | if (L == ExternalLinkage && getLangOptions().CPlusPlus && |
| 5676 | VD->getType()->getLinkage() == UniqueExternalLinkage) |
| 5677 | L = UniqueExternalLinkage; |
| 5678 | |
| 5679 | switch (L) { |
| 5680 | case NoLinkage: |
| 5681 | case InternalLinkage: |
| 5682 | case UniqueExternalLinkage: |
| 5683 | return GVA_Internal; |
| 5684 | |
| 5685 | case ExternalLinkage: |
| 5686 | switch (TSK) { |
| 5687 | case TSK_Undeclared: |
| 5688 | case TSK_ExplicitSpecialization: |
| 5689 | return GVA_StrongExternal; |
| 5690 | |
| 5691 | case TSK_ExplicitInstantiationDeclaration: |
| 5692 | llvm_unreachable("Variable should not be instantiated"); |
| 5693 | // Fall through to treat this like any other instantiation. |
| 5694 | |
| 5695 | case TSK_ExplicitInstantiationDefinition: |
| 5696 | return GVA_ExplicitTemplateInstantiation; |
| 5697 | |
| 5698 | case TSK_ImplicitInstantiation: |
| 5699 | return GVA_TemplateInstantiation; |
| 5700 | } |
| 5701 | } |
| 5702 | |
| 5703 | return GVA_StrongExternal; |
| 5704 | } |
| 5705 | |
Argyrios Kyrtzidis | 4ac7c0b | 2010-07-29 20:08:05 +0000 | [diff] [blame] | 5706 | bool ASTContext::DeclMustBeEmitted(const Decl *D) { |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5707 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 5708 | if (!VD->isFileVarDecl()) |
| 5709 | return false; |
| 5710 | } else if (!isa<FunctionDecl>(D)) |
| 5711 | return false; |
| 5712 | |
Argyrios Kyrtzidis | ab411c8 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 5713 | // Weak references don't produce any output by themselves. |
| 5714 | if (D->hasAttr<WeakRefAttr>()) |
| 5715 | return false; |
| 5716 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5717 | // Aliases and used decls are required. |
| 5718 | if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>()) |
| 5719 | return true; |
| 5720 | |
| 5721 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 5722 | // Forward declarations aren't required. |
| 5723 | if (!FD->isThisDeclarationADefinition()) |
| 5724 | return false; |
| 5725 | |
| 5726 | // Constructors and destructors are required. |
| 5727 | if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>()) |
| 5728 | return true; |
| 5729 | |
| 5730 | // The key function for a class is required. |
| 5731 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 5732 | const CXXRecordDecl *RD = MD->getParent(); |
| 5733 | if (MD->isOutOfLine() && RD->isDynamicClass()) { |
| 5734 | const CXXMethodDecl *KeyFunc = getKeyFunction(RD); |
| 5735 | if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl()) |
| 5736 | return true; |
| 5737 | } |
| 5738 | } |
| 5739 | |
| 5740 | GVALinkage Linkage = GetGVALinkageForFunction(FD); |
| 5741 | |
| 5742 | // static, static inline, always_inline, and extern inline functions can |
| 5743 | // always be deferred. Normal inline functions can be deferred in C99/C++. |
| 5744 | // Implicit template instantiations can also be deferred in C++. |
| 5745 | if (Linkage == GVA_Internal || Linkage == GVA_C99Inline || |
| 5746 | Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) |
| 5747 | return false; |
| 5748 | return true; |
| 5749 | } |
| 5750 | |
| 5751 | const VarDecl *VD = cast<VarDecl>(D); |
| 5752 | assert(VD->isFileVarDecl() && "Expected file scoped var"); |
| 5753 | |
Argyrios Kyrtzidis | ab411c8 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 5754 | if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly) |
| 5755 | return false; |
| 5756 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5757 | // Structs that have non-trivial constructors or destructors are required. |
| 5758 | |
| 5759 | // FIXME: Handle references. |
| 5760 | if (const RecordType *RT = VD->getType()->getAs<RecordType>()) { |
| 5761 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5762 | if (RD->hasDefinition() && |
| 5763 | (!RD->hasTrivialConstructor() || !RD->hasTrivialDestructor())) |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5764 | return true; |
| 5765 | } |
| 5766 | } |
| 5767 | |
| 5768 | GVALinkage L = GetGVALinkageForVariable(VD); |
| 5769 | if (L == GVA_Internal || L == GVA_TemplateInstantiation) { |
| 5770 | if (!(VD->getInit() && VD->getInit()->HasSideEffects(*this))) |
| 5771 | return false; |
| 5772 | } |
| 5773 | |
| 5774 | return true; |
| 5775 | } |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 5776 | |
Charles Davis | ee743f9 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 5777 | CallingConv ASTContext::getDefaultMethodCallConv() { |
| 5778 | // Pass through to the C++ ABI object |
| 5779 | return ABI->getDefaultMethodCallConv(); |
| 5780 | } |
| 5781 | |
Anders Carlsson | dae0cb5 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 5782 | bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) { |
| 5783 | // Pass through to the C++ ABI object |
| 5784 | return ABI->isNearlyEmpty(RD); |
| 5785 | } |
| 5786 | |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 5787 | CXXABI::~CXXABI() {} |