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" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 21 | #include "clang/AST/ExternalASTSource.h" |
Anders Carlsson | 19cc4ab | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 22 | #include "clang/AST/RecordLayout.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 23 | #include "clang/Basic/Builtins.h" |
Chris Lattner | a9376d4 | 2009-03-28 03:45:20 +0000 | [diff] [blame] | 24 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 25 | #include "clang/Basic/TargetInfo.h" |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallString.h" |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/StringExtras.h" |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 28 | #include "llvm/Support/MathExtras.h" |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 29 | #include "llvm/Support/raw_ostream.h" |
Anders Carlsson | 29445a0 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 30 | #include "RecordLayoutBuilder.h" |
| 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), |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 44 | ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0), |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 45 | sigjmp_bufDecl(0), BlockDescriptorType(0), BlockDescriptorExtendedType(0), |
Douglas Gregor | c6fbbed | 2010-03-19 22:13:20 +0000 | [diff] [blame] | 46 | SourceMgr(SM), LangOpts(LOpts), FreeMemory(FreeMem), Target(t), |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 47 | Idents(idents), Selectors(sels), |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 48 | BuiltinInfo(builtins), ExternalSource(0), PrintingPolicy(LOpts), |
| 49 | LastSDM(0, 0) { |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 50 | ObjCIdRedefinitionType = QualType(); |
| 51 | ObjCClassRedefinitionType = QualType(); |
Fariborz Jahanian | 369a3bd | 2009-11-25 23:07:42 +0000 | [diff] [blame] | 52 | ObjCSelRedefinitionType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 53 | if (size_reserve > 0) Types.reserve(size_reserve); |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 54 | TUDecl = TranslationUnitDecl::Create(*this); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 55 | InitBuiltinTypes(); |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 58 | ASTContext::~ASTContext() { |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 59 | // Release the DenseMaps associated with DeclContext objects. |
| 60 | // FIXME: Is this the ideal solution? |
| 61 | ReleaseDeclContextMaps(); |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 62 | |
| 63 | // Release all of the memory associated with overridden C++ methods. |
| 64 | for (llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::iterator |
| 65 | OM = OverriddenMethods.begin(), OMEnd = OverriddenMethods.end(); |
| 66 | OM != OMEnd; ++OM) |
| 67 | OM->second.Destroy(); |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 68 | |
Ted Kremenek | bbfd68d | 2009-12-23 21:13:52 +0000 | [diff] [blame] | 69 | if (FreeMemory) { |
| 70 | // Deallocate all the types. |
| 71 | while (!Types.empty()) { |
| 72 | Types.back()->Destroy(*this); |
| 73 | Types.pop_back(); |
| 74 | } |
Eli Friedman | b26153c | 2008-05-27 03:08:09 +0000 | [diff] [blame] | 75 | |
Ted Kremenek | bbfd68d | 2009-12-23 21:13:52 +0000 | [diff] [blame] | 76 | for (llvm::FoldingSet<ExtQuals>::iterator |
| 77 | I = ExtQualNodes.begin(), E = ExtQualNodes.end(); I != E; ) { |
| 78 | // Increment in loop to prevent using deallocated memory. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 79 | Deallocate(&*I++); |
Nuno Lopes | b74668e | 2008-12-17 22:30:25 +0000 | [diff] [blame] | 80 | } |
Nuno Lopes | b74668e | 2008-12-17 22:30:25 +0000 | [diff] [blame] | 81 | |
Ted Kremenek | 503524a | 2010-03-08 20:56:29 +0000 | [diff] [blame] | 82 | for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator |
| 83 | I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) { |
| 84 | // Increment in loop to prevent using deallocated memory. |
| 85 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 86 | R->Destroy(*this); |
| 87 | } |
Ted Kremenek | bbfd68d | 2009-12-23 21:13:52 +0000 | [diff] [blame] | 88 | |
Ted Kremenek | 503524a | 2010-03-08 20:56:29 +0000 | [diff] [blame] | 89 | for (llvm::DenseMap<const ObjCContainerDecl*, |
| 90 | const ASTRecordLayout*>::iterator |
| 91 | I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) { |
| 92 | // Increment in loop to prevent using deallocated memory. |
| 93 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 94 | R->Destroy(*this); |
| 95 | } |
Nuno Lopes | b74668e | 2008-12-17 22:30:25 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 98 | // Destroy nested-name-specifiers. |
Douglas Gregor | 1ae0afa | 2009-03-27 23:54:10 +0000 | [diff] [blame] | 99 | for (llvm::FoldingSet<NestedNameSpecifier>::iterator |
| 100 | NNS = NestedNameSpecifiers.begin(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 101 | NNSEnd = NestedNameSpecifiers.end(); |
Ted Kremenek | bbfd68d | 2009-12-23 21:13:52 +0000 | [diff] [blame] | 102 | NNS != NNSEnd; ) { |
| 103 | // Increment in loop to prevent using deallocated memory. |
Douglas Gregor | 1ae0afa | 2009-03-27 23:54:10 +0000 | [diff] [blame] | 104 | (*NNS++).Destroy(*this); |
Ted Kremenek | bbfd68d | 2009-12-23 21:13:52 +0000 | [diff] [blame] | 105 | } |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 106 | |
| 107 | if (GlobalNestedNameSpecifier) |
| 108 | GlobalNestedNameSpecifier->Destroy(*this); |
| 109 | |
Eli Friedman | b26153c | 2008-05-27 03:08:09 +0000 | [diff] [blame] | 110 | TUDecl->Destroy(*this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 113 | void |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 114 | ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) { |
| 115 | ExternalSource.reset(Source.take()); |
| 116 | } |
| 117 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 118 | void ASTContext::PrintStats() const { |
| 119 | fprintf(stderr, "*** AST Context Stats:\n"); |
| 120 | fprintf(stderr, " %d types total.\n", (int)Types.size()); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 121 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 122 | unsigned counts[] = { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 123 | #define TYPE(Name, Parent) 0, |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 124 | #define ABSTRACT_TYPE(Name, Parent) |
| 125 | #include "clang/AST/TypeNodes.def" |
| 126 | 0 // Extra |
| 127 | }; |
Douglas Gregor | c2ee10d | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 128 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 129 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 130 | Type *T = Types[i]; |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 131 | counts[(unsigned)T->getTypeClass()]++; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 134 | unsigned Idx = 0; |
| 135 | unsigned TotalBytes = 0; |
| 136 | #define TYPE(Name, Parent) \ |
| 137 | if (counts[Idx]) \ |
| 138 | fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \ |
| 139 | TotalBytes += counts[Idx] * sizeof(Name##Type); \ |
| 140 | ++Idx; |
| 141 | #define ABSTRACT_TYPE(Name, Parent) |
| 142 | #include "clang/AST/TypeNodes.def" |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 143 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 144 | fprintf(stderr, "Total bytes = %d\n", int(TotalBytes)); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 145 | |
| 146 | if (ExternalSource.get()) { |
| 147 | fprintf(stderr, "\n"); |
| 148 | ExternalSource->PrintStats(); |
| 149 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 153 | void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 154 | BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 155 | R = CanQualType::CreateUnsafe(QualType(Ty, 0)); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 156 | Types.push_back(Ty); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 159 | void ASTContext::InitBuiltinTypes() { |
| 160 | assert(VoidTy.isNull() && "Context reinitialized?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 161 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 162 | // C99 6.2.5p19. |
| 163 | InitBuiltinType(VoidTy, BuiltinType::Void); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 164 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 165 | // C99 6.2.5p2. |
| 166 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
| 167 | // C99 6.2.5p3. |
Eli Friedman | 15b9176 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 168 | if (LangOpts.CharIsSigned) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 169 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 170 | else |
| 171 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
| 172 | // C99 6.2.5p4. |
| 173 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 174 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 175 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 176 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 177 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 178 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 179 | // C99 6.2.5p6. |
| 180 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 181 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 182 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 183 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 184 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 185 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 186 | // C99 6.2.5p10. |
| 187 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 188 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 189 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 190 | |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 191 | // GNU extension, 128-bit integers. |
| 192 | InitBuiltinType(Int128Ty, BuiltinType::Int128); |
| 193 | InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128); |
| 194 | |
Chris Lattner | 3a25032 | 2009-02-26 23:43:47 +0000 | [diff] [blame] | 195 | if (LangOpts.CPlusPlus) // C++ 3.9.1p5 |
| 196 | InitBuiltinType(WCharTy, BuiltinType::WChar); |
| 197 | else // C99 |
| 198 | WCharTy = getFromTargetType(Target.getWCharType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 199 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 200 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 201 | InitBuiltinType(Char16Ty, BuiltinType::Char16); |
| 202 | else // C99 |
| 203 | Char16Ty = getFromTargetType(Target.getChar16Type()); |
| 204 | |
| 205 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 206 | InitBuiltinType(Char32Ty, BuiltinType::Char32); |
| 207 | else // C99 |
| 208 | Char32Ty = getFromTargetType(Target.getChar32Type()); |
| 209 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 210 | // Placeholder type for functions. |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 211 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
| 212 | |
| 213 | // Placeholder type for type-dependent expressions whose type is |
| 214 | // completely unknown. No code should ever check a type against |
| 215 | // DependentTy and users should never see it; however, it is here to |
| 216 | // help diagnose failures to properly check for type-dependent |
| 217 | // expressions. |
| 218 | InitBuiltinType(DependentTy, BuiltinType::Dependent); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 219 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 220 | // Placeholder type for C++0x auto declarations whose real type has |
Anders Carlsson | e89d159 | 2009-06-26 18:41:36 +0000 | [diff] [blame] | 221 | // not yet been deduced. |
| 222 | InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 223 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 224 | // C99 6.2.5p11. |
| 225 | FloatComplexTy = getComplexType(FloatTy); |
| 226 | DoubleComplexTy = getComplexType(DoubleTy); |
| 227 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 228 | |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 229 | BuiltinVaListType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 230 | |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 231 | // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope(). |
| 232 | ObjCIdTypedefType = QualType(); |
| 233 | ObjCClassTypedefType = QualType(); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 234 | ObjCSelTypedefType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 235 | |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 236 | // Builtin types for 'id', 'Class', and 'SEL'. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 237 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); |
| 238 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 239 | InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 240 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 241 | ObjCConstantStringType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 242 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 243 | // void * type |
| 244 | VoidPtrTy = getPointerType(VoidTy); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 245 | |
| 246 | // nullptr type (C++0x 2.14.7) |
| 247 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 250 | MemberSpecializationInfo * |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 251 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 252 | assert(Var->isStaticDataMember() && "Not a static data member"); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 253 | llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 254 | = InstantiatedFromStaticDataMember.find(Var); |
| 255 | if (Pos == InstantiatedFromStaticDataMember.end()) |
| 256 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 257 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 258 | return Pos->second; |
| 259 | } |
| 260 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 261 | void |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 262 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, |
| 263 | TemplateSpecializationKind TSK) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 264 | assert(Inst->isStaticDataMember() && "Not a static data member"); |
| 265 | assert(Tmpl->isStaticDataMember() && "Not a static data member"); |
| 266 | assert(!InstantiatedFromStaticDataMember[Inst] && |
| 267 | "Already noted what static data member was instantiated from"); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 268 | InstantiatedFromStaticDataMember[Inst] |
| 269 | = new (*this) MemberSpecializationInfo(Tmpl, TSK); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 270 | } |
| 271 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 272 | NamedDecl * |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 273 | ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 274 | llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 275 | = InstantiatedFromUsingDecl.find(UUD); |
| 276 | if (Pos == InstantiatedFromUsingDecl.end()) |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 277 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 278 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 279 | return Pos->second; |
| 280 | } |
| 281 | |
| 282 | void |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 283 | ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) { |
| 284 | assert((isa<UsingDecl>(Pattern) || |
| 285 | isa<UnresolvedUsingValueDecl>(Pattern) || |
| 286 | isa<UnresolvedUsingTypenameDecl>(Pattern)) && |
| 287 | "pattern decl is not a using decl"); |
| 288 | assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists"); |
| 289 | InstantiatedFromUsingDecl[Inst] = Pattern; |
| 290 | } |
| 291 | |
| 292 | UsingShadowDecl * |
| 293 | ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) { |
| 294 | llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos |
| 295 | = InstantiatedFromUsingShadowDecl.find(Inst); |
| 296 | if (Pos == InstantiatedFromUsingShadowDecl.end()) |
| 297 | return 0; |
| 298 | |
| 299 | return Pos->second; |
| 300 | } |
| 301 | |
| 302 | void |
| 303 | ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, |
| 304 | UsingShadowDecl *Pattern) { |
| 305 | assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists"); |
| 306 | InstantiatedFromUsingShadowDecl[Inst] = Pattern; |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 309 | FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { |
| 310 | llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos |
| 311 | = InstantiatedFromUnnamedFieldDecl.find(Field); |
| 312 | if (Pos == InstantiatedFromUnnamedFieldDecl.end()) |
| 313 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 314 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 315 | return Pos->second; |
| 316 | } |
| 317 | |
| 318 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, |
| 319 | FieldDecl *Tmpl) { |
| 320 | assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed"); |
| 321 | assert(!Tmpl->getDeclName() && "Template field decl is not unnamed"); |
| 322 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] && |
| 323 | "Already noted what unnamed field was instantiated from"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 324 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 325 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; |
| 326 | } |
| 327 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 328 | CXXMethodVector::iterator CXXMethodVector::begin() const { |
| 329 | if ((Storage & 0x01) == 0) |
| 330 | return reinterpret_cast<iterator>(&Storage); |
| 331 | |
| 332 | vector_type *Vec = reinterpret_cast<vector_type *>(Storage & ~0x01); |
| 333 | return &Vec->front(); |
| 334 | } |
| 335 | |
| 336 | CXXMethodVector::iterator CXXMethodVector::end() const { |
| 337 | if ((Storage & 0x01) == 0) { |
| 338 | if (Storage == 0) |
| 339 | return reinterpret_cast<iterator>(&Storage); |
| 340 | |
| 341 | return reinterpret_cast<iterator>(&Storage) + 1; |
| 342 | } |
| 343 | |
| 344 | vector_type *Vec = reinterpret_cast<vector_type *>(Storage & ~0x01); |
| 345 | return &Vec->front() + Vec->size(); |
| 346 | } |
| 347 | |
| 348 | void CXXMethodVector::push_back(const CXXMethodDecl *Method) { |
| 349 | if (Storage == 0) { |
| 350 | // 0 -> 1 element. |
| 351 | Storage = reinterpret_cast<uintptr_t>(Method); |
| 352 | return; |
| 353 | } |
| 354 | |
| 355 | vector_type *Vec; |
| 356 | if ((Storage & 0x01) == 0) { |
| 357 | // 1 -> 2 elements. Allocate a new vector and push the element into that |
| 358 | // vector. |
| 359 | Vec = new vector_type; |
| 360 | Vec->push_back(reinterpret_cast<const CXXMethodDecl *>(Storage)); |
| 361 | Storage = reinterpret_cast<uintptr_t>(Vec) | 0x01; |
| 362 | } else |
| 363 | Vec = reinterpret_cast<vector_type *>(Storage & ~0x01); |
| 364 | |
| 365 | // Add the new method to the vector. |
| 366 | Vec->push_back(Method); |
| 367 | } |
| 368 | |
| 369 | void CXXMethodVector::Destroy() { |
| 370 | if (Storage & 0x01) |
| 371 | delete reinterpret_cast<vector_type *>(Storage & ~0x01); |
| 372 | |
| 373 | Storage = 0; |
| 374 | } |
| 375 | |
| 376 | |
| 377 | ASTContext::overridden_cxx_method_iterator |
| 378 | ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const { |
| 379 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 380 | = OverriddenMethods.find(Method); |
| 381 | if (Pos == OverriddenMethods.end()) |
| 382 | return 0; |
| 383 | |
| 384 | return Pos->second.begin(); |
| 385 | } |
| 386 | |
| 387 | ASTContext::overridden_cxx_method_iterator |
| 388 | ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const { |
| 389 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 390 | = OverriddenMethods.find(Method); |
| 391 | if (Pos == OverriddenMethods.end()) |
| 392 | return 0; |
| 393 | |
| 394 | return Pos->second.end(); |
| 395 | } |
| 396 | |
| 397 | void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method, |
| 398 | const CXXMethodDecl *Overridden) { |
| 399 | OverriddenMethods[Method].push_back(Overridden); |
| 400 | } |
| 401 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 402 | namespace { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 403 | class BeforeInTranslationUnit |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 404 | : std::binary_function<SourceRange, SourceRange, bool> { |
| 405 | SourceManager *SourceMgr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 406 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 407 | public: |
| 408 | explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 409 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 410 | bool operator()(SourceRange X, SourceRange Y) { |
| 411 | return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin()); |
| 412 | } |
| 413 | }; |
| 414 | } |
| 415 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 416 | //===----------------------------------------------------------------------===// |
| 417 | // Type Sizing and Analysis |
| 418 | //===----------------------------------------------------------------------===// |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 419 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 420 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 421 | /// scalar floating point type. |
| 422 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 423 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 424 | assert(BT && "Not a floating point type!"); |
| 425 | switch (BT->getKind()) { |
| 426 | default: assert(0 && "Not a floating point type!"); |
| 427 | case BuiltinType::Float: return Target.getFloatFormat(); |
| 428 | case BuiltinType::Double: return Target.getDoubleFormat(); |
| 429 | case BuiltinType::LongDouble: return Target.getLongDoubleFormat(); |
| 430 | } |
| 431 | } |
| 432 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 433 | /// getDeclAlign - Return a conservative estimate of the alignment of the |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 434 | /// specified decl. Note that bitfields do not have a valid alignment, so |
| 435 | /// this method will assert on them. |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 436 | /// If @p RefAsPointee, references are treated like their underlying type |
| 437 | /// (for alignof), else they're treated like pointers (for CodeGen). |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 438 | CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) { |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 439 | unsigned Align = Target.getCharWidth(); |
| 440 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 441 | if (const AlignedAttr* AA = D->getAttr<AlignedAttr>()) |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 442 | Align = std::max(Align, AA->getMaxAlignment()); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 443 | |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 444 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
| 445 | QualType T = VD->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 446 | if (const ReferenceType* RT = T->getAs<ReferenceType>()) { |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 447 | if (RefAsPointee) |
| 448 | T = RT->getPointeeType(); |
| 449 | else |
| 450 | T = getPointerType(RT->getPointeeType()); |
| 451 | } |
| 452 | if (!T->isIncompleteType() && !T->isFunctionType()) { |
Anders Carlsson | 4cc2cfd | 2009-04-10 04:47:03 +0000 | [diff] [blame] | 453 | // Incomplete or function types default to 1. |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 454 | while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T)) |
| 455 | T = cast<ArrayType>(T)->getElementType(); |
| 456 | |
| 457 | Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); |
| 458 | } |
Charles Davis | 05f6247 | 2010-02-23 04:52:00 +0000 | [diff] [blame] | 459 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) { |
| 460 | // In the case of a field in a packed struct, we want the minimum |
| 461 | // of the alignment of the field and the alignment of the struct. |
| 462 | Align = std::min(Align, |
| 463 | getPreferredTypeAlign(FD->getParent()->getTypeForDecl())); |
| 464 | } |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 465 | } |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 466 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 467 | return CharUnits::fromQuantity(Align / Target.getCharWidth()); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 468 | } |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 469 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 470 | /// getTypeSize - Return the size of the specified type, in bits. This method |
| 471 | /// does not work on incomplete types. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 472 | /// |
| 473 | /// FIXME: Pointers into different addr spaces could have different sizes and |
| 474 | /// alignment requirements: getPointerInfo should take an AddrSpace, this |
| 475 | /// should take a QualType, &c. |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 476 | std::pair<uint64_t, unsigned> |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 477 | ASTContext::getTypeInfo(const Type *T) { |
Mike Stump | 5e30100 | 2009-02-27 18:32:39 +0000 | [diff] [blame] | 478 | uint64_t Width=0; |
| 479 | unsigned Align=8; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 480 | switch (T->getTypeClass()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 481 | #define TYPE(Class, Base) |
| 482 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 483 | #define NON_CANONICAL_TYPE(Class, Base) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 484 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 485 | #include "clang/AST/TypeNodes.def" |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 486 | assert(false && "Should not see dependent types"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 487 | break; |
| 488 | |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 489 | case Type::FunctionNoProto: |
| 490 | case Type::FunctionProto: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 491 | // GCC extension: alignof(function) = 32 bits |
| 492 | Width = 0; |
| 493 | Align = 32; |
| 494 | break; |
| 495 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 496 | case Type::IncompleteArray: |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 497 | case Type::VariableArray: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 498 | Width = 0; |
| 499 | Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); |
| 500 | break; |
| 501 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 502 | case Type::ConstantArray: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 503 | const ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 504 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 505 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 506 | Width = EltInfo.first*CAT->getSize().getZExtValue(); |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 507 | Align = EltInfo.second; |
| 508 | break; |
Christopher Lamb | 5c09a02 | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 509 | } |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 510 | case Type::ExtVector: |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 511 | case Type::Vector: { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 512 | const VectorType *VT = cast<VectorType>(T); |
| 513 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType()); |
| 514 | Width = EltInfo.first*VT->getNumElements(); |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 515 | Align = Width; |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 516 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 517 | // This happens for non-power-of-2 length vectors. |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 518 | if (VT->getNumElements() & (VT->getNumElements()-1)) { |
| 519 | Align = llvm::NextPowerOf2(Align); |
| 520 | Width = llvm::RoundUpToAlignment(Width, Align); |
| 521 | } |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 522 | break; |
| 523 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 524 | |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 525 | case Type::Builtin: |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 526 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 527 | default: assert(0 && "Unknown builtin type!"); |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 528 | case BuiltinType::Void: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 529 | // GCC extension: alignof(void) = 8 bits. |
| 530 | Width = 0; |
| 531 | Align = 8; |
| 532 | break; |
| 533 | |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 534 | case BuiltinType::Bool: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 535 | Width = Target.getBoolWidth(); |
| 536 | Align = Target.getBoolAlign(); |
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::Char_S: |
| 539 | case BuiltinType::Char_U: |
| 540 | case BuiltinType::UChar: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 541 | case BuiltinType::SChar: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 542 | Width = Target.getCharWidth(); |
| 543 | Align = Target.getCharAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 544 | break; |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 545 | case BuiltinType::WChar: |
| 546 | Width = Target.getWCharWidth(); |
| 547 | Align = Target.getWCharAlign(); |
| 548 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 549 | case BuiltinType::Char16: |
| 550 | Width = Target.getChar16Width(); |
| 551 | Align = Target.getChar16Align(); |
| 552 | break; |
| 553 | case BuiltinType::Char32: |
| 554 | Width = Target.getChar32Width(); |
| 555 | Align = Target.getChar32Align(); |
| 556 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 557 | case BuiltinType::UShort: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 558 | case BuiltinType::Short: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 559 | Width = Target.getShortWidth(); |
| 560 | Align = Target.getShortAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 561 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 562 | case BuiltinType::UInt: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 563 | case BuiltinType::Int: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 564 | Width = Target.getIntWidth(); |
| 565 | Align = Target.getIntAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 566 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 567 | case BuiltinType::ULong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 568 | case BuiltinType::Long: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 569 | Width = Target.getLongWidth(); |
| 570 | Align = Target.getLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 571 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 572 | case BuiltinType::ULongLong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 573 | case BuiltinType::LongLong: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 574 | Width = Target.getLongLongWidth(); |
| 575 | Align = Target.getLongLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 576 | break; |
Chris Lattner | ec16cb9 | 2009-04-30 02:55:13 +0000 | [diff] [blame] | 577 | case BuiltinType::Int128: |
| 578 | case BuiltinType::UInt128: |
| 579 | Width = 128; |
| 580 | Align = 128; // int128_t is 128-bit aligned on all targets. |
| 581 | break; |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 582 | case BuiltinType::Float: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 583 | Width = Target.getFloatWidth(); |
| 584 | Align = Target.getFloatAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 585 | break; |
| 586 | case BuiltinType::Double: |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 587 | Width = Target.getDoubleWidth(); |
| 588 | Align = Target.getDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 589 | break; |
| 590 | case BuiltinType::LongDouble: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 591 | Width = Target.getLongDoubleWidth(); |
| 592 | Align = Target.getLongDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 593 | break; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 594 | case BuiltinType::NullPtr: |
| 595 | Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) |
| 596 | Align = Target.getPointerAlign(0); // == sizeof(void*) |
Sebastian Redl | 1590d9c | 2009-05-27 19:34:06 +0000 | [diff] [blame] | 597 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 598 | } |
Chris Lattner | bfef6d7 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 599 | break; |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 600 | case Type::ObjCObjectPointer: |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 601 | Width = Target.getPointerWidth(0); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 602 | Align = Target.getPointerAlign(0); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 603 | break; |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 604 | case Type::BlockPointer: { |
| 605 | unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace(); |
| 606 | Width = Target.getPointerWidth(AS); |
| 607 | Align = Target.getPointerAlign(AS); |
| 608 | break; |
| 609 | } |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 610 | case Type::LValueReference: |
| 611 | case Type::RValueReference: { |
| 612 | // alignof and sizeof should never enter this code path here, so we go |
| 613 | // the pointer route. |
| 614 | unsigned AS = cast<ReferenceType>(T)->getPointeeType().getAddressSpace(); |
| 615 | Width = Target.getPointerWidth(AS); |
| 616 | Align = Target.getPointerAlign(AS); |
| 617 | break; |
| 618 | } |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 619 | case Type::Pointer: { |
| 620 | unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace(); |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 621 | Width = Target.getPointerWidth(AS); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 622 | Align = Target.getPointerAlign(AS); |
| 623 | break; |
| 624 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 625 | case Type::MemberPointer: { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 626 | QualType Pointee = cast<MemberPointerType>(T)->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 627 | std::pair<uint64_t, unsigned> PtrDiffInfo = |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 628 | getTypeInfo(getPointerDiffType()); |
| 629 | Width = PtrDiffInfo.first; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 630 | if (Pointee->isFunctionType()) |
| 631 | Width *= 2; |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 632 | Align = PtrDiffInfo.second; |
| 633 | break; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 634 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 635 | case Type::Complex: { |
| 636 | // Complex types have the same alignment as their elements, but twice the |
| 637 | // size. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 638 | std::pair<uint64_t, unsigned> EltInfo = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 639 | getTypeInfo(cast<ComplexType>(T)->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 640 | Width = EltInfo.first*2; |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 641 | Align = EltInfo.second; |
| 642 | break; |
| 643 | } |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 644 | case Type::ObjCInterface: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 645 | const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 646 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
| 647 | Width = Layout.getSize(); |
| 648 | Align = Layout.getAlignment(); |
| 649 | break; |
| 650 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 651 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 652 | case Type::Enum: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 653 | const TagType *TT = cast<TagType>(T); |
| 654 | |
| 655 | if (TT->getDecl()->isInvalidDecl()) { |
Chris Lattner | 8389eab | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 656 | Width = 1; |
| 657 | Align = 1; |
| 658 | break; |
| 659 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 660 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 661 | if (const EnumType *ET = dyn_cast<EnumType>(TT)) |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 662 | return getTypeInfo(ET->getDecl()->getIntegerType()); |
| 663 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 664 | const RecordType *RT = cast<RecordType>(TT); |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 665 | const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); |
| 666 | Width = Layout.getSize(); |
| 667 | Align = Layout.getAlignment(); |
Chris Lattner | dc0d73e | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 668 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 669 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 670 | |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 671 | case Type::SubstTemplateTypeParm: |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 672 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> |
| 673 | getReplacementType().getTypePtr()); |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 674 | |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 675 | case Type::Elaborated: |
| 676 | return getTypeInfo(cast<ElaboratedType>(T)->getUnderlyingType() |
| 677 | .getTypePtr()); |
John McCall | 7da2431 | 2009-09-05 00:15:47 +0000 | [diff] [blame] | 678 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 679 | case Type::Typedef: { |
| 680 | const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl(); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 681 | if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 682 | Align = std::max(Aligned->getMaxAlignment(), |
| 683 | getTypeAlign(Typedef->getUnderlyingType().getTypePtr())); |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 684 | Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr()); |
| 685 | } else |
| 686 | return getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 687 | break; |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 688 | } |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 689 | |
| 690 | case Type::TypeOfExpr: |
| 691 | return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType() |
| 692 | .getTypePtr()); |
| 693 | |
| 694 | case Type::TypeOf: |
| 695 | return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr()); |
| 696 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 697 | case Type::Decltype: |
| 698 | return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType() |
| 699 | .getTypePtr()); |
| 700 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 701 | case Type::QualifiedName: |
| 702 | return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 703 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 704 | case Type::InjectedClassName: |
| 705 | return getTypeInfo(cast<InjectedClassNameType>(T) |
| 706 | ->getUnderlyingType().getTypePtr()); |
| 707 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 708 | case Type::TemplateSpecialization: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 709 | assert(getCanonicalType(T) != T && |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 710 | "Cannot request the size of a dependent type"); |
| 711 | // FIXME: this is likely to be wrong once we support template |
| 712 | // aliases, since a template alias could refer to a typedef that |
| 713 | // has an __aligned__ attribute on it. |
| 714 | return getTypeInfo(getCanonicalType(T)); |
| 715 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 716 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 717 | assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2"); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 718 | return std::make_pair(Width, Align); |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 719 | } |
| 720 | |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 721 | /// getTypeSizeInChars - Return the size of the specified type, in characters. |
| 722 | /// This method does not work on incomplete types. |
| 723 | CharUnits ASTContext::getTypeSizeInChars(QualType T) { |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 724 | return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth()); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 725 | } |
| 726 | CharUnits ASTContext::getTypeSizeInChars(const Type *T) { |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 727 | return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth()); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 728 | } |
| 729 | |
Ken Dyck | 16e20cc | 2010-01-26 17:25:18 +0000 | [diff] [blame] | 730 | /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 731 | /// characters. This method does not work on incomplete types. |
| 732 | CharUnits ASTContext::getTypeAlignInChars(QualType T) { |
| 733 | return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth()); |
| 734 | } |
| 735 | CharUnits ASTContext::getTypeAlignInChars(const Type *T) { |
| 736 | return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth()); |
| 737 | } |
| 738 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 739 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 740 | /// type for the current target in bits. This can be different than the ABI |
| 741 | /// alignment in cases where it is beneficial for performance to overalign |
| 742 | /// a data type. |
| 743 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) { |
| 744 | unsigned ABIAlign = getTypeAlign(T); |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 745 | |
| 746 | // Double and long long should be naturally aligned if possible. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 747 | if (const ComplexType* CT = T->getAs<ComplexType>()) |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 748 | T = CT->getElementType().getTypePtr(); |
| 749 | if (T->isSpecificBuiltinType(BuiltinType::Double) || |
| 750 | T->isSpecificBuiltinType(BuiltinType::LongLong)) |
| 751 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); |
| 752 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 753 | return ABIAlign; |
| 754 | } |
| 755 | |
Daniel Dunbar | a80a0f6 | 2009-04-22 17:43:55 +0000 | [diff] [blame] | 756 | static void CollectLocalObjCIvars(ASTContext *Ctx, |
| 757 | const ObjCInterfaceDecl *OI, |
| 758 | llvm::SmallVectorImpl<FieldDecl*> &Fields) { |
Fariborz Jahanian | a769c00 | 2008-12-17 21:40:49 +0000 | [diff] [blame] | 759 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 760 | E = OI->ivar_end(); I != E; ++I) { |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 761 | ObjCIvarDecl *IVDecl = *I; |
Fariborz Jahanian | a769c00 | 2008-12-17 21:40:49 +0000 | [diff] [blame] | 762 | if (!IVDecl->isInvalidDecl()) |
| 763 | Fields.push_back(cast<FieldDecl>(IVDecl)); |
| 764 | } |
| 765 | } |
| 766 | |
Daniel Dunbar | a80a0f6 | 2009-04-22 17:43:55 +0000 | [diff] [blame] | 767 | void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 768 | llvm::SmallVectorImpl<FieldDecl*> &Fields) { |
| 769 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
| 770 | CollectObjCIvars(SuperClass, Fields); |
| 771 | CollectLocalObjCIvars(this, OI, Fields); |
| 772 | } |
| 773 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 774 | /// ShallowCollectObjCIvars - |
| 775 | /// Collect all ivars, including those synthesized, in the current class. |
| 776 | /// |
| 777 | void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI, |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 778 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) { |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 779 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 780 | E = OI->ivar_end(); I != E; ++I) { |
| 781 | Ivars.push_back(*I); |
| 782 | } |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 783 | |
| 784 | CollectNonClassIvars(OI, Ivars); |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 787 | /// CollectNonClassIvars - |
| 788 | /// 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] | 789 | /// This includes synthesized ivars (via @synthesize) and those in |
| 790 | // class's @implementation. |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 791 | /// |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 792 | void ASTContext::CollectNonClassIvars(const ObjCInterfaceDecl *OI, |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 793 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) { |
Fariborz Jahanian | 0e5ad25 | 2010-02-23 01:26:30 +0000 | [diff] [blame] | 794 | // Find ivars declared in class extension. |
| 795 | if (const ObjCCategoryDecl *CDecl = OI->getClassExtension()) { |
| 796 | for (ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(), |
| 797 | E = CDecl->ivar_end(); I != E; ++I) { |
| 798 | Ivars.push_back(*I); |
| 799 | } |
| 800 | } |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 801 | |
Ted Kremenek | 7d2aa11 | 2010-03-11 19:44:54 +0000 | [diff] [blame] | 802 | // Also add any ivar defined in this class's implementation. This |
| 803 | // includes synthesized ivars. |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 804 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) { |
| 805 | for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(), |
| 806 | E = ImplDecl->ivar_end(); I != E; ++I) |
| 807 | Ivars.push_back(*I); |
| 808 | } |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 809 | } |
| 810 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 811 | /// CollectInheritedProtocols - Collect all protocols in current class and |
| 812 | /// those inherited by it. |
| 813 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 814 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 815 | if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
| 816 | for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(), |
| 817 | PE = OI->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(), |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 821 | PE = Proto->protocol_end(); P != PE; ++P) { |
| 822 | Protocols.insert(*P); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 823 | CollectInheritedProtocols(*P, Protocols); |
| 824 | } |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 825 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 826 | |
| 827 | // Categories of this Interface. |
| 828 | for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList(); |
| 829 | CDeclChain; CDeclChain = CDeclChain->getNextClassCategory()) |
| 830 | CollectInheritedProtocols(CDeclChain, Protocols); |
| 831 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) |
| 832 | while (SD) { |
| 833 | CollectInheritedProtocols(SD, Protocols); |
| 834 | SD = SD->getSuperClass(); |
| 835 | } |
| 836 | return; |
| 837 | } |
| 838 | if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
| 839 | for (ObjCInterfaceDecl::protocol_iterator P = OC->protocol_begin(), |
| 840 | PE = OC->protocol_end(); P != PE; ++P) { |
| 841 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 842 | Protocols.insert(Proto); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 843 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 844 | PE = Proto->protocol_end(); P != PE; ++P) |
| 845 | CollectInheritedProtocols(*P, Protocols); |
| 846 | } |
| 847 | return; |
| 848 | } |
| 849 | if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
| 850 | for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(), |
| 851 | PE = OP->protocol_end(); P != PE; ++P) { |
| 852 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 853 | Protocols.insert(Proto); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 854 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 855 | PE = Proto->protocol_end(); P != PE; ++P) |
| 856 | CollectInheritedProtocols(*P, Protocols); |
| 857 | } |
| 858 | return; |
| 859 | } |
| 860 | } |
| 861 | |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 862 | unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) { |
| 863 | unsigned count = 0; |
| 864 | // Count ivars declared in class extension. |
| 865 | if (const ObjCCategoryDecl *CDecl = OI->getClassExtension()) { |
| 866 | for (ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(), |
| 867 | E = CDecl->ivar_end(); I != E; ++I) { |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 868 | ++count; |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 869 | } |
| 870 | } |
| 871 | |
| 872 | // Count ivar defined in this class's implementation. This |
| 873 | // includes synthesized ivars. |
| 874 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) |
| 875 | for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(), |
| 876 | E = ImplDecl->ivar_end(); I != E; ++I) |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 877 | ++count; |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 878 | return count; |
| 879 | } |
| 880 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 881 | /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. |
| 882 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
| 883 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 884 | I = ObjCImpls.find(D); |
| 885 | if (I != ObjCImpls.end()) |
| 886 | return cast<ObjCImplementationDecl>(I->second); |
| 887 | return 0; |
| 888 | } |
| 889 | /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. |
| 890 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
| 891 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 892 | I = ObjCImpls.find(D); |
| 893 | if (I != ObjCImpls.end()) |
| 894 | return cast<ObjCCategoryImplDecl>(I->second); |
| 895 | return 0; |
| 896 | } |
| 897 | |
| 898 | /// \brief Set the implementation of ObjCInterfaceDecl. |
| 899 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
| 900 | ObjCImplementationDecl *ImplD) { |
| 901 | assert(IFaceD && ImplD && "Passed null params"); |
| 902 | ObjCImpls[IFaceD] = ImplD; |
| 903 | } |
| 904 | /// \brief Set the implementation of ObjCCategoryDecl. |
| 905 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
| 906 | ObjCCategoryImplDecl *ImplD) { |
| 907 | assert(CatD && ImplD && "Passed null params"); |
| 908 | ObjCImpls[CatD] = ImplD; |
| 909 | } |
| 910 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 911 | /// \brief Allocate an uninitialized TypeSourceInfo. |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 912 | /// |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 913 | /// The caller should initialize the memory held by TypeSourceInfo using |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 914 | /// the TypeLoc wrappers. |
| 915 | /// |
| 916 | /// \param T the type that will be the basis for type source info. This type |
| 917 | /// should refer to how the declarator was written in source code, not to |
| 918 | /// what type semantic analysis resolved the declarator to. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 919 | TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T, |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 920 | unsigned DataSize) { |
| 921 | if (!DataSize) |
| 922 | DataSize = TypeLoc::getFullDataSizeForType(T); |
| 923 | else |
| 924 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 925 | "incorrect data size provided to CreateTypeSourceInfo!"); |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 926 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 927 | TypeSourceInfo *TInfo = |
| 928 | (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8); |
| 929 | new (TInfo) TypeSourceInfo(T); |
| 930 | return TInfo; |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 931 | } |
| 932 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 933 | TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, |
John McCall | a4eb74d | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 934 | SourceLocation L) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 935 | TypeSourceInfo *DI = CreateTypeSourceInfo(T); |
John McCall | a4eb74d | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 936 | DI->getTypeLoc().initialize(L); |
| 937 | return DI; |
| 938 | } |
| 939 | |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 940 | /// getInterfaceLayoutImpl - Get or compute information about the |
| 941 | /// layout of the given interface. |
| 942 | /// |
| 943 | /// \param Impl - If given, also include the layout of the interface's |
| 944 | /// implementation. This may differ by including synthesized ivars. |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 945 | const ASTRecordLayout & |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 946 | ASTContext::getObjCLayout(const ObjCInterfaceDecl *D, |
| 947 | const ObjCImplementationDecl *Impl) { |
Daniel Dunbar | 532d4da | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 948 | assert(!D->isForwardDecl() && "Invalid interface decl!"); |
| 949 | |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 950 | // Look up this layout, if already laid out, return what we have. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 951 | ObjCContainerDecl *Key = |
Daniel Dunbar | d8fd6ff | 2009-05-03 11:41:43 +0000 | [diff] [blame] | 952 | Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D; |
| 953 | if (const ASTRecordLayout *Entry = ObjCLayouts[Key]) |
| 954 | return *Entry; |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 955 | |
Daniel Dunbar | 453addb | 2009-05-03 11:16:44 +0000 | [diff] [blame] | 956 | // Add in synthesized ivar count if laying out an implementation. |
| 957 | if (Impl) { |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 958 | unsigned SynthCount = CountNonClassIvars(D); |
Daniel Dunbar | d8fd6ff | 2009-05-03 11:41:43 +0000 | [diff] [blame] | 959 | // If there aren't any sythesized ivars then reuse the interface |
Daniel Dunbar | 453addb | 2009-05-03 11:16:44 +0000 | [diff] [blame] | 960 | // entry. Note we can't cache this because we simply free all |
| 961 | // entries later; however we shouldn't look up implementations |
| 962 | // frequently. |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 963 | if (SynthCount == 0) |
Daniel Dunbar | 453addb | 2009-05-03 11:16:44 +0000 | [diff] [blame] | 964 | return getObjCLayout(D, 0); |
| 965 | } |
| 966 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 967 | const ASTRecordLayout *NewEntry = |
Anders Carlsson | 29445a0 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 968 | ASTRecordLayoutBuilder::ComputeLayout(*this, D, Impl); |
| 969 | ObjCLayouts[Key] = NewEntry; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 970 | |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 971 | return *NewEntry; |
| 972 | } |
| 973 | |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 974 | const ASTRecordLayout & |
| 975 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) { |
| 976 | return getObjCLayout(D, 0); |
| 977 | } |
| 978 | |
| 979 | const ASTRecordLayout & |
| 980 | ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) { |
| 981 | return getObjCLayout(D->getClassInterface(), D); |
| 982 | } |
| 983 | |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 984 | /// getASTRecordLayout - Get or compute information about the layout of the |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 985 | /// specified record (struct/union/class), which indicates its size and field |
| 986 | /// position information. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 987 | const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 988 | D = D->getDefinition(); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 989 | assert(D && "Cannot get layout of forward declarations!"); |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 990 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 991 | // Look up this layout, if already laid out, return what we have. |
Eli Friedman | ab22c43 | 2009-07-22 20:29:16 +0000 | [diff] [blame] | 992 | // Note that we can't save a reference to the entry because this function |
| 993 | // is recursive. |
| 994 | const ASTRecordLayout *Entry = ASTRecordLayouts[D]; |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 995 | if (Entry) return *Entry; |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 996 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 997 | const ASTRecordLayout *NewEntry = |
Anders Carlsson | 29445a0 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 998 | ASTRecordLayoutBuilder::ComputeLayout(*this, D); |
Eli Friedman | ab22c43 | 2009-07-22 20:29:16 +0000 | [diff] [blame] | 999 | ASTRecordLayouts[D] = NewEntry; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1000 | |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1001 | return *NewEntry; |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
Anders Carlsson | f53df23 | 2009-12-07 04:35:11 +0000 | [diff] [blame] | 1004 | const CXXMethodDecl *ASTContext::getKeyFunction(const CXXRecordDecl *RD) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1005 | RD = cast<CXXRecordDecl>(RD->getDefinition()); |
Anders Carlsson | f53df23 | 2009-12-07 04:35:11 +0000 | [diff] [blame] | 1006 | assert(RD && "Cannot get key function for forward declarations!"); |
| 1007 | |
| 1008 | const CXXMethodDecl *&Entry = KeyFunctions[RD]; |
| 1009 | if (!Entry) |
| 1010 | Entry = ASTRecordLayoutBuilder::ComputeKeyFunction(RD); |
| 1011 | else |
| 1012 | assert(Entry == ASTRecordLayoutBuilder::ComputeKeyFunction(RD) && |
| 1013 | "Key function changed!"); |
| 1014 | |
| 1015 | return Entry; |
| 1016 | } |
| 1017 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1018 | //===----------------------------------------------------------------------===// |
| 1019 | // Type creation/memoization methods |
| 1020 | //===----------------------------------------------------------------------===// |
| 1021 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1022 | QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) { |
| 1023 | unsigned Fast = Quals.getFastQualifiers(); |
| 1024 | Quals.removeFastQualifiers(); |
| 1025 | |
| 1026 | // Check if we've already instantiated this type. |
| 1027 | llvm::FoldingSetNodeID ID; |
| 1028 | ExtQuals::Profile(ID, TypeNode, Quals); |
| 1029 | void *InsertPos = 0; |
| 1030 | if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) { |
| 1031 | assert(EQ->getQualifiers() == Quals); |
| 1032 | QualType T = QualType(EQ, Fast); |
| 1033 | return T; |
| 1034 | } |
| 1035 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1036 | ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1037 | ExtQualNodes.InsertNode(New, InsertPos); |
| 1038 | QualType T = QualType(New, Fast); |
| 1039 | return T; |
| 1040 | } |
| 1041 | |
| 1042 | QualType ASTContext::getVolatileType(QualType T) { |
| 1043 | QualType CanT = getCanonicalType(T); |
| 1044 | if (CanT.isVolatileQualified()) return T; |
| 1045 | |
| 1046 | QualifierCollector Quals; |
| 1047 | const Type *TypeNode = Quals.strip(T); |
| 1048 | Quals.addVolatile(); |
| 1049 | |
| 1050 | return getExtQualType(TypeNode, Quals); |
| 1051 | } |
| 1052 | |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 1053 | QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1054 | QualType CanT = getCanonicalType(T); |
| 1055 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1056 | return T; |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1057 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1058 | // If we are composing extended qualifiers together, merge together |
| 1059 | // into one ExtQuals node. |
| 1060 | QualifierCollector Quals; |
| 1061 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1062 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1063 | // If this type already has an address space specified, it cannot get |
| 1064 | // another one. |
| 1065 | assert(!Quals.hasAddressSpace() && |
| 1066 | "Type cannot be in multiple addr spaces!"); |
| 1067 | Quals.addAddressSpace(AddressSpace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1068 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1069 | return getExtQualType(TypeNode, Quals); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1072 | QualType ASTContext::getObjCGCQualType(QualType T, |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1073 | Qualifiers::GC GCAttr) { |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1074 | QualType CanT = getCanonicalType(T); |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1075 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1076 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1077 | |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1078 | if (T->isPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1079 | QualType Pointee = T->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 1080 | if (Pointee->isAnyPointerType()) { |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1081 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 1082 | return getPointerType(ResultType); |
| 1083 | } |
| 1084 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1085 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1086 | // If we are composing extended qualifiers together, merge together |
| 1087 | // into one ExtQuals node. |
| 1088 | QualifierCollector Quals; |
| 1089 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1090 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1091 | // If this type already has an ObjCGC specified, it cannot get |
| 1092 | // another one. |
| 1093 | assert(!Quals.hasObjCGCAttr() && |
| 1094 | "Type cannot have multiple ObjCGCs!"); |
| 1095 | Quals.addObjCGCAttr(GCAttr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1096 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1097 | return getExtQualType(TypeNode, Quals); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1098 | } |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1099 | |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1100 | static QualType getExtFunctionType(ASTContext& Context, QualType T, |
| 1101 | const FunctionType::ExtInfo &Info) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1102 | QualType ResultType; |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1103 | if (const PointerType *Pointer = T->getAs<PointerType>()) { |
| 1104 | QualType Pointee = Pointer->getPointeeType(); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1105 | ResultType = getExtFunctionType(Context, Pointee, Info); |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1106 | if (ResultType == Pointee) |
| 1107 | return T; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1108 | |
| 1109 | ResultType = Context.getPointerType(ResultType); |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1110 | } else if (const BlockPointerType *BlockPointer |
| 1111 | = T->getAs<BlockPointerType>()) { |
| 1112 | QualType Pointee = BlockPointer->getPointeeType(); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1113 | ResultType = getExtFunctionType(Context, Pointee, Info); |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1114 | if (ResultType == Pointee) |
| 1115 | return T; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1116 | |
| 1117 | ResultType = Context.getBlockPointerType(ResultType); |
| 1118 | } else if (const FunctionType *F = T->getAs<FunctionType>()) { |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1119 | if (F->getExtInfo() == Info) |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1120 | return T; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1121 | |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1122 | if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(F)) { |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1123 | ResultType = Context.getFunctionNoProtoType(FNPT->getResultType(), |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1124 | Info); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1125 | } else { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1126 | const FunctionProtoType *FPT = cast<FunctionProtoType>(F); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1127 | ResultType |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1128 | = Context.getFunctionType(FPT->getResultType(), FPT->arg_type_begin(), |
| 1129 | FPT->getNumArgs(), FPT->isVariadic(), |
| 1130 | FPT->getTypeQuals(), |
| 1131 | FPT->hasExceptionSpec(), |
| 1132 | FPT->hasAnyExceptionSpec(), |
| 1133 | FPT->getNumExceptions(), |
| 1134 | FPT->exception_begin(), |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1135 | Info); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1136 | } |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1137 | } else |
| 1138 | return T; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1139 | |
| 1140 | return Context.getQualifiedType(ResultType, T.getLocalQualifiers()); |
| 1141 | } |
| 1142 | |
| 1143 | QualType ASTContext::getNoReturnType(QualType T, bool AddNoReturn) { |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 1144 | FunctionType::ExtInfo Info = getFunctionExtInfo(T); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1145 | return getExtFunctionType(*this, T, |
| 1146 | Info.withNoReturn(AddNoReturn)); |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | QualType ASTContext::getCallConvType(QualType T, CallingConv CallConv) { |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 1150 | FunctionType::ExtInfo Info = getFunctionExtInfo(T); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1151 | return getExtFunctionType(*this, T, |
| 1152 | Info.withCallingConv(CallConv)); |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 1155 | QualType ASTContext::getRegParmType(QualType T, unsigned RegParm) { |
| 1156 | FunctionType::ExtInfo Info = getFunctionExtInfo(T); |
| 1157 | return getExtFunctionType(*this, T, |
| 1158 | Info.withRegParm(RegParm)); |
| 1159 | } |
| 1160 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1161 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 1162 | /// number with the specified element type. |
| 1163 | QualType ASTContext::getComplexType(QualType T) { |
| 1164 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1165 | // structure. |
| 1166 | llvm::FoldingSetNodeID ID; |
| 1167 | ComplexType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1168 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1169 | void *InsertPos = 0; |
| 1170 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1171 | return QualType(CT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1172 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1173 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1174 | // so fill in the canonical type field. |
| 1175 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1176 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1177 | Canonical = getComplexType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1178 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1179 | // Get the new insert position for the node we care about. |
| 1180 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1181 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1182 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1183 | ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1184 | Types.push_back(New); |
| 1185 | ComplexTypes.InsertNode(New, InsertPos); |
| 1186 | return QualType(New, 0); |
| 1187 | } |
| 1188 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1189 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 1190 | /// the specified type. |
| 1191 | QualType ASTContext::getPointerType(QualType T) { |
| 1192 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1193 | // structure. |
| 1194 | llvm::FoldingSetNodeID ID; |
| 1195 | PointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1196 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1197 | void *InsertPos = 0; |
| 1198 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1199 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1200 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1201 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1202 | // so fill in the canonical type field. |
| 1203 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1204 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1205 | Canonical = getPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1206 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1207 | // Get the new insert position for the node we care about. |
| 1208 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1209 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1210 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1211 | PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1212 | Types.push_back(New); |
| 1213 | PointerTypes.InsertNode(New, InsertPos); |
| 1214 | return QualType(New, 0); |
| 1215 | } |
| 1216 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1217 | /// getBlockPointerType - Return the uniqued reference to the type for |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1218 | /// a pointer to the specified block. |
| 1219 | QualType ASTContext::getBlockPointerType(QualType T) { |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1220 | assert(T->isFunctionType() && "block of function types only"); |
| 1221 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1222 | // structure. |
| 1223 | llvm::FoldingSetNodeID ID; |
| 1224 | BlockPointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1225 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1226 | void *InsertPos = 0; |
| 1227 | if (BlockPointerType *PT = |
| 1228 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1229 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1230 | |
| 1231 | // 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] | 1232 | // type either so fill in the canonical type field. |
| 1233 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1234 | if (!T.isCanonical()) { |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1235 | Canonical = getBlockPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1236 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1237 | // Get the new insert position for the node we care about. |
| 1238 | BlockPointerType *NewIP = |
| 1239 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1240 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1241 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1242 | BlockPointerType *New |
| 1243 | = new (*this, TypeAlignment) BlockPointerType(T, Canonical); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1244 | Types.push_back(New); |
| 1245 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 1246 | return QualType(New, 0); |
| 1247 | } |
| 1248 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1249 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 1250 | /// lvalue reference to the specified type. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1251 | QualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1252 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1253 | // structure. |
| 1254 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1255 | ReferenceType::Profile(ID, T, SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1256 | |
| 1257 | void *InsertPos = 0; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1258 | if (LValueReferenceType *RT = |
| 1259 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1260 | return QualType(RT, 0); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1261 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1262 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1263 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1264 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1265 | // either, so fill in the canonical type field. |
| 1266 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1267 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
| 1268 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1269 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1270 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1271 | // Get the new insert position for the node we care about. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1272 | LValueReferenceType *NewIP = |
| 1273 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1274 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1275 | } |
| 1276 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1277 | LValueReferenceType *New |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1278 | = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, |
| 1279 | SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1280 | Types.push_back(New); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1281 | LValueReferenceTypes.InsertNode(New, InsertPos); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1282 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1283 | return QualType(New, 0); |
| 1284 | } |
| 1285 | |
| 1286 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 1287 | /// rvalue reference to the specified type. |
| 1288 | QualType ASTContext::getRValueReferenceType(QualType T) { |
| 1289 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1290 | // structure. |
| 1291 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1292 | ReferenceType::Profile(ID, T, false); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1293 | |
| 1294 | void *InsertPos = 0; |
| 1295 | if (RValueReferenceType *RT = |
| 1296 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1297 | return QualType(RT, 0); |
| 1298 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1299 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1300 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1301 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1302 | // either, so fill in the canonical type field. |
| 1303 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1304 | if (InnerRef || !T.isCanonical()) { |
| 1305 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1306 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1307 | |
| 1308 | // Get the new insert position for the node we care about. |
| 1309 | RValueReferenceType *NewIP = |
| 1310 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1311 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
| 1312 | } |
| 1313 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1314 | RValueReferenceType *New |
| 1315 | = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1316 | Types.push_back(New); |
| 1317 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1318 | return QualType(New, 0); |
| 1319 | } |
| 1320 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1321 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 1322 | /// member pointer to the specified type, in the specified class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1323 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1324 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1325 | // structure. |
| 1326 | llvm::FoldingSetNodeID ID; |
| 1327 | MemberPointerType::Profile(ID, T, Cls); |
| 1328 | |
| 1329 | void *InsertPos = 0; |
| 1330 | if (MemberPointerType *PT = |
| 1331 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1332 | return QualType(PT, 0); |
| 1333 | |
| 1334 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 1335 | // type either, so fill in the canonical type field. |
| 1336 | QualType Canonical; |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1337 | if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1338 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 1339 | |
| 1340 | // Get the new insert position for the node we care about. |
| 1341 | MemberPointerType *NewIP = |
| 1342 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1343 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
| 1344 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1345 | MemberPointerType *New |
| 1346 | = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1347 | Types.push_back(New); |
| 1348 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 1349 | return QualType(New, 0); |
| 1350 | } |
| 1351 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1352 | /// getConstantArrayType - Return the unique reference to the type for an |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1353 | /// array of the specified element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1354 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1355 | const llvm::APInt &ArySizeIn, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1356 | ArrayType::ArraySizeModifier ASM, |
| 1357 | unsigned EltTypeQuals) { |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1358 | assert((EltTy->isDependentType() || |
| 1359 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && |
Eli Friedman | 587cbdf | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1360 | "Constant array of VLAs is illegal!"); |
| 1361 | |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1362 | // Convert the array size into a canonical width matching the pointer size for |
| 1363 | // the target. |
| 1364 | llvm::APInt ArySize(ArySizeIn); |
| 1365 | ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1366 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1367 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 0be2ef2 | 2009-02-19 17:31:02 +0000 | [diff] [blame] | 1368 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1369 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1370 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1371 | if (ConstantArrayType *ATP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1372 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1373 | return QualType(ATP, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1374 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1375 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1376 | // so fill in the canonical type field. |
| 1377 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1378 | if (!EltTy.isCanonical()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1379 | Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1380 | ASM, EltTypeQuals); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1381 | // Get the new insert position for the node we care about. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1382 | ConstantArrayType *NewIP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1383 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1384 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1385 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1386 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1387 | ConstantArrayType *New = new(*this,TypeAlignment) |
| 1388 | ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1389 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1390 | Types.push_back(New); |
| 1391 | return QualType(New, 0); |
| 1392 | } |
| 1393 | |
Steve Naroff | bdbf7b0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 1394 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 1395 | /// variable array of the specified element type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1396 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 1397 | Expr *NumElts, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1398 | ArrayType::ArraySizeModifier ASM, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1399 | unsigned EltTypeQuals, |
| 1400 | SourceRange Brackets) { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1401 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 1402 | // that have an expression provided for their size. |
| 1403 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1404 | VariableArrayType *New = new(*this, TypeAlignment) |
| 1405 | VariableArrayType(EltTy, QualType(), NumElts, ASM, EltTypeQuals, Brackets); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1406 | |
| 1407 | VariableArrayTypes.push_back(New); |
| 1408 | Types.push_back(New); |
| 1409 | return QualType(New, 0); |
| 1410 | } |
| 1411 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1412 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 1413 | /// the type for a dependently-sized array of the specified element |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1414 | /// type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1415 | QualType ASTContext::getDependentSizedArrayType(QualType EltTy, |
| 1416 | Expr *NumElts, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1417 | ArrayType::ArraySizeModifier ASM, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1418 | unsigned EltTypeQuals, |
| 1419 | SourceRange Brackets) { |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1420 | assert((!NumElts || NumElts->isTypeDependent() || |
| 1421 | NumElts->isValueDependent()) && |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1422 | "Size must be type- or value-dependent!"); |
| 1423 | |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1424 | void *InsertPos = 0; |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1425 | DependentSizedArrayType *Canon = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1426 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1427 | |
| 1428 | if (NumElts) { |
| 1429 | // Dependently-sized array types that do not have a specified |
| 1430 | // number of elements will have their sizes deduced from an |
| 1431 | // initializer. |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1432 | DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM, |
| 1433 | EltTypeQuals, NumElts); |
| 1434 | |
| 1435 | Canon = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1436 | } |
| 1437 | |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1438 | DependentSizedArrayType *New; |
| 1439 | if (Canon) { |
| 1440 | // We already have a canonical version of this array type; use it as |
| 1441 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1442 | New = new (*this, TypeAlignment) |
| 1443 | DependentSizedArrayType(*this, EltTy, QualType(Canon, 0), |
| 1444 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1445 | } else { |
| 1446 | QualType CanonEltTy = getCanonicalType(EltTy); |
| 1447 | if (CanonEltTy == EltTy) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1448 | New = new (*this, TypeAlignment) |
| 1449 | DependentSizedArrayType(*this, EltTy, QualType(), |
| 1450 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1451 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1452 | if (NumElts) { |
| 1453 | DependentSizedArrayType *CanonCheck |
| 1454 | = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1455 | assert(!CanonCheck && "Dependent-sized canonical array type broken"); |
| 1456 | (void)CanonCheck; |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1457 | DependentSizedArrayTypes.InsertNode(New, InsertPos); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1458 | } |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1459 | } else { |
| 1460 | QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts, |
| 1461 | ASM, EltTypeQuals, |
| 1462 | SourceRange()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1463 | New = new (*this, TypeAlignment) |
| 1464 | DependentSizedArrayType(*this, EltTy, Canon, |
| 1465 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1466 | } |
| 1467 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1468 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1469 | Types.push_back(New); |
| 1470 | return QualType(New, 0); |
| 1471 | } |
| 1472 | |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1473 | QualType ASTContext::getIncompleteArrayType(QualType EltTy, |
| 1474 | ArrayType::ArraySizeModifier ASM, |
| 1475 | unsigned EltTypeQuals) { |
| 1476 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 0be2ef2 | 2009-02-19 17:31:02 +0000 | [diff] [blame] | 1477 | IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1478 | |
| 1479 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1480 | if (IncompleteArrayType *ATP = |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1481 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1482 | return QualType(ATP, 0); |
| 1483 | |
| 1484 | // If the element type isn't canonical, this won't be a canonical type |
| 1485 | // either, so fill in the canonical type field. |
| 1486 | QualType Canonical; |
| 1487 | |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1488 | if (!EltTy.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1489 | Canonical = getIncompleteArrayType(getCanonicalType(EltTy), |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1490 | ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1491 | |
| 1492 | // Get the new insert position for the node we care about. |
| 1493 | IncompleteArrayType *NewIP = |
| 1494 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1495 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1496 | } |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1497 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1498 | IncompleteArrayType *New = new (*this, TypeAlignment) |
| 1499 | IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1500 | |
| 1501 | IncompleteArrayTypes.InsertNode(New, InsertPos); |
| 1502 | Types.push_back(New); |
| 1503 | return QualType(New, 0); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1504 | } |
| 1505 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1506 | /// getVectorType - Return the unique reference to a vector type of |
| 1507 | /// 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] | 1508 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts, |
| 1509 | bool IsAltiVec, bool IsPixel) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1510 | BuiltinType *baseType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1511 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1512 | baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1513 | assert(baseType != 0 && "getVectorType(): Expecting a built-in type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1514 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1515 | // Check if we've already instantiated a vector of this type. |
| 1516 | llvm::FoldingSetNodeID ID; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1517 | VectorType::Profile(ID, vecType, NumElts, Type::Vector, |
| 1518 | IsAltiVec, IsPixel); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1519 | void *InsertPos = 0; |
| 1520 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1521 | return QualType(VTP, 0); |
| 1522 | |
| 1523 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1524 | // so fill in the canonical type field. |
| 1525 | QualType Canonical; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1526 | if (!vecType.isCanonical() || IsAltiVec || IsPixel) { |
| 1527 | Canonical = getVectorType(getCanonicalType(vecType), |
| 1528 | NumElts, false, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1529 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1530 | // Get the new insert position for the node we care about. |
| 1531 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1532 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1533 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1534 | VectorType *New = new (*this, TypeAlignment) |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1535 | VectorType(vecType, NumElts, Canonical, IsAltiVec, IsPixel); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1536 | VectorTypes.InsertNode(New, InsertPos); |
| 1537 | Types.push_back(New); |
| 1538 | return QualType(New, 0); |
| 1539 | } |
| 1540 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1541 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1542 | /// 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] | 1543 | QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) { |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1544 | BuiltinType *baseType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1545 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1546 | baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1547 | assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1548 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1549 | // Check if we've already instantiated a vector of this type. |
| 1550 | llvm::FoldingSetNodeID ID; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1551 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, false, false); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1552 | void *InsertPos = 0; |
| 1553 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1554 | return QualType(VTP, 0); |
| 1555 | |
| 1556 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1557 | // so fill in the canonical type field. |
| 1558 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1559 | if (!vecType.isCanonical()) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1560 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1561 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1562 | // Get the new insert position for the node we care about. |
| 1563 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1564 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1565 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1566 | ExtVectorType *New = new (*this, TypeAlignment) |
| 1567 | ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1568 | VectorTypes.InsertNode(New, InsertPos); |
| 1569 | Types.push_back(New); |
| 1570 | return QualType(New, 0); |
| 1571 | } |
| 1572 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1573 | QualType ASTContext::getDependentSizedExtVectorType(QualType vecType, |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1574 | Expr *SizeExpr, |
| 1575 | SourceLocation AttrLoc) { |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1576 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1577 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1578 | SizeExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1579 | |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1580 | void *InsertPos = 0; |
| 1581 | DependentSizedExtVectorType *Canon |
| 1582 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1583 | DependentSizedExtVectorType *New; |
| 1584 | if (Canon) { |
| 1585 | // We already have a canonical version of this array type; use it as |
| 1586 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1587 | New = new (*this, TypeAlignment) |
| 1588 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), |
| 1589 | SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1590 | } else { |
| 1591 | QualType CanonVecTy = getCanonicalType(vecType); |
| 1592 | if (CanonVecTy == vecType) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1593 | New = new (*this, TypeAlignment) |
| 1594 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, |
| 1595 | AttrLoc); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1596 | |
| 1597 | DependentSizedExtVectorType *CanonCheck |
| 1598 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1599 | assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken"); |
| 1600 | (void)CanonCheck; |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1601 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); |
| 1602 | } else { |
| 1603 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 1604 | SourceLocation()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1605 | New = new (*this, TypeAlignment) |
| 1606 | DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1607 | } |
| 1608 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1609 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1610 | Types.push_back(New); |
| 1611 | return QualType(New, 0); |
| 1612 | } |
| 1613 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1614 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1615 | /// |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1616 | QualType ASTContext::getFunctionNoProtoType(QualType ResultTy, |
| 1617 | const FunctionType::ExtInfo &Info) { |
| 1618 | const CallingConv CallConv = Info.getCC(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1619 | // Unique functions, to guarantee there is only one function of a particular |
| 1620 | // structure. |
| 1621 | llvm::FoldingSetNodeID ID; |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1622 | FunctionNoProtoType::Profile(ID, ResultTy, Info); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1623 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1624 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1625 | if (FunctionNoProtoType *FT = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1626 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1627 | return QualType(FT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1628 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1629 | QualType Canonical; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1630 | if (!ResultTy.isCanonical() || |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1631 | getCanonicalCallConv(CallConv) != CallConv) { |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1632 | Canonical = |
| 1633 | getFunctionNoProtoType(getCanonicalType(ResultTy), |
| 1634 | Info.withCallingConv(getCanonicalCallConv(CallConv))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1635 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1636 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1637 | FunctionNoProtoType *NewIP = |
| 1638 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1639 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1640 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1641 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1642 | FunctionNoProtoType *New = new (*this, TypeAlignment) |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1643 | FunctionNoProtoType(ResultTy, Canonical, Info); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1644 | Types.push_back(New); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1645 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1646 | return QualType(New, 0); |
| 1647 | } |
| 1648 | |
| 1649 | /// getFunctionType - Return a normal function type with a typed argument |
| 1650 | /// list. isVariadic indicates whether the argument list includes '...'. |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 1651 | QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray, |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1652 | unsigned NumArgs, bool isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1653 | unsigned TypeQuals, bool hasExceptionSpec, |
| 1654 | bool hasAnyExceptionSpec, unsigned NumExs, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1655 | const QualType *ExArray, |
| 1656 | const FunctionType::ExtInfo &Info) { |
| 1657 | const CallingConv CallConv= Info.getCC(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1658 | // Unique functions, to guarantee there is only one function of a particular |
| 1659 | // structure. |
| 1660 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1661 | FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1662 | TypeQuals, hasExceptionSpec, hasAnyExceptionSpec, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1663 | NumExs, ExArray, Info); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1664 | |
| 1665 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1666 | if (FunctionProtoType *FTP = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1667 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1668 | return QualType(FTP, 0); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1669 | |
| 1670 | // Determine whether the type being created is already canonical or not. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1671 | bool isCanonical = !hasExceptionSpec && ResultTy.isCanonical(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1672 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1673 | if (!ArgArray[i].isCanonicalAsParam()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1674 | isCanonical = false; |
| 1675 | |
| 1676 | // If this type isn't canonical, get the canonical version of it. |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1677 | // The exception spec is not part of the canonical type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1678 | QualType Canonical; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1679 | if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1680 | llvm::SmallVector<QualType, 16> CanonicalArgs; |
| 1681 | CanonicalArgs.reserve(NumArgs); |
| 1682 | for (unsigned i = 0; i != NumArgs; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1683 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1684 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1685 | Canonical = getFunctionType(getCanonicalType(ResultTy), |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1686 | CanonicalArgs.data(), NumArgs, |
Douglas Gregor | 47259d9 | 2009-08-05 19:03:35 +0000 | [diff] [blame] | 1687 | isVariadic, TypeQuals, false, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1688 | false, 0, 0, |
| 1689 | Info.withCallingConv(getCanonicalCallConv(CallConv))); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1690 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1691 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1692 | FunctionProtoType *NewIP = |
| 1693 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1694 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1695 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1696 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1697 | // FunctionProtoType objects are allocated with extra bytes after them |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1698 | // for two variable size arrays (for parameter and exception types) at the |
| 1699 | // end of them. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1700 | FunctionProtoType *FTP = |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1701 | (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) + |
| 1702 | NumArgs*sizeof(QualType) + |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1703 | NumExs*sizeof(QualType), TypeAlignment); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1704 | new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1705 | TypeQuals, hasExceptionSpec, hasAnyExceptionSpec, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1706 | ExArray, NumExs, Canonical, Info); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1707 | Types.push_back(FTP); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1708 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1709 | return QualType(FTP, 0); |
| 1710 | } |
| 1711 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1712 | #ifndef NDEBUG |
| 1713 | static bool NeedsInjectedClassNameType(const RecordDecl *D) { |
| 1714 | if (!isa<CXXRecordDecl>(D)) return false; |
| 1715 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 1716 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) |
| 1717 | return true; |
| 1718 | if (RD->getDescribedClassTemplate() && |
| 1719 | !isa<ClassTemplateSpecializationDecl>(RD)) |
| 1720 | return true; |
| 1721 | return false; |
| 1722 | } |
| 1723 | #endif |
| 1724 | |
| 1725 | /// getInjectedClassNameType - Return the unique reference to the |
| 1726 | /// injected class name type for the specified templated declaration. |
| 1727 | QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl, |
| 1728 | QualType TST) { |
| 1729 | assert(NeedsInjectedClassNameType(Decl)); |
| 1730 | if (Decl->TypeForDecl) { |
| 1731 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
| 1732 | } else if (CXXRecordDecl *PrevDecl |
| 1733 | = cast_or_null<CXXRecordDecl>(Decl->getPreviousDeclaration())) { |
| 1734 | assert(PrevDecl->TypeForDecl && "previous declaration has no type"); |
| 1735 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 1736 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
| 1737 | } else { |
| 1738 | Decl->TypeForDecl = new (*this, TypeAlignment) |
| 1739 | InjectedClassNameType(Decl, TST, TST->getCanonicalTypeInternal()); |
| 1740 | Types.push_back(Decl->TypeForDecl); |
| 1741 | } |
| 1742 | return QualType(Decl->TypeForDecl, 0); |
| 1743 | } |
| 1744 | |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1745 | /// getTypeDeclType - Return the unique reference to the type for the |
| 1746 | /// specified type declaration. |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1747 | QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) { |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 1748 | assert(Decl && "Passed null for Decl param"); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1749 | assert(!Decl->TypeForDecl && "TypeForDecl present in slow case"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1750 | |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1751 | if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1752 | return getTypedefType(Typedef); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1753 | |
| 1754 | if (const ObjCInterfaceDecl *ObjCInterface |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 1755 | = dyn_cast<ObjCInterfaceDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1756 | return getObjCInterfaceType(ObjCInterface); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1757 | |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1758 | assert(!isa<TemplateTypeParmDecl>(Decl) && |
| 1759 | "Template type parameter types are always available."); |
| 1760 | |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1761 | if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1762 | assert(!Record->getPreviousDeclaration() && |
| 1763 | "struct/union has previous declaration"); |
| 1764 | assert(!NeedsInjectedClassNameType(Record)); |
| 1765 | Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Record); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1766 | } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1767 | assert(!Enum->getPreviousDeclaration() && |
| 1768 | "enum has previous declaration"); |
| 1769 | Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Enum); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1770 | } else if (const UnresolvedUsingTypenameDecl *Using = |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1771 | dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) { |
| 1772 | Decl->TypeForDecl = new (*this, TypeAlignment) UnresolvedUsingType(Using); |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 1773 | } else |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1774 | llvm_unreachable("TypeDecl without a type?"); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1775 | |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 1776 | Types.push_back(Decl->TypeForDecl); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1777 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1778 | } |
| 1779 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1780 | /// getTypedefType - Return the unique reference to the type for the |
| 1781 | /// specified typename decl. |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1782 | QualType ASTContext::getTypedefType(const TypedefDecl *Decl) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1783 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1784 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1785 | QualType Canonical = getCanonicalType(Decl->getUnderlyingType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1786 | Decl->TypeForDecl = new(*this, TypeAlignment) |
| 1787 | TypedefType(Type::Typedef, Decl, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1788 | Types.push_back(Decl->TypeForDecl); |
| 1789 | return QualType(Decl->TypeForDecl, 0); |
| 1790 | } |
| 1791 | |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1792 | /// \brief Retrieve a substitution-result type. |
| 1793 | QualType |
| 1794 | ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, |
| 1795 | QualType Replacement) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1796 | assert(Replacement.isCanonical() |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1797 | && "replacement types must always be canonical"); |
| 1798 | |
| 1799 | llvm::FoldingSetNodeID ID; |
| 1800 | SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); |
| 1801 | void *InsertPos = 0; |
| 1802 | SubstTemplateTypeParmType *SubstParm |
| 1803 | = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1804 | |
| 1805 | if (!SubstParm) { |
| 1806 | SubstParm = new (*this, TypeAlignment) |
| 1807 | SubstTemplateTypeParmType(Parm, Replacement); |
| 1808 | Types.push_back(SubstParm); |
| 1809 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 1810 | } |
| 1811 | |
| 1812 | return QualType(SubstParm, 0); |
| 1813 | } |
| 1814 | |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1815 | /// \brief Retrieve the template type parameter type for a template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1816 | /// parameter or parameter pack with the given depth, index, and (optionally) |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1817 | /// name. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1818 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1819 | bool ParameterPack, |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1820 | IdentifierInfo *Name) { |
| 1821 | llvm::FoldingSetNodeID ID; |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1822 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1823 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1824 | TemplateTypeParmType *TypeParm |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1825 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1826 | |
| 1827 | if (TypeParm) |
| 1828 | return QualType(TypeParm, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1829 | |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1830 | if (Name) { |
| 1831 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1832 | TypeParm = new (*this, TypeAlignment) |
| 1833 | TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1834 | |
| 1835 | TemplateTypeParmType *TypeCheck |
| 1836 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1837 | assert(!TypeCheck && "Template type parameter canonical type broken"); |
| 1838 | (void)TypeCheck; |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1839 | } else |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1840 | TypeParm = new (*this, TypeAlignment) |
| 1841 | TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1842 | |
| 1843 | Types.push_back(TypeParm); |
| 1844 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 1845 | |
| 1846 | return QualType(TypeParm, 0); |
| 1847 | } |
| 1848 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1849 | TypeSourceInfo * |
| 1850 | ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name, |
| 1851 | SourceLocation NameLoc, |
| 1852 | const TemplateArgumentListInfo &Args, |
| 1853 | QualType CanonType) { |
| 1854 | QualType TST = getTemplateSpecializationType(Name, Args, CanonType); |
| 1855 | |
| 1856 | TypeSourceInfo *DI = CreateTypeSourceInfo(TST); |
| 1857 | TemplateSpecializationTypeLoc TL |
| 1858 | = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc()); |
| 1859 | TL.setTemplateNameLoc(NameLoc); |
| 1860 | TL.setLAngleLoc(Args.getLAngleLoc()); |
| 1861 | TL.setRAngleLoc(Args.getRAngleLoc()); |
| 1862 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 1863 | TL.setArgLocInfo(i, Args[i].getLocInfo()); |
| 1864 | return DI; |
| 1865 | } |
| 1866 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1867 | QualType |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1868 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1869 | const TemplateArgumentListInfo &Args, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1870 | QualType Canon) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1871 | unsigned NumArgs = Args.size(); |
| 1872 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1873 | llvm::SmallVector<TemplateArgument, 4> ArgVec; |
| 1874 | ArgVec.reserve(NumArgs); |
| 1875 | for (unsigned i = 0; i != NumArgs; ++i) |
| 1876 | ArgVec.push_back(Args[i].getArgument()); |
| 1877 | |
| 1878 | return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, Canon); |
| 1879 | } |
| 1880 | |
| 1881 | QualType |
| 1882 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1883 | const TemplateArgument *Args, |
| 1884 | unsigned NumArgs, |
| 1885 | QualType Canon) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1886 | if (!Canon.isNull()) |
| 1887 | Canon = getCanonicalType(Canon); |
| 1888 | else { |
| 1889 | // Build the canonical template specialization type. |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1890 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); |
| 1891 | llvm::SmallVector<TemplateArgument, 4> CanonArgs; |
| 1892 | CanonArgs.reserve(NumArgs); |
| 1893 | for (unsigned I = 0; I != NumArgs; ++I) |
| 1894 | CanonArgs.push_back(getCanonicalTemplateArgument(Args[I])); |
| 1895 | |
| 1896 | // Determine whether this canonical template specialization type already |
| 1897 | // exists. |
| 1898 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1899 | TemplateSpecializationType::Profile(ID, CanonTemplate, |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 1900 | CanonArgs.data(), NumArgs, *this); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1901 | |
| 1902 | void *InsertPos = 0; |
| 1903 | TemplateSpecializationType *Spec |
| 1904 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1905 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1906 | if (!Spec) { |
| 1907 | // Allocate a new canonical template specialization type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1908 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1909 | sizeof(TemplateArgument) * NumArgs), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1910 | TypeAlignment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1911 | Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate, |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1912 | CanonArgs.data(), NumArgs, |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1913 | Canon); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1914 | Types.push_back(Spec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1915 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1916 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1917 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1918 | if (Canon.isNull()) |
| 1919 | Canon = QualType(Spec, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1920 | assert(Canon->isDependentType() && |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1921 | "Non-dependent template-id type must have a canonical type"); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1922 | } |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 1923 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1924 | // Allocate the (non-canonical) template specialization type, but don't |
| 1925 | // try to unique it: these types typically have location information that |
| 1926 | // we don't unique and don't want to lose. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1927 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1928 | sizeof(TemplateArgument) * NumArgs), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1929 | TypeAlignment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1930 | TemplateSpecializationType *Spec |
| 1931 | = new (Mem) TemplateSpecializationType(*this, Template, Args, NumArgs, |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 1932 | Canon); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1933 | |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1934 | Types.push_back(Spec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1935 | return QualType(Spec, 0); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1936 | } |
| 1937 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1938 | QualType |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1939 | ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS, |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1940 | QualType NamedType) { |
| 1941 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1942 | QualifiedNameType::Profile(ID, NNS, NamedType); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1943 | |
| 1944 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1945 | QualifiedNameType *T |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1946 | = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1947 | if (T) |
| 1948 | return QualType(T, 0); |
| 1949 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1950 | QualType Canon = NamedType; |
| 1951 | if (!Canon.isCanonical()) { |
| 1952 | Canon = getCanonicalType(NamedType); |
| 1953 | QualifiedNameType *CheckT |
| 1954 | = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1955 | assert(!CheckT && "Qualified name canonical type broken"); |
| 1956 | (void)CheckT; |
| 1957 | } |
| 1958 | |
| 1959 | T = new (*this) QualifiedNameType(NNS, NamedType, Canon); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1960 | Types.push_back(T); |
| 1961 | QualifiedNameTypes.InsertNode(T, InsertPos); |
| 1962 | return QualType(T, 0); |
| 1963 | } |
| 1964 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 1965 | QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, |
| 1966 | NestedNameSpecifier *NNS, |
| 1967 | const IdentifierInfo *Name, |
| 1968 | QualType Canon) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1969 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 1970 | |
| 1971 | if (Canon.isNull()) { |
| 1972 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 1973 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 1974 | if (Keyword == ETK_None) |
| 1975 | CanonKeyword = ETK_Typename; |
| 1976 | |
| 1977 | if (CanonNNS != NNS || CanonKeyword != Keyword) |
| 1978 | Canon = getDependentNameType(CanonKeyword, CanonNNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1979 | } |
| 1980 | |
| 1981 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 1982 | DependentNameType::Profile(ID, Keyword, NNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1983 | |
| 1984 | void *InsertPos = 0; |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 1985 | DependentNameType *T |
| 1986 | = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1987 | if (T) |
| 1988 | return QualType(T, 0); |
| 1989 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 1990 | T = new (*this) DependentNameType(Keyword, NNS, Name, Canon); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1991 | Types.push_back(T); |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 1992 | DependentNameTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1993 | return QualType(T, 0); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1994 | } |
| 1995 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1996 | QualType |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 1997 | ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, |
| 1998 | NestedNameSpecifier *NNS, |
| 1999 | const TemplateSpecializationType *TemplateId, |
| 2000 | QualType Canon) { |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2001 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 2002 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2003 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2004 | DependentNameType::Profile(ID, Keyword, NNS, TemplateId); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2005 | |
| 2006 | void *InsertPos = 0; |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2007 | DependentNameType *T |
| 2008 | = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2009 | if (T) |
| 2010 | return QualType(T, 0); |
| 2011 | |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2012 | if (Canon.isNull()) { |
| 2013 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 2014 | QualType CanonType = getCanonicalType(QualType(TemplateId, 0)); |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2015 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 2016 | if (Keyword == ETK_None) |
| 2017 | CanonKeyword = ETK_Typename; |
| 2018 | if (CanonNNS != NNS || CanonKeyword != Keyword || |
| 2019 | CanonType != QualType(TemplateId, 0)) { |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2020 | const TemplateSpecializationType *CanonTemplateId |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2021 | = CanonType->getAs<TemplateSpecializationType>(); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2022 | assert(CanonTemplateId && |
| 2023 | "Canonical type must also be a template specialization type"); |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2024 | Canon = getDependentNameType(CanonKeyword, CanonNNS, CanonTemplateId); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2025 | } |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2026 | |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2027 | DependentNameType *CheckT |
| 2028 | = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2029 | assert(!CheckT && "Typename canonical type is broken"); (void)CheckT; |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2030 | } |
| 2031 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2032 | T = new (*this) DependentNameType(Keyword, NNS, TemplateId, Canon); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2033 | Types.push_back(T); |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2034 | DependentNameTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2035 | return QualType(T, 0); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2036 | } |
| 2037 | |
John McCall | 7da2431 | 2009-09-05 00:15:47 +0000 | [diff] [blame] | 2038 | QualType |
| 2039 | ASTContext::getElaboratedType(QualType UnderlyingType, |
| 2040 | ElaboratedType::TagKind Tag) { |
| 2041 | llvm::FoldingSetNodeID ID; |
| 2042 | ElaboratedType::Profile(ID, UnderlyingType, Tag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2043 | |
John McCall | 7da2431 | 2009-09-05 00:15:47 +0000 | [diff] [blame] | 2044 | void *InsertPos = 0; |
| 2045 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2046 | if (T) |
| 2047 | return QualType(T, 0); |
| 2048 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2049 | QualType Canon = UnderlyingType; |
| 2050 | if (!Canon.isCanonical()) { |
| 2051 | Canon = getCanonicalType(Canon); |
| 2052 | ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2053 | assert(!CheckT && "Elaborated canonical type is broken"); (void)CheckT; |
| 2054 | } |
John McCall | 7da2431 | 2009-09-05 00:15:47 +0000 | [diff] [blame] | 2055 | |
| 2056 | T = new (*this) ElaboratedType(UnderlyingType, Tag, Canon); |
| 2057 | Types.push_back(T); |
| 2058 | ElaboratedTypes.InsertNode(T, InsertPos); |
| 2059 | return QualType(T, 0); |
| 2060 | } |
| 2061 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2062 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 2063 | /// alphabetically. |
| 2064 | static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, |
| 2065 | const ObjCProtocolDecl *RHS) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2066 | return LHS->getDeclName() < RHS->getDeclName(); |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2067 | } |
| 2068 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2069 | static bool areSortedAndUniqued(ObjCProtocolDecl **Protocols, |
| 2070 | unsigned NumProtocols) { |
| 2071 | if (NumProtocols == 0) return true; |
| 2072 | |
| 2073 | for (unsigned i = 1; i != NumProtocols; ++i) |
| 2074 | if (!CmpProtocolNames(Protocols[i-1], Protocols[i])) |
| 2075 | return false; |
| 2076 | return true; |
| 2077 | } |
| 2078 | |
| 2079 | static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols, |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2080 | unsigned &NumProtocols) { |
| 2081 | ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2082 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2083 | // Sort protocols, keyed by name. |
| 2084 | std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); |
| 2085 | |
| 2086 | // Remove duplicates. |
| 2087 | ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); |
| 2088 | NumProtocols = ProtocolsEnd-Protocols; |
| 2089 | } |
| 2090 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2091 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 2092 | /// the given interface decl and the conforming protocol list. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2093 | QualType ASTContext::getObjCObjectPointerType(QualType InterfaceT, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2094 | ObjCProtocolDecl **Protocols, |
Fariborz Jahanian | a422864 | 2010-03-05 22:42:55 +0000 | [diff] [blame] | 2095 | unsigned NumProtocols, |
| 2096 | unsigned Quals) { |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2097 | llvm::FoldingSetNodeID ID; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2098 | ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols); |
Fariborz Jahanian | a422864 | 2010-03-05 22:42:55 +0000 | [diff] [blame] | 2099 | Qualifiers Qs = Qualifiers::fromCVRMask(Quals); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2100 | |
| 2101 | void *InsertPos = 0; |
| 2102 | if (ObjCObjectPointerType *QT = |
| 2103 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Fariborz Jahanian | a422864 | 2010-03-05 22:42:55 +0000 | [diff] [blame] | 2104 | return getQualifiedType(QualType(QT, 0), Qs); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2105 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2106 | // Sort the protocol list alphabetically to canonicalize it. |
| 2107 | QualType Canonical; |
| 2108 | if (!InterfaceT.isCanonical() || |
| 2109 | !areSortedAndUniqued(Protocols, NumProtocols)) { |
| 2110 | if (!areSortedAndUniqued(Protocols, NumProtocols)) { |
| 2111 | llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(NumProtocols); |
| 2112 | unsigned UniqueCount = NumProtocols; |
| 2113 | |
| 2114 | std::copy(Protocols, Protocols + NumProtocols, Sorted.begin()); |
| 2115 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
| 2116 | |
| 2117 | Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT), |
| 2118 | &Sorted[0], UniqueCount); |
| 2119 | } else { |
| 2120 | Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT), |
| 2121 | Protocols, NumProtocols); |
| 2122 | } |
| 2123 | |
| 2124 | // Regenerate InsertPos. |
| 2125 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2126 | } |
| 2127 | |
Douglas Gregor | fd6a088 | 2010-02-08 22:59:26 +0000 | [diff] [blame] | 2128 | // No match. |
| 2129 | unsigned Size = sizeof(ObjCObjectPointerType) |
| 2130 | + NumProtocols * sizeof(ObjCProtocolDecl *); |
| 2131 | void *Mem = Allocate(Size, TypeAlignment); |
| 2132 | ObjCObjectPointerType *QType = new (Mem) ObjCObjectPointerType(Canonical, |
| 2133 | InterfaceT, |
| 2134 | Protocols, |
| 2135 | NumProtocols); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2136 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2137 | Types.push_back(QType); |
| 2138 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
Fariborz Jahanian | a422864 | 2010-03-05 22:42:55 +0000 | [diff] [blame] | 2139 | return getQualifiedType(QualType(QType, 0), Qs); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2140 | } |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2141 | |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 2142 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 2143 | /// specified ObjC interface decl. The list of protocols is optional. |
| 2144 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl, |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2145 | ObjCProtocolDecl **Protocols, unsigned NumProtocols) { |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2146 | llvm::FoldingSetNodeID ID; |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 2147 | ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2148 | |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2149 | void *InsertPos = 0; |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 2150 | if (ObjCInterfaceType *QT = |
| 2151 | ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2152 | return QualType(QT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2153 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2154 | // Sort the protocol list alphabetically to canonicalize it. |
| 2155 | QualType Canonical; |
| 2156 | if (NumProtocols && !areSortedAndUniqued(Protocols, NumProtocols)) { |
| 2157 | llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(NumProtocols); |
| 2158 | std::copy(Protocols, Protocols + NumProtocols, Sorted.begin()); |
| 2159 | |
| 2160 | unsigned UniqueCount = NumProtocols; |
| 2161 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
| 2162 | |
| 2163 | Canonical = getObjCInterfaceType(Decl, &Sorted[0], UniqueCount); |
| 2164 | |
| 2165 | ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2166 | } |
| 2167 | |
Douglas Gregor | fd6a088 | 2010-02-08 22:59:26 +0000 | [diff] [blame] | 2168 | unsigned Size = sizeof(ObjCInterfaceType) |
| 2169 | + NumProtocols * sizeof(ObjCProtocolDecl *); |
| 2170 | void *Mem = Allocate(Size, TypeAlignment); |
| 2171 | ObjCInterfaceType *QType = new (Mem) ObjCInterfaceType(Canonical, |
| 2172 | const_cast<ObjCInterfaceDecl*>(Decl), |
| 2173 | Protocols, |
| 2174 | NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2175 | |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2176 | Types.push_back(QType); |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 2177 | ObjCInterfaceTypes.InsertNode(QType, InsertPos); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2178 | return QualType(QType, 0); |
| 2179 | } |
| 2180 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2181 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 2182 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2183 | /// multiple declarations that refer to "typeof(x)" all contain different |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2184 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2185 | /// on canonical type's (which are always unique). |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2186 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2187 | TypeOfExprType *toe; |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2188 | if (tofExpr->isTypeDependent()) { |
| 2189 | llvm::FoldingSetNodeID ID; |
| 2190 | DependentTypeOfExprType::Profile(ID, *this, tofExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2191 | |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2192 | void *InsertPos = 0; |
| 2193 | DependentTypeOfExprType *Canon |
| 2194 | = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2195 | if (Canon) { |
| 2196 | // We already have a "canonical" version of an identical, dependent |
| 2197 | // typeof(expr) type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2198 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2199 | QualType((TypeOfExprType*)Canon, 0)); |
| 2200 | } |
| 2201 | else { |
| 2202 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2203 | Canon |
| 2204 | = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2205 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); |
| 2206 | toe = Canon; |
| 2207 | } |
| 2208 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2209 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2210 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2211 | } |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2212 | Types.push_back(toe); |
| 2213 | return QualType(toe, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2214 | } |
| 2215 | |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2216 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 2217 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 2218 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2219 | /// an issue. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2220 | /// on canonical type's (which are always unique). |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2221 | QualType ASTContext::getTypeOfType(QualType tofType) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2222 | QualType Canonical = getCanonicalType(tofType); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2223 | TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2224 | Types.push_back(tot); |
| 2225 | return QualType(tot, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2226 | } |
| 2227 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2228 | /// getDecltypeForExpr - Given an expr, will return the decltype for that |
| 2229 | /// expression, according to the rules in C++0x [dcl.type.simple]p4 |
| 2230 | static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) { |
Anders Carlsson | a07c33e | 2009-06-25 15:00:34 +0000 | [diff] [blame] | 2231 | if (e->isTypeDependent()) |
| 2232 | return Context.DependentTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2233 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2234 | // If e is an id expression or a class member access, decltype(e) is defined |
| 2235 | // as the type of the entity named by e. |
| 2236 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) { |
| 2237 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) |
| 2238 | return VD->getType(); |
| 2239 | } |
| 2240 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) { |
| 2241 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) |
| 2242 | return FD->getType(); |
| 2243 | } |
| 2244 | // If e is a function call or an invocation of an overloaded operator, |
| 2245 | // (parentheses around e are ignored), decltype(e) is defined as the |
| 2246 | // return type of that function. |
| 2247 | if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens())) |
| 2248 | return CE->getCallReturnType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2249 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2250 | QualType T = e->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2251 | |
| 2252 | // 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] | 2253 | // defined as T&, otherwise decltype(e) is defined as T. |
| 2254 | if (e->isLvalue(Context) == Expr::LV_Valid) |
| 2255 | T = Context.getLValueReferenceType(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2256 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2257 | return T; |
| 2258 | } |
| 2259 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2260 | /// getDecltypeType - Unlike many "get<Type>" functions, we don't unique |
| 2261 | /// DecltypeType AST's. The only motivation to unique these nodes would be |
| 2262 | /// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2263 | /// an issue. This doesn't effect the type checker, since it operates |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2264 | /// on canonical type's (which are always unique). |
| 2265 | QualType ASTContext::getDecltypeType(Expr *e) { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2266 | DecltypeType *dt; |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2267 | if (e->isTypeDependent()) { |
| 2268 | llvm::FoldingSetNodeID ID; |
| 2269 | DependentDecltypeType::Profile(ID, *this, e); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2270 | |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2271 | void *InsertPos = 0; |
| 2272 | DependentDecltypeType *Canon |
| 2273 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2274 | if (Canon) { |
| 2275 | // We already have a "canonical" version of an equivalent, dependent |
| 2276 | // decltype type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2277 | dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy, |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2278 | QualType((DecltypeType*)Canon, 0)); |
| 2279 | } |
| 2280 | else { |
| 2281 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2282 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2283 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); |
| 2284 | dt = Canon; |
| 2285 | } |
| 2286 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2287 | QualType T = getDecltypeForExpr(e, *this); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2288 | dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T)); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2289 | } |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2290 | Types.push_back(dt); |
| 2291 | return QualType(dt, 0); |
| 2292 | } |
| 2293 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2294 | /// getTagDeclType - Return the unique reference to the type for the |
| 2295 | /// specified TagDecl (struct/union/class/enum) decl. |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 2296 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) { |
Ted Kremenek | d778f88 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 2297 | assert (Decl); |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 2298 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 2299 | // away const? mutable? |
| 2300 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2301 | } |
| 2302 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2303 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 2304 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 2305 | /// needs to agree with the definition in <stddef.h>. |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 2306 | CanQualType ASTContext::getSizeType() const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2307 | return getFromTargetType(Target.getSizeType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2308 | } |
| 2309 | |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 2310 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 2311 | /// Used when in C++, as a GCC extension. |
| 2312 | QualType ASTContext::getSignedWCharType() const { |
| 2313 | // FIXME: derive from "Target" ? |
| 2314 | return WCharTy; |
| 2315 | } |
| 2316 | |
| 2317 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 2318 | /// Used when in C++, as a GCC extension. |
| 2319 | QualType ASTContext::getUnsignedWCharType() const { |
| 2320 | // FIXME: derive from "Target" ? |
| 2321 | return UnsignedIntTy; |
| 2322 | } |
| 2323 | |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 2324 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) |
| 2325 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 2326 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2327 | return getFromTargetType(Target.getPtrDiffType(0)); |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 2328 | } |
| 2329 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2330 | //===----------------------------------------------------------------------===// |
| 2331 | // Type Operators |
| 2332 | //===----------------------------------------------------------------------===// |
| 2333 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2334 | CanQualType ASTContext::getCanonicalParamType(QualType T) { |
| 2335 | // Push qualifiers into arrays, and then discard any remaining |
| 2336 | // qualifiers. |
| 2337 | T = getCanonicalType(T); |
| 2338 | const Type *Ty = T.getTypePtr(); |
| 2339 | |
| 2340 | QualType Result; |
| 2341 | if (isa<ArrayType>(Ty)) { |
| 2342 | Result = getArrayDecayedType(QualType(Ty,0)); |
| 2343 | } else if (isa<FunctionType>(Ty)) { |
| 2344 | Result = getPointerType(QualType(Ty, 0)); |
| 2345 | } else { |
| 2346 | Result = QualType(Ty, 0); |
| 2347 | } |
| 2348 | |
| 2349 | return CanQualType::CreateUnsafe(Result); |
| 2350 | } |
| 2351 | |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 2352 | /// getCanonicalType - Return the canonical (structural) type corresponding to |
| 2353 | /// the specified potentially non-canonical type. The non-canonical version |
| 2354 | /// of a type may have many "decorated" versions of types. Decorators can |
| 2355 | /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed |
| 2356 | /// to be free of any of these, allowing two canonical types to be compared |
| 2357 | /// for exact equality with a simple pointer comparison. |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2358 | CanQualType ASTContext::getCanonicalType(QualType T) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2359 | QualifierCollector Quals; |
| 2360 | const Type *Ptr = Quals.strip(T); |
| 2361 | QualType CanType = Ptr->getCanonicalTypeInternal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2362 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2363 | // The canonical internal type will be the canonical type *except* |
| 2364 | // that we push type qualifiers down through array types. |
| 2365 | |
| 2366 | // If there are no new qualifiers to push down, stop here. |
| 2367 | if (!Quals.hasQualifiers()) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2368 | return CanQualType::CreateUnsafe(CanType); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2369 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2370 | // If the type qualifiers are on an array type, get the canonical |
| 2371 | // type of the array with the qualifiers applied to the element |
| 2372 | // type. |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2373 | ArrayType *AT = dyn_cast<ArrayType>(CanType); |
| 2374 | if (!AT) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2375 | return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2376 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2377 | // Get the canonical version of the element with the extra qualifiers on it. |
| 2378 | // This can recursively sink qualifiers through multiple levels of arrays. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2379 | QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2380 | NewEltTy = getCanonicalType(NewEltTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2381 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2382 | if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2383 | return CanQualType::CreateUnsafe( |
| 2384 | getConstantArrayType(NewEltTy, CAT->getSize(), |
| 2385 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2386 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2387 | if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2388 | return CanQualType::CreateUnsafe( |
| 2389 | getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2390 | IAT->getIndexTypeCVRQualifiers())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2391 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2392 | if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2393 | return CanQualType::CreateUnsafe( |
| 2394 | getDependentSizedArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2395 | DSAT->getSizeExpr() ? |
| 2396 | DSAT->getSizeExpr()->Retain() : 0, |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2397 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2398 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 87a924e | 2009-10-30 22:56:57 +0000 | [diff] [blame] | 2399 | DSAT->getBracketsRange())->getCanonicalTypeInternal()); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2400 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2401 | VariableArrayType *VAT = cast<VariableArrayType>(AT); |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2402 | return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2403 | VAT->getSizeExpr() ? |
| 2404 | VAT->getSizeExpr()->Retain() : 0, |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2405 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2406 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2407 | VAT->getBracketsRange())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2408 | } |
| 2409 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2410 | QualType ASTContext::getUnqualifiedArrayType(QualType T, |
| 2411 | Qualifiers &Quals) { |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2412 | Quals = T.getQualifiers(); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2413 | if (!isa<ArrayType>(T)) { |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2414 | return T.getUnqualifiedType(); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2415 | } |
| 2416 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2417 | const ArrayType *AT = cast<ArrayType>(T); |
| 2418 | QualType Elt = AT->getElementType(); |
Zhongxing Xu | c1ae0a8 | 2010-01-05 08:15:06 +0000 | [diff] [blame] | 2419 | QualType UnqualElt = getUnqualifiedArrayType(Elt, Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2420 | if (Elt == UnqualElt) |
| 2421 | return T; |
| 2422 | |
| 2423 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T)) { |
| 2424 | return getConstantArrayType(UnqualElt, CAT->getSize(), |
| 2425 | CAT->getSizeModifier(), 0); |
| 2426 | } |
| 2427 | |
| 2428 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(T)) { |
| 2429 | return getIncompleteArrayType(UnqualElt, IAT->getSizeModifier(), 0); |
| 2430 | } |
| 2431 | |
| 2432 | const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(T); |
| 2433 | return getDependentSizedArrayType(UnqualElt, DSAT->getSizeExpr()->Retain(), |
| 2434 | DSAT->getSizeModifier(), 0, |
| 2435 | SourceRange()); |
| 2436 | } |
| 2437 | |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2438 | DeclarationName ASTContext::getNameForTemplate(TemplateName Name) { |
| 2439 | if (TemplateDecl *TD = Name.getAsTemplateDecl()) |
| 2440 | return TD->getDeclName(); |
| 2441 | |
| 2442 | if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) { |
| 2443 | if (DTN->isIdentifier()) { |
| 2444 | return DeclarationNames.getIdentifier(DTN->getIdentifier()); |
| 2445 | } else { |
| 2446 | return DeclarationNames.getCXXOperatorName(DTN->getOperator()); |
| 2447 | } |
| 2448 | } |
| 2449 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 2450 | OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate(); |
| 2451 | assert(Storage); |
| 2452 | return (*Storage->begin())->getDeclName(); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2453 | } |
| 2454 | |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2455 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) { |
| 2456 | // If this template name refers to a template, the canonical |
| 2457 | // template name merely stores the template itself. |
| 2458 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 2459 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2460 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 2461 | assert(!Name.getAsOverloadedTemplate()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2462 | |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2463 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 2464 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 2465 | return DTN->CanonicalTemplateName; |
| 2466 | } |
| 2467 | |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 2468 | bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) { |
| 2469 | X = getCanonicalTemplateName(X); |
| 2470 | Y = getCanonicalTemplateName(Y); |
| 2471 | return X.getAsVoidPointer() == Y.getAsVoidPointer(); |
| 2472 | } |
| 2473 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2474 | TemplateArgument |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2475 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) { |
| 2476 | switch (Arg.getKind()) { |
| 2477 | case TemplateArgument::Null: |
| 2478 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2479 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2480 | case TemplateArgument::Expression: |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2481 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2482 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2483 | case TemplateArgument::Declaration: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2484 | return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2485 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2486 | case TemplateArgument::Template: |
| 2487 | return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate())); |
| 2488 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2489 | case TemplateArgument::Integral: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2490 | return TemplateArgument(*Arg.getAsIntegral(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2491 | getCanonicalType(Arg.getIntegralType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2492 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2493 | case TemplateArgument::Type: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2494 | return TemplateArgument(getCanonicalType(Arg.getAsType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2495 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2496 | case TemplateArgument::Pack: { |
| 2497 | // FIXME: Allocate in ASTContext |
| 2498 | TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()]; |
| 2499 | unsigned Idx = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2500 | for (TemplateArgument::pack_iterator A = Arg.pack_begin(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2501 | AEnd = Arg.pack_end(); |
| 2502 | A != AEnd; (void)++A, ++Idx) |
| 2503 | CanonArgs[Idx] = getCanonicalTemplateArgument(*A); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2504 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2505 | TemplateArgument Result; |
| 2506 | Result.setArgumentPack(CanonArgs, Arg.pack_size(), false); |
| 2507 | return Result; |
| 2508 | } |
| 2509 | } |
| 2510 | |
| 2511 | // Silence GCC warning |
| 2512 | assert(false && "Unhandled template argument kind"); |
| 2513 | return TemplateArgument(); |
| 2514 | } |
| 2515 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2516 | NestedNameSpecifier * |
| 2517 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2518 | if (!NNS) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2519 | return 0; |
| 2520 | |
| 2521 | switch (NNS->getKind()) { |
| 2522 | case NestedNameSpecifier::Identifier: |
| 2523 | // Canonicalize the prefix but keep the identifier the same. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2524 | return NestedNameSpecifier::Create(*this, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2525 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 2526 | NNS->getAsIdentifier()); |
| 2527 | |
| 2528 | case NestedNameSpecifier::Namespace: |
| 2529 | // A namespace is canonical; build a nested-name-specifier with |
| 2530 | // this namespace and no prefix. |
| 2531 | return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace()); |
| 2532 | |
| 2533 | case NestedNameSpecifier::TypeSpec: |
| 2534 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 2535 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2536 | return NestedNameSpecifier::Create(*this, 0, |
| 2537 | NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2538 | T.getTypePtr()); |
| 2539 | } |
| 2540 | |
| 2541 | case NestedNameSpecifier::Global: |
| 2542 | // The global specifier is canonical and unique. |
| 2543 | return NNS; |
| 2544 | } |
| 2545 | |
| 2546 | // Required to silence a GCC warning |
| 2547 | return 0; |
| 2548 | } |
| 2549 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2550 | |
| 2551 | const ArrayType *ASTContext::getAsArrayType(QualType T) { |
| 2552 | // Handle the non-qualified case efficiently. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2553 | if (!T.hasLocalQualifiers()) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2554 | // Handle the common positive case fast. |
| 2555 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 2556 | return AT; |
| 2557 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2558 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2559 | // Handle the common negative case fast. |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2560 | QualType CType = T->getCanonicalTypeInternal(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2561 | if (!isa<ArrayType>(CType)) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2562 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2563 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2564 | // Apply any qualifiers from the array type to the element type. This |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2565 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 2566 | // 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] | 2567 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2568 | // If we get here, we either have type qualifiers on the type, or we have |
| 2569 | // 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] | 2570 | // we must propagate them down into the element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2571 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2572 | QualifierCollector Qs; |
| 2573 | const Type *Ty = Qs.strip(T.getDesugaredType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2574 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2575 | // If we have a simple case, just return now. |
| 2576 | const ArrayType *ATy = dyn_cast<ArrayType>(Ty); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2577 | if (ATy == 0 || Qs.empty()) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2578 | return ATy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2579 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2580 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 2581 | // qualifiers into the array element type and return a new array type. |
| 2582 | // Get the canonical version of the element with the extra qualifiers on it. |
| 2583 | // This can recursively sink qualifiers through multiple levels of arrays. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2584 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2585 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2586 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 2587 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 2588 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2589 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2590 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 2591 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 2592 | IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2593 | IAT->getIndexTypeCVRQualifiers())); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2594 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2595 | if (const DependentSizedArrayType *DSAT |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2596 | = dyn_cast<DependentSizedArrayType>(ATy)) |
| 2597 | return cast<ArrayType>( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2598 | getDependentSizedArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2599 | DSAT->getSizeExpr() ? |
| 2600 | DSAT->getSizeExpr()->Retain() : 0, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2601 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2602 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2603 | DSAT->getBracketsRange())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2604 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2605 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2606 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2607 | VAT->getSizeExpr() ? |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2608 | VAT->getSizeExpr()->Retain() : 0, |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2609 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2610 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2611 | VAT->getBracketsRange())); |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 2612 | } |
| 2613 | |
| 2614 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2615 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 2616 | /// specified array type to a pointer. This operation is non-trivial when |
| 2617 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 2618 | /// this returns a pointer to a properly qualified element of the array. |
| 2619 | /// |
| 2620 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
| 2621 | QualType ASTContext::getArrayDecayedType(QualType Ty) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2622 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 2623 | // typedefs in the element type of the array. This also handles propagation |
| 2624 | // of type qualifiers from the array type into the element type if present |
| 2625 | // (C99 6.7.3p8). |
| 2626 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 2627 | assert(PrettyArrayType && "Not an array type!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2628 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2629 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2630 | |
| 2631 | // int x[restrict 4] -> int *restrict |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2632 | return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2633 | } |
| 2634 | |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2635 | QualType ASTContext::getBaseElementType(QualType QT) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2636 | QualifierCollector Qs; |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2637 | while (true) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2638 | const Type *UT = Qs.strip(QT); |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2639 | if (const ArrayType *AT = getAsArrayType(QualType(UT,0))) { |
| 2640 | QT = AT->getElementType(); |
Mike Stump | 6dcbc29 | 2009-07-25 23:24:03 +0000 | [diff] [blame] | 2641 | } else { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2642 | return Qs.apply(QT); |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2643 | } |
| 2644 | } |
| 2645 | } |
| 2646 | |
Anders Carlsson | fbbce49 | 2009-09-25 01:23:32 +0000 | [diff] [blame] | 2647 | QualType ASTContext::getBaseElementType(const ArrayType *AT) { |
| 2648 | QualType ElemTy = AT->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2649 | |
Anders Carlsson | fbbce49 | 2009-09-25 01:23:32 +0000 | [diff] [blame] | 2650 | if (const ArrayType *AT = getAsArrayType(ElemTy)) |
| 2651 | return getBaseElementType(AT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2652 | |
Anders Carlsson | 6183a99 | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 2653 | return ElemTy; |
| 2654 | } |
| 2655 | |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 2656 | /// getConstantArrayElementCount - Returns number of constant array elements. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2657 | uint64_t |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 2658 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
| 2659 | uint64_t ElementCount = 1; |
| 2660 | do { |
| 2661 | ElementCount *= CA->getSize().getZExtValue(); |
| 2662 | CA = dyn_cast<ConstantArrayType>(CA->getElementType()); |
| 2663 | } while (CA); |
| 2664 | return ElementCount; |
| 2665 | } |
| 2666 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2667 | /// getFloatingRank - Return a relative rank for floating point types. |
| 2668 | /// 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] | 2669 | static FloatingRank getFloatingRank(QualType T) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2670 | if (const ComplexType *CT = T->getAs<ComplexType>()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2671 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2672 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2673 | assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"); |
| 2674 | switch (T->getAs<BuiltinType>()->getKind()) { |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2675 | default: assert(0 && "getFloatingRank(): not a floating type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2676 | case BuiltinType::Float: return FloatRank; |
| 2677 | case BuiltinType::Double: return DoubleRank; |
| 2678 | case BuiltinType::LongDouble: return LongDoubleRank; |
| 2679 | } |
| 2680 | } |
| 2681 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2682 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 2683 | /// point or a complex type (based on typeDomain/typeSize). |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 2684 | /// 'typeDomain' is a real floating point or complex type. |
| 2685 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 2686 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 2687 | QualType Domain) const { |
| 2688 | FloatingRank EltRank = getFloatingRank(Size); |
| 2689 | if (Domain->isComplexType()) { |
| 2690 | switch (EltRank) { |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 2691 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 2692 | case FloatRank: return FloatComplexTy; |
| 2693 | case DoubleRank: return DoubleComplexTy; |
| 2694 | case LongDoubleRank: return LongDoubleComplexTy; |
| 2695 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2696 | } |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 2697 | |
| 2698 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 2699 | switch (EltRank) { |
| 2700 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
| 2701 | case FloatRank: return FloatTy; |
| 2702 | case DoubleRank: return DoubleTy; |
| 2703 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 2704 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2705 | } |
| 2706 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2707 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 2708 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 2709 | /// '_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] | 2710 | /// LHS < RHS, return -1. |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2711 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) { |
| 2712 | FloatingRank LHSR = getFloatingRank(LHS); |
| 2713 | FloatingRank RHSR = getFloatingRank(RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2714 | |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2715 | if (LHSR == RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 2716 | return 0; |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2717 | if (LHSR > RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 2718 | return 1; |
| 2719 | return -1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2720 | } |
| 2721 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2722 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 2723 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 2724 | /// or if it is not canonicalized. |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2725 | unsigned ASTContext::getIntegerRank(Type *T) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2726 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2727 | if (EnumType* ET = dyn_cast<EnumType>(T)) |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 2728 | T = ET->getDecl()->getPromotionType().getTypePtr(); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2729 | |
Eli Friedman | a342675 | 2009-07-05 23:44:27 +0000 | [diff] [blame] | 2730 | if (T->isSpecificBuiltinType(BuiltinType::WChar)) |
| 2731 | T = getFromTargetType(Target.getWCharType()).getTypePtr(); |
| 2732 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2733 | if (T->isSpecificBuiltinType(BuiltinType::Char16)) |
| 2734 | T = getFromTargetType(Target.getChar16Type()).getTypePtr(); |
| 2735 | |
| 2736 | if (T->isSpecificBuiltinType(BuiltinType::Char32)) |
| 2737 | T = getFromTargetType(Target.getChar32Type()).getTypePtr(); |
| 2738 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2739 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2740 | default: assert(0 && "getIntegerRank(): not a built-in integer"); |
| 2741 | case BuiltinType::Bool: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2742 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2743 | case BuiltinType::Char_S: |
| 2744 | case BuiltinType::Char_U: |
| 2745 | case BuiltinType::SChar: |
| 2746 | case BuiltinType::UChar: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2747 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2748 | case BuiltinType::Short: |
| 2749 | case BuiltinType::UShort: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2750 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2751 | case BuiltinType::Int: |
| 2752 | case BuiltinType::UInt: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2753 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2754 | case BuiltinType::Long: |
| 2755 | case BuiltinType::ULong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2756 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2757 | case BuiltinType::LongLong: |
| 2758 | case BuiltinType::ULongLong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2759 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 2760 | case BuiltinType::Int128: |
| 2761 | case BuiltinType::UInt128: |
| 2762 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2763 | } |
| 2764 | } |
| 2765 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 2766 | /// \brief Whether this is a promotable bitfield reference according |
| 2767 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
| 2768 | /// |
| 2769 | /// \returns the type this bit-field will promote to, or NULL if no |
| 2770 | /// promotion occurs. |
| 2771 | QualType ASTContext::isPromotableBitField(Expr *E) { |
| 2772 | FieldDecl *Field = E->getBitField(); |
| 2773 | if (!Field) |
| 2774 | return QualType(); |
| 2775 | |
| 2776 | QualType FT = Field->getType(); |
| 2777 | |
| 2778 | llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this); |
| 2779 | uint64_t BitWidth = BitWidthAP.getZExtValue(); |
| 2780 | uint64_t IntSize = getTypeSize(IntTy); |
| 2781 | // GCC extension compatibility: if the bit-field size is less than or equal |
| 2782 | // to the size of int, it gets promoted no matter what its type is. |
| 2783 | // For instance, unsigned long bf : 4 gets promoted to signed int. |
| 2784 | if (BitWidth < IntSize) |
| 2785 | return IntTy; |
| 2786 | |
| 2787 | if (BitWidth == IntSize) |
| 2788 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
| 2789 | |
| 2790 | // Types bigger than int are not subject to promotions, and therefore act |
| 2791 | // like the base type. |
| 2792 | // FIXME: This doesn't quite match what gcc does, but what gcc does here |
| 2793 | // is ridiculous. |
| 2794 | return QualType(); |
| 2795 | } |
| 2796 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 2797 | /// getPromotedIntegerType - Returns the type that Promotable will |
| 2798 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
| 2799 | /// integer type. |
| 2800 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) { |
| 2801 | assert(!Promotable.isNull()); |
| 2802 | assert(Promotable->isPromotableIntegerType()); |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 2803 | if (const EnumType *ET = Promotable->getAs<EnumType>()) |
| 2804 | return ET->getDecl()->getPromotionType(); |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 2805 | if (Promotable->isSignedIntegerType()) |
| 2806 | return IntTy; |
| 2807 | uint64_t PromotableSize = getTypeSize(Promotable); |
| 2808 | uint64_t IntSize = getTypeSize(IntTy); |
| 2809 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
| 2810 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
| 2811 | } |
| 2812 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2813 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2814 | /// 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] | 2815 | /// LHS < RHS, return -1. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2816 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2817 | Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 2818 | Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2819 | if (LHSC == RHSC) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2820 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2821 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 2822 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2823 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2824 | unsigned LHSRank = getIntegerRank(LHSC); |
| 2825 | unsigned RHSRank = getIntegerRank(RHSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2826 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2827 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 2828 | if (LHSRank == RHSRank) return 0; |
| 2829 | return LHSRank > RHSRank ? 1 : -1; |
| 2830 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2831 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2832 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 2833 | if (LHSUnsigned) { |
| 2834 | // If the unsigned [LHS] type is larger, return it. |
| 2835 | if (LHSRank >= RHSRank) |
| 2836 | return 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2837 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2838 | // If the signed type can represent all values of the unsigned type, it |
| 2839 | // 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] | 2840 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2841 | return -1; |
| 2842 | } |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2843 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2844 | // If the unsigned [RHS] type is larger, return it. |
| 2845 | if (RHSRank >= LHSRank) |
| 2846 | return -1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2847 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2848 | // If the signed type can represent all values of the unsigned type, it |
| 2849 | // 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] | 2850 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2851 | return 1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2852 | } |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2853 | |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 2854 | static RecordDecl * |
| 2855 | CreateRecordDecl(ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC, |
| 2856 | SourceLocation L, IdentifierInfo *Id) { |
| 2857 | if (Ctx.getLangOptions().CPlusPlus) |
| 2858 | return CXXRecordDecl::Create(Ctx, TK, DC, L, Id); |
| 2859 | else |
| 2860 | return RecordDecl::Create(Ctx, TK, DC, L, Id); |
| 2861 | } |
| 2862 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2863 | // getCFConstantStringType - Return the type used for constant CFStrings. |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2864 | QualType ASTContext::getCFConstantStringType() { |
| 2865 | if (!CFConstantStringTypeDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2866 | CFConstantStringTypeDecl = |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 2867 | CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
| 2868 | &Idents.get("NSConstantString")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 2869 | CFConstantStringTypeDecl->startDefinition(); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 2870 | |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 2871 | QualType FieldTypes[4]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2872 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2873 | // const int *isa; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2874 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 2875 | // int flags; |
| 2876 | FieldTypes[1] = IntTy; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2877 | // const char *str; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2878 | FieldTypes[2] = getPointerType(CharTy.withConst()); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2879 | // long length; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2880 | FieldTypes[3] = LongTy; |
| 2881 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2882 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2883 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2884 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2885 | SourceLocation(), 0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2886 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2887 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2888 | /*Mutable=*/false); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2889 | CFConstantStringTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2890 | } |
| 2891 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2892 | CFConstantStringTypeDecl->completeDefinition(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2893 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2894 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2895 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 8467583 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 2896 | } |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 2897 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 2898 | void ASTContext::setCFConstantStringType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2899 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 2900 | assert(Rec && "Invalid CFConstantStringType"); |
| 2901 | CFConstantStringTypeDecl = Rec->getDecl(); |
| 2902 | } |
| 2903 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2904 | QualType ASTContext::getObjCFastEnumerationStateType() { |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2905 | if (!ObjCFastEnumerationStateTypeDecl) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2906 | ObjCFastEnumerationStateTypeDecl = |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 2907 | CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
| 2908 | &Idents.get("__objcFastEnumerationState")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 2909 | ObjCFastEnumerationStateTypeDecl->startDefinition(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2910 | |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2911 | QualType FieldTypes[] = { |
| 2912 | UnsignedLongTy, |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2913 | getPointerType(ObjCIdTypedefType), |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2914 | getPointerType(UnsignedLongTy), |
| 2915 | getConstantArrayType(UnsignedLongTy, |
| 2916 | llvm::APInt(32, 5), ArrayType::Normal, 0) |
| 2917 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2918 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2919 | for (size_t i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2920 | FieldDecl *Field = FieldDecl::Create(*this, |
| 2921 | ObjCFastEnumerationStateTypeDecl, |
| 2922 | SourceLocation(), 0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2923 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2924 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2925 | /*Mutable=*/false); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2926 | ObjCFastEnumerationStateTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2927 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2928 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2929 | ObjCFastEnumerationStateTypeDecl->completeDefinition(); |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2930 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2931 | |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2932 | return getTagDeclType(ObjCFastEnumerationStateTypeDecl); |
| 2933 | } |
| 2934 | |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2935 | QualType ASTContext::getBlockDescriptorType() { |
| 2936 | if (BlockDescriptorType) |
| 2937 | return getTagDeclType(BlockDescriptorType); |
| 2938 | |
| 2939 | RecordDecl *T; |
| 2940 | // FIXME: Needs the FlagAppleBlock bit. |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 2941 | T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
| 2942 | &Idents.get("__block_descriptor")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 2943 | T->startDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2944 | |
| 2945 | QualType FieldTypes[] = { |
| 2946 | UnsignedLongTy, |
| 2947 | UnsignedLongTy, |
| 2948 | }; |
| 2949 | |
| 2950 | const char *FieldNames[] = { |
| 2951 | "reserved", |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 2952 | "Size" |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2953 | }; |
| 2954 | |
| 2955 | for (size_t i = 0; i < 2; ++i) { |
| 2956 | FieldDecl *Field = FieldDecl::Create(*this, |
| 2957 | T, |
| 2958 | SourceLocation(), |
| 2959 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2960 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2961 | /*BitWidth=*/0, |
| 2962 | /*Mutable=*/false); |
| 2963 | T->addDecl(Field); |
| 2964 | } |
| 2965 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2966 | T->completeDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2967 | |
| 2968 | BlockDescriptorType = T; |
| 2969 | |
| 2970 | return getTagDeclType(BlockDescriptorType); |
| 2971 | } |
| 2972 | |
| 2973 | void ASTContext::setBlockDescriptorType(QualType T) { |
| 2974 | const RecordType *Rec = T->getAs<RecordType>(); |
| 2975 | assert(Rec && "Invalid BlockDescriptorType"); |
| 2976 | BlockDescriptorType = Rec->getDecl(); |
| 2977 | } |
| 2978 | |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 2979 | QualType ASTContext::getBlockDescriptorExtendedType() { |
| 2980 | if (BlockDescriptorExtendedType) |
| 2981 | return getTagDeclType(BlockDescriptorExtendedType); |
| 2982 | |
| 2983 | RecordDecl *T; |
| 2984 | // FIXME: Needs the FlagAppleBlock bit. |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 2985 | T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
| 2986 | &Idents.get("__block_descriptor_withcopydispose")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 2987 | T->startDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 2988 | |
| 2989 | QualType FieldTypes[] = { |
| 2990 | UnsignedLongTy, |
| 2991 | UnsignedLongTy, |
| 2992 | getPointerType(VoidPtrTy), |
| 2993 | getPointerType(VoidPtrTy) |
| 2994 | }; |
| 2995 | |
| 2996 | const char *FieldNames[] = { |
| 2997 | "reserved", |
| 2998 | "Size", |
| 2999 | "CopyFuncPtr", |
| 3000 | "DestroyFuncPtr" |
| 3001 | }; |
| 3002 | |
| 3003 | for (size_t i = 0; i < 4; ++i) { |
| 3004 | FieldDecl *Field = FieldDecl::Create(*this, |
| 3005 | T, |
| 3006 | SourceLocation(), |
| 3007 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3008 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3009 | /*BitWidth=*/0, |
| 3010 | /*Mutable=*/false); |
| 3011 | T->addDecl(Field); |
| 3012 | } |
| 3013 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3014 | T->completeDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3015 | |
| 3016 | BlockDescriptorExtendedType = T; |
| 3017 | |
| 3018 | return getTagDeclType(BlockDescriptorExtendedType); |
| 3019 | } |
| 3020 | |
| 3021 | void ASTContext::setBlockDescriptorExtendedType(QualType T) { |
| 3022 | const RecordType *Rec = T->getAs<RecordType>(); |
| 3023 | assert(Rec && "Invalid BlockDescriptorType"); |
| 3024 | BlockDescriptorExtendedType = Rec->getDecl(); |
| 3025 | } |
| 3026 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3027 | bool ASTContext::BlockRequiresCopying(QualType Ty) { |
| 3028 | if (Ty->isBlockPointerType()) |
| 3029 | return true; |
| 3030 | if (isObjCNSObjectType(Ty)) |
| 3031 | return true; |
| 3032 | if (Ty->isObjCObjectPointerType()) |
| 3033 | return true; |
| 3034 | return false; |
| 3035 | } |
| 3036 | |
| 3037 | QualType ASTContext::BuildByRefType(const char *DeclName, QualType Ty) { |
| 3038 | // type = struct __Block_byref_1_X { |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3039 | // void *__isa; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3040 | // struct __Block_byref_1_X *__forwarding; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3041 | // unsigned int __flags; |
| 3042 | // unsigned int __size; |
Mike Stump | 38e1627 | 2009-10-21 22:01:24 +0000 | [diff] [blame] | 3043 | // void *__copy_helper; // as needed |
| 3044 | // void *__destroy_help // as needed |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3045 | // int X; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3046 | // } * |
| 3047 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3048 | bool HasCopyAndDispose = BlockRequiresCopying(Ty); |
| 3049 | |
| 3050 | // FIXME: Move up |
Fariborz Jahanian | 4d0d85c | 2009-10-24 00:16:42 +0000 | [diff] [blame] | 3051 | static unsigned int UniqueBlockByRefTypeID = 0; |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 3052 | llvm::SmallString<36> Name; |
| 3053 | llvm::raw_svector_ostream(Name) << "__Block_byref_" << |
| 3054 | ++UniqueBlockByRefTypeID << '_' << DeclName; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3055 | RecordDecl *T; |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3056 | T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
| 3057 | &Idents.get(Name.str())); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3058 | T->startDefinition(); |
| 3059 | QualType Int32Ty = IntTy; |
| 3060 | assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported"); |
| 3061 | QualType FieldTypes[] = { |
| 3062 | getPointerType(VoidPtrTy), |
| 3063 | getPointerType(getTagDeclType(T)), |
| 3064 | Int32Ty, |
| 3065 | Int32Ty, |
| 3066 | getPointerType(VoidPtrTy), |
| 3067 | getPointerType(VoidPtrTy), |
| 3068 | Ty |
| 3069 | }; |
| 3070 | |
| 3071 | const char *FieldNames[] = { |
| 3072 | "__isa", |
| 3073 | "__forwarding", |
| 3074 | "__flags", |
| 3075 | "__size", |
| 3076 | "__copy_helper", |
| 3077 | "__destroy_helper", |
| 3078 | DeclName, |
| 3079 | }; |
| 3080 | |
| 3081 | for (size_t i = 0; i < 7; ++i) { |
| 3082 | if (!HasCopyAndDispose && i >=4 && i <= 5) |
| 3083 | continue; |
| 3084 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
| 3085 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3086 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3087 | /*BitWidth=*/0, /*Mutable=*/false); |
| 3088 | T->addDecl(Field); |
| 3089 | } |
| 3090 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3091 | T->completeDefinition(); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3092 | |
| 3093 | return getPointerType(getTagDeclType(T)); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3094 | } |
| 3095 | |
| 3096 | |
| 3097 | QualType ASTContext::getBlockParmType( |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3098 | bool BlockHasCopyDispose, |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3099 | llvm::SmallVector<const Expr *, 8> &BlockDeclRefDecls) { |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3100 | // FIXME: Move up |
Fariborz Jahanian | 4d0d85c | 2009-10-24 00:16:42 +0000 | [diff] [blame] | 3101 | static unsigned int UniqueBlockParmTypeID = 0; |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 3102 | llvm::SmallString<36> Name; |
| 3103 | llvm::raw_svector_ostream(Name) << "__block_literal_" |
| 3104 | << ++UniqueBlockParmTypeID; |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3105 | RecordDecl *T; |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3106 | T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
| 3107 | &Idents.get(Name.str())); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3108 | T->startDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3109 | QualType FieldTypes[] = { |
| 3110 | getPointerType(VoidPtrTy), |
| 3111 | IntTy, |
| 3112 | IntTy, |
| 3113 | getPointerType(VoidPtrTy), |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3114 | (BlockHasCopyDispose ? |
| 3115 | getPointerType(getBlockDescriptorExtendedType()) : |
| 3116 | getPointerType(getBlockDescriptorType())) |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3117 | }; |
| 3118 | |
| 3119 | const char *FieldNames[] = { |
| 3120 | "__isa", |
| 3121 | "__flags", |
| 3122 | "__reserved", |
| 3123 | "__FuncPtr", |
| 3124 | "__descriptor" |
| 3125 | }; |
| 3126 | |
| 3127 | for (size_t i = 0; i < 5; ++i) { |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3128 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3129 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3130 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3131 | /*BitWidth=*/0, /*Mutable=*/false); |
| 3132 | T->addDecl(Field); |
| 3133 | } |
| 3134 | |
| 3135 | for (size_t i = 0; i < BlockDeclRefDecls.size(); ++i) { |
| 3136 | const Expr *E = BlockDeclRefDecls[i]; |
| 3137 | const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); |
| 3138 | clang::IdentifierInfo *Name = 0; |
| 3139 | if (BDRE) { |
| 3140 | const ValueDecl *D = BDRE->getDecl(); |
| 3141 | Name = &Idents.get(D->getName()); |
| 3142 | } |
| 3143 | QualType FieldType = E->getType(); |
| 3144 | |
| 3145 | if (BDRE && BDRE->isByRef()) |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3146 | FieldType = BuildByRefType(BDRE->getDecl()->getNameAsCString(), |
| 3147 | FieldType); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3148 | |
| 3149 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3150 | Name, FieldType, /*TInfo=*/0, |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3151 | /*BitWidth=*/0, /*Mutable=*/false); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3152 | T->addDecl(Field); |
| 3153 | } |
| 3154 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3155 | T->completeDefinition(); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3156 | |
| 3157 | return getPointerType(getTagDeclType(T)); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3158 | } |
| 3159 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3160 | void ASTContext::setObjCFastEnumerationStateType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3161 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3162 | assert(Rec && "Invalid ObjCFAstEnumerationStateType"); |
| 3163 | ObjCFastEnumerationStateTypeDecl = Rec->getDecl(); |
| 3164 | } |
| 3165 | |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3166 | // This returns true if a type has been typedefed to BOOL: |
| 3167 | // typedef <type> BOOL; |
Chris Lattner | 2d99833 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 3168 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3169 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | bb49c3e | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 3170 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 3171 | return II->isStr("BOOL"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3172 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3173 | return false; |
| 3174 | } |
| 3175 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3176 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3177 | /// purpose. |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3178 | CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) { |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3179 | CharUnits sz = getTypeSizeInChars(type); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3180 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3181 | // 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] | 3182 | if (sz.isPositive() && type->isIntegralType()) |
| 3183 | sz = std::max(sz, getTypeSizeInChars(IntTy)); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3184 | // Treat arrays as pointers, since that's how they're passed in. |
| 3185 | else if (type->isArrayType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3186 | sz = getTypeSizeInChars(VoidPtrTy); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3187 | return sz; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3188 | } |
| 3189 | |
| 3190 | static inline |
| 3191 | std::string charUnitsToString(const CharUnits &CU) { |
| 3192 | return llvm::itostr(CU.getQuantity()); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3193 | } |
| 3194 | |
Fariborz Jahanian | 6f46c26 | 2010-04-08 18:06:22 +0000 | [diff] [blame] | 3195 | /// getObjCEncodingForBlockDecl - Return the encoded type for this block |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3196 | /// declaration. |
| 3197 | void ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr, |
| 3198 | std::string& S) { |
| 3199 | const BlockDecl *Decl = Expr->getBlockDecl(); |
| 3200 | QualType BlockTy = |
| 3201 | Expr->getType()->getAs<BlockPointerType>()->getPointeeType(); |
| 3202 | // Encode result type. |
| 3203 | getObjCEncodingForType(cast<FunctionType>(BlockTy)->getResultType(), S); |
| 3204 | // Compute size of all parameters. |
| 3205 | // Start with computing size of a pointer in number of bytes. |
| 3206 | // FIXME: There might(should) be a better way of doing this computation! |
| 3207 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3208 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
| 3209 | CharUnits ParmOffset = PtrSize; |
Fariborz Jahanian | 6f46c26 | 2010-04-08 18:06:22 +0000 | [diff] [blame] | 3210 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3211 | E = Decl->param_end(); PI != E; ++PI) { |
| 3212 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3213 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3214 | assert (sz.isPositive() && "BlockExpr - Incomplete param type"); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3215 | ParmOffset += sz; |
| 3216 | } |
| 3217 | // Size of the argument frame |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3218 | S += charUnitsToString(ParmOffset); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3219 | // Block pointer and offset. |
| 3220 | S += "@?0"; |
| 3221 | ParmOffset = PtrSize; |
| 3222 | |
| 3223 | // Argument types. |
| 3224 | ParmOffset = PtrSize; |
| 3225 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E = |
| 3226 | Decl->param_end(); PI != E; ++PI) { |
| 3227 | ParmVarDecl *PVDecl = *PI; |
| 3228 | QualType PType = PVDecl->getOriginalType(); |
| 3229 | if (const ArrayType *AT = |
| 3230 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3231 | // Use array's original type only if it has known number of |
| 3232 | // elements. |
| 3233 | if (!isa<ConstantArrayType>(AT)) |
| 3234 | PType = PVDecl->getType(); |
| 3235 | } else if (PType->isFunctionType()) |
| 3236 | PType = PVDecl->getType(); |
| 3237 | getObjCEncodingForType(PType, S); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3238 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3239 | ParmOffset += getObjCEncodingTypeSize(PType); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3240 | } |
| 3241 | } |
| 3242 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3243 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3244 | /// declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3245 | void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Chris Lattner | e6db3b0 | 2008-11-19 07:24:05 +0000 | [diff] [blame] | 3246 | std::string& S) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3247 | // FIXME: This is not very efficient. |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3248 | // Encode type qualifer, 'in', 'inout', etc. for the return type. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3249 | getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3250 | // Encode result type. |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3251 | getObjCEncodingForType(Decl->getResultType(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3252 | // Compute size of all parameters. |
| 3253 | // Start with computing size of a pointer in number of bytes. |
| 3254 | // FIXME: There might(should) be a better way of doing this computation! |
| 3255 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3256 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3257 | // The first two arguments (self and _cmd) are pointers; account for |
| 3258 | // their size. |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3259 | CharUnits ParmOffset = 2 * PtrSize; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3260 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 3261 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3262 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3263 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3264 | assert (sz.isPositive() && |
| 3265 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3266 | ParmOffset += sz; |
| 3267 | } |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3268 | S += charUnitsToString(ParmOffset); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3269 | S += "@0:"; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3270 | S += charUnitsToString(PtrSize); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3271 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3272 | // Argument types. |
| 3273 | ParmOffset = 2 * PtrSize; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3274 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 3275 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3276 | ParmVarDecl *PVDecl = *PI; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3277 | QualType PType = PVDecl->getOriginalType(); |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 3278 | if (const ArrayType *AT = |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 3279 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3280 | // Use array's original type only if it has known number of |
| 3281 | // elements. |
Steve Naroff | bb3fde3 | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 3282 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 3283 | PType = PVDecl->getType(); |
| 3284 | } else if (PType->isFunctionType()) |
| 3285 | PType = PVDecl->getType(); |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3286 | // Process argument qualifiers for user supplied arguments; such as, |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3287 | // 'in', 'inout', etc. |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 3288 | getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3289 | getObjCEncodingForType(PType, S); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3290 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3291 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3292 | } |
| 3293 | } |
| 3294 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3295 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 3296 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3297 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 3298 | /// NULL when getting encodings for protocol properties. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3299 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 3300 | /// attributes readonly and bycopy are encoded as single characters. The |
| 3301 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 3302 | /// encoded as single characters, followed by an identifier. Property types |
| 3303 | /// are also encoded as a parametrized attribute. The characters used to encode |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 3304 | /// these attributes are defined by the following enumeration: |
| 3305 | /// @code |
| 3306 | /// enum PropertyAttributes { |
| 3307 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 3308 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 3309 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 3310 | /// kPropertyDynamic = 'D', // property is dynamic |
| 3311 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 3312 | /// kPropertySetter = 'S', // followed by setter selector name |
| 3313 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
| 3314 | /// kPropertyType = 't' // followed by old-style type encoding. |
| 3315 | /// kPropertyWeak = 'W' // 'weak' property |
| 3316 | /// kPropertyStrong = 'P' // property GC'able |
| 3317 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 3318 | /// }; |
| 3319 | /// @endcode |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3320 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3321 | const Decl *Container, |
Chris Lattner | e6db3b0 | 2008-11-19 07:24:05 +0000 | [diff] [blame] | 3322 | std::string& S) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3323 | // Collect information from the property implementation decl(s). |
| 3324 | bool Dynamic = false; |
| 3325 | ObjCPropertyImplDecl *SynthesizePID = 0; |
| 3326 | |
| 3327 | // FIXME: Duplicated code due to poor abstraction. |
| 3328 | if (Container) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3329 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3330 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
| 3331 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3332 | i = CID->propimpl_begin(), e = CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3333 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3334 | ObjCPropertyImplDecl *PID = *i; |
| 3335 | if (PID->getPropertyDecl() == PD) { |
| 3336 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 3337 | Dynamic = true; |
| 3338 | } else { |
| 3339 | SynthesizePID = PID; |
| 3340 | } |
| 3341 | } |
| 3342 | } |
| 3343 | } else { |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3344 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3345 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3346 | i = OID->propimpl_begin(), e = OID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3347 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3348 | ObjCPropertyImplDecl *PID = *i; |
| 3349 | if (PID->getPropertyDecl() == PD) { |
| 3350 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 3351 | Dynamic = true; |
| 3352 | } else { |
| 3353 | SynthesizePID = PID; |
| 3354 | } |
| 3355 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3356 | } |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3357 | } |
| 3358 | } |
| 3359 | |
| 3360 | // FIXME: This is not very efficient. |
| 3361 | S = "T"; |
| 3362 | |
| 3363 | // Encode result type. |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3364 | // GCC has some special rules regarding encoding of properties which |
| 3365 | // closely resembles encoding of ivars. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3366 | getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3367 | true /* outermost type */, |
| 3368 | true /* encoding for property */); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3369 | |
| 3370 | if (PD->isReadOnly()) { |
| 3371 | S += ",R"; |
| 3372 | } else { |
| 3373 | switch (PD->getSetterKind()) { |
| 3374 | case ObjCPropertyDecl::Assign: break; |
| 3375 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3376 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3377 | } |
| 3378 | } |
| 3379 | |
| 3380 | // It really isn't clear at all what this means, since properties |
| 3381 | // are "dynamic by default". |
| 3382 | if (Dynamic) |
| 3383 | S += ",D"; |
| 3384 | |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3385 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 3386 | S += ",N"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3387 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3388 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 3389 | S += ",G"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3390 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3391 | } |
| 3392 | |
| 3393 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 3394 | S += ",S"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3395 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3396 | } |
| 3397 | |
| 3398 | if (SynthesizePID) { |
| 3399 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 3400 | S += ",V"; |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 3401 | S += OID->getNameAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3402 | } |
| 3403 | |
| 3404 | // FIXME: OBJCGC: weak & strong |
| 3405 | } |
| 3406 | |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3407 | /// getLegacyIntegralTypeEncoding - |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3408 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
| 3409 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3410 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
| 3411 | /// |
| 3412 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3413 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3414 | if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) { |
Fariborz Jahanian | c657eba | 2009-02-11 23:59:18 +0000 | [diff] [blame] | 3415 | if (BT->getKind() == BuiltinType::ULong && |
| 3416 | ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3417 | PointeeTy = UnsignedIntTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3418 | else |
Fariborz Jahanian | c657eba | 2009-02-11 23:59:18 +0000 | [diff] [blame] | 3419 | if (BT->getKind() == BuiltinType::Long && |
| 3420 | ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3421 | PointeeTy = IntTy; |
| 3422 | } |
| 3423 | } |
| 3424 | } |
| 3425 | |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3426 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 3427 | const FieldDecl *Field) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3428 | // We follow the behavior of gcc, expanding structures which are |
| 3429 | // directly pointed to, and expanding embedded structures. Note that |
| 3430 | // these rules are sufficient to prevent recursive encoding of the |
| 3431 | // same type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3432 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
Fariborz Jahanian | 5b8c7d9 | 2008-12-22 23:22:27 +0000 | [diff] [blame] | 3433 | true /* outermost type */); |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3434 | } |
| 3435 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3436 | static void EncodeBitField(const ASTContext *Context, std::string& S, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 3437 | const FieldDecl *FD) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3438 | const Expr *E = FD->getBitWidth(); |
| 3439 | assert(E && "bitfield width not there - getObjCEncodingForTypeImpl"); |
| 3440 | ASTContext *Ctx = const_cast<ASTContext*>(Context); |
Eli Friedman | 9a901bb | 2009-04-26 19:19:15 +0000 | [diff] [blame] | 3441 | unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue(); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3442 | S += 'b'; |
| 3443 | S += llvm::utostr(N); |
| 3444 | } |
| 3445 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 3446 | // FIXME: Use SmallString for accumulating string. |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3447 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 3448 | bool ExpandPointedToStructures, |
| 3449 | bool ExpandStructures, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 3450 | const FieldDecl *FD, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3451 | bool OutermostType, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3452 | bool EncodingProperty) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3453 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3454 | if (FD && FD->isBitField()) |
| 3455 | return EncodeBitField(this, S, FD); |
| 3456 | char encoding; |
| 3457 | switch (BT->getKind()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3458 | default: assert(0 && "Unhandled builtin type kind"); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3459 | case BuiltinType::Void: encoding = 'v'; break; |
| 3460 | case BuiltinType::Bool: encoding = 'B'; break; |
| 3461 | case BuiltinType::Char_U: |
| 3462 | case BuiltinType::UChar: encoding = 'C'; break; |
| 3463 | case BuiltinType::UShort: encoding = 'S'; break; |
| 3464 | case BuiltinType::UInt: encoding = 'I'; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3465 | case BuiltinType::ULong: |
| 3466 | encoding = |
| 3467 | (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q'; |
Fariborz Jahanian | 72696e1 | 2009-02-11 22:31:45 +0000 | [diff] [blame] | 3468 | break; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3469 | case BuiltinType::UInt128: encoding = 'T'; break; |
| 3470 | case BuiltinType::ULongLong: encoding = 'Q'; break; |
| 3471 | case BuiltinType::Char_S: |
| 3472 | case BuiltinType::SChar: encoding = 'c'; break; |
| 3473 | case BuiltinType::Short: encoding = 's'; break; |
| 3474 | case BuiltinType::Int: encoding = 'i'; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3475 | case BuiltinType::Long: |
| 3476 | encoding = |
| 3477 | (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3478 | break; |
| 3479 | case BuiltinType::LongLong: encoding = 'q'; break; |
| 3480 | case BuiltinType::Int128: encoding = 't'; break; |
| 3481 | case BuiltinType::Float: encoding = 'f'; break; |
| 3482 | case BuiltinType::Double: encoding = 'd'; break; |
| 3483 | case BuiltinType::LongDouble: encoding = 'd'; break; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3484 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3485 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3486 | S += encoding; |
| 3487 | return; |
| 3488 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3489 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3490 | if (const ComplexType *CT = T->getAs<ComplexType>()) { |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 3491 | S += 'j'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3492 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false, |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 3493 | false); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3494 | return; |
| 3495 | } |
Fariborz Jahanian | 60bce3e | 2009-11-23 20:40:50 +0000 | [diff] [blame] | 3496 | |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 3497 | // encoding for pointer or r3eference types. |
| 3498 | QualType PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3499 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Fariborz Jahanian | 8d2c0a9 | 2009-11-30 18:43:52 +0000 | [diff] [blame] | 3500 | if (PT->isObjCSelType()) { |
| 3501 | S += ':'; |
| 3502 | return; |
| 3503 | } |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 3504 | PointeeTy = PT->getPointeeType(); |
| 3505 | } |
| 3506 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 3507 | PointeeTy = RT->getPointeeType(); |
| 3508 | if (!PointeeTy.isNull()) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3509 | bool isReadOnly = false; |
| 3510 | // For historical/compatibility reasons, the read-only qualifier of the |
| 3511 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 3512 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3513 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3514 | if (isa<TypedefType>(T.getTypePtr())) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3515 | if (OutermostType && T.isConstQualified()) { |
| 3516 | isReadOnly = true; |
| 3517 | S += 'r'; |
| 3518 | } |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 3519 | } else if (OutermostType) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3520 | QualType P = PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3521 | while (P->getAs<PointerType>()) |
| 3522 | P = P->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3523 | if (P.isConstQualified()) { |
| 3524 | isReadOnly = true; |
| 3525 | S += 'r'; |
| 3526 | } |
| 3527 | } |
| 3528 | if (isReadOnly) { |
| 3529 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 3530 | // combinations need to be rearranged. |
| 3531 | // Rewrite "in const" from "nr" to "rn" |
| 3532 | const char * s = S.c_str(); |
| 3533 | int len = S.length(); |
| 3534 | if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') { |
| 3535 | std::string replace = "rn"; |
| 3536 | S.replace(S.end()-2, S.end(), replace); |
| 3537 | } |
| 3538 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3539 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3540 | if (PointeeTy->isCharType()) { |
| 3541 | // char pointer types should be encoded as '*' unless it is a |
| 3542 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3543 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3544 | S += '*'; |
| 3545 | return; |
| 3546 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3547 | } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) { |
Steve Naroff | 9533a7f | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 3548 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 3549 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 3550 | S += '#'; |
| 3551 | return; |
| 3552 | } |
| 3553 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 3554 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 3555 | S += '@'; |
| 3556 | return; |
| 3557 | } |
| 3558 | // fall through... |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3559 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3560 | S += '^'; |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3561 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 3562 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3563 | getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3564 | NULL); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3565 | return; |
| 3566 | } |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 3567 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3568 | if (const ArrayType *AT = |
| 3569 | // Ignore type qualifiers etc. |
| 3570 | dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3571 | if (isa<IncompleteArrayType>(AT)) { |
| 3572 | // Incomplete arrays are encoded as a pointer to the array element. |
| 3573 | S += '^'; |
| 3574 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3575 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3576 | false, ExpandStructures, FD); |
| 3577 | } else { |
| 3578 | S += '['; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3579 | |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3580 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 3581 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 3582 | else { |
| 3583 | //Variable length arrays are encoded as a regular array with 0 elements. |
| 3584 | assert(isa<VariableArrayType>(AT) && "Unknown array type!"); |
| 3585 | S += '0'; |
| 3586 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3587 | |
| 3588 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3589 | false, ExpandStructures, FD); |
| 3590 | S += ']'; |
| 3591 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3592 | return; |
| 3593 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3594 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3595 | if (T->getAs<FunctionType>()) { |
Anders Carlsson | c0a87b7 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 3596 | S += '?'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3597 | return; |
| 3598 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3599 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3600 | if (const RecordType *RTy = T->getAs<RecordType>()) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3601 | RecordDecl *RDecl = RTy->getDecl(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3602 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 3603 | // Anonymous structures print as '?' |
| 3604 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 3605 | S += II->getName(); |
| 3606 | } else { |
| 3607 | S += '?'; |
| 3608 | } |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3609 | if (ExpandStructures) { |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3610 | S += '='; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3611 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 3612 | FieldEnd = RDecl->field_end(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3613 | Field != FieldEnd; ++Field) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3614 | if (FD) { |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3615 | S += '"'; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3616 | S += Field->getNameAsString(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3617 | S += '"'; |
| 3618 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3619 | |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3620 | // Special case bit-fields. |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3621 | if (Field->isBitField()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3622 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3623 | (*Field)); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3624 | } else { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3625 | QualType qt = Field->getType(); |
| 3626 | getLegacyIntegralTypeEncoding(qt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3627 | getObjCEncodingForTypeImpl(qt, S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3628 | FD); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3629 | } |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3630 | } |
Fariborz Jahanian | 6de88a8 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 3631 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3632 | S += RDecl->isUnion() ? ')' : '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3633 | return; |
| 3634 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3635 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3636 | if (T->isEnumeralType()) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3637 | if (FD && FD->isBitField()) |
| 3638 | EncodeBitField(this, S, FD); |
| 3639 | else |
| 3640 | S += 'i'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3641 | return; |
| 3642 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3643 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3644 | if (T->isBlockPointerType()) { |
Steve Naroff | 21a98b1 | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 3645 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3646 | return; |
| 3647 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3648 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3649 | if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3650 | // @encode(class_name) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3651 | ObjCInterfaceDecl *OI = OIT->getDecl(); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3652 | S += '{'; |
| 3653 | const IdentifierInfo *II = OI->getIdentifier(); |
| 3654 | S += II->getName(); |
| 3655 | S += '='; |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3656 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3657 | CollectObjCIvars(OI, RecFields); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3658 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3659 | if (RecFields[i]->isBitField()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3660 | getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3661 | RecFields[i]); |
| 3662 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3663 | getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3664 | FD); |
| 3665 | } |
| 3666 | S += '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3667 | return; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3668 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3669 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3670 | if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3671 | if (OPT->isObjCIdType()) { |
| 3672 | S += '@'; |
| 3673 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3674 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3675 | |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 3676 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
| 3677 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
| 3678 | // Since this is a binary compatibility issue, need to consult with runtime |
| 3679 | // folks. Fortunately, this is a *very* obsure construct. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3680 | S += '#'; |
| 3681 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3682 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3683 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3684 | if (OPT->isObjCQualifiedIdType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3685 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3686 | ExpandPointedToStructures, |
| 3687 | ExpandStructures, FD); |
| 3688 | if (FD || EncodingProperty) { |
| 3689 | // Note that we do extended encoding of protocol qualifer list |
| 3690 | // Only when doing ivar or property encoding. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3691 | S += '"'; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3692 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 3693 | E = OPT->qual_end(); I != E; ++I) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3694 | S += '<'; |
| 3695 | S += (*I)->getNameAsString(); |
| 3696 | S += '>'; |
| 3697 | } |
| 3698 | S += '"'; |
| 3699 | } |
| 3700 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3701 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3702 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3703 | QualType PointeeTy = OPT->getPointeeType(); |
| 3704 | if (!EncodingProperty && |
| 3705 | isa<TypedefType>(PointeeTy.getTypePtr())) { |
| 3706 | // Another historical/compatibility reason. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3707 | // We encode the underlying type which comes out as |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3708 | // {...}; |
| 3709 | S += '^'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3710 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 3711 | false, ExpandPointedToStructures, |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3712 | NULL); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3713 | return; |
| 3714 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3715 | |
| 3716 | S += '@'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 3717 | if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3718 | S += '"'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 3719 | S += OPT->getInterfaceDecl()->getIdentifier()->getName(); |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3720 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 3721 | E = OPT->qual_end(); I != E; ++I) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3722 | S += '<'; |
| 3723 | S += (*I)->getNameAsString(); |
| 3724 | S += '>'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3725 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3726 | S += '"'; |
| 3727 | } |
| 3728 | return; |
| 3729 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3730 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3731 | assert(0 && "@encode for type not implemented!"); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3732 | } |
| 3733 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3734 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3735 | std::string& S) const { |
| 3736 | if (QT & Decl::OBJC_TQ_In) |
| 3737 | S += 'n'; |
| 3738 | if (QT & Decl::OBJC_TQ_Inout) |
| 3739 | S += 'N'; |
| 3740 | if (QT & Decl::OBJC_TQ_Out) |
| 3741 | S += 'o'; |
| 3742 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 3743 | S += 'O'; |
| 3744 | if (QT & Decl::OBJC_TQ_Byref) |
| 3745 | S += 'R'; |
| 3746 | if (QT & Decl::OBJC_TQ_Oneway) |
| 3747 | S += 'V'; |
| 3748 | } |
| 3749 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3750 | void ASTContext::setBuiltinVaListType(QualType T) { |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 3751 | assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3752 | |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 3753 | BuiltinVaListType = T; |
| 3754 | } |
| 3755 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3756 | void ASTContext::setObjCIdType(QualType T) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3757 | ObjCIdTypedefType = T; |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 3758 | } |
| 3759 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3760 | void ASTContext::setObjCSelType(QualType T) { |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 3761 | ObjCSelTypedefType = T; |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 3762 | } |
| 3763 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3764 | void ASTContext::setObjCProtoType(QualType QT) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3765 | ObjCProtoType = QT; |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 3766 | } |
| 3767 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3768 | void ASTContext::setObjCClassType(QualType T) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3769 | ObjCClassTypedefType = T; |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 3770 | } |
| 3771 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3772 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3773 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 3774 | "'NSConstantString' type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3775 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3776 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 3777 | } |
| 3778 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 3779 | /// \brief Retrieve the template name that corresponds to a non-empty |
| 3780 | /// lookup. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3781 | TemplateName ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin, |
| 3782 | UnresolvedSetIterator End) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 3783 | unsigned size = End - Begin; |
| 3784 | assert(size > 1 && "set is not overloaded!"); |
| 3785 | |
| 3786 | void *memory = Allocate(sizeof(OverloadedTemplateStorage) + |
| 3787 | size * sizeof(FunctionTemplateDecl*)); |
| 3788 | OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size); |
| 3789 | |
| 3790 | NamedDecl **Storage = OT->getStorage(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3791 | for (UnresolvedSetIterator I = Begin; I != End; ++I) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 3792 | NamedDecl *D = *I; |
| 3793 | assert(isa<FunctionTemplateDecl>(D) || |
| 3794 | (isa<UsingShadowDecl>(D) && |
| 3795 | isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))); |
| 3796 | *Storage++ = D; |
| 3797 | } |
| 3798 | |
| 3799 | return TemplateName(OT); |
| 3800 | } |
| 3801 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3802 | /// \brief Retrieve the template name that represents a qualified |
| 3803 | /// template name such as \c std::vector. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3804 | TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3805 | bool TemplateKeyword, |
| 3806 | TemplateDecl *Template) { |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3807 | // FIXME: Canonicalization? |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3808 | llvm::FoldingSetNodeID ID; |
| 3809 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 3810 | |
| 3811 | void *InsertPos = 0; |
| 3812 | QualifiedTemplateName *QTN = |
| 3813 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 3814 | if (!QTN) { |
| 3815 | QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); |
| 3816 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 3817 | } |
| 3818 | |
| 3819 | return TemplateName(QTN); |
| 3820 | } |
| 3821 | |
| 3822 | /// \brief Retrieve the template name that represents a dependent |
| 3823 | /// template name such as \c MetaFun::template apply. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3824 | TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3825 | const IdentifierInfo *Name) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3826 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 3827 | "Nested name specifier must be dependent"); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3828 | |
| 3829 | llvm::FoldingSetNodeID ID; |
| 3830 | DependentTemplateName::Profile(ID, NNS, Name); |
| 3831 | |
| 3832 | void *InsertPos = 0; |
| 3833 | DependentTemplateName *QTN = |
| 3834 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 3835 | |
| 3836 | if (QTN) |
| 3837 | return TemplateName(QTN); |
| 3838 | |
| 3839 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 3840 | if (CanonNNS == NNS) { |
| 3841 | QTN = new (*this,4) DependentTemplateName(NNS, Name); |
| 3842 | } else { |
| 3843 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
| 3844 | QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3845 | DependentTemplateName *CheckQTN = |
| 3846 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 3847 | assert(!CheckQTN && "Dependent type name canonicalization broken"); |
| 3848 | (void)CheckQTN; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3849 | } |
| 3850 | |
| 3851 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 3852 | return TemplateName(QTN); |
| 3853 | } |
| 3854 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 3855 | /// \brief Retrieve the template name that represents a dependent |
| 3856 | /// template name such as \c MetaFun::template operator+. |
| 3857 | TemplateName |
| 3858 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
| 3859 | OverloadedOperatorKind Operator) { |
| 3860 | assert((!NNS || NNS->isDependent()) && |
| 3861 | "Nested name specifier must be dependent"); |
| 3862 | |
| 3863 | llvm::FoldingSetNodeID ID; |
| 3864 | DependentTemplateName::Profile(ID, NNS, Operator); |
| 3865 | |
| 3866 | void *InsertPos = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3867 | DependentTemplateName *QTN |
| 3868 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 3869 | |
| 3870 | if (QTN) |
| 3871 | return TemplateName(QTN); |
| 3872 | |
| 3873 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 3874 | if (CanonNNS == NNS) { |
| 3875 | QTN = new (*this,4) DependentTemplateName(NNS, Operator); |
| 3876 | } else { |
| 3877 | TemplateName Canon = getDependentTemplateName(CanonNNS, Operator); |
| 3878 | QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3879 | |
| 3880 | DependentTemplateName *CheckQTN |
| 3881 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 3882 | assert(!CheckQTN && "Dependent template name canonicalization broken"); |
| 3883 | (void)CheckQTN; |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 3884 | } |
| 3885 | |
| 3886 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 3887 | return TemplateName(QTN); |
| 3888 | } |
| 3889 | |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 3890 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | d934112 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 3891 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 3892 | /// is actually a value of type @c TargetInfo::IntType. |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 3893 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 3894 | switch (Type) { |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 3895 | case TargetInfo::NoInt: return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 3896 | case TargetInfo::SignedShort: return ShortTy; |
| 3897 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 3898 | case TargetInfo::SignedInt: return IntTy; |
| 3899 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 3900 | case TargetInfo::SignedLong: return LongTy; |
| 3901 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 3902 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 3903 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 3904 | } |
| 3905 | |
| 3906 | assert(false && "Unhandled TargetInfo::IntType value"); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 3907 | return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 3908 | } |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 3909 | |
| 3910 | //===----------------------------------------------------------------------===// |
| 3911 | // Type Predicates. |
| 3912 | //===----------------------------------------------------------------------===// |
| 3913 | |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3914 | /// isObjCNSObjectType - Return true if this is an NSObject object using |
| 3915 | /// NSObject attribute on a c-style pointer type. |
| 3916 | /// FIXME - Make it work directly on types. |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 3917 | /// FIXME: Move to Type. |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3918 | /// |
| 3919 | bool ASTContext::isObjCNSObjectType(QualType Ty) const { |
| 3920 | if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) { |
| 3921 | if (TypedefDecl *TD = TDT->getDecl()) |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 3922 | if (TD->getAttr<ObjCNSObjectAttr>()) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3923 | return true; |
| 3924 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3925 | return false; |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3926 | } |
| 3927 | |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3928 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 3929 | /// garbage collection attribute. |
| 3930 | /// |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3931 | Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const { |
| 3932 | Qualifiers::GC GCAttrs = Qualifiers::GCNone; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3933 | if (getLangOptions().ObjC1 && |
| 3934 | getLangOptions().getGCMode() != LangOptions::NonGC) { |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 3935 | GCAttrs = Ty.getObjCGCAttr(); |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3936 | // Default behavious under objective-c's gc is for objective-c pointers |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3937 | // (or pointers to them) be treated as though they were declared |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 3938 | // as __strong. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3939 | if (GCAttrs == Qualifiers::GCNone) { |
Fariborz Jahanian | 75212ee | 2009-09-10 23:38:45 +0000 | [diff] [blame] | 3940 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3941 | GCAttrs = Qualifiers::Strong; |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 3942 | else if (Ty->isPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3943 | return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 3944 | } |
Fariborz Jahanian | c211218 | 2009-04-11 00:00:54 +0000 | [diff] [blame] | 3945 | // Non-pointers have none gc'able attribute regardless of the attribute |
| 3946 | // set on them. |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 3947 | else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3948 | return Qualifiers::GCNone; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3949 | } |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 3950 | return GCAttrs; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3951 | } |
| 3952 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3953 | //===----------------------------------------------------------------------===// |
| 3954 | // Type Compatibility Testing |
| 3955 | //===----------------------------------------------------------------------===// |
Chris Lattner | 770951b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 3956 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3957 | /// areCompatVectorTypes - Return true if the two specified vector types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3958 | /// compatible. |
| 3959 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 3960 | const VectorType *RHS) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 3961 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3962 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3963 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3964 | } |
| 3965 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3966 | //===----------------------------------------------------------------------===// |
| 3967 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 3968 | //===----------------------------------------------------------------------===// |
| 3969 | |
| 3970 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 3971 | /// inheritance hierarchy of 'rProto'. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 3972 | bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 3973 | ObjCProtocolDecl *rProto) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3974 | if (lProto == rProto) |
| 3975 | return true; |
| 3976 | for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), |
| 3977 | E = rProto->protocol_end(); PI != E; ++PI) |
| 3978 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 3979 | return true; |
| 3980 | return false; |
| 3981 | } |
| 3982 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3983 | /// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...> |
| 3984 | /// return true if lhs's protocols conform to rhs's protocol; false |
| 3985 | /// otherwise. |
| 3986 | bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) { |
| 3987 | if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType()) |
| 3988 | return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false); |
| 3989 | return false; |
| 3990 | } |
| 3991 | |
| 3992 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 3993 | /// ObjCQualifiedIDType. |
| 3994 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 3995 | bool compare) { |
| 3996 | // Allow id<P..> and an 'id' or void* type in all cases. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3997 | if (lhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3998 | lhs->isObjCIdType() || lhs->isObjCClassType()) |
| 3999 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4000 | else if (rhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4001 | rhs->isObjCIdType() || rhs->isObjCClassType()) |
| 4002 | return true; |
| 4003 | |
| 4004 | if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4005 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4006 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4007 | if (!rhsOPT) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4008 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4009 | if (rhsOPT->qual_empty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4010 | // If the RHS is a unqualified interface pointer "NSString*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4011 | // make sure we check the class hierarchy. |
| 4012 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 4013 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4014 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4015 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4016 | // see if static class implements all of id's protocols, directly or |
| 4017 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 4018 | if (!rhsID->ClassImplementsProtocol(*I, true)) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4019 | return false; |
| 4020 | } |
| 4021 | } |
| 4022 | // If there are no qualifiers and no interface, we have an 'id'. |
| 4023 | return true; |
| 4024 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4025 | // Both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4026 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4027 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4028 | ObjCProtocolDecl *lhsProto = *I; |
| 4029 | bool match = false; |
| 4030 | |
| 4031 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4032 | // see if static class implements all of id's protocols, directly or |
| 4033 | // through its super class and categories. |
| 4034 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 4035 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 4036 | ObjCProtocolDecl *rhsProto = *J; |
| 4037 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 4038 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 4039 | match = true; |
| 4040 | break; |
| 4041 | } |
| 4042 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4043 | // If the RHS is a qualified interface pointer "NSString<P>*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4044 | // make sure we check the class hierarchy. |
| 4045 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 4046 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4047 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4048 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4049 | // see if static class implements all of id's protocols, directly or |
| 4050 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 4051 | if (rhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4052 | match = true; |
| 4053 | break; |
| 4054 | } |
| 4055 | } |
| 4056 | } |
| 4057 | if (!match) |
| 4058 | return false; |
| 4059 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4060 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4061 | return true; |
| 4062 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4063 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4064 | const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 4065 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 4066 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4067 | if (const ObjCObjectPointerType *lhsOPT = |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4068 | lhs->getAsObjCInterfacePointerType()) { |
| 4069 | if (lhsOPT->qual_empty()) { |
| 4070 | bool match = false; |
| 4071 | if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { |
| 4072 | for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(), |
| 4073 | E = rhsQID->qual_end(); I != E; ++I) { |
| 4074 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4075 | // see if static class implements all of id's protocols, directly or |
| 4076 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 4077 | if (lhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4078 | match = true; |
| 4079 | break; |
| 4080 | } |
| 4081 | } |
| 4082 | if (!match) |
| 4083 | return false; |
| 4084 | } |
| 4085 | return true; |
| 4086 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4087 | // Both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4088 | for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(), |
| 4089 | E = lhsOPT->qual_end(); I != E; ++I) { |
| 4090 | ObjCProtocolDecl *lhsProto = *I; |
| 4091 | bool match = false; |
| 4092 | |
| 4093 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4094 | // see if static class implements all of id's protocols, directly or |
| 4095 | // through its super class and categories. |
| 4096 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 4097 | E = rhsQID->qual_end(); J != E; ++J) { |
| 4098 | ObjCProtocolDecl *rhsProto = *J; |
| 4099 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 4100 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 4101 | match = true; |
| 4102 | break; |
| 4103 | } |
| 4104 | } |
| 4105 | if (!match) |
| 4106 | return false; |
| 4107 | } |
| 4108 | return true; |
| 4109 | } |
| 4110 | return false; |
| 4111 | } |
| 4112 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4113 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4114 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 4115 | /// protocol qualifiers on the LHS or RHS. |
| 4116 | /// |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4117 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 4118 | const ObjCObjectPointerType *RHSOPT) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 4119 | // If either type represents the built-in 'id' or 'Class' types, return true. |
| 4120 | if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4121 | return true; |
| 4122 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4123 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4124 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 4125 | QualType(RHSOPT,0), |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4126 | false); |
| 4127 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4128 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 4129 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4130 | if (LHS && RHS) // We have 2 user-defined types. |
| 4131 | return canAssignObjCInterfaces(LHS, RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4132 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4133 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4134 | } |
| 4135 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4136 | /// canAssignObjCInterfacesInBlockPointer - This routine is specifically written |
| 4137 | /// for providing type-safty for objective-c pointers used to pass/return |
| 4138 | /// arguments in block literals. When passed as arguments, passing 'A*' where |
| 4139 | /// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is |
| 4140 | /// not OK. For the return type, the opposite is not OK. |
| 4141 | bool ASTContext::canAssignObjCInterfacesInBlockPointer( |
| 4142 | const ObjCObjectPointerType *LHSOPT, |
| 4143 | const ObjCObjectPointerType *RHSOPT) { |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 4144 | if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4145 | return true; |
| 4146 | |
| 4147 | if (LHSOPT->isObjCBuiltinType()) { |
| 4148 | return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType(); |
| 4149 | } |
| 4150 | |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 4151 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4152 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 4153 | QualType(RHSOPT,0), |
| 4154 | false); |
| 4155 | |
| 4156 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 4157 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 4158 | if (LHS && RHS) { // We have 2 user-defined types. |
| 4159 | if (LHS != RHS) { |
| 4160 | if (LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
| 4161 | return false; |
| 4162 | if (RHS->getDecl()->isSuperClassOf(LHS->getDecl())) |
| 4163 | return true; |
| 4164 | } |
| 4165 | else |
| 4166 | return true; |
| 4167 | } |
| 4168 | return false; |
| 4169 | } |
| 4170 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4171 | /// getIntersectionOfProtocols - This routine finds the intersection of set |
| 4172 | /// of protocols inherited from two distinct objective-c pointer objects. |
| 4173 | /// It is used to build composite qualifier list of the composite type of |
| 4174 | /// the conditional expression involving two objective-c pointer objects. |
| 4175 | static |
| 4176 | void getIntersectionOfProtocols(ASTContext &Context, |
| 4177 | const ObjCObjectPointerType *LHSOPT, |
| 4178 | const ObjCObjectPointerType *RHSOPT, |
| 4179 | llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) { |
| 4180 | |
| 4181 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 4182 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 4183 | |
| 4184 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet; |
| 4185 | unsigned LHSNumProtocols = LHS->getNumProtocols(); |
| 4186 | if (LHSNumProtocols > 0) |
| 4187 | InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end()); |
| 4188 | else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 4189 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
| 4190 | Context.CollectInheritedProtocols(LHS->getDecl(), LHSInheritedProtocols); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4191 | InheritedProtocolSet.insert(LHSInheritedProtocols.begin(), |
| 4192 | LHSInheritedProtocols.end()); |
| 4193 | } |
| 4194 | |
| 4195 | unsigned RHSNumProtocols = RHS->getNumProtocols(); |
| 4196 | if (RHSNumProtocols > 0) { |
| 4197 | ObjCProtocolDecl **RHSProtocols = (ObjCProtocolDecl **)RHS->qual_begin(); |
| 4198 | for (unsigned i = 0; i < RHSNumProtocols; ++i) |
| 4199 | if (InheritedProtocolSet.count(RHSProtocols[i])) |
| 4200 | IntersectionOfProtocols.push_back(RHSProtocols[i]); |
| 4201 | } |
| 4202 | else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 4203 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols; |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4204 | Context.CollectInheritedProtocols(RHS->getDecl(), RHSInheritedProtocols); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 4205 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 4206 | RHSInheritedProtocols.begin(), |
| 4207 | E = RHSInheritedProtocols.end(); I != E; ++I) |
| 4208 | if (InheritedProtocolSet.count((*I))) |
| 4209 | IntersectionOfProtocols.push_back((*I)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4210 | } |
| 4211 | } |
| 4212 | |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4213 | /// areCommonBaseCompatible - Returns common base class of the two classes if |
| 4214 | /// one found. Note that this is O'2 algorithm. But it will be called as the |
| 4215 | /// last type comparison in a ?-exp of ObjC pointer types before a |
| 4216 | /// warning is issued. So, its invokation is extremely rare. |
| 4217 | QualType ASTContext::areCommonBaseCompatible( |
| 4218 | const ObjCObjectPointerType *LHSOPT, |
| 4219 | const ObjCObjectPointerType *RHSOPT) { |
| 4220 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 4221 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 4222 | if (!LHS || !RHS) |
| 4223 | return QualType(); |
| 4224 | |
| 4225 | while (const ObjCInterfaceDecl *LHSIDecl = LHS->getDecl()->getSuperClass()) { |
| 4226 | QualType LHSTy = getObjCInterfaceType(LHSIDecl); |
| 4227 | LHS = LHSTy->getAs<ObjCInterfaceType>(); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4228 | if (canAssignObjCInterfaces(LHS, RHS)) { |
| 4229 | llvm::SmallVector<ObjCProtocolDecl *, 8> IntersectionOfProtocols; |
| 4230 | getIntersectionOfProtocols(*this, |
| 4231 | LHSOPT, RHSOPT, IntersectionOfProtocols); |
| 4232 | if (IntersectionOfProtocols.empty()) |
| 4233 | LHSTy = getObjCObjectPointerType(LHSTy); |
| 4234 | else |
| 4235 | LHSTy = getObjCObjectPointerType(LHSTy, &IntersectionOfProtocols[0], |
| 4236 | IntersectionOfProtocols.size()); |
| 4237 | return LHSTy; |
| 4238 | } |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4239 | } |
| 4240 | |
| 4241 | return QualType(); |
| 4242 | } |
| 4243 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4244 | bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS, |
| 4245 | const ObjCInterfaceType *RHS) { |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4246 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 4247 | // the LHS. |
| 4248 | if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
| 4249 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4250 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4251 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 4252 | // protocol qualified at all, then we are good. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4253 | if (LHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4254 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4255 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4256 | // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it |
| 4257 | // isn't a superset. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4258 | if (RHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4259 | return true; // FIXME: should return false! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4260 | |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4261 | for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(), |
| 4262 | LHSPE = LHS->qual_end(); |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4263 | LHSPI != LHSPE; LHSPI++) { |
| 4264 | bool RHSImplementsProtocol = false; |
| 4265 | |
| 4266 | // If the RHS doesn't implement the protocol on the left, the types |
| 4267 | // are incompatible. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4268 | for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(), |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4269 | RHSPE = RHS->qual_end(); |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 4270 | RHSPI != RHSPE; RHSPI++) { |
| 4271 | if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) { |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4272 | RHSImplementsProtocol = true; |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 4273 | break; |
| 4274 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4275 | } |
| 4276 | // FIXME: For better diagnostics, consider passing back the protocol name. |
| 4277 | if (!RHSImplementsProtocol) |
| 4278 | return false; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4279 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4280 | // The RHS implements all protocols listed on the LHS. |
| 4281 | return true; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4282 | } |
| 4283 | |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4284 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 4285 | // get the "pointed to" types |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4286 | const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
| 4287 | const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4288 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4289 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4290 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4291 | |
| 4292 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 4293 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4294 | } |
| 4295 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4296 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4297 | /// both shall have the identically qualified version of a compatible type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4298 | /// 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] | 4299 | /// same. See 6.7.[2,3,5] for additional rules. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4300 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) { |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 4301 | if (getLangOptions().CPlusPlus) |
| 4302 | return hasSameType(LHS, RHS); |
| 4303 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4304 | return !mergeTypes(LHS, RHS).isNull(); |
| 4305 | } |
| 4306 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4307 | bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) { |
| 4308 | return !mergeTypes(LHS, RHS, true).isNull(); |
| 4309 | } |
| 4310 | |
| 4311 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs, |
| 4312 | bool OfBlockPointer) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4313 | const FunctionType *lbase = lhs->getAs<FunctionType>(); |
| 4314 | const FunctionType *rbase = rhs->getAs<FunctionType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4315 | const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 4316 | const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4317 | bool allLTypes = true; |
| 4318 | bool allRTypes = true; |
| 4319 | |
| 4320 | // Check return type |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4321 | QualType retType; |
| 4322 | if (OfBlockPointer) |
| 4323 | retType = mergeTypes(rbase->getResultType(), lbase->getResultType(), true); |
| 4324 | else |
| 4325 | retType = mergeTypes(lbase->getResultType(), rbase->getResultType()); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4326 | if (retType.isNull()) return QualType(); |
Fariborz Jahanian | 6de8b62 | 2010-02-10 00:32:12 +0000 | [diff] [blame] | 4327 | if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType())) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4328 | allLTypes = false; |
Fariborz Jahanian | 6de8b62 | 2010-02-10 00:32:12 +0000 | [diff] [blame] | 4329 | if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType())) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4330 | allRTypes = false; |
Mike Stump | 6dcbc29 | 2009-07-25 23:24:03 +0000 | [diff] [blame] | 4331 | // FIXME: double check this |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 4332 | // FIXME: should we error if lbase->getRegParmAttr() != 0 && |
| 4333 | // rbase->getRegParmAttr() != 0 && |
| 4334 | // lbase->getRegParmAttr() != rbase->getRegParmAttr()? |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 4335 | FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo(); |
| 4336 | FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo(); |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 4337 | unsigned RegParm = lbaseInfo.getRegParm() == 0 ? rbaseInfo.getRegParm() : |
| 4338 | lbaseInfo.getRegParm(); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 4339 | bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn(); |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 4340 | if (NoReturn != lbaseInfo.getNoReturn() || |
| 4341 | RegParm != lbaseInfo.getRegParm()) |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 4342 | allLTypes = false; |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 4343 | if (NoReturn != rbaseInfo.getNoReturn() || |
| 4344 | RegParm != rbaseInfo.getRegParm()) |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 4345 | allRTypes = false; |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 4346 | CallingConv lcc = lbaseInfo.getCC(); |
| 4347 | CallingConv rcc = rbaseInfo.getCC(); |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 4348 | // Compatible functions must have compatible calling conventions |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4349 | if (!isSameCallConv(lcc, rcc)) |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 4350 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4351 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4352 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 4353 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 4354 | "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4355 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 4356 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 4357 | |
| 4358 | // Compatible functions must have the same number of arguments |
| 4359 | if (lproto_nargs != rproto_nargs) |
| 4360 | return QualType(); |
| 4361 | |
| 4362 | // Variadic and non-variadic functions aren't compatible |
| 4363 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 4364 | return QualType(); |
| 4365 | |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 4366 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 4367 | return QualType(); |
| 4368 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4369 | // Check argument compatibility |
| 4370 | llvm::SmallVector<QualType, 10> types; |
| 4371 | for (unsigned i = 0; i < lproto_nargs; i++) { |
| 4372 | QualType largtype = lproto->getArgType(i).getUnqualifiedType(); |
| 4373 | QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4374 | QualType argtype = mergeTypes(largtype, rargtype, OfBlockPointer); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4375 | if (argtype.isNull()) return QualType(); |
| 4376 | types.push_back(argtype); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4377 | if (getCanonicalType(argtype) != getCanonicalType(largtype)) |
| 4378 | allLTypes = false; |
| 4379 | if (getCanonicalType(argtype) != getCanonicalType(rargtype)) |
| 4380 | allRTypes = false; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4381 | } |
| 4382 | if (allLTypes) return lhs; |
| 4383 | if (allRTypes) return rhs; |
| 4384 | return getFunctionType(retType, types.begin(), types.size(), |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 4385 | lproto->isVariadic(), lproto->getTypeQuals(), |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 4386 | false, false, 0, 0, |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 4387 | FunctionType::ExtInfo(NoReturn, RegParm, lcc)); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4388 | } |
| 4389 | |
| 4390 | if (lproto) allRTypes = false; |
| 4391 | if (rproto) allLTypes = false; |
| 4392 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4393 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4394 | if (proto) { |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 4395 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4396 | if (proto->isVariadic()) return QualType(); |
| 4397 | // Check that the types are compatible with the types that |
| 4398 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 4399 | // The only types actually affected are promotable integer |
| 4400 | // types and floats, which would be passed as a different |
| 4401 | // type depending on whether the prototype is visible. |
| 4402 | unsigned proto_nargs = proto->getNumArgs(); |
| 4403 | for (unsigned i = 0; i < proto_nargs; ++i) { |
| 4404 | QualType argTy = proto->getArgType(i); |
Douglas Gregor | b0f8eac | 2010-02-03 19:27:29 +0000 | [diff] [blame] | 4405 | |
| 4406 | // Look at the promotion type of enum types, since that is the type used |
| 4407 | // to pass enum values. |
| 4408 | if (const EnumType *Enum = argTy->getAs<EnumType>()) |
| 4409 | argTy = Enum->getDecl()->getPromotionType(); |
| 4410 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4411 | if (argTy->isPromotableIntegerType() || |
| 4412 | getCanonicalType(argTy).getUnqualifiedType() == FloatTy) |
| 4413 | return QualType(); |
| 4414 | } |
| 4415 | |
| 4416 | if (allLTypes) return lhs; |
| 4417 | if (allRTypes) return rhs; |
| 4418 | return getFunctionType(retType, proto->arg_type_begin(), |
Mike Stump | 2d3c191 | 2009-07-27 00:44:23 +0000 | [diff] [blame] | 4419 | proto->getNumArgs(), proto->isVariadic(), |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 4420 | proto->getTypeQuals(), |
| 4421 | false, false, 0, 0, |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 4422 | FunctionType::ExtInfo(NoReturn, RegParm, lcc)); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4423 | } |
| 4424 | |
| 4425 | if (allLTypes) return lhs; |
| 4426 | if (allRTypes) return rhs; |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 4427 | FunctionType::ExtInfo Info(NoReturn, RegParm, lcc); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 4428 | return getFunctionNoProtoType(retType, Info); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4429 | } |
| 4430 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4431 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, |
| 4432 | bool OfBlockPointer) { |
Bill Wendling | 43d6975 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 4433 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 4434 | // type is adjusted to "T" prior to any further analysis, the expression |
| 4435 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4436 | // expression is an lvalue unless the reference is an rvalue reference and |
| 4437 | // the expression is a function call (possibly inside parentheses). |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 4438 | assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?"); |
| 4439 | assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?"); |
| 4440 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4441 | QualType LHSCan = getCanonicalType(LHS), |
| 4442 | RHSCan = getCanonicalType(RHS); |
| 4443 | |
| 4444 | // If two types are identical, they are compatible. |
| 4445 | if (LHSCan == RHSCan) |
| 4446 | return LHS; |
| 4447 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4448 | // If the qualifiers are different, the types aren't compatible... mostly. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4449 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 4450 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4451 | if (LQuals != RQuals) { |
| 4452 | // If any of these qualifiers are different, we have a type |
| 4453 | // mismatch. |
| 4454 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 4455 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 4456 | return QualType(); |
| 4457 | |
| 4458 | // Exactly one GC qualifier difference is allowed: __strong is |
| 4459 | // okay if the other type has no GC qualifier but is an Objective |
| 4460 | // C object pointer (i.e. implicitly strong by default). We fix |
| 4461 | // this by pretending that the unqualified type was actually |
| 4462 | // qualified __strong. |
| 4463 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 4464 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 4465 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 4466 | |
| 4467 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 4468 | return QualType(); |
| 4469 | |
| 4470 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
| 4471 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); |
| 4472 | } |
| 4473 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
| 4474 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); |
| 4475 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4476 | return QualType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4477 | } |
| 4478 | |
| 4479 | // Okay, qualifiers are equal. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4480 | |
Eli Friedman | 852d63b | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 4481 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 4482 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4483 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4484 | // We want to consider the two function types to be the same for these |
| 4485 | // comparisons, just force one to the other. |
| 4486 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 4487 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 4c721d3 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 4488 | |
| 4489 | // Same as above for arrays |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 4490 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 4491 | LHSClass = Type::ConstantArray; |
| 4492 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 4493 | RHSClass = Type::ConstantArray; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4494 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 4495 | // Canonicalize ExtVector -> Vector. |
| 4496 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 4497 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4498 | |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 4499 | // If the canonical type classes don't match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4500 | if (LHSClass != RHSClass) { |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4501 | // 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] | 4502 | // a signed integer type, or an unsigned integer type. |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 4503 | // Compatibility is based on the underlying type, not the promotion |
| 4504 | // type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4505 | if (const EnumType* ETy = LHS->getAs<EnumType>()) { |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4506 | if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType()) |
| 4507 | return RHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 4508 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4509 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4510 | if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType()) |
| 4511 | return LHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 4512 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4513 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4514 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4515 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4516 | |
Steve Naroff | 4a74678 | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 4517 | // The canonical type classes match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4518 | switch (LHSClass) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4519 | #define TYPE(Class, Base) |
| 4520 | #define ABSTRACT_TYPE(Class, Base) |
John McCall | ad5e738 | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 4521 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4522 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 4523 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 4524 | #include "clang/AST/TypeNodes.def" |
| 4525 | assert(false && "Non-canonical and dependent types shouldn't get here"); |
| 4526 | return QualType(); |
| 4527 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4528 | case Type::LValueReference: |
| 4529 | case Type::RValueReference: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4530 | case Type::MemberPointer: |
| 4531 | assert(false && "C++ should never be in mergeTypes"); |
| 4532 | return QualType(); |
| 4533 | |
| 4534 | case Type::IncompleteArray: |
| 4535 | case Type::VariableArray: |
| 4536 | case Type::FunctionProto: |
| 4537 | case Type::ExtVector: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4538 | assert(false && "Types are eliminated above"); |
| 4539 | return QualType(); |
| 4540 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4541 | case Type::Pointer: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4542 | { |
| 4543 | // Merge two pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4544 | QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); |
| 4545 | QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4546 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee); |
| 4547 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 4548 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4549 | return LHS; |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 4550 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4551 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4552 | return getPointerType(ResultType); |
| 4553 | } |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 4554 | case Type::BlockPointer: |
| 4555 | { |
| 4556 | // Merge two block pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4557 | QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); |
| 4558 | QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4559 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer); |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 4560 | if (ResultType.isNull()) return QualType(); |
| 4561 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 4562 | return LHS; |
| 4563 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 4564 | return RHS; |
| 4565 | return getBlockPointerType(ResultType); |
| 4566 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4567 | case Type::ConstantArray: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4568 | { |
| 4569 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 4570 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 4571 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 4572 | return QualType(); |
| 4573 | |
| 4574 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 4575 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
| 4576 | QualType ResultType = mergeTypes(LHSElem, RHSElem); |
| 4577 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4578 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 4579 | return LHS; |
| 4580 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 4581 | return RHS; |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 4582 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 4583 | ArrayType::ArraySizeModifier(), 0); |
| 4584 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 4585 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4586 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 4587 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4588 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 4589 | return LHS; |
| 4590 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 4591 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4592 | if (LVAT) { |
| 4593 | // FIXME: This isn't correct! But tricky to implement because |
| 4594 | // the array's size has to be the size of LHS, but the type |
| 4595 | // has to be different. |
| 4596 | return LHS; |
| 4597 | } |
| 4598 | if (RVAT) { |
| 4599 | // FIXME: This isn't correct! But tricky to implement because |
| 4600 | // the array's size has to be the size of RHS, but the type |
| 4601 | // has to be different. |
| 4602 | return RHS; |
| 4603 | } |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 4604 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 4605 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4606 | return getIncompleteArrayType(ResultType, |
| 4607 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4608 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4609 | case Type::FunctionNoProto: |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4610 | return mergeFunctionTypes(LHS, RHS, OfBlockPointer); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4611 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4612 | case Type::Enum: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4613 | return QualType(); |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4614 | case Type::Builtin: |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 4615 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4616 | return QualType(); |
Daniel Dunbar | 64cfdb7 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 4617 | case Type::Complex: |
| 4618 | // Distinct complex types are incompatible. |
| 4619 | return QualType(); |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 4620 | case Type::Vector: |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 4621 | // FIXME: The merged type should be an ExtVector! |
John McCall | 1c471f3 | 2010-03-12 23:14:13 +0000 | [diff] [blame] | 4622 | if (areCompatVectorTypes(LHSCan->getAs<VectorType>(), |
| 4623 | RHSCan->getAs<VectorType>())) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4624 | return LHS; |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4625 | return QualType(); |
Cedric Venet | 61490e9 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 4626 | case Type::ObjCInterface: { |
Steve Naroff | 5fd659d | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 4627 | // Check if the interfaces are assignment compatible. |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 4628 | // FIXME: This should be type compatibility, e.g. whether |
| 4629 | // "LHS x; RHS x;" at global scope is legal. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4630 | const ObjCInterfaceType* LHSIface = LHS->getAs<ObjCInterfaceType>(); |
| 4631 | const ObjCInterfaceType* RHSIface = RHS->getAs<ObjCInterfaceType>(); |
Steve Naroff | 5fd659d | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 4632 | if (LHSIface && RHSIface && |
| 4633 | canAssignObjCInterfaces(LHSIface, RHSIface)) |
| 4634 | return LHS; |
| 4635 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4636 | return QualType(); |
Cedric Venet | 61490e9 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 4637 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4638 | case Type::ObjCObjectPointer: { |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4639 | if (OfBlockPointer) { |
| 4640 | if (canAssignObjCInterfacesInBlockPointer( |
| 4641 | LHS->getAs<ObjCObjectPointerType>(), |
| 4642 | RHS->getAs<ObjCObjectPointerType>())) |
| 4643 | return LHS; |
| 4644 | return QualType(); |
| 4645 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4646 | if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(), |
| 4647 | RHS->getAs<ObjCObjectPointerType>())) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4648 | return LHS; |
| 4649 | |
Steve Naroff | bc76dd0 | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 4650 | return QualType(); |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4651 | } |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4652 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4653 | |
| 4654 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4655 | } |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 4656 | |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 4657 | //===----------------------------------------------------------------------===// |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4658 | // Integer Predicates |
| 4659 | //===----------------------------------------------------------------------===// |
Chris Lattner | 88054de | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 4660 | |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4661 | unsigned ASTContext::getIntWidth(QualType T) { |
Sebastian Redl | 632d772 | 2009-11-05 21:10:57 +0000 | [diff] [blame] | 4662 | if (T->isBooleanType()) |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4663 | return 1; |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 4664 | if (EnumType *ET = dyn_cast<EnumType>(T)) |
Eli Friedman | 29a7f33 | 2009-12-10 22:29:29 +0000 | [diff] [blame] | 4665 | T = ET->getDecl()->getIntegerType(); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4666 | // For builtin types, just use the standard type sizing method |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4667 | return (unsigned)getTypeSize(T); |
| 4668 | } |
| 4669 | |
| 4670 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) { |
| 4671 | assert(T->isSignedIntegerType() && "Unexpected type"); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4672 | |
| 4673 | // Turn <4 x signed int> -> <4 x unsigned int> |
| 4674 | if (const VectorType *VTy = T->getAs<VectorType>()) |
| 4675 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4676 | VTy->getNumElements(), VTy->isAltiVec(), VTy->isPixel()); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4677 | |
| 4678 | // For enums, we return the unsigned version of the base type. |
| 4679 | if (const EnumType *ETy = T->getAs<EnumType>()) |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4680 | T = ETy->getDecl()->getIntegerType(); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4681 | |
| 4682 | const BuiltinType *BTy = T->getAs<BuiltinType>(); |
| 4683 | assert(BTy && "Unexpected signed integer type"); |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4684 | switch (BTy->getKind()) { |
| 4685 | case BuiltinType::Char_S: |
| 4686 | case BuiltinType::SChar: |
| 4687 | return UnsignedCharTy; |
| 4688 | case BuiltinType::Short: |
| 4689 | return UnsignedShortTy; |
| 4690 | case BuiltinType::Int: |
| 4691 | return UnsignedIntTy; |
| 4692 | case BuiltinType::Long: |
| 4693 | return UnsignedLongTy; |
| 4694 | case BuiltinType::LongLong: |
| 4695 | return UnsignedLongLongTy; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 4696 | case BuiltinType::Int128: |
| 4697 | return UnsignedInt128Ty; |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4698 | default: |
| 4699 | assert(0 && "Unexpected signed integer type"); |
| 4700 | return QualType(); |
| 4701 | } |
| 4702 | } |
| 4703 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 4704 | ExternalASTSource::~ExternalASTSource() { } |
| 4705 | |
| 4706 | void ExternalASTSource::PrintStats() { } |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4707 | |
| 4708 | |
| 4709 | //===----------------------------------------------------------------------===// |
| 4710 | // Builtin Type Computation |
| 4711 | //===----------------------------------------------------------------------===// |
| 4712 | |
| 4713 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
| 4714 | /// pointer over the consumed characters. This returns the resultant type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4715 | static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context, |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4716 | ASTContext::GetBuiltinTypeError &Error, |
| 4717 | bool AllowTypeModifiers = true) { |
| 4718 | // Modifiers. |
| 4719 | int HowLong = 0; |
| 4720 | bool Signed = false, Unsigned = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4721 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4722 | // Read the modifiers first. |
| 4723 | bool Done = false; |
| 4724 | while (!Done) { |
| 4725 | switch (*Str++) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4726 | default: Done = true; --Str; break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4727 | case 'S': |
| 4728 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 4729 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 4730 | Signed = true; |
| 4731 | break; |
| 4732 | case 'U': |
| 4733 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
| 4734 | assert(!Unsigned && "Can't use 'S' modifier multiple times!"); |
| 4735 | Unsigned = true; |
| 4736 | break; |
| 4737 | case 'L': |
| 4738 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 4739 | ++HowLong; |
| 4740 | break; |
| 4741 | } |
| 4742 | } |
| 4743 | |
| 4744 | QualType Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4745 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4746 | // Read the base type. |
| 4747 | switch (*Str++) { |
| 4748 | default: assert(0 && "Unknown builtin type letter!"); |
| 4749 | case 'v': |
| 4750 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 4751 | "Bad modifiers used with 'v'!"); |
| 4752 | Type = Context.VoidTy; |
| 4753 | break; |
| 4754 | case 'f': |
| 4755 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 4756 | "Bad modifiers used with 'f'!"); |
| 4757 | Type = Context.FloatTy; |
| 4758 | break; |
| 4759 | case 'd': |
| 4760 | assert(HowLong < 2 && !Signed && !Unsigned && |
| 4761 | "Bad modifiers used with 'd'!"); |
| 4762 | if (HowLong) |
| 4763 | Type = Context.LongDoubleTy; |
| 4764 | else |
| 4765 | Type = Context.DoubleTy; |
| 4766 | break; |
| 4767 | case 's': |
| 4768 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 4769 | if (Unsigned) |
| 4770 | Type = Context.UnsignedShortTy; |
| 4771 | else |
| 4772 | Type = Context.ShortTy; |
| 4773 | break; |
| 4774 | case 'i': |
| 4775 | if (HowLong == 3) |
| 4776 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 4777 | else if (HowLong == 2) |
| 4778 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 4779 | else if (HowLong == 1) |
| 4780 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 4781 | else |
| 4782 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 4783 | break; |
| 4784 | case 'c': |
| 4785 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 4786 | if (Signed) |
| 4787 | Type = Context.SignedCharTy; |
| 4788 | else if (Unsigned) |
| 4789 | Type = Context.UnsignedCharTy; |
| 4790 | else |
| 4791 | Type = Context.CharTy; |
| 4792 | break; |
| 4793 | case 'b': // boolean |
| 4794 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 4795 | Type = Context.BoolTy; |
| 4796 | break; |
| 4797 | case 'z': // size_t. |
| 4798 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 4799 | Type = Context.getSizeType(); |
| 4800 | break; |
| 4801 | case 'F': |
| 4802 | Type = Context.getCFConstantStringType(); |
| 4803 | break; |
| 4804 | case 'a': |
| 4805 | Type = Context.getBuiltinVaListType(); |
| 4806 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 4807 | break; |
| 4808 | case 'A': |
| 4809 | // This is a "reference" to a va_list; however, what exactly |
| 4810 | // this means depends on how va_list is defined. There are two |
| 4811 | // different kinds of va_list: ones passed by value, and ones |
| 4812 | // passed by reference. An example of a by-value va_list is |
| 4813 | // x86, where va_list is a char*. An example of by-ref va_list |
| 4814 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 4815 | // we want this argument to be a char*&; for x86-64, we want |
| 4816 | // it to be a __va_list_tag*. |
| 4817 | Type = Context.getBuiltinVaListType(); |
| 4818 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 4819 | if (Type->isArrayType()) { |
| 4820 | Type = Context.getArrayDecayedType(Type); |
| 4821 | } else { |
| 4822 | Type = Context.getLValueReferenceType(Type); |
| 4823 | } |
| 4824 | break; |
| 4825 | case 'V': { |
| 4826 | char *End; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4827 | unsigned NumElements = strtoul(Str, &End, 10); |
| 4828 | assert(End != Str && "Missing vector size"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4829 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4830 | Str = End; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4831 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4832 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false); |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4833 | // FIXME: Don't know what to do about AltiVec. |
| 4834 | Type = Context.getVectorType(ElementType, NumElements, false, false); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4835 | break; |
| 4836 | } |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 4837 | case 'X': { |
| 4838 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false); |
| 4839 | Type = Context.getComplexType(ElementType); |
| 4840 | break; |
| 4841 | } |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 4842 | case 'P': |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 4843 | Type = Context.getFILEType(); |
| 4844 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 4845 | Error = ASTContext::GE_Missing_stdio; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4846 | return QualType(); |
| 4847 | } |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 4848 | break; |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 4849 | case 'J': |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 4850 | if (Signed) |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 4851 | Type = Context.getsigjmp_bufType(); |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 4852 | else |
| 4853 | Type = Context.getjmp_bufType(); |
| 4854 | |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 4855 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 4856 | Error = ASTContext::GE_Missing_setjmp; |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 4857 | return QualType(); |
| 4858 | } |
| 4859 | break; |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 4860 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4861 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4862 | if (!AllowTypeModifiers) |
| 4863 | return Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4864 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4865 | Done = false; |
| 4866 | while (!Done) { |
John McCall | 187ab37 | 2010-03-12 04:21:28 +0000 | [diff] [blame] | 4867 | switch (char c = *Str++) { |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4868 | default: Done = true; --Str; break; |
| 4869 | case '*': |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4870 | case '&': |
John McCall | 187ab37 | 2010-03-12 04:21:28 +0000 | [diff] [blame] | 4871 | { |
| 4872 | // Both pointers and references can have their pointee types |
| 4873 | // qualified with an address space. |
| 4874 | char *End; |
| 4875 | unsigned AddrSpace = strtoul(Str, &End, 10); |
| 4876 | if (End != Str && AddrSpace != 0) { |
| 4877 | Type = Context.getAddrSpaceQualType(Type, AddrSpace); |
| 4878 | Str = End; |
| 4879 | } |
| 4880 | } |
| 4881 | if (c == '*') |
| 4882 | Type = Context.getPointerType(Type); |
| 4883 | else |
| 4884 | Type = Context.getLValueReferenceType(Type); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4885 | break; |
| 4886 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 4887 | case 'C': |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4888 | Type = Type.withConst(); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4889 | break; |
Fariborz Jahanian | 013af39 | 2010-01-26 22:48:42 +0000 | [diff] [blame] | 4890 | case 'D': |
| 4891 | Type = Context.getVolatileType(Type); |
| 4892 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4893 | } |
| 4894 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4895 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4896 | return Type; |
| 4897 | } |
| 4898 | |
| 4899 | /// GetBuiltinType - Return the type for the specified builtin. |
| 4900 | QualType ASTContext::GetBuiltinType(unsigned id, |
| 4901 | GetBuiltinTypeError &Error) { |
| 4902 | const char *TypeStr = BuiltinInfo.GetTypeString(id); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4903 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4904 | llvm::SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4905 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4906 | Error = GE_None; |
| 4907 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error); |
| 4908 | if (Error != GE_None) |
| 4909 | return QualType(); |
| 4910 | while (TypeStr[0] && TypeStr[0] != '.') { |
| 4911 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error); |
| 4912 | if (Error != GE_None) |
| 4913 | return QualType(); |
| 4914 | |
| 4915 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 4916 | if (Ty->isArrayType()) |
| 4917 | Ty = getArrayDecayedType(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4918 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4919 | ArgTypes.push_back(Ty); |
| 4920 | } |
| 4921 | |
| 4922 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 4923 | "'.' should only occur at end of builtin type list!"); |
| 4924 | |
| 4925 | // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);". |
| 4926 | if (ArgTypes.size() == 0 && TypeStr[0] == '.') |
| 4927 | return getFunctionNoProtoType(ResType); |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 4928 | |
| 4929 | // FIXME: Should we create noreturn types? |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4930 | return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 4931 | TypeStr[0] == '.', 0, false, false, 0, 0, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 4932 | FunctionType::ExtInfo()); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4933 | } |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4934 | |
| 4935 | QualType |
| 4936 | ASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) { |
| 4937 | // Perform the usual unary conversions. We do this early so that |
| 4938 | // integral promotions to "int" can allow us to exit early, in the |
| 4939 | // lhs == rhs check. Also, for conversion purposes, we ignore any |
| 4940 | // qualifiers. For example, "const float" and "float" are |
| 4941 | // equivalent. |
| 4942 | if (lhs->isPromotableIntegerType()) |
| 4943 | lhs = getPromotedIntegerType(lhs); |
| 4944 | else |
| 4945 | lhs = lhs.getUnqualifiedType(); |
| 4946 | if (rhs->isPromotableIntegerType()) |
| 4947 | rhs = getPromotedIntegerType(rhs); |
| 4948 | else |
| 4949 | rhs = rhs.getUnqualifiedType(); |
| 4950 | |
| 4951 | // If both types are identical, no conversion is needed. |
| 4952 | if (lhs == rhs) |
| 4953 | return lhs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4954 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4955 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 4956 | // The caller can deal with this (e.g. pointer + int). |
| 4957 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 4958 | return lhs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4959 | |
| 4960 | // At this point, we have two different arithmetic types. |
| 4961 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4962 | // Handle complex types first (C99 6.3.1.8p1). |
| 4963 | if (lhs->isComplexType() || rhs->isComplexType()) { |
| 4964 | // if we have an integer operand, the result is the complex type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4965 | if (rhs->isIntegerType() || rhs->isComplexIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4966 | // convert the rhs to the lhs complex type. |
| 4967 | return lhs; |
| 4968 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4969 | if (lhs->isIntegerType() || lhs->isComplexIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4970 | // convert the lhs to the rhs complex type. |
| 4971 | return rhs; |
| 4972 | } |
| 4973 | // This handles complex/complex, complex/float, or float/complex. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4974 | // When both operands are complex, the shorter operand is converted to the |
| 4975 | // type of the longer, and that is the type of the result. This corresponds |
| 4976 | // to what is done when combining two real floating-point operands. |
| 4977 | // The fun begins when size promotion occur across type domains. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4978 | // 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] | 4979 | // floating-point type, the less precise type is converted, within it's |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4980 | // 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] | 4981 | // when combining a "long double" with a "double _Complex", the |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4982 | // "double _Complex" is promoted to "long double _Complex". |
| 4983 | int result = getFloatingTypeOrder(lhs, rhs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4984 | |
| 4985 | if (result > 0) { // The left side is bigger, convert rhs. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4986 | rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4987 | } else if (result < 0) { // The right side is bigger, convert lhs. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4988 | lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4989 | } |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4990 | // At this point, lhs and rhs have the same rank/size. Now, make sure the |
| 4991 | // domains match. This is a requirement for our implementation, C99 |
| 4992 | // does not require this promotion. |
| 4993 | if (lhs != rhs) { // Domains don't match, we have complex/float mix. |
| 4994 | if (lhs->isRealFloatingType()) { // handle "double, _Complex double". |
| 4995 | return rhs; |
| 4996 | } else { // handle "_Complex double, double". |
| 4997 | return lhs; |
| 4998 | } |
| 4999 | } |
| 5000 | return lhs; // The domain/size match exactly. |
| 5001 | } |
| 5002 | // Now handle "real" floating types (i.e. float, double, long double). |
| 5003 | if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) { |
| 5004 | // if we have an integer operand, the result is the real floating type. |
| 5005 | if (rhs->isIntegerType()) { |
| 5006 | // convert rhs to the lhs floating point type. |
| 5007 | return lhs; |
| 5008 | } |
| 5009 | if (rhs->isComplexIntegerType()) { |
| 5010 | // convert rhs to the complex floating point type. |
| 5011 | return getComplexType(lhs); |
| 5012 | } |
| 5013 | if (lhs->isIntegerType()) { |
| 5014 | // convert lhs to the rhs floating point type. |
| 5015 | return rhs; |
| 5016 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5017 | if (lhs->isComplexIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5018 | // convert lhs to the complex floating point type. |
| 5019 | return getComplexType(rhs); |
| 5020 | } |
| 5021 | // We have two real floating types, float/complex combos were handled above. |
| 5022 | // Convert the smaller operand to the bigger result. |
| 5023 | int result = getFloatingTypeOrder(lhs, rhs); |
| 5024 | if (result > 0) // convert the rhs |
| 5025 | return lhs; |
| 5026 | assert(result < 0 && "illegal float comparison"); |
| 5027 | return rhs; // convert the lhs |
| 5028 | } |
| 5029 | if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) { |
| 5030 | // Handle GCC complex int extension. |
| 5031 | const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType(); |
| 5032 | const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType(); |
| 5033 | |
| 5034 | if (lhsComplexInt && rhsComplexInt) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5035 | if (getIntegerTypeOrder(lhsComplexInt->getElementType(), |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5036 | rhsComplexInt->getElementType()) >= 0) |
| 5037 | return lhs; // convert the rhs |
| 5038 | return rhs; |
| 5039 | } else if (lhsComplexInt && rhs->isIntegerType()) { |
| 5040 | // convert the rhs to the lhs complex type. |
| 5041 | return lhs; |
| 5042 | } else if (rhsComplexInt && lhs->isIntegerType()) { |
| 5043 | // convert the lhs to the rhs complex type. |
| 5044 | return rhs; |
| 5045 | } |
| 5046 | } |
| 5047 | // Finally, we have two differing integer types. |
| 5048 | // The rules for this case are in C99 6.3.1.8 |
| 5049 | int compare = getIntegerTypeOrder(lhs, rhs); |
| 5050 | bool lhsSigned = lhs->isSignedIntegerType(), |
| 5051 | rhsSigned = rhs->isSignedIntegerType(); |
| 5052 | QualType destType; |
| 5053 | if (lhsSigned == rhsSigned) { |
| 5054 | // Same signedness; use the higher-ranked type |
| 5055 | destType = compare >= 0 ? lhs : rhs; |
| 5056 | } else if (compare != (lhsSigned ? 1 : -1)) { |
| 5057 | // The unsigned type has greater than or equal rank to the |
| 5058 | // signed type, so use the unsigned type |
| 5059 | destType = lhsSigned ? rhs : lhs; |
| 5060 | } else if (getIntWidth(lhs) != getIntWidth(rhs)) { |
| 5061 | // The two types are different widths; if we are here, that |
| 5062 | // means the signed type is larger than the unsigned type, so |
| 5063 | // use the signed type. |
| 5064 | destType = lhsSigned ? lhs : rhs; |
| 5065 | } else { |
| 5066 | // The signed type is higher-ranked than the unsigned type, |
| 5067 | // but isn't actually any bigger (like unsigned int and long |
| 5068 | // on most 32-bit systems). Use the unsigned type corresponding |
| 5069 | // to the signed type. |
| 5070 | destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs); |
| 5071 | } |
| 5072 | return destType; |
| 5073 | } |