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 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 814 | case Type::Typedef: { |
| 815 | const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl(); |
Douglas Gregor | df1367a | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 816 | std::pair<uint64_t, unsigned> Info |
| 817 | = getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); |
| 818 | Align = std::max(Typedef->getMaxAlignment(), Info.second); |
| 819 | Width = Info.first; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 820 | break; |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 821 | } |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 822 | |
| 823 | case Type::TypeOfExpr: |
| 824 | return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType() |
| 825 | .getTypePtr()); |
| 826 | |
| 827 | case Type::TypeOf: |
| 828 | return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr()); |
| 829 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 830 | case Type::Decltype: |
| 831 | return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType() |
| 832 | .getTypePtr()); |
| 833 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 834 | case Type::Elaborated: |
| 835 | return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 836 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 837 | case Type::TemplateSpecialization: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 838 | assert(getCanonicalType(T) != T && |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 839 | "Cannot request the size of a dependent type"); |
| 840 | // FIXME: this is likely to be wrong once we support template |
| 841 | // aliases, since a template alias could refer to a typedef that |
| 842 | // has an __aligned__ attribute on it. |
| 843 | return getTypeInfo(getCanonicalType(T)); |
| 844 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 845 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 846 | assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2"); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 847 | return std::make_pair(Width, Align); |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 850 | /// getTypeSizeInChars - Return the size of the specified type, in characters. |
| 851 | /// This method does not work on incomplete types. |
| 852 | CharUnits ASTContext::getTypeSizeInChars(QualType T) { |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 853 | return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth()); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 854 | } |
| 855 | CharUnits ASTContext::getTypeSizeInChars(const Type *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 | |
Ken Dyck | 16e20cc | 2010-01-26 17:25:18 +0000 | [diff] [blame] | 859 | /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 860 | /// characters. This method does not work on incomplete types. |
| 861 | CharUnits ASTContext::getTypeAlignInChars(QualType T) { |
| 862 | return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth()); |
| 863 | } |
| 864 | CharUnits ASTContext::getTypeAlignInChars(const Type *T) { |
| 865 | return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth()); |
| 866 | } |
| 867 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 868 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 869 | /// type for the current target in bits. This can be different than the ABI |
| 870 | /// alignment in cases where it is beneficial for performance to overalign |
| 871 | /// a data type. |
| 872 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) { |
| 873 | unsigned ABIAlign = getTypeAlign(T); |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 874 | |
| 875 | // Double and long long should be naturally aligned if possible. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 876 | if (const ComplexType* CT = T->getAs<ComplexType>()) |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 877 | T = CT->getElementType().getTypePtr(); |
| 878 | if (T->isSpecificBuiltinType(BuiltinType::Double) || |
| 879 | T->isSpecificBuiltinType(BuiltinType::LongLong)) |
| 880 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); |
| 881 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 882 | return ABIAlign; |
| 883 | } |
| 884 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 885 | /// ShallowCollectObjCIvars - |
| 886 | /// Collect all ivars, including those synthesized, in the current class. |
| 887 | /// |
| 888 | void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI, |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 889 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) { |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 890 | // FIXME. This need be removed but there are two many places which |
| 891 | // assume const-ness of ObjCInterfaceDecl |
| 892 | ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI); |
| 893 | for (ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv; |
| 894 | Iv= Iv->getNextIvar()) |
| 895 | Ivars.push_back(Iv); |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 896 | } |
| 897 | |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 898 | /// DeepCollectObjCIvars - |
| 899 | /// This routine first collects all declared, but not synthesized, ivars in |
| 900 | /// super class and then collects all ivars, including those synthesized for |
| 901 | /// current class. This routine is used for implementation of current class |
| 902 | /// when all ivars, declared and synthesized are known. |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 903 | /// |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 904 | void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 905 | bool leafClass, |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 906 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) { |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 907 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
| 908 | DeepCollectObjCIvars(SuperClass, false, Ivars); |
| 909 | if (!leafClass) { |
| 910 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 911 | E = OI->ivar_end(); I != E; ++I) |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 912 | Ivars.push_back(*I); |
| 913 | } |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 914 | else |
| 915 | ShallowCollectObjCIvars(OI, Ivars); |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 916 | } |
| 917 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 918 | /// CollectInheritedProtocols - Collect all protocols in current class and |
| 919 | /// those inherited by it. |
| 920 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 921 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 922 | if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 923 | // We can use protocol_iterator here instead of |
| 924 | // all_referenced_protocol_iterator since we are walking all categories. |
| 925 | for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(), |
| 926 | PE = OI->all_referenced_protocol_end(); P != PE; ++P) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 927 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 928 | Protocols.insert(Proto); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 929 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 930 | PE = Proto->protocol_end(); P != PE; ++P) { |
| 931 | Protocols.insert(*P); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 932 | CollectInheritedProtocols(*P, Protocols); |
| 933 | } |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 934 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 935 | |
| 936 | // Categories of this Interface. |
| 937 | for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList(); |
| 938 | CDeclChain; CDeclChain = CDeclChain->getNextClassCategory()) |
| 939 | CollectInheritedProtocols(CDeclChain, Protocols); |
| 940 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) |
| 941 | while (SD) { |
| 942 | CollectInheritedProtocols(SD, Protocols); |
| 943 | SD = SD->getSuperClass(); |
| 944 | } |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 945 | } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 946 | for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(), |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 947 | PE = OC->protocol_end(); P != PE; ++P) { |
| 948 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 949 | Protocols.insert(Proto); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 950 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 951 | PE = Proto->protocol_end(); P != PE; ++P) |
| 952 | CollectInheritedProtocols(*P, Protocols); |
| 953 | } |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 954 | } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 955 | for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(), |
| 956 | PE = OP->protocol_end(); P != PE; ++P) { |
| 957 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 958 | Protocols.insert(Proto); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 959 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 960 | PE = Proto->protocol_end(); P != PE; ++P) |
| 961 | CollectInheritedProtocols(*P, Protocols); |
| 962 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 963 | } |
| 964 | } |
| 965 | |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 966 | unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) { |
| 967 | unsigned count = 0; |
| 968 | // Count ivars declared in class extension. |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 969 | for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl; |
| 970 | CDecl = CDecl->getNextClassExtension()) |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 971 | count += CDecl->ivar_size(); |
| 972 | |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 973 | // Count ivar defined in this class's implementation. This |
| 974 | // includes synthesized ivars. |
| 975 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 976 | count += ImplDecl->ivar_size(); |
| 977 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 978 | return count; |
| 979 | } |
| 980 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 981 | /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. |
| 982 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
| 983 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 984 | I = ObjCImpls.find(D); |
| 985 | if (I != ObjCImpls.end()) |
| 986 | return cast<ObjCImplementationDecl>(I->second); |
| 987 | return 0; |
| 988 | } |
| 989 | /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. |
| 990 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
| 991 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 992 | I = ObjCImpls.find(D); |
| 993 | if (I != ObjCImpls.end()) |
| 994 | return cast<ObjCCategoryImplDecl>(I->second); |
| 995 | return 0; |
| 996 | } |
| 997 | |
| 998 | /// \brief Set the implementation of ObjCInterfaceDecl. |
| 999 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
| 1000 | ObjCImplementationDecl *ImplD) { |
| 1001 | assert(IFaceD && ImplD && "Passed null params"); |
| 1002 | ObjCImpls[IFaceD] = ImplD; |
| 1003 | } |
| 1004 | /// \brief Set the implementation of ObjCCategoryDecl. |
| 1005 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
| 1006 | ObjCCategoryImplDecl *ImplD) { |
| 1007 | assert(CatD && ImplD && "Passed null params"); |
| 1008 | ObjCImpls[CatD] = ImplD; |
| 1009 | } |
| 1010 | |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1011 | /// \brief Get the copy initialization expression of VarDecl,or NULL if |
| 1012 | /// none exists. |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1013 | Expr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) { |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1014 | assert(VD && "Passed null params"); |
| 1015 | assert(VD->hasAttr<BlocksAttr>() && |
| 1016 | "getBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1017 | llvm::DenseMap<const VarDecl*, Expr*>::iterator |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1018 | I = BlockVarCopyInits.find(VD); |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1019 | return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0; |
| 1020 | } |
| 1021 | |
| 1022 | /// \brief Set the copy inialization expression of a block var decl. |
| 1023 | void ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) { |
| 1024 | assert(VD && Init && "Passed null params"); |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1025 | assert(VD->hasAttr<BlocksAttr>() && |
| 1026 | "setBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1027 | BlockVarCopyInits[VD] = Init; |
| 1028 | } |
| 1029 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1030 | /// \brief Allocate an uninitialized TypeSourceInfo. |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1031 | /// |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1032 | /// The caller should initialize the memory held by TypeSourceInfo using |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1033 | /// the TypeLoc wrappers. |
| 1034 | /// |
| 1035 | /// \param T the type that will be the basis for type source info. This type |
| 1036 | /// should refer to how the declarator was written in source code, not to |
| 1037 | /// what type semantic analysis resolved the declarator to. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1038 | TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T, |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1039 | unsigned DataSize) { |
| 1040 | if (!DataSize) |
| 1041 | DataSize = TypeLoc::getFullDataSizeForType(T); |
| 1042 | else |
| 1043 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1044 | "incorrect data size provided to CreateTypeSourceInfo!"); |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1045 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1046 | TypeSourceInfo *TInfo = |
| 1047 | (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8); |
| 1048 | new (TInfo) TypeSourceInfo(T); |
| 1049 | return TInfo; |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1052 | TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, |
John McCall | a4eb74d | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 1053 | SourceLocation L) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1054 | TypeSourceInfo *DI = CreateTypeSourceInfo(T); |
John McCall | a4eb74d | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 1055 | DI->getTypeLoc().initialize(L); |
| 1056 | return DI; |
| 1057 | } |
| 1058 | |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1059 | const ASTRecordLayout & |
| 1060 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) { |
| 1061 | return getObjCLayout(D, 0); |
| 1062 | } |
| 1063 | |
| 1064 | const ASTRecordLayout & |
| 1065 | ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) { |
| 1066 | return getObjCLayout(D->getClassInterface(), D); |
| 1067 | } |
| 1068 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1069 | //===----------------------------------------------------------------------===// |
| 1070 | // Type creation/memoization methods |
| 1071 | //===----------------------------------------------------------------------===// |
| 1072 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1073 | QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) { |
| 1074 | unsigned Fast = Quals.getFastQualifiers(); |
| 1075 | Quals.removeFastQualifiers(); |
| 1076 | |
| 1077 | // Check if we've already instantiated this type. |
| 1078 | llvm::FoldingSetNodeID ID; |
| 1079 | ExtQuals::Profile(ID, TypeNode, Quals); |
| 1080 | void *InsertPos = 0; |
| 1081 | if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) { |
| 1082 | assert(EQ->getQualifiers() == Quals); |
| 1083 | QualType T = QualType(EQ, Fast); |
| 1084 | return T; |
| 1085 | } |
| 1086 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame^] | 1087 | ExtQuals *New = new (*this, TypeAlignment) ExtQuals(TypeNode, Quals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1088 | ExtQualNodes.InsertNode(New, InsertPos); |
| 1089 | QualType T = QualType(New, Fast); |
| 1090 | return T; |
| 1091 | } |
| 1092 | |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 1093 | QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1094 | QualType CanT = getCanonicalType(T); |
| 1095 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1096 | return T; |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1097 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1098 | // If we are composing extended qualifiers together, merge together |
| 1099 | // into one ExtQuals node. |
| 1100 | QualifierCollector Quals; |
| 1101 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1102 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1103 | // If this type already has an address space specified, it cannot get |
| 1104 | // another one. |
| 1105 | assert(!Quals.hasAddressSpace() && |
| 1106 | "Type cannot be in multiple addr spaces!"); |
| 1107 | Quals.addAddressSpace(AddressSpace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1108 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1109 | return getExtQualType(TypeNode, Quals); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1112 | QualType ASTContext::getObjCGCQualType(QualType T, |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1113 | Qualifiers::GC GCAttr) { |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1114 | QualType CanT = getCanonicalType(T); |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1115 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1116 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1117 | |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1118 | if (T->isPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1119 | QualType Pointee = T->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 1120 | if (Pointee->isAnyPointerType()) { |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1121 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 1122 | return getPointerType(ResultType); |
| 1123 | } |
| 1124 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1125 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1126 | // If we are composing extended qualifiers together, merge together |
| 1127 | // into one ExtQuals node. |
| 1128 | QualifierCollector Quals; |
| 1129 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1130 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1131 | // If this type already has an ObjCGC specified, it cannot get |
| 1132 | // another one. |
| 1133 | assert(!Quals.hasObjCGCAttr() && |
| 1134 | "Type cannot have multiple ObjCGCs!"); |
| 1135 | Quals.addObjCGCAttr(GCAttr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1136 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1137 | return getExtQualType(TypeNode, Quals); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1138 | } |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1139 | |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1140 | static QualType getExtFunctionType(ASTContext& Context, QualType T, |
| 1141 | const FunctionType::ExtInfo &Info) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1142 | QualType ResultType; |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1143 | if (const PointerType *Pointer = T->getAs<PointerType>()) { |
| 1144 | QualType Pointee = Pointer->getPointeeType(); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1145 | ResultType = getExtFunctionType(Context, Pointee, Info); |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1146 | if (ResultType == Pointee) |
| 1147 | return T; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1148 | |
| 1149 | ResultType = Context.getPointerType(ResultType); |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1150 | } else if (const BlockPointerType *BlockPointer |
| 1151 | = T->getAs<BlockPointerType>()) { |
| 1152 | QualType Pointee = BlockPointer->getPointeeType(); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1153 | ResultType = getExtFunctionType(Context, Pointee, Info); |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1154 | if (ResultType == Pointee) |
| 1155 | return T; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1156 | |
| 1157 | ResultType = Context.getBlockPointerType(ResultType); |
Douglas Gregor | afac01d | 2010-09-01 16:29:03 +0000 | [diff] [blame] | 1158 | } else if (const MemberPointerType *MemberPointer |
| 1159 | = T->getAs<MemberPointerType>()) { |
| 1160 | QualType Pointee = MemberPointer->getPointeeType(); |
| 1161 | ResultType = getExtFunctionType(Context, Pointee, Info); |
| 1162 | if (ResultType == Pointee) |
| 1163 | return T; |
| 1164 | |
| 1165 | ResultType = Context.getMemberPointerType(ResultType, |
| 1166 | MemberPointer->getClass()); |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1167 | } else if (const FunctionType *F = T->getAs<FunctionType>()) { |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1168 | if (F->getExtInfo() == Info) |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1169 | return T; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1170 | |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1171 | if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(F)) { |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1172 | ResultType = Context.getFunctionNoProtoType(FNPT->getResultType(), |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1173 | Info); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1174 | } else { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1175 | const FunctionProtoType *FPT = cast<FunctionProtoType>(F); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1176 | ResultType |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1177 | = Context.getFunctionType(FPT->getResultType(), FPT->arg_type_begin(), |
| 1178 | FPT->getNumArgs(), FPT->isVariadic(), |
| 1179 | FPT->getTypeQuals(), |
| 1180 | FPT->hasExceptionSpec(), |
| 1181 | FPT->hasAnyExceptionSpec(), |
| 1182 | FPT->getNumExceptions(), |
| 1183 | FPT->exception_begin(), |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1184 | Info); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1185 | } |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1186 | } else |
| 1187 | return T; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1188 | |
| 1189 | return Context.getQualifiedType(ResultType, T.getLocalQualifiers()); |
| 1190 | } |
| 1191 | |
| 1192 | QualType ASTContext::getNoReturnType(QualType T, bool AddNoReturn) { |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 1193 | FunctionType::ExtInfo Info = getFunctionExtInfo(T); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1194 | return getExtFunctionType(*this, T, |
| 1195 | Info.withNoReturn(AddNoReturn)); |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | QualType ASTContext::getCallConvType(QualType T, CallingConv CallConv) { |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 1199 | FunctionType::ExtInfo Info = getFunctionExtInfo(T); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1200 | return getExtFunctionType(*this, T, |
| 1201 | Info.withCallingConv(CallConv)); |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 1204 | QualType ASTContext::getRegParmType(QualType T, unsigned RegParm) { |
| 1205 | FunctionType::ExtInfo Info = getFunctionExtInfo(T); |
| 1206 | return getExtFunctionType(*this, T, |
| 1207 | Info.withRegParm(RegParm)); |
| 1208 | } |
| 1209 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1210 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 1211 | /// number with the specified element type. |
| 1212 | QualType ASTContext::getComplexType(QualType T) { |
| 1213 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1214 | // structure. |
| 1215 | llvm::FoldingSetNodeID ID; |
| 1216 | ComplexType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1217 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1218 | void *InsertPos = 0; |
| 1219 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1220 | return QualType(CT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1221 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1222 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1223 | // so fill in the canonical type field. |
| 1224 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1225 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1226 | Canonical = getComplexType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1227 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1228 | // Get the new insert position for the node we care about. |
| 1229 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1230 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1231 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1232 | ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1233 | Types.push_back(New); |
| 1234 | ComplexTypes.InsertNode(New, InsertPos); |
| 1235 | return QualType(New, 0); |
| 1236 | } |
| 1237 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1238 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 1239 | /// the specified type. |
| 1240 | QualType ASTContext::getPointerType(QualType T) { |
| 1241 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1242 | // structure. |
| 1243 | llvm::FoldingSetNodeID ID; |
| 1244 | PointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1245 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1246 | void *InsertPos = 0; |
| 1247 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1248 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1249 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1250 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1251 | // so fill in the canonical type field. |
| 1252 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1253 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1254 | Canonical = getPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1255 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1256 | // Get the new insert position for the node we care about. |
| 1257 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1258 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1259 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1260 | PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1261 | Types.push_back(New); |
| 1262 | PointerTypes.InsertNode(New, InsertPos); |
| 1263 | return QualType(New, 0); |
| 1264 | } |
| 1265 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1266 | /// getBlockPointerType - Return the uniqued reference to the type for |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1267 | /// a pointer to the specified block. |
| 1268 | QualType ASTContext::getBlockPointerType(QualType T) { |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1269 | assert(T->isFunctionType() && "block of function types only"); |
| 1270 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1271 | // structure. |
| 1272 | llvm::FoldingSetNodeID ID; |
| 1273 | BlockPointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1274 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1275 | void *InsertPos = 0; |
| 1276 | if (BlockPointerType *PT = |
| 1277 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1278 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1279 | |
| 1280 | // 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] | 1281 | // type either so fill in the canonical type field. |
| 1282 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1283 | if (!T.isCanonical()) { |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1284 | Canonical = getBlockPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1285 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1286 | // Get the new insert position for the node we care about. |
| 1287 | BlockPointerType *NewIP = |
| 1288 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1289 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1290 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1291 | BlockPointerType *New |
| 1292 | = new (*this, TypeAlignment) BlockPointerType(T, Canonical); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1293 | Types.push_back(New); |
| 1294 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 1295 | return QualType(New, 0); |
| 1296 | } |
| 1297 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1298 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 1299 | /// lvalue reference to the specified type. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1300 | QualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1301 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1302 | // structure. |
| 1303 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1304 | ReferenceType::Profile(ID, T, SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1305 | |
| 1306 | void *InsertPos = 0; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1307 | if (LValueReferenceType *RT = |
| 1308 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1309 | return QualType(RT, 0); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1310 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1311 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1312 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1313 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1314 | // either, so fill in the canonical type field. |
| 1315 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1316 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
| 1317 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1318 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1319 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1320 | // Get the new insert position for the node we care about. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1321 | LValueReferenceType *NewIP = |
| 1322 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1323 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1326 | LValueReferenceType *New |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1327 | = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, |
| 1328 | SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1329 | Types.push_back(New); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1330 | LValueReferenceTypes.InsertNode(New, InsertPos); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1331 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1332 | return QualType(New, 0); |
| 1333 | } |
| 1334 | |
| 1335 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 1336 | /// rvalue reference to the specified type. |
| 1337 | QualType ASTContext::getRValueReferenceType(QualType T) { |
| 1338 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1339 | // structure. |
| 1340 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1341 | ReferenceType::Profile(ID, T, false); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1342 | |
| 1343 | void *InsertPos = 0; |
| 1344 | if (RValueReferenceType *RT = |
| 1345 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1346 | return QualType(RT, 0); |
| 1347 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1348 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1349 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1350 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1351 | // either, so fill in the canonical type field. |
| 1352 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1353 | if (InnerRef || !T.isCanonical()) { |
| 1354 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1355 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1356 | |
| 1357 | // Get the new insert position for the node we care about. |
| 1358 | RValueReferenceType *NewIP = |
| 1359 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1360 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
| 1361 | } |
| 1362 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1363 | RValueReferenceType *New |
| 1364 | = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1365 | Types.push_back(New); |
| 1366 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1367 | return QualType(New, 0); |
| 1368 | } |
| 1369 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1370 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 1371 | /// member pointer to the specified type, in the specified class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1372 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1373 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1374 | // structure. |
| 1375 | llvm::FoldingSetNodeID ID; |
| 1376 | MemberPointerType::Profile(ID, T, Cls); |
| 1377 | |
| 1378 | void *InsertPos = 0; |
| 1379 | if (MemberPointerType *PT = |
| 1380 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1381 | return QualType(PT, 0); |
| 1382 | |
| 1383 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 1384 | // type either, so fill in the canonical type field. |
| 1385 | QualType Canonical; |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1386 | if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1387 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 1388 | |
| 1389 | // Get the new insert position for the node we care about. |
| 1390 | MemberPointerType *NewIP = |
| 1391 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1392 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
| 1393 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1394 | MemberPointerType *New |
| 1395 | = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1396 | Types.push_back(New); |
| 1397 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 1398 | return QualType(New, 0); |
| 1399 | } |
| 1400 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1401 | /// getConstantArrayType - Return the unique reference to the type for an |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1402 | /// array of the specified element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1403 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1404 | const llvm::APInt &ArySizeIn, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1405 | ArrayType::ArraySizeModifier ASM, |
| 1406 | unsigned EltTypeQuals) { |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1407 | assert((EltTy->isDependentType() || |
| 1408 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && |
Eli Friedman | 587cbdf | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1409 | "Constant array of VLAs is illegal!"); |
| 1410 | |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1411 | // Convert the array size into a canonical width matching the pointer size for |
| 1412 | // the target. |
| 1413 | llvm::APInt ArySize(ArySizeIn); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1414 | ArySize = |
| 1415 | ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1416 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1417 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 0be2ef2 | 2009-02-19 17:31:02 +0000 | [diff] [blame] | 1418 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1419 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1420 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1421 | if (ConstantArrayType *ATP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1422 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1423 | return QualType(ATP, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1424 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1425 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1426 | // so fill in the canonical type field. |
| 1427 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1428 | if (!EltTy.isCanonical()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1429 | Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1430 | ASM, EltTypeQuals); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1431 | // Get the new insert position for the node we care about. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1432 | ConstantArrayType *NewIP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1433 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1434 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1435 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1436 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1437 | ConstantArrayType *New = new(*this,TypeAlignment) |
| 1438 | ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1439 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1440 | Types.push_back(New); |
| 1441 | return QualType(New, 0); |
| 1442 | } |
| 1443 | |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1444 | /// getIncompleteArrayType - Returns a unique reference to the type for a |
| 1445 | /// incomplete array of the specified element type. |
| 1446 | QualType ASTContext::getUnknownSizeVariableArrayType(QualType Ty) { |
| 1447 | QualType ElemTy = getBaseElementType(Ty); |
| 1448 | DeclarationName Name; |
| 1449 | llvm::SmallVector<QualType, 8> ATypes; |
| 1450 | QualType ATy = Ty; |
| 1451 | while (const ArrayType *AT = getAsArrayType(ATy)) { |
| 1452 | ATypes.push_back(ATy); |
| 1453 | ATy = AT->getElementType(); |
| 1454 | } |
| 1455 | for (int i = ATypes.size() - 1; i >= 0; i--) { |
| 1456 | if (const VariableArrayType *VAT = getAsVariableArrayType(ATypes[i])) { |
| 1457 | ElemTy = getVariableArrayType(ElemTy, /*ArraySize*/0, ArrayType::Star, |
| 1458 | 0, VAT->getBracketsRange()); |
| 1459 | } |
| 1460 | else if (const ConstantArrayType *CAT = getAsConstantArrayType(ATypes[i])) { |
| 1461 | llvm::APSInt ConstVal(CAT->getSize()); |
| 1462 | ElemTy = getConstantArrayType(ElemTy, ConstVal, ArrayType::Normal, 0); |
| 1463 | } |
| 1464 | else if (getAsIncompleteArrayType(ATypes[i])) { |
| 1465 | ElemTy = getVariableArrayType(ElemTy, /*ArraySize*/0, ArrayType::Normal, |
| 1466 | 0, SourceRange()); |
| 1467 | } |
| 1468 | else |
| 1469 | assert(false && "DependentArrayType is seen"); |
| 1470 | } |
| 1471 | return ElemTy; |
| 1472 | } |
| 1473 | |
| 1474 | /// getVariableArrayDecayedType - Returns a vla type where known sizes |
| 1475 | /// are replaced with [*] |
| 1476 | QualType ASTContext::getVariableArrayDecayedType(QualType Ty) { |
| 1477 | if (Ty->isPointerType()) { |
| 1478 | QualType BaseType = Ty->getAs<PointerType>()->getPointeeType(); |
| 1479 | if (isa<VariableArrayType>(BaseType)) { |
| 1480 | ArrayType *AT = dyn_cast<ArrayType>(BaseType); |
| 1481 | VariableArrayType *VAT = cast<VariableArrayType>(AT); |
| 1482 | if (VAT->getSizeExpr()) { |
| 1483 | Ty = getUnknownSizeVariableArrayType(BaseType); |
| 1484 | Ty = getPointerType(Ty); |
| 1485 | } |
| 1486 | } |
| 1487 | } |
| 1488 | return Ty; |
| 1489 | } |
| 1490 | |
| 1491 | |
Steve Naroff | bdbf7b0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 1492 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 1493 | /// variable array of the specified element type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1494 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 1495 | Expr *NumElts, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1496 | ArrayType::ArraySizeModifier ASM, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1497 | unsigned EltTypeQuals, |
| 1498 | SourceRange Brackets) { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1499 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 1500 | // that have an expression provided for their size. |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 1501 | QualType CanonType; |
| 1502 | |
| 1503 | if (!EltTy.isCanonical()) { |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 1504 | CanonType = getVariableArrayType(getCanonicalType(EltTy), NumElts, ASM, |
| 1505 | EltTypeQuals, Brackets); |
| 1506 | } |
| 1507 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1508 | VariableArrayType *New = new(*this, TypeAlignment) |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 1509 | VariableArrayType(EltTy, CanonType, NumElts, ASM, EltTypeQuals, Brackets); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1510 | |
| 1511 | VariableArrayTypes.push_back(New); |
| 1512 | Types.push_back(New); |
| 1513 | return QualType(New, 0); |
| 1514 | } |
| 1515 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1516 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 1517 | /// the type for a dependently-sized array of the specified element |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1518 | /// type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1519 | QualType ASTContext::getDependentSizedArrayType(QualType EltTy, |
| 1520 | Expr *NumElts, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1521 | ArrayType::ArraySizeModifier ASM, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1522 | unsigned EltTypeQuals, |
| 1523 | SourceRange Brackets) { |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1524 | assert((!NumElts || NumElts->isTypeDependent() || |
| 1525 | NumElts->isValueDependent()) && |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1526 | "Size must be type- or value-dependent!"); |
| 1527 | |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1528 | void *InsertPos = 0; |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1529 | DependentSizedArrayType *Canon = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1530 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1531 | |
Douglas Gregor | 32adc8b | 2010-10-26 00:51:02 +0000 | [diff] [blame] | 1532 | QualType CanonicalEltTy = getCanonicalType(EltTy); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1533 | if (NumElts) { |
| 1534 | // Dependently-sized array types that do not have a specified |
| 1535 | // number of elements will have their sizes deduced from an |
| 1536 | // initializer. |
Douglas Gregor | 32adc8b | 2010-10-26 00:51:02 +0000 | [diff] [blame] | 1537 | DependentSizedArrayType::Profile(ID, *this, CanonicalEltTy, ASM, |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1538 | EltTypeQuals, NumElts); |
| 1539 | |
| 1540 | Canon = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1541 | } |
| 1542 | |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1543 | DependentSizedArrayType *New; |
| 1544 | if (Canon) { |
| 1545 | // We already have a canonical version of this array type; use it as |
| 1546 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1547 | New = new (*this, TypeAlignment) |
| 1548 | DependentSizedArrayType(*this, EltTy, QualType(Canon, 0), |
| 1549 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | 32adc8b | 2010-10-26 00:51:02 +0000 | [diff] [blame] | 1550 | } else if (CanonicalEltTy == EltTy) { |
| 1551 | // This is a canonical type. Record it. |
| 1552 | New = new (*this, TypeAlignment) |
| 1553 | DependentSizedArrayType(*this, EltTy, QualType(), |
| 1554 | NumElts, ASM, EltTypeQuals, Brackets); |
| 1555 | |
| 1556 | if (NumElts) { |
| 1557 | #ifndef NDEBUG |
| 1558 | DependentSizedArrayType *CanonCheck |
| 1559 | = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1560 | assert(!CanonCheck && "Dependent-sized canonical array type broken"); |
| 1561 | (void)CanonCheck; |
| 1562 | #endif |
| 1563 | DependentSizedArrayTypes.InsertNode(New, InsertPos); |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1564 | } |
Douglas Gregor | 32adc8b | 2010-10-26 00:51:02 +0000 | [diff] [blame] | 1565 | } else { |
| 1566 | QualType Canon = getDependentSizedArrayType(CanonicalEltTy, NumElts, |
| 1567 | ASM, EltTypeQuals, |
| 1568 | SourceRange()); |
| 1569 | New = new (*this, TypeAlignment) |
| 1570 | DependentSizedArrayType(*this, EltTy, Canon, |
| 1571 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1572 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1573 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1574 | Types.push_back(New); |
| 1575 | return QualType(New, 0); |
| 1576 | } |
| 1577 | |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1578 | QualType ASTContext::getIncompleteArrayType(QualType EltTy, |
| 1579 | ArrayType::ArraySizeModifier ASM, |
| 1580 | unsigned EltTypeQuals) { |
| 1581 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 0be2ef2 | 2009-02-19 17:31:02 +0000 | [diff] [blame] | 1582 | IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1583 | |
| 1584 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1585 | if (IncompleteArrayType *ATP = |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1586 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1587 | return QualType(ATP, 0); |
| 1588 | |
| 1589 | // If the element type isn't canonical, this won't be a canonical type |
| 1590 | // either, so fill in the canonical type field. |
| 1591 | QualType Canonical; |
| 1592 | |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1593 | if (!EltTy.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1594 | Canonical = getIncompleteArrayType(getCanonicalType(EltTy), |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1595 | ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1596 | |
| 1597 | // Get the new insert position for the node we care about. |
| 1598 | IncompleteArrayType *NewIP = |
| 1599 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1600 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1601 | } |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1602 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1603 | IncompleteArrayType *New = new (*this, TypeAlignment) |
| 1604 | IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1605 | |
| 1606 | IncompleteArrayTypes.InsertNode(New, InsertPos); |
| 1607 | Types.push_back(New); |
| 1608 | return QualType(New, 0); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1609 | } |
| 1610 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1611 | /// getVectorType - Return the unique reference to a vector type of |
| 1612 | /// 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] | 1613 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1614 | VectorType::VectorKind VecKind) { |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 1615 | BuiltinType *BaseType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1616 | |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 1617 | BaseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); |
| 1618 | assert(BaseType != 0 && "getVectorType(): Expecting a built-in type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1619 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1620 | // Check if we've already instantiated a vector of this type. |
| 1621 | llvm::FoldingSetNodeID ID; |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1622 | VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind); |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1623 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1624 | void *InsertPos = 0; |
| 1625 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1626 | return QualType(VTP, 0); |
| 1627 | |
| 1628 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1629 | // so fill in the canonical type field. |
| 1630 | QualType Canonical; |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1631 | if (!vecType.isCanonical()) { |
Bob Wilson | 231da7e | 2010-11-16 00:32:20 +0000 | [diff] [blame] | 1632 | Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1633 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1634 | // Get the new insert position for the node we care about. |
| 1635 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1636 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1637 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1638 | VectorType *New = new (*this, TypeAlignment) |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1639 | VectorType(vecType, NumElts, Canonical, VecKind); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1640 | VectorTypes.InsertNode(New, InsertPos); |
| 1641 | Types.push_back(New); |
| 1642 | return QualType(New, 0); |
| 1643 | } |
| 1644 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1645 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1646 | /// 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] | 1647 | QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) { |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1648 | BuiltinType *baseType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1649 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1650 | baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1651 | assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1652 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1653 | // Check if we've already instantiated a vector of this type. |
| 1654 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1655 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1656 | VectorType::GenericVector); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1657 | void *InsertPos = 0; |
| 1658 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1659 | return QualType(VTP, 0); |
| 1660 | |
| 1661 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1662 | // so fill in the canonical type field. |
| 1663 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1664 | if (!vecType.isCanonical()) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1665 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1666 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1667 | // Get the new insert position for the node we care about. |
| 1668 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1669 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1670 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1671 | ExtVectorType *New = new (*this, TypeAlignment) |
| 1672 | ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1673 | VectorTypes.InsertNode(New, InsertPos); |
| 1674 | Types.push_back(New); |
| 1675 | return QualType(New, 0); |
| 1676 | } |
| 1677 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1678 | QualType ASTContext::getDependentSizedExtVectorType(QualType vecType, |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1679 | Expr *SizeExpr, |
| 1680 | SourceLocation AttrLoc) { |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1681 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1682 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1683 | SizeExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1684 | |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1685 | void *InsertPos = 0; |
| 1686 | DependentSizedExtVectorType *Canon |
| 1687 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1688 | DependentSizedExtVectorType *New; |
| 1689 | if (Canon) { |
| 1690 | // We already have a canonical version of this array type; use it as |
| 1691 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1692 | New = new (*this, TypeAlignment) |
| 1693 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), |
| 1694 | SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1695 | } else { |
| 1696 | QualType CanonVecTy = getCanonicalType(vecType); |
| 1697 | if (CanonVecTy == vecType) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1698 | New = new (*this, TypeAlignment) |
| 1699 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, |
| 1700 | AttrLoc); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1701 | |
| 1702 | DependentSizedExtVectorType *CanonCheck |
| 1703 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1704 | assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken"); |
| 1705 | (void)CanonCheck; |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1706 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); |
| 1707 | } else { |
| 1708 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 1709 | SourceLocation()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1710 | New = new (*this, TypeAlignment) |
| 1711 | DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1712 | } |
| 1713 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1714 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1715 | Types.push_back(New); |
| 1716 | return QualType(New, 0); |
| 1717 | } |
| 1718 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1719 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1720 | /// |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1721 | QualType ASTContext::getFunctionNoProtoType(QualType ResultTy, |
| 1722 | const FunctionType::ExtInfo &Info) { |
| 1723 | const CallingConv CallConv = Info.getCC(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1724 | // Unique functions, to guarantee there is only one function of a particular |
| 1725 | // structure. |
| 1726 | llvm::FoldingSetNodeID ID; |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1727 | FunctionNoProtoType::Profile(ID, ResultTy, Info); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1728 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1729 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1730 | if (FunctionNoProtoType *FT = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1731 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1732 | return QualType(FT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1733 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1734 | QualType Canonical; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1735 | if (!ResultTy.isCanonical() || |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1736 | getCanonicalCallConv(CallConv) != CallConv) { |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1737 | Canonical = |
| 1738 | getFunctionNoProtoType(getCanonicalType(ResultTy), |
| 1739 | Info.withCallingConv(getCanonicalCallConv(CallConv))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1740 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1741 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1742 | FunctionNoProtoType *NewIP = |
| 1743 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1744 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1745 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1746 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1747 | FunctionNoProtoType *New = new (*this, TypeAlignment) |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1748 | FunctionNoProtoType(ResultTy, Canonical, Info); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1749 | Types.push_back(New); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1750 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1751 | return QualType(New, 0); |
| 1752 | } |
| 1753 | |
| 1754 | /// getFunctionType - Return a normal function type with a typed argument |
| 1755 | /// list. isVariadic indicates whether the argument list includes '...'. |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 1756 | QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray, |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1757 | unsigned NumArgs, bool isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1758 | unsigned TypeQuals, bool hasExceptionSpec, |
| 1759 | bool hasAnyExceptionSpec, unsigned NumExs, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1760 | const QualType *ExArray, |
| 1761 | const FunctionType::ExtInfo &Info) { |
Douglas Gregor | dab60ad | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 1762 | |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1763 | const CallingConv CallConv= Info.getCC(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1764 | // Unique functions, to guarantee there is only one function of a particular |
| 1765 | // structure. |
| 1766 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1767 | FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1768 | TypeQuals, hasExceptionSpec, hasAnyExceptionSpec, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1769 | NumExs, ExArray, Info); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1770 | |
| 1771 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1772 | if (FunctionProtoType *FTP = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1773 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1774 | return QualType(FTP, 0); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1775 | |
| 1776 | // Determine whether the type being created is already canonical or not. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1777 | bool isCanonical = !hasExceptionSpec && ResultTy.isCanonical(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1778 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1779 | if (!ArgArray[i].isCanonicalAsParam()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1780 | isCanonical = false; |
| 1781 | |
| 1782 | // If this type isn't canonical, get the canonical version of it. |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1783 | // The exception spec is not part of the canonical type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1784 | QualType Canonical; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1785 | if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1786 | llvm::SmallVector<QualType, 16> CanonicalArgs; |
| 1787 | CanonicalArgs.reserve(NumArgs); |
| 1788 | for (unsigned i = 0; i != NumArgs; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1789 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1790 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1791 | Canonical = getFunctionType(getCanonicalType(ResultTy), |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1792 | CanonicalArgs.data(), NumArgs, |
Douglas Gregor | 47259d9 | 2009-08-05 19:03:35 +0000 | [diff] [blame] | 1793 | isVariadic, TypeQuals, false, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1794 | false, 0, 0, |
| 1795 | Info.withCallingConv(getCanonicalCallConv(CallConv))); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1796 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1797 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1798 | FunctionProtoType *NewIP = |
| 1799 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1800 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1801 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1802 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1803 | // FunctionProtoType objects are allocated with extra bytes after them |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1804 | // for two variable size arrays (for parameter and exception types) at the |
| 1805 | // end of them. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1806 | FunctionProtoType *FTP = |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1807 | (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) + |
| 1808 | NumArgs*sizeof(QualType) + |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1809 | NumExs*sizeof(QualType), TypeAlignment); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1810 | new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1811 | TypeQuals, hasExceptionSpec, hasAnyExceptionSpec, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1812 | ExArray, NumExs, Canonical, Info); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1813 | Types.push_back(FTP); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1814 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1815 | return QualType(FTP, 0); |
| 1816 | } |
| 1817 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1818 | #ifndef NDEBUG |
| 1819 | static bool NeedsInjectedClassNameType(const RecordDecl *D) { |
| 1820 | if (!isa<CXXRecordDecl>(D)) return false; |
| 1821 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 1822 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) |
| 1823 | return true; |
| 1824 | if (RD->getDescribedClassTemplate() && |
| 1825 | !isa<ClassTemplateSpecializationDecl>(RD)) |
| 1826 | return true; |
| 1827 | return false; |
| 1828 | } |
| 1829 | #endif |
| 1830 | |
| 1831 | /// getInjectedClassNameType - Return the unique reference to the |
| 1832 | /// injected class name type for the specified templated declaration. |
| 1833 | QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl, |
| 1834 | QualType TST) { |
| 1835 | assert(NeedsInjectedClassNameType(Decl)); |
| 1836 | if (Decl->TypeForDecl) { |
| 1837 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
Argyrios Kyrtzidis | 37ffed3 | 2010-07-02 11:55:32 +0000 | [diff] [blame] | 1838 | } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDeclaration()) { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1839 | assert(PrevDecl->TypeForDecl && "previous declaration has no type"); |
| 1840 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 1841 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
| 1842 | } else { |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1843 | Decl->TypeForDecl = |
| 1844 | new (*this, TypeAlignment) InjectedClassNameType(Decl, TST); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1845 | Types.push_back(Decl->TypeForDecl); |
| 1846 | } |
| 1847 | return QualType(Decl->TypeForDecl, 0); |
| 1848 | } |
| 1849 | |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1850 | /// getTypeDeclType - Return the unique reference to the type for the |
| 1851 | /// specified type declaration. |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1852 | QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) { |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 1853 | assert(Decl && "Passed null for Decl param"); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1854 | assert(!Decl->TypeForDecl && "TypeForDecl present in slow case"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1855 | |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1856 | if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1857 | return getTypedefType(Typedef); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1858 | |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1859 | assert(!isa<TemplateTypeParmDecl>(Decl) && |
| 1860 | "Template type parameter types are always available."); |
| 1861 | |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1862 | if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1863 | assert(!Record->getPreviousDeclaration() && |
| 1864 | "struct/union has previous declaration"); |
| 1865 | assert(!NeedsInjectedClassNameType(Record)); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 1866 | return getRecordType(Record); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1867 | } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1868 | assert(!Enum->getPreviousDeclaration() && |
| 1869 | "enum has previous declaration"); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 1870 | return getEnumType(Enum); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1871 | } else if (const UnresolvedUsingTypenameDecl *Using = |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1872 | dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) { |
| 1873 | Decl->TypeForDecl = new (*this, TypeAlignment) UnresolvedUsingType(Using); |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 1874 | } else |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1875 | llvm_unreachable("TypeDecl without a type?"); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1876 | |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1877 | Types.push_back(Decl->TypeForDecl); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1878 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1879 | } |
| 1880 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1881 | /// getTypedefType - Return the unique reference to the type for the |
| 1882 | /// specified typename decl. |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 1883 | QualType |
| 1884 | ASTContext::getTypedefType(const TypedefDecl *Decl, QualType Canonical) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1885 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1886 | |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 1887 | if (Canonical.isNull()) |
| 1888 | Canonical = getCanonicalType(Decl->getUnderlyingType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1889 | Decl->TypeForDecl = new(*this, TypeAlignment) |
| 1890 | TypedefType(Type::Typedef, Decl, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1891 | Types.push_back(Decl->TypeForDecl); |
| 1892 | return QualType(Decl->TypeForDecl, 0); |
| 1893 | } |
| 1894 | |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 1895 | QualType ASTContext::getRecordType(const RecordDecl *Decl) { |
| 1896 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 1897 | |
| 1898 | if (const RecordDecl *PrevDecl = Decl->getPreviousDeclaration()) |
| 1899 | if (PrevDecl->TypeForDecl) |
| 1900 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 1901 | |
| 1902 | Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Decl); |
| 1903 | Types.push_back(Decl->TypeForDecl); |
| 1904 | return QualType(Decl->TypeForDecl, 0); |
| 1905 | } |
| 1906 | |
| 1907 | QualType ASTContext::getEnumType(const EnumDecl *Decl) { |
| 1908 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 1909 | |
| 1910 | if (const EnumDecl *PrevDecl = Decl->getPreviousDeclaration()) |
| 1911 | if (PrevDecl->TypeForDecl) |
| 1912 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 1913 | |
| 1914 | Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Decl); |
| 1915 | Types.push_back(Decl->TypeForDecl); |
| 1916 | return QualType(Decl->TypeForDecl, 0); |
| 1917 | } |
| 1918 | |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1919 | /// \brief Retrieve a substitution-result type. |
| 1920 | QualType |
| 1921 | ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, |
| 1922 | QualType Replacement) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1923 | assert(Replacement.isCanonical() |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1924 | && "replacement types must always be canonical"); |
| 1925 | |
| 1926 | llvm::FoldingSetNodeID ID; |
| 1927 | SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); |
| 1928 | void *InsertPos = 0; |
| 1929 | SubstTemplateTypeParmType *SubstParm |
| 1930 | = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1931 | |
| 1932 | if (!SubstParm) { |
| 1933 | SubstParm = new (*this, TypeAlignment) |
| 1934 | SubstTemplateTypeParmType(Parm, Replacement); |
| 1935 | Types.push_back(SubstParm); |
| 1936 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 1937 | } |
| 1938 | |
| 1939 | return QualType(SubstParm, 0); |
| 1940 | } |
| 1941 | |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1942 | /// \brief Retrieve the template type parameter type for a template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1943 | /// parameter or parameter pack with the given depth, index, and (optionally) |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1944 | /// name. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1945 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1946 | bool ParameterPack, |
Douglas Gregor | efed5c8 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 1947 | IdentifierInfo *Name) { |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1948 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | efed5c8 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 1949 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1950 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1951 | TemplateTypeParmType *TypeParm |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1952 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1953 | |
| 1954 | if (TypeParm) |
| 1955 | return QualType(TypeParm, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1956 | |
Douglas Gregor | efed5c8 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 1957 | if (Name) { |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1958 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | efed5c8 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 1959 | TypeParm = new (*this, TypeAlignment) |
| 1960 | TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1961 | |
| 1962 | TemplateTypeParmType *TypeCheck |
| 1963 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1964 | assert(!TypeCheck && "Template type parameter canonical type broken"); |
| 1965 | (void)TypeCheck; |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1966 | } else |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1967 | TypeParm = new (*this, TypeAlignment) |
| 1968 | TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1969 | |
| 1970 | Types.push_back(TypeParm); |
| 1971 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 1972 | |
| 1973 | return QualType(TypeParm, 0); |
| 1974 | } |
| 1975 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1976 | TypeSourceInfo * |
| 1977 | ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name, |
| 1978 | SourceLocation NameLoc, |
| 1979 | const TemplateArgumentListInfo &Args, |
| 1980 | QualType CanonType) { |
| 1981 | QualType TST = getTemplateSpecializationType(Name, Args, CanonType); |
| 1982 | |
| 1983 | TypeSourceInfo *DI = CreateTypeSourceInfo(TST); |
| 1984 | TemplateSpecializationTypeLoc TL |
| 1985 | = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc()); |
| 1986 | TL.setTemplateNameLoc(NameLoc); |
| 1987 | TL.setLAngleLoc(Args.getLAngleLoc()); |
| 1988 | TL.setRAngleLoc(Args.getRAngleLoc()); |
| 1989 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 1990 | TL.setArgLocInfo(i, Args[i].getLocInfo()); |
| 1991 | return DI; |
| 1992 | } |
| 1993 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1994 | QualType |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1995 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1996 | const TemplateArgumentListInfo &Args, |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 1997 | QualType Canon) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1998 | unsigned NumArgs = Args.size(); |
| 1999 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2000 | llvm::SmallVector<TemplateArgument, 4> ArgVec; |
| 2001 | ArgVec.reserve(NumArgs); |
| 2002 | for (unsigned i = 0; i != NumArgs; ++i) |
| 2003 | ArgVec.push_back(Args[i].getArgument()); |
| 2004 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2005 | return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 2006 | Canon); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2007 | } |
| 2008 | |
| 2009 | QualType |
| 2010 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2011 | const TemplateArgument *Args, |
| 2012 | unsigned NumArgs, |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 2013 | QualType Canon) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 2014 | if (!Canon.isNull()) |
| 2015 | Canon = getCanonicalType(Canon); |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2016 | else |
| 2017 | Canon = getCanonicalTemplateSpecializationType(Template, Args, NumArgs); |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 2018 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2019 | // Allocate the (non-canonical) template specialization type, but don't |
| 2020 | // try to unique it: these types typically have location information that |
| 2021 | // we don't unique and don't want to lose. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2022 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2023 | sizeof(TemplateArgument) * NumArgs), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2024 | TypeAlignment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2025 | TemplateSpecializationType *Spec |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 2026 | = new (Mem) TemplateSpecializationType(Template, |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2027 | Args, NumArgs, |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 2028 | Canon); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2029 | |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2030 | Types.push_back(Spec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2031 | return QualType(Spec, 0); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2032 | } |
| 2033 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2034 | QualType |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2035 | ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template, |
| 2036 | const TemplateArgument *Args, |
| 2037 | unsigned NumArgs) { |
| 2038 | // Build the canonical template specialization type. |
| 2039 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); |
| 2040 | llvm::SmallVector<TemplateArgument, 4> CanonArgs; |
| 2041 | CanonArgs.reserve(NumArgs); |
| 2042 | for (unsigned I = 0; I != NumArgs; ++I) |
| 2043 | CanonArgs.push_back(getCanonicalTemplateArgument(Args[I])); |
| 2044 | |
| 2045 | // Determine whether this canonical template specialization type already |
| 2046 | // exists. |
| 2047 | llvm::FoldingSetNodeID ID; |
| 2048 | TemplateSpecializationType::Profile(ID, CanonTemplate, |
| 2049 | CanonArgs.data(), NumArgs, *this); |
| 2050 | |
| 2051 | void *InsertPos = 0; |
| 2052 | TemplateSpecializationType *Spec |
| 2053 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2054 | |
| 2055 | if (!Spec) { |
| 2056 | // Allocate a new canonical template specialization type. |
| 2057 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
| 2058 | sizeof(TemplateArgument) * NumArgs), |
| 2059 | TypeAlignment); |
| 2060 | Spec = new (Mem) TemplateSpecializationType(CanonTemplate, |
| 2061 | CanonArgs.data(), NumArgs, |
| 2062 | QualType()); |
| 2063 | Types.push_back(Spec); |
| 2064 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
| 2065 | } |
| 2066 | |
| 2067 | assert(Spec->isDependentType() && |
| 2068 | "Non-dependent template-id type must have a canonical type"); |
| 2069 | return QualType(Spec, 0); |
| 2070 | } |
| 2071 | |
| 2072 | QualType |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2073 | ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword, |
| 2074 | NestedNameSpecifier *NNS, |
| 2075 | QualType NamedType) { |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2076 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2077 | ElaboratedType::Profile(ID, Keyword, NNS, NamedType); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2078 | |
| 2079 | void *InsertPos = 0; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2080 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2081 | if (T) |
| 2082 | return QualType(T, 0); |
| 2083 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2084 | QualType Canon = NamedType; |
| 2085 | if (!Canon.isCanonical()) { |
| 2086 | Canon = getCanonicalType(NamedType); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2087 | ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2088 | assert(!CheckT && "Elaborated canonical type broken"); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2089 | (void)CheckT; |
| 2090 | } |
| 2091 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2092 | T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2093 | Types.push_back(T); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2094 | ElaboratedTypes.InsertNode(T, InsertPos); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2095 | return QualType(T, 0); |
| 2096 | } |
| 2097 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2098 | QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, |
| 2099 | NestedNameSpecifier *NNS, |
| 2100 | const IdentifierInfo *Name, |
| 2101 | QualType Canon) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2102 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 2103 | |
| 2104 | if (Canon.isNull()) { |
| 2105 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2106 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 2107 | if (Keyword == ETK_None) |
| 2108 | CanonKeyword = ETK_Typename; |
| 2109 | |
| 2110 | if (CanonNNS != NNS || CanonKeyword != Keyword) |
| 2111 | Canon = getDependentNameType(CanonKeyword, CanonNNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2112 | } |
| 2113 | |
| 2114 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2115 | DependentNameType::Profile(ID, Keyword, NNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2116 | |
| 2117 | void *InsertPos = 0; |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2118 | DependentNameType *T |
| 2119 | = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2120 | if (T) |
| 2121 | return QualType(T, 0); |
| 2122 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2123 | T = new (*this) DependentNameType(Keyword, NNS, Name, Canon); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2124 | Types.push_back(T); |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2125 | DependentNameTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2126 | return QualType(T, 0); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2127 | } |
| 2128 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2129 | QualType |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2130 | ASTContext::getDependentTemplateSpecializationType( |
| 2131 | ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2132 | NestedNameSpecifier *NNS, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2133 | const IdentifierInfo *Name, |
| 2134 | const TemplateArgumentListInfo &Args) { |
| 2135 | // TODO: avoid this copy |
| 2136 | llvm::SmallVector<TemplateArgument, 16> ArgCopy; |
| 2137 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
| 2138 | ArgCopy.push_back(Args[I].getArgument()); |
| 2139 | return getDependentTemplateSpecializationType(Keyword, NNS, Name, |
| 2140 | ArgCopy.size(), |
| 2141 | ArgCopy.data()); |
| 2142 | } |
| 2143 | |
| 2144 | QualType |
| 2145 | ASTContext::getDependentTemplateSpecializationType( |
| 2146 | ElaboratedTypeKeyword Keyword, |
| 2147 | NestedNameSpecifier *NNS, |
| 2148 | const IdentifierInfo *Name, |
| 2149 | unsigned NumArgs, |
| 2150 | const TemplateArgument *Args) { |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2151 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 2152 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2153 | llvm::FoldingSetNodeID ID; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2154 | DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS, |
| 2155 | Name, NumArgs, Args); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2156 | |
| 2157 | void *InsertPos = 0; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2158 | DependentTemplateSpecializationType *T |
| 2159 | = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2160 | if (T) |
| 2161 | return QualType(T, 0); |
| 2162 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2163 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2164 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2165 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 2166 | if (Keyword == ETK_None) CanonKeyword = ETK_Typename; |
| 2167 | |
| 2168 | bool AnyNonCanonArgs = false; |
| 2169 | llvm::SmallVector<TemplateArgument, 16> CanonArgs(NumArgs); |
| 2170 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 2171 | CanonArgs[I] = getCanonicalTemplateArgument(Args[I]); |
| 2172 | if (!CanonArgs[I].structurallyEquals(Args[I])) |
| 2173 | AnyNonCanonArgs = true; |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2174 | } |
| 2175 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2176 | QualType Canon; |
| 2177 | if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) { |
| 2178 | Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS, |
| 2179 | Name, NumArgs, |
| 2180 | CanonArgs.data()); |
| 2181 | |
| 2182 | // Find the insert position again. |
| 2183 | DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2184 | } |
| 2185 | |
| 2186 | void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) + |
| 2187 | sizeof(TemplateArgument) * NumArgs), |
| 2188 | TypeAlignment); |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 2189 | T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2190 | Name, NumArgs, Args, Canon); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2191 | Types.push_back(T); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2192 | DependentTemplateSpecializationTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2193 | return QualType(T, 0); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2194 | } |
| 2195 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2196 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 2197 | /// alphabetically. |
| 2198 | static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, |
| 2199 | const ObjCProtocolDecl *RHS) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2200 | return LHS->getDeclName() < RHS->getDeclName(); |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2201 | } |
| 2202 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2203 | static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols, |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2204 | unsigned NumProtocols) { |
| 2205 | if (NumProtocols == 0) return true; |
| 2206 | |
| 2207 | for (unsigned i = 1; i != NumProtocols; ++i) |
| 2208 | if (!CmpProtocolNames(Protocols[i-1], Protocols[i])) |
| 2209 | return false; |
| 2210 | return true; |
| 2211 | } |
| 2212 | |
| 2213 | static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols, |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2214 | unsigned &NumProtocols) { |
| 2215 | ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2216 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2217 | // Sort protocols, keyed by name. |
| 2218 | std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); |
| 2219 | |
| 2220 | // Remove duplicates. |
| 2221 | ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); |
| 2222 | NumProtocols = ProtocolsEnd-Protocols; |
| 2223 | } |
| 2224 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2225 | QualType ASTContext::getObjCObjectType(QualType BaseType, |
| 2226 | ObjCProtocolDecl * const *Protocols, |
| 2227 | unsigned NumProtocols) { |
| 2228 | // If the base type is an interface and there aren't any protocols |
| 2229 | // to add, then the interface type will do just fine. |
| 2230 | if (!NumProtocols && isa<ObjCInterfaceType>(BaseType)) |
| 2231 | return BaseType; |
| 2232 | |
| 2233 | // Look in the folding set for an existing type. |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2234 | llvm::FoldingSetNodeID ID; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2235 | ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2236 | void *InsertPos = 0; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2237 | if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2238 | return QualType(QT, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2239 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2240 | // Build the canonical type, which has the canonical base type and |
| 2241 | // a sorted-and-uniqued list of protocols. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2242 | QualType Canonical; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2243 | bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols); |
| 2244 | if (!ProtocolsSorted || !BaseType.isCanonical()) { |
| 2245 | if (!ProtocolsSorted) { |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 2246 | llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols, |
| 2247 | Protocols + NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2248 | unsigned UniqueCount = NumProtocols; |
| 2249 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2250 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2251 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 2252 | &Sorted[0], UniqueCount); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2253 | } else { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2254 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 2255 | Protocols, NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2256 | } |
| 2257 | |
| 2258 | // Regenerate InsertPos. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2259 | ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2260 | } |
| 2261 | |
| 2262 | unsigned Size = sizeof(ObjCObjectTypeImpl); |
| 2263 | Size += NumProtocols * sizeof(ObjCProtocolDecl *); |
| 2264 | void *Mem = Allocate(Size, TypeAlignment); |
| 2265 | ObjCObjectTypeImpl *T = |
| 2266 | new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols); |
| 2267 | |
| 2268 | Types.push_back(T); |
| 2269 | ObjCObjectTypes.InsertNode(T, InsertPos); |
| 2270 | return QualType(T, 0); |
| 2271 | } |
| 2272 | |
| 2273 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 2274 | /// the given object type. |
| 2275 | QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) { |
| 2276 | llvm::FoldingSetNodeID ID; |
| 2277 | ObjCObjectPointerType::Profile(ID, ObjectT); |
| 2278 | |
| 2279 | void *InsertPos = 0; |
| 2280 | if (ObjCObjectPointerType *QT = |
| 2281 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2282 | return QualType(QT, 0); |
| 2283 | |
| 2284 | // Find the canonical object type. |
| 2285 | QualType Canonical; |
| 2286 | if (!ObjectT.isCanonical()) { |
| 2287 | Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT)); |
| 2288 | |
| 2289 | // Regenerate InsertPos. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2290 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2291 | } |
| 2292 | |
Douglas Gregor | fd6a088 | 2010-02-08 22:59:26 +0000 | [diff] [blame] | 2293 | // No match. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2294 | void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment); |
| 2295 | ObjCObjectPointerType *QType = |
| 2296 | new (Mem) ObjCObjectPointerType(Canonical, ObjectT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2297 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2298 | Types.push_back(QType); |
| 2299 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2300 | return QualType(QType, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2301 | } |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2302 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2303 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 2304 | /// specified ObjC interface decl. The list of protocols is optional. |
| 2305 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) { |
| 2306 | if (Decl->TypeForDecl) |
| 2307 | return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2308 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2309 | // FIXME: redeclarations? |
| 2310 | void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment); |
| 2311 | ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl); |
| 2312 | Decl->TypeForDecl = T; |
| 2313 | Types.push_back(T); |
| 2314 | return QualType(T, 0); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2315 | } |
| 2316 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2317 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 2318 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2319 | /// multiple declarations that refer to "typeof(x)" all contain different |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2320 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2321 | /// on canonical type's (which are always unique). |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2322 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2323 | TypeOfExprType *toe; |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2324 | if (tofExpr->isTypeDependent()) { |
| 2325 | llvm::FoldingSetNodeID ID; |
| 2326 | DependentTypeOfExprType::Profile(ID, *this, tofExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2327 | |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2328 | void *InsertPos = 0; |
| 2329 | DependentTypeOfExprType *Canon |
| 2330 | = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2331 | if (Canon) { |
| 2332 | // We already have a "canonical" version of an identical, dependent |
| 2333 | // typeof(expr) type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2334 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2335 | QualType((TypeOfExprType*)Canon, 0)); |
| 2336 | } |
| 2337 | else { |
| 2338 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2339 | Canon |
| 2340 | = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2341 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); |
| 2342 | toe = Canon; |
| 2343 | } |
| 2344 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2345 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2346 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2347 | } |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2348 | Types.push_back(toe); |
| 2349 | return QualType(toe, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2350 | } |
| 2351 | |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2352 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 2353 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 2354 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2355 | /// an issue. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2356 | /// on canonical type's (which are always unique). |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2357 | QualType ASTContext::getTypeOfType(QualType tofType) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2358 | QualType Canonical = getCanonicalType(tofType); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2359 | TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2360 | Types.push_back(tot); |
| 2361 | return QualType(tot, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2362 | } |
| 2363 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2364 | /// getDecltypeForExpr - Given an expr, will return the decltype for that |
| 2365 | /// expression, according to the rules in C++0x [dcl.type.simple]p4 |
| 2366 | static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) { |
Anders Carlsson | a07c33e | 2009-06-25 15:00:34 +0000 | [diff] [blame] | 2367 | if (e->isTypeDependent()) |
| 2368 | return Context.DependentTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2369 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2370 | // If e is an id expression or a class member access, decltype(e) is defined |
| 2371 | // as the type of the entity named by e. |
| 2372 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) { |
| 2373 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) |
| 2374 | return VD->getType(); |
| 2375 | } |
| 2376 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) { |
| 2377 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) |
| 2378 | return FD->getType(); |
| 2379 | } |
| 2380 | // If e is a function call or an invocation of an overloaded operator, |
| 2381 | // (parentheses around e are ignored), decltype(e) is defined as the |
| 2382 | // return type of that function. |
| 2383 | if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens())) |
| 2384 | return CE->getCallReturnType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2385 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2386 | QualType T = e->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2387 | |
| 2388 | // 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] | 2389 | // defined as T&, otherwise decltype(e) is defined as T. |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 2390 | if (e->isLValue()) |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2391 | T = Context.getLValueReferenceType(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2392 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2393 | return T; |
| 2394 | } |
| 2395 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2396 | /// getDecltypeType - Unlike many "get<Type>" functions, we don't unique |
| 2397 | /// DecltypeType AST's. The only motivation to unique these nodes would be |
| 2398 | /// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2399 | /// an issue. This doesn't effect the type checker, since it operates |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2400 | /// on canonical type's (which are always unique). |
| 2401 | QualType ASTContext::getDecltypeType(Expr *e) { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2402 | DecltypeType *dt; |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2403 | if (e->isTypeDependent()) { |
| 2404 | llvm::FoldingSetNodeID ID; |
| 2405 | DependentDecltypeType::Profile(ID, *this, e); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2406 | |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2407 | void *InsertPos = 0; |
| 2408 | DependentDecltypeType *Canon |
| 2409 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2410 | if (Canon) { |
| 2411 | // We already have a "canonical" version of an equivalent, dependent |
| 2412 | // decltype type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2413 | dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy, |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2414 | QualType((DecltypeType*)Canon, 0)); |
| 2415 | } |
| 2416 | else { |
| 2417 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2418 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2419 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); |
| 2420 | dt = Canon; |
| 2421 | } |
| 2422 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2423 | QualType T = getDecltypeForExpr(e, *this); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2424 | dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T)); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2425 | } |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2426 | Types.push_back(dt); |
| 2427 | return QualType(dt, 0); |
| 2428 | } |
| 2429 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2430 | /// getTagDeclType - Return the unique reference to the type for the |
| 2431 | /// specified TagDecl (struct/union/class/enum) decl. |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 2432 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) { |
Ted Kremenek | d778f88 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 2433 | assert (Decl); |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 2434 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 2435 | // away const? mutable? |
| 2436 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2437 | } |
| 2438 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2439 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 2440 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 2441 | /// needs to agree with the definition in <stddef.h>. |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 2442 | CanQualType ASTContext::getSizeType() const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2443 | return getFromTargetType(Target.getSizeType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2444 | } |
| 2445 | |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 2446 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 2447 | /// Used when in C++, as a GCC extension. |
| 2448 | QualType ASTContext::getSignedWCharType() const { |
| 2449 | // FIXME: derive from "Target" ? |
| 2450 | return WCharTy; |
| 2451 | } |
| 2452 | |
| 2453 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 2454 | /// Used when in C++, as a GCC extension. |
| 2455 | QualType ASTContext::getUnsignedWCharType() const { |
| 2456 | // FIXME: derive from "Target" ? |
| 2457 | return UnsignedIntTy; |
| 2458 | } |
| 2459 | |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 2460 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) |
| 2461 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 2462 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2463 | return getFromTargetType(Target.getPtrDiffType(0)); |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 2464 | } |
| 2465 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2466 | //===----------------------------------------------------------------------===// |
| 2467 | // Type Operators |
| 2468 | //===----------------------------------------------------------------------===// |
| 2469 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2470 | CanQualType ASTContext::getCanonicalParamType(QualType T) { |
| 2471 | // Push qualifiers into arrays, and then discard any remaining |
| 2472 | // qualifiers. |
| 2473 | T = getCanonicalType(T); |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2474 | T = getVariableArrayDecayedType(T); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2475 | const Type *Ty = T.getTypePtr(); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2476 | QualType Result; |
| 2477 | if (isa<ArrayType>(Ty)) { |
| 2478 | Result = getArrayDecayedType(QualType(Ty,0)); |
| 2479 | } else if (isa<FunctionType>(Ty)) { |
| 2480 | Result = getPointerType(QualType(Ty, 0)); |
| 2481 | } else { |
| 2482 | Result = QualType(Ty, 0); |
| 2483 | } |
| 2484 | |
| 2485 | return CanQualType::CreateUnsafe(Result); |
| 2486 | } |
| 2487 | |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 2488 | /// getCanonicalType - Return the canonical (structural) type corresponding to |
| 2489 | /// the specified potentially non-canonical type. The non-canonical version |
| 2490 | /// of a type may have many "decorated" versions of types. Decorators can |
| 2491 | /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed |
| 2492 | /// to be free of any of these, allowing two canonical types to be compared |
| 2493 | /// for exact equality with a simple pointer comparison. |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2494 | CanQualType ASTContext::getCanonicalType(QualType T) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2495 | QualifierCollector Quals; |
| 2496 | const Type *Ptr = Quals.strip(T); |
| 2497 | QualType CanType = Ptr->getCanonicalTypeInternal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2498 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2499 | // The canonical internal type will be the canonical type *except* |
| 2500 | // that we push type qualifiers down through array types. |
| 2501 | |
| 2502 | // If there are no new qualifiers to push down, stop here. |
| 2503 | if (!Quals.hasQualifiers()) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2504 | return CanQualType::CreateUnsafe(CanType); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2505 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2506 | // If the type qualifiers are on an array type, get the canonical |
| 2507 | // type of the array with the qualifiers applied to the element |
| 2508 | // type. |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2509 | ArrayType *AT = dyn_cast<ArrayType>(CanType); |
| 2510 | if (!AT) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2511 | return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2512 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2513 | // Get the canonical version of the element with the extra qualifiers on it. |
| 2514 | // This can recursively sink qualifiers through multiple levels of arrays. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2515 | QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2516 | NewEltTy = getCanonicalType(NewEltTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2517 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2518 | if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2519 | return CanQualType::CreateUnsafe( |
| 2520 | getConstantArrayType(NewEltTy, CAT->getSize(), |
| 2521 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2522 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2523 | if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2524 | return CanQualType::CreateUnsafe( |
| 2525 | getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2526 | IAT->getIndexTypeCVRQualifiers())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2527 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2528 | if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2529 | return CanQualType::CreateUnsafe( |
| 2530 | getDependentSizedArrayType(NewEltTy, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 2531 | DSAT->getSizeExpr(), |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2532 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2533 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 87a924e | 2009-10-30 22:56:57 +0000 | [diff] [blame] | 2534 | DSAT->getBracketsRange())->getCanonicalTypeInternal()); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2535 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2536 | VariableArrayType *VAT = cast<VariableArrayType>(AT); |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2537 | return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 2538 | VAT->getSizeExpr(), |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2539 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2540 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2541 | VAT->getBracketsRange())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2542 | } |
| 2543 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2544 | QualType ASTContext::getUnqualifiedArrayType(QualType T, |
| 2545 | Qualifiers &Quals) { |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2546 | Quals = T.getQualifiers(); |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2547 | const ArrayType *AT = getAsArrayType(T); |
| 2548 | if (!AT) { |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2549 | return T.getUnqualifiedType(); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2550 | } |
| 2551 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2552 | QualType Elt = AT->getElementType(); |
Zhongxing Xu | c1ae0a8 | 2010-01-05 08:15:06 +0000 | [diff] [blame] | 2553 | QualType UnqualElt = getUnqualifiedArrayType(Elt, Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2554 | if (Elt == UnqualElt) |
| 2555 | return T; |
| 2556 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2557 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2558 | return getConstantArrayType(UnqualElt, CAT->getSize(), |
| 2559 | CAT->getSizeModifier(), 0); |
| 2560 | } |
| 2561 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2562 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2563 | return getIncompleteArrayType(UnqualElt, IAT->getSizeModifier(), 0); |
| 2564 | } |
| 2565 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2566 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) { |
| 2567 | return getVariableArrayType(UnqualElt, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 2568 | VAT->getSizeExpr(), |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2569 | VAT->getSizeModifier(), |
| 2570 | VAT->getIndexTypeCVRQualifiers(), |
| 2571 | VAT->getBracketsRange()); |
| 2572 | } |
| 2573 | |
| 2574 | const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT); |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 2575 | return getDependentSizedArrayType(UnqualElt, DSAT->getSizeExpr(), |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2576 | DSAT->getSizeModifier(), 0, |
| 2577 | SourceRange()); |
| 2578 | } |
| 2579 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2580 | /// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that |
| 2581 | /// may be similar (C++ 4.4), replaces T1 and T2 with the type that |
| 2582 | /// they point to and return true. If T1 and T2 aren't pointer types |
| 2583 | /// or pointer-to-member types, or if they are not similar at this |
| 2584 | /// level, returns false and leaves T1 and T2 unchanged. Top-level |
| 2585 | /// qualifiers on T1 and T2 are ignored. This function will typically |
| 2586 | /// be called in a loop that successively "unwraps" pointer and |
| 2587 | /// pointer-to-member types to compare them at each level. |
| 2588 | bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) { |
| 2589 | const PointerType *T1PtrType = T1->getAs<PointerType>(), |
| 2590 | *T2PtrType = T2->getAs<PointerType>(); |
| 2591 | if (T1PtrType && T2PtrType) { |
| 2592 | T1 = T1PtrType->getPointeeType(); |
| 2593 | T2 = T2PtrType->getPointeeType(); |
| 2594 | return true; |
| 2595 | } |
| 2596 | |
| 2597 | const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(), |
| 2598 | *T2MPType = T2->getAs<MemberPointerType>(); |
| 2599 | if (T1MPType && T2MPType && |
| 2600 | hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0), |
| 2601 | QualType(T2MPType->getClass(), 0))) { |
| 2602 | T1 = T1MPType->getPointeeType(); |
| 2603 | T2 = T2MPType->getPointeeType(); |
| 2604 | return true; |
| 2605 | } |
| 2606 | |
| 2607 | if (getLangOptions().ObjC1) { |
| 2608 | const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(), |
| 2609 | *T2OPType = T2->getAs<ObjCObjectPointerType>(); |
| 2610 | if (T1OPType && T2OPType) { |
| 2611 | T1 = T1OPType->getPointeeType(); |
| 2612 | T2 = T2OPType->getPointeeType(); |
| 2613 | return true; |
| 2614 | } |
| 2615 | } |
| 2616 | |
| 2617 | // FIXME: Block pointers, too? |
| 2618 | |
| 2619 | return false; |
| 2620 | } |
| 2621 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2622 | DeclarationNameInfo ASTContext::getNameForTemplate(TemplateName Name, |
| 2623 | SourceLocation NameLoc) { |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2624 | if (TemplateDecl *TD = Name.getAsTemplateDecl()) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2625 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 2626 | return DeclarationNameInfo(TD->getDeclName(), NameLoc); |
| 2627 | |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2628 | if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2629 | DeclarationName DName; |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2630 | if (DTN->isIdentifier()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2631 | DName = DeclarationNames.getIdentifier(DTN->getIdentifier()); |
| 2632 | return DeclarationNameInfo(DName, NameLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2633 | } else { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2634 | DName = DeclarationNames.getCXXOperatorName(DTN->getOperator()); |
| 2635 | // DNInfo work in progress: FIXME: source locations? |
| 2636 | DeclarationNameLoc DNLoc; |
| 2637 | DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding(); |
| 2638 | DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding(); |
| 2639 | return DeclarationNameInfo(DName, NameLoc, DNLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2640 | } |
| 2641 | } |
| 2642 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 2643 | OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate(); |
| 2644 | assert(Storage); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2645 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 2646 | return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2647 | } |
| 2648 | |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2649 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) { |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 2650 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 2651 | if (TemplateTemplateParmDecl *TTP |
| 2652 | = dyn_cast<TemplateTemplateParmDecl>(Template)) |
| 2653 | Template = getCanonicalTemplateTemplateParmDecl(TTP); |
| 2654 | |
| 2655 | // The canonical template name is the canonical template declaration. |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 2656 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 2657 | } |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2658 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 2659 | assert(!Name.getAsOverloadedTemplate()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2660 | |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2661 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 2662 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 2663 | return DTN->CanonicalTemplateName; |
| 2664 | } |
| 2665 | |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 2666 | bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) { |
| 2667 | X = getCanonicalTemplateName(X); |
| 2668 | Y = getCanonicalTemplateName(Y); |
| 2669 | return X.getAsVoidPointer() == Y.getAsVoidPointer(); |
| 2670 | } |
| 2671 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2672 | TemplateArgument |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2673 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) { |
| 2674 | switch (Arg.getKind()) { |
| 2675 | case TemplateArgument::Null: |
| 2676 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2677 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2678 | case TemplateArgument::Expression: |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2679 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2680 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2681 | case TemplateArgument::Declaration: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2682 | return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2683 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2684 | case TemplateArgument::Template: |
| 2685 | return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate())); |
| 2686 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2687 | case TemplateArgument::Integral: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2688 | return TemplateArgument(*Arg.getAsIntegral(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2689 | getCanonicalType(Arg.getIntegralType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2690 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2691 | case TemplateArgument::Type: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2692 | return TemplateArgument(getCanonicalType(Arg.getAsType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2693 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2694 | case TemplateArgument::Pack: { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2695 | TemplateArgument *CanonArgs |
| 2696 | = new (*this) TemplateArgument[Arg.pack_size()]; |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2697 | unsigned Idx = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2698 | for (TemplateArgument::pack_iterator A = Arg.pack_begin(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2699 | AEnd = Arg.pack_end(); |
| 2700 | A != AEnd; (void)++A, ++Idx) |
| 2701 | CanonArgs[Idx] = getCanonicalTemplateArgument(*A); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2702 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2703 | return TemplateArgument(CanonArgs, Arg.pack_size()); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2704 | } |
| 2705 | } |
| 2706 | |
| 2707 | // Silence GCC warning |
| 2708 | assert(false && "Unhandled template argument kind"); |
| 2709 | return TemplateArgument(); |
| 2710 | } |
| 2711 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2712 | NestedNameSpecifier * |
| 2713 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2714 | if (!NNS) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2715 | return 0; |
| 2716 | |
| 2717 | switch (NNS->getKind()) { |
| 2718 | case NestedNameSpecifier::Identifier: |
| 2719 | // Canonicalize the prefix but keep the identifier the same. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2720 | return NestedNameSpecifier::Create(*this, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2721 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 2722 | NNS->getAsIdentifier()); |
| 2723 | |
| 2724 | case NestedNameSpecifier::Namespace: |
| 2725 | // A namespace is canonical; build a nested-name-specifier with |
| 2726 | // this namespace and no prefix. |
| 2727 | return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace()); |
| 2728 | |
| 2729 | case NestedNameSpecifier::TypeSpec: |
| 2730 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 2731 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 2732 | |
| 2733 | // If we have some kind of dependent-named type (e.g., "typename T::type"), |
| 2734 | // break it apart into its prefix and identifier, then reconsititute those |
| 2735 | // as the canonical nested-name-specifier. This is required to canonicalize |
| 2736 | // a dependent nested-name-specifier involving typedefs of dependent-name |
| 2737 | // types, e.g., |
| 2738 | // typedef typename T::type T1; |
| 2739 | // typedef typename T1::type T2; |
| 2740 | if (const DependentNameType *DNT = T->getAs<DependentNameType>()) { |
| 2741 | NestedNameSpecifier *Prefix |
| 2742 | = getCanonicalNestedNameSpecifier(DNT->getQualifier()); |
| 2743 | return NestedNameSpecifier::Create(*this, Prefix, |
| 2744 | const_cast<IdentifierInfo *>(DNT->getIdentifier())); |
| 2745 | } |
| 2746 | |
Douglas Gregor | 643f843 | 2010-11-04 00:14:23 +0000 | [diff] [blame] | 2747 | // Do the same thing as above, but with dependent-named specializations. |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 2748 | if (const DependentTemplateSpecializationType *DTST |
| 2749 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 2750 | NestedNameSpecifier *Prefix |
| 2751 | = getCanonicalNestedNameSpecifier(DTST->getQualifier()); |
| 2752 | TemplateName Name |
| 2753 | = getDependentTemplateName(Prefix, DTST->getIdentifier()); |
| 2754 | T = getTemplateSpecializationType(Name, |
| 2755 | DTST->getArgs(), DTST->getNumArgs()); |
| 2756 | T = getCanonicalType(T); |
| 2757 | } |
| 2758 | |
| 2759 | return NestedNameSpecifier::Create(*this, 0, false, T.getTypePtr()); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2760 | } |
| 2761 | |
| 2762 | case NestedNameSpecifier::Global: |
| 2763 | // The global specifier is canonical and unique. |
| 2764 | return NNS; |
| 2765 | } |
| 2766 | |
| 2767 | // Required to silence a GCC warning |
| 2768 | return 0; |
| 2769 | } |
| 2770 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2771 | |
| 2772 | const ArrayType *ASTContext::getAsArrayType(QualType T) { |
| 2773 | // Handle the non-qualified case efficiently. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2774 | if (!T.hasLocalQualifiers()) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2775 | // Handle the common positive case fast. |
| 2776 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 2777 | return AT; |
| 2778 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2779 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2780 | // Handle the common negative case fast. |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2781 | QualType CType = T->getCanonicalTypeInternal(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2782 | if (!isa<ArrayType>(CType)) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2783 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2784 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2785 | // Apply any qualifiers from the array type to the element type. This |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2786 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 2787 | // 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] | 2788 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2789 | // If we get here, we either have type qualifiers on the type, or we have |
| 2790 | // 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] | 2791 | // we must propagate them down into the element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2792 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2793 | QualifierCollector Qs; |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame^] | 2794 | const Type *Ty = Qs.strip(T.getDesugaredType(*this)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2795 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2796 | // If we have a simple case, just return now. |
| 2797 | const ArrayType *ATy = dyn_cast<ArrayType>(Ty); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2798 | if (ATy == 0 || Qs.empty()) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2799 | return ATy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2800 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2801 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 2802 | // qualifiers into the array element type and return a new array type. |
| 2803 | // Get the canonical version of the element with the extra qualifiers on it. |
| 2804 | // This can recursively sink qualifiers through multiple levels of arrays. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2805 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2806 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2807 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 2808 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 2809 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2810 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2811 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 2812 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 2813 | IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2814 | IAT->getIndexTypeCVRQualifiers())); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2815 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2816 | if (const DependentSizedArrayType *DSAT |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2817 | = dyn_cast<DependentSizedArrayType>(ATy)) |
| 2818 | return cast<ArrayType>( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2819 | getDependentSizedArrayType(NewEltTy, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 2820 | DSAT->getSizeExpr(), |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2821 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2822 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2823 | DSAT->getBracketsRange())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2824 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2825 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2826 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 2827 | VAT->getSizeExpr(), |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2828 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2829 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2830 | VAT->getBracketsRange())); |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 2831 | } |
| 2832 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2833 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 2834 | /// specified array type to a pointer. This operation is non-trivial when |
| 2835 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 2836 | /// this returns a pointer to a properly qualified element of the array. |
| 2837 | /// |
| 2838 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
| 2839 | QualType ASTContext::getArrayDecayedType(QualType Ty) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2840 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 2841 | // typedefs in the element type of the array. This also handles propagation |
| 2842 | // of type qualifiers from the array type into the element type if present |
| 2843 | // (C99 6.7.3p8). |
| 2844 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 2845 | assert(PrettyArrayType && "Not an array type!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2846 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2847 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2848 | |
| 2849 | // int x[restrict 4] -> int *restrict |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2850 | return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2851 | } |
| 2852 | |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2853 | QualType ASTContext::getBaseElementType(QualType QT) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2854 | QualifierCollector Qs; |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 2855 | while (const ArrayType *AT = getAsArrayType(QualType(Qs.strip(QT), 0))) |
| 2856 | QT = AT->getElementType(); |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame^] | 2857 | return Qs.apply(*this, QT); |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2858 | } |
| 2859 | |
Anders Carlsson | fbbce49 | 2009-09-25 01:23:32 +0000 | [diff] [blame] | 2860 | QualType ASTContext::getBaseElementType(const ArrayType *AT) { |
| 2861 | QualType ElemTy = AT->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2862 | |
Anders Carlsson | fbbce49 | 2009-09-25 01:23:32 +0000 | [diff] [blame] | 2863 | if (const ArrayType *AT = getAsArrayType(ElemTy)) |
| 2864 | return getBaseElementType(AT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2865 | |
Anders Carlsson | 6183a99 | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 2866 | return ElemTy; |
| 2867 | } |
| 2868 | |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 2869 | /// getConstantArrayElementCount - Returns number of constant array elements. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2870 | uint64_t |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 2871 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
| 2872 | uint64_t ElementCount = 1; |
| 2873 | do { |
| 2874 | ElementCount *= CA->getSize().getZExtValue(); |
| 2875 | CA = dyn_cast<ConstantArrayType>(CA->getElementType()); |
| 2876 | } while (CA); |
| 2877 | return ElementCount; |
| 2878 | } |
| 2879 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2880 | /// getFloatingRank - Return a relative rank for floating point types. |
| 2881 | /// 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] | 2882 | static FloatingRank getFloatingRank(QualType T) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2883 | if (const ComplexType *CT = T->getAs<ComplexType>()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2884 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2885 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2886 | assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"); |
| 2887 | switch (T->getAs<BuiltinType>()->getKind()) { |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2888 | default: assert(0 && "getFloatingRank(): not a floating type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2889 | case BuiltinType::Float: return FloatRank; |
| 2890 | case BuiltinType::Double: return DoubleRank; |
| 2891 | case BuiltinType::LongDouble: return LongDoubleRank; |
| 2892 | } |
| 2893 | } |
| 2894 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2895 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 2896 | /// point or a complex type (based on typeDomain/typeSize). |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 2897 | /// 'typeDomain' is a real floating point or complex type. |
| 2898 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 2899 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 2900 | QualType Domain) const { |
| 2901 | FloatingRank EltRank = getFloatingRank(Size); |
| 2902 | if (Domain->isComplexType()) { |
| 2903 | switch (EltRank) { |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 2904 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 2905 | case FloatRank: return FloatComplexTy; |
| 2906 | case DoubleRank: return DoubleComplexTy; |
| 2907 | case LongDoubleRank: return LongDoubleComplexTy; |
| 2908 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2909 | } |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 2910 | |
| 2911 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 2912 | switch (EltRank) { |
| 2913 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
| 2914 | case FloatRank: return FloatTy; |
| 2915 | case DoubleRank: return DoubleTy; |
| 2916 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 2917 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2918 | } |
| 2919 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2920 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 2921 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 2922 | /// '_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] | 2923 | /// LHS < RHS, return -1. |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2924 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) { |
| 2925 | FloatingRank LHSR = getFloatingRank(LHS); |
| 2926 | FloatingRank RHSR = getFloatingRank(RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2927 | |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2928 | if (LHSR == RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 2929 | return 0; |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2930 | if (LHSR > RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 2931 | return 1; |
| 2932 | return -1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2933 | } |
| 2934 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2935 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 2936 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 2937 | /// or if it is not canonicalized. |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2938 | unsigned ASTContext::getIntegerRank(Type *T) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2939 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2940 | if (EnumType* ET = dyn_cast<EnumType>(T)) |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 2941 | T = ET->getDecl()->getPromotionType().getTypePtr(); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2942 | |
Eli Friedman | a342675 | 2009-07-05 23:44:27 +0000 | [diff] [blame] | 2943 | if (T->isSpecificBuiltinType(BuiltinType::WChar)) |
| 2944 | T = getFromTargetType(Target.getWCharType()).getTypePtr(); |
| 2945 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2946 | if (T->isSpecificBuiltinType(BuiltinType::Char16)) |
| 2947 | T = getFromTargetType(Target.getChar16Type()).getTypePtr(); |
| 2948 | |
| 2949 | if (T->isSpecificBuiltinType(BuiltinType::Char32)) |
| 2950 | T = getFromTargetType(Target.getChar32Type()).getTypePtr(); |
| 2951 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2952 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2953 | default: assert(0 && "getIntegerRank(): not a built-in integer"); |
| 2954 | case BuiltinType::Bool: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2955 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2956 | case BuiltinType::Char_S: |
| 2957 | case BuiltinType::Char_U: |
| 2958 | case BuiltinType::SChar: |
| 2959 | case BuiltinType::UChar: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2960 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2961 | case BuiltinType::Short: |
| 2962 | case BuiltinType::UShort: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2963 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2964 | case BuiltinType::Int: |
| 2965 | case BuiltinType::UInt: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2966 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2967 | case BuiltinType::Long: |
| 2968 | case BuiltinType::ULong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2969 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2970 | case BuiltinType::LongLong: |
| 2971 | case BuiltinType::ULongLong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2972 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 2973 | case BuiltinType::Int128: |
| 2974 | case BuiltinType::UInt128: |
| 2975 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2976 | } |
| 2977 | } |
| 2978 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 2979 | /// \brief Whether this is a promotable bitfield reference according |
| 2980 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
| 2981 | /// |
| 2982 | /// \returns the type this bit-field will promote to, or NULL if no |
| 2983 | /// promotion occurs. |
| 2984 | QualType ASTContext::isPromotableBitField(Expr *E) { |
Douglas Gregor | ceafbde | 2010-05-24 20:13:53 +0000 | [diff] [blame] | 2985 | if (E->isTypeDependent() || E->isValueDependent()) |
| 2986 | return QualType(); |
| 2987 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 2988 | FieldDecl *Field = E->getBitField(); |
| 2989 | if (!Field) |
| 2990 | return QualType(); |
| 2991 | |
| 2992 | QualType FT = Field->getType(); |
| 2993 | |
| 2994 | llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this); |
| 2995 | uint64_t BitWidth = BitWidthAP.getZExtValue(); |
| 2996 | uint64_t IntSize = getTypeSize(IntTy); |
| 2997 | // GCC extension compatibility: if the bit-field size is less than or equal |
| 2998 | // to the size of int, it gets promoted no matter what its type is. |
| 2999 | // For instance, unsigned long bf : 4 gets promoted to signed int. |
| 3000 | if (BitWidth < IntSize) |
| 3001 | return IntTy; |
| 3002 | |
| 3003 | if (BitWidth == IntSize) |
| 3004 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
| 3005 | |
| 3006 | // Types bigger than int are not subject to promotions, and therefore act |
| 3007 | // like the base type. |
| 3008 | // FIXME: This doesn't quite match what gcc does, but what gcc does here |
| 3009 | // is ridiculous. |
| 3010 | return QualType(); |
| 3011 | } |
| 3012 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3013 | /// getPromotedIntegerType - Returns the type that Promotable will |
| 3014 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
| 3015 | /// integer type. |
| 3016 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) { |
| 3017 | assert(!Promotable.isNull()); |
| 3018 | assert(Promotable->isPromotableIntegerType()); |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 3019 | if (const EnumType *ET = Promotable->getAs<EnumType>()) |
| 3020 | return ET->getDecl()->getPromotionType(); |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3021 | if (Promotable->isSignedIntegerType()) |
| 3022 | return IntTy; |
| 3023 | uint64_t PromotableSize = getTypeSize(Promotable); |
| 3024 | uint64_t IntSize = getTypeSize(IntTy); |
| 3025 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
| 3026 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
| 3027 | } |
| 3028 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3029 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3030 | /// 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] | 3031 | /// LHS < RHS, return -1. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3032 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3033 | Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 3034 | Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3035 | if (LHSC == RHSC) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3036 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3037 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 3038 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3039 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3040 | unsigned LHSRank = getIntegerRank(LHSC); |
| 3041 | unsigned RHSRank = getIntegerRank(RHSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3042 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3043 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 3044 | if (LHSRank == RHSRank) return 0; |
| 3045 | return LHSRank > RHSRank ? 1 : -1; |
| 3046 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3047 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3048 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 3049 | if (LHSUnsigned) { |
| 3050 | // If the unsigned [LHS] type is larger, return it. |
| 3051 | if (LHSRank >= RHSRank) |
| 3052 | return 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3053 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3054 | // If the signed type can represent all values of the unsigned type, it |
| 3055 | // 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] | 3056 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3057 | return -1; |
| 3058 | } |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3059 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3060 | // If the unsigned [RHS] type is larger, return it. |
| 3061 | if (RHSRank >= LHSRank) |
| 3062 | return -1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3063 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3064 | // If the signed type can represent all values of the unsigned type, it |
| 3065 | // 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] | 3066 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3067 | return 1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3068 | } |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3069 | |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3070 | static RecordDecl * |
| 3071 | CreateRecordDecl(ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC, |
| 3072 | SourceLocation L, IdentifierInfo *Id) { |
| 3073 | if (Ctx.getLangOptions().CPlusPlus) |
| 3074 | return CXXRecordDecl::Create(Ctx, TK, DC, L, Id); |
| 3075 | else |
| 3076 | return RecordDecl::Create(Ctx, TK, DC, L, Id); |
| 3077 | } |
| 3078 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3079 | // getCFConstantStringType - Return the type used for constant CFStrings. |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3080 | QualType ASTContext::getCFConstantStringType() { |
| 3081 | if (!CFConstantStringTypeDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3082 | CFConstantStringTypeDecl = |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3083 | CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3084 | &Idents.get("NSConstantString")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3085 | CFConstantStringTypeDecl->startDefinition(); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3086 | |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 3087 | QualType FieldTypes[4]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3088 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3089 | // const int *isa; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3090 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 3091 | // int flags; |
| 3092 | FieldTypes[1] = IntTy; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3093 | // const char *str; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3094 | FieldTypes[2] = getPointerType(CharTy.withConst()); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3095 | // long length; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3096 | FieldTypes[3] = LongTy; |
| 3097 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3098 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3099 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3100 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3101 | SourceLocation(), 0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3102 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3103 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 3104 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3105 | Field->setAccess(AS_public); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3106 | CFConstantStringTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3107 | } |
| 3108 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3109 | CFConstantStringTypeDecl->completeDefinition(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3110 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3111 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3112 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 8467583 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 3113 | } |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 3114 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3115 | void ASTContext::setCFConstantStringType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3116 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3117 | assert(Rec && "Invalid CFConstantStringType"); |
| 3118 | CFConstantStringTypeDecl = Rec->getDecl(); |
| 3119 | } |
| 3120 | |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 3121 | // getNSConstantStringType - Return the type used for constant NSStrings. |
| 3122 | QualType ASTContext::getNSConstantStringType() { |
| 3123 | if (!NSConstantStringTypeDecl) { |
| 3124 | NSConstantStringTypeDecl = |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3125 | CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 3126 | &Idents.get("__builtin_NSString")); |
| 3127 | NSConstantStringTypeDecl->startDefinition(); |
| 3128 | |
| 3129 | QualType FieldTypes[3]; |
| 3130 | |
| 3131 | // const int *isa; |
| 3132 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
| 3133 | // const char *str; |
| 3134 | FieldTypes[1] = getPointerType(CharTy.withConst()); |
| 3135 | // unsigned int length; |
| 3136 | FieldTypes[2] = UnsignedIntTy; |
| 3137 | |
| 3138 | // Create fields |
| 3139 | for (unsigned i = 0; i < 3; ++i) { |
| 3140 | FieldDecl *Field = FieldDecl::Create(*this, NSConstantStringTypeDecl, |
| 3141 | SourceLocation(), 0, |
| 3142 | FieldTypes[i], /*TInfo=*/0, |
| 3143 | /*BitWidth=*/0, |
| 3144 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3145 | Field->setAccess(AS_public); |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 3146 | NSConstantStringTypeDecl->addDecl(Field); |
| 3147 | } |
| 3148 | |
| 3149 | NSConstantStringTypeDecl->completeDefinition(); |
| 3150 | } |
| 3151 | |
| 3152 | return getTagDeclType(NSConstantStringTypeDecl); |
| 3153 | } |
| 3154 | |
| 3155 | void ASTContext::setNSConstantStringType(QualType T) { |
| 3156 | const RecordType *Rec = T->getAs<RecordType>(); |
| 3157 | assert(Rec && "Invalid NSConstantStringType"); |
| 3158 | NSConstantStringTypeDecl = Rec->getDecl(); |
| 3159 | } |
| 3160 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3161 | QualType ASTContext::getObjCFastEnumerationStateType() { |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3162 | if (!ObjCFastEnumerationStateTypeDecl) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3163 | ObjCFastEnumerationStateTypeDecl = |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3164 | CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3165 | &Idents.get("__objcFastEnumerationState")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3166 | ObjCFastEnumerationStateTypeDecl->startDefinition(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3167 | |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3168 | QualType FieldTypes[] = { |
| 3169 | UnsignedLongTy, |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3170 | getPointerType(ObjCIdTypedefType), |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3171 | getPointerType(UnsignedLongTy), |
| 3172 | getConstantArrayType(UnsignedLongTy, |
| 3173 | llvm::APInt(32, 5), ArrayType::Normal, 0) |
| 3174 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3175 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3176 | for (size_t i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3177 | FieldDecl *Field = FieldDecl::Create(*this, |
| 3178 | ObjCFastEnumerationStateTypeDecl, |
| 3179 | SourceLocation(), 0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3180 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3181 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 3182 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3183 | Field->setAccess(AS_public); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3184 | ObjCFastEnumerationStateTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3185 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3186 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3187 | ObjCFastEnumerationStateTypeDecl->completeDefinition(); |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3188 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3189 | |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 3190 | return getTagDeclType(ObjCFastEnumerationStateTypeDecl); |
| 3191 | } |
| 3192 | |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3193 | QualType ASTContext::getBlockDescriptorType() { |
| 3194 | if (BlockDescriptorType) |
| 3195 | return getTagDeclType(BlockDescriptorType); |
| 3196 | |
| 3197 | RecordDecl *T; |
| 3198 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3199 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3200 | &Idents.get("__block_descriptor")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3201 | T->startDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3202 | |
| 3203 | QualType FieldTypes[] = { |
| 3204 | UnsignedLongTy, |
| 3205 | UnsignedLongTy, |
| 3206 | }; |
| 3207 | |
| 3208 | const char *FieldNames[] = { |
| 3209 | "reserved", |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3210 | "Size" |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3211 | }; |
| 3212 | |
| 3213 | for (size_t i = 0; i < 2; ++i) { |
| 3214 | FieldDecl *Field = FieldDecl::Create(*this, |
| 3215 | T, |
| 3216 | SourceLocation(), |
| 3217 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3218 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3219 | /*BitWidth=*/0, |
| 3220 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3221 | Field->setAccess(AS_public); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3222 | T->addDecl(Field); |
| 3223 | } |
| 3224 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3225 | T->completeDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3226 | |
| 3227 | BlockDescriptorType = T; |
| 3228 | |
| 3229 | return getTagDeclType(BlockDescriptorType); |
| 3230 | } |
| 3231 | |
| 3232 | void ASTContext::setBlockDescriptorType(QualType T) { |
| 3233 | const RecordType *Rec = T->getAs<RecordType>(); |
| 3234 | assert(Rec && "Invalid BlockDescriptorType"); |
| 3235 | BlockDescriptorType = Rec->getDecl(); |
| 3236 | } |
| 3237 | |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3238 | QualType ASTContext::getBlockDescriptorExtendedType() { |
| 3239 | if (BlockDescriptorExtendedType) |
| 3240 | return getTagDeclType(BlockDescriptorExtendedType); |
| 3241 | |
| 3242 | RecordDecl *T; |
| 3243 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3244 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3245 | &Idents.get("__block_descriptor_withcopydispose")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3246 | T->startDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3247 | |
| 3248 | QualType FieldTypes[] = { |
| 3249 | UnsignedLongTy, |
| 3250 | UnsignedLongTy, |
| 3251 | getPointerType(VoidPtrTy), |
| 3252 | getPointerType(VoidPtrTy) |
| 3253 | }; |
| 3254 | |
| 3255 | const char *FieldNames[] = { |
| 3256 | "reserved", |
| 3257 | "Size", |
| 3258 | "CopyFuncPtr", |
| 3259 | "DestroyFuncPtr" |
| 3260 | }; |
| 3261 | |
| 3262 | for (size_t i = 0; i < 4; ++i) { |
| 3263 | FieldDecl *Field = FieldDecl::Create(*this, |
| 3264 | T, |
| 3265 | SourceLocation(), |
| 3266 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3267 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3268 | /*BitWidth=*/0, |
| 3269 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3270 | Field->setAccess(AS_public); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3271 | T->addDecl(Field); |
| 3272 | } |
| 3273 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3274 | T->completeDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3275 | |
| 3276 | BlockDescriptorExtendedType = T; |
| 3277 | |
| 3278 | return getTagDeclType(BlockDescriptorExtendedType); |
| 3279 | } |
| 3280 | |
| 3281 | void ASTContext::setBlockDescriptorExtendedType(QualType T) { |
| 3282 | const RecordType *Rec = T->getAs<RecordType>(); |
| 3283 | assert(Rec && "Invalid BlockDescriptorType"); |
| 3284 | BlockDescriptorExtendedType = Rec->getDecl(); |
| 3285 | } |
| 3286 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3287 | bool ASTContext::BlockRequiresCopying(QualType Ty) { |
| 3288 | if (Ty->isBlockPointerType()) |
| 3289 | return true; |
| 3290 | if (isObjCNSObjectType(Ty)) |
| 3291 | return true; |
| 3292 | if (Ty->isObjCObjectPointerType()) |
| 3293 | return true; |
Fariborz Jahanian | e38be61 | 2010-11-17 00:21:28 +0000 | [diff] [blame] | 3294 | if (getLangOptions().CPlusPlus) { |
| 3295 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3296 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 3297 | return RD->hasConstCopyConstructor(*this); |
| 3298 | |
| 3299 | } |
| 3300 | } |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3301 | return false; |
| 3302 | } |
| 3303 | |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3304 | QualType ASTContext::BuildByRefType(llvm::StringRef DeclName, QualType Ty) { |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3305 | // type = struct __Block_byref_1_X { |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3306 | // void *__isa; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3307 | // struct __Block_byref_1_X *__forwarding; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3308 | // unsigned int __flags; |
| 3309 | // unsigned int __size; |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 3310 | // void *__copy_helper; // as needed |
| 3311 | // void *__destroy_help // as needed |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3312 | // int X; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3313 | // } * |
| 3314 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3315 | bool HasCopyAndDispose = BlockRequiresCopying(Ty); |
| 3316 | |
| 3317 | // FIXME: Move up |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 3318 | llvm::SmallString<36> Name; |
| 3319 | llvm::raw_svector_ostream(Name) << "__Block_byref_" << |
| 3320 | ++UniqueBlockByRefTypeID << '_' << DeclName; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3321 | RecordDecl *T; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3322 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3323 | &Idents.get(Name.str())); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3324 | T->startDefinition(); |
| 3325 | QualType Int32Ty = IntTy; |
| 3326 | assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported"); |
| 3327 | QualType FieldTypes[] = { |
| 3328 | getPointerType(VoidPtrTy), |
| 3329 | getPointerType(getTagDeclType(T)), |
| 3330 | Int32Ty, |
| 3331 | Int32Ty, |
| 3332 | getPointerType(VoidPtrTy), |
| 3333 | getPointerType(VoidPtrTy), |
| 3334 | Ty |
| 3335 | }; |
| 3336 | |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3337 | llvm::StringRef FieldNames[] = { |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3338 | "__isa", |
| 3339 | "__forwarding", |
| 3340 | "__flags", |
| 3341 | "__size", |
| 3342 | "__copy_helper", |
| 3343 | "__destroy_helper", |
| 3344 | DeclName, |
| 3345 | }; |
| 3346 | |
| 3347 | for (size_t i = 0; i < 7; ++i) { |
| 3348 | if (!HasCopyAndDispose && i >=4 && i <= 5) |
| 3349 | continue; |
| 3350 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
| 3351 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3352 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3353 | /*BitWidth=*/0, /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3354 | Field->setAccess(AS_public); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3355 | T->addDecl(Field); |
| 3356 | } |
| 3357 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3358 | T->completeDefinition(); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3359 | |
| 3360 | return getPointerType(getTagDeclType(T)); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3361 | } |
| 3362 | |
| 3363 | |
| 3364 | QualType ASTContext::getBlockParmType( |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3365 | bool BlockHasCopyDispose, |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 3366 | llvm::SmallVectorImpl<const Expr *> &Layout) { |
| 3367 | |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3368 | // FIXME: Move up |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 3369 | llvm::SmallString<36> Name; |
| 3370 | llvm::raw_svector_ostream(Name) << "__block_literal_" |
| 3371 | << ++UniqueBlockParmTypeID; |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3372 | RecordDecl *T; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3373 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3374 | &Idents.get(Name.str())); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3375 | T->startDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3376 | QualType FieldTypes[] = { |
| 3377 | getPointerType(VoidPtrTy), |
| 3378 | IntTy, |
| 3379 | IntTy, |
| 3380 | getPointerType(VoidPtrTy), |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3381 | (BlockHasCopyDispose ? |
| 3382 | getPointerType(getBlockDescriptorExtendedType()) : |
| 3383 | getPointerType(getBlockDescriptorType())) |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3384 | }; |
| 3385 | |
| 3386 | const char *FieldNames[] = { |
| 3387 | "__isa", |
| 3388 | "__flags", |
| 3389 | "__reserved", |
| 3390 | "__FuncPtr", |
| 3391 | "__descriptor" |
| 3392 | }; |
| 3393 | |
| 3394 | for (size_t i = 0; i < 5; ++i) { |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3395 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3396 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3397 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3398 | /*BitWidth=*/0, /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3399 | Field->setAccess(AS_public); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3400 | T->addDecl(Field); |
| 3401 | } |
| 3402 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 3403 | for (unsigned i = 0; i < Layout.size(); ++i) { |
| 3404 | const Expr *E = Layout[i]; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3405 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 3406 | QualType FieldType = E->getType(); |
| 3407 | IdentifierInfo *FieldName = 0; |
| 3408 | if (isa<CXXThisExpr>(E)) { |
| 3409 | FieldName = &Idents.get("this"); |
| 3410 | } else if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E)) { |
| 3411 | const ValueDecl *D = BDRE->getDecl(); |
| 3412 | FieldName = D->getIdentifier(); |
| 3413 | if (BDRE->isByRef()) |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3414 | FieldType = BuildByRefType(D->getName(), FieldType); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 3415 | } else { |
| 3416 | // Padding. |
| 3417 | assert(isa<ConstantArrayType>(FieldType) && |
| 3418 | isa<DeclRefExpr>(E) && |
| 3419 | !cast<DeclRefExpr>(E)->getDecl()->getDeclName() && |
| 3420 | "doesn't match characteristics of padding decl"); |
| 3421 | } |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3422 | |
| 3423 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 3424 | FieldName, FieldType, /*TInfo=*/0, |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3425 | /*BitWidth=*/0, /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3426 | Field->setAccess(AS_public); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3427 | T->addDecl(Field); |
| 3428 | } |
| 3429 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3430 | T->completeDefinition(); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3431 | |
| 3432 | return getPointerType(getTagDeclType(T)); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3433 | } |
| 3434 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3435 | void ASTContext::setObjCFastEnumerationStateType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3436 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3437 | assert(Rec && "Invalid ObjCFAstEnumerationStateType"); |
| 3438 | ObjCFastEnumerationStateTypeDecl = Rec->getDecl(); |
| 3439 | } |
| 3440 | |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3441 | // This returns true if a type has been typedefed to BOOL: |
| 3442 | // typedef <type> BOOL; |
Chris Lattner | 2d99833 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 3443 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3444 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | bb49c3e | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 3445 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 3446 | return II->isStr("BOOL"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3447 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3448 | return false; |
| 3449 | } |
| 3450 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3451 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3452 | /// purpose. |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3453 | CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) { |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3454 | CharUnits sz = getTypeSizeInChars(type); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3455 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3456 | // 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] | 3457 | if (sz.isPositive() && type->isIntegralOrEnumerationType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3458 | sz = std::max(sz, getTypeSizeInChars(IntTy)); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3459 | // Treat arrays as pointers, since that's how they're passed in. |
| 3460 | else if (type->isArrayType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3461 | sz = getTypeSizeInChars(VoidPtrTy); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3462 | return sz; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3463 | } |
| 3464 | |
| 3465 | static inline |
| 3466 | std::string charUnitsToString(const CharUnits &CU) { |
| 3467 | return llvm::itostr(CU.getQuantity()); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3468 | } |
| 3469 | |
Fariborz Jahanian | 6f46c26 | 2010-04-08 18:06:22 +0000 | [diff] [blame] | 3470 | /// getObjCEncodingForBlockDecl - Return the encoded type for this block |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3471 | /// declaration. |
| 3472 | void ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr, |
| 3473 | std::string& S) { |
| 3474 | const BlockDecl *Decl = Expr->getBlockDecl(); |
| 3475 | QualType BlockTy = |
| 3476 | Expr->getType()->getAs<BlockPointerType>()->getPointeeType(); |
| 3477 | // Encode result type. |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 3478 | getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3479 | // Compute size of all parameters. |
| 3480 | // Start with computing size of a pointer in number of bytes. |
| 3481 | // FIXME: There might(should) be a better way of doing this computation! |
| 3482 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3483 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
| 3484 | CharUnits ParmOffset = PtrSize; |
Fariborz Jahanian | 6f46c26 | 2010-04-08 18:06:22 +0000 | [diff] [blame] | 3485 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3486 | E = Decl->param_end(); PI != E; ++PI) { |
| 3487 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3488 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3489 | assert (sz.isPositive() && "BlockExpr - Incomplete param type"); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3490 | ParmOffset += sz; |
| 3491 | } |
| 3492 | // Size of the argument frame |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3493 | S += charUnitsToString(ParmOffset); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3494 | // Block pointer and offset. |
| 3495 | S += "@?0"; |
| 3496 | ParmOffset = PtrSize; |
| 3497 | |
| 3498 | // Argument types. |
| 3499 | ParmOffset = PtrSize; |
| 3500 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E = |
| 3501 | Decl->param_end(); PI != E; ++PI) { |
| 3502 | ParmVarDecl *PVDecl = *PI; |
| 3503 | QualType PType = PVDecl->getOriginalType(); |
| 3504 | if (const ArrayType *AT = |
| 3505 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3506 | // Use array's original type only if it has known number of |
| 3507 | // elements. |
| 3508 | if (!isa<ConstantArrayType>(AT)) |
| 3509 | PType = PVDecl->getType(); |
| 3510 | } else if (PType->isFunctionType()) |
| 3511 | PType = PVDecl->getType(); |
| 3512 | getObjCEncodingForType(PType, S); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3513 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3514 | ParmOffset += getObjCEncodingTypeSize(PType); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3515 | } |
| 3516 | } |
| 3517 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3518 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3519 | /// declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3520 | void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Chris Lattner | e6db3b0 | 2008-11-19 07:24:05 +0000 | [diff] [blame] | 3521 | std::string& S) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3522 | // FIXME: This is not very efficient. |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3523 | // Encode type qualifer, 'in', 'inout', etc. for the return type. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3524 | getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3525 | // Encode result type. |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3526 | getObjCEncodingForType(Decl->getResultType(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3527 | // Compute size of all parameters. |
| 3528 | // Start with computing size of a pointer in number of bytes. |
| 3529 | // FIXME: There might(should) be a better way of doing this computation! |
| 3530 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3531 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3532 | // The first two arguments (self and _cmd) are pointers; account for |
| 3533 | // their size. |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3534 | CharUnits ParmOffset = 2 * PtrSize; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3535 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 3536 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3537 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3538 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3539 | assert (sz.isPositive() && |
| 3540 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3541 | ParmOffset += sz; |
| 3542 | } |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3543 | S += charUnitsToString(ParmOffset); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3544 | S += "@0:"; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3545 | S += charUnitsToString(PtrSize); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3546 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3547 | // Argument types. |
| 3548 | ParmOffset = 2 * PtrSize; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3549 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 3550 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3551 | ParmVarDecl *PVDecl = *PI; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3552 | QualType PType = PVDecl->getOriginalType(); |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 3553 | if (const ArrayType *AT = |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 3554 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3555 | // Use array's original type only if it has known number of |
| 3556 | // elements. |
Steve Naroff | bb3fde3 | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 3557 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 3558 | PType = PVDecl->getType(); |
| 3559 | } else if (PType->isFunctionType()) |
| 3560 | PType = PVDecl->getType(); |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3561 | // Process argument qualifiers for user supplied arguments; such as, |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3562 | // 'in', 'inout', etc. |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 3563 | getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 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); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3567 | } |
| 3568 | } |
| 3569 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3570 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 3571 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3572 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 3573 | /// NULL when getting encodings for protocol properties. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3574 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 3575 | /// attributes readonly and bycopy are encoded as single characters. The |
| 3576 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 3577 | /// encoded as single characters, followed by an identifier. Property types |
| 3578 | /// are also encoded as a parametrized attribute. The characters used to encode |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 3579 | /// these attributes are defined by the following enumeration: |
| 3580 | /// @code |
| 3581 | /// enum PropertyAttributes { |
| 3582 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 3583 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 3584 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 3585 | /// kPropertyDynamic = 'D', // property is dynamic |
| 3586 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 3587 | /// kPropertySetter = 'S', // followed by setter selector name |
| 3588 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
| 3589 | /// kPropertyType = 't' // followed by old-style type encoding. |
| 3590 | /// kPropertyWeak = 'W' // 'weak' property |
| 3591 | /// kPropertyStrong = 'P' // property GC'able |
| 3592 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 3593 | /// }; |
| 3594 | /// @endcode |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3595 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3596 | const Decl *Container, |
Chris Lattner | e6db3b0 | 2008-11-19 07:24:05 +0000 | [diff] [blame] | 3597 | std::string& S) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3598 | // Collect information from the property implementation decl(s). |
| 3599 | bool Dynamic = false; |
| 3600 | ObjCPropertyImplDecl *SynthesizePID = 0; |
| 3601 | |
| 3602 | // FIXME: Duplicated code due to poor abstraction. |
| 3603 | if (Container) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3604 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3605 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
| 3606 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3607 | i = CID->propimpl_begin(), e = CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3608 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3609 | ObjCPropertyImplDecl *PID = *i; |
| 3610 | if (PID->getPropertyDecl() == PD) { |
| 3611 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 3612 | Dynamic = true; |
| 3613 | } else { |
| 3614 | SynthesizePID = PID; |
| 3615 | } |
| 3616 | } |
| 3617 | } |
| 3618 | } else { |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3619 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3620 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3621 | i = OID->propimpl_begin(), e = OID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3622 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3623 | ObjCPropertyImplDecl *PID = *i; |
| 3624 | if (PID->getPropertyDecl() == PD) { |
| 3625 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 3626 | Dynamic = true; |
| 3627 | } else { |
| 3628 | SynthesizePID = PID; |
| 3629 | } |
| 3630 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3631 | } |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3632 | } |
| 3633 | } |
| 3634 | |
| 3635 | // FIXME: This is not very efficient. |
| 3636 | S = "T"; |
| 3637 | |
| 3638 | // Encode result type. |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3639 | // GCC has some special rules regarding encoding of properties which |
| 3640 | // closely resembles encoding of ivars. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3641 | getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3642 | true /* outermost type */, |
| 3643 | true /* encoding for property */); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3644 | |
| 3645 | if (PD->isReadOnly()) { |
| 3646 | S += ",R"; |
| 3647 | } else { |
| 3648 | switch (PD->getSetterKind()) { |
| 3649 | case ObjCPropertyDecl::Assign: break; |
| 3650 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3651 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3652 | } |
| 3653 | } |
| 3654 | |
| 3655 | // It really isn't clear at all what this means, since properties |
| 3656 | // are "dynamic by default". |
| 3657 | if (Dynamic) |
| 3658 | S += ",D"; |
| 3659 | |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3660 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 3661 | S += ",N"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3662 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3663 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 3664 | S += ",G"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3665 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3666 | } |
| 3667 | |
| 3668 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 3669 | S += ",S"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3670 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3671 | } |
| 3672 | |
| 3673 | if (SynthesizePID) { |
| 3674 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 3675 | S += ",V"; |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 3676 | S += OID->getNameAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3677 | } |
| 3678 | |
| 3679 | // FIXME: OBJCGC: weak & strong |
| 3680 | } |
| 3681 | |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3682 | /// getLegacyIntegralTypeEncoding - |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3683 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
| 3684 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3685 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
| 3686 | /// |
| 3687 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3688 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3689 | if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) { |
Fariborz Jahanian | c657eba | 2009-02-11 23:59:18 +0000 | [diff] [blame] | 3690 | if (BT->getKind() == BuiltinType::ULong && |
| 3691 | ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3692 | PointeeTy = UnsignedIntTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3693 | else |
Fariborz Jahanian | c657eba | 2009-02-11 23:59:18 +0000 | [diff] [blame] | 3694 | if (BT->getKind() == BuiltinType::Long && |
| 3695 | ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3696 | PointeeTy = IntTy; |
| 3697 | } |
| 3698 | } |
| 3699 | } |
| 3700 | |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3701 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 3702 | const FieldDecl *Field) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3703 | // We follow the behavior of gcc, expanding structures which are |
| 3704 | // directly pointed to, and expanding embedded structures. Note that |
| 3705 | // these rules are sufficient to prevent recursive encoding of the |
| 3706 | // same type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3707 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
Fariborz Jahanian | 5b8c7d9 | 2008-12-22 23:22:27 +0000 | [diff] [blame] | 3708 | true /* outermost type */); |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3709 | } |
| 3710 | |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3711 | static char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) { |
| 3712 | switch (T->getAs<BuiltinType>()->getKind()) { |
| 3713 | default: assert(0 && "Unhandled builtin type kind"); |
| 3714 | case BuiltinType::Void: return 'v'; |
| 3715 | case BuiltinType::Bool: return 'B'; |
| 3716 | case BuiltinType::Char_U: |
| 3717 | case BuiltinType::UChar: return 'C'; |
| 3718 | case BuiltinType::UShort: return 'S'; |
| 3719 | case BuiltinType::UInt: return 'I'; |
| 3720 | case BuiltinType::ULong: |
| 3721 | return |
| 3722 | (const_cast<ASTContext *>(C))->getIntWidth(T) == 32 ? 'L' : 'Q'; |
| 3723 | case BuiltinType::UInt128: return 'T'; |
| 3724 | case BuiltinType::ULongLong: return 'Q'; |
| 3725 | case BuiltinType::Char_S: |
| 3726 | case BuiltinType::SChar: return 'c'; |
| 3727 | case BuiltinType::Short: return 's'; |
John McCall | 24da709 | 2010-06-11 10:11:05 +0000 | [diff] [blame] | 3728 | case BuiltinType::WChar: |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3729 | case BuiltinType::Int: return 'i'; |
| 3730 | case BuiltinType::Long: |
| 3731 | return |
| 3732 | (const_cast<ASTContext *>(C))->getIntWidth(T) == 32 ? 'l' : 'q'; |
| 3733 | case BuiltinType::LongLong: return 'q'; |
| 3734 | case BuiltinType::Int128: return 't'; |
| 3735 | case BuiltinType::Float: return 'f'; |
| 3736 | case BuiltinType::Double: return 'd'; |
Daniel Dunbar | 3a0be84 | 2010-10-11 21:13:48 +0000 | [diff] [blame] | 3737 | case BuiltinType::LongDouble: return 'D'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3738 | } |
| 3739 | } |
| 3740 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3741 | static void EncodeBitField(const ASTContext *Context, std::string& S, |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3742 | QualType T, const FieldDecl *FD) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3743 | const Expr *E = FD->getBitWidth(); |
| 3744 | assert(E && "bitfield width not there - getObjCEncodingForTypeImpl"); |
| 3745 | ASTContext *Ctx = const_cast<ASTContext*>(Context); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3746 | S += 'b'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3747 | // The NeXT runtime encodes bit fields as b followed by the number of bits. |
| 3748 | // The GNU runtime requires more information; bitfields are encoded as b, |
| 3749 | // then the offset (in bits) of the first element, then the type of the |
| 3750 | // bitfield, then the size in bits. For example, in this structure: |
| 3751 | // |
| 3752 | // struct |
| 3753 | // { |
| 3754 | // int integer; |
| 3755 | // int flags:2; |
| 3756 | // }; |
| 3757 | // On a 32-bit system, the encoding for flags would be b2 for the NeXT |
| 3758 | // runtime, but b32i2 for the GNU runtime. The reason for this extra |
| 3759 | // information is not especially sensible, but we're stuck with it for |
| 3760 | // compatibility with GCC, although providing it breaks anything that |
| 3761 | // actually uses runtime introspection and wants to work on both runtimes... |
| 3762 | if (!Ctx->getLangOptions().NeXTRuntime) { |
| 3763 | const RecordDecl *RD = FD->getParent(); |
| 3764 | const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD); |
| 3765 | // FIXME: This same linear search is also used in ExprConstant - it might |
| 3766 | // be better if the FieldDecl stored its offset. We'd be increasing the |
| 3767 | // size of the object slightly, but saving some time every time it is used. |
| 3768 | unsigned i = 0; |
| 3769 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 3770 | FieldEnd = RD->field_end(); |
| 3771 | Field != FieldEnd; (void)++Field, ++i) { |
| 3772 | if (*Field == FD) |
| 3773 | break; |
| 3774 | } |
| 3775 | S += llvm::utostr(RL.getFieldOffset(i)); |
| 3776 | S += ObjCEncodingForPrimitiveKind(Context, T); |
| 3777 | } |
| 3778 | unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue(); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3779 | S += llvm::utostr(N); |
| 3780 | } |
| 3781 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 3782 | // FIXME: Use SmallString for accumulating string. |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3783 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 3784 | bool ExpandPointedToStructures, |
| 3785 | bool ExpandStructures, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 3786 | const FieldDecl *FD, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3787 | bool OutermostType, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3788 | bool EncodingProperty) { |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3789 | if (T->getAs<BuiltinType>()) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3790 | if (FD && FD->isBitField()) |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3791 | return EncodeBitField(this, S, T, FD); |
| 3792 | S += ObjCEncodingForPrimitiveKind(this, T); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3793 | return; |
| 3794 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3795 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3796 | if (const ComplexType *CT = T->getAs<ComplexType>()) { |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 3797 | S += 'j'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3798 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false, |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 3799 | false); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3800 | return; |
| 3801 | } |
Fariborz Jahanian | 60bce3e | 2009-11-23 20:40:50 +0000 | [diff] [blame] | 3802 | |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 3803 | // encoding for pointer or r3eference types. |
| 3804 | QualType PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3805 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Fariborz Jahanian | 8d2c0a9 | 2009-11-30 18:43:52 +0000 | [diff] [blame] | 3806 | if (PT->isObjCSelType()) { |
| 3807 | S += ':'; |
| 3808 | return; |
| 3809 | } |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 3810 | PointeeTy = PT->getPointeeType(); |
| 3811 | } |
| 3812 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 3813 | PointeeTy = RT->getPointeeType(); |
| 3814 | if (!PointeeTy.isNull()) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3815 | bool isReadOnly = false; |
| 3816 | // For historical/compatibility reasons, the read-only qualifier of the |
| 3817 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 3818 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3819 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3820 | if (isa<TypedefType>(T.getTypePtr())) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3821 | if (OutermostType && T.isConstQualified()) { |
| 3822 | isReadOnly = true; |
| 3823 | S += 'r'; |
| 3824 | } |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 3825 | } else if (OutermostType) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3826 | QualType P = PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3827 | while (P->getAs<PointerType>()) |
| 3828 | P = P->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3829 | if (P.isConstQualified()) { |
| 3830 | isReadOnly = true; |
| 3831 | S += 'r'; |
| 3832 | } |
| 3833 | } |
| 3834 | if (isReadOnly) { |
| 3835 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 3836 | // combinations need to be rearranged. |
| 3837 | // Rewrite "in const" from "nr" to "rn" |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 3838 | if (llvm::StringRef(S).endswith("nr")) |
| 3839 | S.replace(S.end()-2, S.end(), "rn"); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3840 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3841 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3842 | if (PointeeTy->isCharType()) { |
| 3843 | // char pointer types should be encoded as '*' unless it is a |
| 3844 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3845 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3846 | S += '*'; |
| 3847 | return; |
| 3848 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3849 | } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) { |
Steve Naroff | 9533a7f | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 3850 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 3851 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 3852 | S += '#'; |
| 3853 | return; |
| 3854 | } |
| 3855 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 3856 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 3857 | S += '@'; |
| 3858 | return; |
| 3859 | } |
| 3860 | // fall through... |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3861 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3862 | S += '^'; |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3863 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 3864 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3865 | getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3866 | NULL); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3867 | return; |
| 3868 | } |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 3869 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3870 | if (const ArrayType *AT = |
| 3871 | // Ignore type qualifiers etc. |
| 3872 | dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3873 | if (isa<IncompleteArrayType>(AT)) { |
| 3874 | // Incomplete arrays are encoded as a pointer to the array element. |
| 3875 | S += '^'; |
| 3876 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3877 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3878 | false, ExpandStructures, FD); |
| 3879 | } else { |
| 3880 | S += '['; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3881 | |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3882 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 3883 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 3884 | else { |
| 3885 | //Variable length arrays are encoded as a regular array with 0 elements. |
| 3886 | assert(isa<VariableArrayType>(AT) && "Unknown array type!"); |
| 3887 | S += '0'; |
| 3888 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3889 | |
| 3890 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3891 | false, ExpandStructures, FD); |
| 3892 | S += ']'; |
| 3893 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3894 | return; |
| 3895 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3896 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3897 | if (T->getAs<FunctionType>()) { |
Anders Carlsson | c0a87b7 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 3898 | S += '?'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3899 | return; |
| 3900 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3901 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3902 | if (const RecordType *RTy = T->getAs<RecordType>()) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3903 | RecordDecl *RDecl = RTy->getDecl(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3904 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 3905 | // Anonymous structures print as '?' |
| 3906 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 3907 | S += II->getName(); |
Fariborz Jahanian | 6fb9439 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 3908 | if (ClassTemplateSpecializationDecl *Spec |
| 3909 | = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) { |
| 3910 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 3911 | std::string TemplateArgsStr |
| 3912 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3913 | TemplateArgs.data(), |
| 3914 | TemplateArgs.size(), |
Fariborz Jahanian | 6fb9439 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 3915 | (*this).PrintingPolicy); |
| 3916 | |
| 3917 | S += TemplateArgsStr; |
| 3918 | } |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 3919 | } else { |
| 3920 | S += '?'; |
| 3921 | } |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3922 | if (ExpandStructures) { |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3923 | S += '='; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3924 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 3925 | FieldEnd = RDecl->field_end(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3926 | Field != FieldEnd; ++Field) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3927 | if (FD) { |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3928 | S += '"'; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3929 | S += Field->getNameAsString(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3930 | S += '"'; |
| 3931 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3932 | |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3933 | // Special case bit-fields. |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3934 | if (Field->isBitField()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3935 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3936 | (*Field)); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3937 | } else { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3938 | QualType qt = Field->getType(); |
| 3939 | getLegacyIntegralTypeEncoding(qt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3940 | getObjCEncodingForTypeImpl(qt, S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3941 | FD); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3942 | } |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3943 | } |
Fariborz Jahanian | 6de88a8 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 3944 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3945 | S += RDecl->isUnion() ? ')' : '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3946 | return; |
| 3947 | } |
Fariborz Jahanian | e6012c7 | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 3948 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3949 | if (T->isEnumeralType()) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3950 | if (FD && FD->isBitField()) |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 3951 | EncodeBitField(this, S, T, FD); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3952 | else |
| 3953 | S += 'i'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3954 | return; |
| 3955 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3956 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3957 | if (T->isBlockPointerType()) { |
Steve Naroff | 21a98b1 | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 3958 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3959 | return; |
| 3960 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3961 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3962 | // Ignore protocol qualifiers when mangling at this level. |
| 3963 | if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>()) |
| 3964 | T = OT->getBaseType(); |
| 3965 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3966 | if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3967 | // @encode(class_name) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3968 | ObjCInterfaceDecl *OI = OIT->getDecl(); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3969 | S += '{'; |
| 3970 | const IdentifierInfo *II = OI->getIdentifier(); |
| 3971 | S += II->getName(); |
| 3972 | S += '='; |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 3973 | llvm::SmallVector<ObjCIvarDecl*, 32> Ivars; |
| 3974 | DeepCollectObjCIvars(OI, true, Ivars); |
| 3975 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) { |
| 3976 | FieldDecl *Field = cast<FieldDecl>(Ivars[i]); |
| 3977 | if (Field->isBitField()) |
| 3978 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3979 | else |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 3980 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3981 | } |
| 3982 | S += '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3983 | return; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3984 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3985 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3986 | if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3987 | if (OPT->isObjCIdType()) { |
| 3988 | S += '@'; |
| 3989 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3990 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3991 | |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 3992 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
| 3993 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
| 3994 | // Since this is a binary compatibility issue, need to consult with runtime |
| 3995 | // folks. Fortunately, this is a *very* obsure construct. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3996 | S += '#'; |
| 3997 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3998 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3999 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4000 | if (OPT->isObjCQualifiedIdType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4001 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4002 | ExpandPointedToStructures, |
| 4003 | ExpandStructures, FD); |
| 4004 | if (FD || EncodingProperty) { |
| 4005 | // Note that we do extended encoding of protocol qualifer list |
| 4006 | // Only when doing ivar or property encoding. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4007 | S += '"'; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4008 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 4009 | E = OPT->qual_end(); I != E; ++I) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4010 | S += '<'; |
| 4011 | S += (*I)->getNameAsString(); |
| 4012 | S += '>'; |
| 4013 | } |
| 4014 | S += '"'; |
| 4015 | } |
| 4016 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4017 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4018 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4019 | QualType PointeeTy = OPT->getPointeeType(); |
| 4020 | if (!EncodingProperty && |
| 4021 | isa<TypedefType>(PointeeTy.getTypePtr())) { |
| 4022 | // Another historical/compatibility reason. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4023 | // We encode the underlying type which comes out as |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4024 | // {...}; |
| 4025 | S += '^'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4026 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 4027 | false, ExpandPointedToStructures, |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4028 | NULL); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4029 | return; |
| 4030 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4031 | |
| 4032 | S += '@'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4033 | if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4034 | S += '"'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4035 | S += OPT->getInterfaceDecl()->getIdentifier()->getName(); |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4036 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 4037 | E = OPT->qual_end(); I != E; ++I) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4038 | S += '<'; |
| 4039 | S += (*I)->getNameAsString(); |
| 4040 | S += '>'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4041 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4042 | S += '"'; |
| 4043 | } |
| 4044 | return; |
| 4045 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4046 | |
John McCall | 532ec7b | 2010-05-17 23:56:34 +0000 | [diff] [blame] | 4047 | // gcc just blithely ignores member pointers. |
| 4048 | // TODO: maybe there should be a mangling for these |
| 4049 | if (T->getAs<MemberPointerType>()) |
| 4050 | return; |
Fariborz Jahanian | e6012c7 | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 4051 | |
| 4052 | if (T->isVectorType()) { |
| 4053 | // This matches gcc's encoding, even though technically it is |
| 4054 | // insufficient. |
| 4055 | // FIXME. We should do a better job than gcc. |
| 4056 | return; |
| 4057 | } |
| 4058 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4059 | assert(0 && "@encode for type not implemented!"); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4060 | } |
| 4061 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4062 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 4063 | std::string& S) const { |
| 4064 | if (QT & Decl::OBJC_TQ_In) |
| 4065 | S += 'n'; |
| 4066 | if (QT & Decl::OBJC_TQ_Inout) |
| 4067 | S += 'N'; |
| 4068 | if (QT & Decl::OBJC_TQ_Out) |
| 4069 | S += 'o'; |
| 4070 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 4071 | S += 'O'; |
| 4072 | if (QT & Decl::OBJC_TQ_Byref) |
| 4073 | S += 'R'; |
| 4074 | if (QT & Decl::OBJC_TQ_Oneway) |
| 4075 | S += 'V'; |
| 4076 | } |
| 4077 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4078 | void ASTContext::setBuiltinVaListType(QualType T) { |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 4079 | assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4080 | |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 4081 | BuiltinVaListType = T; |
| 4082 | } |
| 4083 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4084 | void ASTContext::setObjCIdType(QualType T) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 4085 | ObjCIdTypedefType = T; |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 4086 | } |
| 4087 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4088 | void ASTContext::setObjCSelType(QualType T) { |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 4089 | ObjCSelTypedefType = T; |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 4090 | } |
| 4091 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4092 | void ASTContext::setObjCProtoType(QualType QT) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4093 | ObjCProtoType = QT; |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 4094 | } |
| 4095 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4096 | void ASTContext::setObjCClassType(QualType T) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 4097 | ObjCClassTypedefType = T; |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 4098 | } |
| 4099 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4100 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4101 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 4102 | "'NSConstantString' type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4103 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4104 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 4105 | } |
| 4106 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4107 | /// \brief Retrieve the template name that corresponds to a non-empty |
| 4108 | /// lookup. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4109 | TemplateName ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin, |
| 4110 | UnresolvedSetIterator End) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4111 | unsigned size = End - Begin; |
| 4112 | assert(size > 1 && "set is not overloaded!"); |
| 4113 | |
| 4114 | void *memory = Allocate(sizeof(OverloadedTemplateStorage) + |
| 4115 | size * sizeof(FunctionTemplateDecl*)); |
| 4116 | OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size); |
| 4117 | |
| 4118 | NamedDecl **Storage = OT->getStorage(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4119 | for (UnresolvedSetIterator I = Begin; I != End; ++I) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 4120 | NamedDecl *D = *I; |
| 4121 | assert(isa<FunctionTemplateDecl>(D) || |
| 4122 | (isa<UsingShadowDecl>(D) && |
| 4123 | isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))); |
| 4124 | *Storage++ = D; |
| 4125 | } |
| 4126 | |
| 4127 | return TemplateName(OT); |
| 4128 | } |
| 4129 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4130 | /// \brief Retrieve the template name that represents a qualified |
| 4131 | /// template name such as \c std::vector. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4132 | TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4133 | bool TemplateKeyword, |
| 4134 | TemplateDecl *Template) { |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4135 | // FIXME: Canonicalization? |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4136 | llvm::FoldingSetNodeID ID; |
| 4137 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 4138 | |
| 4139 | void *InsertPos = 0; |
| 4140 | QualifiedTemplateName *QTN = |
| 4141 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4142 | if (!QTN) { |
| 4143 | QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); |
| 4144 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 4145 | } |
| 4146 | |
| 4147 | return TemplateName(QTN); |
| 4148 | } |
| 4149 | |
| 4150 | /// \brief Retrieve the template name that represents a dependent |
| 4151 | /// template name such as \c MetaFun::template apply. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4152 | TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4153 | const IdentifierInfo *Name) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4154 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 4155 | "Nested name specifier must be dependent"); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4156 | |
| 4157 | llvm::FoldingSetNodeID ID; |
| 4158 | DependentTemplateName::Profile(ID, NNS, Name); |
| 4159 | |
| 4160 | void *InsertPos = 0; |
| 4161 | DependentTemplateName *QTN = |
| 4162 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4163 | |
| 4164 | if (QTN) |
| 4165 | return TemplateName(QTN); |
| 4166 | |
| 4167 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 4168 | if (CanonNNS == NNS) { |
| 4169 | QTN = new (*this,4) DependentTemplateName(NNS, Name); |
| 4170 | } else { |
| 4171 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
| 4172 | QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4173 | DependentTemplateName *CheckQTN = |
| 4174 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4175 | assert(!CheckQTN && "Dependent type name canonicalization broken"); |
| 4176 | (void)CheckQTN; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4177 | } |
| 4178 | |
| 4179 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 4180 | return TemplateName(QTN); |
| 4181 | } |
| 4182 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4183 | /// \brief Retrieve the template name that represents a dependent |
| 4184 | /// template name such as \c MetaFun::template operator+. |
| 4185 | TemplateName |
| 4186 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
| 4187 | OverloadedOperatorKind Operator) { |
| 4188 | assert((!NNS || NNS->isDependent()) && |
| 4189 | "Nested name specifier must be dependent"); |
| 4190 | |
| 4191 | llvm::FoldingSetNodeID ID; |
| 4192 | DependentTemplateName::Profile(ID, NNS, Operator); |
| 4193 | |
| 4194 | void *InsertPos = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4195 | DependentTemplateName *QTN |
| 4196 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4197 | |
| 4198 | if (QTN) |
| 4199 | return TemplateName(QTN); |
| 4200 | |
| 4201 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 4202 | if (CanonNNS == NNS) { |
| 4203 | QTN = new (*this,4) DependentTemplateName(NNS, Operator); |
| 4204 | } else { |
| 4205 | TemplateName Canon = getDependentTemplateName(CanonNNS, Operator); |
| 4206 | QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4207 | |
| 4208 | DependentTemplateName *CheckQTN |
| 4209 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 4210 | assert(!CheckQTN && "Dependent template name canonicalization broken"); |
| 4211 | (void)CheckQTN; |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4212 | } |
| 4213 | |
| 4214 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 4215 | return TemplateName(QTN); |
| 4216 | } |
| 4217 | |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4218 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | d934112 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 4219 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 4220 | /// is actually a value of type @c TargetInfo::IntType. |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 4221 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4222 | switch (Type) { |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 4223 | case TargetInfo::NoInt: return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4224 | case TargetInfo::SignedShort: return ShortTy; |
| 4225 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 4226 | case TargetInfo::SignedInt: return IntTy; |
| 4227 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 4228 | case TargetInfo::SignedLong: return LongTy; |
| 4229 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 4230 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 4231 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 4232 | } |
| 4233 | |
| 4234 | assert(false && "Unhandled TargetInfo::IntType value"); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 4235 | return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 4236 | } |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 4237 | |
| 4238 | //===----------------------------------------------------------------------===// |
| 4239 | // Type Predicates. |
| 4240 | //===----------------------------------------------------------------------===// |
| 4241 | |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4242 | /// isObjCNSObjectType - Return true if this is an NSObject object using |
| 4243 | /// NSObject attribute on a c-style pointer type. |
| 4244 | /// FIXME - Make it work directly on types. |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 4245 | /// FIXME: Move to Type. |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4246 | /// |
| 4247 | bool ASTContext::isObjCNSObjectType(QualType Ty) const { |
| 4248 | if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) { |
| 4249 | if (TypedefDecl *TD = TDT->getDecl()) |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 4250 | if (TD->getAttr<ObjCNSObjectAttr>()) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4251 | return true; |
| 4252 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4253 | return false; |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4254 | } |
| 4255 | |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4256 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 4257 | /// garbage collection attribute. |
| 4258 | /// |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4259 | Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const { |
| 4260 | Qualifiers::GC GCAttrs = Qualifiers::GCNone; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4261 | if (getLangOptions().ObjC1 && |
| 4262 | getLangOptions().getGCMode() != LangOptions::NonGC) { |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 4263 | GCAttrs = Ty.getObjCGCAttr(); |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4264 | // Default behavious under objective-c's gc is for objective-c pointers |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4265 | // (or pointers to them) be treated as though they were declared |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 4266 | // as __strong. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4267 | if (GCAttrs == Qualifiers::GCNone) { |
Fariborz Jahanian | 75212ee | 2009-09-10 23:38:45 +0000 | [diff] [blame] | 4268 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4269 | GCAttrs = Qualifiers::Strong; |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 4270 | else if (Ty->isPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4271 | return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 4272 | } |
Fariborz Jahanian | c211218 | 2009-04-11 00:00:54 +0000 | [diff] [blame] | 4273 | // Non-pointers have none gc'able attribute regardless of the attribute |
| 4274 | // set on them. |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 4275 | else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4276 | return Qualifiers::GCNone; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4277 | } |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 4278 | return GCAttrs; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4279 | } |
| 4280 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4281 | //===----------------------------------------------------------------------===// |
| 4282 | // Type Compatibility Testing |
| 4283 | //===----------------------------------------------------------------------===// |
Chris Lattner | 770951b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 4284 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4285 | /// areCompatVectorTypes - Return true if the two specified vector types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4286 | /// compatible. |
| 4287 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 4288 | const VectorType *RHS) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 4289 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4290 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4291 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4292 | } |
| 4293 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 4294 | bool ASTContext::areCompatibleVectorTypes(QualType FirstVec, |
| 4295 | QualType SecondVec) { |
| 4296 | assert(FirstVec->isVectorType() && "FirstVec should be a vector type"); |
| 4297 | assert(SecondVec->isVectorType() && "SecondVec should be a vector type"); |
| 4298 | |
| 4299 | if (hasSameUnqualifiedType(FirstVec, SecondVec)) |
| 4300 | return true; |
| 4301 | |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 4302 | // Treat Neon vector types and most AltiVec vector types as if they are the |
| 4303 | // equivalent GCC vector types. |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 4304 | const VectorType *First = FirstVec->getAs<VectorType>(); |
| 4305 | const VectorType *Second = SecondVec->getAs<VectorType>(); |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 4306 | if (First->getNumElements() == Second->getNumElements() && |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 4307 | hasSameType(First->getElementType(), Second->getElementType()) && |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 4308 | First->getVectorKind() != VectorType::AltiVecPixel && |
| 4309 | First->getVectorKind() != VectorType::AltiVecBool && |
| 4310 | Second->getVectorKind() != VectorType::AltiVecPixel && |
| 4311 | Second->getVectorKind() != VectorType::AltiVecBool) |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 4312 | return true; |
| 4313 | |
| 4314 | return false; |
| 4315 | } |
| 4316 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4317 | //===----------------------------------------------------------------------===// |
| 4318 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 4319 | //===----------------------------------------------------------------------===// |
| 4320 | |
| 4321 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 4322 | /// inheritance hierarchy of 'rProto'. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 4323 | bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 4324 | ObjCProtocolDecl *rProto) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4325 | if (lProto == rProto) |
| 4326 | return true; |
| 4327 | for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), |
| 4328 | E = rProto->protocol_end(); PI != E; ++PI) |
| 4329 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 4330 | return true; |
| 4331 | return false; |
| 4332 | } |
| 4333 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4334 | /// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...> |
| 4335 | /// return true if lhs's protocols conform to rhs's protocol; false |
| 4336 | /// otherwise. |
| 4337 | bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) { |
| 4338 | if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType()) |
| 4339 | return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false); |
| 4340 | return false; |
| 4341 | } |
| 4342 | |
Fariborz Jahanian | a8f8dac | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 4343 | /// ObjCQualifiedClassTypesAreCompatible - compare Class<p,...> and |
| 4344 | /// Class<p1, ...>. |
| 4345 | bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs, |
| 4346 | QualType rhs) { |
| 4347 | const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>(); |
| 4348 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
| 4349 | assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible"); |
| 4350 | |
| 4351 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4352 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4353 | bool match = false; |
| 4354 | ObjCProtocolDecl *lhsProto = *I; |
| 4355 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 4356 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 4357 | ObjCProtocolDecl *rhsProto = *J; |
| 4358 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) { |
| 4359 | match = true; |
| 4360 | break; |
| 4361 | } |
| 4362 | } |
| 4363 | if (!match) |
| 4364 | return false; |
| 4365 | } |
| 4366 | return true; |
| 4367 | } |
| 4368 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4369 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 4370 | /// ObjCQualifiedIDType. |
| 4371 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 4372 | bool compare) { |
| 4373 | // Allow id<P..> and an 'id' or void* type in all cases. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4374 | if (lhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4375 | lhs->isObjCIdType() || lhs->isObjCClassType()) |
| 4376 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4377 | else if (rhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4378 | rhs->isObjCIdType() || rhs->isObjCClassType()) |
| 4379 | return true; |
| 4380 | |
| 4381 | if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4382 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4383 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4384 | if (!rhsOPT) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4385 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4386 | if (rhsOPT->qual_empty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4387 | // If the RHS is a unqualified interface pointer "NSString*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4388 | // make sure we check the class hierarchy. |
| 4389 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 4390 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4391 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4392 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4393 | // see if static class implements all of id's protocols, directly or |
| 4394 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 4395 | if (!rhsID->ClassImplementsProtocol(*I, true)) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4396 | return false; |
| 4397 | } |
| 4398 | } |
| 4399 | // If there are no qualifiers and no interface, we have an 'id'. |
| 4400 | return true; |
| 4401 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4402 | // Both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4403 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4404 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4405 | ObjCProtocolDecl *lhsProto = *I; |
| 4406 | bool match = false; |
| 4407 | |
| 4408 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4409 | // see if static class implements all of id's protocols, directly or |
| 4410 | // through its super class and categories. |
| 4411 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 4412 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 4413 | ObjCProtocolDecl *rhsProto = *J; |
| 4414 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 4415 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 4416 | match = true; |
| 4417 | break; |
| 4418 | } |
| 4419 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4420 | // If the RHS is a qualified interface pointer "NSString<P>*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4421 | // make sure we check the class hierarchy. |
| 4422 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 4423 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4424 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4425 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4426 | // see if static class implements all of id's protocols, directly or |
| 4427 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 4428 | if (rhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4429 | match = true; |
| 4430 | break; |
| 4431 | } |
| 4432 | } |
| 4433 | } |
| 4434 | if (!match) |
| 4435 | return false; |
| 4436 | } |
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 | return true; |
| 4439 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4440 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4441 | const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 4442 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 4443 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4444 | if (const ObjCObjectPointerType *lhsOPT = |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4445 | lhs->getAsObjCInterfacePointerType()) { |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 4446 | // If both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4447 | for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(), |
| 4448 | E = lhsOPT->qual_end(); I != E; ++I) { |
| 4449 | ObjCProtocolDecl *lhsProto = *I; |
| 4450 | bool match = false; |
| 4451 | |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 4452 | // 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] | 4453 | // see if static class implements all of id's protocols, directly or |
| 4454 | // through its super class and categories. |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 4455 | // First, lhs protocols in the qualifier list must be found, direct |
| 4456 | // or indirect in rhs's qualifier list or it is a mismatch. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4457 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 4458 | E = rhsQID->qual_end(); J != E; ++J) { |
| 4459 | ObjCProtocolDecl *rhsProto = *J; |
| 4460 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 4461 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 4462 | match = true; |
| 4463 | break; |
| 4464 | } |
| 4465 | } |
| 4466 | if (!match) |
| 4467 | return false; |
| 4468 | } |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 4469 | |
| 4470 | // Static class's protocols, or its super class or category protocols |
| 4471 | // must be found, direct or indirect in rhs's qualifier list or it is a mismatch. |
| 4472 | if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { |
| 4473 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
| 4474 | CollectInheritedProtocols(lhsID, LHSInheritedProtocols); |
| 4475 | // This is rather dubious but matches gcc's behavior. If lhs has |
| 4476 | // no type qualifier and its class has no static protocol(s) |
| 4477 | // assume that it is mismatch. |
| 4478 | if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty()) |
| 4479 | return false; |
| 4480 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 4481 | LHSInheritedProtocols.begin(), |
| 4482 | E = LHSInheritedProtocols.end(); I != E; ++I) { |
| 4483 | bool match = false; |
| 4484 | ObjCProtocolDecl *lhsProto = (*I); |
| 4485 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 4486 | E = rhsQID->qual_end(); J != E; ++J) { |
| 4487 | ObjCProtocolDecl *rhsProto = *J; |
| 4488 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 4489 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 4490 | match = true; |
| 4491 | break; |
| 4492 | } |
| 4493 | } |
| 4494 | if (!match) |
| 4495 | return false; |
| 4496 | } |
| 4497 | } |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4498 | return true; |
| 4499 | } |
| 4500 | return false; |
| 4501 | } |
| 4502 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4503 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4504 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 4505 | /// protocol qualifiers on the LHS or RHS. |
| 4506 | /// |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4507 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 4508 | const ObjCObjectPointerType *RHSOPT) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4509 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 4510 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 4511 | |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 4512 | // 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] | 4513 | if (LHS->isObjCUnqualifiedIdOrClass() || |
| 4514 | RHS->isObjCUnqualifiedIdOrClass()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4515 | return true; |
| 4516 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4517 | if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4518 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 4519 | QualType(RHSOPT,0), |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4520 | false); |
Fariborz Jahanian | a8f8dac | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 4521 | |
| 4522 | if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass()) |
| 4523 | return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0), |
| 4524 | QualType(RHSOPT,0)); |
| 4525 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4526 | // If we have 2 user-defined types, fall into that path. |
| 4527 | if (LHS->getInterface() && RHS->getInterface()) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4528 | return canAssignObjCInterfaces(LHS, RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4529 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4530 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4531 | } |
| 4532 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4533 | /// canAssignObjCInterfacesInBlockPointer - This routine is specifically written |
| 4534 | /// for providing type-safty for objective-c pointers used to pass/return |
| 4535 | /// arguments in block literals. When passed as arguments, passing 'A*' where |
| 4536 | /// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is |
| 4537 | /// not OK. For the return type, the opposite is not OK. |
| 4538 | bool ASTContext::canAssignObjCInterfacesInBlockPointer( |
| 4539 | const ObjCObjectPointerType *LHSOPT, |
| 4540 | const ObjCObjectPointerType *RHSOPT) { |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 4541 | if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4542 | return true; |
| 4543 | |
| 4544 | if (LHSOPT->isObjCBuiltinType()) { |
| 4545 | return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType(); |
| 4546 | } |
| 4547 | |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 4548 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4549 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 4550 | QualType(RHSOPT,0), |
| 4551 | false); |
| 4552 | |
| 4553 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 4554 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 4555 | if (LHS && RHS) { // We have 2 user-defined types. |
| 4556 | if (LHS != RHS) { |
| 4557 | if (LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
| 4558 | return false; |
| 4559 | if (RHS->getDecl()->isSuperClassOf(LHS->getDecl())) |
| 4560 | return true; |
| 4561 | } |
| 4562 | else |
| 4563 | return true; |
| 4564 | } |
| 4565 | return false; |
| 4566 | } |
| 4567 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4568 | /// getIntersectionOfProtocols - This routine finds the intersection of set |
| 4569 | /// of protocols inherited from two distinct objective-c pointer objects. |
| 4570 | /// It is used to build composite qualifier list of the composite type of |
| 4571 | /// the conditional expression involving two objective-c pointer objects. |
| 4572 | static |
| 4573 | void getIntersectionOfProtocols(ASTContext &Context, |
| 4574 | const ObjCObjectPointerType *LHSOPT, |
| 4575 | const ObjCObjectPointerType *RHSOPT, |
| 4576 | llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) { |
| 4577 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4578 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 4579 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 4580 | assert(LHS->getInterface() && "LHS must have an interface base"); |
| 4581 | assert(RHS->getInterface() && "RHS must have an interface base"); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4582 | |
| 4583 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet; |
| 4584 | unsigned LHSNumProtocols = LHS->getNumProtocols(); |
| 4585 | if (LHSNumProtocols > 0) |
| 4586 | InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end()); |
| 4587 | else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 4588 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4589 | Context.CollectInheritedProtocols(LHS->getInterface(), |
| 4590 | LHSInheritedProtocols); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4591 | InheritedProtocolSet.insert(LHSInheritedProtocols.begin(), |
| 4592 | LHSInheritedProtocols.end()); |
| 4593 | } |
| 4594 | |
| 4595 | unsigned RHSNumProtocols = RHS->getNumProtocols(); |
| 4596 | if (RHSNumProtocols > 0) { |
Dan Gohman | cb421fa | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 4597 | ObjCProtocolDecl **RHSProtocols = |
| 4598 | const_cast<ObjCProtocolDecl **>(RHS->qual_begin()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4599 | for (unsigned i = 0; i < RHSNumProtocols; ++i) |
| 4600 | if (InheritedProtocolSet.count(RHSProtocols[i])) |
| 4601 | IntersectionOfProtocols.push_back(RHSProtocols[i]); |
| 4602 | } |
| 4603 | else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 4604 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4605 | Context.CollectInheritedProtocols(RHS->getInterface(), |
| 4606 | RHSInheritedProtocols); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 4607 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 4608 | RHSInheritedProtocols.begin(), |
| 4609 | E = RHSInheritedProtocols.end(); I != E; ++I) |
| 4610 | if (InheritedProtocolSet.count((*I))) |
| 4611 | IntersectionOfProtocols.push_back((*I)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4612 | } |
| 4613 | } |
| 4614 | |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4615 | /// areCommonBaseCompatible - Returns common base class of the two classes if |
| 4616 | /// one found. Note that this is O'2 algorithm. But it will be called as the |
| 4617 | /// last type comparison in a ?-exp of ObjC pointer types before a |
| 4618 | /// warning is issued. So, its invokation is extremely rare. |
| 4619 | QualType ASTContext::areCommonBaseCompatible( |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4620 | const ObjCObjectPointerType *Lptr, |
| 4621 | const ObjCObjectPointerType *Rptr) { |
| 4622 | const ObjCObjectType *LHS = Lptr->getObjectType(); |
| 4623 | const ObjCObjectType *RHS = Rptr->getObjectType(); |
| 4624 | const ObjCInterfaceDecl* LDecl = LHS->getInterface(); |
| 4625 | const ObjCInterfaceDecl* RDecl = RHS->getInterface(); |
| 4626 | if (!LDecl || !RDecl) |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4627 | return QualType(); |
| 4628 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4629 | while ((LDecl = LDecl->getSuperClass())) { |
| 4630 | LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4631 | if (canAssignObjCInterfaces(LHS, RHS)) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4632 | llvm::SmallVector<ObjCProtocolDecl *, 8> Protocols; |
| 4633 | getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols); |
| 4634 | |
| 4635 | QualType Result = QualType(LHS, 0); |
| 4636 | if (!Protocols.empty()) |
| 4637 | Result = getObjCObjectType(Result, Protocols.data(), Protocols.size()); |
| 4638 | Result = getObjCObjectPointerType(Result); |
| 4639 | return Result; |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4640 | } |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4641 | } |
| 4642 | |
| 4643 | return QualType(); |
| 4644 | } |
| 4645 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4646 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS, |
| 4647 | const ObjCObjectType *RHS) { |
| 4648 | assert(LHS->getInterface() && "LHS is not an interface type"); |
| 4649 | assert(RHS->getInterface() && "RHS is not an interface type"); |
| 4650 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4651 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 4652 | // the LHS. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4653 | if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface())) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4654 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4655 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4656 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 4657 | // protocol qualified at all, then we are good. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4658 | if (LHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4659 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4660 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4661 | // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it |
| 4662 | // isn't a superset. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4663 | if (RHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4664 | return true; // FIXME: should return false! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4665 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4666 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 4667 | LHSPE = LHS->qual_end(); |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4668 | LHSPI != LHSPE; LHSPI++) { |
| 4669 | bool RHSImplementsProtocol = false; |
| 4670 | |
| 4671 | // If the RHS doesn't implement the protocol on the left, the types |
| 4672 | // are incompatible. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4673 | for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(), |
| 4674 | RHSPE = RHS->qual_end(); |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 4675 | RHSPI != RHSPE; RHSPI++) { |
| 4676 | if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) { |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4677 | RHSImplementsProtocol = true; |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 4678 | break; |
| 4679 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4680 | } |
| 4681 | // FIXME: For better diagnostics, consider passing back the protocol name. |
| 4682 | if (!RHSImplementsProtocol) |
| 4683 | return false; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4684 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4685 | // The RHS implements all protocols listed on the LHS. |
| 4686 | return true; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4687 | } |
| 4688 | |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4689 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 4690 | // get the "pointed to" types |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4691 | const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
| 4692 | const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4693 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4694 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4695 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4696 | |
| 4697 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 4698 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4699 | } |
| 4700 | |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4701 | bool ASTContext::canBindObjCObjectType(QualType To, QualType From) { |
| 4702 | return canAssignObjCInterfaces( |
| 4703 | getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(), |
| 4704 | getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>()); |
| 4705 | } |
| 4706 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4707 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4708 | /// both shall have the identically qualified version of a compatible type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4709 | /// 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] | 4710 | /// same. See 6.7.[2,3,5] for additional rules. |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4711 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS, |
| 4712 | bool CompareUnqualified) { |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 4713 | if (getLangOptions().CPlusPlus) |
| 4714 | return hasSameType(LHS, RHS); |
| 4715 | |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4716 | return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4717 | } |
| 4718 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4719 | bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) { |
| 4720 | return !mergeTypes(LHS, RHS, true).isNull(); |
| 4721 | } |
| 4722 | |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 4723 | /// mergeTransparentUnionType - if T is a transparent union type and a member |
| 4724 | /// of T is compatible with SubType, return the merged type, else return |
| 4725 | /// QualType() |
| 4726 | QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType, |
| 4727 | bool OfBlockPointer, |
| 4728 | bool Unqualified) { |
| 4729 | if (const RecordType *UT = T->getAsUnionType()) { |
| 4730 | RecordDecl *UD = UT->getDecl(); |
| 4731 | if (UD->hasAttr<TransparentUnionAttr>()) { |
| 4732 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 4733 | itend = UD->field_end(); it != itend; ++it) { |
Peter Collingbourne | f91d757 | 2010-12-02 21:00:06 +0000 | [diff] [blame] | 4734 | QualType ET = it->getType().getUnqualifiedType(); |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 4735 | QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified); |
| 4736 | if (!MT.isNull()) |
| 4737 | return MT; |
| 4738 | } |
| 4739 | } |
| 4740 | } |
| 4741 | |
| 4742 | return QualType(); |
| 4743 | } |
| 4744 | |
| 4745 | /// mergeFunctionArgumentTypes - merge two types which appear as function |
| 4746 | /// argument types |
| 4747 | QualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs, |
| 4748 | bool OfBlockPointer, |
| 4749 | bool Unqualified) { |
| 4750 | // GNU extension: two types are compatible if they appear as a function |
| 4751 | // argument, one of the types is a transparent union type and the other |
| 4752 | // type is compatible with a union member |
| 4753 | QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer, |
| 4754 | Unqualified); |
| 4755 | if (!lmerge.isNull()) |
| 4756 | return lmerge; |
| 4757 | |
| 4758 | QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer, |
| 4759 | Unqualified); |
| 4760 | if (!rmerge.isNull()) |
| 4761 | return rmerge; |
| 4762 | |
| 4763 | return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified); |
| 4764 | } |
| 4765 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4766 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs, |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4767 | bool OfBlockPointer, |
| 4768 | bool Unqualified) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4769 | const FunctionType *lbase = lhs->getAs<FunctionType>(); |
| 4770 | const FunctionType *rbase = rhs->getAs<FunctionType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4771 | const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 4772 | const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4773 | bool allLTypes = true; |
| 4774 | bool allRTypes = true; |
| 4775 | |
| 4776 | // Check return type |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4777 | QualType retType; |
| 4778 | if (OfBlockPointer) |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4779 | retType = mergeTypes(rbase->getResultType(), lbase->getResultType(), true, |
| 4780 | Unqualified); |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4781 | else |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4782 | retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), |
| 4783 | false, Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4784 | if (retType.isNull()) return QualType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4785 | |
| 4786 | if (Unqualified) |
| 4787 | retType = retType.getUnqualifiedType(); |
| 4788 | |
| 4789 | CanQualType LRetType = getCanonicalType(lbase->getResultType()); |
| 4790 | CanQualType RRetType = getCanonicalType(rbase->getResultType()); |
| 4791 | if (Unqualified) { |
| 4792 | LRetType = LRetType.getUnqualifiedType(); |
| 4793 | RRetType = RRetType.getUnqualifiedType(); |
| 4794 | } |
| 4795 | |
| 4796 | if (getCanonicalType(retType) != LRetType) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4797 | allLTypes = false; |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4798 | if (getCanonicalType(retType) != RRetType) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4799 | allRTypes = false; |
Daniel Dunbar | 6a15c85 | 2010-04-28 16:20:58 +0000 | [diff] [blame] | 4800 | // FIXME: double check this |
| 4801 | // FIXME: should we error if lbase->getRegParmAttr() != 0 && |
| 4802 | // rbase->getRegParmAttr() != 0 && |
| 4803 | // lbase->getRegParmAttr() != rbase->getRegParmAttr()? |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 4804 | FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo(); |
| 4805 | FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo(); |
Daniel Dunbar | 6a15c85 | 2010-04-28 16:20:58 +0000 | [diff] [blame] | 4806 | unsigned RegParm = lbaseInfo.getRegParm() == 0 ? rbaseInfo.getRegParm() : |
| 4807 | lbaseInfo.getRegParm(); |
| 4808 | bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn(); |
| 4809 | if (NoReturn != lbaseInfo.getNoReturn() || |
| 4810 | RegParm != lbaseInfo.getRegParm()) |
| 4811 | allLTypes = false; |
| 4812 | if (NoReturn != rbaseInfo.getNoReturn() || |
| 4813 | RegParm != rbaseInfo.getRegParm()) |
| 4814 | allRTypes = false; |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 4815 | CallingConv lcc = lbaseInfo.getCC(); |
| 4816 | CallingConv rcc = rbaseInfo.getCC(); |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 4817 | // Compatible functions must have compatible calling conventions |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4818 | if (!isSameCallConv(lcc, rcc)) |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 4819 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4820 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4821 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 4822 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 4823 | "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4824 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 4825 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 4826 | |
| 4827 | // Compatible functions must have the same number of arguments |
| 4828 | if (lproto_nargs != rproto_nargs) |
| 4829 | return QualType(); |
| 4830 | |
| 4831 | // Variadic and non-variadic functions aren't compatible |
| 4832 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 4833 | return QualType(); |
| 4834 | |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 4835 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 4836 | return QualType(); |
| 4837 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4838 | // Check argument compatibility |
| 4839 | llvm::SmallVector<QualType, 10> types; |
| 4840 | for (unsigned i = 0; i < lproto_nargs; i++) { |
| 4841 | QualType largtype = lproto->getArgType(i).getUnqualifiedType(); |
| 4842 | QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 4843 | QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype, |
| 4844 | OfBlockPointer, |
| 4845 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4846 | if (argtype.isNull()) return QualType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4847 | |
| 4848 | if (Unqualified) |
| 4849 | argtype = argtype.getUnqualifiedType(); |
| 4850 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4851 | types.push_back(argtype); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4852 | if (Unqualified) { |
| 4853 | largtype = largtype.getUnqualifiedType(); |
| 4854 | rargtype = rargtype.getUnqualifiedType(); |
| 4855 | } |
| 4856 | |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4857 | if (getCanonicalType(argtype) != getCanonicalType(largtype)) |
| 4858 | allLTypes = false; |
| 4859 | if (getCanonicalType(argtype) != getCanonicalType(rargtype)) |
| 4860 | allRTypes = false; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4861 | } |
| 4862 | if (allLTypes) return lhs; |
| 4863 | if (allRTypes) return rhs; |
| 4864 | return getFunctionType(retType, types.begin(), types.size(), |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 4865 | lproto->isVariadic(), lproto->getTypeQuals(), |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 4866 | false, false, 0, 0, |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 4867 | FunctionType::ExtInfo(NoReturn, RegParm, lcc)); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4868 | } |
| 4869 | |
| 4870 | if (lproto) allRTypes = false; |
| 4871 | if (rproto) allLTypes = false; |
| 4872 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4873 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4874 | if (proto) { |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 4875 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4876 | if (proto->isVariadic()) return QualType(); |
| 4877 | // Check that the types are compatible with the types that |
| 4878 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 4879 | // The only types actually affected are promotable integer |
| 4880 | // types and floats, which would be passed as a different |
| 4881 | // type depending on whether the prototype is visible. |
| 4882 | unsigned proto_nargs = proto->getNumArgs(); |
| 4883 | for (unsigned i = 0; i < proto_nargs; ++i) { |
| 4884 | QualType argTy = proto->getArgType(i); |
Douglas Gregor | b0f8eac | 2010-02-03 19:27:29 +0000 | [diff] [blame] | 4885 | |
| 4886 | // Look at the promotion type of enum types, since that is the type used |
| 4887 | // to pass enum values. |
| 4888 | if (const EnumType *Enum = argTy->getAs<EnumType>()) |
| 4889 | argTy = Enum->getDecl()->getPromotionType(); |
| 4890 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4891 | if (argTy->isPromotableIntegerType() || |
| 4892 | getCanonicalType(argTy).getUnqualifiedType() == FloatTy) |
| 4893 | return QualType(); |
| 4894 | } |
| 4895 | |
| 4896 | if (allLTypes) return lhs; |
| 4897 | if (allRTypes) return rhs; |
| 4898 | return getFunctionType(retType, proto->arg_type_begin(), |
Mike Stump | 2d3c191 | 2009-07-27 00:44:23 +0000 | [diff] [blame] | 4899 | proto->getNumArgs(), proto->isVariadic(), |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 4900 | proto->getTypeQuals(), |
| 4901 | false, false, 0, 0, |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 4902 | FunctionType::ExtInfo(NoReturn, RegParm, lcc)); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4903 | } |
| 4904 | |
| 4905 | if (allLTypes) return lhs; |
| 4906 | if (allRTypes) return rhs; |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 4907 | FunctionType::ExtInfo Info(NoReturn, RegParm, lcc); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 4908 | return getFunctionNoProtoType(retType, Info); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4909 | } |
| 4910 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4911 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4912 | bool OfBlockPointer, |
| 4913 | bool Unqualified) { |
Bill Wendling | 43d6975 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 4914 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 4915 | // type is adjusted to "T" prior to any further analysis, the expression |
| 4916 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4917 | // expression is an lvalue unless the reference is an rvalue reference and |
| 4918 | // the expression is a function call (possibly inside parentheses). |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 4919 | assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?"); |
| 4920 | assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?"); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 4921 | |
| 4922 | if (Unqualified) { |
| 4923 | LHS = LHS.getUnqualifiedType(); |
| 4924 | RHS = RHS.getUnqualifiedType(); |
| 4925 | } |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 4926 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4927 | QualType LHSCan = getCanonicalType(LHS), |
| 4928 | RHSCan = getCanonicalType(RHS); |
| 4929 | |
| 4930 | // If two types are identical, they are compatible. |
| 4931 | if (LHSCan == RHSCan) |
| 4932 | return LHS; |
| 4933 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4934 | // If the qualifiers are different, the types aren't compatible... mostly. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4935 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 4936 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4937 | if (LQuals != RQuals) { |
| 4938 | // If any of these qualifiers are different, we have a type |
| 4939 | // mismatch. |
| 4940 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 4941 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 4942 | return QualType(); |
| 4943 | |
| 4944 | // Exactly one GC qualifier difference is allowed: __strong is |
| 4945 | // okay if the other type has no GC qualifier but is an Objective |
| 4946 | // C object pointer (i.e. implicitly strong by default). We fix |
| 4947 | // this by pretending that the unqualified type was actually |
| 4948 | // qualified __strong. |
| 4949 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 4950 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 4951 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 4952 | |
| 4953 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 4954 | return QualType(); |
| 4955 | |
| 4956 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
| 4957 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); |
| 4958 | } |
| 4959 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
| 4960 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); |
| 4961 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4962 | return QualType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4963 | } |
| 4964 | |
| 4965 | // Okay, qualifiers are equal. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4966 | |
Eli Friedman | 852d63b | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 4967 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 4968 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4969 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4970 | // We want to consider the two function types to be the same for these |
| 4971 | // comparisons, just force one to the other. |
| 4972 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 4973 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 4c721d3 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 4974 | |
| 4975 | // Same as above for arrays |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 4976 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 4977 | LHSClass = Type::ConstantArray; |
| 4978 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 4979 | RHSClass = Type::ConstantArray; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4980 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4981 | // ObjCInterfaces are just specialized ObjCObjects. |
| 4982 | if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject; |
| 4983 | if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject; |
| 4984 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 4985 | // Canonicalize ExtVector -> Vector. |
| 4986 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 4987 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4988 | |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 4989 | // If the canonical type classes don't match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4990 | if (LHSClass != RHSClass) { |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4991 | // 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] | 4992 | // a signed integer type, or an unsigned integer type. |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 4993 | // Compatibility is based on the underlying type, not the promotion |
| 4994 | // type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4995 | if (const EnumType* ETy = LHS->getAs<EnumType>()) { |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4996 | if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType()) |
| 4997 | return RHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 4998 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4999 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5000 | if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType()) |
| 5001 | return LHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 5002 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5003 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5004 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5005 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5006 | |
Steve Naroff | 4a74678 | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 5007 | // The canonical type classes match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5008 | switch (LHSClass) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5009 | #define TYPE(Class, Base) |
| 5010 | #define ABSTRACT_TYPE(Class, Base) |
John McCall | ad5e738 | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 5011 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5012 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 5013 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 5014 | #include "clang/AST/TypeNodes.def" |
| 5015 | assert(false && "Non-canonical and dependent types shouldn't get here"); |
| 5016 | return QualType(); |
| 5017 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 5018 | case Type::LValueReference: |
| 5019 | case Type::RValueReference: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5020 | case Type::MemberPointer: |
| 5021 | assert(false && "C++ should never be in mergeTypes"); |
| 5022 | return QualType(); |
| 5023 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5024 | case Type::ObjCInterface: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5025 | case Type::IncompleteArray: |
| 5026 | case Type::VariableArray: |
| 5027 | case Type::FunctionProto: |
| 5028 | case Type::ExtVector: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5029 | assert(false && "Types are eliminated above"); |
| 5030 | return QualType(); |
| 5031 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5032 | case Type::Pointer: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5033 | { |
| 5034 | // Merge two pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5035 | QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); |
| 5036 | QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5037 | if (Unqualified) { |
| 5038 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 5039 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 5040 | } |
| 5041 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false, |
| 5042 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5043 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 5044 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5045 | return LHS; |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 5046 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5047 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5048 | return getPointerType(ResultType); |
| 5049 | } |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 5050 | case Type::BlockPointer: |
| 5051 | { |
| 5052 | // Merge two block pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5053 | QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); |
| 5054 | QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5055 | if (Unqualified) { |
| 5056 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 5057 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 5058 | } |
| 5059 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer, |
| 5060 | Unqualified); |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 5061 | if (ResultType.isNull()) return QualType(); |
| 5062 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 5063 | return LHS; |
| 5064 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 5065 | return RHS; |
| 5066 | return getBlockPointerType(ResultType); |
| 5067 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5068 | case Type::ConstantArray: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5069 | { |
| 5070 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 5071 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 5072 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 5073 | return QualType(); |
| 5074 | |
| 5075 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 5076 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5077 | if (Unqualified) { |
| 5078 | LHSElem = LHSElem.getUnqualifiedType(); |
| 5079 | RHSElem = RHSElem.getUnqualifiedType(); |
| 5080 | } |
| 5081 | |
| 5082 | QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5083 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5084 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 5085 | return LHS; |
| 5086 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 5087 | return RHS; |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 5088 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 5089 | ArrayType::ArraySizeModifier(), 0); |
| 5090 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 5091 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5092 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 5093 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5094 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 5095 | return LHS; |
| 5096 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 5097 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5098 | if (LVAT) { |
| 5099 | // FIXME: This isn't correct! But tricky to implement because |
| 5100 | // the array's size has to be the size of LHS, but the type |
| 5101 | // has to be different. |
| 5102 | return LHS; |
| 5103 | } |
| 5104 | if (RVAT) { |
| 5105 | // FIXME: This isn't correct! But tricky to implement because |
| 5106 | // the array's size has to be the size of RHS, but the type |
| 5107 | // has to be different. |
| 5108 | return RHS; |
| 5109 | } |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 5110 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 5111 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 5112 | return getIncompleteArrayType(ResultType, |
| 5113 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5114 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5115 | case Type::FunctionNoProto: |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5116 | return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5117 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5118 | case Type::Enum: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5119 | return QualType(); |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 5120 | case Type::Builtin: |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 5121 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5122 | return QualType(); |
Daniel Dunbar | 64cfdb7 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 5123 | case Type::Complex: |
| 5124 | // Distinct complex types are incompatible. |
| 5125 | return QualType(); |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 5126 | case Type::Vector: |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 5127 | // FIXME: The merged type should be an ExtVector! |
John McCall | 1c471f3 | 2010-03-12 23:14:13 +0000 | [diff] [blame] | 5128 | if (areCompatVectorTypes(LHSCan->getAs<VectorType>(), |
| 5129 | RHSCan->getAs<VectorType>())) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5130 | return LHS; |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5131 | return QualType(); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5132 | case Type::ObjCObject: { |
| 5133 | // Check if the types are assignment compatible. |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 5134 | // FIXME: This should be type compatibility, e.g. whether |
| 5135 | // "LHS x; RHS x;" at global scope is legal. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5136 | const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>(); |
| 5137 | const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>(); |
| 5138 | if (canAssignObjCInterfaces(LHSIface, RHSIface)) |
Steve Naroff | 5fd659d | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 5139 | return LHS; |
| 5140 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5141 | return QualType(); |
Cedric Venet | 61490e9 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 5142 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5143 | case Type::ObjCObjectPointer: { |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5144 | if (OfBlockPointer) { |
| 5145 | if (canAssignObjCInterfacesInBlockPointer( |
| 5146 | LHS->getAs<ObjCObjectPointerType>(), |
| 5147 | RHS->getAs<ObjCObjectPointerType>())) |
| 5148 | return LHS; |
| 5149 | return QualType(); |
| 5150 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5151 | if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(), |
| 5152 | RHS->getAs<ObjCObjectPointerType>())) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5153 | return LHS; |
| 5154 | |
Steve Naroff | bc76dd0 | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 5155 | return QualType(); |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5156 | } |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5157 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5158 | |
| 5159 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5160 | } |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 5161 | |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 5162 | /// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and |
| 5163 | /// 'RHS' attributes and returns the merged version; including for function |
| 5164 | /// return types. |
| 5165 | QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) { |
| 5166 | QualType LHSCan = getCanonicalType(LHS), |
| 5167 | RHSCan = getCanonicalType(RHS); |
| 5168 | // If two types are identical, they are compatible. |
| 5169 | if (LHSCan == RHSCan) |
| 5170 | return LHS; |
| 5171 | if (RHSCan->isFunctionType()) { |
| 5172 | if (!LHSCan->isFunctionType()) |
| 5173 | return QualType(); |
| 5174 | QualType OldReturnType = |
| 5175 | cast<FunctionType>(RHSCan.getTypePtr())->getResultType(); |
| 5176 | QualType NewReturnType = |
| 5177 | cast<FunctionType>(LHSCan.getTypePtr())->getResultType(); |
| 5178 | QualType ResReturnType = |
| 5179 | mergeObjCGCQualifiers(NewReturnType, OldReturnType); |
| 5180 | if (ResReturnType.isNull()) |
| 5181 | return QualType(); |
| 5182 | if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) { |
| 5183 | // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo(); |
| 5184 | // In either case, use OldReturnType to build the new function type. |
| 5185 | const FunctionType *F = LHS->getAs<FunctionType>(); |
| 5186 | if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) { |
| 5187 | FunctionType::ExtInfo Info = getFunctionExtInfo(LHS); |
| 5188 | QualType ResultType |
| 5189 | = getFunctionType(OldReturnType, FPT->arg_type_begin(), |
| 5190 | FPT->getNumArgs(), FPT->isVariadic(), |
| 5191 | FPT->getTypeQuals(), |
| 5192 | FPT->hasExceptionSpec(), |
| 5193 | FPT->hasAnyExceptionSpec(), |
| 5194 | FPT->getNumExceptions(), |
| 5195 | FPT->exception_begin(), |
| 5196 | Info); |
| 5197 | return ResultType; |
| 5198 | } |
| 5199 | } |
| 5200 | return QualType(); |
| 5201 | } |
| 5202 | |
| 5203 | // If the qualifiers are different, the types can still be merged. |
| 5204 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 5205 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
| 5206 | if (LQuals != RQuals) { |
| 5207 | // If any of these qualifiers are different, we have a type mismatch. |
| 5208 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 5209 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 5210 | return QualType(); |
| 5211 | |
| 5212 | // Exactly one GC qualifier difference is allowed: __strong is |
| 5213 | // okay if the other type has no GC qualifier but is an Objective |
| 5214 | // C object pointer (i.e. implicitly strong by default). We fix |
| 5215 | // this by pretending that the unqualified type was actually |
| 5216 | // qualified __strong. |
| 5217 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 5218 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 5219 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 5220 | |
| 5221 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 5222 | return QualType(); |
| 5223 | |
| 5224 | if (GC_L == Qualifiers::Strong) |
| 5225 | return LHS; |
| 5226 | if (GC_R == Qualifiers::Strong) |
| 5227 | return RHS; |
| 5228 | return QualType(); |
| 5229 | } |
| 5230 | |
| 5231 | if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) { |
| 5232 | QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5233 | QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5234 | QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT); |
| 5235 | if (ResQT == LHSBaseQT) |
| 5236 | return LHS; |
| 5237 | if (ResQT == RHSBaseQT) |
| 5238 | return RHS; |
| 5239 | } |
| 5240 | return QualType(); |
| 5241 | } |
| 5242 | |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 5243 | //===----------------------------------------------------------------------===// |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5244 | // Integer Predicates |
| 5245 | //===----------------------------------------------------------------------===// |
Chris Lattner | 88054de | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 5246 | |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5247 | unsigned ASTContext::getIntWidth(QualType T) { |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 5248 | if (EnumType *ET = dyn_cast<EnumType>(T)) |
Eli Friedman | 29a7f33 | 2009-12-10 22:29:29 +0000 | [diff] [blame] | 5249 | T = ET->getDecl()->getIntegerType(); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 5250 | if (T->isBooleanType()) |
| 5251 | return 1; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 5252 | // For builtin types, just use the standard type sizing method |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5253 | return (unsigned)getTypeSize(T); |
| 5254 | } |
| 5255 | |
| 5256 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) { |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5257 | assert(T->hasSignedIntegerRepresentation() && "Unexpected type"); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5258 | |
| 5259 | // Turn <4 x signed int> -> <4 x unsigned int> |
| 5260 | if (const VectorType *VTy = T->getAs<VectorType>()) |
| 5261 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 5262 | VTy->getNumElements(), VTy->getVectorKind()); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5263 | |
| 5264 | // For enums, we return the unsigned version of the base type. |
| 5265 | if (const EnumType *ETy = T->getAs<EnumType>()) |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5266 | T = ETy->getDecl()->getIntegerType(); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5267 | |
| 5268 | const BuiltinType *BTy = T->getAs<BuiltinType>(); |
| 5269 | assert(BTy && "Unexpected signed integer type"); |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5270 | switch (BTy->getKind()) { |
| 5271 | case BuiltinType::Char_S: |
| 5272 | case BuiltinType::SChar: |
| 5273 | return UnsignedCharTy; |
| 5274 | case BuiltinType::Short: |
| 5275 | return UnsignedShortTy; |
| 5276 | case BuiltinType::Int: |
| 5277 | return UnsignedIntTy; |
| 5278 | case BuiltinType::Long: |
| 5279 | return UnsignedLongTy; |
| 5280 | case BuiltinType::LongLong: |
| 5281 | return UnsignedLongLongTy; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 5282 | case BuiltinType::Int128: |
| 5283 | return UnsignedInt128Ty; |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 5284 | default: |
| 5285 | assert(0 && "Unexpected signed integer type"); |
| 5286 | return QualType(); |
| 5287 | } |
| 5288 | } |
| 5289 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 5290 | ExternalASTSource::~ExternalASTSource() { } |
| 5291 | |
| 5292 | void ExternalASTSource::PrintStats() { } |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5293 | |
Argyrios Kyrtzidis | 7b90340 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 5294 | ASTMutationListener::~ASTMutationListener() { } |
| 5295 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5296 | |
| 5297 | //===----------------------------------------------------------------------===// |
| 5298 | // Builtin Type Computation |
| 5299 | //===----------------------------------------------------------------------===// |
| 5300 | |
| 5301 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5302 | /// pointer over the consumed characters. This returns the resultant type. If |
| 5303 | /// AllowTypeModifiers is false then modifier like * are not parsed, just basic |
| 5304 | /// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of |
| 5305 | /// a vector of "i*". |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5306 | /// |
| 5307 | /// RequiresICE is filled in on return to indicate whether the value is required |
| 5308 | /// to be an Integer Constant Expression. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5309 | static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context, |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5310 | ASTContext::GetBuiltinTypeError &Error, |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5311 | bool &RequiresICE, |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5312 | bool AllowTypeModifiers) { |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5313 | // Modifiers. |
| 5314 | int HowLong = 0; |
| 5315 | bool Signed = false, Unsigned = false; |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5316 | RequiresICE = false; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 5317 | |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5318 | // Read the prefixed modifiers first. |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5319 | bool Done = false; |
| 5320 | while (!Done) { |
| 5321 | switch (*Str++) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5322 | default: Done = true; --Str; break; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 5323 | case 'I': |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5324 | RequiresICE = true; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 5325 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5326 | case 'S': |
| 5327 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 5328 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 5329 | Signed = true; |
| 5330 | break; |
| 5331 | case 'U': |
| 5332 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
| 5333 | assert(!Unsigned && "Can't use 'S' modifier multiple times!"); |
| 5334 | Unsigned = true; |
| 5335 | break; |
| 5336 | case 'L': |
| 5337 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 5338 | ++HowLong; |
| 5339 | break; |
| 5340 | } |
| 5341 | } |
| 5342 | |
| 5343 | QualType Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5344 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5345 | // Read the base type. |
| 5346 | switch (*Str++) { |
| 5347 | default: assert(0 && "Unknown builtin type letter!"); |
| 5348 | case 'v': |
| 5349 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 5350 | "Bad modifiers used with 'v'!"); |
| 5351 | Type = Context.VoidTy; |
| 5352 | break; |
| 5353 | case 'f': |
| 5354 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 5355 | "Bad modifiers used with 'f'!"); |
| 5356 | Type = Context.FloatTy; |
| 5357 | break; |
| 5358 | case 'd': |
| 5359 | assert(HowLong < 2 && !Signed && !Unsigned && |
| 5360 | "Bad modifiers used with 'd'!"); |
| 5361 | if (HowLong) |
| 5362 | Type = Context.LongDoubleTy; |
| 5363 | else |
| 5364 | Type = Context.DoubleTy; |
| 5365 | break; |
| 5366 | case 's': |
| 5367 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 5368 | if (Unsigned) |
| 5369 | Type = Context.UnsignedShortTy; |
| 5370 | else |
| 5371 | Type = Context.ShortTy; |
| 5372 | break; |
| 5373 | case 'i': |
| 5374 | if (HowLong == 3) |
| 5375 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 5376 | else if (HowLong == 2) |
| 5377 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 5378 | else if (HowLong == 1) |
| 5379 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 5380 | else |
| 5381 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 5382 | break; |
| 5383 | case 'c': |
| 5384 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 5385 | if (Signed) |
| 5386 | Type = Context.SignedCharTy; |
| 5387 | else if (Unsigned) |
| 5388 | Type = Context.UnsignedCharTy; |
| 5389 | else |
| 5390 | Type = Context.CharTy; |
| 5391 | break; |
| 5392 | case 'b': // boolean |
| 5393 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 5394 | Type = Context.BoolTy; |
| 5395 | break; |
| 5396 | case 'z': // size_t. |
| 5397 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 5398 | Type = Context.getSizeType(); |
| 5399 | break; |
| 5400 | case 'F': |
| 5401 | Type = Context.getCFConstantStringType(); |
| 5402 | break; |
Fariborz Jahanian | ba8bda0 | 2010-11-09 21:38:20 +0000 | [diff] [blame] | 5403 | case 'G': |
| 5404 | Type = Context.getObjCIdType(); |
| 5405 | break; |
| 5406 | case 'H': |
| 5407 | Type = Context.getObjCSelType(); |
| 5408 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5409 | case 'a': |
| 5410 | Type = Context.getBuiltinVaListType(); |
| 5411 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 5412 | break; |
| 5413 | case 'A': |
| 5414 | // This is a "reference" to a va_list; however, what exactly |
| 5415 | // this means depends on how va_list is defined. There are two |
| 5416 | // different kinds of va_list: ones passed by value, and ones |
| 5417 | // passed by reference. An example of a by-value va_list is |
| 5418 | // x86, where va_list is a char*. An example of by-ref va_list |
| 5419 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 5420 | // we want this argument to be a char*&; for x86-64, we want |
| 5421 | // it to be a __va_list_tag*. |
| 5422 | Type = Context.getBuiltinVaListType(); |
| 5423 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5424 | if (Type->isArrayType()) |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5425 | Type = Context.getArrayDecayedType(Type); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5426 | else |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5427 | Type = Context.getLValueReferenceType(Type); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5428 | break; |
| 5429 | case 'V': { |
| 5430 | char *End; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5431 | unsigned NumElements = strtoul(Str, &End, 10); |
| 5432 | assert(End != Str && "Missing vector size"); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5433 | Str = End; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5434 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5435 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, |
| 5436 | RequiresICE, false); |
| 5437 | assert(!RequiresICE && "Can't require vector ICE"); |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5438 | |
| 5439 | // TODO: No way to make AltiVec vectors in builtins yet. |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 5440 | Type = Context.getVectorType(ElementType, NumElements, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 5441 | VectorType::GenericVector); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5442 | break; |
| 5443 | } |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 5444 | case 'X': { |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5445 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
| 5446 | false); |
| 5447 | assert(!RequiresICE && "Can't require complex ICE"); |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 5448 | Type = Context.getComplexType(ElementType); |
| 5449 | break; |
| 5450 | } |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 5451 | case 'P': |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 5452 | Type = Context.getFILEType(); |
| 5453 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 5454 | Error = ASTContext::GE_Missing_stdio; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5455 | return QualType(); |
| 5456 | } |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 5457 | break; |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 5458 | case 'J': |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 5459 | if (Signed) |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 5460 | Type = Context.getsigjmp_bufType(); |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 5461 | else |
| 5462 | Type = Context.getjmp_bufType(); |
| 5463 | |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 5464 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 5465 | Error = ASTContext::GE_Missing_setjmp; |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 5466 | return QualType(); |
| 5467 | } |
| 5468 | break; |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 5469 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5470 | |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5471 | // If there are modifiers and if we're allowed to parse them, go for it. |
| 5472 | Done = !AllowTypeModifiers; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5473 | while (!Done) { |
John McCall | 187ab37 | 2010-03-12 04:21:28 +0000 | [diff] [blame] | 5474 | switch (char c = *Str++) { |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5475 | default: Done = true; --Str; break; |
| 5476 | case '*': |
| 5477 | case '&': { |
| 5478 | // Both pointers and references can have their pointee types |
| 5479 | // qualified with an address space. |
| 5480 | char *End; |
| 5481 | unsigned AddrSpace = strtoul(Str, &End, 10); |
| 5482 | if (End != Str && AddrSpace != 0) { |
| 5483 | Type = Context.getAddrSpaceQualType(Type, AddrSpace); |
| 5484 | Str = End; |
| 5485 | } |
| 5486 | if (c == '*') |
| 5487 | Type = Context.getPointerType(Type); |
| 5488 | else |
| 5489 | Type = Context.getLValueReferenceType(Type); |
| 5490 | break; |
| 5491 | } |
| 5492 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 5493 | case 'C': |
| 5494 | Type = Type.withConst(); |
| 5495 | break; |
| 5496 | case 'D': |
| 5497 | Type = Context.getVolatileType(Type); |
| 5498 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5499 | } |
| 5500 | } |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 5501 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5502 | assert((!RequiresICE || Type->isIntegralOrEnumerationType()) && |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 5503 | "Integer constant 'I' type must be an integer"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5504 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5505 | return Type; |
| 5506 | } |
| 5507 | |
| 5508 | /// GetBuiltinType - Return the type for the specified builtin. |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5509 | QualType ASTContext::GetBuiltinType(unsigned Id, |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5510 | GetBuiltinTypeError &Error, |
| 5511 | unsigned *IntegerConstantArgs) { |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 5512 | const char *TypeStr = BuiltinInfo.GetTypeString(Id); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5513 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5514 | llvm::SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5515 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5516 | bool RequiresICE = false; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5517 | Error = GE_None; |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5518 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error, |
| 5519 | RequiresICE, true); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5520 | if (Error != GE_None) |
| 5521 | return QualType(); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5522 | |
| 5523 | assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE"); |
| 5524 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5525 | while (TypeStr[0] && TypeStr[0] != '.') { |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5526 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5527 | if (Error != GE_None) |
| 5528 | return QualType(); |
| 5529 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 5530 | // If this argument is required to be an IntegerConstantExpression and the |
| 5531 | // caller cares, fill in the bitmask we return. |
| 5532 | if (RequiresICE && IntegerConstantArgs) |
| 5533 | *IntegerConstantArgs |= 1 << ArgTypes.size(); |
| 5534 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5535 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 5536 | if (Ty->isArrayType()) |
| 5537 | Ty = getArrayDecayedType(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5538 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5539 | ArgTypes.push_back(Ty); |
| 5540 | } |
| 5541 | |
| 5542 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 5543 | "'.' should only occur at end of builtin type list!"); |
| 5544 | |
| 5545 | // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);". |
| 5546 | if (ArgTypes.size() == 0 && TypeStr[0] == '.') |
| 5547 | return getFunctionNoProtoType(ResType); |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 5548 | |
| 5549 | // FIXME: Should we create noreturn types? |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5550 | return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 5551 | TypeStr[0] == '.', 0, false, false, 0, 0, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 5552 | FunctionType::ExtInfo()); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 5553 | } |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5554 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5555 | GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) { |
| 5556 | GVALinkage External = GVA_StrongExternal; |
| 5557 | |
| 5558 | Linkage L = FD->getLinkage(); |
| 5559 | if (L == ExternalLinkage && getLangOptions().CPlusPlus && |
| 5560 | FD->getType()->getLinkage() == UniqueExternalLinkage) |
| 5561 | L = UniqueExternalLinkage; |
| 5562 | |
| 5563 | switch (L) { |
| 5564 | case NoLinkage: |
| 5565 | case InternalLinkage: |
| 5566 | case UniqueExternalLinkage: |
| 5567 | return GVA_Internal; |
| 5568 | |
| 5569 | case ExternalLinkage: |
| 5570 | switch (FD->getTemplateSpecializationKind()) { |
| 5571 | case TSK_Undeclared: |
| 5572 | case TSK_ExplicitSpecialization: |
| 5573 | External = GVA_StrongExternal; |
| 5574 | break; |
| 5575 | |
| 5576 | case TSK_ExplicitInstantiationDefinition: |
| 5577 | return GVA_ExplicitTemplateInstantiation; |
| 5578 | |
| 5579 | case TSK_ExplicitInstantiationDeclaration: |
| 5580 | case TSK_ImplicitInstantiation: |
| 5581 | External = GVA_TemplateInstantiation; |
| 5582 | break; |
| 5583 | } |
| 5584 | } |
| 5585 | |
| 5586 | if (!FD->isInlined()) |
| 5587 | return External; |
| 5588 | |
| 5589 | if (!getLangOptions().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) { |
| 5590 | // GNU or C99 inline semantics. Determine whether this symbol should be |
| 5591 | // externally visible. |
| 5592 | if (FD->isInlineDefinitionExternallyVisible()) |
| 5593 | return External; |
| 5594 | |
| 5595 | // C99 inline semantics, where the symbol is not externally visible. |
| 5596 | return GVA_C99Inline; |
| 5597 | } |
| 5598 | |
| 5599 | // C++0x [temp.explicit]p9: |
| 5600 | // [ Note: The intent is that an inline function that is the subject of |
| 5601 | // an explicit instantiation declaration will still be implicitly |
| 5602 | // instantiated when used so that the body can be considered for |
| 5603 | // inlining, but that no out-of-line copy of the inline function would be |
| 5604 | // generated in the translation unit. -- end note ] |
| 5605 | if (FD->getTemplateSpecializationKind() |
| 5606 | == TSK_ExplicitInstantiationDeclaration) |
| 5607 | return GVA_C99Inline; |
| 5608 | |
| 5609 | return GVA_CXXInline; |
| 5610 | } |
| 5611 | |
| 5612 | GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) { |
| 5613 | // If this is a static data member, compute the kind of template |
| 5614 | // specialization. Otherwise, this variable is not part of a |
| 5615 | // template. |
| 5616 | TemplateSpecializationKind TSK = TSK_Undeclared; |
| 5617 | if (VD->isStaticDataMember()) |
| 5618 | TSK = VD->getTemplateSpecializationKind(); |
| 5619 | |
| 5620 | Linkage L = VD->getLinkage(); |
| 5621 | if (L == ExternalLinkage && getLangOptions().CPlusPlus && |
| 5622 | VD->getType()->getLinkage() == UniqueExternalLinkage) |
| 5623 | L = UniqueExternalLinkage; |
| 5624 | |
| 5625 | switch (L) { |
| 5626 | case NoLinkage: |
| 5627 | case InternalLinkage: |
| 5628 | case UniqueExternalLinkage: |
| 5629 | return GVA_Internal; |
| 5630 | |
| 5631 | case ExternalLinkage: |
| 5632 | switch (TSK) { |
| 5633 | case TSK_Undeclared: |
| 5634 | case TSK_ExplicitSpecialization: |
| 5635 | return GVA_StrongExternal; |
| 5636 | |
| 5637 | case TSK_ExplicitInstantiationDeclaration: |
| 5638 | llvm_unreachable("Variable should not be instantiated"); |
| 5639 | // Fall through to treat this like any other instantiation. |
| 5640 | |
| 5641 | case TSK_ExplicitInstantiationDefinition: |
| 5642 | return GVA_ExplicitTemplateInstantiation; |
| 5643 | |
| 5644 | case TSK_ImplicitInstantiation: |
| 5645 | return GVA_TemplateInstantiation; |
| 5646 | } |
| 5647 | } |
| 5648 | |
| 5649 | return GVA_StrongExternal; |
| 5650 | } |
| 5651 | |
Argyrios Kyrtzidis | 4ac7c0b | 2010-07-29 20:08:05 +0000 | [diff] [blame] | 5652 | bool ASTContext::DeclMustBeEmitted(const Decl *D) { |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5653 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 5654 | if (!VD->isFileVarDecl()) |
| 5655 | return false; |
| 5656 | } else if (!isa<FunctionDecl>(D)) |
| 5657 | return false; |
| 5658 | |
Argyrios Kyrtzidis | ab411c8 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 5659 | // Weak references don't produce any output by themselves. |
| 5660 | if (D->hasAttr<WeakRefAttr>()) |
| 5661 | return false; |
| 5662 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5663 | // Aliases and used decls are required. |
| 5664 | if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>()) |
| 5665 | return true; |
| 5666 | |
| 5667 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 5668 | // Forward declarations aren't required. |
| 5669 | if (!FD->isThisDeclarationADefinition()) |
| 5670 | return false; |
| 5671 | |
| 5672 | // Constructors and destructors are required. |
| 5673 | if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>()) |
| 5674 | return true; |
| 5675 | |
| 5676 | // The key function for a class is required. |
| 5677 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 5678 | const CXXRecordDecl *RD = MD->getParent(); |
| 5679 | if (MD->isOutOfLine() && RD->isDynamicClass()) { |
| 5680 | const CXXMethodDecl *KeyFunc = getKeyFunction(RD); |
| 5681 | if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl()) |
| 5682 | return true; |
| 5683 | } |
| 5684 | } |
| 5685 | |
| 5686 | GVALinkage Linkage = GetGVALinkageForFunction(FD); |
| 5687 | |
| 5688 | // static, static inline, always_inline, and extern inline functions can |
| 5689 | // always be deferred. Normal inline functions can be deferred in C99/C++. |
| 5690 | // Implicit template instantiations can also be deferred in C++. |
| 5691 | if (Linkage == GVA_Internal || Linkage == GVA_C99Inline || |
| 5692 | Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) |
| 5693 | return false; |
| 5694 | return true; |
| 5695 | } |
| 5696 | |
| 5697 | const VarDecl *VD = cast<VarDecl>(D); |
| 5698 | assert(VD->isFileVarDecl() && "Expected file scoped var"); |
| 5699 | |
Argyrios Kyrtzidis | ab411c8 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 5700 | if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly) |
| 5701 | return false; |
| 5702 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5703 | // Structs that have non-trivial constructors or destructors are required. |
| 5704 | |
| 5705 | // FIXME: Handle references. |
| 5706 | if (const RecordType *RT = VD->getType()->getAs<RecordType>()) { |
| 5707 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5708 | if (RD->hasDefinition() && |
| 5709 | (!RD->hasTrivialConstructor() || !RD->hasTrivialDestructor())) |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 5710 | return true; |
| 5711 | } |
| 5712 | } |
| 5713 | |
| 5714 | GVALinkage L = GetGVALinkageForVariable(VD); |
| 5715 | if (L == GVA_Internal || L == GVA_TemplateInstantiation) { |
| 5716 | if (!(VD->getInit() && VD->getInit()->HasSideEffects(*this))) |
| 5717 | return false; |
| 5718 | } |
| 5719 | |
| 5720 | return true; |
| 5721 | } |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 5722 | |
Charles Davis | ee743f9 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 5723 | CallingConv ASTContext::getDefaultMethodCallConv() { |
| 5724 | // Pass through to the C++ ABI object |
| 5725 | return ABI->getDefaultMethodCallConv(); |
| 5726 | } |
| 5727 | |
Anders Carlsson | dae0cb5 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 5728 | bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) { |
| 5729 | // Pass through to the C++ ABI object |
| 5730 | return ABI->isNearlyEmpty(RD); |
| 5731 | } |
| 5732 | |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 5733 | CXXABI::~CXXABI() {} |