Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the ASTContext interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/ASTContext.h" |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 15 | #include "clang/AST/CharUnits.h" |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclCXX.h" |
Steve Naroff | 980e508 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclTemplate.h" |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeLoc.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 20 | #include "clang/AST/Expr.h" |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 21 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExternalASTSource.h" |
Anders Carlsson | 19cc4ab | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 23 | #include "clang/AST/RecordLayout.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 24 | #include "clang/Basic/Builtins.h" |
Chris Lattner | a9376d4 | 2009-03-28 03:45:20 +0000 | [diff] [blame] | 25 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 26 | #include "clang/Basic/TargetInfo.h" |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SmallString.h" |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/StringExtras.h" |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 29 | #include "llvm/Support/MathExtras.h" |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 30 | #include "llvm/Support/raw_ostream.h" |
Anders Carlsson | 29445a0 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 31 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 32 | using namespace clang; |
| 33 | |
| 34 | enum FloatingRank { |
| 35 | FloatRank, DoubleRank, LongDoubleRank |
| 36 | }; |
| 37 | |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 38 | ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM, |
Daniel Dunbar | 444be73 | 2009-11-13 05:51:54 +0000 | [diff] [blame] | 39 | const TargetInfo &t, |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 40 | IdentifierTable &idents, SelectorTable &sels, |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 41 | Builtin::Context &builtins, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 42 | bool FreeMem, unsigned size_reserve) : |
| 43 | GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0), |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 44 | NSConstantStringTypeDecl(0), |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 45 | ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0), |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 46 | sigjmp_bufDecl(0), BlockDescriptorType(0), BlockDescriptorExtendedType(0), |
Douglas Gregor | c6fbbed | 2010-03-19 22:13:20 +0000 | [diff] [blame] | 47 | SourceMgr(SM), LangOpts(LOpts), FreeMemory(FreeMem), Target(t), |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 48 | Idents(idents), Selectors(sels), |
Ted Kremenek | ac9590e | 2010-05-10 20:40:08 +0000 | [diff] [blame] | 49 | BuiltinInfo(builtins), |
| 50 | DeclarationNames(*this), |
| 51 | ExternalSource(0), PrintingPolicy(LOpts), |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 52 | LastSDM(0, 0) { |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 53 | ObjCIdRedefinitionType = QualType(); |
| 54 | ObjCClassRedefinitionType = QualType(); |
Fariborz Jahanian | 369a3bd | 2009-11-25 23:07:42 +0000 | [diff] [blame] | 55 | ObjCSelRedefinitionType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 56 | if (size_reserve > 0) Types.reserve(size_reserve); |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 57 | TUDecl = TranslationUnitDecl::Create(*this); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 58 | InitBuiltinTypes(); |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 61 | ASTContext::~ASTContext() { |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 62 | // Release the DenseMaps associated with DeclContext objects. |
| 63 | // FIXME: Is this the ideal solution? |
| 64 | ReleaseDeclContextMaps(); |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 65 | |
Douglas Gregor | 0054531 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 66 | if (!FreeMemory) { |
| 67 | // Call all of the deallocation functions. |
| 68 | for (unsigned I = 0, N = Deallocations.size(); I != N; ++I) |
| 69 | Deallocations[I].first(Deallocations[I].second); |
| 70 | } |
| 71 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 72 | // Release all of the memory associated with overridden C++ methods. |
| 73 | for (llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::iterator |
| 74 | OM = OverriddenMethods.begin(), OMEnd = OverriddenMethods.end(); |
| 75 | OM != OMEnd; ++OM) |
| 76 | OM->second.Destroy(); |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 77 | |
Ted Kremenek | bbfd68d | 2009-12-23 21:13:52 +0000 | [diff] [blame] | 78 | if (FreeMemory) { |
| 79 | // Deallocate all the types. |
| 80 | while (!Types.empty()) { |
| 81 | Types.back()->Destroy(*this); |
| 82 | Types.pop_back(); |
| 83 | } |
Eli Friedman | b26153c | 2008-05-27 03:08:09 +0000 | [diff] [blame] | 84 | |
Ted Kremenek | bbfd68d | 2009-12-23 21:13:52 +0000 | [diff] [blame] | 85 | for (llvm::FoldingSet<ExtQuals>::iterator |
| 86 | I = ExtQualNodes.begin(), E = ExtQualNodes.end(); I != E; ) { |
| 87 | // Increment in loop to prevent using deallocated memory. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 88 | Deallocate(&*I++); |
Nuno Lopes | b74668e | 2008-12-17 22:30:25 +0000 | [diff] [blame] | 89 | } |
Nuno Lopes | b74668e | 2008-12-17 22:30:25 +0000 | [diff] [blame] | 90 | |
Ted Kremenek | 503524a | 2010-03-08 20:56:29 +0000 | [diff] [blame] | 91 | for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator |
| 92 | I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) { |
| 93 | // Increment in loop to prevent using deallocated memory. |
| 94 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 95 | R->Destroy(*this); |
| 96 | } |
Ted Kremenek | bbfd68d | 2009-12-23 21:13:52 +0000 | [diff] [blame] | 97 | |
Ted Kremenek | 503524a | 2010-03-08 20:56:29 +0000 | [diff] [blame] | 98 | for (llvm::DenseMap<const ObjCContainerDecl*, |
| 99 | const ASTRecordLayout*>::iterator |
| 100 | I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) { |
| 101 | // Increment in loop to prevent using deallocated memory. |
| 102 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 103 | R->Destroy(*this); |
| 104 | } |
Nuno Lopes | b74668e | 2008-12-17 22:30:25 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 107 | // Destroy nested-name-specifiers. |
Douglas Gregor | 1ae0afa | 2009-03-27 23:54:10 +0000 | [diff] [blame] | 108 | for (llvm::FoldingSet<NestedNameSpecifier>::iterator |
| 109 | NNS = NestedNameSpecifiers.begin(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 110 | NNSEnd = NestedNameSpecifiers.end(); |
Ted Kremenek | bbfd68d | 2009-12-23 21:13:52 +0000 | [diff] [blame] | 111 | NNS != NNSEnd; ) { |
| 112 | // Increment in loop to prevent using deallocated memory. |
Douglas Gregor | 1ae0afa | 2009-03-27 23:54:10 +0000 | [diff] [blame] | 113 | (*NNS++).Destroy(*this); |
Ted Kremenek | bbfd68d | 2009-12-23 21:13:52 +0000 | [diff] [blame] | 114 | } |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 115 | |
| 116 | if (GlobalNestedNameSpecifier) |
| 117 | GlobalNestedNameSpecifier->Destroy(*this); |
| 118 | |
Eli Friedman | b26153c | 2008-05-27 03:08:09 +0000 | [diff] [blame] | 119 | TUDecl->Destroy(*this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Douglas Gregor | 0054531 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 122 | void ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) { |
| 123 | Deallocations.push_back(std::make_pair(Callback, Data)); |
| 124 | } |
| 125 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 126 | void |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 127 | ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) { |
| 128 | ExternalSource.reset(Source.take()); |
| 129 | } |
| 130 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 131 | void ASTContext::PrintStats() const { |
| 132 | fprintf(stderr, "*** AST Context Stats:\n"); |
| 133 | fprintf(stderr, " %d types total.\n", (int)Types.size()); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 134 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 135 | unsigned counts[] = { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 136 | #define TYPE(Name, Parent) 0, |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 137 | #define ABSTRACT_TYPE(Name, Parent) |
| 138 | #include "clang/AST/TypeNodes.def" |
| 139 | 0 // Extra |
| 140 | }; |
Douglas Gregor | c2ee10d | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 141 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 142 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 143 | Type *T = Types[i]; |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 144 | counts[(unsigned)T->getTypeClass()]++; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 147 | unsigned Idx = 0; |
| 148 | unsigned TotalBytes = 0; |
| 149 | #define TYPE(Name, Parent) \ |
| 150 | if (counts[Idx]) \ |
| 151 | fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \ |
| 152 | TotalBytes += counts[Idx] * sizeof(Name##Type); \ |
| 153 | ++Idx; |
| 154 | #define ABSTRACT_TYPE(Name, Parent) |
| 155 | #include "clang/AST/TypeNodes.def" |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 156 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 157 | fprintf(stderr, "Total bytes = %d\n", int(TotalBytes)); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 158 | |
| 159 | if (ExternalSource.get()) { |
| 160 | fprintf(stderr, "\n"); |
| 161 | ExternalSource->PrintStats(); |
| 162 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 166 | void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 167 | BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 168 | R = CanQualType::CreateUnsafe(QualType(Ty, 0)); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 169 | Types.push_back(Ty); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 172 | void ASTContext::InitBuiltinTypes() { |
| 173 | assert(VoidTy.isNull() && "Context reinitialized?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 174 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 175 | // C99 6.2.5p19. |
| 176 | InitBuiltinType(VoidTy, BuiltinType::Void); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 177 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 178 | // C99 6.2.5p2. |
| 179 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
| 180 | // C99 6.2.5p3. |
Eli Friedman | 15b9176 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 181 | if (LangOpts.CharIsSigned) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 182 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 183 | else |
| 184 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
| 185 | // C99 6.2.5p4. |
| 186 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 187 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 188 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 189 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 190 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 191 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 192 | // C99 6.2.5p6. |
| 193 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 194 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 195 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 196 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 197 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 198 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 199 | // C99 6.2.5p10. |
| 200 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 201 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 202 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 203 | |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 204 | // GNU extension, 128-bit integers. |
| 205 | InitBuiltinType(Int128Ty, BuiltinType::Int128); |
| 206 | InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128); |
| 207 | |
Chris Lattner | 3a25032 | 2009-02-26 23:43:47 +0000 | [diff] [blame] | 208 | if (LangOpts.CPlusPlus) // C++ 3.9.1p5 |
| 209 | InitBuiltinType(WCharTy, BuiltinType::WChar); |
| 210 | else // C99 |
| 211 | WCharTy = getFromTargetType(Target.getWCharType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 212 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 213 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 214 | InitBuiltinType(Char16Ty, BuiltinType::Char16); |
| 215 | else // C99 |
| 216 | Char16Ty = getFromTargetType(Target.getChar16Type()); |
| 217 | |
| 218 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 219 | InitBuiltinType(Char32Ty, BuiltinType::Char32); |
| 220 | else // C99 |
| 221 | Char32Ty = getFromTargetType(Target.getChar32Type()); |
| 222 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 223 | // Placeholder type for functions. |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 224 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
| 225 | |
| 226 | // Placeholder type for type-dependent expressions whose type is |
| 227 | // completely unknown. No code should ever check a type against |
| 228 | // DependentTy and users should never see it; however, it is here to |
| 229 | // help diagnose failures to properly check for type-dependent |
| 230 | // expressions. |
| 231 | InitBuiltinType(DependentTy, BuiltinType::Dependent); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 232 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 233 | // Placeholder type for C++0x auto declarations whose real type has |
Anders Carlsson | e89d159 | 2009-06-26 18:41:36 +0000 | [diff] [blame] | 234 | // not yet been deduced. |
| 235 | InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 236 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 237 | // C99 6.2.5p11. |
| 238 | FloatComplexTy = getComplexType(FloatTy); |
| 239 | DoubleComplexTy = getComplexType(DoubleTy); |
| 240 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 241 | |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 242 | BuiltinVaListType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 243 | |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 244 | // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope(). |
| 245 | ObjCIdTypedefType = QualType(); |
| 246 | ObjCClassTypedefType = QualType(); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 247 | ObjCSelTypedefType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 248 | |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 249 | // Builtin types for 'id', 'Class', and 'SEL'. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 250 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); |
| 251 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 252 | InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 253 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 254 | ObjCConstantStringType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 255 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 256 | // void * type |
| 257 | VoidPtrTy = getPointerType(VoidTy); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 258 | |
| 259 | // nullptr type (C++0x 2.14.7) |
| 260 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 263 | MemberSpecializationInfo * |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 264 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 265 | assert(Var->isStaticDataMember() && "Not a static data member"); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 266 | llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 267 | = InstantiatedFromStaticDataMember.find(Var); |
| 268 | if (Pos == InstantiatedFromStaticDataMember.end()) |
| 269 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 270 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 271 | return Pos->second; |
| 272 | } |
| 273 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 274 | void |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 275 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, |
| 276 | TemplateSpecializationKind TSK) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 277 | assert(Inst->isStaticDataMember() && "Not a static data member"); |
| 278 | assert(Tmpl->isStaticDataMember() && "Not a static data member"); |
| 279 | assert(!InstantiatedFromStaticDataMember[Inst] && |
| 280 | "Already noted what static data member was instantiated from"); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 281 | InstantiatedFromStaticDataMember[Inst] |
| 282 | = new (*this) MemberSpecializationInfo(Tmpl, TSK); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 283 | } |
| 284 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 285 | NamedDecl * |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 286 | ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 287 | llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 288 | = InstantiatedFromUsingDecl.find(UUD); |
| 289 | if (Pos == InstantiatedFromUsingDecl.end()) |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 290 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 291 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 292 | return Pos->second; |
| 293 | } |
| 294 | |
| 295 | void |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 296 | ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) { |
| 297 | assert((isa<UsingDecl>(Pattern) || |
| 298 | isa<UnresolvedUsingValueDecl>(Pattern) || |
| 299 | isa<UnresolvedUsingTypenameDecl>(Pattern)) && |
| 300 | "pattern decl is not a using decl"); |
| 301 | assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists"); |
| 302 | InstantiatedFromUsingDecl[Inst] = Pattern; |
| 303 | } |
| 304 | |
| 305 | UsingShadowDecl * |
| 306 | ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) { |
| 307 | llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos |
| 308 | = InstantiatedFromUsingShadowDecl.find(Inst); |
| 309 | if (Pos == InstantiatedFromUsingShadowDecl.end()) |
| 310 | return 0; |
| 311 | |
| 312 | return Pos->second; |
| 313 | } |
| 314 | |
| 315 | void |
| 316 | ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, |
| 317 | UsingShadowDecl *Pattern) { |
| 318 | assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists"); |
| 319 | InstantiatedFromUsingShadowDecl[Inst] = Pattern; |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 322 | FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { |
| 323 | llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos |
| 324 | = InstantiatedFromUnnamedFieldDecl.find(Field); |
| 325 | if (Pos == InstantiatedFromUnnamedFieldDecl.end()) |
| 326 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 327 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 328 | return Pos->second; |
| 329 | } |
| 330 | |
| 331 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, |
| 332 | FieldDecl *Tmpl) { |
| 333 | assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed"); |
| 334 | assert(!Tmpl->getDeclName() && "Template field decl is not unnamed"); |
| 335 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] && |
| 336 | "Already noted what unnamed field was instantiated from"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 337 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 338 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; |
| 339 | } |
| 340 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 341 | ASTContext::overridden_cxx_method_iterator |
| 342 | ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const { |
| 343 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 344 | = OverriddenMethods.find(Method); |
| 345 | if (Pos == OverriddenMethods.end()) |
| 346 | return 0; |
| 347 | |
| 348 | return Pos->second.begin(); |
| 349 | } |
| 350 | |
| 351 | ASTContext::overridden_cxx_method_iterator |
| 352 | ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const { |
| 353 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 354 | = OverriddenMethods.find(Method); |
| 355 | if (Pos == OverriddenMethods.end()) |
| 356 | return 0; |
| 357 | |
| 358 | return Pos->second.end(); |
| 359 | } |
| 360 | |
| 361 | void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method, |
| 362 | const CXXMethodDecl *Overridden) { |
| 363 | OverriddenMethods[Method].push_back(Overridden); |
| 364 | } |
| 365 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 366 | namespace { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 367 | class BeforeInTranslationUnit |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 368 | : std::binary_function<SourceRange, SourceRange, bool> { |
| 369 | SourceManager *SourceMgr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 370 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 371 | public: |
| 372 | explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 373 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 374 | bool operator()(SourceRange X, SourceRange Y) { |
| 375 | return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin()); |
| 376 | } |
| 377 | }; |
| 378 | } |
| 379 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 380 | //===----------------------------------------------------------------------===// |
| 381 | // Type Sizing and Analysis |
| 382 | //===----------------------------------------------------------------------===// |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 383 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 384 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 385 | /// scalar floating point type. |
| 386 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 387 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 388 | assert(BT && "Not a floating point type!"); |
| 389 | switch (BT->getKind()) { |
| 390 | default: assert(0 && "Not a floating point type!"); |
| 391 | case BuiltinType::Float: return Target.getFloatFormat(); |
| 392 | case BuiltinType::Double: return Target.getDoubleFormat(); |
| 393 | case BuiltinType::LongDouble: return Target.getLongDoubleFormat(); |
| 394 | } |
| 395 | } |
| 396 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 397 | /// getDeclAlign - Return a conservative estimate of the alignment of the |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 398 | /// specified decl. Note that bitfields do not have a valid alignment, so |
| 399 | /// this method will assert on them. |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 400 | /// If @p RefAsPointee, references are treated like their underlying type |
| 401 | /// (for alignof), else they're treated like pointers (for CodeGen). |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 402 | CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) { |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 403 | unsigned Align = Target.getCharWidth(); |
| 404 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 405 | if (const AlignedAttr* AA = D->getAttr<AlignedAttr>()) |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 406 | Align = std::max(Align, AA->getMaxAlignment()); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 407 | |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 408 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
| 409 | QualType T = VD->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 410 | if (const ReferenceType* RT = T->getAs<ReferenceType>()) { |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 411 | if (RefAsPointee) |
| 412 | T = RT->getPointeeType(); |
| 413 | else |
| 414 | T = getPointerType(RT->getPointeeType()); |
| 415 | } |
| 416 | if (!T->isIncompleteType() && !T->isFunctionType()) { |
Anders Carlsson | 4cc2cfd | 2009-04-10 04:47:03 +0000 | [diff] [blame] | 417 | // Incomplete or function types default to 1. |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 418 | while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T)) |
| 419 | T = cast<ArrayType>(T)->getElementType(); |
| 420 | |
| 421 | Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); |
| 422 | } |
Charles Davis | 05f6247 | 2010-02-23 04:52:00 +0000 | [diff] [blame] | 423 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) { |
| 424 | // In the case of a field in a packed struct, we want the minimum |
| 425 | // of the alignment of the field and the alignment of the struct. |
| 426 | Align = std::min(Align, |
| 427 | getPreferredTypeAlign(FD->getParent()->getTypeForDecl())); |
| 428 | } |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 429 | } |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 430 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 431 | return CharUnits::fromQuantity(Align / Target.getCharWidth()); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 432 | } |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 433 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 434 | std::pair<CharUnits, CharUnits> |
| 435 | ASTContext::getTypeInfoInChars(const Type *T) { |
| 436 | std::pair<uint64_t, unsigned> Info = getTypeInfo(T); |
| 437 | return std::make_pair(CharUnits::fromQuantity(Info.first / getCharWidth()), |
| 438 | CharUnits::fromQuantity(Info.second / getCharWidth())); |
| 439 | } |
| 440 | |
| 441 | std::pair<CharUnits, CharUnits> |
| 442 | ASTContext::getTypeInfoInChars(QualType T) { |
| 443 | return getTypeInfoInChars(T.getTypePtr()); |
| 444 | } |
| 445 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 446 | /// getTypeSize - Return the size of the specified type, in bits. This method |
| 447 | /// does not work on incomplete types. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 448 | /// |
| 449 | /// FIXME: Pointers into different addr spaces could have different sizes and |
| 450 | /// alignment requirements: getPointerInfo should take an AddrSpace, this |
| 451 | /// should take a QualType, &c. |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 452 | std::pair<uint64_t, unsigned> |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 453 | ASTContext::getTypeInfo(const Type *T) { |
Mike Stump | 5e30100 | 2009-02-27 18:32:39 +0000 | [diff] [blame] | 454 | uint64_t Width=0; |
| 455 | unsigned Align=8; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 456 | switch (T->getTypeClass()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 457 | #define TYPE(Class, Base) |
| 458 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 459 | #define NON_CANONICAL_TYPE(Class, Base) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 460 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 461 | #include "clang/AST/TypeNodes.def" |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 462 | assert(false && "Should not see dependent types"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 463 | break; |
| 464 | |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 465 | case Type::FunctionNoProto: |
| 466 | case Type::FunctionProto: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 467 | // GCC extension: alignof(function) = 32 bits |
| 468 | Width = 0; |
| 469 | Align = 32; |
| 470 | break; |
| 471 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 472 | case Type::IncompleteArray: |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 473 | case Type::VariableArray: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 474 | Width = 0; |
| 475 | Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); |
| 476 | break; |
| 477 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 478 | case Type::ConstantArray: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 479 | const ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 480 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 481 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 482 | Width = EltInfo.first*CAT->getSize().getZExtValue(); |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 483 | Align = EltInfo.second; |
| 484 | break; |
Christopher Lamb | 5c09a02 | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 485 | } |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 486 | case Type::ExtVector: |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 487 | case Type::Vector: { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 488 | const VectorType *VT = cast<VectorType>(T); |
| 489 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType()); |
| 490 | Width = EltInfo.first*VT->getNumElements(); |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 491 | Align = Width; |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 492 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 493 | // This happens for non-power-of-2 length vectors. |
Dan Gohman | 8eefcd3 | 2010-04-21 23:32:43 +0000 | [diff] [blame] | 494 | if (Align & (Align-1)) { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 495 | Align = llvm::NextPowerOf2(Align); |
| 496 | Width = llvm::RoundUpToAlignment(Width, Align); |
| 497 | } |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 498 | break; |
| 499 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 500 | |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 501 | case Type::Builtin: |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 502 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 503 | default: assert(0 && "Unknown builtin type!"); |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 504 | case BuiltinType::Void: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 505 | // GCC extension: alignof(void) = 8 bits. |
| 506 | Width = 0; |
| 507 | Align = 8; |
| 508 | break; |
| 509 | |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 510 | case BuiltinType::Bool: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 511 | Width = Target.getBoolWidth(); |
| 512 | Align = Target.getBoolAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 513 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 514 | case BuiltinType::Char_S: |
| 515 | case BuiltinType::Char_U: |
| 516 | case BuiltinType::UChar: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 517 | case BuiltinType::SChar: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 518 | Width = Target.getCharWidth(); |
| 519 | Align = Target.getCharAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 520 | break; |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 521 | case BuiltinType::WChar: |
| 522 | Width = Target.getWCharWidth(); |
| 523 | Align = Target.getWCharAlign(); |
| 524 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 525 | case BuiltinType::Char16: |
| 526 | Width = Target.getChar16Width(); |
| 527 | Align = Target.getChar16Align(); |
| 528 | break; |
| 529 | case BuiltinType::Char32: |
| 530 | Width = Target.getChar32Width(); |
| 531 | Align = Target.getChar32Align(); |
| 532 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 533 | case BuiltinType::UShort: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 534 | case BuiltinType::Short: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 535 | Width = Target.getShortWidth(); |
| 536 | Align = Target.getShortAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 537 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 538 | case BuiltinType::UInt: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 539 | case BuiltinType::Int: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 540 | Width = Target.getIntWidth(); |
| 541 | Align = Target.getIntAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 542 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 543 | case BuiltinType::ULong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 544 | case BuiltinType::Long: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 545 | Width = Target.getLongWidth(); |
| 546 | Align = Target.getLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 547 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 548 | case BuiltinType::ULongLong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 549 | case BuiltinType::LongLong: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 550 | Width = Target.getLongLongWidth(); |
| 551 | Align = Target.getLongLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 552 | break; |
Chris Lattner | ec16cb9 | 2009-04-30 02:55:13 +0000 | [diff] [blame] | 553 | case BuiltinType::Int128: |
| 554 | case BuiltinType::UInt128: |
| 555 | Width = 128; |
| 556 | Align = 128; // int128_t is 128-bit aligned on all targets. |
| 557 | break; |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 558 | case BuiltinType::Float: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 559 | Width = Target.getFloatWidth(); |
| 560 | Align = Target.getFloatAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 561 | break; |
| 562 | case BuiltinType::Double: |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 563 | Width = Target.getDoubleWidth(); |
| 564 | Align = Target.getDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 565 | break; |
| 566 | case BuiltinType::LongDouble: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 567 | Width = Target.getLongDoubleWidth(); |
| 568 | Align = Target.getLongDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 569 | break; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 570 | case BuiltinType::NullPtr: |
| 571 | Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) |
| 572 | Align = Target.getPointerAlign(0); // == sizeof(void*) |
Sebastian Redl | 1590d9c | 2009-05-27 19:34:06 +0000 | [diff] [blame] | 573 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 574 | } |
Chris Lattner | bfef6d7 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 575 | break; |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 576 | case Type::ObjCObjectPointer: |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 577 | Width = Target.getPointerWidth(0); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 578 | Align = Target.getPointerAlign(0); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 579 | break; |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 580 | case Type::BlockPointer: { |
| 581 | unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace(); |
| 582 | Width = Target.getPointerWidth(AS); |
| 583 | Align = Target.getPointerAlign(AS); |
| 584 | break; |
| 585 | } |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 586 | case Type::LValueReference: |
| 587 | case Type::RValueReference: { |
| 588 | // alignof and sizeof should never enter this code path here, so we go |
| 589 | // the pointer route. |
| 590 | unsigned AS = cast<ReferenceType>(T)->getPointeeType().getAddressSpace(); |
| 591 | Width = Target.getPointerWidth(AS); |
| 592 | Align = Target.getPointerAlign(AS); |
| 593 | break; |
| 594 | } |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 595 | case Type::Pointer: { |
| 596 | unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace(); |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 597 | Width = Target.getPointerWidth(AS); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 598 | Align = Target.getPointerAlign(AS); |
| 599 | break; |
| 600 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 601 | case Type::MemberPointer: { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 602 | QualType Pointee = cast<MemberPointerType>(T)->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 603 | std::pair<uint64_t, unsigned> PtrDiffInfo = |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 604 | getTypeInfo(getPointerDiffType()); |
| 605 | Width = PtrDiffInfo.first; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 606 | if (Pointee->isFunctionType()) |
| 607 | Width *= 2; |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 608 | Align = PtrDiffInfo.second; |
| 609 | break; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 610 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 611 | case Type::Complex: { |
| 612 | // Complex types have the same alignment as their elements, but twice the |
| 613 | // size. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 614 | std::pair<uint64_t, unsigned> EltInfo = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 615 | getTypeInfo(cast<ComplexType>(T)->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 616 | Width = EltInfo.first*2; |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 617 | Align = EltInfo.second; |
| 618 | break; |
| 619 | } |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 620 | case Type::ObjCObject: |
| 621 | return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr()); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 622 | case Type::ObjCInterface: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 623 | const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 624 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
| 625 | Width = Layout.getSize(); |
| 626 | Align = Layout.getAlignment(); |
| 627 | break; |
| 628 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 629 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 630 | case Type::Enum: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 631 | const TagType *TT = cast<TagType>(T); |
| 632 | |
| 633 | if (TT->getDecl()->isInvalidDecl()) { |
Chris Lattner | 8389eab | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 634 | Width = 1; |
| 635 | Align = 1; |
| 636 | break; |
| 637 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 638 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 639 | if (const EnumType *ET = dyn_cast<EnumType>(TT)) |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 640 | return getTypeInfo(ET->getDecl()->getIntegerType()); |
| 641 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 642 | const RecordType *RT = cast<RecordType>(TT); |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 643 | const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); |
| 644 | Width = Layout.getSize(); |
| 645 | Align = Layout.getAlignment(); |
Chris Lattner | dc0d73e | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 646 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 647 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 648 | |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 649 | case Type::SubstTemplateTypeParm: |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 650 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> |
| 651 | getReplacementType().getTypePtr()); |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 652 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 653 | case Type::Typedef: { |
| 654 | const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl(); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 655 | if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 656 | Align = std::max(Aligned->getMaxAlignment(), |
| 657 | getTypeAlign(Typedef->getUnderlyingType().getTypePtr())); |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 658 | Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr()); |
| 659 | } else |
| 660 | return getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 661 | break; |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 662 | } |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 663 | |
| 664 | case Type::TypeOfExpr: |
| 665 | return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType() |
| 666 | .getTypePtr()); |
| 667 | |
| 668 | case Type::TypeOf: |
| 669 | return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr()); |
| 670 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 671 | case Type::Decltype: |
| 672 | return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType() |
| 673 | .getTypePtr()); |
| 674 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 675 | case Type::Elaborated: |
| 676 | return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 677 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 678 | case Type::TemplateSpecialization: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 679 | assert(getCanonicalType(T) != T && |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 680 | "Cannot request the size of a dependent type"); |
| 681 | // FIXME: this is likely to be wrong once we support template |
| 682 | // aliases, since a template alias could refer to a typedef that |
| 683 | // has an __aligned__ attribute on it. |
| 684 | return getTypeInfo(getCanonicalType(T)); |
| 685 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 686 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 687 | assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2"); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 688 | return std::make_pair(Width, Align); |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 691 | /// getTypeSizeInChars - Return the size of the specified type, in characters. |
| 692 | /// This method does not work on incomplete types. |
| 693 | CharUnits ASTContext::getTypeSizeInChars(QualType T) { |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 694 | return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth()); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 695 | } |
| 696 | CharUnits ASTContext::getTypeSizeInChars(const Type *T) { |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 697 | return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth()); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Ken Dyck | 16e20cc | 2010-01-26 17:25:18 +0000 | [diff] [blame] | 700 | /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 701 | /// characters. This method does not work on incomplete types. |
| 702 | CharUnits ASTContext::getTypeAlignInChars(QualType T) { |
| 703 | return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth()); |
| 704 | } |
| 705 | CharUnits ASTContext::getTypeAlignInChars(const Type *T) { |
| 706 | return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth()); |
| 707 | } |
| 708 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 709 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 710 | /// type for the current target in bits. This can be different than the ABI |
| 711 | /// alignment in cases where it is beneficial for performance to overalign |
| 712 | /// a data type. |
| 713 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) { |
| 714 | unsigned ABIAlign = getTypeAlign(T); |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 715 | |
| 716 | // Double and long long should be naturally aligned if possible. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 717 | if (const ComplexType* CT = T->getAs<ComplexType>()) |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 718 | T = CT->getElementType().getTypePtr(); |
| 719 | if (T->isSpecificBuiltinType(BuiltinType::Double) || |
| 720 | T->isSpecificBuiltinType(BuiltinType::LongLong)) |
| 721 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); |
| 722 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 723 | return ABIAlign; |
| 724 | } |
| 725 | |
Daniel Dunbar | a80a0f6 | 2009-04-22 17:43:55 +0000 | [diff] [blame] | 726 | static void CollectLocalObjCIvars(ASTContext *Ctx, |
| 727 | const ObjCInterfaceDecl *OI, |
| 728 | llvm::SmallVectorImpl<FieldDecl*> &Fields) { |
Fariborz Jahanian | a769c00 | 2008-12-17 21:40:49 +0000 | [diff] [blame] | 729 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 730 | E = OI->ivar_end(); I != E; ++I) { |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 731 | ObjCIvarDecl *IVDecl = *I; |
Fariborz Jahanian | a769c00 | 2008-12-17 21:40:49 +0000 | [diff] [blame] | 732 | if (!IVDecl->isInvalidDecl()) |
| 733 | Fields.push_back(cast<FieldDecl>(IVDecl)); |
| 734 | } |
| 735 | } |
| 736 | |
Daniel Dunbar | a80a0f6 | 2009-04-22 17:43:55 +0000 | [diff] [blame] | 737 | void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 738 | llvm::SmallVectorImpl<FieldDecl*> &Fields) { |
| 739 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
| 740 | CollectObjCIvars(SuperClass, Fields); |
| 741 | CollectLocalObjCIvars(this, OI, Fields); |
| 742 | } |
| 743 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 744 | /// ShallowCollectObjCIvars - |
| 745 | /// Collect all ivars, including those synthesized, in the current class. |
| 746 | /// |
| 747 | void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI, |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 748 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) { |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 749 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 750 | E = OI->ivar_end(); I != E; ++I) { |
| 751 | Ivars.push_back(*I); |
| 752 | } |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 753 | |
| 754 | CollectNonClassIvars(OI, Ivars); |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 755 | } |
| 756 | |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 757 | /// CollectNonClassIvars - |
| 758 | /// This routine collects all other ivars which are not declared in the class. |
Ted Kremenek | 7d2aa11 | 2010-03-11 19:44:54 +0000 | [diff] [blame] | 759 | /// This includes synthesized ivars (via @synthesize) and those in |
| 760 | // class's @implementation. |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 761 | /// |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 762 | void ASTContext::CollectNonClassIvars(const ObjCInterfaceDecl *OI, |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 763 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) { |
Fariborz Jahanian | 0e5ad25 | 2010-02-23 01:26:30 +0000 | [diff] [blame] | 764 | // Find ivars declared in class extension. |
| 765 | if (const ObjCCategoryDecl *CDecl = OI->getClassExtension()) { |
| 766 | for (ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(), |
| 767 | E = CDecl->ivar_end(); I != E; ++I) { |
| 768 | Ivars.push_back(*I); |
| 769 | } |
| 770 | } |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 771 | |
Ted Kremenek | 7d2aa11 | 2010-03-11 19:44:54 +0000 | [diff] [blame] | 772 | // Also add any ivar defined in this class's implementation. This |
| 773 | // includes synthesized ivars. |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 774 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) { |
| 775 | for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(), |
| 776 | E = ImplDecl->ivar_end(); I != E; ++I) |
| 777 | Ivars.push_back(*I); |
| 778 | } |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 779 | } |
| 780 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 781 | /// CollectInheritedProtocols - Collect all protocols in current class and |
| 782 | /// those inherited by it. |
| 783 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 784 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 785 | if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
| 786 | for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(), |
| 787 | PE = OI->protocol_end(); P != PE; ++P) { |
| 788 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 789 | Protocols.insert(Proto); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 790 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 791 | PE = Proto->protocol_end(); P != PE; ++P) { |
| 792 | Protocols.insert(*P); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 793 | CollectInheritedProtocols(*P, Protocols); |
| 794 | } |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 795 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 796 | |
| 797 | // Categories of this Interface. |
| 798 | for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList(); |
| 799 | CDeclChain; CDeclChain = CDeclChain->getNextClassCategory()) |
| 800 | CollectInheritedProtocols(CDeclChain, Protocols); |
| 801 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) |
| 802 | while (SD) { |
| 803 | CollectInheritedProtocols(SD, Protocols); |
| 804 | SD = SD->getSuperClass(); |
| 805 | } |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 806 | } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 807 | for (ObjCInterfaceDecl::protocol_iterator P = OC->protocol_begin(), |
| 808 | PE = OC->protocol_end(); P != PE; ++P) { |
| 809 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 810 | Protocols.insert(Proto); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 811 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 812 | PE = Proto->protocol_end(); P != PE; ++P) |
| 813 | CollectInheritedProtocols(*P, Protocols); |
| 814 | } |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 815 | } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 816 | for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(), |
| 817 | PE = OP->protocol_end(); P != PE; ++P) { |
| 818 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 819 | Protocols.insert(Proto); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 820 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 821 | PE = Proto->protocol_end(); P != PE; ++P) |
| 822 | CollectInheritedProtocols(*P, Protocols); |
| 823 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 824 | } |
| 825 | } |
| 826 | |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 827 | unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) { |
| 828 | unsigned count = 0; |
| 829 | // Count ivars declared in class extension. |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 830 | if (const ObjCCategoryDecl *CDecl = OI->getClassExtension()) |
| 831 | count += CDecl->ivar_size(); |
| 832 | |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 833 | // Count ivar defined in this class's implementation. This |
| 834 | // includes synthesized ivars. |
| 835 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 836 | count += ImplDecl->ivar_size(); |
| 837 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 838 | return count; |
| 839 | } |
| 840 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 841 | /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. |
| 842 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
| 843 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 844 | I = ObjCImpls.find(D); |
| 845 | if (I != ObjCImpls.end()) |
| 846 | return cast<ObjCImplementationDecl>(I->second); |
| 847 | return 0; |
| 848 | } |
| 849 | /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. |
| 850 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
| 851 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 852 | I = ObjCImpls.find(D); |
| 853 | if (I != ObjCImpls.end()) |
| 854 | return cast<ObjCCategoryImplDecl>(I->second); |
| 855 | return 0; |
| 856 | } |
| 857 | |
| 858 | /// \brief Set the implementation of ObjCInterfaceDecl. |
| 859 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
| 860 | ObjCImplementationDecl *ImplD) { |
| 861 | assert(IFaceD && ImplD && "Passed null params"); |
| 862 | ObjCImpls[IFaceD] = ImplD; |
| 863 | } |
| 864 | /// \brief Set the implementation of ObjCCategoryDecl. |
| 865 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
| 866 | ObjCCategoryImplDecl *ImplD) { |
| 867 | assert(CatD && ImplD && "Passed null params"); |
| 868 | ObjCImpls[CatD] = ImplD; |
| 869 | } |
| 870 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 871 | /// \brief Allocate an uninitialized TypeSourceInfo. |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 872 | /// |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 873 | /// The caller should initialize the memory held by TypeSourceInfo using |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 874 | /// the TypeLoc wrappers. |
| 875 | /// |
| 876 | /// \param T the type that will be the basis for type source info. This type |
| 877 | /// should refer to how the declarator was written in source code, not to |
| 878 | /// what type semantic analysis resolved the declarator to. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 879 | TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T, |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 880 | unsigned DataSize) { |
| 881 | if (!DataSize) |
| 882 | DataSize = TypeLoc::getFullDataSizeForType(T); |
| 883 | else |
| 884 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 885 | "incorrect data size provided to CreateTypeSourceInfo!"); |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 886 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 887 | TypeSourceInfo *TInfo = |
| 888 | (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8); |
| 889 | new (TInfo) TypeSourceInfo(T); |
| 890 | return TInfo; |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 891 | } |
| 892 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 893 | TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, |
John McCall | a4eb74d | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 894 | SourceLocation L) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 895 | TypeSourceInfo *DI = CreateTypeSourceInfo(T); |
John McCall | a4eb74d | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 896 | DI->getTypeLoc().initialize(L); |
| 897 | return DI; |
| 898 | } |
| 899 | |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 900 | const ASTRecordLayout & |
| 901 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) { |
| 902 | return getObjCLayout(D, 0); |
| 903 | } |
| 904 | |
| 905 | const ASTRecordLayout & |
| 906 | ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) { |
| 907 | return getObjCLayout(D->getClassInterface(), D); |
| 908 | } |
| 909 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 910 | //===----------------------------------------------------------------------===// |
| 911 | // Type creation/memoization methods |
| 912 | //===----------------------------------------------------------------------===// |
| 913 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 914 | QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) { |
| 915 | unsigned Fast = Quals.getFastQualifiers(); |
| 916 | Quals.removeFastQualifiers(); |
| 917 | |
| 918 | // Check if we've already instantiated this type. |
| 919 | llvm::FoldingSetNodeID ID; |
| 920 | ExtQuals::Profile(ID, TypeNode, Quals); |
| 921 | void *InsertPos = 0; |
| 922 | if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) { |
| 923 | assert(EQ->getQualifiers() == Quals); |
| 924 | QualType T = QualType(EQ, Fast); |
| 925 | return T; |
| 926 | } |
| 927 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 928 | ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 929 | ExtQualNodes.InsertNode(New, InsertPos); |
| 930 | QualType T = QualType(New, Fast); |
| 931 | return T; |
| 932 | } |
| 933 | |
| 934 | QualType ASTContext::getVolatileType(QualType T) { |
| 935 | QualType CanT = getCanonicalType(T); |
| 936 | if (CanT.isVolatileQualified()) return T; |
| 937 | |
| 938 | QualifierCollector Quals; |
| 939 | const Type *TypeNode = Quals.strip(T); |
| 940 | Quals.addVolatile(); |
| 941 | |
| 942 | return getExtQualType(TypeNode, Quals); |
| 943 | } |
| 944 | |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 945 | QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 946 | QualType CanT = getCanonicalType(T); |
| 947 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 948 | return T; |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 949 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 950 | // If we are composing extended qualifiers together, merge together |
| 951 | // into one ExtQuals node. |
| 952 | QualifierCollector Quals; |
| 953 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 954 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 955 | // If this type already has an address space specified, it cannot get |
| 956 | // another one. |
| 957 | assert(!Quals.hasAddressSpace() && |
| 958 | "Type cannot be in multiple addr spaces!"); |
| 959 | Quals.addAddressSpace(AddressSpace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 960 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 961 | return getExtQualType(TypeNode, Quals); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 962 | } |
| 963 | |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 964 | QualType ASTContext::getObjCGCQualType(QualType T, |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 965 | Qualifiers::GC GCAttr) { |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 966 | QualType CanT = getCanonicalType(T); |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 967 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 968 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 969 | |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 970 | if (T->isPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 971 | QualType Pointee = T->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 972 | if (Pointee->isAnyPointerType()) { |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 973 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 974 | return getPointerType(ResultType); |
| 975 | } |
| 976 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 977 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 978 | // If we are composing extended qualifiers together, merge together |
| 979 | // into one ExtQuals node. |
| 980 | QualifierCollector Quals; |
| 981 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 982 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 983 | // If this type already has an ObjCGC specified, it cannot get |
| 984 | // another one. |
| 985 | assert(!Quals.hasObjCGCAttr() && |
| 986 | "Type cannot have multiple ObjCGCs!"); |
| 987 | Quals.addObjCGCAttr(GCAttr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 988 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 989 | return getExtQualType(TypeNode, Quals); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 990 | } |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 991 | |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 992 | static QualType getExtFunctionType(ASTContext& Context, QualType T, |
| 993 | const FunctionType::ExtInfo &Info) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 994 | QualType ResultType; |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 995 | if (const PointerType *Pointer = T->getAs<PointerType>()) { |
| 996 | QualType Pointee = Pointer->getPointeeType(); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 997 | ResultType = getExtFunctionType(Context, Pointee, Info); |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 998 | if (ResultType == Pointee) |
| 999 | return T; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1000 | |
| 1001 | ResultType = Context.getPointerType(ResultType); |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1002 | } else if (const BlockPointerType *BlockPointer |
| 1003 | = T->getAs<BlockPointerType>()) { |
| 1004 | QualType Pointee = BlockPointer->getPointeeType(); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1005 | ResultType = getExtFunctionType(Context, Pointee, Info); |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1006 | if (ResultType == Pointee) |
| 1007 | return T; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1008 | |
| 1009 | ResultType = Context.getBlockPointerType(ResultType); |
| 1010 | } else if (const FunctionType *F = T->getAs<FunctionType>()) { |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1011 | if (F->getExtInfo() == Info) |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1012 | return T; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1013 | |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1014 | if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(F)) { |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1015 | ResultType = Context.getFunctionNoProtoType(FNPT->getResultType(), |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1016 | Info); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1017 | } else { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1018 | const FunctionProtoType *FPT = cast<FunctionProtoType>(F); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1019 | ResultType |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1020 | = Context.getFunctionType(FPT->getResultType(), FPT->arg_type_begin(), |
| 1021 | FPT->getNumArgs(), FPT->isVariadic(), |
| 1022 | FPT->getTypeQuals(), |
| 1023 | FPT->hasExceptionSpec(), |
| 1024 | FPT->hasAnyExceptionSpec(), |
| 1025 | FPT->getNumExceptions(), |
| 1026 | FPT->exception_begin(), |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1027 | Info); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1028 | } |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1029 | } else |
| 1030 | return T; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1031 | |
| 1032 | return Context.getQualifiedType(ResultType, T.getLocalQualifiers()); |
| 1033 | } |
| 1034 | |
| 1035 | QualType ASTContext::getNoReturnType(QualType T, bool AddNoReturn) { |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 1036 | FunctionType::ExtInfo Info = getFunctionExtInfo(T); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1037 | return getExtFunctionType(*this, T, |
| 1038 | Info.withNoReturn(AddNoReturn)); |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | QualType ASTContext::getCallConvType(QualType T, CallingConv CallConv) { |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 1042 | FunctionType::ExtInfo Info = getFunctionExtInfo(T); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1043 | return getExtFunctionType(*this, T, |
| 1044 | Info.withCallingConv(CallConv)); |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 1047 | QualType ASTContext::getRegParmType(QualType T, unsigned RegParm) { |
| 1048 | FunctionType::ExtInfo Info = getFunctionExtInfo(T); |
| 1049 | return getExtFunctionType(*this, T, |
| 1050 | Info.withRegParm(RegParm)); |
| 1051 | } |
| 1052 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1053 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 1054 | /// number with the specified element type. |
| 1055 | QualType ASTContext::getComplexType(QualType T) { |
| 1056 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1057 | // structure. |
| 1058 | llvm::FoldingSetNodeID ID; |
| 1059 | ComplexType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1060 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1061 | void *InsertPos = 0; |
| 1062 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1063 | return QualType(CT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1064 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1065 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1066 | // so fill in the canonical type field. |
| 1067 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1068 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1069 | Canonical = getComplexType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1070 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1071 | // Get the new insert position for the node we care about. |
| 1072 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1073 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1074 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1075 | ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1076 | Types.push_back(New); |
| 1077 | ComplexTypes.InsertNode(New, InsertPos); |
| 1078 | return QualType(New, 0); |
| 1079 | } |
| 1080 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1081 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 1082 | /// the specified type. |
| 1083 | QualType ASTContext::getPointerType(QualType T) { |
| 1084 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1085 | // structure. |
| 1086 | llvm::FoldingSetNodeID ID; |
| 1087 | PointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1088 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1089 | void *InsertPos = 0; |
| 1090 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1091 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1092 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1093 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1094 | // so fill in the canonical type field. |
| 1095 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1096 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1097 | Canonical = getPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1098 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1099 | // Get the new insert position for the node we care about. |
| 1100 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1101 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1102 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1103 | PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1104 | Types.push_back(New); |
| 1105 | PointerTypes.InsertNode(New, InsertPos); |
| 1106 | return QualType(New, 0); |
| 1107 | } |
| 1108 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1109 | /// getBlockPointerType - Return the uniqued reference to the type for |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1110 | /// a pointer to the specified block. |
| 1111 | QualType ASTContext::getBlockPointerType(QualType T) { |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1112 | assert(T->isFunctionType() && "block of function types only"); |
| 1113 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1114 | // structure. |
| 1115 | llvm::FoldingSetNodeID ID; |
| 1116 | BlockPointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1117 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1118 | void *InsertPos = 0; |
| 1119 | if (BlockPointerType *PT = |
| 1120 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1121 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1122 | |
| 1123 | // 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] | 1124 | // type either so fill in the canonical type field. |
| 1125 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1126 | if (!T.isCanonical()) { |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1127 | Canonical = getBlockPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1128 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1129 | // Get the new insert position for the node we care about. |
| 1130 | BlockPointerType *NewIP = |
| 1131 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1132 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1133 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1134 | BlockPointerType *New |
| 1135 | = new (*this, TypeAlignment) BlockPointerType(T, Canonical); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1136 | Types.push_back(New); |
| 1137 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 1138 | return QualType(New, 0); |
| 1139 | } |
| 1140 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1141 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 1142 | /// lvalue reference to the specified type. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1143 | QualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1144 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1145 | // structure. |
| 1146 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1147 | ReferenceType::Profile(ID, T, SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1148 | |
| 1149 | void *InsertPos = 0; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1150 | if (LValueReferenceType *RT = |
| 1151 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1152 | return QualType(RT, 0); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1153 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1154 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1155 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1156 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1157 | // either, so fill in the canonical type field. |
| 1158 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1159 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
| 1160 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1161 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1162 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1163 | // Get the new insert position for the node we care about. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1164 | LValueReferenceType *NewIP = |
| 1165 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1166 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1169 | LValueReferenceType *New |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1170 | = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, |
| 1171 | SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1172 | Types.push_back(New); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1173 | LValueReferenceTypes.InsertNode(New, InsertPos); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1174 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1175 | return QualType(New, 0); |
| 1176 | } |
| 1177 | |
| 1178 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 1179 | /// rvalue reference to the specified type. |
| 1180 | QualType ASTContext::getRValueReferenceType(QualType T) { |
| 1181 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1182 | // structure. |
| 1183 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1184 | ReferenceType::Profile(ID, T, false); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1185 | |
| 1186 | void *InsertPos = 0; |
| 1187 | if (RValueReferenceType *RT = |
| 1188 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1189 | return QualType(RT, 0); |
| 1190 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1191 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1192 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1193 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1194 | // either, so fill in the canonical type field. |
| 1195 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1196 | if (InnerRef || !T.isCanonical()) { |
| 1197 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1198 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1199 | |
| 1200 | // Get the new insert position for the node we care about. |
| 1201 | RValueReferenceType *NewIP = |
| 1202 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1203 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
| 1204 | } |
| 1205 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1206 | RValueReferenceType *New |
| 1207 | = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1208 | Types.push_back(New); |
| 1209 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1210 | return QualType(New, 0); |
| 1211 | } |
| 1212 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1213 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 1214 | /// member pointer to the specified type, in the specified class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1215 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1216 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1217 | // structure. |
| 1218 | llvm::FoldingSetNodeID ID; |
| 1219 | MemberPointerType::Profile(ID, T, Cls); |
| 1220 | |
| 1221 | void *InsertPos = 0; |
| 1222 | if (MemberPointerType *PT = |
| 1223 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1224 | return QualType(PT, 0); |
| 1225 | |
| 1226 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 1227 | // type either, so fill in the canonical type field. |
| 1228 | QualType Canonical; |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1229 | if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1230 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 1231 | |
| 1232 | // Get the new insert position for the node we care about. |
| 1233 | MemberPointerType *NewIP = |
| 1234 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1235 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
| 1236 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1237 | MemberPointerType *New |
| 1238 | = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1239 | Types.push_back(New); |
| 1240 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 1241 | return QualType(New, 0); |
| 1242 | } |
| 1243 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1244 | /// getConstantArrayType - Return the unique reference to the type for an |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1245 | /// array of the specified element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1246 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1247 | const llvm::APInt &ArySizeIn, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1248 | ArrayType::ArraySizeModifier ASM, |
| 1249 | unsigned EltTypeQuals) { |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1250 | assert((EltTy->isDependentType() || |
| 1251 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && |
Eli Friedman | 587cbdf | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1252 | "Constant array of VLAs is illegal!"); |
| 1253 | |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1254 | // Convert the array size into a canonical width matching the pointer size for |
| 1255 | // the target. |
| 1256 | llvm::APInt ArySize(ArySizeIn); |
| 1257 | ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1258 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1259 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 0be2ef2 | 2009-02-19 17:31:02 +0000 | [diff] [blame] | 1260 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1261 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1262 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1263 | if (ConstantArrayType *ATP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1264 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1265 | return QualType(ATP, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1266 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1267 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1268 | // so fill in the canonical type field. |
| 1269 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1270 | if (!EltTy.isCanonical()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1271 | Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1272 | ASM, EltTypeQuals); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1273 | // Get the new insert position for the node we care about. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1274 | ConstantArrayType *NewIP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1275 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1276 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1277 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1278 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1279 | ConstantArrayType *New = new(*this,TypeAlignment) |
| 1280 | ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1281 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1282 | Types.push_back(New); |
| 1283 | return QualType(New, 0); |
| 1284 | } |
| 1285 | |
Steve Naroff | bdbf7b0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 1286 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 1287 | /// variable array of the specified element type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1288 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 1289 | Expr *NumElts, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1290 | ArrayType::ArraySizeModifier ASM, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1291 | unsigned EltTypeQuals, |
| 1292 | SourceRange Brackets) { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1293 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 1294 | // that have an expression provided for their size. |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 1295 | QualType CanonType; |
| 1296 | |
| 1297 | if (!EltTy.isCanonical()) { |
| 1298 | if (NumElts) |
| 1299 | NumElts->Retain(); |
| 1300 | CanonType = getVariableArrayType(getCanonicalType(EltTy), NumElts, ASM, |
| 1301 | EltTypeQuals, Brackets); |
| 1302 | } |
| 1303 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1304 | VariableArrayType *New = new(*this, TypeAlignment) |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 1305 | VariableArrayType(EltTy, CanonType, NumElts, ASM, EltTypeQuals, Brackets); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1306 | |
| 1307 | VariableArrayTypes.push_back(New); |
| 1308 | Types.push_back(New); |
| 1309 | return QualType(New, 0); |
| 1310 | } |
| 1311 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1312 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 1313 | /// the type for a dependently-sized array of the specified element |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1314 | /// type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1315 | QualType ASTContext::getDependentSizedArrayType(QualType EltTy, |
| 1316 | Expr *NumElts, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1317 | ArrayType::ArraySizeModifier ASM, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1318 | unsigned EltTypeQuals, |
| 1319 | SourceRange Brackets) { |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1320 | assert((!NumElts || NumElts->isTypeDependent() || |
| 1321 | NumElts->isValueDependent()) && |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1322 | "Size must be type- or value-dependent!"); |
| 1323 | |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1324 | void *InsertPos = 0; |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1325 | DependentSizedArrayType *Canon = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1326 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1327 | |
| 1328 | if (NumElts) { |
| 1329 | // Dependently-sized array types that do not have a specified |
| 1330 | // number of elements will have their sizes deduced from an |
| 1331 | // initializer. |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1332 | DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM, |
| 1333 | EltTypeQuals, NumElts); |
| 1334 | |
| 1335 | Canon = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1336 | } |
| 1337 | |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1338 | DependentSizedArrayType *New; |
| 1339 | if (Canon) { |
| 1340 | // We already have a canonical version of this array type; use it as |
| 1341 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1342 | New = new (*this, TypeAlignment) |
| 1343 | DependentSizedArrayType(*this, EltTy, QualType(Canon, 0), |
| 1344 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1345 | } else { |
| 1346 | QualType CanonEltTy = getCanonicalType(EltTy); |
| 1347 | if (CanonEltTy == EltTy) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1348 | New = new (*this, TypeAlignment) |
| 1349 | DependentSizedArrayType(*this, EltTy, QualType(), |
| 1350 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1351 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1352 | if (NumElts) { |
| 1353 | DependentSizedArrayType *CanonCheck |
| 1354 | = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1355 | assert(!CanonCheck && "Dependent-sized canonical array type broken"); |
| 1356 | (void)CanonCheck; |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1357 | DependentSizedArrayTypes.InsertNode(New, InsertPos); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1358 | } |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1359 | } else { |
| 1360 | QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts, |
| 1361 | ASM, EltTypeQuals, |
| 1362 | SourceRange()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1363 | New = new (*this, TypeAlignment) |
| 1364 | DependentSizedArrayType(*this, EltTy, Canon, |
| 1365 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1366 | } |
| 1367 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1368 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1369 | Types.push_back(New); |
| 1370 | return QualType(New, 0); |
| 1371 | } |
| 1372 | |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1373 | QualType ASTContext::getIncompleteArrayType(QualType EltTy, |
| 1374 | ArrayType::ArraySizeModifier ASM, |
| 1375 | unsigned EltTypeQuals) { |
| 1376 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 0be2ef2 | 2009-02-19 17:31:02 +0000 | [diff] [blame] | 1377 | IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1378 | |
| 1379 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1380 | if (IncompleteArrayType *ATP = |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1381 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1382 | return QualType(ATP, 0); |
| 1383 | |
| 1384 | // If the element type isn't canonical, this won't be a canonical type |
| 1385 | // either, so fill in the canonical type field. |
| 1386 | QualType Canonical; |
| 1387 | |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1388 | if (!EltTy.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1389 | Canonical = getIncompleteArrayType(getCanonicalType(EltTy), |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1390 | ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1391 | |
| 1392 | // Get the new insert position for the node we care about. |
| 1393 | IncompleteArrayType *NewIP = |
| 1394 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1395 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1396 | } |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1397 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1398 | IncompleteArrayType *New = new (*this, TypeAlignment) |
| 1399 | IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1400 | |
| 1401 | IncompleteArrayTypes.InsertNode(New, InsertPos); |
| 1402 | Types.push_back(New); |
| 1403 | return QualType(New, 0); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1406 | /// getVectorType - Return the unique reference to a vector type of |
| 1407 | /// 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] | 1408 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts, |
| 1409 | bool IsAltiVec, bool IsPixel) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1410 | BuiltinType *baseType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1411 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1412 | baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1413 | assert(baseType != 0 && "getVectorType(): Expecting a built-in type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1414 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1415 | // Check if we've already instantiated a vector of this type. |
| 1416 | llvm::FoldingSetNodeID ID; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1417 | VectorType::Profile(ID, vecType, NumElts, Type::Vector, |
| 1418 | IsAltiVec, IsPixel); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1419 | void *InsertPos = 0; |
| 1420 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1421 | return QualType(VTP, 0); |
| 1422 | |
| 1423 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1424 | // so fill in the canonical type field. |
| 1425 | QualType Canonical; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1426 | if (!vecType.isCanonical() || IsAltiVec || IsPixel) { |
| 1427 | Canonical = getVectorType(getCanonicalType(vecType), |
| 1428 | NumElts, false, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1429 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1430 | // Get the new insert position for the node we care about. |
| 1431 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1432 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1433 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1434 | VectorType *New = new (*this, TypeAlignment) |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1435 | VectorType(vecType, NumElts, Canonical, IsAltiVec, IsPixel); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1436 | VectorTypes.InsertNode(New, InsertPos); |
| 1437 | Types.push_back(New); |
| 1438 | return QualType(New, 0); |
| 1439 | } |
| 1440 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1441 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1442 | /// 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] | 1443 | QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) { |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1444 | BuiltinType *baseType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1445 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1446 | baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1447 | assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1448 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1449 | // Check if we've already instantiated a vector of this type. |
| 1450 | llvm::FoldingSetNodeID ID; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1451 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, false, false); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1452 | void *InsertPos = 0; |
| 1453 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1454 | return QualType(VTP, 0); |
| 1455 | |
| 1456 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1457 | // so fill in the canonical type field. |
| 1458 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1459 | if (!vecType.isCanonical()) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1460 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1461 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1462 | // Get the new insert position for the node we care about. |
| 1463 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1464 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1465 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1466 | ExtVectorType *New = new (*this, TypeAlignment) |
| 1467 | ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1468 | VectorTypes.InsertNode(New, InsertPos); |
| 1469 | Types.push_back(New); |
| 1470 | return QualType(New, 0); |
| 1471 | } |
| 1472 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1473 | QualType ASTContext::getDependentSizedExtVectorType(QualType vecType, |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1474 | Expr *SizeExpr, |
| 1475 | SourceLocation AttrLoc) { |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1476 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1477 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1478 | SizeExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1479 | |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1480 | void *InsertPos = 0; |
| 1481 | DependentSizedExtVectorType *Canon |
| 1482 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1483 | DependentSizedExtVectorType *New; |
| 1484 | if (Canon) { |
| 1485 | // We already have a canonical version of this array type; use it as |
| 1486 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1487 | New = new (*this, TypeAlignment) |
| 1488 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), |
| 1489 | SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1490 | } else { |
| 1491 | QualType CanonVecTy = getCanonicalType(vecType); |
| 1492 | if (CanonVecTy == vecType) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1493 | New = new (*this, TypeAlignment) |
| 1494 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, |
| 1495 | AttrLoc); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1496 | |
| 1497 | DependentSizedExtVectorType *CanonCheck |
| 1498 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1499 | assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken"); |
| 1500 | (void)CanonCheck; |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1501 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); |
| 1502 | } else { |
| 1503 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 1504 | SourceLocation()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1505 | New = new (*this, TypeAlignment) |
| 1506 | DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1507 | } |
| 1508 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1509 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1510 | Types.push_back(New); |
| 1511 | return QualType(New, 0); |
| 1512 | } |
| 1513 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1514 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1515 | /// |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1516 | QualType ASTContext::getFunctionNoProtoType(QualType ResultTy, |
| 1517 | const FunctionType::ExtInfo &Info) { |
| 1518 | const CallingConv CallConv = Info.getCC(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1519 | // Unique functions, to guarantee there is only one function of a particular |
| 1520 | // structure. |
| 1521 | llvm::FoldingSetNodeID ID; |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1522 | FunctionNoProtoType::Profile(ID, ResultTy, Info); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1523 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1524 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1525 | if (FunctionNoProtoType *FT = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1526 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1527 | return QualType(FT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1528 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1529 | QualType Canonical; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1530 | if (!ResultTy.isCanonical() || |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1531 | getCanonicalCallConv(CallConv) != CallConv) { |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1532 | Canonical = |
| 1533 | getFunctionNoProtoType(getCanonicalType(ResultTy), |
| 1534 | Info.withCallingConv(getCanonicalCallConv(CallConv))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1535 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1536 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1537 | FunctionNoProtoType *NewIP = |
| 1538 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1539 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1540 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1541 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1542 | FunctionNoProtoType *New = new (*this, TypeAlignment) |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1543 | FunctionNoProtoType(ResultTy, Canonical, Info); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1544 | Types.push_back(New); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1545 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1546 | return QualType(New, 0); |
| 1547 | } |
| 1548 | |
| 1549 | /// getFunctionType - Return a normal function type with a typed argument |
| 1550 | /// list. isVariadic indicates whether the argument list includes '...'. |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 1551 | QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray, |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1552 | unsigned NumArgs, bool isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1553 | unsigned TypeQuals, bool hasExceptionSpec, |
| 1554 | bool hasAnyExceptionSpec, unsigned NumExs, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1555 | const QualType *ExArray, |
| 1556 | const FunctionType::ExtInfo &Info) { |
| 1557 | const CallingConv CallConv= Info.getCC(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1558 | // Unique functions, to guarantee there is only one function of a particular |
| 1559 | // structure. |
| 1560 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1561 | FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1562 | TypeQuals, hasExceptionSpec, hasAnyExceptionSpec, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1563 | NumExs, ExArray, Info); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1564 | |
| 1565 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1566 | if (FunctionProtoType *FTP = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1567 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1568 | return QualType(FTP, 0); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1569 | |
| 1570 | // Determine whether the type being created is already canonical or not. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1571 | bool isCanonical = !hasExceptionSpec && ResultTy.isCanonical(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1572 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1573 | if (!ArgArray[i].isCanonicalAsParam()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1574 | isCanonical = false; |
| 1575 | |
| 1576 | // If this type isn't canonical, get the canonical version of it. |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1577 | // The exception spec is not part of the canonical type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1578 | QualType Canonical; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1579 | if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1580 | llvm::SmallVector<QualType, 16> CanonicalArgs; |
| 1581 | CanonicalArgs.reserve(NumArgs); |
| 1582 | for (unsigned i = 0; i != NumArgs; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1583 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1584 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1585 | Canonical = getFunctionType(getCanonicalType(ResultTy), |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1586 | CanonicalArgs.data(), NumArgs, |
Douglas Gregor | 47259d9 | 2009-08-05 19:03:35 +0000 | [diff] [blame] | 1587 | isVariadic, TypeQuals, false, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1588 | false, 0, 0, |
| 1589 | Info.withCallingConv(getCanonicalCallConv(CallConv))); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1590 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1591 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1592 | FunctionProtoType *NewIP = |
| 1593 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1594 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1595 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1596 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1597 | // FunctionProtoType objects are allocated with extra bytes after them |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1598 | // for two variable size arrays (for parameter and exception types) at the |
| 1599 | // end of them. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1600 | FunctionProtoType *FTP = |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1601 | (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) + |
| 1602 | NumArgs*sizeof(QualType) + |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1603 | NumExs*sizeof(QualType), TypeAlignment); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1604 | new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1605 | TypeQuals, hasExceptionSpec, hasAnyExceptionSpec, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1606 | ExArray, NumExs, Canonical, Info); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1607 | Types.push_back(FTP); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1608 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1609 | return QualType(FTP, 0); |
| 1610 | } |
| 1611 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1612 | #ifndef NDEBUG |
| 1613 | static bool NeedsInjectedClassNameType(const RecordDecl *D) { |
| 1614 | if (!isa<CXXRecordDecl>(D)) return false; |
| 1615 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 1616 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) |
| 1617 | return true; |
| 1618 | if (RD->getDescribedClassTemplate() && |
| 1619 | !isa<ClassTemplateSpecializationDecl>(RD)) |
| 1620 | return true; |
| 1621 | return false; |
| 1622 | } |
| 1623 | #endif |
| 1624 | |
| 1625 | /// getInjectedClassNameType - Return the unique reference to the |
| 1626 | /// injected class name type for the specified templated declaration. |
| 1627 | QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl, |
| 1628 | QualType TST) { |
| 1629 | assert(NeedsInjectedClassNameType(Decl)); |
| 1630 | if (Decl->TypeForDecl) { |
| 1631 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
| 1632 | } else if (CXXRecordDecl *PrevDecl |
| 1633 | = cast_or_null<CXXRecordDecl>(Decl->getPreviousDeclaration())) { |
| 1634 | assert(PrevDecl->TypeForDecl && "previous declaration has no type"); |
| 1635 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 1636 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
| 1637 | } else { |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1638 | Decl->TypeForDecl = |
| 1639 | new (*this, TypeAlignment) InjectedClassNameType(Decl, TST); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1640 | Types.push_back(Decl->TypeForDecl); |
| 1641 | } |
| 1642 | return QualType(Decl->TypeForDecl, 0); |
| 1643 | } |
| 1644 | |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1645 | /// getTypeDeclType - Return the unique reference to the type for the |
| 1646 | /// specified type declaration. |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1647 | QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) { |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 1648 | assert(Decl && "Passed null for Decl param"); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1649 | assert(!Decl->TypeForDecl && "TypeForDecl present in slow case"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1650 | |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1651 | if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1652 | return getTypedefType(Typedef); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1653 | |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1654 | assert(!isa<TemplateTypeParmDecl>(Decl) && |
| 1655 | "Template type parameter types are always available."); |
| 1656 | |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1657 | if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1658 | assert(!Record->getPreviousDeclaration() && |
| 1659 | "struct/union has previous declaration"); |
| 1660 | assert(!NeedsInjectedClassNameType(Record)); |
| 1661 | Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Record); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1662 | } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1663 | assert(!Enum->getPreviousDeclaration() && |
| 1664 | "enum has previous declaration"); |
| 1665 | Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Enum); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1666 | } else if (const UnresolvedUsingTypenameDecl *Using = |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1667 | dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) { |
| 1668 | Decl->TypeForDecl = new (*this, TypeAlignment) UnresolvedUsingType(Using); |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 1669 | } else |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1670 | llvm_unreachable("TypeDecl without a type?"); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1671 | |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1672 | Types.push_back(Decl->TypeForDecl); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1673 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1676 | /// getTypedefType - Return the unique reference to the type for the |
| 1677 | /// specified typename decl. |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1678 | QualType ASTContext::getTypedefType(const TypedefDecl *Decl) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1679 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1680 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1681 | QualType Canonical = getCanonicalType(Decl->getUnderlyingType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1682 | Decl->TypeForDecl = new(*this, TypeAlignment) |
| 1683 | TypedefType(Type::Typedef, Decl, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1684 | Types.push_back(Decl->TypeForDecl); |
| 1685 | return QualType(Decl->TypeForDecl, 0); |
| 1686 | } |
| 1687 | |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1688 | /// \brief Retrieve a substitution-result type. |
| 1689 | QualType |
| 1690 | ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, |
| 1691 | QualType Replacement) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1692 | assert(Replacement.isCanonical() |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1693 | && "replacement types must always be canonical"); |
| 1694 | |
| 1695 | llvm::FoldingSetNodeID ID; |
| 1696 | SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); |
| 1697 | void *InsertPos = 0; |
| 1698 | SubstTemplateTypeParmType *SubstParm |
| 1699 | = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1700 | |
| 1701 | if (!SubstParm) { |
| 1702 | SubstParm = new (*this, TypeAlignment) |
| 1703 | SubstTemplateTypeParmType(Parm, Replacement); |
| 1704 | Types.push_back(SubstParm); |
| 1705 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 1706 | } |
| 1707 | |
| 1708 | return QualType(SubstParm, 0); |
| 1709 | } |
| 1710 | |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1711 | /// \brief Retrieve the template type parameter type for a template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1712 | /// parameter or parameter pack with the given depth, index, and (optionally) |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1713 | /// name. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1714 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1715 | bool ParameterPack, |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1716 | IdentifierInfo *Name) { |
| 1717 | llvm::FoldingSetNodeID ID; |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1718 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1719 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1720 | TemplateTypeParmType *TypeParm |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1721 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1722 | |
| 1723 | if (TypeParm) |
| 1724 | return QualType(TypeParm, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1725 | |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1726 | if (Name) { |
| 1727 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1728 | TypeParm = new (*this, TypeAlignment) |
| 1729 | TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1730 | |
| 1731 | TemplateTypeParmType *TypeCheck |
| 1732 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1733 | assert(!TypeCheck && "Template type parameter canonical type broken"); |
| 1734 | (void)TypeCheck; |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1735 | } else |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1736 | TypeParm = new (*this, TypeAlignment) |
| 1737 | TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1738 | |
| 1739 | Types.push_back(TypeParm); |
| 1740 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 1741 | |
| 1742 | return QualType(TypeParm, 0); |
| 1743 | } |
| 1744 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1745 | TypeSourceInfo * |
| 1746 | ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name, |
| 1747 | SourceLocation NameLoc, |
| 1748 | const TemplateArgumentListInfo &Args, |
| 1749 | QualType CanonType) { |
| 1750 | QualType TST = getTemplateSpecializationType(Name, Args, CanonType); |
| 1751 | |
| 1752 | TypeSourceInfo *DI = CreateTypeSourceInfo(TST); |
| 1753 | TemplateSpecializationTypeLoc TL |
| 1754 | = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc()); |
| 1755 | TL.setTemplateNameLoc(NameLoc); |
| 1756 | TL.setLAngleLoc(Args.getLAngleLoc()); |
| 1757 | TL.setRAngleLoc(Args.getRAngleLoc()); |
| 1758 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 1759 | TL.setArgLocInfo(i, Args[i].getLocInfo()); |
| 1760 | return DI; |
| 1761 | } |
| 1762 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1763 | QualType |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1764 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1765 | const TemplateArgumentListInfo &Args, |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1766 | QualType Canon, |
| 1767 | bool IsCurrentInstantiation) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1768 | unsigned NumArgs = Args.size(); |
| 1769 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1770 | llvm::SmallVector<TemplateArgument, 4> ArgVec; |
| 1771 | ArgVec.reserve(NumArgs); |
| 1772 | for (unsigned i = 0; i != NumArgs; ++i) |
| 1773 | ArgVec.push_back(Args[i].getArgument()); |
| 1774 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1775 | return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, |
| 1776 | Canon, IsCurrentInstantiation); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1777 | } |
| 1778 | |
| 1779 | QualType |
| 1780 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1781 | const TemplateArgument *Args, |
| 1782 | unsigned NumArgs, |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1783 | QualType Canon, |
| 1784 | bool IsCurrentInstantiation) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1785 | if (!Canon.isNull()) |
| 1786 | Canon = getCanonicalType(Canon); |
| 1787 | else { |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1788 | assert(!IsCurrentInstantiation && |
| 1789 | "current-instantiation specializations should always " |
| 1790 | "have a canonical type"); |
| 1791 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1792 | // Build the canonical template specialization type. |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1793 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); |
| 1794 | llvm::SmallVector<TemplateArgument, 4> CanonArgs; |
| 1795 | CanonArgs.reserve(NumArgs); |
| 1796 | for (unsigned I = 0; I != NumArgs; ++I) |
| 1797 | CanonArgs.push_back(getCanonicalTemplateArgument(Args[I])); |
| 1798 | |
| 1799 | // Determine whether this canonical template specialization type already |
| 1800 | // exists. |
| 1801 | llvm::FoldingSetNodeID ID; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1802 | TemplateSpecializationType::Profile(ID, CanonTemplate, false, |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 1803 | CanonArgs.data(), NumArgs, *this); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1804 | |
| 1805 | void *InsertPos = 0; |
| 1806 | TemplateSpecializationType *Spec |
| 1807 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1808 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1809 | if (!Spec) { |
| 1810 | // Allocate a new canonical template specialization type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1811 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1812 | sizeof(TemplateArgument) * NumArgs), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1813 | TypeAlignment); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1814 | Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate, false, |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1815 | CanonArgs.data(), NumArgs, |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1816 | Canon); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1817 | Types.push_back(Spec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1818 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1819 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1820 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1821 | if (Canon.isNull()) |
| 1822 | Canon = QualType(Spec, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1823 | assert(Canon->isDependentType() && |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1824 | "Non-dependent template-id type must have a canonical type"); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1825 | } |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 1826 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1827 | // Allocate the (non-canonical) template specialization type, but don't |
| 1828 | // try to unique it: these types typically have location information that |
| 1829 | // we don't unique and don't want to lose. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1830 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1831 | sizeof(TemplateArgument) * NumArgs), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1832 | TypeAlignment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1833 | TemplateSpecializationType *Spec |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1834 | = new (Mem) TemplateSpecializationType(*this, Template, |
| 1835 | IsCurrentInstantiation, |
| 1836 | Args, NumArgs, |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 1837 | Canon); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1838 | |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1839 | Types.push_back(Spec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1840 | return QualType(Spec, 0); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1843 | QualType |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1844 | ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword, |
| 1845 | NestedNameSpecifier *NNS, |
| 1846 | QualType NamedType) { |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1847 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1848 | ElaboratedType::Profile(ID, Keyword, NNS, NamedType); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1849 | |
| 1850 | void *InsertPos = 0; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1851 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1852 | if (T) |
| 1853 | return QualType(T, 0); |
| 1854 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1855 | QualType Canon = NamedType; |
| 1856 | if (!Canon.isCanonical()) { |
| 1857 | Canon = getCanonicalType(NamedType); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1858 | ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1859 | assert(!CheckT && "Elaborated canonical type broken"); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1860 | (void)CheckT; |
| 1861 | } |
| 1862 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1863 | T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1864 | Types.push_back(T); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1865 | ElaboratedTypes.InsertNode(T, InsertPos); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1866 | return QualType(T, 0); |
| 1867 | } |
| 1868 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 1869 | QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, |
| 1870 | NestedNameSpecifier *NNS, |
| 1871 | const IdentifierInfo *Name, |
| 1872 | QualType Canon) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1873 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 1874 | |
| 1875 | if (Canon.isNull()) { |
| 1876 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 1877 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 1878 | if (Keyword == ETK_None) |
| 1879 | CanonKeyword = ETK_Typename; |
| 1880 | |
| 1881 | if (CanonNNS != NNS || CanonKeyword != Keyword) |
| 1882 | Canon = getDependentNameType(CanonKeyword, CanonNNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1883 | } |
| 1884 | |
| 1885 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 1886 | DependentNameType::Profile(ID, Keyword, NNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1887 | |
| 1888 | void *InsertPos = 0; |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 1889 | DependentNameType *T |
| 1890 | = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1891 | if (T) |
| 1892 | return QualType(T, 0); |
| 1893 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 1894 | T = new (*this) DependentNameType(Keyword, NNS, Name, Canon); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1895 | Types.push_back(T); |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 1896 | DependentNameTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1897 | return QualType(T, 0); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1898 | } |
| 1899 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1900 | QualType |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 1901 | ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, |
| 1902 | NestedNameSpecifier *NNS, |
| 1903 | const TemplateSpecializationType *TemplateId, |
| 1904 | QualType Canon) { |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 1905 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 1906 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1907 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 1908 | DependentNameType::Profile(ID, Keyword, NNS, TemplateId); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1909 | |
| 1910 | void *InsertPos = 0; |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 1911 | DependentNameType *T |
| 1912 | = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1913 | if (T) |
| 1914 | return QualType(T, 0); |
| 1915 | |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 1916 | if (Canon.isNull()) { |
| 1917 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 1918 | QualType CanonType = getCanonicalType(QualType(TemplateId, 0)); |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 1919 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 1920 | if (Keyword == ETK_None) |
| 1921 | CanonKeyword = ETK_Typename; |
| 1922 | if (CanonNNS != NNS || CanonKeyword != Keyword || |
| 1923 | CanonType != QualType(TemplateId, 0)) { |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 1924 | const TemplateSpecializationType *CanonTemplateId |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1925 | = CanonType->getAs<TemplateSpecializationType>(); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 1926 | assert(CanonTemplateId && |
| 1927 | "Canonical type must also be a template specialization type"); |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 1928 | Canon = getDependentNameType(CanonKeyword, CanonNNS, CanonTemplateId); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 1929 | } |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1930 | |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 1931 | DependentNameType *CheckT |
| 1932 | = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1933 | assert(!CheckT && "Typename canonical type is broken"); (void)CheckT; |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 1934 | } |
| 1935 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 1936 | T = new (*this) DependentNameType(Keyword, NNS, TemplateId, Canon); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 1937 | Types.push_back(T); |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 1938 | DependentNameTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1939 | return QualType(T, 0); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 1940 | } |
| 1941 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 1942 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 1943 | /// alphabetically. |
| 1944 | static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, |
| 1945 | const ObjCProtocolDecl *RHS) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1946 | return LHS->getDeclName() < RHS->getDeclName(); |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 1947 | } |
| 1948 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1949 | static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols, |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1950 | unsigned NumProtocols) { |
| 1951 | if (NumProtocols == 0) return true; |
| 1952 | |
| 1953 | for (unsigned i = 1; i != NumProtocols; ++i) |
| 1954 | if (!CmpProtocolNames(Protocols[i-1], Protocols[i])) |
| 1955 | return false; |
| 1956 | return true; |
| 1957 | } |
| 1958 | |
| 1959 | static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols, |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 1960 | unsigned &NumProtocols) { |
| 1961 | ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1962 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 1963 | // Sort protocols, keyed by name. |
| 1964 | std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); |
| 1965 | |
| 1966 | // Remove duplicates. |
| 1967 | ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); |
| 1968 | NumProtocols = ProtocolsEnd-Protocols; |
| 1969 | } |
| 1970 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1971 | QualType ASTContext::getObjCObjectType(QualType BaseType, |
| 1972 | ObjCProtocolDecl * const *Protocols, |
| 1973 | unsigned NumProtocols) { |
| 1974 | // If the base type is an interface and there aren't any protocols |
| 1975 | // to add, then the interface type will do just fine. |
| 1976 | if (!NumProtocols && isa<ObjCInterfaceType>(BaseType)) |
| 1977 | return BaseType; |
| 1978 | |
| 1979 | // Look in the folding set for an existing type. |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1980 | llvm::FoldingSetNodeID ID; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1981 | ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1982 | void *InsertPos = 0; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1983 | if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1984 | return QualType(QT, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1985 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1986 | // Build the canonical type, which has the canonical base type and |
| 1987 | // a sorted-and-uniqued list of protocols. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1988 | QualType Canonical; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1989 | bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols); |
| 1990 | if (!ProtocolsSorted || !BaseType.isCanonical()) { |
| 1991 | if (!ProtocolsSorted) { |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 1992 | llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols, |
| 1993 | Protocols + NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1994 | unsigned UniqueCount = NumProtocols; |
| 1995 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1996 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1997 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 1998 | &Sorted[0], UniqueCount); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1999 | } else { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2000 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 2001 | Protocols, NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2002 | } |
| 2003 | |
| 2004 | // Regenerate InsertPos. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2005 | ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2006 | } |
| 2007 | |
| 2008 | unsigned Size = sizeof(ObjCObjectTypeImpl); |
| 2009 | Size += NumProtocols * sizeof(ObjCProtocolDecl *); |
| 2010 | void *Mem = Allocate(Size, TypeAlignment); |
| 2011 | ObjCObjectTypeImpl *T = |
| 2012 | new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols); |
| 2013 | |
| 2014 | Types.push_back(T); |
| 2015 | ObjCObjectTypes.InsertNode(T, InsertPos); |
| 2016 | return QualType(T, 0); |
| 2017 | } |
| 2018 | |
| 2019 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 2020 | /// the given object type. |
| 2021 | QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) { |
| 2022 | llvm::FoldingSetNodeID ID; |
| 2023 | ObjCObjectPointerType::Profile(ID, ObjectT); |
| 2024 | |
| 2025 | void *InsertPos = 0; |
| 2026 | if (ObjCObjectPointerType *QT = |
| 2027 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2028 | return QualType(QT, 0); |
| 2029 | |
| 2030 | // Find the canonical object type. |
| 2031 | QualType Canonical; |
| 2032 | if (!ObjectT.isCanonical()) { |
| 2033 | Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT)); |
| 2034 | |
| 2035 | // Regenerate InsertPos. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2036 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2037 | } |
| 2038 | |
Douglas Gregor | fd6a088 | 2010-02-08 22:59:26 +0000 | [diff] [blame] | 2039 | // No match. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2040 | void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment); |
| 2041 | ObjCObjectPointerType *QType = |
| 2042 | new (Mem) ObjCObjectPointerType(Canonical, ObjectT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2043 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2044 | Types.push_back(QType); |
| 2045 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2046 | return QualType(QType, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2047 | } |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2048 | |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 2049 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 2050 | /// specified ObjC interface decl. The list of protocols is optional. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2051 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) { |
| 2052 | if (Decl->TypeForDecl) |
| 2053 | return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2054 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2055 | // FIXME: redeclarations? |
| 2056 | void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment); |
| 2057 | ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl); |
| 2058 | Decl->TypeForDecl = T; |
| 2059 | Types.push_back(T); |
| 2060 | return QualType(T, 0); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2061 | } |
| 2062 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2063 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 2064 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2065 | /// multiple declarations that refer to "typeof(x)" all contain different |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2066 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2067 | /// on canonical type's (which are always unique). |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2068 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2069 | TypeOfExprType *toe; |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2070 | if (tofExpr->isTypeDependent()) { |
| 2071 | llvm::FoldingSetNodeID ID; |
| 2072 | DependentTypeOfExprType::Profile(ID, *this, tofExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2073 | |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2074 | void *InsertPos = 0; |
| 2075 | DependentTypeOfExprType *Canon |
| 2076 | = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2077 | if (Canon) { |
| 2078 | // We already have a "canonical" version of an identical, dependent |
| 2079 | // typeof(expr) type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2080 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2081 | QualType((TypeOfExprType*)Canon, 0)); |
| 2082 | } |
| 2083 | else { |
| 2084 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2085 | Canon |
| 2086 | = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2087 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); |
| 2088 | toe = Canon; |
| 2089 | } |
| 2090 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2091 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2092 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2093 | } |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2094 | Types.push_back(toe); |
| 2095 | return QualType(toe, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2096 | } |
| 2097 | |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2098 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 2099 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 2100 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2101 | /// an issue. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2102 | /// on canonical type's (which are always unique). |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2103 | QualType ASTContext::getTypeOfType(QualType tofType) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2104 | QualType Canonical = getCanonicalType(tofType); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2105 | TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2106 | Types.push_back(tot); |
| 2107 | return QualType(tot, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2108 | } |
| 2109 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2110 | /// getDecltypeForExpr - Given an expr, will return the decltype for that |
| 2111 | /// expression, according to the rules in C++0x [dcl.type.simple]p4 |
| 2112 | static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) { |
Anders Carlsson | a07c33e | 2009-06-25 15:00:34 +0000 | [diff] [blame] | 2113 | if (e->isTypeDependent()) |
| 2114 | return Context.DependentTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2115 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2116 | // If e is an id expression or a class member access, decltype(e) is defined |
| 2117 | // as the type of the entity named by e. |
| 2118 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) { |
| 2119 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) |
| 2120 | return VD->getType(); |
| 2121 | } |
| 2122 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) { |
| 2123 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) |
| 2124 | return FD->getType(); |
| 2125 | } |
| 2126 | // If e is a function call or an invocation of an overloaded operator, |
| 2127 | // (parentheses around e are ignored), decltype(e) is defined as the |
| 2128 | // return type of that function. |
| 2129 | if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens())) |
| 2130 | return CE->getCallReturnType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2131 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2132 | QualType T = e->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2133 | |
| 2134 | // 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] | 2135 | // defined as T&, otherwise decltype(e) is defined as T. |
| 2136 | if (e->isLvalue(Context) == Expr::LV_Valid) |
| 2137 | T = Context.getLValueReferenceType(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2138 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2139 | return T; |
| 2140 | } |
| 2141 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2142 | /// getDecltypeType - Unlike many "get<Type>" functions, we don't unique |
| 2143 | /// DecltypeType AST's. The only motivation to unique these nodes would be |
| 2144 | /// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2145 | /// an issue. This doesn't effect the type checker, since it operates |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2146 | /// on canonical type's (which are always unique). |
| 2147 | QualType ASTContext::getDecltypeType(Expr *e) { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2148 | DecltypeType *dt; |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2149 | if (e->isTypeDependent()) { |
| 2150 | llvm::FoldingSetNodeID ID; |
| 2151 | DependentDecltypeType::Profile(ID, *this, e); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2152 | |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2153 | void *InsertPos = 0; |
| 2154 | DependentDecltypeType *Canon |
| 2155 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2156 | if (Canon) { |
| 2157 | // We already have a "canonical" version of an equivalent, dependent |
| 2158 | // decltype type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2159 | dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy, |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2160 | QualType((DecltypeType*)Canon, 0)); |
| 2161 | } |
| 2162 | else { |
| 2163 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2164 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2165 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); |
| 2166 | dt = Canon; |
| 2167 | } |
| 2168 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2169 | QualType T = getDecltypeForExpr(e, *this); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2170 | dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T)); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2171 | } |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2172 | Types.push_back(dt); |
| 2173 | return QualType(dt, 0); |
| 2174 | } |
| 2175 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2176 | /// getTagDeclType - Return the unique reference to the type for the |
| 2177 | /// specified TagDecl (struct/union/class/enum) decl. |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 2178 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) { |
Ted Kremenek | d778f88 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 2179 | assert (Decl); |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 2180 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 2181 | // away const? mutable? |
| 2182 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2183 | } |
| 2184 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2185 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 2186 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 2187 | /// needs to agree with the definition in <stddef.h>. |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 2188 | CanQualType ASTContext::getSizeType() const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2189 | return getFromTargetType(Target.getSizeType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2190 | } |
| 2191 | |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 2192 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 2193 | /// Used when in C++, as a GCC extension. |
| 2194 | QualType ASTContext::getSignedWCharType() const { |
| 2195 | // FIXME: derive from "Target" ? |
| 2196 | return WCharTy; |
| 2197 | } |
| 2198 | |
| 2199 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 2200 | /// Used when in C++, as a GCC extension. |
| 2201 | QualType ASTContext::getUnsignedWCharType() const { |
| 2202 | // FIXME: derive from "Target" ? |
| 2203 | return UnsignedIntTy; |
| 2204 | } |
| 2205 | |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 2206 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) |
| 2207 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 2208 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2209 | return getFromTargetType(Target.getPtrDiffType(0)); |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 2210 | } |
| 2211 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2212 | //===----------------------------------------------------------------------===// |
| 2213 | // Type Operators |
| 2214 | //===----------------------------------------------------------------------===// |
| 2215 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2216 | CanQualType ASTContext::getCanonicalParamType(QualType T) { |
| 2217 | // Push qualifiers into arrays, and then discard any remaining |
| 2218 | // qualifiers. |
| 2219 | T = getCanonicalType(T); |
| 2220 | const Type *Ty = T.getTypePtr(); |
| 2221 | |
| 2222 | QualType Result; |
| 2223 | if (isa<ArrayType>(Ty)) { |
| 2224 | Result = getArrayDecayedType(QualType(Ty,0)); |
| 2225 | } else if (isa<FunctionType>(Ty)) { |
| 2226 | Result = getPointerType(QualType(Ty, 0)); |
| 2227 | } else { |
| 2228 | Result = QualType(Ty, 0); |
| 2229 | } |
| 2230 | |
| 2231 | return CanQualType::CreateUnsafe(Result); |
| 2232 | } |
| 2233 | |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 2234 | /// getCanonicalType - Return the canonical (structural) type corresponding to |
| 2235 | /// the specified potentially non-canonical type. The non-canonical version |
| 2236 | /// of a type may have many "decorated" versions of types. Decorators can |
| 2237 | /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed |
| 2238 | /// to be free of any of these, allowing two canonical types to be compared |
| 2239 | /// for exact equality with a simple pointer comparison. |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2240 | CanQualType ASTContext::getCanonicalType(QualType T) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2241 | QualifierCollector Quals; |
| 2242 | const Type *Ptr = Quals.strip(T); |
| 2243 | QualType CanType = Ptr->getCanonicalTypeInternal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2244 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2245 | // The canonical internal type will be the canonical type *except* |
| 2246 | // that we push type qualifiers down through array types. |
| 2247 | |
| 2248 | // If there are no new qualifiers to push down, stop here. |
| 2249 | if (!Quals.hasQualifiers()) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2250 | return CanQualType::CreateUnsafe(CanType); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2251 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2252 | // If the type qualifiers are on an array type, get the canonical |
| 2253 | // type of the array with the qualifiers applied to the element |
| 2254 | // type. |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2255 | ArrayType *AT = dyn_cast<ArrayType>(CanType); |
| 2256 | if (!AT) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2257 | return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2258 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2259 | // Get the canonical version of the element with the extra qualifiers on it. |
| 2260 | // This can recursively sink qualifiers through multiple levels of arrays. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2261 | QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2262 | NewEltTy = getCanonicalType(NewEltTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2263 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2264 | if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2265 | return CanQualType::CreateUnsafe( |
| 2266 | getConstantArrayType(NewEltTy, CAT->getSize(), |
| 2267 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2268 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2269 | if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2270 | return CanQualType::CreateUnsafe( |
| 2271 | getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2272 | IAT->getIndexTypeCVRQualifiers())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2273 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2274 | if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2275 | return CanQualType::CreateUnsafe( |
| 2276 | getDependentSizedArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2277 | DSAT->getSizeExpr() ? |
| 2278 | DSAT->getSizeExpr()->Retain() : 0, |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2279 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2280 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 87a924e | 2009-10-30 22:56:57 +0000 | [diff] [blame] | 2281 | DSAT->getBracketsRange())->getCanonicalTypeInternal()); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2282 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2283 | VariableArrayType *VAT = cast<VariableArrayType>(AT); |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2284 | return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2285 | VAT->getSizeExpr() ? |
| 2286 | VAT->getSizeExpr()->Retain() : 0, |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2287 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2288 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2289 | VAT->getBracketsRange())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2290 | } |
| 2291 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2292 | QualType ASTContext::getUnqualifiedArrayType(QualType T, |
| 2293 | Qualifiers &Quals) { |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2294 | Quals = T.getQualifiers(); |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2295 | const ArrayType *AT = getAsArrayType(T); |
| 2296 | if (!AT) { |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2297 | return T.getUnqualifiedType(); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2298 | } |
| 2299 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2300 | QualType Elt = AT->getElementType(); |
Zhongxing Xu | c1ae0a8 | 2010-01-05 08:15:06 +0000 | [diff] [blame] | 2301 | QualType UnqualElt = getUnqualifiedArrayType(Elt, Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2302 | if (Elt == UnqualElt) |
| 2303 | return T; |
| 2304 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2305 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2306 | return getConstantArrayType(UnqualElt, CAT->getSize(), |
| 2307 | CAT->getSizeModifier(), 0); |
| 2308 | } |
| 2309 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2310 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2311 | return getIncompleteArrayType(UnqualElt, IAT->getSizeModifier(), 0); |
| 2312 | } |
| 2313 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 2314 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) { |
| 2315 | return getVariableArrayType(UnqualElt, |
| 2316 | VAT->getSizeExpr() ? |
| 2317 | VAT->getSizeExpr()->Retain() : 0, |
| 2318 | VAT->getSizeModifier(), |
| 2319 | VAT->getIndexTypeCVRQualifiers(), |
| 2320 | VAT->getBracketsRange()); |
| 2321 | } |
| 2322 | |
| 2323 | const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2324 | return getDependentSizedArrayType(UnqualElt, DSAT->getSizeExpr()->Retain(), |
| 2325 | DSAT->getSizeModifier(), 0, |
| 2326 | SourceRange()); |
| 2327 | } |
| 2328 | |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2329 | DeclarationName ASTContext::getNameForTemplate(TemplateName Name) { |
| 2330 | if (TemplateDecl *TD = Name.getAsTemplateDecl()) |
| 2331 | return TD->getDeclName(); |
| 2332 | |
| 2333 | if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) { |
| 2334 | if (DTN->isIdentifier()) { |
| 2335 | return DeclarationNames.getIdentifier(DTN->getIdentifier()); |
| 2336 | } else { |
| 2337 | return DeclarationNames.getCXXOperatorName(DTN->getOperator()); |
| 2338 | } |
| 2339 | } |
| 2340 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 2341 | OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate(); |
| 2342 | assert(Storage); |
| 2343 | return (*Storage->begin())->getDeclName(); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2344 | } |
| 2345 | |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2346 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) { |
| 2347 | // If this template name refers to a template, the canonical |
| 2348 | // template name merely stores the template itself. |
| 2349 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 2350 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2351 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 2352 | assert(!Name.getAsOverloadedTemplate()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2353 | |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2354 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 2355 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 2356 | return DTN->CanonicalTemplateName; |
| 2357 | } |
| 2358 | |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 2359 | bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) { |
| 2360 | X = getCanonicalTemplateName(X); |
| 2361 | Y = getCanonicalTemplateName(Y); |
| 2362 | return X.getAsVoidPointer() == Y.getAsVoidPointer(); |
| 2363 | } |
| 2364 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2365 | TemplateArgument |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2366 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) { |
| 2367 | switch (Arg.getKind()) { |
| 2368 | case TemplateArgument::Null: |
| 2369 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2370 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2371 | case TemplateArgument::Expression: |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2372 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2373 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2374 | case TemplateArgument::Declaration: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2375 | return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2376 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2377 | case TemplateArgument::Template: |
| 2378 | return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate())); |
| 2379 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2380 | case TemplateArgument::Integral: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2381 | return TemplateArgument(*Arg.getAsIntegral(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2382 | getCanonicalType(Arg.getIntegralType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2383 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2384 | case TemplateArgument::Type: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2385 | return TemplateArgument(getCanonicalType(Arg.getAsType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2386 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2387 | case TemplateArgument::Pack: { |
| 2388 | // FIXME: Allocate in ASTContext |
| 2389 | TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()]; |
| 2390 | unsigned Idx = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2391 | for (TemplateArgument::pack_iterator A = Arg.pack_begin(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2392 | AEnd = Arg.pack_end(); |
| 2393 | A != AEnd; (void)++A, ++Idx) |
| 2394 | CanonArgs[Idx] = getCanonicalTemplateArgument(*A); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2395 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2396 | TemplateArgument Result; |
| 2397 | Result.setArgumentPack(CanonArgs, Arg.pack_size(), false); |
| 2398 | return Result; |
| 2399 | } |
| 2400 | } |
| 2401 | |
| 2402 | // Silence GCC warning |
| 2403 | assert(false && "Unhandled template argument kind"); |
| 2404 | return TemplateArgument(); |
| 2405 | } |
| 2406 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2407 | NestedNameSpecifier * |
| 2408 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2409 | if (!NNS) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2410 | return 0; |
| 2411 | |
| 2412 | switch (NNS->getKind()) { |
| 2413 | case NestedNameSpecifier::Identifier: |
| 2414 | // Canonicalize the prefix but keep the identifier the same. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2415 | return NestedNameSpecifier::Create(*this, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2416 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 2417 | NNS->getAsIdentifier()); |
| 2418 | |
| 2419 | case NestedNameSpecifier::Namespace: |
| 2420 | // A namespace is canonical; build a nested-name-specifier with |
| 2421 | // this namespace and no prefix. |
| 2422 | return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace()); |
| 2423 | |
| 2424 | case NestedNameSpecifier::TypeSpec: |
| 2425 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 2426 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2427 | return NestedNameSpecifier::Create(*this, 0, |
| 2428 | NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2429 | T.getTypePtr()); |
| 2430 | } |
| 2431 | |
| 2432 | case NestedNameSpecifier::Global: |
| 2433 | // The global specifier is canonical and unique. |
| 2434 | return NNS; |
| 2435 | } |
| 2436 | |
| 2437 | // Required to silence a GCC warning |
| 2438 | return 0; |
| 2439 | } |
| 2440 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2441 | |
| 2442 | const ArrayType *ASTContext::getAsArrayType(QualType T) { |
| 2443 | // Handle the non-qualified case efficiently. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2444 | if (!T.hasLocalQualifiers()) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2445 | // Handle the common positive case fast. |
| 2446 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 2447 | return AT; |
| 2448 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2449 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2450 | // Handle the common negative case fast. |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2451 | QualType CType = T->getCanonicalTypeInternal(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2452 | if (!isa<ArrayType>(CType)) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2453 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2454 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2455 | // Apply any qualifiers from the array type to the element type. This |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2456 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 2457 | // 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] | 2458 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2459 | // If we get here, we either have type qualifiers on the type, or we have |
| 2460 | // 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] | 2461 | // we must propagate them down into the element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2462 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2463 | QualifierCollector Qs; |
| 2464 | const Type *Ty = Qs.strip(T.getDesugaredType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2465 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2466 | // If we have a simple case, just return now. |
| 2467 | const ArrayType *ATy = dyn_cast<ArrayType>(Ty); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2468 | if (ATy == 0 || Qs.empty()) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2469 | return ATy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2470 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2471 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 2472 | // qualifiers into the array element type and return a new array type. |
| 2473 | // Get the canonical version of the element with the extra qualifiers on it. |
| 2474 | // This can recursively sink qualifiers through multiple levels of arrays. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2475 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2476 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2477 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 2478 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 2479 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2480 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2481 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 2482 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 2483 | IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2484 | IAT->getIndexTypeCVRQualifiers())); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2485 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2486 | if (const DependentSizedArrayType *DSAT |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2487 | = dyn_cast<DependentSizedArrayType>(ATy)) |
| 2488 | return cast<ArrayType>( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2489 | getDependentSizedArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2490 | DSAT->getSizeExpr() ? |
| 2491 | DSAT->getSizeExpr()->Retain() : 0, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2492 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2493 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2494 | DSAT->getBracketsRange())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2495 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2496 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2497 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2498 | VAT->getSizeExpr() ? |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2499 | VAT->getSizeExpr()->Retain() : 0, |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2500 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2501 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2502 | VAT->getBracketsRange())); |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 2503 | } |
| 2504 | |
| 2505 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2506 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 2507 | /// specified array type to a pointer. This operation is non-trivial when |
| 2508 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 2509 | /// this returns a pointer to a properly qualified element of the array. |
| 2510 | /// |
| 2511 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
| 2512 | QualType ASTContext::getArrayDecayedType(QualType Ty) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2513 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 2514 | // typedefs in the element type of the array. This also handles propagation |
| 2515 | // of type qualifiers from the array type into the element type if present |
| 2516 | // (C99 6.7.3p8). |
| 2517 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 2518 | assert(PrettyArrayType && "Not an array type!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2519 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2520 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2521 | |
| 2522 | // int x[restrict 4] -> int *restrict |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2523 | return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2524 | } |
| 2525 | |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2526 | QualType ASTContext::getBaseElementType(QualType QT) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2527 | QualifierCollector Qs; |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 2528 | while (const ArrayType *AT = getAsArrayType(QualType(Qs.strip(QT), 0))) |
| 2529 | QT = AT->getElementType(); |
| 2530 | return Qs.apply(QT); |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2531 | } |
| 2532 | |
Anders Carlsson | fbbce49 | 2009-09-25 01:23:32 +0000 | [diff] [blame] | 2533 | QualType ASTContext::getBaseElementType(const ArrayType *AT) { |
| 2534 | QualType ElemTy = AT->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2535 | |
Anders Carlsson | fbbce49 | 2009-09-25 01:23:32 +0000 | [diff] [blame] | 2536 | if (const ArrayType *AT = getAsArrayType(ElemTy)) |
| 2537 | return getBaseElementType(AT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2538 | |
Anders Carlsson | 6183a99 | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 2539 | return ElemTy; |
| 2540 | } |
| 2541 | |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 2542 | /// getConstantArrayElementCount - Returns number of constant array elements. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2543 | uint64_t |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 2544 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
| 2545 | uint64_t ElementCount = 1; |
| 2546 | do { |
| 2547 | ElementCount *= CA->getSize().getZExtValue(); |
| 2548 | CA = dyn_cast<ConstantArrayType>(CA->getElementType()); |
| 2549 | } while (CA); |
| 2550 | return ElementCount; |
| 2551 | } |
| 2552 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2553 | /// getFloatingRank - Return a relative rank for floating point types. |
| 2554 | /// 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] | 2555 | static FloatingRank getFloatingRank(QualType T) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2556 | if (const ComplexType *CT = T->getAs<ComplexType>()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2557 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2558 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2559 | assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"); |
| 2560 | switch (T->getAs<BuiltinType>()->getKind()) { |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2561 | default: assert(0 && "getFloatingRank(): not a floating type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2562 | case BuiltinType::Float: return FloatRank; |
| 2563 | case BuiltinType::Double: return DoubleRank; |
| 2564 | case BuiltinType::LongDouble: return LongDoubleRank; |
| 2565 | } |
| 2566 | } |
| 2567 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2568 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 2569 | /// point or a complex type (based on typeDomain/typeSize). |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 2570 | /// 'typeDomain' is a real floating point or complex type. |
| 2571 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 2572 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 2573 | QualType Domain) const { |
| 2574 | FloatingRank EltRank = getFloatingRank(Size); |
| 2575 | if (Domain->isComplexType()) { |
| 2576 | switch (EltRank) { |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 2577 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 2578 | case FloatRank: return FloatComplexTy; |
| 2579 | case DoubleRank: return DoubleComplexTy; |
| 2580 | case LongDoubleRank: return LongDoubleComplexTy; |
| 2581 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2582 | } |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 2583 | |
| 2584 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 2585 | switch (EltRank) { |
| 2586 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
| 2587 | case FloatRank: return FloatTy; |
| 2588 | case DoubleRank: return DoubleTy; |
| 2589 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 2590 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2591 | } |
| 2592 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2593 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 2594 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 2595 | /// '_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] | 2596 | /// LHS < RHS, return -1. |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2597 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) { |
| 2598 | FloatingRank LHSR = getFloatingRank(LHS); |
| 2599 | FloatingRank RHSR = getFloatingRank(RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2600 | |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2601 | if (LHSR == RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 2602 | return 0; |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2603 | if (LHSR > RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 2604 | return 1; |
| 2605 | return -1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2606 | } |
| 2607 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2608 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 2609 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 2610 | /// or if it is not canonicalized. |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2611 | unsigned ASTContext::getIntegerRank(Type *T) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2612 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2613 | if (EnumType* ET = dyn_cast<EnumType>(T)) |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 2614 | T = ET->getDecl()->getPromotionType().getTypePtr(); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2615 | |
Eli Friedman | a342675 | 2009-07-05 23:44:27 +0000 | [diff] [blame] | 2616 | if (T->isSpecificBuiltinType(BuiltinType::WChar)) |
| 2617 | T = getFromTargetType(Target.getWCharType()).getTypePtr(); |
| 2618 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2619 | if (T->isSpecificBuiltinType(BuiltinType::Char16)) |
| 2620 | T = getFromTargetType(Target.getChar16Type()).getTypePtr(); |
| 2621 | |
| 2622 | if (T->isSpecificBuiltinType(BuiltinType::Char32)) |
| 2623 | T = getFromTargetType(Target.getChar32Type()).getTypePtr(); |
| 2624 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2625 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2626 | default: assert(0 && "getIntegerRank(): not a built-in integer"); |
| 2627 | case BuiltinType::Bool: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2628 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2629 | case BuiltinType::Char_S: |
| 2630 | case BuiltinType::Char_U: |
| 2631 | case BuiltinType::SChar: |
| 2632 | case BuiltinType::UChar: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2633 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2634 | case BuiltinType::Short: |
| 2635 | case BuiltinType::UShort: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2636 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2637 | case BuiltinType::Int: |
| 2638 | case BuiltinType::UInt: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2639 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2640 | case BuiltinType::Long: |
| 2641 | case BuiltinType::ULong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2642 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2643 | case BuiltinType::LongLong: |
| 2644 | case BuiltinType::ULongLong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2645 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 2646 | case BuiltinType::Int128: |
| 2647 | case BuiltinType::UInt128: |
| 2648 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2649 | } |
| 2650 | } |
| 2651 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 2652 | /// \brief Whether this is a promotable bitfield reference according |
| 2653 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
| 2654 | /// |
| 2655 | /// \returns the type this bit-field will promote to, or NULL if no |
| 2656 | /// promotion occurs. |
| 2657 | QualType ASTContext::isPromotableBitField(Expr *E) { |
Douglas Gregor | ceafbde | 2010-05-24 20:13:53 +0000 | [diff] [blame] | 2658 | if (E->isTypeDependent() || E->isValueDependent()) |
| 2659 | return QualType(); |
| 2660 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 2661 | FieldDecl *Field = E->getBitField(); |
| 2662 | if (!Field) |
| 2663 | return QualType(); |
| 2664 | |
| 2665 | QualType FT = Field->getType(); |
| 2666 | |
| 2667 | llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this); |
| 2668 | uint64_t BitWidth = BitWidthAP.getZExtValue(); |
| 2669 | uint64_t IntSize = getTypeSize(IntTy); |
| 2670 | // GCC extension compatibility: if the bit-field size is less than or equal |
| 2671 | // to the size of int, it gets promoted no matter what its type is. |
| 2672 | // For instance, unsigned long bf : 4 gets promoted to signed int. |
| 2673 | if (BitWidth < IntSize) |
| 2674 | return IntTy; |
| 2675 | |
| 2676 | if (BitWidth == IntSize) |
| 2677 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
| 2678 | |
| 2679 | // Types bigger than int are not subject to promotions, and therefore act |
| 2680 | // like the base type. |
| 2681 | // FIXME: This doesn't quite match what gcc does, but what gcc does here |
| 2682 | // is ridiculous. |
| 2683 | return QualType(); |
| 2684 | } |
| 2685 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 2686 | /// getPromotedIntegerType - Returns the type that Promotable will |
| 2687 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
| 2688 | /// integer type. |
| 2689 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) { |
| 2690 | assert(!Promotable.isNull()); |
| 2691 | assert(Promotable->isPromotableIntegerType()); |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 2692 | if (const EnumType *ET = Promotable->getAs<EnumType>()) |
| 2693 | return ET->getDecl()->getPromotionType(); |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 2694 | if (Promotable->isSignedIntegerType()) |
| 2695 | return IntTy; |
| 2696 | uint64_t PromotableSize = getTypeSize(Promotable); |
| 2697 | uint64_t IntSize = getTypeSize(IntTy); |
| 2698 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
| 2699 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
| 2700 | } |
| 2701 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2702 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2703 | /// 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] | 2704 | /// LHS < RHS, return -1. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2705 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2706 | Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 2707 | Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2708 | if (LHSC == RHSC) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2709 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2710 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 2711 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2712 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2713 | unsigned LHSRank = getIntegerRank(LHSC); |
| 2714 | unsigned RHSRank = getIntegerRank(RHSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2715 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2716 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 2717 | if (LHSRank == RHSRank) return 0; |
| 2718 | return LHSRank > RHSRank ? 1 : -1; |
| 2719 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2720 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2721 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 2722 | if (LHSUnsigned) { |
| 2723 | // If the unsigned [LHS] type is larger, return it. |
| 2724 | if (LHSRank >= RHSRank) |
| 2725 | return 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2726 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2727 | // If the signed type can represent all values of the unsigned type, it |
| 2728 | // 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] | 2729 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2730 | return -1; |
| 2731 | } |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2732 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2733 | // If the unsigned [RHS] type is larger, return it. |
| 2734 | if (RHSRank >= LHSRank) |
| 2735 | return -1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2736 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2737 | // If the signed type can represent all values of the unsigned type, it |
| 2738 | // 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] | 2739 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2740 | return 1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2741 | } |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2742 | |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 2743 | static RecordDecl * |
| 2744 | CreateRecordDecl(ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC, |
| 2745 | SourceLocation L, IdentifierInfo *Id) { |
| 2746 | if (Ctx.getLangOptions().CPlusPlus) |
| 2747 | return CXXRecordDecl::Create(Ctx, TK, DC, L, Id); |
| 2748 | else |
| 2749 | return RecordDecl::Create(Ctx, TK, DC, L, Id); |
| 2750 | } |
| 2751 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2752 | // getCFConstantStringType - Return the type used for constant CFStrings. |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2753 | QualType ASTContext::getCFConstantStringType() { |
| 2754 | if (!CFConstantStringTypeDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2755 | CFConstantStringTypeDecl = |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2756 | CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 2757 | &Idents.get("NSConstantString")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 2758 | CFConstantStringTypeDecl->startDefinition(); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 2759 | |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 2760 | QualType FieldTypes[4]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2761 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2762 | // const int *isa; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2763 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 2764 | // int flags; |
| 2765 | FieldTypes[1] = IntTy; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2766 | // const char *str; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2767 | FieldTypes[2] = getPointerType(CharTy.withConst()); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2768 | // long length; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2769 | FieldTypes[3] = LongTy; |
| 2770 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2771 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2772 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2773 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2774 | SourceLocation(), 0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2775 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2776 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2777 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 2778 | Field->setAccess(AS_public); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2779 | CFConstantStringTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2780 | } |
| 2781 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2782 | CFConstantStringTypeDecl->completeDefinition(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2783 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2784 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2785 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 8467583 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 2786 | } |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 2787 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 2788 | void ASTContext::setCFConstantStringType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2789 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 2790 | assert(Rec && "Invalid CFConstantStringType"); |
| 2791 | CFConstantStringTypeDecl = Rec->getDecl(); |
| 2792 | } |
| 2793 | |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 2794 | // getNSConstantStringType - Return the type used for constant NSStrings. |
| 2795 | QualType ASTContext::getNSConstantStringType() { |
| 2796 | if (!NSConstantStringTypeDecl) { |
| 2797 | NSConstantStringTypeDecl = |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2798 | CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 2799 | &Idents.get("__builtin_NSString")); |
| 2800 | NSConstantStringTypeDecl->startDefinition(); |
| 2801 | |
| 2802 | QualType FieldTypes[3]; |
| 2803 | |
| 2804 | // const int *isa; |
| 2805 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
| 2806 | // const char *str; |
| 2807 | FieldTypes[1] = getPointerType(CharTy.withConst()); |
| 2808 | // unsigned int length; |
| 2809 | FieldTypes[2] = UnsignedIntTy; |
| 2810 | |
| 2811 | // Create fields |
| 2812 | for (unsigned i = 0; i < 3; ++i) { |
| 2813 | FieldDecl *Field = FieldDecl::Create(*this, NSConstantStringTypeDecl, |
| 2814 | SourceLocation(), 0, |
| 2815 | FieldTypes[i], /*TInfo=*/0, |
| 2816 | /*BitWidth=*/0, |
| 2817 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 2818 | Field->setAccess(AS_public); |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 2819 | NSConstantStringTypeDecl->addDecl(Field); |
| 2820 | } |
| 2821 | |
| 2822 | NSConstantStringTypeDecl->completeDefinition(); |
| 2823 | } |
| 2824 | |
| 2825 | return getTagDeclType(NSConstantStringTypeDecl); |
| 2826 | } |
| 2827 | |
| 2828 | void ASTContext::setNSConstantStringType(QualType T) { |
| 2829 | const RecordType *Rec = T->getAs<RecordType>(); |
| 2830 | assert(Rec && "Invalid NSConstantStringType"); |
| 2831 | NSConstantStringTypeDecl = Rec->getDecl(); |
| 2832 | } |
| 2833 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2834 | QualType ASTContext::getObjCFastEnumerationStateType() { |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2835 | if (!ObjCFastEnumerationStateTypeDecl) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2836 | ObjCFastEnumerationStateTypeDecl = |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2837 | CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 2838 | &Idents.get("__objcFastEnumerationState")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 2839 | ObjCFastEnumerationStateTypeDecl->startDefinition(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2840 | |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2841 | QualType FieldTypes[] = { |
| 2842 | UnsignedLongTy, |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2843 | getPointerType(ObjCIdTypedefType), |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2844 | getPointerType(UnsignedLongTy), |
| 2845 | getConstantArrayType(UnsignedLongTy, |
| 2846 | llvm::APInt(32, 5), ArrayType::Normal, 0) |
| 2847 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2848 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2849 | for (size_t i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2850 | FieldDecl *Field = FieldDecl::Create(*this, |
| 2851 | ObjCFastEnumerationStateTypeDecl, |
| 2852 | SourceLocation(), 0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2853 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2854 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2855 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 2856 | Field->setAccess(AS_public); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2857 | ObjCFastEnumerationStateTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2858 | } |
Fariborz Jahanian | 38c9ab8 | 2010-05-27 16:05:06 +0000 | [diff] [blame^] | 2859 | if (getLangOptions().CPlusPlus) |
| 2860 | if (CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(ObjCFastEnumerationStateTypeDecl)) |
| 2861 | CXXRD->setEmpty(false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2862 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2863 | ObjCFastEnumerationStateTypeDecl->completeDefinition(); |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2864 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2865 | |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2866 | return getTagDeclType(ObjCFastEnumerationStateTypeDecl); |
| 2867 | } |
| 2868 | |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2869 | QualType ASTContext::getBlockDescriptorType() { |
| 2870 | if (BlockDescriptorType) |
| 2871 | return getTagDeclType(BlockDescriptorType); |
| 2872 | |
| 2873 | RecordDecl *T; |
| 2874 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2875 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 2876 | &Idents.get("__block_descriptor")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 2877 | T->startDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2878 | |
| 2879 | QualType FieldTypes[] = { |
| 2880 | UnsignedLongTy, |
| 2881 | UnsignedLongTy, |
| 2882 | }; |
| 2883 | |
| 2884 | const char *FieldNames[] = { |
| 2885 | "reserved", |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 2886 | "Size" |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2887 | }; |
| 2888 | |
| 2889 | for (size_t i = 0; i < 2; ++i) { |
| 2890 | FieldDecl *Field = FieldDecl::Create(*this, |
| 2891 | T, |
| 2892 | SourceLocation(), |
| 2893 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2894 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2895 | /*BitWidth=*/0, |
| 2896 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 2897 | Field->setAccess(AS_public); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2898 | T->addDecl(Field); |
| 2899 | } |
| 2900 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2901 | T->completeDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2902 | |
| 2903 | BlockDescriptorType = T; |
| 2904 | |
| 2905 | return getTagDeclType(BlockDescriptorType); |
| 2906 | } |
| 2907 | |
| 2908 | void ASTContext::setBlockDescriptorType(QualType T) { |
| 2909 | const RecordType *Rec = T->getAs<RecordType>(); |
| 2910 | assert(Rec && "Invalid BlockDescriptorType"); |
| 2911 | BlockDescriptorType = Rec->getDecl(); |
| 2912 | } |
| 2913 | |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 2914 | QualType ASTContext::getBlockDescriptorExtendedType() { |
| 2915 | if (BlockDescriptorExtendedType) |
| 2916 | return getTagDeclType(BlockDescriptorExtendedType); |
| 2917 | |
| 2918 | RecordDecl *T; |
| 2919 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2920 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 2921 | &Idents.get("__block_descriptor_withcopydispose")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 2922 | T->startDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 2923 | |
| 2924 | QualType FieldTypes[] = { |
| 2925 | UnsignedLongTy, |
| 2926 | UnsignedLongTy, |
| 2927 | getPointerType(VoidPtrTy), |
| 2928 | getPointerType(VoidPtrTy) |
| 2929 | }; |
| 2930 | |
| 2931 | const char *FieldNames[] = { |
| 2932 | "reserved", |
| 2933 | "Size", |
| 2934 | "CopyFuncPtr", |
| 2935 | "DestroyFuncPtr" |
| 2936 | }; |
| 2937 | |
| 2938 | for (size_t i = 0; i < 4; ++i) { |
| 2939 | FieldDecl *Field = FieldDecl::Create(*this, |
| 2940 | T, |
| 2941 | SourceLocation(), |
| 2942 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2943 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 2944 | /*BitWidth=*/0, |
| 2945 | /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 2946 | Field->setAccess(AS_public); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 2947 | T->addDecl(Field); |
| 2948 | } |
| 2949 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2950 | T->completeDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 2951 | |
| 2952 | BlockDescriptorExtendedType = T; |
| 2953 | |
| 2954 | return getTagDeclType(BlockDescriptorExtendedType); |
| 2955 | } |
| 2956 | |
| 2957 | void ASTContext::setBlockDescriptorExtendedType(QualType T) { |
| 2958 | const RecordType *Rec = T->getAs<RecordType>(); |
| 2959 | assert(Rec && "Invalid BlockDescriptorType"); |
| 2960 | BlockDescriptorExtendedType = Rec->getDecl(); |
| 2961 | } |
| 2962 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 2963 | bool ASTContext::BlockRequiresCopying(QualType Ty) { |
| 2964 | if (Ty->isBlockPointerType()) |
| 2965 | return true; |
| 2966 | if (isObjCNSObjectType(Ty)) |
| 2967 | return true; |
| 2968 | if (Ty->isObjCObjectPointerType()) |
| 2969 | return true; |
| 2970 | return false; |
| 2971 | } |
| 2972 | |
| 2973 | QualType ASTContext::BuildByRefType(const char *DeclName, QualType Ty) { |
| 2974 | // type = struct __Block_byref_1_X { |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 2975 | // void *__isa; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 2976 | // struct __Block_byref_1_X *__forwarding; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 2977 | // unsigned int __flags; |
| 2978 | // unsigned int __size; |
Mike Stump | 38e1627 | 2009-10-21 22:01:24 +0000 | [diff] [blame] | 2979 | // void *__copy_helper; // as needed |
| 2980 | // void *__destroy_help // as needed |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 2981 | // int X; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 2982 | // } * |
| 2983 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 2984 | bool HasCopyAndDispose = BlockRequiresCopying(Ty); |
| 2985 | |
| 2986 | // FIXME: Move up |
Fariborz Jahanian | 4d0d85c | 2009-10-24 00:16:42 +0000 | [diff] [blame] | 2987 | static unsigned int UniqueBlockByRefTypeID = 0; |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 2988 | llvm::SmallString<36> Name; |
| 2989 | llvm::raw_svector_ostream(Name) << "__Block_byref_" << |
| 2990 | ++UniqueBlockByRefTypeID << '_' << DeclName; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 2991 | RecordDecl *T; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2992 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 2993 | &Idents.get(Name.str())); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 2994 | T->startDefinition(); |
| 2995 | QualType Int32Ty = IntTy; |
| 2996 | assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported"); |
| 2997 | QualType FieldTypes[] = { |
| 2998 | getPointerType(VoidPtrTy), |
| 2999 | getPointerType(getTagDeclType(T)), |
| 3000 | Int32Ty, |
| 3001 | Int32Ty, |
| 3002 | getPointerType(VoidPtrTy), |
| 3003 | getPointerType(VoidPtrTy), |
| 3004 | Ty |
| 3005 | }; |
| 3006 | |
| 3007 | const char *FieldNames[] = { |
| 3008 | "__isa", |
| 3009 | "__forwarding", |
| 3010 | "__flags", |
| 3011 | "__size", |
| 3012 | "__copy_helper", |
| 3013 | "__destroy_helper", |
| 3014 | DeclName, |
| 3015 | }; |
| 3016 | |
| 3017 | for (size_t i = 0; i < 7; ++i) { |
| 3018 | if (!HasCopyAndDispose && i >=4 && i <= 5) |
| 3019 | continue; |
| 3020 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
| 3021 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3022 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3023 | /*BitWidth=*/0, /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3024 | Field->setAccess(AS_public); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3025 | T->addDecl(Field); |
| 3026 | } |
| 3027 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3028 | T->completeDefinition(); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3029 | |
| 3030 | return getPointerType(getTagDeclType(T)); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3031 | } |
| 3032 | |
| 3033 | |
| 3034 | QualType ASTContext::getBlockParmType( |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3035 | bool BlockHasCopyDispose, |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 3036 | llvm::SmallVectorImpl<const Expr *> &Layout) { |
| 3037 | |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3038 | // FIXME: Move up |
Fariborz Jahanian | 4d0d85c | 2009-10-24 00:16:42 +0000 | [diff] [blame] | 3039 | static unsigned int UniqueBlockParmTypeID = 0; |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 3040 | llvm::SmallString<36> Name; |
| 3041 | llvm::raw_svector_ostream(Name) << "__block_literal_" |
| 3042 | << ++UniqueBlockParmTypeID; |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3043 | RecordDecl *T; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3044 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(), |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3045 | &Idents.get(Name.str())); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3046 | T->startDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3047 | QualType FieldTypes[] = { |
| 3048 | getPointerType(VoidPtrTy), |
| 3049 | IntTy, |
| 3050 | IntTy, |
| 3051 | getPointerType(VoidPtrTy), |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3052 | (BlockHasCopyDispose ? |
| 3053 | getPointerType(getBlockDescriptorExtendedType()) : |
| 3054 | getPointerType(getBlockDescriptorType())) |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3055 | }; |
| 3056 | |
| 3057 | const char *FieldNames[] = { |
| 3058 | "__isa", |
| 3059 | "__flags", |
| 3060 | "__reserved", |
| 3061 | "__FuncPtr", |
| 3062 | "__descriptor" |
| 3063 | }; |
| 3064 | |
| 3065 | for (size_t i = 0; i < 5; ++i) { |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3066 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3067 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3068 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3069 | /*BitWidth=*/0, /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3070 | Field->setAccess(AS_public); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3071 | T->addDecl(Field); |
| 3072 | } |
| 3073 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 3074 | for (unsigned i = 0; i < Layout.size(); ++i) { |
| 3075 | const Expr *E = Layout[i]; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3076 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 3077 | QualType FieldType = E->getType(); |
| 3078 | IdentifierInfo *FieldName = 0; |
| 3079 | if (isa<CXXThisExpr>(E)) { |
| 3080 | FieldName = &Idents.get("this"); |
| 3081 | } else if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E)) { |
| 3082 | const ValueDecl *D = BDRE->getDecl(); |
| 3083 | FieldName = D->getIdentifier(); |
| 3084 | if (BDRE->isByRef()) |
| 3085 | FieldType = BuildByRefType(D->getNameAsCString(), FieldType); |
| 3086 | } else { |
| 3087 | // Padding. |
| 3088 | assert(isa<ConstantArrayType>(FieldType) && |
| 3089 | isa<DeclRefExpr>(E) && |
| 3090 | !cast<DeclRefExpr>(E)->getDecl()->getDeclName() && |
| 3091 | "doesn't match characteristics of padding decl"); |
| 3092 | } |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3093 | |
| 3094 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 3095 | FieldName, FieldType, /*TInfo=*/0, |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3096 | /*BitWidth=*/0, /*Mutable=*/false); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3097 | Field->setAccess(AS_public); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3098 | T->addDecl(Field); |
| 3099 | } |
| 3100 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3101 | T->completeDefinition(); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3102 | |
| 3103 | return getPointerType(getTagDeclType(T)); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3104 | } |
| 3105 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3106 | void ASTContext::setObjCFastEnumerationStateType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3107 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3108 | assert(Rec && "Invalid ObjCFAstEnumerationStateType"); |
| 3109 | ObjCFastEnumerationStateTypeDecl = Rec->getDecl(); |
| 3110 | } |
| 3111 | |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3112 | // This returns true if a type has been typedefed to BOOL: |
| 3113 | // typedef <type> BOOL; |
Chris Lattner | 2d99833 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 3114 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3115 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | bb49c3e | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 3116 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 3117 | return II->isStr("BOOL"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3118 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3119 | return false; |
| 3120 | } |
| 3121 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3122 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3123 | /// purpose. |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3124 | CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) { |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3125 | CharUnits sz = getTypeSizeInChars(type); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3126 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3127 | // Make all integer and enum types at least as large as an int |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3128 | if (sz.isPositive() && type->isIntegralType()) |
| 3129 | sz = std::max(sz, getTypeSizeInChars(IntTy)); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3130 | // Treat arrays as pointers, since that's how they're passed in. |
| 3131 | else if (type->isArrayType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3132 | sz = getTypeSizeInChars(VoidPtrTy); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3133 | return sz; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3134 | } |
| 3135 | |
| 3136 | static inline |
| 3137 | std::string charUnitsToString(const CharUnits &CU) { |
| 3138 | return llvm::itostr(CU.getQuantity()); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3139 | } |
| 3140 | |
Fariborz Jahanian | 6f46c26 | 2010-04-08 18:06:22 +0000 | [diff] [blame] | 3141 | /// getObjCEncodingForBlockDecl - Return the encoded type for this block |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3142 | /// declaration. |
| 3143 | void ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr, |
| 3144 | std::string& S) { |
| 3145 | const BlockDecl *Decl = Expr->getBlockDecl(); |
| 3146 | QualType BlockTy = |
| 3147 | Expr->getType()->getAs<BlockPointerType>()->getPointeeType(); |
| 3148 | // Encode result type. |
| 3149 | getObjCEncodingForType(cast<FunctionType>(BlockTy)->getResultType(), S); |
| 3150 | // Compute size of all parameters. |
| 3151 | // Start with computing size of a pointer in number of bytes. |
| 3152 | // FIXME: There might(should) be a better way of doing this computation! |
| 3153 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3154 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
| 3155 | CharUnits ParmOffset = PtrSize; |
Fariborz Jahanian | 6f46c26 | 2010-04-08 18:06:22 +0000 | [diff] [blame] | 3156 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3157 | E = Decl->param_end(); PI != E; ++PI) { |
| 3158 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3159 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3160 | assert (sz.isPositive() && "BlockExpr - Incomplete param type"); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3161 | ParmOffset += sz; |
| 3162 | } |
| 3163 | // Size of the argument frame |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3164 | S += charUnitsToString(ParmOffset); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3165 | // Block pointer and offset. |
| 3166 | S += "@?0"; |
| 3167 | ParmOffset = PtrSize; |
| 3168 | |
| 3169 | // Argument types. |
| 3170 | ParmOffset = PtrSize; |
| 3171 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E = |
| 3172 | Decl->param_end(); PI != E; ++PI) { |
| 3173 | ParmVarDecl *PVDecl = *PI; |
| 3174 | QualType PType = PVDecl->getOriginalType(); |
| 3175 | if (const ArrayType *AT = |
| 3176 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3177 | // Use array's original type only if it has known number of |
| 3178 | // elements. |
| 3179 | if (!isa<ConstantArrayType>(AT)) |
| 3180 | PType = PVDecl->getType(); |
| 3181 | } else if (PType->isFunctionType()) |
| 3182 | PType = PVDecl->getType(); |
| 3183 | getObjCEncodingForType(PType, S); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3184 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3185 | ParmOffset += getObjCEncodingTypeSize(PType); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3186 | } |
| 3187 | } |
| 3188 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3189 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3190 | /// declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3191 | void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Chris Lattner | e6db3b0 | 2008-11-19 07:24:05 +0000 | [diff] [blame] | 3192 | std::string& S) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3193 | // FIXME: This is not very efficient. |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3194 | // Encode type qualifer, 'in', 'inout', etc. for the return type. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3195 | getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3196 | // Encode result type. |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3197 | getObjCEncodingForType(Decl->getResultType(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3198 | // Compute size of all parameters. |
| 3199 | // Start with computing size of a pointer in number of bytes. |
| 3200 | // FIXME: There might(should) be a better way of doing this computation! |
| 3201 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3202 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3203 | // The first two arguments (self and _cmd) are pointers; account for |
| 3204 | // their size. |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3205 | CharUnits ParmOffset = 2 * PtrSize; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3206 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 3207 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3208 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3209 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3210 | assert (sz.isPositive() && |
| 3211 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3212 | ParmOffset += sz; |
| 3213 | } |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3214 | S += charUnitsToString(ParmOffset); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3215 | S += "@0:"; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3216 | S += charUnitsToString(PtrSize); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3217 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3218 | // Argument types. |
| 3219 | ParmOffset = 2 * PtrSize; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3220 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 3221 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3222 | ParmVarDecl *PVDecl = *PI; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3223 | QualType PType = PVDecl->getOriginalType(); |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 3224 | if (const ArrayType *AT = |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 3225 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3226 | // Use array's original type only if it has known number of |
| 3227 | // elements. |
Steve Naroff | bb3fde3 | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 3228 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 3229 | PType = PVDecl->getType(); |
| 3230 | } else if (PType->isFunctionType()) |
| 3231 | PType = PVDecl->getType(); |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3232 | // Process argument qualifiers for user supplied arguments; such as, |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3233 | // 'in', 'inout', etc. |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 3234 | getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3235 | getObjCEncodingForType(PType, S); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3236 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3237 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3238 | } |
| 3239 | } |
| 3240 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3241 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 3242 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3243 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 3244 | /// NULL when getting encodings for protocol properties. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3245 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 3246 | /// attributes readonly and bycopy are encoded as single characters. The |
| 3247 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 3248 | /// encoded as single characters, followed by an identifier. Property types |
| 3249 | /// are also encoded as a parametrized attribute. The characters used to encode |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 3250 | /// these attributes are defined by the following enumeration: |
| 3251 | /// @code |
| 3252 | /// enum PropertyAttributes { |
| 3253 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 3254 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 3255 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 3256 | /// kPropertyDynamic = 'D', // property is dynamic |
| 3257 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 3258 | /// kPropertySetter = 'S', // followed by setter selector name |
| 3259 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
| 3260 | /// kPropertyType = 't' // followed by old-style type encoding. |
| 3261 | /// kPropertyWeak = 'W' // 'weak' property |
| 3262 | /// kPropertyStrong = 'P' // property GC'able |
| 3263 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 3264 | /// }; |
| 3265 | /// @endcode |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3266 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3267 | const Decl *Container, |
Chris Lattner | e6db3b0 | 2008-11-19 07:24:05 +0000 | [diff] [blame] | 3268 | std::string& S) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3269 | // Collect information from the property implementation decl(s). |
| 3270 | bool Dynamic = false; |
| 3271 | ObjCPropertyImplDecl *SynthesizePID = 0; |
| 3272 | |
| 3273 | // FIXME: Duplicated code due to poor abstraction. |
| 3274 | if (Container) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3275 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3276 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
| 3277 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3278 | i = CID->propimpl_begin(), e = CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3279 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3280 | ObjCPropertyImplDecl *PID = *i; |
| 3281 | if (PID->getPropertyDecl() == PD) { |
| 3282 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 3283 | Dynamic = true; |
| 3284 | } else { |
| 3285 | SynthesizePID = PID; |
| 3286 | } |
| 3287 | } |
| 3288 | } |
| 3289 | } else { |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3290 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3291 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3292 | i = OID->propimpl_begin(), e = OID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3293 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3294 | ObjCPropertyImplDecl *PID = *i; |
| 3295 | if (PID->getPropertyDecl() == PD) { |
| 3296 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 3297 | Dynamic = true; |
| 3298 | } else { |
| 3299 | SynthesizePID = PID; |
| 3300 | } |
| 3301 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3302 | } |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3303 | } |
| 3304 | } |
| 3305 | |
| 3306 | // FIXME: This is not very efficient. |
| 3307 | S = "T"; |
| 3308 | |
| 3309 | // Encode result type. |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3310 | // GCC has some special rules regarding encoding of properties which |
| 3311 | // closely resembles encoding of ivars. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3312 | getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3313 | true /* outermost type */, |
| 3314 | true /* encoding for property */); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3315 | |
| 3316 | if (PD->isReadOnly()) { |
| 3317 | S += ",R"; |
| 3318 | } else { |
| 3319 | switch (PD->getSetterKind()) { |
| 3320 | case ObjCPropertyDecl::Assign: break; |
| 3321 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3322 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3323 | } |
| 3324 | } |
| 3325 | |
| 3326 | // It really isn't clear at all what this means, since properties |
| 3327 | // are "dynamic by default". |
| 3328 | if (Dynamic) |
| 3329 | S += ",D"; |
| 3330 | |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3331 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 3332 | S += ",N"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3333 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3334 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 3335 | S += ",G"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3336 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3337 | } |
| 3338 | |
| 3339 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 3340 | S += ",S"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3341 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3342 | } |
| 3343 | |
| 3344 | if (SynthesizePID) { |
| 3345 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 3346 | S += ",V"; |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 3347 | S += OID->getNameAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3348 | } |
| 3349 | |
| 3350 | // FIXME: OBJCGC: weak & strong |
| 3351 | } |
| 3352 | |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3353 | /// getLegacyIntegralTypeEncoding - |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3354 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
| 3355 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3356 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
| 3357 | /// |
| 3358 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3359 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3360 | if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) { |
Fariborz Jahanian | c657eba | 2009-02-11 23:59:18 +0000 | [diff] [blame] | 3361 | if (BT->getKind() == BuiltinType::ULong && |
| 3362 | ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3363 | PointeeTy = UnsignedIntTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3364 | else |
Fariborz Jahanian | c657eba | 2009-02-11 23:59:18 +0000 | [diff] [blame] | 3365 | if (BT->getKind() == BuiltinType::Long && |
| 3366 | ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3367 | PointeeTy = IntTy; |
| 3368 | } |
| 3369 | } |
| 3370 | } |
| 3371 | |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3372 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 3373 | const FieldDecl *Field) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3374 | // We follow the behavior of gcc, expanding structures which are |
| 3375 | // directly pointed to, and expanding embedded structures. Note that |
| 3376 | // these rules are sufficient to prevent recursive encoding of the |
| 3377 | // same type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3378 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
Fariborz Jahanian | 5b8c7d9 | 2008-12-22 23:22:27 +0000 | [diff] [blame] | 3379 | true /* outermost type */); |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3380 | } |
| 3381 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3382 | static void EncodeBitField(const ASTContext *Context, std::string& S, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 3383 | const FieldDecl *FD) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3384 | const Expr *E = FD->getBitWidth(); |
| 3385 | assert(E && "bitfield width not there - getObjCEncodingForTypeImpl"); |
| 3386 | ASTContext *Ctx = const_cast<ASTContext*>(Context); |
Eli Friedman | 9a901bb | 2009-04-26 19:19:15 +0000 | [diff] [blame] | 3387 | unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue(); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3388 | S += 'b'; |
| 3389 | S += llvm::utostr(N); |
| 3390 | } |
| 3391 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 3392 | // FIXME: Use SmallString for accumulating string. |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3393 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 3394 | bool ExpandPointedToStructures, |
| 3395 | bool ExpandStructures, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 3396 | const FieldDecl *FD, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3397 | bool OutermostType, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3398 | bool EncodingProperty) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3399 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3400 | if (FD && FD->isBitField()) |
| 3401 | return EncodeBitField(this, S, FD); |
| 3402 | char encoding; |
| 3403 | switch (BT->getKind()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3404 | default: assert(0 && "Unhandled builtin type kind"); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3405 | case BuiltinType::Void: encoding = 'v'; break; |
| 3406 | case BuiltinType::Bool: encoding = 'B'; break; |
| 3407 | case BuiltinType::Char_U: |
| 3408 | case BuiltinType::UChar: encoding = 'C'; break; |
| 3409 | case BuiltinType::UShort: encoding = 'S'; break; |
| 3410 | case BuiltinType::UInt: encoding = 'I'; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3411 | case BuiltinType::ULong: |
| 3412 | encoding = |
| 3413 | (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q'; |
Fariborz Jahanian | 72696e1 | 2009-02-11 22:31:45 +0000 | [diff] [blame] | 3414 | break; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3415 | case BuiltinType::UInt128: encoding = 'T'; break; |
| 3416 | case BuiltinType::ULongLong: encoding = 'Q'; break; |
| 3417 | case BuiltinType::Char_S: |
| 3418 | case BuiltinType::SChar: encoding = 'c'; break; |
| 3419 | case BuiltinType::Short: encoding = 's'; break; |
| 3420 | case BuiltinType::Int: encoding = 'i'; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3421 | case BuiltinType::Long: |
| 3422 | encoding = |
| 3423 | (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3424 | break; |
| 3425 | case BuiltinType::LongLong: encoding = 'q'; break; |
| 3426 | case BuiltinType::Int128: encoding = 't'; break; |
| 3427 | case BuiltinType::Float: encoding = 'f'; break; |
| 3428 | case BuiltinType::Double: encoding = 'd'; break; |
| 3429 | case BuiltinType::LongDouble: encoding = 'd'; break; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3430 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3431 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3432 | S += encoding; |
| 3433 | return; |
| 3434 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3435 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3436 | if (const ComplexType *CT = T->getAs<ComplexType>()) { |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 3437 | S += 'j'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3438 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false, |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 3439 | false); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3440 | return; |
| 3441 | } |
Fariborz Jahanian | 60bce3e | 2009-11-23 20:40:50 +0000 | [diff] [blame] | 3442 | |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 3443 | // encoding for pointer or r3eference types. |
| 3444 | QualType PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3445 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Fariborz Jahanian | 8d2c0a9 | 2009-11-30 18:43:52 +0000 | [diff] [blame] | 3446 | if (PT->isObjCSelType()) { |
| 3447 | S += ':'; |
| 3448 | return; |
| 3449 | } |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 3450 | PointeeTy = PT->getPointeeType(); |
| 3451 | } |
| 3452 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 3453 | PointeeTy = RT->getPointeeType(); |
| 3454 | if (!PointeeTy.isNull()) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3455 | bool isReadOnly = false; |
| 3456 | // For historical/compatibility reasons, the read-only qualifier of the |
| 3457 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 3458 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3459 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3460 | if (isa<TypedefType>(T.getTypePtr())) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3461 | if (OutermostType && T.isConstQualified()) { |
| 3462 | isReadOnly = true; |
| 3463 | S += 'r'; |
| 3464 | } |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 3465 | } else if (OutermostType) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3466 | QualType P = PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3467 | while (P->getAs<PointerType>()) |
| 3468 | P = P->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3469 | if (P.isConstQualified()) { |
| 3470 | isReadOnly = true; |
| 3471 | S += 'r'; |
| 3472 | } |
| 3473 | } |
| 3474 | if (isReadOnly) { |
| 3475 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 3476 | // combinations need to be rearranged. |
| 3477 | // Rewrite "in const" from "nr" to "rn" |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 3478 | if (llvm::StringRef(S).endswith("nr")) |
| 3479 | S.replace(S.end()-2, S.end(), "rn"); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3480 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3481 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3482 | if (PointeeTy->isCharType()) { |
| 3483 | // char pointer types should be encoded as '*' unless it is a |
| 3484 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3485 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3486 | S += '*'; |
| 3487 | return; |
| 3488 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3489 | } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) { |
Steve Naroff | 9533a7f | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 3490 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 3491 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 3492 | S += '#'; |
| 3493 | return; |
| 3494 | } |
| 3495 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 3496 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 3497 | S += '@'; |
| 3498 | return; |
| 3499 | } |
| 3500 | // fall through... |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3501 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3502 | S += '^'; |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3503 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 3504 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3505 | getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3506 | NULL); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3507 | return; |
| 3508 | } |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 3509 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3510 | if (const ArrayType *AT = |
| 3511 | // Ignore type qualifiers etc. |
| 3512 | dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3513 | if (isa<IncompleteArrayType>(AT)) { |
| 3514 | // Incomplete arrays are encoded as a pointer to the array element. |
| 3515 | S += '^'; |
| 3516 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3517 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3518 | false, ExpandStructures, FD); |
| 3519 | } else { |
| 3520 | S += '['; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3521 | |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3522 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 3523 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 3524 | else { |
| 3525 | //Variable length arrays are encoded as a regular array with 0 elements. |
| 3526 | assert(isa<VariableArrayType>(AT) && "Unknown array type!"); |
| 3527 | S += '0'; |
| 3528 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3529 | |
| 3530 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3531 | false, ExpandStructures, FD); |
| 3532 | S += ']'; |
| 3533 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3534 | return; |
| 3535 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3536 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3537 | if (T->getAs<FunctionType>()) { |
Anders Carlsson | c0a87b7 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 3538 | S += '?'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3539 | return; |
| 3540 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3541 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3542 | if (const RecordType *RTy = T->getAs<RecordType>()) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3543 | RecordDecl *RDecl = RTy->getDecl(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3544 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 3545 | // Anonymous structures print as '?' |
| 3546 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 3547 | S += II->getName(); |
Fariborz Jahanian | 6fb9439 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 3548 | if (ClassTemplateSpecializationDecl *Spec |
| 3549 | = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) { |
| 3550 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 3551 | std::string TemplateArgsStr |
| 3552 | = TemplateSpecializationType::PrintTemplateArgumentList( |
| 3553 | TemplateArgs.getFlatArgumentList(), |
| 3554 | TemplateArgs.flat_size(), |
| 3555 | (*this).PrintingPolicy); |
| 3556 | |
| 3557 | S += TemplateArgsStr; |
| 3558 | } |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 3559 | } else { |
| 3560 | S += '?'; |
| 3561 | } |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3562 | if (ExpandStructures) { |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3563 | S += '='; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3564 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 3565 | FieldEnd = RDecl->field_end(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3566 | Field != FieldEnd; ++Field) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3567 | if (FD) { |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3568 | S += '"'; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3569 | S += Field->getNameAsString(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3570 | S += '"'; |
| 3571 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3572 | |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3573 | // Special case bit-fields. |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3574 | if (Field->isBitField()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3575 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3576 | (*Field)); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3577 | } else { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3578 | QualType qt = Field->getType(); |
| 3579 | getLegacyIntegralTypeEncoding(qt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3580 | getObjCEncodingForTypeImpl(qt, S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3581 | FD); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3582 | } |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3583 | } |
Fariborz Jahanian | 6de88a8 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 3584 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3585 | S += RDecl->isUnion() ? ')' : '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3586 | return; |
| 3587 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3588 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3589 | if (T->isEnumeralType()) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3590 | if (FD && FD->isBitField()) |
| 3591 | EncodeBitField(this, S, FD); |
| 3592 | else |
| 3593 | S += 'i'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3594 | return; |
| 3595 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3596 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3597 | if (T->isBlockPointerType()) { |
Steve Naroff | 21a98b1 | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 3598 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3599 | return; |
| 3600 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3601 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3602 | // Ignore protocol qualifiers when mangling at this level. |
| 3603 | if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>()) |
| 3604 | T = OT->getBaseType(); |
| 3605 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3606 | if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3607 | // @encode(class_name) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3608 | ObjCInterfaceDecl *OI = OIT->getDecl(); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3609 | S += '{'; |
| 3610 | const IdentifierInfo *II = OI->getIdentifier(); |
| 3611 | S += II->getName(); |
| 3612 | S += '='; |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3613 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3614 | CollectObjCIvars(OI, RecFields); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3615 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3616 | if (RecFields[i]->isBitField()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3617 | getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3618 | RecFields[i]); |
| 3619 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3620 | getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3621 | FD); |
| 3622 | } |
| 3623 | S += '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3624 | return; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3625 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3626 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3627 | if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3628 | if (OPT->isObjCIdType()) { |
| 3629 | S += '@'; |
| 3630 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3631 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3632 | |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 3633 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
| 3634 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
| 3635 | // Since this is a binary compatibility issue, need to consult with runtime |
| 3636 | // folks. Fortunately, this is a *very* obsure construct. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3637 | S += '#'; |
| 3638 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3639 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3640 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3641 | if (OPT->isObjCQualifiedIdType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3642 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3643 | ExpandPointedToStructures, |
| 3644 | ExpandStructures, FD); |
| 3645 | if (FD || EncodingProperty) { |
| 3646 | // Note that we do extended encoding of protocol qualifer list |
| 3647 | // Only when doing ivar or property encoding. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3648 | S += '"'; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3649 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 3650 | E = OPT->qual_end(); I != E; ++I) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3651 | S += '<'; |
| 3652 | S += (*I)->getNameAsString(); |
| 3653 | S += '>'; |
| 3654 | } |
| 3655 | S += '"'; |
| 3656 | } |
| 3657 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3658 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3659 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3660 | QualType PointeeTy = OPT->getPointeeType(); |
| 3661 | if (!EncodingProperty && |
| 3662 | isa<TypedefType>(PointeeTy.getTypePtr())) { |
| 3663 | // Another historical/compatibility reason. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3664 | // We encode the underlying type which comes out as |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3665 | // {...}; |
| 3666 | S += '^'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3667 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 3668 | false, ExpandPointedToStructures, |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3669 | NULL); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3670 | return; |
| 3671 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3672 | |
| 3673 | S += '@'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 3674 | if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3675 | S += '"'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 3676 | S += OPT->getInterfaceDecl()->getIdentifier()->getName(); |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3677 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 3678 | E = OPT->qual_end(); I != E; ++I) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3679 | S += '<'; |
| 3680 | S += (*I)->getNameAsString(); |
| 3681 | S += '>'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3682 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3683 | S += '"'; |
| 3684 | } |
| 3685 | return; |
| 3686 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3687 | |
John McCall | 532ec7b | 2010-05-17 23:56:34 +0000 | [diff] [blame] | 3688 | // gcc just blithely ignores member pointers. |
| 3689 | // TODO: maybe there should be a mangling for these |
| 3690 | if (T->getAs<MemberPointerType>()) |
| 3691 | return; |
| 3692 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3693 | assert(0 && "@encode for type not implemented!"); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3694 | } |
| 3695 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3696 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3697 | std::string& S) const { |
| 3698 | if (QT & Decl::OBJC_TQ_In) |
| 3699 | S += 'n'; |
| 3700 | if (QT & Decl::OBJC_TQ_Inout) |
| 3701 | S += 'N'; |
| 3702 | if (QT & Decl::OBJC_TQ_Out) |
| 3703 | S += 'o'; |
| 3704 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 3705 | S += 'O'; |
| 3706 | if (QT & Decl::OBJC_TQ_Byref) |
| 3707 | S += 'R'; |
| 3708 | if (QT & Decl::OBJC_TQ_Oneway) |
| 3709 | S += 'V'; |
| 3710 | } |
| 3711 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3712 | void ASTContext::setBuiltinVaListType(QualType T) { |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 3713 | assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3714 | |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 3715 | BuiltinVaListType = T; |
| 3716 | } |
| 3717 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3718 | void ASTContext::setObjCIdType(QualType T) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3719 | ObjCIdTypedefType = T; |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 3720 | } |
| 3721 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3722 | void ASTContext::setObjCSelType(QualType T) { |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 3723 | ObjCSelTypedefType = T; |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 3724 | } |
| 3725 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3726 | void ASTContext::setObjCProtoType(QualType QT) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3727 | ObjCProtoType = QT; |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 3728 | } |
| 3729 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3730 | void ASTContext::setObjCClassType(QualType T) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3731 | ObjCClassTypedefType = T; |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 3732 | } |
| 3733 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3734 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3735 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 3736 | "'NSConstantString' type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3737 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3738 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 3739 | } |
| 3740 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 3741 | /// \brief Retrieve the template name that corresponds to a non-empty |
| 3742 | /// lookup. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3743 | TemplateName ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin, |
| 3744 | UnresolvedSetIterator End) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 3745 | unsigned size = End - Begin; |
| 3746 | assert(size > 1 && "set is not overloaded!"); |
| 3747 | |
| 3748 | void *memory = Allocate(sizeof(OverloadedTemplateStorage) + |
| 3749 | size * sizeof(FunctionTemplateDecl*)); |
| 3750 | OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size); |
| 3751 | |
| 3752 | NamedDecl **Storage = OT->getStorage(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3753 | for (UnresolvedSetIterator I = Begin; I != End; ++I) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 3754 | NamedDecl *D = *I; |
| 3755 | assert(isa<FunctionTemplateDecl>(D) || |
| 3756 | (isa<UsingShadowDecl>(D) && |
| 3757 | isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))); |
| 3758 | *Storage++ = D; |
| 3759 | } |
| 3760 | |
| 3761 | return TemplateName(OT); |
| 3762 | } |
| 3763 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3764 | /// \brief Retrieve the template name that represents a qualified |
| 3765 | /// template name such as \c std::vector. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3766 | TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3767 | bool TemplateKeyword, |
| 3768 | TemplateDecl *Template) { |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3769 | // FIXME: Canonicalization? |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3770 | llvm::FoldingSetNodeID ID; |
| 3771 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 3772 | |
| 3773 | void *InsertPos = 0; |
| 3774 | QualifiedTemplateName *QTN = |
| 3775 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 3776 | if (!QTN) { |
| 3777 | QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); |
| 3778 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 3779 | } |
| 3780 | |
| 3781 | return TemplateName(QTN); |
| 3782 | } |
| 3783 | |
| 3784 | /// \brief Retrieve the template name that represents a dependent |
| 3785 | /// template name such as \c MetaFun::template apply. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3786 | TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3787 | const IdentifierInfo *Name) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3788 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 3789 | "Nested name specifier must be dependent"); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3790 | |
| 3791 | llvm::FoldingSetNodeID ID; |
| 3792 | DependentTemplateName::Profile(ID, NNS, Name); |
| 3793 | |
| 3794 | void *InsertPos = 0; |
| 3795 | DependentTemplateName *QTN = |
| 3796 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 3797 | |
| 3798 | if (QTN) |
| 3799 | return TemplateName(QTN); |
| 3800 | |
| 3801 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 3802 | if (CanonNNS == NNS) { |
| 3803 | QTN = new (*this,4) DependentTemplateName(NNS, Name); |
| 3804 | } else { |
| 3805 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
| 3806 | QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3807 | DependentTemplateName *CheckQTN = |
| 3808 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 3809 | assert(!CheckQTN && "Dependent type name canonicalization broken"); |
| 3810 | (void)CheckQTN; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3811 | } |
| 3812 | |
| 3813 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 3814 | return TemplateName(QTN); |
| 3815 | } |
| 3816 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 3817 | /// \brief Retrieve the template name that represents a dependent |
| 3818 | /// template name such as \c MetaFun::template operator+. |
| 3819 | TemplateName |
| 3820 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
| 3821 | OverloadedOperatorKind Operator) { |
| 3822 | assert((!NNS || NNS->isDependent()) && |
| 3823 | "Nested name specifier must be dependent"); |
| 3824 | |
| 3825 | llvm::FoldingSetNodeID ID; |
| 3826 | DependentTemplateName::Profile(ID, NNS, Operator); |
| 3827 | |
| 3828 | void *InsertPos = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3829 | DependentTemplateName *QTN |
| 3830 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 3831 | |
| 3832 | if (QTN) |
| 3833 | return TemplateName(QTN); |
| 3834 | |
| 3835 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 3836 | if (CanonNNS == NNS) { |
| 3837 | QTN = new (*this,4) DependentTemplateName(NNS, Operator); |
| 3838 | } else { |
| 3839 | TemplateName Canon = getDependentTemplateName(CanonNNS, Operator); |
| 3840 | QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3841 | |
| 3842 | DependentTemplateName *CheckQTN |
| 3843 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 3844 | assert(!CheckQTN && "Dependent template name canonicalization broken"); |
| 3845 | (void)CheckQTN; |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 3846 | } |
| 3847 | |
| 3848 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 3849 | return TemplateName(QTN); |
| 3850 | } |
| 3851 | |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 3852 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | d934112 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 3853 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 3854 | /// is actually a value of type @c TargetInfo::IntType. |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 3855 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 3856 | switch (Type) { |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 3857 | case TargetInfo::NoInt: return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 3858 | case TargetInfo::SignedShort: return ShortTy; |
| 3859 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 3860 | case TargetInfo::SignedInt: return IntTy; |
| 3861 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 3862 | case TargetInfo::SignedLong: return LongTy; |
| 3863 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 3864 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 3865 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 3866 | } |
| 3867 | |
| 3868 | assert(false && "Unhandled TargetInfo::IntType value"); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 3869 | return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 3870 | } |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 3871 | |
| 3872 | //===----------------------------------------------------------------------===// |
| 3873 | // Type Predicates. |
| 3874 | //===----------------------------------------------------------------------===// |
| 3875 | |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3876 | /// isObjCNSObjectType - Return true if this is an NSObject object using |
| 3877 | /// NSObject attribute on a c-style pointer type. |
| 3878 | /// FIXME - Make it work directly on types. |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 3879 | /// FIXME: Move to Type. |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3880 | /// |
| 3881 | bool ASTContext::isObjCNSObjectType(QualType Ty) const { |
| 3882 | if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) { |
| 3883 | if (TypedefDecl *TD = TDT->getDecl()) |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 3884 | if (TD->getAttr<ObjCNSObjectAttr>()) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3885 | return true; |
| 3886 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3887 | return false; |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3888 | } |
| 3889 | |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3890 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 3891 | /// garbage collection attribute. |
| 3892 | /// |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3893 | Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const { |
| 3894 | Qualifiers::GC GCAttrs = Qualifiers::GCNone; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3895 | if (getLangOptions().ObjC1 && |
| 3896 | getLangOptions().getGCMode() != LangOptions::NonGC) { |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 3897 | GCAttrs = Ty.getObjCGCAttr(); |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3898 | // Default behavious under objective-c's gc is for objective-c pointers |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3899 | // (or pointers to them) be treated as though they were declared |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 3900 | // as __strong. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3901 | if (GCAttrs == Qualifiers::GCNone) { |
Fariborz Jahanian | 75212ee | 2009-09-10 23:38:45 +0000 | [diff] [blame] | 3902 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3903 | GCAttrs = Qualifiers::Strong; |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 3904 | else if (Ty->isPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3905 | return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 3906 | } |
Fariborz Jahanian | c211218 | 2009-04-11 00:00:54 +0000 | [diff] [blame] | 3907 | // Non-pointers have none gc'able attribute regardless of the attribute |
| 3908 | // set on them. |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 3909 | else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3910 | return Qualifiers::GCNone; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3911 | } |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 3912 | return GCAttrs; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3913 | } |
| 3914 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3915 | //===----------------------------------------------------------------------===// |
| 3916 | // Type Compatibility Testing |
| 3917 | //===----------------------------------------------------------------------===// |
Chris Lattner | 770951b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 3918 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3919 | /// areCompatVectorTypes - Return true if the two specified vector types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3920 | /// compatible. |
| 3921 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 3922 | const VectorType *RHS) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 3923 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3924 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3925 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3926 | } |
| 3927 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3928 | //===----------------------------------------------------------------------===// |
| 3929 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 3930 | //===----------------------------------------------------------------------===// |
| 3931 | |
| 3932 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 3933 | /// inheritance hierarchy of 'rProto'. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 3934 | bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 3935 | ObjCProtocolDecl *rProto) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3936 | if (lProto == rProto) |
| 3937 | return true; |
| 3938 | for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), |
| 3939 | E = rProto->protocol_end(); PI != E; ++PI) |
| 3940 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 3941 | return true; |
| 3942 | return false; |
| 3943 | } |
| 3944 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3945 | /// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...> |
| 3946 | /// return true if lhs's protocols conform to rhs's protocol; false |
| 3947 | /// otherwise. |
| 3948 | bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) { |
| 3949 | if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType()) |
| 3950 | return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false); |
| 3951 | return false; |
| 3952 | } |
| 3953 | |
| 3954 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 3955 | /// ObjCQualifiedIDType. |
| 3956 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 3957 | bool compare) { |
| 3958 | // Allow id<P..> and an 'id' or void* type in all cases. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3959 | if (lhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3960 | lhs->isObjCIdType() || lhs->isObjCClassType()) |
| 3961 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3962 | else if (rhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3963 | rhs->isObjCIdType() || rhs->isObjCClassType()) |
| 3964 | return true; |
| 3965 | |
| 3966 | if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3967 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3968 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3969 | if (!rhsOPT) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3970 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3971 | if (rhsOPT->qual_empty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3972 | // If the RHS is a unqualified interface pointer "NSString*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3973 | // make sure we check the class hierarchy. |
| 3974 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 3975 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 3976 | E = lhsQID->qual_end(); I != E; ++I) { |
| 3977 | // when comparing an id<P> on lhs with a static type on rhs, |
| 3978 | // see if static class implements all of id's protocols, directly or |
| 3979 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 3980 | if (!rhsID->ClassImplementsProtocol(*I, true)) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3981 | return false; |
| 3982 | } |
| 3983 | } |
| 3984 | // If there are no qualifiers and no interface, we have an 'id'. |
| 3985 | return true; |
| 3986 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3987 | // Both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3988 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 3989 | E = lhsQID->qual_end(); I != E; ++I) { |
| 3990 | ObjCProtocolDecl *lhsProto = *I; |
| 3991 | bool match = false; |
| 3992 | |
| 3993 | // when comparing an id<P> on lhs with a static type on rhs, |
| 3994 | // see if static class implements all of id's protocols, directly or |
| 3995 | // through its super class and categories. |
| 3996 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 3997 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 3998 | ObjCProtocolDecl *rhsProto = *J; |
| 3999 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 4000 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 4001 | match = true; |
| 4002 | break; |
| 4003 | } |
| 4004 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4005 | // If the RHS is a qualified interface pointer "NSString<P>*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4006 | // make sure we check the class hierarchy. |
| 4007 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 4008 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4009 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4010 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4011 | // see if static class implements all of id's protocols, directly or |
| 4012 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 4013 | if (rhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4014 | match = true; |
| 4015 | break; |
| 4016 | } |
| 4017 | } |
| 4018 | } |
| 4019 | if (!match) |
| 4020 | return false; |
| 4021 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4022 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4023 | return true; |
| 4024 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4025 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4026 | const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 4027 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 4028 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4029 | if (const ObjCObjectPointerType *lhsOPT = |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4030 | lhs->getAsObjCInterfacePointerType()) { |
| 4031 | if (lhsOPT->qual_empty()) { |
| 4032 | bool match = false; |
| 4033 | if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { |
| 4034 | for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(), |
| 4035 | E = rhsQID->qual_end(); I != E; ++I) { |
| 4036 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4037 | // see if static class implements all of id's protocols, directly or |
| 4038 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 4039 | if (lhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4040 | match = true; |
| 4041 | break; |
| 4042 | } |
| 4043 | } |
| 4044 | if (!match) |
| 4045 | return false; |
| 4046 | } |
| 4047 | return true; |
| 4048 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4049 | // Both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4050 | for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(), |
| 4051 | E = lhsOPT->qual_end(); I != E; ++I) { |
| 4052 | ObjCProtocolDecl *lhsProto = *I; |
| 4053 | bool match = false; |
| 4054 | |
| 4055 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4056 | // see if static class implements all of id's protocols, directly or |
| 4057 | // through its super class and categories. |
| 4058 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 4059 | E = rhsQID->qual_end(); J != E; ++J) { |
| 4060 | ObjCProtocolDecl *rhsProto = *J; |
| 4061 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 4062 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 4063 | match = true; |
| 4064 | break; |
| 4065 | } |
| 4066 | } |
| 4067 | if (!match) |
| 4068 | return false; |
| 4069 | } |
| 4070 | return true; |
| 4071 | } |
| 4072 | return false; |
| 4073 | } |
| 4074 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4075 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4076 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 4077 | /// protocol qualifiers on the LHS or RHS. |
| 4078 | /// |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4079 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 4080 | const ObjCObjectPointerType *RHSOPT) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4081 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 4082 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 4083 | |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 4084 | // 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] | 4085 | if (LHS->isObjCUnqualifiedIdOrClass() || |
| 4086 | RHS->isObjCUnqualifiedIdOrClass()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4087 | return true; |
| 4088 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4089 | if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4090 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 4091 | QualType(RHSOPT,0), |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4092 | false); |
| 4093 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4094 | // If we have 2 user-defined types, fall into that path. |
| 4095 | if (LHS->getInterface() && RHS->getInterface()) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4096 | return canAssignObjCInterfaces(LHS, RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4097 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4098 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4099 | } |
| 4100 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4101 | /// canAssignObjCInterfacesInBlockPointer - This routine is specifically written |
| 4102 | /// for providing type-safty for objective-c pointers used to pass/return |
| 4103 | /// arguments in block literals. When passed as arguments, passing 'A*' where |
| 4104 | /// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is |
| 4105 | /// not OK. For the return type, the opposite is not OK. |
| 4106 | bool ASTContext::canAssignObjCInterfacesInBlockPointer( |
| 4107 | const ObjCObjectPointerType *LHSOPT, |
| 4108 | const ObjCObjectPointerType *RHSOPT) { |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 4109 | if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4110 | return true; |
| 4111 | |
| 4112 | if (LHSOPT->isObjCBuiltinType()) { |
| 4113 | return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType(); |
| 4114 | } |
| 4115 | |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 4116 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4117 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 4118 | QualType(RHSOPT,0), |
| 4119 | false); |
| 4120 | |
| 4121 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 4122 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 4123 | if (LHS && RHS) { // We have 2 user-defined types. |
| 4124 | if (LHS != RHS) { |
| 4125 | if (LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
| 4126 | return false; |
| 4127 | if (RHS->getDecl()->isSuperClassOf(LHS->getDecl())) |
| 4128 | return true; |
| 4129 | } |
| 4130 | else |
| 4131 | return true; |
| 4132 | } |
| 4133 | return false; |
| 4134 | } |
| 4135 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4136 | /// getIntersectionOfProtocols - This routine finds the intersection of set |
| 4137 | /// of protocols inherited from two distinct objective-c pointer objects. |
| 4138 | /// It is used to build composite qualifier list of the composite type of |
| 4139 | /// the conditional expression involving two objective-c pointer objects. |
| 4140 | static |
| 4141 | void getIntersectionOfProtocols(ASTContext &Context, |
| 4142 | const ObjCObjectPointerType *LHSOPT, |
| 4143 | const ObjCObjectPointerType *RHSOPT, |
| 4144 | llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) { |
| 4145 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4146 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 4147 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 4148 | assert(LHS->getInterface() && "LHS must have an interface base"); |
| 4149 | assert(RHS->getInterface() && "RHS must have an interface base"); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4150 | |
| 4151 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet; |
| 4152 | unsigned LHSNumProtocols = LHS->getNumProtocols(); |
| 4153 | if (LHSNumProtocols > 0) |
| 4154 | InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end()); |
| 4155 | else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 4156 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4157 | Context.CollectInheritedProtocols(LHS->getInterface(), |
| 4158 | LHSInheritedProtocols); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4159 | InheritedProtocolSet.insert(LHSInheritedProtocols.begin(), |
| 4160 | LHSInheritedProtocols.end()); |
| 4161 | } |
| 4162 | |
| 4163 | unsigned RHSNumProtocols = RHS->getNumProtocols(); |
| 4164 | if (RHSNumProtocols > 0) { |
Dan Gohman | cb421fa | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 4165 | ObjCProtocolDecl **RHSProtocols = |
| 4166 | const_cast<ObjCProtocolDecl **>(RHS->qual_begin()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4167 | for (unsigned i = 0; i < RHSNumProtocols; ++i) |
| 4168 | if (InheritedProtocolSet.count(RHSProtocols[i])) |
| 4169 | IntersectionOfProtocols.push_back(RHSProtocols[i]); |
| 4170 | } |
| 4171 | else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 4172 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4173 | Context.CollectInheritedProtocols(RHS->getInterface(), |
| 4174 | RHSInheritedProtocols); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 4175 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 4176 | RHSInheritedProtocols.begin(), |
| 4177 | E = RHSInheritedProtocols.end(); I != E; ++I) |
| 4178 | if (InheritedProtocolSet.count((*I))) |
| 4179 | IntersectionOfProtocols.push_back((*I)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4180 | } |
| 4181 | } |
| 4182 | |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4183 | /// areCommonBaseCompatible - Returns common base class of the two classes if |
| 4184 | /// one found. Note that this is O'2 algorithm. But it will be called as the |
| 4185 | /// last type comparison in a ?-exp of ObjC pointer types before a |
| 4186 | /// warning is issued. So, its invokation is extremely rare. |
| 4187 | QualType ASTContext::areCommonBaseCompatible( |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4188 | const ObjCObjectPointerType *Lptr, |
| 4189 | const ObjCObjectPointerType *Rptr) { |
| 4190 | const ObjCObjectType *LHS = Lptr->getObjectType(); |
| 4191 | const ObjCObjectType *RHS = Rptr->getObjectType(); |
| 4192 | const ObjCInterfaceDecl* LDecl = LHS->getInterface(); |
| 4193 | const ObjCInterfaceDecl* RDecl = RHS->getInterface(); |
| 4194 | if (!LDecl || !RDecl) |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4195 | return QualType(); |
| 4196 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4197 | while ((LDecl = LDecl->getSuperClass())) { |
| 4198 | LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4199 | if (canAssignObjCInterfaces(LHS, RHS)) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4200 | llvm::SmallVector<ObjCProtocolDecl *, 8> Protocols; |
| 4201 | getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols); |
| 4202 | |
| 4203 | QualType Result = QualType(LHS, 0); |
| 4204 | if (!Protocols.empty()) |
| 4205 | Result = getObjCObjectType(Result, Protocols.data(), Protocols.size()); |
| 4206 | Result = getObjCObjectPointerType(Result); |
| 4207 | return Result; |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4208 | } |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4209 | } |
| 4210 | |
| 4211 | return QualType(); |
| 4212 | } |
| 4213 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4214 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS, |
| 4215 | const ObjCObjectType *RHS) { |
| 4216 | assert(LHS->getInterface() && "LHS is not an interface type"); |
| 4217 | assert(RHS->getInterface() && "RHS is not an interface type"); |
| 4218 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4219 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 4220 | // the LHS. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4221 | if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface())) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4222 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4223 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4224 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 4225 | // protocol qualified at all, then we are good. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4226 | if (LHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4227 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4228 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4229 | // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it |
| 4230 | // isn't a superset. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4231 | if (RHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4232 | return true; // FIXME: should return false! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4233 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4234 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 4235 | LHSPE = LHS->qual_end(); |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4236 | LHSPI != LHSPE; LHSPI++) { |
| 4237 | bool RHSImplementsProtocol = false; |
| 4238 | |
| 4239 | // If the RHS doesn't implement the protocol on the left, the types |
| 4240 | // are incompatible. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4241 | for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(), |
| 4242 | RHSPE = RHS->qual_end(); |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 4243 | RHSPI != RHSPE; RHSPI++) { |
| 4244 | if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) { |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4245 | RHSImplementsProtocol = true; |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 4246 | break; |
| 4247 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4248 | } |
| 4249 | // FIXME: For better diagnostics, consider passing back the protocol name. |
| 4250 | if (!RHSImplementsProtocol) |
| 4251 | return false; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4252 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4253 | // The RHS implements all protocols listed on the LHS. |
| 4254 | return true; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4255 | } |
| 4256 | |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4257 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 4258 | // get the "pointed to" types |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4259 | const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
| 4260 | const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4261 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4262 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4263 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4264 | |
| 4265 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 4266 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4267 | } |
| 4268 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4269 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4270 | /// both shall have the identically qualified version of a compatible type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4271 | /// 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] | 4272 | /// same. See 6.7.[2,3,5] for additional rules. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4273 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) { |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 4274 | if (getLangOptions().CPlusPlus) |
| 4275 | return hasSameType(LHS, RHS); |
| 4276 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4277 | return !mergeTypes(LHS, RHS).isNull(); |
| 4278 | } |
| 4279 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4280 | bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) { |
| 4281 | return !mergeTypes(LHS, RHS, true).isNull(); |
| 4282 | } |
| 4283 | |
| 4284 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs, |
| 4285 | bool OfBlockPointer) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4286 | const FunctionType *lbase = lhs->getAs<FunctionType>(); |
| 4287 | const FunctionType *rbase = rhs->getAs<FunctionType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4288 | const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 4289 | const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4290 | bool allLTypes = true; |
| 4291 | bool allRTypes = true; |
| 4292 | |
| 4293 | // Check return type |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4294 | QualType retType; |
| 4295 | if (OfBlockPointer) |
| 4296 | retType = mergeTypes(rbase->getResultType(), lbase->getResultType(), true); |
| 4297 | else |
| 4298 | retType = mergeTypes(lbase->getResultType(), rbase->getResultType()); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4299 | if (retType.isNull()) return QualType(); |
Fariborz Jahanian | 6de8b62 | 2010-02-10 00:32:12 +0000 | [diff] [blame] | 4300 | if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType())) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4301 | allLTypes = false; |
Fariborz Jahanian | 6de8b62 | 2010-02-10 00:32:12 +0000 | [diff] [blame] | 4302 | if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType())) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4303 | allRTypes = false; |
Daniel Dunbar | 6a15c85 | 2010-04-28 16:20:58 +0000 | [diff] [blame] | 4304 | // FIXME: double check this |
| 4305 | // FIXME: should we error if lbase->getRegParmAttr() != 0 && |
| 4306 | // rbase->getRegParmAttr() != 0 && |
| 4307 | // lbase->getRegParmAttr() != rbase->getRegParmAttr()? |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 4308 | FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo(); |
| 4309 | FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo(); |
Daniel Dunbar | 6a15c85 | 2010-04-28 16:20:58 +0000 | [diff] [blame] | 4310 | unsigned RegParm = lbaseInfo.getRegParm() == 0 ? rbaseInfo.getRegParm() : |
| 4311 | lbaseInfo.getRegParm(); |
| 4312 | bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn(); |
| 4313 | if (NoReturn != lbaseInfo.getNoReturn() || |
| 4314 | RegParm != lbaseInfo.getRegParm()) |
| 4315 | allLTypes = false; |
| 4316 | if (NoReturn != rbaseInfo.getNoReturn() || |
| 4317 | RegParm != rbaseInfo.getRegParm()) |
| 4318 | allRTypes = false; |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 4319 | CallingConv lcc = lbaseInfo.getCC(); |
| 4320 | CallingConv rcc = rbaseInfo.getCC(); |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 4321 | // Compatible functions must have compatible calling conventions |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4322 | if (!isSameCallConv(lcc, rcc)) |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 4323 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4324 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4325 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 4326 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 4327 | "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4328 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 4329 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 4330 | |
| 4331 | // Compatible functions must have the same number of arguments |
| 4332 | if (lproto_nargs != rproto_nargs) |
| 4333 | return QualType(); |
| 4334 | |
| 4335 | // Variadic and non-variadic functions aren't compatible |
| 4336 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 4337 | return QualType(); |
| 4338 | |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 4339 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 4340 | return QualType(); |
| 4341 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4342 | // Check argument compatibility |
| 4343 | llvm::SmallVector<QualType, 10> types; |
| 4344 | for (unsigned i = 0; i < lproto_nargs; i++) { |
| 4345 | QualType largtype = lproto->getArgType(i).getUnqualifiedType(); |
| 4346 | QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4347 | QualType argtype = mergeTypes(largtype, rargtype, OfBlockPointer); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4348 | if (argtype.isNull()) return QualType(); |
| 4349 | types.push_back(argtype); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4350 | if (getCanonicalType(argtype) != getCanonicalType(largtype)) |
| 4351 | allLTypes = false; |
| 4352 | if (getCanonicalType(argtype) != getCanonicalType(rargtype)) |
| 4353 | allRTypes = false; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4354 | } |
| 4355 | if (allLTypes) return lhs; |
| 4356 | if (allRTypes) return rhs; |
| 4357 | return getFunctionType(retType, types.begin(), types.size(), |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 4358 | lproto->isVariadic(), lproto->getTypeQuals(), |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 4359 | false, false, 0, 0, |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 4360 | FunctionType::ExtInfo(NoReturn, RegParm, lcc)); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4361 | } |
| 4362 | |
| 4363 | if (lproto) allRTypes = false; |
| 4364 | if (rproto) allLTypes = false; |
| 4365 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4366 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4367 | if (proto) { |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 4368 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4369 | if (proto->isVariadic()) return QualType(); |
| 4370 | // Check that the types are compatible with the types that |
| 4371 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 4372 | // The only types actually affected are promotable integer |
| 4373 | // types and floats, which would be passed as a different |
| 4374 | // type depending on whether the prototype is visible. |
| 4375 | unsigned proto_nargs = proto->getNumArgs(); |
| 4376 | for (unsigned i = 0; i < proto_nargs; ++i) { |
| 4377 | QualType argTy = proto->getArgType(i); |
Douglas Gregor | b0f8eac | 2010-02-03 19:27:29 +0000 | [diff] [blame] | 4378 | |
| 4379 | // Look at the promotion type of enum types, since that is the type used |
| 4380 | // to pass enum values. |
| 4381 | if (const EnumType *Enum = argTy->getAs<EnumType>()) |
| 4382 | argTy = Enum->getDecl()->getPromotionType(); |
| 4383 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4384 | if (argTy->isPromotableIntegerType() || |
| 4385 | getCanonicalType(argTy).getUnqualifiedType() == FloatTy) |
| 4386 | return QualType(); |
| 4387 | } |
| 4388 | |
| 4389 | if (allLTypes) return lhs; |
| 4390 | if (allRTypes) return rhs; |
| 4391 | return getFunctionType(retType, proto->arg_type_begin(), |
Mike Stump | 2d3c191 | 2009-07-27 00:44:23 +0000 | [diff] [blame] | 4392 | proto->getNumArgs(), proto->isVariadic(), |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 4393 | proto->getTypeQuals(), |
| 4394 | false, false, 0, 0, |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 4395 | FunctionType::ExtInfo(NoReturn, RegParm, lcc)); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4396 | } |
| 4397 | |
| 4398 | if (allLTypes) return lhs; |
| 4399 | if (allRTypes) return rhs; |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 4400 | FunctionType::ExtInfo Info(NoReturn, RegParm, lcc); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 4401 | return getFunctionNoProtoType(retType, Info); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4402 | } |
| 4403 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4404 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, |
| 4405 | bool OfBlockPointer) { |
Bill Wendling | 43d6975 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 4406 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 4407 | // type is adjusted to "T" prior to any further analysis, the expression |
| 4408 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4409 | // expression is an lvalue unless the reference is an rvalue reference and |
| 4410 | // the expression is a function call (possibly inside parentheses). |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 4411 | assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?"); |
| 4412 | assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?"); |
| 4413 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4414 | QualType LHSCan = getCanonicalType(LHS), |
| 4415 | RHSCan = getCanonicalType(RHS); |
| 4416 | |
| 4417 | // If two types are identical, they are compatible. |
| 4418 | if (LHSCan == RHSCan) |
| 4419 | return LHS; |
| 4420 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4421 | // If the qualifiers are different, the types aren't compatible... mostly. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4422 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 4423 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4424 | if (LQuals != RQuals) { |
| 4425 | // If any of these qualifiers are different, we have a type |
| 4426 | // mismatch. |
| 4427 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 4428 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 4429 | return QualType(); |
| 4430 | |
| 4431 | // Exactly one GC qualifier difference is allowed: __strong is |
| 4432 | // okay if the other type has no GC qualifier but is an Objective |
| 4433 | // C object pointer (i.e. implicitly strong by default). We fix |
| 4434 | // this by pretending that the unqualified type was actually |
| 4435 | // qualified __strong. |
| 4436 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 4437 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 4438 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 4439 | |
| 4440 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 4441 | return QualType(); |
| 4442 | |
| 4443 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
| 4444 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); |
| 4445 | } |
| 4446 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
| 4447 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); |
| 4448 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4449 | return QualType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4450 | } |
| 4451 | |
| 4452 | // Okay, qualifiers are equal. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4453 | |
Eli Friedman | 852d63b | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 4454 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 4455 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4456 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4457 | // We want to consider the two function types to be the same for these |
| 4458 | // comparisons, just force one to the other. |
| 4459 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 4460 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 4c721d3 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 4461 | |
| 4462 | // Same as above for arrays |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 4463 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 4464 | LHSClass = Type::ConstantArray; |
| 4465 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 4466 | RHSClass = Type::ConstantArray; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4467 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4468 | // ObjCInterfaces are just specialized ObjCObjects. |
| 4469 | if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject; |
| 4470 | if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject; |
| 4471 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 4472 | // Canonicalize ExtVector -> Vector. |
| 4473 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 4474 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4475 | |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 4476 | // If the canonical type classes don't match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4477 | if (LHSClass != RHSClass) { |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4478 | // 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] | 4479 | // a signed integer type, or an unsigned integer type. |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 4480 | // Compatibility is based on the underlying type, not the promotion |
| 4481 | // type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4482 | if (const EnumType* ETy = LHS->getAs<EnumType>()) { |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4483 | if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType()) |
| 4484 | return RHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 4485 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4486 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4487 | if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType()) |
| 4488 | return LHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 4489 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4490 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4491 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4492 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4493 | |
Steve Naroff | 4a74678 | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 4494 | // The canonical type classes match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4495 | switch (LHSClass) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4496 | #define TYPE(Class, Base) |
| 4497 | #define ABSTRACT_TYPE(Class, Base) |
John McCall | ad5e738 | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 4498 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4499 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 4500 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 4501 | #include "clang/AST/TypeNodes.def" |
| 4502 | assert(false && "Non-canonical and dependent types shouldn't get here"); |
| 4503 | return QualType(); |
| 4504 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4505 | case Type::LValueReference: |
| 4506 | case Type::RValueReference: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4507 | case Type::MemberPointer: |
| 4508 | assert(false && "C++ should never be in mergeTypes"); |
| 4509 | return QualType(); |
| 4510 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4511 | case Type::ObjCInterface: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4512 | case Type::IncompleteArray: |
| 4513 | case Type::VariableArray: |
| 4514 | case Type::FunctionProto: |
| 4515 | case Type::ExtVector: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4516 | assert(false && "Types are eliminated above"); |
| 4517 | return QualType(); |
| 4518 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4519 | case Type::Pointer: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4520 | { |
| 4521 | // Merge two pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4522 | QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); |
| 4523 | QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4524 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee); |
| 4525 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 4526 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4527 | return LHS; |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 4528 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4529 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4530 | return getPointerType(ResultType); |
| 4531 | } |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 4532 | case Type::BlockPointer: |
| 4533 | { |
| 4534 | // Merge two block pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4535 | QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); |
| 4536 | QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4537 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer); |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 4538 | if (ResultType.isNull()) return QualType(); |
| 4539 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 4540 | return LHS; |
| 4541 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 4542 | return RHS; |
| 4543 | return getBlockPointerType(ResultType); |
| 4544 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4545 | case Type::ConstantArray: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4546 | { |
| 4547 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 4548 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 4549 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 4550 | return QualType(); |
| 4551 | |
| 4552 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 4553 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
| 4554 | QualType ResultType = mergeTypes(LHSElem, RHSElem); |
| 4555 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4556 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 4557 | return LHS; |
| 4558 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 4559 | return RHS; |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 4560 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 4561 | ArrayType::ArraySizeModifier(), 0); |
| 4562 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 4563 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4564 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 4565 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4566 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 4567 | return LHS; |
| 4568 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 4569 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4570 | if (LVAT) { |
| 4571 | // FIXME: This isn't correct! But tricky to implement because |
| 4572 | // the array's size has to be the size of LHS, but the type |
| 4573 | // has to be different. |
| 4574 | return LHS; |
| 4575 | } |
| 4576 | if (RVAT) { |
| 4577 | // FIXME: This isn't correct! But tricky to implement because |
| 4578 | // the array's size has to be the size of RHS, but the type |
| 4579 | // has to be different. |
| 4580 | return RHS; |
| 4581 | } |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 4582 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 4583 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4584 | return getIncompleteArrayType(ResultType, |
| 4585 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4586 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4587 | case Type::FunctionNoProto: |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4588 | return mergeFunctionTypes(LHS, RHS, OfBlockPointer); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4589 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4590 | case Type::Enum: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4591 | return QualType(); |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4592 | case Type::Builtin: |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 4593 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4594 | return QualType(); |
Daniel Dunbar | 64cfdb7 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 4595 | case Type::Complex: |
| 4596 | // Distinct complex types are incompatible. |
| 4597 | return QualType(); |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 4598 | case Type::Vector: |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 4599 | // FIXME: The merged type should be an ExtVector! |
John McCall | 1c471f3 | 2010-03-12 23:14:13 +0000 | [diff] [blame] | 4600 | if (areCompatVectorTypes(LHSCan->getAs<VectorType>(), |
| 4601 | RHSCan->getAs<VectorType>())) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4602 | return LHS; |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4603 | return QualType(); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4604 | case Type::ObjCObject: { |
| 4605 | // Check if the types are assignment compatible. |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 4606 | // FIXME: This should be type compatibility, e.g. whether |
| 4607 | // "LHS x; RHS x;" at global scope is legal. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4608 | const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>(); |
| 4609 | const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>(); |
| 4610 | if (canAssignObjCInterfaces(LHSIface, RHSIface)) |
Steve Naroff | 5fd659d | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 4611 | return LHS; |
| 4612 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4613 | return QualType(); |
Cedric Venet | 61490e9 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 4614 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4615 | case Type::ObjCObjectPointer: { |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4616 | if (OfBlockPointer) { |
| 4617 | if (canAssignObjCInterfacesInBlockPointer( |
| 4618 | LHS->getAs<ObjCObjectPointerType>(), |
| 4619 | RHS->getAs<ObjCObjectPointerType>())) |
| 4620 | return LHS; |
| 4621 | return QualType(); |
| 4622 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4623 | if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(), |
| 4624 | RHS->getAs<ObjCObjectPointerType>())) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4625 | return LHS; |
| 4626 | |
Steve Naroff | bc76dd0 | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 4627 | return QualType(); |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4628 | } |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4629 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4630 | |
| 4631 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4632 | } |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 4633 | |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 4634 | /// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and |
| 4635 | /// 'RHS' attributes and returns the merged version; including for function |
| 4636 | /// return types. |
| 4637 | QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) { |
| 4638 | QualType LHSCan = getCanonicalType(LHS), |
| 4639 | RHSCan = getCanonicalType(RHS); |
| 4640 | // If two types are identical, they are compatible. |
| 4641 | if (LHSCan == RHSCan) |
| 4642 | return LHS; |
| 4643 | if (RHSCan->isFunctionType()) { |
| 4644 | if (!LHSCan->isFunctionType()) |
| 4645 | return QualType(); |
| 4646 | QualType OldReturnType = |
| 4647 | cast<FunctionType>(RHSCan.getTypePtr())->getResultType(); |
| 4648 | QualType NewReturnType = |
| 4649 | cast<FunctionType>(LHSCan.getTypePtr())->getResultType(); |
| 4650 | QualType ResReturnType = |
| 4651 | mergeObjCGCQualifiers(NewReturnType, OldReturnType); |
| 4652 | if (ResReturnType.isNull()) |
| 4653 | return QualType(); |
| 4654 | if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) { |
| 4655 | // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo(); |
| 4656 | // In either case, use OldReturnType to build the new function type. |
| 4657 | const FunctionType *F = LHS->getAs<FunctionType>(); |
| 4658 | if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) { |
| 4659 | FunctionType::ExtInfo Info = getFunctionExtInfo(LHS); |
| 4660 | QualType ResultType |
| 4661 | = getFunctionType(OldReturnType, FPT->arg_type_begin(), |
| 4662 | FPT->getNumArgs(), FPT->isVariadic(), |
| 4663 | FPT->getTypeQuals(), |
| 4664 | FPT->hasExceptionSpec(), |
| 4665 | FPT->hasAnyExceptionSpec(), |
| 4666 | FPT->getNumExceptions(), |
| 4667 | FPT->exception_begin(), |
| 4668 | Info); |
| 4669 | return ResultType; |
| 4670 | } |
| 4671 | } |
| 4672 | return QualType(); |
| 4673 | } |
| 4674 | |
| 4675 | // If the qualifiers are different, the types can still be merged. |
| 4676 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 4677 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
| 4678 | if (LQuals != RQuals) { |
| 4679 | // If any of these qualifiers are different, we have a type mismatch. |
| 4680 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 4681 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 4682 | return QualType(); |
| 4683 | |
| 4684 | // Exactly one GC qualifier difference is allowed: __strong is |
| 4685 | // okay if the other type has no GC qualifier but is an Objective |
| 4686 | // C object pointer (i.e. implicitly strong by default). We fix |
| 4687 | // this by pretending that the unqualified type was actually |
| 4688 | // qualified __strong. |
| 4689 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 4690 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 4691 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 4692 | |
| 4693 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 4694 | return QualType(); |
| 4695 | |
| 4696 | if (GC_L == Qualifiers::Strong) |
| 4697 | return LHS; |
| 4698 | if (GC_R == Qualifiers::Strong) |
| 4699 | return RHS; |
| 4700 | return QualType(); |
| 4701 | } |
| 4702 | |
| 4703 | if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) { |
| 4704 | QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4705 | QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4706 | QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT); |
| 4707 | if (ResQT == LHSBaseQT) |
| 4708 | return LHS; |
| 4709 | if (ResQT == RHSBaseQT) |
| 4710 | return RHS; |
| 4711 | } |
| 4712 | return QualType(); |
| 4713 | } |
| 4714 | |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 4715 | //===----------------------------------------------------------------------===// |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4716 | // Integer Predicates |
| 4717 | //===----------------------------------------------------------------------===// |
Chris Lattner | 88054de | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 4718 | |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4719 | unsigned ASTContext::getIntWidth(QualType T) { |
Sebastian Redl | 632d772 | 2009-11-05 21:10:57 +0000 | [diff] [blame] | 4720 | if (T->isBooleanType()) |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4721 | return 1; |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 4722 | if (EnumType *ET = dyn_cast<EnumType>(T)) |
Eli Friedman | 29a7f33 | 2009-12-10 22:29:29 +0000 | [diff] [blame] | 4723 | T = ET->getDecl()->getIntegerType(); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4724 | // For builtin types, just use the standard type sizing method |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4725 | return (unsigned)getTypeSize(T); |
| 4726 | } |
| 4727 | |
| 4728 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) { |
| 4729 | assert(T->isSignedIntegerType() && "Unexpected type"); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4730 | |
| 4731 | // Turn <4 x signed int> -> <4 x unsigned int> |
| 4732 | if (const VectorType *VTy = T->getAs<VectorType>()) |
| 4733 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4734 | VTy->getNumElements(), VTy->isAltiVec(), VTy->isPixel()); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4735 | |
| 4736 | // For enums, we return the unsigned version of the base type. |
| 4737 | if (const EnumType *ETy = T->getAs<EnumType>()) |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4738 | T = ETy->getDecl()->getIntegerType(); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4739 | |
| 4740 | const BuiltinType *BTy = T->getAs<BuiltinType>(); |
| 4741 | assert(BTy && "Unexpected signed integer type"); |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4742 | switch (BTy->getKind()) { |
| 4743 | case BuiltinType::Char_S: |
| 4744 | case BuiltinType::SChar: |
| 4745 | return UnsignedCharTy; |
| 4746 | case BuiltinType::Short: |
| 4747 | return UnsignedShortTy; |
| 4748 | case BuiltinType::Int: |
| 4749 | return UnsignedIntTy; |
| 4750 | case BuiltinType::Long: |
| 4751 | return UnsignedLongTy; |
| 4752 | case BuiltinType::LongLong: |
| 4753 | return UnsignedLongLongTy; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 4754 | case BuiltinType::Int128: |
| 4755 | return UnsignedInt128Ty; |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4756 | default: |
| 4757 | assert(0 && "Unexpected signed integer type"); |
| 4758 | return QualType(); |
| 4759 | } |
| 4760 | } |
| 4761 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 4762 | ExternalASTSource::~ExternalASTSource() { } |
| 4763 | |
| 4764 | void ExternalASTSource::PrintStats() { } |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4765 | |
| 4766 | |
| 4767 | //===----------------------------------------------------------------------===// |
| 4768 | // Builtin Type Computation |
| 4769 | //===----------------------------------------------------------------------===// |
| 4770 | |
| 4771 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
| 4772 | /// pointer over the consumed characters. This returns the resultant type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4773 | static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context, |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4774 | ASTContext::GetBuiltinTypeError &Error, |
| 4775 | bool AllowTypeModifiers = true) { |
| 4776 | // Modifiers. |
| 4777 | int HowLong = 0; |
| 4778 | bool Signed = false, Unsigned = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4779 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4780 | // Read the modifiers first. |
| 4781 | bool Done = false; |
| 4782 | while (!Done) { |
| 4783 | switch (*Str++) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4784 | default: Done = true; --Str; break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4785 | case 'S': |
| 4786 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 4787 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 4788 | Signed = true; |
| 4789 | break; |
| 4790 | case 'U': |
| 4791 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
| 4792 | assert(!Unsigned && "Can't use 'S' modifier multiple times!"); |
| 4793 | Unsigned = true; |
| 4794 | break; |
| 4795 | case 'L': |
| 4796 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 4797 | ++HowLong; |
| 4798 | break; |
| 4799 | } |
| 4800 | } |
| 4801 | |
| 4802 | QualType Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4803 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4804 | // Read the base type. |
| 4805 | switch (*Str++) { |
| 4806 | default: assert(0 && "Unknown builtin type letter!"); |
| 4807 | case 'v': |
| 4808 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 4809 | "Bad modifiers used with 'v'!"); |
| 4810 | Type = Context.VoidTy; |
| 4811 | break; |
| 4812 | case 'f': |
| 4813 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 4814 | "Bad modifiers used with 'f'!"); |
| 4815 | Type = Context.FloatTy; |
| 4816 | break; |
| 4817 | case 'd': |
| 4818 | assert(HowLong < 2 && !Signed && !Unsigned && |
| 4819 | "Bad modifiers used with 'd'!"); |
| 4820 | if (HowLong) |
| 4821 | Type = Context.LongDoubleTy; |
| 4822 | else |
| 4823 | Type = Context.DoubleTy; |
| 4824 | break; |
| 4825 | case 's': |
| 4826 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 4827 | if (Unsigned) |
| 4828 | Type = Context.UnsignedShortTy; |
| 4829 | else |
| 4830 | Type = Context.ShortTy; |
| 4831 | break; |
| 4832 | case 'i': |
| 4833 | if (HowLong == 3) |
| 4834 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 4835 | else if (HowLong == 2) |
| 4836 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 4837 | else if (HowLong == 1) |
| 4838 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 4839 | else |
| 4840 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 4841 | break; |
| 4842 | case 'c': |
| 4843 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 4844 | if (Signed) |
| 4845 | Type = Context.SignedCharTy; |
| 4846 | else if (Unsigned) |
| 4847 | Type = Context.UnsignedCharTy; |
| 4848 | else |
| 4849 | Type = Context.CharTy; |
| 4850 | break; |
| 4851 | case 'b': // boolean |
| 4852 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 4853 | Type = Context.BoolTy; |
| 4854 | break; |
| 4855 | case 'z': // size_t. |
| 4856 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 4857 | Type = Context.getSizeType(); |
| 4858 | break; |
| 4859 | case 'F': |
| 4860 | Type = Context.getCFConstantStringType(); |
| 4861 | break; |
| 4862 | case 'a': |
| 4863 | Type = Context.getBuiltinVaListType(); |
| 4864 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 4865 | break; |
| 4866 | case 'A': |
| 4867 | // This is a "reference" to a va_list; however, what exactly |
| 4868 | // this means depends on how va_list is defined. There are two |
| 4869 | // different kinds of va_list: ones passed by value, and ones |
| 4870 | // passed by reference. An example of a by-value va_list is |
| 4871 | // x86, where va_list is a char*. An example of by-ref va_list |
| 4872 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 4873 | // we want this argument to be a char*&; for x86-64, we want |
| 4874 | // it to be a __va_list_tag*. |
| 4875 | Type = Context.getBuiltinVaListType(); |
| 4876 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 4877 | if (Type->isArrayType()) { |
| 4878 | Type = Context.getArrayDecayedType(Type); |
| 4879 | } else { |
| 4880 | Type = Context.getLValueReferenceType(Type); |
| 4881 | } |
| 4882 | break; |
| 4883 | case 'V': { |
| 4884 | char *End; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4885 | unsigned NumElements = strtoul(Str, &End, 10); |
| 4886 | assert(End != Str && "Missing vector size"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4887 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4888 | Str = End; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4889 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4890 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false); |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4891 | // FIXME: Don't know what to do about AltiVec. |
| 4892 | Type = Context.getVectorType(ElementType, NumElements, false, false); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4893 | break; |
| 4894 | } |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 4895 | case 'X': { |
| 4896 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false); |
| 4897 | Type = Context.getComplexType(ElementType); |
| 4898 | break; |
| 4899 | } |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 4900 | case 'P': |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 4901 | Type = Context.getFILEType(); |
| 4902 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 4903 | Error = ASTContext::GE_Missing_stdio; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4904 | return QualType(); |
| 4905 | } |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 4906 | break; |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 4907 | case 'J': |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 4908 | if (Signed) |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 4909 | Type = Context.getsigjmp_bufType(); |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 4910 | else |
| 4911 | Type = Context.getjmp_bufType(); |
| 4912 | |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 4913 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 4914 | Error = ASTContext::GE_Missing_setjmp; |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 4915 | return QualType(); |
| 4916 | } |
| 4917 | break; |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 4918 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4919 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4920 | if (!AllowTypeModifiers) |
| 4921 | return Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4922 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4923 | Done = false; |
| 4924 | while (!Done) { |
John McCall | 187ab37 | 2010-03-12 04:21:28 +0000 | [diff] [blame] | 4925 | switch (char c = *Str++) { |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4926 | default: Done = true; --Str; break; |
| 4927 | case '*': |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4928 | case '&': |
John McCall | 187ab37 | 2010-03-12 04:21:28 +0000 | [diff] [blame] | 4929 | { |
| 4930 | // Both pointers and references can have their pointee types |
| 4931 | // qualified with an address space. |
| 4932 | char *End; |
| 4933 | unsigned AddrSpace = strtoul(Str, &End, 10); |
| 4934 | if (End != Str && AddrSpace != 0) { |
| 4935 | Type = Context.getAddrSpaceQualType(Type, AddrSpace); |
| 4936 | Str = End; |
| 4937 | } |
| 4938 | } |
| 4939 | if (c == '*') |
| 4940 | Type = Context.getPointerType(Type); |
| 4941 | else |
| 4942 | Type = Context.getLValueReferenceType(Type); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4943 | break; |
| 4944 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 4945 | case 'C': |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4946 | Type = Type.withConst(); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4947 | break; |
Fariborz Jahanian | 013af39 | 2010-01-26 22:48:42 +0000 | [diff] [blame] | 4948 | case 'D': |
| 4949 | Type = Context.getVolatileType(Type); |
| 4950 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4951 | } |
| 4952 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4953 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4954 | return Type; |
| 4955 | } |
| 4956 | |
| 4957 | /// GetBuiltinType - Return the type for the specified builtin. |
| 4958 | QualType ASTContext::GetBuiltinType(unsigned id, |
| 4959 | GetBuiltinTypeError &Error) { |
| 4960 | const char *TypeStr = BuiltinInfo.GetTypeString(id); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4961 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4962 | llvm::SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4963 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4964 | Error = GE_None; |
| 4965 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error); |
| 4966 | if (Error != GE_None) |
| 4967 | return QualType(); |
| 4968 | while (TypeStr[0] && TypeStr[0] != '.') { |
| 4969 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error); |
| 4970 | if (Error != GE_None) |
| 4971 | return QualType(); |
| 4972 | |
| 4973 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 4974 | if (Ty->isArrayType()) |
| 4975 | Ty = getArrayDecayedType(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4976 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4977 | ArgTypes.push_back(Ty); |
| 4978 | } |
| 4979 | |
| 4980 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 4981 | "'.' should only occur at end of builtin type list!"); |
| 4982 | |
| 4983 | // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);". |
| 4984 | if (ArgTypes.size() == 0 && TypeStr[0] == '.') |
| 4985 | return getFunctionNoProtoType(ResType); |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 4986 | |
| 4987 | // FIXME: Should we create noreturn types? |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4988 | return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 4989 | TypeStr[0] == '.', 0, false, false, 0, 0, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 4990 | FunctionType::ExtInfo()); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4991 | } |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4992 | |
| 4993 | QualType |
| 4994 | ASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) { |
| 4995 | // Perform the usual unary conversions. We do this early so that |
| 4996 | // integral promotions to "int" can allow us to exit early, in the |
| 4997 | // lhs == rhs check. Also, for conversion purposes, we ignore any |
| 4998 | // qualifiers. For example, "const float" and "float" are |
| 4999 | // equivalent. |
| 5000 | if (lhs->isPromotableIntegerType()) |
| 5001 | lhs = getPromotedIntegerType(lhs); |
| 5002 | else |
| 5003 | lhs = lhs.getUnqualifiedType(); |
| 5004 | if (rhs->isPromotableIntegerType()) |
| 5005 | rhs = getPromotedIntegerType(rhs); |
| 5006 | else |
| 5007 | rhs = rhs.getUnqualifiedType(); |
| 5008 | |
| 5009 | // If both types are identical, no conversion is needed. |
| 5010 | if (lhs == rhs) |
| 5011 | return lhs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5012 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5013 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 5014 | // The caller can deal with this (e.g. pointer + int). |
| 5015 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 5016 | return lhs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5017 | |
| 5018 | // At this point, we have two different arithmetic types. |
| 5019 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5020 | // Handle complex types first (C99 6.3.1.8p1). |
| 5021 | if (lhs->isComplexType() || rhs->isComplexType()) { |
| 5022 | // if we have an integer operand, the result is the complex type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5023 | if (rhs->isIntegerType() || rhs->isComplexIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5024 | // convert the rhs to the lhs complex type. |
| 5025 | return lhs; |
| 5026 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5027 | if (lhs->isIntegerType() || lhs->isComplexIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5028 | // convert the lhs to the rhs complex type. |
| 5029 | return rhs; |
| 5030 | } |
| 5031 | // This handles complex/complex, complex/float, or float/complex. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5032 | // When both operands are complex, the shorter operand is converted to the |
| 5033 | // type of the longer, and that is the type of the result. This corresponds |
| 5034 | // to what is done when combining two real floating-point operands. |
| 5035 | // The fun begins when size promotion occur across type domains. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5036 | // From H&S 6.3.4: When one operand is complex and the other is a real |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5037 | // floating-point type, the less precise type is converted, within it's |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5038 | // real or complex domain, to the precision of the other type. For example, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5039 | // when combining a "long double" with a "double _Complex", the |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5040 | // "double _Complex" is promoted to "long double _Complex". |
| 5041 | int result = getFloatingTypeOrder(lhs, rhs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5042 | |
| 5043 | if (result > 0) { // The left side is bigger, convert rhs. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5044 | rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5045 | } else if (result < 0) { // The right side is bigger, convert lhs. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5046 | lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5047 | } |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5048 | // At this point, lhs and rhs have the same rank/size. Now, make sure the |
| 5049 | // domains match. This is a requirement for our implementation, C99 |
| 5050 | // does not require this promotion. |
| 5051 | if (lhs != rhs) { // Domains don't match, we have complex/float mix. |
| 5052 | if (lhs->isRealFloatingType()) { // handle "double, _Complex double". |
| 5053 | return rhs; |
| 5054 | } else { // handle "_Complex double, double". |
| 5055 | return lhs; |
| 5056 | } |
| 5057 | } |
| 5058 | return lhs; // The domain/size match exactly. |
| 5059 | } |
| 5060 | // Now handle "real" floating types (i.e. float, double, long double). |
| 5061 | if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) { |
| 5062 | // if we have an integer operand, the result is the real floating type. |
| 5063 | if (rhs->isIntegerType()) { |
| 5064 | // convert rhs to the lhs floating point type. |
| 5065 | return lhs; |
| 5066 | } |
| 5067 | if (rhs->isComplexIntegerType()) { |
| 5068 | // convert rhs to the complex floating point type. |
| 5069 | return getComplexType(lhs); |
| 5070 | } |
| 5071 | if (lhs->isIntegerType()) { |
| 5072 | // convert lhs to the rhs floating point type. |
| 5073 | return rhs; |
| 5074 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5075 | if (lhs->isComplexIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5076 | // convert lhs to the complex floating point type. |
| 5077 | return getComplexType(rhs); |
| 5078 | } |
| 5079 | // We have two real floating types, float/complex combos were handled above. |
| 5080 | // Convert the smaller operand to the bigger result. |
| 5081 | int result = getFloatingTypeOrder(lhs, rhs); |
| 5082 | if (result > 0) // convert the rhs |
| 5083 | return lhs; |
| 5084 | assert(result < 0 && "illegal float comparison"); |
| 5085 | return rhs; // convert the lhs |
| 5086 | } |
| 5087 | if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) { |
| 5088 | // Handle GCC complex int extension. |
| 5089 | const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType(); |
| 5090 | const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType(); |
| 5091 | |
| 5092 | if (lhsComplexInt && rhsComplexInt) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5093 | if (getIntegerTypeOrder(lhsComplexInt->getElementType(), |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5094 | rhsComplexInt->getElementType()) >= 0) |
| 5095 | return lhs; // convert the rhs |
| 5096 | return rhs; |
| 5097 | } else if (lhsComplexInt && rhs->isIntegerType()) { |
| 5098 | // convert the rhs to the lhs complex type. |
| 5099 | return lhs; |
| 5100 | } else if (rhsComplexInt && lhs->isIntegerType()) { |
| 5101 | // convert the lhs to the rhs complex type. |
| 5102 | return rhs; |
| 5103 | } |
| 5104 | } |
| 5105 | // Finally, we have two differing integer types. |
| 5106 | // The rules for this case are in C99 6.3.1.8 |
| 5107 | int compare = getIntegerTypeOrder(lhs, rhs); |
| 5108 | bool lhsSigned = lhs->isSignedIntegerType(), |
| 5109 | rhsSigned = rhs->isSignedIntegerType(); |
| 5110 | QualType destType; |
| 5111 | if (lhsSigned == rhsSigned) { |
| 5112 | // Same signedness; use the higher-ranked type |
| 5113 | destType = compare >= 0 ? lhs : rhs; |
| 5114 | } else if (compare != (lhsSigned ? 1 : -1)) { |
| 5115 | // The unsigned type has greater than or equal rank to the |
| 5116 | // signed type, so use the unsigned type |
| 5117 | destType = lhsSigned ? rhs : lhs; |
| 5118 | } else if (getIntWidth(lhs) != getIntWidth(rhs)) { |
| 5119 | // The two types are different widths; if we are here, that |
| 5120 | // means the signed type is larger than the unsigned type, so |
| 5121 | // use the signed type. |
| 5122 | destType = lhsSigned ? lhs : rhs; |
| 5123 | } else { |
| 5124 | // The signed type is higher-ranked than the unsigned type, |
| 5125 | // but isn't actually any bigger (like unsigned int and long |
| 5126 | // on most 32-bit systems). Use the unsigned type corresponding |
| 5127 | // to the signed type. |
| 5128 | destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs); |
| 5129 | } |
| 5130 | return destType; |
| 5131 | } |