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), |
| 46 | SourceMgr(SM), LangOpts(LOpts), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 47 | LoadedExternalComments(false), FreeMemory(FreeMem), Target(t), |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 48 | Idents(idents), Selectors(sels), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 49 | BuiltinInfo(builtins), ExternalSource(0), PrintingPolicy(LOpts) { |
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(); |
| 62 | |
Ted Kremenek | bbfd68d | 2009-12-23 21:13:52 +0000 | [diff] [blame] | 63 | if (FreeMemory) { |
| 64 | // Deallocate all the types. |
| 65 | while (!Types.empty()) { |
| 66 | Types.back()->Destroy(*this); |
| 67 | Types.pop_back(); |
| 68 | } |
Eli Friedman | b26153c | 2008-05-27 03:08:09 +0000 | [diff] [blame] | 69 | |
Ted Kremenek | bbfd68d | 2009-12-23 21:13:52 +0000 | [diff] [blame] | 70 | for (llvm::FoldingSet<ExtQuals>::iterator |
| 71 | I = ExtQualNodes.begin(), E = ExtQualNodes.end(); I != E; ) { |
| 72 | // Increment in loop to prevent using deallocated memory. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 73 | Deallocate(&*I++); |
Nuno Lopes | b74668e | 2008-12-17 22:30:25 +0000 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
Ted Kremenek | bbfd68d | 2009-12-23 21:13:52 +0000 | [diff] [blame] | 77 | for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator |
| 78 | I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) { |
| 79 | // Increment in loop to prevent using deallocated memory. |
| 80 | ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second); |
| 81 | delete R; |
| 82 | } |
| 83 | |
| 84 | for (llvm::DenseMap<const ObjCContainerDecl*, |
| 85 | const ASTRecordLayout*>::iterator |
| 86 | I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) { |
| 87 | // Increment in loop to prevent using deallocated memory. |
| 88 | ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second); |
| 89 | delete R; |
Nuno Lopes | b74668e | 2008-12-17 22:30:25 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 92 | // Destroy nested-name-specifiers. |
Douglas Gregor | 1ae0afa | 2009-03-27 23:54:10 +0000 | [diff] [blame] | 93 | for (llvm::FoldingSet<NestedNameSpecifier>::iterator |
| 94 | NNS = NestedNameSpecifiers.begin(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 95 | NNSEnd = NestedNameSpecifiers.end(); |
Ted Kremenek | bbfd68d | 2009-12-23 21:13:52 +0000 | [diff] [blame] | 96 | NNS != NNSEnd; ) { |
| 97 | // Increment in loop to prevent using deallocated memory. |
Douglas Gregor | 1ae0afa | 2009-03-27 23:54:10 +0000 | [diff] [blame] | 98 | (*NNS++).Destroy(*this); |
Ted Kremenek | bbfd68d | 2009-12-23 21:13:52 +0000 | [diff] [blame] | 99 | } |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 100 | |
| 101 | if (GlobalNestedNameSpecifier) |
| 102 | GlobalNestedNameSpecifier->Destroy(*this); |
| 103 | |
Eli Friedman | b26153c | 2008-05-27 03:08:09 +0000 | [diff] [blame] | 104 | TUDecl->Destroy(*this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 107 | void |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 108 | ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) { |
| 109 | ExternalSource.reset(Source.take()); |
| 110 | } |
| 111 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 112 | void ASTContext::PrintStats() const { |
| 113 | fprintf(stderr, "*** AST Context Stats:\n"); |
| 114 | fprintf(stderr, " %d types total.\n", (int)Types.size()); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 115 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 116 | unsigned counts[] = { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 117 | #define TYPE(Name, Parent) 0, |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 118 | #define ABSTRACT_TYPE(Name, Parent) |
| 119 | #include "clang/AST/TypeNodes.def" |
| 120 | 0 // Extra |
| 121 | }; |
Douglas Gregor | c2ee10d | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 122 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 123 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 124 | Type *T = Types[i]; |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 125 | counts[(unsigned)T->getTypeClass()]++; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 128 | unsigned Idx = 0; |
| 129 | unsigned TotalBytes = 0; |
| 130 | #define TYPE(Name, Parent) \ |
| 131 | if (counts[Idx]) \ |
| 132 | fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \ |
| 133 | TotalBytes += counts[Idx] * sizeof(Name##Type); \ |
| 134 | ++Idx; |
| 135 | #define ABSTRACT_TYPE(Name, Parent) |
| 136 | #include "clang/AST/TypeNodes.def" |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 137 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 138 | fprintf(stderr, "Total bytes = %d\n", int(TotalBytes)); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 139 | |
| 140 | if (ExternalSource.get()) { |
| 141 | fprintf(stderr, "\n"); |
| 142 | ExternalSource->PrintStats(); |
| 143 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 147 | void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 148 | BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 149 | R = CanQualType::CreateUnsafe(QualType(Ty, 0)); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 150 | Types.push_back(Ty); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 153 | void ASTContext::InitBuiltinTypes() { |
| 154 | assert(VoidTy.isNull() && "Context reinitialized?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 156 | // C99 6.2.5p19. |
| 157 | InitBuiltinType(VoidTy, BuiltinType::Void); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 158 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 159 | // C99 6.2.5p2. |
| 160 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
| 161 | // C99 6.2.5p3. |
Eli Friedman | 15b9176 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 162 | if (LangOpts.CharIsSigned) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 163 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 164 | else |
| 165 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
| 166 | // C99 6.2.5p4. |
| 167 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 168 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 169 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 170 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 171 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 172 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 173 | // C99 6.2.5p6. |
| 174 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 175 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 176 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 177 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 178 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 179 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 180 | // C99 6.2.5p10. |
| 181 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 182 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 183 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 184 | |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 185 | // GNU extension, 128-bit integers. |
| 186 | InitBuiltinType(Int128Ty, BuiltinType::Int128); |
| 187 | InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128); |
| 188 | |
Chris Lattner | 3a25032 | 2009-02-26 23:43:47 +0000 | [diff] [blame] | 189 | if (LangOpts.CPlusPlus) // C++ 3.9.1p5 |
| 190 | InitBuiltinType(WCharTy, BuiltinType::WChar); |
| 191 | else // C99 |
| 192 | WCharTy = getFromTargetType(Target.getWCharType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 193 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 194 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 195 | InitBuiltinType(Char16Ty, BuiltinType::Char16); |
| 196 | else // C99 |
| 197 | Char16Ty = getFromTargetType(Target.getChar16Type()); |
| 198 | |
| 199 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 200 | InitBuiltinType(Char32Ty, BuiltinType::Char32); |
| 201 | else // C99 |
| 202 | Char32Ty = getFromTargetType(Target.getChar32Type()); |
| 203 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 204 | // Placeholder type for functions. |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 205 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
| 206 | |
| 207 | // Placeholder type for type-dependent expressions whose type is |
| 208 | // completely unknown. No code should ever check a type against |
| 209 | // DependentTy and users should never see it; however, it is here to |
| 210 | // help diagnose failures to properly check for type-dependent |
| 211 | // expressions. |
| 212 | InitBuiltinType(DependentTy, BuiltinType::Dependent); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 213 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 214 | // Placeholder type for C++0x auto declarations whose real type has |
Anders Carlsson | e89d159 | 2009-06-26 18:41:36 +0000 | [diff] [blame] | 215 | // not yet been deduced. |
| 216 | InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 217 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 218 | // C99 6.2.5p11. |
| 219 | FloatComplexTy = getComplexType(FloatTy); |
| 220 | DoubleComplexTy = getComplexType(DoubleTy); |
| 221 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 222 | |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 223 | BuiltinVaListType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 224 | |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 225 | // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope(). |
| 226 | ObjCIdTypedefType = QualType(); |
| 227 | ObjCClassTypedefType = QualType(); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 228 | ObjCSelTypedefType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 229 | |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 230 | // Builtin types for 'id', 'Class', and 'SEL'. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 231 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); |
| 232 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 233 | InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 234 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 235 | ObjCConstantStringType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 236 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 237 | // void * type |
| 238 | VoidPtrTy = getPointerType(VoidTy); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 239 | |
| 240 | // nullptr type (C++0x 2.14.7) |
| 241 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 244 | MemberSpecializationInfo * |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 245 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 246 | assert(Var->isStaticDataMember() && "Not a static data member"); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 247 | llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 248 | = InstantiatedFromStaticDataMember.find(Var); |
| 249 | if (Pos == InstantiatedFromStaticDataMember.end()) |
| 250 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 251 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 252 | return Pos->second; |
| 253 | } |
| 254 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 255 | void |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 256 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, |
| 257 | TemplateSpecializationKind TSK) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 258 | assert(Inst->isStaticDataMember() && "Not a static data member"); |
| 259 | assert(Tmpl->isStaticDataMember() && "Not a static data member"); |
| 260 | assert(!InstantiatedFromStaticDataMember[Inst] && |
| 261 | "Already noted what static data member was instantiated from"); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 262 | InstantiatedFromStaticDataMember[Inst] |
| 263 | = new (*this) MemberSpecializationInfo(Tmpl, TSK); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 264 | } |
| 265 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 266 | NamedDecl * |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 267 | ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 268 | llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 269 | = InstantiatedFromUsingDecl.find(UUD); |
| 270 | if (Pos == InstantiatedFromUsingDecl.end()) |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 271 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 272 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 273 | return Pos->second; |
| 274 | } |
| 275 | |
| 276 | void |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 277 | ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) { |
| 278 | assert((isa<UsingDecl>(Pattern) || |
| 279 | isa<UnresolvedUsingValueDecl>(Pattern) || |
| 280 | isa<UnresolvedUsingTypenameDecl>(Pattern)) && |
| 281 | "pattern decl is not a using decl"); |
| 282 | assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists"); |
| 283 | InstantiatedFromUsingDecl[Inst] = Pattern; |
| 284 | } |
| 285 | |
| 286 | UsingShadowDecl * |
| 287 | ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) { |
| 288 | llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos |
| 289 | = InstantiatedFromUsingShadowDecl.find(Inst); |
| 290 | if (Pos == InstantiatedFromUsingShadowDecl.end()) |
| 291 | return 0; |
| 292 | |
| 293 | return Pos->second; |
| 294 | } |
| 295 | |
| 296 | void |
| 297 | ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, |
| 298 | UsingShadowDecl *Pattern) { |
| 299 | assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists"); |
| 300 | InstantiatedFromUsingShadowDecl[Inst] = Pattern; |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 303 | FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { |
| 304 | llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos |
| 305 | = InstantiatedFromUnnamedFieldDecl.find(Field); |
| 306 | if (Pos == InstantiatedFromUnnamedFieldDecl.end()) |
| 307 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 308 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 309 | return Pos->second; |
| 310 | } |
| 311 | |
| 312 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, |
| 313 | FieldDecl *Tmpl) { |
| 314 | assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed"); |
| 315 | assert(!Tmpl->getDeclName() && "Template field decl is not unnamed"); |
| 316 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] && |
| 317 | "Already noted what unnamed field was instantiated from"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 318 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 319 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; |
| 320 | } |
| 321 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 322 | namespace { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 323 | class BeforeInTranslationUnit |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 324 | : std::binary_function<SourceRange, SourceRange, bool> { |
| 325 | SourceManager *SourceMgr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 326 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 327 | public: |
| 328 | explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 329 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 330 | bool operator()(SourceRange X, SourceRange Y) { |
| 331 | return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin()); |
| 332 | } |
| 333 | }; |
| 334 | } |
| 335 | |
| 336 | /// \brief Determine whether the given comment is a Doxygen-style comment. |
| 337 | /// |
| 338 | /// \param Start the start of the comment text. |
| 339 | /// |
| 340 | /// \param End the end of the comment text. |
| 341 | /// |
| 342 | /// \param Member whether we want to check whether this is a member comment |
| 343 | /// (which requires a < after the Doxygen-comment delimiter). Otherwise, |
| 344 | /// we only return true when we find a non-member comment. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 345 | static bool |
| 346 | isDoxygenComment(SourceManager &SourceMgr, SourceRange Comment, |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 347 | bool Member = false) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 348 | const char *BufferStart |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 349 | = SourceMgr.getBufferData(SourceMgr.getFileID(Comment.getBegin())).first; |
| 350 | const char *Start = BufferStart + SourceMgr.getFileOffset(Comment.getBegin()); |
| 351 | const char* End = BufferStart + SourceMgr.getFileOffset(Comment.getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 352 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 353 | if (End - Start < 4) |
| 354 | return false; |
| 355 | |
| 356 | assert(Start[0] == '/' && "Not a comment?"); |
| 357 | if (Start[1] == '*' && !(Start[2] == '!' || Start[2] == '*')) |
| 358 | return false; |
| 359 | if (Start[1] == '/' && !(Start[2] == '!' || Start[2] == '/')) |
| 360 | return false; |
| 361 | |
| 362 | return (Start[3] == '<') == Member; |
| 363 | } |
| 364 | |
| 365 | /// \brief Retrieve the comment associated with the given declaration, if |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 366 | /// it has one. |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 367 | const char *ASTContext::getCommentForDecl(const Decl *D) { |
| 368 | if (!D) |
| 369 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 370 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 371 | // Check whether we have cached a comment string for this declaration |
| 372 | // already. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 373 | llvm::DenseMap<const Decl *, std::string>::iterator Pos |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 374 | = DeclComments.find(D); |
| 375 | if (Pos != DeclComments.end()) |
| 376 | return Pos->second.c_str(); |
| 377 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 378 | // If we have an external AST source and have not yet loaded comments from |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 379 | // that source, do so now. |
| 380 | if (ExternalSource && !LoadedExternalComments) { |
| 381 | std::vector<SourceRange> LoadedComments; |
| 382 | ExternalSource->ReadComments(LoadedComments); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 383 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 384 | if (!LoadedComments.empty()) |
| 385 | Comments.insert(Comments.begin(), LoadedComments.begin(), |
| 386 | LoadedComments.end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 387 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 388 | LoadedExternalComments = true; |
| 389 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 390 | |
| 391 | // If there are no comments anywhere, we won't find anything. |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 392 | if (Comments.empty()) |
| 393 | return 0; |
| 394 | |
| 395 | // If the declaration doesn't map directly to a location in a file, we |
| 396 | // can't find the comment. |
| 397 | SourceLocation DeclStartLoc = D->getLocStart(); |
| 398 | if (DeclStartLoc.isInvalid() || !DeclStartLoc.isFileID()) |
| 399 | return 0; |
| 400 | |
| 401 | // Find the comment that occurs just before this declaration. |
| 402 | std::vector<SourceRange>::iterator LastComment |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 403 | = std::lower_bound(Comments.begin(), Comments.end(), |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 404 | SourceRange(DeclStartLoc), |
| 405 | BeforeInTranslationUnit(&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 | // Decompose the location for the start of the declaration and find the |
| 408 | // beginning of the file buffer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 409 | std::pair<FileID, unsigned> DeclStartDecomp |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 410 | = SourceMgr.getDecomposedLoc(DeclStartLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 411 | const char *FileBufferStart |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 412 | = SourceMgr.getBufferData(DeclStartDecomp.first).first; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 413 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 414 | // First check whether we have a comment for a member. |
| 415 | if (LastComment != Comments.end() && |
| 416 | !isa<TagDecl>(D) && !isa<NamespaceDecl>(D) && |
| 417 | isDoxygenComment(SourceMgr, *LastComment, true)) { |
| 418 | std::pair<FileID, unsigned> LastCommentEndDecomp |
| 419 | = SourceMgr.getDecomposedLoc(LastComment->getEnd()); |
| 420 | if (DeclStartDecomp.first == LastCommentEndDecomp.first && |
| 421 | SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 422 | == SourceMgr.getLineNumber(LastCommentEndDecomp.first, |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 423 | LastCommentEndDecomp.second)) { |
| 424 | // The Doxygen member comment comes after the declaration starts and |
| 425 | // is on the same line and in the same file as the declaration. This |
| 426 | // is the comment we want. |
| 427 | std::string &Result = DeclComments[D]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 428 | Result.append(FileBufferStart + |
| 429 | SourceMgr.getFileOffset(LastComment->getBegin()), |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 430 | FileBufferStart + LastCommentEndDecomp.second + 1); |
| 431 | return Result.c_str(); |
| 432 | } |
| 433 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 434 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 435 | if (LastComment == Comments.begin()) |
| 436 | return 0; |
| 437 | --LastComment; |
| 438 | |
| 439 | // Decompose the end of the comment. |
| 440 | std::pair<FileID, unsigned> LastCommentEndDecomp |
| 441 | = SourceMgr.getDecomposedLoc(LastComment->getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 442 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 443 | // If the comment and the declaration aren't in the same file, then they |
| 444 | // aren't related. |
| 445 | if (DeclStartDecomp.first != LastCommentEndDecomp.first) |
| 446 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 447 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 448 | // Check that we actually have a Doxygen comment. |
| 449 | if (!isDoxygenComment(SourceMgr, *LastComment)) |
| 450 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 451 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 452 | // Compute the starting line for the declaration and for the end of the |
| 453 | // comment (this is expensive). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 454 | unsigned DeclStartLine |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 455 | = SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second); |
| 456 | unsigned CommentEndLine |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 457 | = SourceMgr.getLineNumber(LastCommentEndDecomp.first, |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 458 | LastCommentEndDecomp.second); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 459 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 460 | // If the comment does not end on the line prior to the declaration, then |
| 461 | // the comment is not associated with the declaration at all. |
| 462 | if (CommentEndLine + 1 != DeclStartLine) |
| 463 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 464 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 465 | // We have a comment, but there may be more comments on the previous lines. |
| 466 | // Keep looking so long as the comments are still Doxygen comments and are |
| 467 | // still adjacent. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 468 | unsigned ExpectedLine |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 469 | = SourceMgr.getSpellingLineNumber(LastComment->getBegin()) - 1; |
| 470 | std::vector<SourceRange>::iterator FirstComment = LastComment; |
| 471 | while (FirstComment != Comments.begin()) { |
| 472 | // Look at the previous comment |
| 473 | --FirstComment; |
| 474 | std::pair<FileID, unsigned> Decomp |
| 475 | = SourceMgr.getDecomposedLoc(FirstComment->getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 476 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 477 | // If this previous comment is in a different file, we're done. |
| 478 | if (Decomp.first != DeclStartDecomp.first) { |
| 479 | ++FirstComment; |
| 480 | break; |
| 481 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 482 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 483 | // If this comment is not a Doxygen comment, we're done. |
| 484 | if (!isDoxygenComment(SourceMgr, *FirstComment)) { |
| 485 | ++FirstComment; |
| 486 | break; |
| 487 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 488 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 489 | // If the line number is not what we expected, we're done. |
| 490 | unsigned Line = SourceMgr.getLineNumber(Decomp.first, Decomp.second); |
| 491 | if (Line != ExpectedLine) { |
| 492 | ++FirstComment; |
| 493 | break; |
| 494 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 495 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 496 | // Set the next expected line number. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 497 | ExpectedLine |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 498 | = SourceMgr.getSpellingLineNumber(FirstComment->getBegin()) - 1; |
| 499 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 500 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 501 | // The iterator range [FirstComment, LastComment] contains all of the |
| 502 | // BCPL comments that, together, are associated with this declaration. |
| 503 | // Form a single comment block string for this declaration that concatenates |
| 504 | // all of these comments. |
| 505 | std::string &Result = DeclComments[D]; |
| 506 | while (FirstComment != LastComment) { |
| 507 | std::pair<FileID, unsigned> DecompStart |
| 508 | = SourceMgr.getDecomposedLoc(FirstComment->getBegin()); |
| 509 | std::pair<FileID, unsigned> DecompEnd |
| 510 | = SourceMgr.getDecomposedLoc(FirstComment->getEnd()); |
| 511 | Result.append(FileBufferStart + DecompStart.second, |
| 512 | FileBufferStart + DecompEnd.second + 1); |
| 513 | ++FirstComment; |
| 514 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 515 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 516 | // Append the last comment line. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 517 | Result.append(FileBufferStart + |
| 518 | SourceMgr.getFileOffset(LastComment->getBegin()), |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 519 | FileBufferStart + LastCommentEndDecomp.second + 1); |
| 520 | return Result.c_str(); |
| 521 | } |
| 522 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 523 | //===----------------------------------------------------------------------===// |
| 524 | // Type Sizing and Analysis |
| 525 | //===----------------------------------------------------------------------===// |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 526 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 527 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 528 | /// scalar floating point type. |
| 529 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 530 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 531 | assert(BT && "Not a floating point type!"); |
| 532 | switch (BT->getKind()) { |
| 533 | default: assert(0 && "Not a floating point type!"); |
| 534 | case BuiltinType::Float: return Target.getFloatFormat(); |
| 535 | case BuiltinType::Double: return Target.getDoubleFormat(); |
| 536 | case BuiltinType::LongDouble: return Target.getLongDoubleFormat(); |
| 537 | } |
| 538 | } |
| 539 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 540 | /// getDeclAlign - Return a conservative estimate of the alignment of the |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 541 | /// specified decl. Note that bitfields do not have a valid alignment, so |
| 542 | /// this method will assert on them. |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 543 | /// If @p RefAsPointee, references are treated like their underlying type |
| 544 | /// (for alignof), else they're treated like pointers (for CodeGen). |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 545 | CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) { |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 546 | unsigned Align = Target.getCharWidth(); |
| 547 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 548 | if (const AlignedAttr* AA = D->getAttr<AlignedAttr>()) |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 549 | Align = std::max(Align, AA->getMaxAlignment()); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 550 | |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 551 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
| 552 | QualType T = VD->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 553 | if (const ReferenceType* RT = T->getAs<ReferenceType>()) { |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 554 | if (RefAsPointee) |
| 555 | T = RT->getPointeeType(); |
| 556 | else |
| 557 | T = getPointerType(RT->getPointeeType()); |
| 558 | } |
| 559 | if (!T->isIncompleteType() && !T->isFunctionType()) { |
Anders Carlsson | 4cc2cfd | 2009-04-10 04:47:03 +0000 | [diff] [blame] | 560 | // Incomplete or function types default to 1. |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 561 | while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T)) |
| 562 | T = cast<ArrayType>(T)->getElementType(); |
| 563 | |
| 564 | Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); |
| 565 | } |
Charles Davis | 05f6247 | 2010-02-23 04:52:00 +0000 | [diff] [blame] | 566 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) { |
| 567 | // In the case of a field in a packed struct, we want the minimum |
| 568 | // of the alignment of the field and the alignment of the struct. |
| 569 | Align = std::min(Align, |
| 570 | getPreferredTypeAlign(FD->getParent()->getTypeForDecl())); |
| 571 | } |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 572 | } |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 573 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 574 | return CharUnits::fromQuantity(Align / Target.getCharWidth()); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 575 | } |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 576 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 577 | /// getTypeSize - Return the size of the specified type, in bits. This method |
| 578 | /// does not work on incomplete types. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 579 | /// |
| 580 | /// FIXME: Pointers into different addr spaces could have different sizes and |
| 581 | /// alignment requirements: getPointerInfo should take an AddrSpace, this |
| 582 | /// should take a QualType, &c. |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 583 | std::pair<uint64_t, unsigned> |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 584 | ASTContext::getTypeInfo(const Type *T) { |
Mike Stump | 5e30100 | 2009-02-27 18:32:39 +0000 | [diff] [blame] | 585 | uint64_t Width=0; |
| 586 | unsigned Align=8; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 587 | switch (T->getTypeClass()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 588 | #define TYPE(Class, Base) |
| 589 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 590 | #define NON_CANONICAL_TYPE(Class, Base) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 591 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 592 | #include "clang/AST/TypeNodes.def" |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 593 | assert(false && "Should not see dependent types"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 594 | break; |
| 595 | |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 596 | case Type::FunctionNoProto: |
| 597 | case Type::FunctionProto: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 598 | // GCC extension: alignof(function) = 32 bits |
| 599 | Width = 0; |
| 600 | Align = 32; |
| 601 | break; |
| 602 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 603 | case Type::IncompleteArray: |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 604 | case Type::VariableArray: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 605 | Width = 0; |
| 606 | Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); |
| 607 | break; |
| 608 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 609 | case Type::ConstantArray: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 610 | const ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 611 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 612 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 613 | Width = EltInfo.first*CAT->getSize().getZExtValue(); |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 614 | Align = EltInfo.second; |
| 615 | break; |
Christopher Lamb | 5c09a02 | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 616 | } |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 617 | case Type::ExtVector: |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 618 | case Type::Vector: { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 619 | const VectorType *VT = cast<VectorType>(T); |
| 620 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType()); |
| 621 | Width = EltInfo.first*VT->getNumElements(); |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 622 | Align = Width; |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 623 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 624 | // This happens for non-power-of-2 length vectors. |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 625 | if (VT->getNumElements() & (VT->getNumElements()-1)) { |
| 626 | Align = llvm::NextPowerOf2(Align); |
| 627 | Width = llvm::RoundUpToAlignment(Width, Align); |
| 628 | } |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 629 | break; |
| 630 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 631 | |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 632 | case Type::Builtin: |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 633 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 634 | default: assert(0 && "Unknown builtin type!"); |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 635 | case BuiltinType::Void: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 636 | // GCC extension: alignof(void) = 8 bits. |
| 637 | Width = 0; |
| 638 | Align = 8; |
| 639 | break; |
| 640 | |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 641 | case BuiltinType::Bool: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 642 | Width = Target.getBoolWidth(); |
| 643 | Align = Target.getBoolAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 644 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 645 | case BuiltinType::Char_S: |
| 646 | case BuiltinType::Char_U: |
| 647 | case BuiltinType::UChar: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 648 | case BuiltinType::SChar: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 649 | Width = Target.getCharWidth(); |
| 650 | Align = Target.getCharAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 651 | break; |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 652 | case BuiltinType::WChar: |
| 653 | Width = Target.getWCharWidth(); |
| 654 | Align = Target.getWCharAlign(); |
| 655 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 656 | case BuiltinType::Char16: |
| 657 | Width = Target.getChar16Width(); |
| 658 | Align = Target.getChar16Align(); |
| 659 | break; |
| 660 | case BuiltinType::Char32: |
| 661 | Width = Target.getChar32Width(); |
| 662 | Align = Target.getChar32Align(); |
| 663 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 664 | case BuiltinType::UShort: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 665 | case BuiltinType::Short: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 666 | Width = Target.getShortWidth(); |
| 667 | Align = Target.getShortAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 668 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 669 | case BuiltinType::UInt: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 670 | case BuiltinType::Int: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 671 | Width = Target.getIntWidth(); |
| 672 | Align = Target.getIntAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 673 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 674 | case BuiltinType::ULong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 675 | case BuiltinType::Long: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 676 | Width = Target.getLongWidth(); |
| 677 | Align = Target.getLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 678 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 679 | case BuiltinType::ULongLong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 680 | case BuiltinType::LongLong: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 681 | Width = Target.getLongLongWidth(); |
| 682 | Align = Target.getLongLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 683 | break; |
Chris Lattner | ec16cb9 | 2009-04-30 02:55:13 +0000 | [diff] [blame] | 684 | case BuiltinType::Int128: |
| 685 | case BuiltinType::UInt128: |
| 686 | Width = 128; |
| 687 | Align = 128; // int128_t is 128-bit aligned on all targets. |
| 688 | break; |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 689 | case BuiltinType::Float: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 690 | Width = Target.getFloatWidth(); |
| 691 | Align = Target.getFloatAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 692 | break; |
| 693 | case BuiltinType::Double: |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 694 | Width = Target.getDoubleWidth(); |
| 695 | Align = Target.getDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 696 | break; |
| 697 | case BuiltinType::LongDouble: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 698 | Width = Target.getLongDoubleWidth(); |
| 699 | Align = Target.getLongDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 700 | break; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 701 | case BuiltinType::NullPtr: |
| 702 | Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) |
| 703 | Align = Target.getPointerAlign(0); // == sizeof(void*) |
Sebastian Redl | 1590d9c | 2009-05-27 19:34:06 +0000 | [diff] [blame] | 704 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 705 | } |
Chris Lattner | bfef6d7 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 706 | break; |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 707 | case Type::ObjCObjectPointer: |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 708 | Width = Target.getPointerWidth(0); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 709 | Align = Target.getPointerAlign(0); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 710 | break; |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 711 | case Type::BlockPointer: { |
| 712 | unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace(); |
| 713 | Width = Target.getPointerWidth(AS); |
| 714 | Align = Target.getPointerAlign(AS); |
| 715 | break; |
| 716 | } |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 717 | case Type::LValueReference: |
| 718 | case Type::RValueReference: { |
| 719 | // alignof and sizeof should never enter this code path here, so we go |
| 720 | // the pointer route. |
| 721 | unsigned AS = cast<ReferenceType>(T)->getPointeeType().getAddressSpace(); |
| 722 | Width = Target.getPointerWidth(AS); |
| 723 | Align = Target.getPointerAlign(AS); |
| 724 | break; |
| 725 | } |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 726 | case Type::Pointer: { |
| 727 | unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace(); |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 728 | Width = Target.getPointerWidth(AS); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 729 | Align = Target.getPointerAlign(AS); |
| 730 | break; |
| 731 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 732 | case Type::MemberPointer: { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 733 | QualType Pointee = cast<MemberPointerType>(T)->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 734 | std::pair<uint64_t, unsigned> PtrDiffInfo = |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 735 | getTypeInfo(getPointerDiffType()); |
| 736 | Width = PtrDiffInfo.first; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 737 | if (Pointee->isFunctionType()) |
| 738 | Width *= 2; |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 739 | Align = PtrDiffInfo.second; |
| 740 | break; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 741 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 742 | case Type::Complex: { |
| 743 | // Complex types have the same alignment as their elements, but twice the |
| 744 | // size. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 745 | std::pair<uint64_t, unsigned> EltInfo = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 746 | getTypeInfo(cast<ComplexType>(T)->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 747 | Width = EltInfo.first*2; |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 748 | Align = EltInfo.second; |
| 749 | break; |
| 750 | } |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 751 | case Type::ObjCInterface: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 752 | const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 753 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
| 754 | Width = Layout.getSize(); |
| 755 | Align = Layout.getAlignment(); |
| 756 | break; |
| 757 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 758 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 759 | case Type::Enum: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 760 | const TagType *TT = cast<TagType>(T); |
| 761 | |
| 762 | if (TT->getDecl()->isInvalidDecl()) { |
Chris Lattner | 8389eab | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 763 | Width = 1; |
| 764 | Align = 1; |
| 765 | break; |
| 766 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 767 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 768 | if (const EnumType *ET = dyn_cast<EnumType>(TT)) |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 769 | return getTypeInfo(ET->getDecl()->getIntegerType()); |
| 770 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 771 | const RecordType *RT = cast<RecordType>(TT); |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 772 | const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); |
| 773 | Width = Layout.getSize(); |
| 774 | Align = Layout.getAlignment(); |
Chris Lattner | dc0d73e | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 775 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 776 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 777 | |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 778 | case Type::SubstTemplateTypeParm: |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 779 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> |
| 780 | getReplacementType().getTypePtr()); |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 781 | |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 782 | case Type::Elaborated: |
| 783 | return getTypeInfo(cast<ElaboratedType>(T)->getUnderlyingType() |
| 784 | .getTypePtr()); |
John McCall | 7da2431 | 2009-09-05 00:15:47 +0000 | [diff] [blame] | 785 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 786 | case Type::Typedef: { |
| 787 | const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl(); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 788 | if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 789 | Align = std::max(Aligned->getMaxAlignment(), |
| 790 | getTypeAlign(Typedef->getUnderlyingType().getTypePtr())); |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 791 | Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr()); |
| 792 | } else |
| 793 | return getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 794 | break; |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 795 | } |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 796 | |
| 797 | case Type::TypeOfExpr: |
| 798 | return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType() |
| 799 | .getTypePtr()); |
| 800 | |
| 801 | case Type::TypeOf: |
| 802 | return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr()); |
| 803 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 804 | case Type::Decltype: |
| 805 | return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType() |
| 806 | .getTypePtr()); |
| 807 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 808 | case Type::QualifiedName: |
| 809 | return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 810 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 811 | case Type::TemplateSpecialization: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 812 | assert(getCanonicalType(T) != T && |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 813 | "Cannot request the size of a dependent type"); |
| 814 | // FIXME: this is likely to be wrong once we support template |
| 815 | // aliases, since a template alias could refer to a typedef that |
| 816 | // has an __aligned__ attribute on it. |
| 817 | return getTypeInfo(getCanonicalType(T)); |
| 818 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 819 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 820 | assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2"); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 821 | return std::make_pair(Width, Align); |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 824 | /// getTypeSizeInChars - Return the size of the specified type, in characters. |
| 825 | /// This method does not work on incomplete types. |
| 826 | CharUnits ASTContext::getTypeSizeInChars(QualType T) { |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 827 | return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth()); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 828 | } |
| 829 | CharUnits ASTContext::getTypeSizeInChars(const Type *T) { |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 830 | return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth()); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 831 | } |
| 832 | |
Ken Dyck | 16e20cc | 2010-01-26 17:25:18 +0000 | [diff] [blame] | 833 | /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 834 | /// characters. This method does not work on incomplete types. |
| 835 | CharUnits ASTContext::getTypeAlignInChars(QualType T) { |
| 836 | return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth()); |
| 837 | } |
| 838 | CharUnits ASTContext::getTypeAlignInChars(const Type *T) { |
| 839 | return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth()); |
| 840 | } |
| 841 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 842 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 843 | /// type for the current target in bits. This can be different than the ABI |
| 844 | /// alignment in cases where it is beneficial for performance to overalign |
| 845 | /// a data type. |
| 846 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) { |
| 847 | unsigned ABIAlign = getTypeAlign(T); |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 848 | |
| 849 | // Double and long long should be naturally aligned if possible. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 850 | if (const ComplexType* CT = T->getAs<ComplexType>()) |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 851 | T = CT->getElementType().getTypePtr(); |
| 852 | if (T->isSpecificBuiltinType(BuiltinType::Double) || |
| 853 | T->isSpecificBuiltinType(BuiltinType::LongLong)) |
| 854 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); |
| 855 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 856 | return ABIAlign; |
| 857 | } |
| 858 | |
Daniel Dunbar | a80a0f6 | 2009-04-22 17:43:55 +0000 | [diff] [blame] | 859 | static void CollectLocalObjCIvars(ASTContext *Ctx, |
| 860 | const ObjCInterfaceDecl *OI, |
| 861 | llvm::SmallVectorImpl<FieldDecl*> &Fields) { |
Fariborz Jahanian | a769c00 | 2008-12-17 21:40:49 +0000 | [diff] [blame] | 862 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 863 | E = OI->ivar_end(); I != E; ++I) { |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 864 | ObjCIvarDecl *IVDecl = *I; |
Fariborz Jahanian | a769c00 | 2008-12-17 21:40:49 +0000 | [diff] [blame] | 865 | if (!IVDecl->isInvalidDecl()) |
| 866 | Fields.push_back(cast<FieldDecl>(IVDecl)); |
| 867 | } |
| 868 | } |
| 869 | |
Daniel Dunbar | a80a0f6 | 2009-04-22 17:43:55 +0000 | [diff] [blame] | 870 | void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 871 | llvm::SmallVectorImpl<FieldDecl*> &Fields) { |
| 872 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
| 873 | CollectObjCIvars(SuperClass, Fields); |
| 874 | CollectLocalObjCIvars(this, OI, Fields); |
| 875 | } |
| 876 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 877 | /// ShallowCollectObjCIvars - |
| 878 | /// Collect all ivars, including those synthesized, in the current class. |
| 879 | /// |
| 880 | void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI, |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 881 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) { |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 882 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 883 | E = OI->ivar_end(); I != E; ++I) { |
| 884 | Ivars.push_back(*I); |
| 885 | } |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 886 | |
| 887 | CollectNonClassIvars(OI, Ivars); |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 888 | } |
| 889 | |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 890 | void ASTContext::CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD, |
| 891 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 892 | for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(), |
| 893 | E = PD->prop_end(); I != E; ++I) |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 894 | if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl()) |
| 895 | Ivars.push_back(Ivar); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 896 | |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 897 | // Also look into nested protocols. |
| 898 | for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(), |
| 899 | E = PD->protocol_end(); P != E; ++P) |
| 900 | CollectProtocolSynthesizedIvars(*P, Ivars); |
| 901 | } |
| 902 | |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 903 | /// CollectNonClassIvars - |
| 904 | /// This routine collects all other ivars which are not declared in the class. |
| 905 | /// This includes synthesized ivars and those in class's implementation. |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 906 | /// |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 907 | void ASTContext::CollectNonClassIvars(const ObjCInterfaceDecl *OI, |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 908 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) { |
Fariborz Jahanian | 0e5ad25 | 2010-02-23 01:26:30 +0000 | [diff] [blame] | 909 | // Find ivars declared in class extension. |
| 910 | if (const ObjCCategoryDecl *CDecl = OI->getClassExtension()) { |
| 911 | for (ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(), |
| 912 | E = CDecl->ivar_end(); I != E; ++I) { |
| 913 | Ivars.push_back(*I); |
| 914 | } |
| 915 | } |
| 916 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 917 | for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(), |
| 918 | E = OI->prop_end(); I != E; ++I) { |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 919 | if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl()) |
| 920 | Ivars.push_back(Ivar); |
| 921 | } |
| 922 | // Also look into interface's protocol list for properties declared |
| 923 | // in the protocol and whose ivars are synthesized. |
| 924 | for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(), |
| 925 | PE = OI->protocol_end(); P != PE; ++P) { |
| 926 | ObjCProtocolDecl *PD = (*P); |
| 927 | CollectProtocolSynthesizedIvars(PD, Ivars); |
| 928 | } |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 929 | |
| 930 | // Also add any ivar defined in this class's implementation |
| 931 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) { |
| 932 | for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(), |
| 933 | E = ImplDecl->ivar_end(); I != E; ++I) |
| 934 | Ivars.push_back(*I); |
| 935 | } |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 936 | } |
| 937 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 938 | /// CollectInheritedProtocols - Collect all protocols in current class and |
| 939 | /// those inherited by it. |
| 940 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 941 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 942 | if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
| 943 | for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(), |
| 944 | PE = OI->protocol_end(); P != PE; ++P) { |
| 945 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 946 | Protocols.insert(Proto); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 947 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 948 | PE = Proto->protocol_end(); P != PE; ++P) |
| 949 | CollectInheritedProtocols(*P, Protocols); |
| 950 | } |
| 951 | |
| 952 | // Categories of this Interface. |
| 953 | for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList(); |
| 954 | CDeclChain; CDeclChain = CDeclChain->getNextClassCategory()) |
| 955 | CollectInheritedProtocols(CDeclChain, Protocols); |
| 956 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) |
| 957 | while (SD) { |
| 958 | CollectInheritedProtocols(SD, Protocols); |
| 959 | SD = SD->getSuperClass(); |
| 960 | } |
| 961 | return; |
| 962 | } |
| 963 | if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
| 964 | for (ObjCInterfaceDecl::protocol_iterator P = OC->protocol_begin(), |
| 965 | PE = OC->protocol_end(); P != PE; ++P) { |
| 966 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 967 | Protocols.insert(Proto); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 968 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 969 | PE = Proto->protocol_end(); P != PE; ++P) |
| 970 | CollectInheritedProtocols(*P, Protocols); |
| 971 | } |
| 972 | return; |
| 973 | } |
| 974 | if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
| 975 | for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(), |
| 976 | PE = OP->protocol_end(); P != PE; ++P) { |
| 977 | ObjCProtocolDecl *Proto = (*P); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 978 | Protocols.insert(Proto); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 979 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 980 | PE = Proto->protocol_end(); P != PE; ++P) |
| 981 | CollectInheritedProtocols(*P, Protocols); |
| 982 | } |
| 983 | return; |
| 984 | } |
| 985 | } |
| 986 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 987 | unsigned ASTContext::CountProtocolSynthesizedIvars(const ObjCProtocolDecl *PD) { |
| 988 | unsigned count = 0; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 989 | for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(), |
| 990 | E = PD->prop_end(); I != E; ++I) |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 991 | if ((*I)->getPropertyIvarDecl()) |
| 992 | ++count; |
| 993 | |
| 994 | // Also look into nested protocols. |
| 995 | for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(), |
| 996 | E = PD->protocol_end(); P != E; ++P) |
| 997 | count += CountProtocolSynthesizedIvars(*P); |
| 998 | return count; |
| 999 | } |
| 1000 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1001 | unsigned ASTContext::CountSynthesizedIvars(const ObjCInterfaceDecl *OI) { |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 1002 | unsigned count = 0; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1003 | for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(), |
| 1004 | E = OI->prop_end(); I != E; ++I) { |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 1005 | if ((*I)->getPropertyIvarDecl()) |
| 1006 | ++count; |
| 1007 | } |
| 1008 | // Also look into interface's protocol list for properties declared |
| 1009 | // in the protocol and whose ivars are synthesized. |
| 1010 | for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(), |
| 1011 | PE = OI->protocol_end(); P != PE; ++P) { |
| 1012 | ObjCProtocolDecl *PD = (*P); |
| 1013 | count += CountProtocolSynthesizedIvars(PD); |
| 1014 | } |
| 1015 | return count; |
| 1016 | } |
| 1017 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1018 | /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. |
| 1019 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
| 1020 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1021 | I = ObjCImpls.find(D); |
| 1022 | if (I != ObjCImpls.end()) |
| 1023 | return cast<ObjCImplementationDecl>(I->second); |
| 1024 | return 0; |
| 1025 | } |
| 1026 | /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. |
| 1027 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
| 1028 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1029 | I = ObjCImpls.find(D); |
| 1030 | if (I != ObjCImpls.end()) |
| 1031 | return cast<ObjCCategoryImplDecl>(I->second); |
| 1032 | return 0; |
| 1033 | } |
| 1034 | |
| 1035 | /// \brief Set the implementation of ObjCInterfaceDecl. |
| 1036 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
| 1037 | ObjCImplementationDecl *ImplD) { |
| 1038 | assert(IFaceD && ImplD && "Passed null params"); |
| 1039 | ObjCImpls[IFaceD] = ImplD; |
| 1040 | } |
| 1041 | /// \brief Set the implementation of ObjCCategoryDecl. |
| 1042 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
| 1043 | ObjCCategoryImplDecl *ImplD) { |
| 1044 | assert(CatD && ImplD && "Passed null params"); |
| 1045 | ObjCImpls[CatD] = ImplD; |
| 1046 | } |
| 1047 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1048 | /// \brief Allocate an uninitialized TypeSourceInfo. |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1049 | /// |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1050 | /// The caller should initialize the memory held by TypeSourceInfo using |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1051 | /// the TypeLoc wrappers. |
| 1052 | /// |
| 1053 | /// \param T the type that will be the basis for type source info. This type |
| 1054 | /// should refer to how the declarator was written in source code, not to |
| 1055 | /// what type semantic analysis resolved the declarator to. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1056 | TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T, |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1057 | unsigned DataSize) { |
| 1058 | if (!DataSize) |
| 1059 | DataSize = TypeLoc::getFullDataSizeForType(T); |
| 1060 | else |
| 1061 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1062 | "incorrect data size provided to CreateTypeSourceInfo!"); |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1063 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1064 | TypeSourceInfo *TInfo = |
| 1065 | (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8); |
| 1066 | new (TInfo) TypeSourceInfo(T); |
| 1067 | return TInfo; |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1070 | TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, |
John McCall | a4eb74d | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 1071 | SourceLocation L) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1072 | TypeSourceInfo *DI = CreateTypeSourceInfo(T); |
John McCall | a4eb74d | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 1073 | DI->getTypeLoc().initialize(L); |
| 1074 | return DI; |
| 1075 | } |
| 1076 | |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1077 | /// getInterfaceLayoutImpl - Get or compute information about the |
| 1078 | /// layout of the given interface. |
| 1079 | /// |
| 1080 | /// \param Impl - If given, also include the layout of the interface's |
| 1081 | /// implementation. This may differ by including synthesized ivars. |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1082 | const ASTRecordLayout & |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1083 | ASTContext::getObjCLayout(const ObjCInterfaceDecl *D, |
| 1084 | const ObjCImplementationDecl *Impl) { |
Daniel Dunbar | 532d4da | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 1085 | assert(!D->isForwardDecl() && "Invalid interface decl!"); |
| 1086 | |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1087 | // Look up this layout, if already laid out, return what we have. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1088 | ObjCContainerDecl *Key = |
Daniel Dunbar | d8fd6ff | 2009-05-03 11:41:43 +0000 | [diff] [blame] | 1089 | Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D; |
| 1090 | if (const ASTRecordLayout *Entry = ObjCLayouts[Key]) |
| 1091 | return *Entry; |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1092 | |
Daniel Dunbar | 453addb | 2009-05-03 11:16:44 +0000 | [diff] [blame] | 1093 | // Add in synthesized ivar count if laying out an implementation. |
| 1094 | if (Impl) { |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 1095 | unsigned SynthCount = CountSynthesizedIvars(D); |
Daniel Dunbar | d8fd6ff | 2009-05-03 11:41:43 +0000 | [diff] [blame] | 1096 | // If there aren't any sythesized ivars then reuse the interface |
Daniel Dunbar | 453addb | 2009-05-03 11:16:44 +0000 | [diff] [blame] | 1097 | // entry. Note we can't cache this because we simply free all |
| 1098 | // entries later; however we shouldn't look up implementations |
| 1099 | // frequently. |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 1100 | if (SynthCount == 0) |
Daniel Dunbar | 453addb | 2009-05-03 11:16:44 +0000 | [diff] [blame] | 1101 | return getObjCLayout(D, 0); |
| 1102 | } |
| 1103 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1104 | const ASTRecordLayout *NewEntry = |
Anders Carlsson | 29445a0 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 1105 | ASTRecordLayoutBuilder::ComputeLayout(*this, D, Impl); |
| 1106 | ObjCLayouts[Key] = NewEntry; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1107 | |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1108 | return *NewEntry; |
| 1109 | } |
| 1110 | |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1111 | const ASTRecordLayout & |
| 1112 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) { |
| 1113 | return getObjCLayout(D, 0); |
| 1114 | } |
| 1115 | |
| 1116 | const ASTRecordLayout & |
| 1117 | ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) { |
| 1118 | return getObjCLayout(D->getClassInterface(), D); |
| 1119 | } |
| 1120 | |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 1121 | /// getASTRecordLayout - Get or compute information about the layout of the |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 1122 | /// specified record (struct/union/class), which indicates its size and field |
| 1123 | /// position information. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1124 | const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1125 | D = D->getDefinition(); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1126 | assert(D && "Cannot get layout of forward declarations!"); |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 1127 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 1128 | // Look up this layout, if already laid out, return what we have. |
Eli Friedman | ab22c43 | 2009-07-22 20:29:16 +0000 | [diff] [blame] | 1129 | // Note that we can't save a reference to the entry because this function |
| 1130 | // is recursive. |
| 1131 | const ASTRecordLayout *Entry = ASTRecordLayouts[D]; |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 1132 | if (Entry) return *Entry; |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 1133 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1134 | const ASTRecordLayout *NewEntry = |
Anders Carlsson | 29445a0 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 1135 | ASTRecordLayoutBuilder::ComputeLayout(*this, D); |
Eli Friedman | ab22c43 | 2009-07-22 20:29:16 +0000 | [diff] [blame] | 1136 | ASTRecordLayouts[D] = NewEntry; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1137 | |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1138 | return *NewEntry; |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
Anders Carlsson | f53df23 | 2009-12-07 04:35:11 +0000 | [diff] [blame] | 1141 | const CXXMethodDecl *ASTContext::getKeyFunction(const CXXRecordDecl *RD) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1142 | RD = cast<CXXRecordDecl>(RD->getDefinition()); |
Anders Carlsson | f53df23 | 2009-12-07 04:35:11 +0000 | [diff] [blame] | 1143 | assert(RD && "Cannot get key function for forward declarations!"); |
| 1144 | |
| 1145 | const CXXMethodDecl *&Entry = KeyFunctions[RD]; |
| 1146 | if (!Entry) |
| 1147 | Entry = ASTRecordLayoutBuilder::ComputeKeyFunction(RD); |
| 1148 | else |
| 1149 | assert(Entry == ASTRecordLayoutBuilder::ComputeKeyFunction(RD) && |
| 1150 | "Key function changed!"); |
| 1151 | |
| 1152 | return Entry; |
| 1153 | } |
| 1154 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1155 | //===----------------------------------------------------------------------===// |
| 1156 | // Type creation/memoization methods |
| 1157 | //===----------------------------------------------------------------------===// |
| 1158 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1159 | QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) { |
| 1160 | unsigned Fast = Quals.getFastQualifiers(); |
| 1161 | Quals.removeFastQualifiers(); |
| 1162 | |
| 1163 | // Check if we've already instantiated this type. |
| 1164 | llvm::FoldingSetNodeID ID; |
| 1165 | ExtQuals::Profile(ID, TypeNode, Quals); |
| 1166 | void *InsertPos = 0; |
| 1167 | if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) { |
| 1168 | assert(EQ->getQualifiers() == Quals); |
| 1169 | QualType T = QualType(EQ, Fast); |
| 1170 | return T; |
| 1171 | } |
| 1172 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1173 | ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1174 | ExtQualNodes.InsertNode(New, InsertPos); |
| 1175 | QualType T = QualType(New, Fast); |
| 1176 | return T; |
| 1177 | } |
| 1178 | |
| 1179 | QualType ASTContext::getVolatileType(QualType T) { |
| 1180 | QualType CanT = getCanonicalType(T); |
| 1181 | if (CanT.isVolatileQualified()) return T; |
| 1182 | |
| 1183 | QualifierCollector Quals; |
| 1184 | const Type *TypeNode = Quals.strip(T); |
| 1185 | Quals.addVolatile(); |
| 1186 | |
| 1187 | return getExtQualType(TypeNode, Quals); |
| 1188 | } |
| 1189 | |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 1190 | QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1191 | QualType CanT = getCanonicalType(T); |
| 1192 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1193 | return T; |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1194 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1195 | // If we are composing extended qualifiers together, merge together |
| 1196 | // into one ExtQuals node. |
| 1197 | QualifierCollector Quals; |
| 1198 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1199 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1200 | // If this type already has an address space specified, it cannot get |
| 1201 | // another one. |
| 1202 | assert(!Quals.hasAddressSpace() && |
| 1203 | "Type cannot be in multiple addr spaces!"); |
| 1204 | Quals.addAddressSpace(AddressSpace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1205 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1206 | return getExtQualType(TypeNode, Quals); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1209 | QualType ASTContext::getObjCGCQualType(QualType T, |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1210 | Qualifiers::GC GCAttr) { |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1211 | QualType CanT = getCanonicalType(T); |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1212 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1213 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1214 | |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1215 | if (T->isPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1216 | QualType Pointee = T->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 1217 | if (Pointee->isAnyPointerType()) { |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1218 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 1219 | return getPointerType(ResultType); |
| 1220 | } |
| 1221 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1222 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1223 | // If we are composing extended qualifiers together, merge together |
| 1224 | // into one ExtQuals node. |
| 1225 | QualifierCollector Quals; |
| 1226 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1227 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1228 | // If this type already has an ObjCGC specified, it cannot get |
| 1229 | // another one. |
| 1230 | assert(!Quals.hasObjCGCAttr() && |
| 1231 | "Type cannot have multiple ObjCGCs!"); |
| 1232 | Quals.addObjCGCAttr(GCAttr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1233 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1234 | return getExtQualType(TypeNode, Quals); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1235 | } |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1236 | |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1237 | static QualType getNoReturnCallConvType(ASTContext& Context, QualType T, |
| 1238 | bool AddNoReturn, |
| 1239 | CallingConv CallConv) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1240 | QualType ResultType; |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1241 | if (const PointerType *Pointer = T->getAs<PointerType>()) { |
| 1242 | QualType Pointee = Pointer->getPointeeType(); |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1243 | ResultType = getNoReturnCallConvType(Context, Pointee, AddNoReturn, |
| 1244 | CallConv); |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1245 | if (ResultType == Pointee) |
| 1246 | return T; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1247 | |
| 1248 | ResultType = Context.getPointerType(ResultType); |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1249 | } else if (const BlockPointerType *BlockPointer |
| 1250 | = T->getAs<BlockPointerType>()) { |
| 1251 | QualType Pointee = BlockPointer->getPointeeType(); |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1252 | ResultType = getNoReturnCallConvType(Context, Pointee, AddNoReturn, |
| 1253 | CallConv); |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1254 | if (ResultType == Pointee) |
| 1255 | return T; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1256 | |
| 1257 | ResultType = Context.getBlockPointerType(ResultType); |
| 1258 | } else if (const FunctionType *F = T->getAs<FunctionType>()) { |
| 1259 | if (F->getNoReturnAttr() == AddNoReturn && F->getCallConv() == CallConv) |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1260 | return T; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1261 | |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1262 | if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(F)) { |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1263 | ResultType = Context.getFunctionNoProtoType(FNPT->getResultType(), |
| 1264 | AddNoReturn, CallConv); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1265 | } else { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1266 | const FunctionProtoType *FPT = cast<FunctionProtoType>(F); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1267 | ResultType |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1268 | = Context.getFunctionType(FPT->getResultType(), FPT->arg_type_begin(), |
| 1269 | FPT->getNumArgs(), FPT->isVariadic(), |
| 1270 | FPT->getTypeQuals(), |
| 1271 | FPT->hasExceptionSpec(), |
| 1272 | FPT->hasAnyExceptionSpec(), |
| 1273 | FPT->getNumExceptions(), |
| 1274 | FPT->exception_begin(), |
| 1275 | AddNoReturn, CallConv); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1276 | } |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1277 | } else |
| 1278 | return T; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1279 | |
| 1280 | return Context.getQualifiedType(ResultType, T.getLocalQualifiers()); |
| 1281 | } |
| 1282 | |
| 1283 | QualType ASTContext::getNoReturnType(QualType T, bool AddNoReturn) { |
| 1284 | return getNoReturnCallConvType(*this, T, AddNoReturn, T.getCallConv()); |
| 1285 | } |
| 1286 | |
| 1287 | QualType ASTContext::getCallConvType(QualType T, CallingConv CallConv) { |
| 1288 | return getNoReturnCallConvType(*this, T, T.getNoReturnAttr(), CallConv); |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1291 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 1292 | /// number with the specified element type. |
| 1293 | QualType ASTContext::getComplexType(QualType T) { |
| 1294 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1295 | // structure. |
| 1296 | llvm::FoldingSetNodeID ID; |
| 1297 | ComplexType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1298 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1299 | void *InsertPos = 0; |
| 1300 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1301 | return QualType(CT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1302 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1303 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1304 | // so fill in the canonical type field. |
| 1305 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1306 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1307 | Canonical = getComplexType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1308 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1309 | // Get the new insert position for the node we care about. |
| 1310 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1311 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1312 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1313 | ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1314 | Types.push_back(New); |
| 1315 | ComplexTypes.InsertNode(New, InsertPos); |
| 1316 | return QualType(New, 0); |
| 1317 | } |
| 1318 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1319 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 1320 | /// the specified type. |
| 1321 | QualType ASTContext::getPointerType(QualType T) { |
| 1322 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1323 | // structure. |
| 1324 | llvm::FoldingSetNodeID ID; |
| 1325 | PointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1326 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1327 | void *InsertPos = 0; |
| 1328 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1329 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1330 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1331 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1332 | // so fill in the canonical type field. |
| 1333 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1334 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1335 | Canonical = getPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1336 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1337 | // Get the new insert position for the node we care about. |
| 1338 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1339 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1340 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1341 | PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1342 | Types.push_back(New); |
| 1343 | PointerTypes.InsertNode(New, InsertPos); |
| 1344 | return QualType(New, 0); |
| 1345 | } |
| 1346 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1347 | /// getBlockPointerType - Return the uniqued reference to the type for |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1348 | /// a pointer to the specified block. |
| 1349 | QualType ASTContext::getBlockPointerType(QualType T) { |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1350 | assert(T->isFunctionType() && "block of function types only"); |
| 1351 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1352 | // structure. |
| 1353 | llvm::FoldingSetNodeID ID; |
| 1354 | BlockPointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1355 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1356 | void *InsertPos = 0; |
| 1357 | if (BlockPointerType *PT = |
| 1358 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1359 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1360 | |
| 1361 | // 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] | 1362 | // type either so fill in the canonical type field. |
| 1363 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1364 | if (!T.isCanonical()) { |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1365 | Canonical = getBlockPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1366 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1367 | // Get the new insert position for the node we care about. |
| 1368 | BlockPointerType *NewIP = |
| 1369 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1370 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1371 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1372 | BlockPointerType *New |
| 1373 | = new (*this, TypeAlignment) BlockPointerType(T, Canonical); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1374 | Types.push_back(New); |
| 1375 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 1376 | return QualType(New, 0); |
| 1377 | } |
| 1378 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1379 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 1380 | /// lvalue reference to the specified type. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1381 | QualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1382 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1383 | // structure. |
| 1384 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1385 | ReferenceType::Profile(ID, T, SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1386 | |
| 1387 | void *InsertPos = 0; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1388 | if (LValueReferenceType *RT = |
| 1389 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1390 | return QualType(RT, 0); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1391 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1392 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1393 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1394 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1395 | // either, so fill in the canonical type field. |
| 1396 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1397 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
| 1398 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1399 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1400 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1401 | // Get the new insert position for the node we care about. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1402 | LValueReferenceType *NewIP = |
| 1403 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1404 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1407 | LValueReferenceType *New |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1408 | = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, |
| 1409 | SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1410 | Types.push_back(New); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1411 | LValueReferenceTypes.InsertNode(New, InsertPos); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1412 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1413 | return QualType(New, 0); |
| 1414 | } |
| 1415 | |
| 1416 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 1417 | /// rvalue reference to the specified type. |
| 1418 | QualType ASTContext::getRValueReferenceType(QualType T) { |
| 1419 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1420 | // structure. |
| 1421 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1422 | ReferenceType::Profile(ID, T, false); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1423 | |
| 1424 | void *InsertPos = 0; |
| 1425 | if (RValueReferenceType *RT = |
| 1426 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1427 | return QualType(RT, 0); |
| 1428 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1429 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1430 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1431 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1432 | // either, so fill in the canonical type field. |
| 1433 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1434 | if (InnerRef || !T.isCanonical()) { |
| 1435 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1436 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1437 | |
| 1438 | // Get the new insert position for the node we care about. |
| 1439 | RValueReferenceType *NewIP = |
| 1440 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1441 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
| 1442 | } |
| 1443 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1444 | RValueReferenceType *New |
| 1445 | = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1446 | Types.push_back(New); |
| 1447 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1448 | return QualType(New, 0); |
| 1449 | } |
| 1450 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1451 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 1452 | /// member pointer to the specified type, in the specified class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1453 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1454 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1455 | // structure. |
| 1456 | llvm::FoldingSetNodeID ID; |
| 1457 | MemberPointerType::Profile(ID, T, Cls); |
| 1458 | |
| 1459 | void *InsertPos = 0; |
| 1460 | if (MemberPointerType *PT = |
| 1461 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1462 | return QualType(PT, 0); |
| 1463 | |
| 1464 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 1465 | // type either, so fill in the canonical type field. |
| 1466 | QualType Canonical; |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1467 | if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1468 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 1469 | |
| 1470 | // Get the new insert position for the node we care about. |
| 1471 | MemberPointerType *NewIP = |
| 1472 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1473 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
| 1474 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1475 | MemberPointerType *New |
| 1476 | = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1477 | Types.push_back(New); |
| 1478 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 1479 | return QualType(New, 0); |
| 1480 | } |
| 1481 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1482 | /// getConstantArrayType - Return the unique reference to the type for an |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1483 | /// array of the specified element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1484 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1485 | const llvm::APInt &ArySizeIn, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1486 | ArrayType::ArraySizeModifier ASM, |
| 1487 | unsigned EltTypeQuals) { |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1488 | assert((EltTy->isDependentType() || |
| 1489 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && |
Eli Friedman | 587cbdf | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1490 | "Constant array of VLAs is illegal!"); |
| 1491 | |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1492 | // Convert the array size into a canonical width matching the pointer size for |
| 1493 | // the target. |
| 1494 | llvm::APInt ArySize(ArySizeIn); |
| 1495 | ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1496 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1497 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 0be2ef2 | 2009-02-19 17:31:02 +0000 | [diff] [blame] | 1498 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1499 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1500 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1501 | if (ConstantArrayType *ATP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1502 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1503 | return QualType(ATP, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1504 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1505 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1506 | // so fill in the canonical type field. |
| 1507 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1508 | if (!EltTy.isCanonical()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1509 | Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1510 | ASM, EltTypeQuals); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1511 | // Get the new insert position for the node we care about. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1512 | ConstantArrayType *NewIP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1513 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1514 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1515 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1516 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1517 | ConstantArrayType *New = new(*this,TypeAlignment) |
| 1518 | ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1519 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1520 | Types.push_back(New); |
| 1521 | return QualType(New, 0); |
| 1522 | } |
| 1523 | |
Steve Naroff | bdbf7b0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 1524 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 1525 | /// variable array of the specified element type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1526 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 1527 | Expr *NumElts, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1528 | ArrayType::ArraySizeModifier ASM, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1529 | unsigned EltTypeQuals, |
| 1530 | SourceRange Brackets) { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1531 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 1532 | // that have an expression provided for their size. |
| 1533 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1534 | VariableArrayType *New = new(*this, TypeAlignment) |
| 1535 | VariableArrayType(EltTy, QualType(), NumElts, ASM, EltTypeQuals, Brackets); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1536 | |
| 1537 | VariableArrayTypes.push_back(New); |
| 1538 | Types.push_back(New); |
| 1539 | return QualType(New, 0); |
| 1540 | } |
| 1541 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1542 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 1543 | /// the type for a dependently-sized array of the specified element |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1544 | /// type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1545 | QualType ASTContext::getDependentSizedArrayType(QualType EltTy, |
| 1546 | Expr *NumElts, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1547 | ArrayType::ArraySizeModifier ASM, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1548 | unsigned EltTypeQuals, |
| 1549 | SourceRange Brackets) { |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1550 | assert((!NumElts || NumElts->isTypeDependent() || |
| 1551 | NumElts->isValueDependent()) && |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1552 | "Size must be type- or value-dependent!"); |
| 1553 | |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1554 | void *InsertPos = 0; |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1555 | DependentSizedArrayType *Canon = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1556 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1557 | |
| 1558 | if (NumElts) { |
| 1559 | // Dependently-sized array types that do not have a specified |
| 1560 | // number of elements will have their sizes deduced from an |
| 1561 | // initializer. |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1562 | DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM, |
| 1563 | EltTypeQuals, NumElts); |
| 1564 | |
| 1565 | Canon = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1566 | } |
| 1567 | |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1568 | DependentSizedArrayType *New; |
| 1569 | if (Canon) { |
| 1570 | // We already have a canonical version of this array type; use it as |
| 1571 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1572 | New = new (*this, TypeAlignment) |
| 1573 | DependentSizedArrayType(*this, EltTy, QualType(Canon, 0), |
| 1574 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1575 | } else { |
| 1576 | QualType CanonEltTy = getCanonicalType(EltTy); |
| 1577 | if (CanonEltTy == EltTy) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1578 | New = new (*this, TypeAlignment) |
| 1579 | DependentSizedArrayType(*this, EltTy, QualType(), |
| 1580 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1581 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1582 | if (NumElts) { |
| 1583 | DependentSizedArrayType *CanonCheck |
| 1584 | = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1585 | assert(!CanonCheck && "Dependent-sized canonical array type broken"); |
| 1586 | (void)CanonCheck; |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1587 | DependentSizedArrayTypes.InsertNode(New, InsertPos); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1588 | } |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1589 | } else { |
| 1590 | QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts, |
| 1591 | ASM, EltTypeQuals, |
| 1592 | SourceRange()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1593 | New = new (*this, TypeAlignment) |
| 1594 | DependentSizedArrayType(*this, EltTy, Canon, |
| 1595 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1596 | } |
| 1597 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1598 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1599 | Types.push_back(New); |
| 1600 | return QualType(New, 0); |
| 1601 | } |
| 1602 | |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1603 | QualType ASTContext::getIncompleteArrayType(QualType EltTy, |
| 1604 | ArrayType::ArraySizeModifier ASM, |
| 1605 | unsigned EltTypeQuals) { |
| 1606 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 0be2ef2 | 2009-02-19 17:31:02 +0000 | [diff] [blame] | 1607 | IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1608 | |
| 1609 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1610 | if (IncompleteArrayType *ATP = |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1611 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1612 | return QualType(ATP, 0); |
| 1613 | |
| 1614 | // If the element type isn't canonical, this won't be a canonical type |
| 1615 | // either, so fill in the canonical type field. |
| 1616 | QualType Canonical; |
| 1617 | |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1618 | if (!EltTy.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1619 | Canonical = getIncompleteArrayType(getCanonicalType(EltTy), |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1620 | ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1621 | |
| 1622 | // Get the new insert position for the node we care about. |
| 1623 | IncompleteArrayType *NewIP = |
| 1624 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1625 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1626 | } |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1627 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1628 | IncompleteArrayType *New = new (*this, TypeAlignment) |
| 1629 | IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1630 | |
| 1631 | IncompleteArrayTypes.InsertNode(New, InsertPos); |
| 1632 | Types.push_back(New); |
| 1633 | return QualType(New, 0); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1634 | } |
| 1635 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1636 | /// getVectorType - Return the unique reference to a vector type of |
| 1637 | /// 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] | 1638 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts, |
| 1639 | bool IsAltiVec, bool IsPixel) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1640 | BuiltinType *baseType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1641 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1642 | baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1643 | assert(baseType != 0 && "getVectorType(): Expecting a built-in type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1644 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1645 | // Check if we've already instantiated a vector of this type. |
| 1646 | llvm::FoldingSetNodeID ID; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1647 | VectorType::Profile(ID, vecType, NumElts, Type::Vector, |
| 1648 | IsAltiVec, IsPixel); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1649 | void *InsertPos = 0; |
| 1650 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1651 | return QualType(VTP, 0); |
| 1652 | |
| 1653 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1654 | // so fill in the canonical type field. |
| 1655 | QualType Canonical; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1656 | if (!vecType.isCanonical() || IsAltiVec || IsPixel) { |
| 1657 | Canonical = getVectorType(getCanonicalType(vecType), |
| 1658 | NumElts, false, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1659 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1660 | // Get the new insert position for the node we care about. |
| 1661 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1662 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1663 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1664 | VectorType *New = new (*this, TypeAlignment) |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1665 | VectorType(vecType, NumElts, Canonical, IsAltiVec, IsPixel); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1666 | VectorTypes.InsertNode(New, InsertPos); |
| 1667 | Types.push_back(New); |
| 1668 | return QualType(New, 0); |
| 1669 | } |
| 1670 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1671 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1672 | /// 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] | 1673 | QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) { |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1674 | BuiltinType *baseType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1675 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1676 | baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1677 | assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1678 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1679 | // Check if we've already instantiated a vector of this type. |
| 1680 | llvm::FoldingSetNodeID ID; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1681 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, false, false); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1682 | void *InsertPos = 0; |
| 1683 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1684 | return QualType(VTP, 0); |
| 1685 | |
| 1686 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1687 | // so fill in the canonical type field. |
| 1688 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1689 | if (!vecType.isCanonical()) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1690 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1691 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1692 | // Get the new insert position for the node we care about. |
| 1693 | VectorType *NewIP = VectorTypes.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; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1695 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1696 | ExtVectorType *New = new (*this, TypeAlignment) |
| 1697 | ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1698 | VectorTypes.InsertNode(New, InsertPos); |
| 1699 | Types.push_back(New); |
| 1700 | return QualType(New, 0); |
| 1701 | } |
| 1702 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1703 | QualType ASTContext::getDependentSizedExtVectorType(QualType vecType, |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1704 | Expr *SizeExpr, |
| 1705 | SourceLocation AttrLoc) { |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1706 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1707 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1708 | SizeExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1709 | |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1710 | void *InsertPos = 0; |
| 1711 | DependentSizedExtVectorType *Canon |
| 1712 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1713 | DependentSizedExtVectorType *New; |
| 1714 | if (Canon) { |
| 1715 | // We already have a canonical version of this array type; use it as |
| 1716 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1717 | New = new (*this, TypeAlignment) |
| 1718 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), |
| 1719 | SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1720 | } else { |
| 1721 | QualType CanonVecTy = getCanonicalType(vecType); |
| 1722 | if (CanonVecTy == vecType) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1723 | New = new (*this, TypeAlignment) |
| 1724 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, |
| 1725 | AttrLoc); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1726 | |
| 1727 | DependentSizedExtVectorType *CanonCheck |
| 1728 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1729 | assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken"); |
| 1730 | (void)CanonCheck; |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1731 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); |
| 1732 | } else { |
| 1733 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 1734 | SourceLocation()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1735 | New = new (*this, TypeAlignment) |
| 1736 | DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1737 | } |
| 1738 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1739 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1740 | Types.push_back(New); |
| 1741 | return QualType(New, 0); |
| 1742 | } |
| 1743 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1744 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1745 | /// |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1746 | QualType ASTContext::getFunctionNoProtoType(QualType ResultTy, bool NoReturn, |
| 1747 | CallingConv CallConv) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1748 | // Unique functions, to guarantee there is only one function of a particular |
| 1749 | // structure. |
| 1750 | llvm::FoldingSetNodeID ID; |
John McCall | f82b4e8 | 2010-02-04 05:44:44 +0000 | [diff] [blame] | 1751 | FunctionNoProtoType::Profile(ID, ResultTy, NoReturn, CallConv); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1752 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1753 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1754 | if (FunctionNoProtoType *FT = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1755 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1756 | return QualType(FT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1757 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1758 | QualType Canonical; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1759 | if (!ResultTy.isCanonical() || |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1760 | getCanonicalCallConv(CallConv) != CallConv) { |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1761 | Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy), NoReturn, |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1762 | getCanonicalCallConv(CallConv)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1763 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1764 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1765 | FunctionNoProtoType *NewIP = |
| 1766 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1767 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1768 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1769 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1770 | FunctionNoProtoType *New = new (*this, TypeAlignment) |
John McCall | f82b4e8 | 2010-02-04 05:44:44 +0000 | [diff] [blame] | 1771 | FunctionNoProtoType(ResultTy, Canonical, NoReturn, CallConv); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1772 | Types.push_back(New); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1773 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1774 | return QualType(New, 0); |
| 1775 | } |
| 1776 | |
| 1777 | /// getFunctionType - Return a normal function type with a typed argument |
| 1778 | /// list. isVariadic indicates whether the argument list includes '...'. |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 1779 | QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray, |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1780 | unsigned NumArgs, bool isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1781 | unsigned TypeQuals, bool hasExceptionSpec, |
| 1782 | bool hasAnyExceptionSpec, unsigned NumExs, |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1783 | const QualType *ExArray, bool NoReturn, |
| 1784 | CallingConv CallConv) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1785 | // Unique functions, to guarantee there is only one function of a particular |
| 1786 | // structure. |
| 1787 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1788 | FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1789 | TypeQuals, hasExceptionSpec, hasAnyExceptionSpec, |
John McCall | f82b4e8 | 2010-02-04 05:44:44 +0000 | [diff] [blame] | 1790 | NumExs, ExArray, NoReturn, CallConv); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1791 | |
| 1792 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1793 | if (FunctionProtoType *FTP = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1794 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1795 | return QualType(FTP, 0); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1796 | |
| 1797 | // Determine whether the type being created is already canonical or not. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1798 | bool isCanonical = !hasExceptionSpec && ResultTy.isCanonical(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1799 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1800 | if (!ArgArray[i].isCanonicalAsParam()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1801 | isCanonical = false; |
| 1802 | |
| 1803 | // If this type isn't canonical, get the canonical version of it. |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1804 | // The exception spec is not part of the canonical type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1805 | QualType Canonical; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1806 | if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1807 | llvm::SmallVector<QualType, 16> CanonicalArgs; |
| 1808 | CanonicalArgs.reserve(NumArgs); |
| 1809 | for (unsigned i = 0; i != NumArgs; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1810 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1811 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1812 | Canonical = getFunctionType(getCanonicalType(ResultTy), |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1813 | CanonicalArgs.data(), NumArgs, |
Douglas Gregor | 47259d9 | 2009-08-05 19:03:35 +0000 | [diff] [blame] | 1814 | isVariadic, TypeQuals, false, |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1815 | false, 0, 0, NoReturn, |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1816 | getCanonicalCallConv(CallConv)); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1817 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1818 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1819 | FunctionProtoType *NewIP = |
| 1820 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1821 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1822 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1823 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1824 | // FunctionProtoType objects are allocated with extra bytes after them |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1825 | // for two variable size arrays (for parameter and exception types) at the |
| 1826 | // end of them. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1827 | FunctionProtoType *FTP = |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1828 | (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) + |
| 1829 | NumArgs*sizeof(QualType) + |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1830 | NumExs*sizeof(QualType), TypeAlignment); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1831 | new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1832 | TypeQuals, hasExceptionSpec, hasAnyExceptionSpec, |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1833 | ExArray, NumExs, Canonical, NoReturn, CallConv); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1834 | Types.push_back(FTP); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1835 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1836 | return QualType(FTP, 0); |
| 1837 | } |
| 1838 | |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1839 | /// getTypeDeclType - Return the unique reference to the type for the |
| 1840 | /// specified type declaration. |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1841 | QualType ASTContext::getTypeDeclType(const TypeDecl *Decl, |
| 1842 | const TypeDecl* PrevDecl) { |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 1843 | assert(Decl && "Passed null for Decl param"); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1844 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1845 | |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1846 | if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1847 | return getTypedefType(Typedef); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1848 | else if (isa<TemplateTypeParmDecl>(Decl)) { |
| 1849 | assert(false && "Template type parameter types are always available."); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1850 | } else if (const ObjCInterfaceDecl *ObjCInterface |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 1851 | = dyn_cast<ObjCInterfaceDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1852 | return getObjCInterfaceType(ObjCInterface); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1853 | |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1854 | if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
Ted Kremenek | 566c2ba | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 1855 | if (PrevDecl) |
| 1856 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
Steve Naroff | f83820b | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1857 | else |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1858 | Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Record); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1859 | } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { |
Ted Kremenek | 566c2ba | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 1860 | if (PrevDecl) |
| 1861 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
Steve Naroff | f83820b | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1862 | else |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1863 | Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Enum); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1864 | } else if (const UnresolvedUsingTypenameDecl *Using = |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1865 | dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) { |
| 1866 | Decl->TypeForDecl = new (*this, TypeAlignment) UnresolvedUsingType(Using); |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 1867 | } else |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1868 | assert(false && "TypeDecl without a type?"); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1869 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1870 | if (!PrevDecl) Types.push_back(Decl->TypeForDecl); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1871 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1872 | } |
| 1873 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1874 | /// getTypedefType - Return the unique reference to the type for the |
| 1875 | /// specified typename decl. |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1876 | QualType ASTContext::getTypedefType(const TypedefDecl *Decl) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1877 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1878 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1879 | QualType Canonical = getCanonicalType(Decl->getUnderlyingType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1880 | Decl->TypeForDecl = new(*this, TypeAlignment) |
| 1881 | TypedefType(Type::Typedef, Decl, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1882 | Types.push_back(Decl->TypeForDecl); |
| 1883 | return QualType(Decl->TypeForDecl, 0); |
| 1884 | } |
| 1885 | |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1886 | /// \brief Retrieve a substitution-result type. |
| 1887 | QualType |
| 1888 | ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, |
| 1889 | QualType Replacement) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1890 | assert(Replacement.isCanonical() |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1891 | && "replacement types must always be canonical"); |
| 1892 | |
| 1893 | llvm::FoldingSetNodeID ID; |
| 1894 | SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); |
| 1895 | void *InsertPos = 0; |
| 1896 | SubstTemplateTypeParmType *SubstParm |
| 1897 | = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1898 | |
| 1899 | if (!SubstParm) { |
| 1900 | SubstParm = new (*this, TypeAlignment) |
| 1901 | SubstTemplateTypeParmType(Parm, Replacement); |
| 1902 | Types.push_back(SubstParm); |
| 1903 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 1904 | } |
| 1905 | |
| 1906 | return QualType(SubstParm, 0); |
| 1907 | } |
| 1908 | |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1909 | /// \brief Retrieve the template type parameter type for a template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1910 | /// parameter or parameter pack with the given depth, index, and (optionally) |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1911 | /// name. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1912 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1913 | bool ParameterPack, |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1914 | IdentifierInfo *Name) { |
| 1915 | llvm::FoldingSetNodeID ID; |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1916 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1917 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1918 | TemplateTypeParmType *TypeParm |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1919 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1920 | |
| 1921 | if (TypeParm) |
| 1922 | return QualType(TypeParm, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1923 | |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1924 | if (Name) { |
| 1925 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1926 | TypeParm = new (*this, TypeAlignment) |
| 1927 | TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 1928 | |
| 1929 | TemplateTypeParmType *TypeCheck |
| 1930 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1931 | assert(!TypeCheck && "Template type parameter canonical type broken"); |
| 1932 | (void)TypeCheck; |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1933 | } else |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1934 | TypeParm = new (*this, TypeAlignment) |
| 1935 | TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1936 | |
| 1937 | Types.push_back(TypeParm); |
| 1938 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 1939 | |
| 1940 | return QualType(TypeParm, 0); |
| 1941 | } |
| 1942 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1943 | QualType |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1944 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1945 | const TemplateArgumentListInfo &Args, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1946 | QualType Canon) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1947 | unsigned NumArgs = Args.size(); |
| 1948 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1949 | llvm::SmallVector<TemplateArgument, 4> ArgVec; |
| 1950 | ArgVec.reserve(NumArgs); |
| 1951 | for (unsigned i = 0; i != NumArgs; ++i) |
| 1952 | ArgVec.push_back(Args[i].getArgument()); |
| 1953 | |
| 1954 | return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, Canon); |
| 1955 | } |
| 1956 | |
| 1957 | QualType |
| 1958 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1959 | const TemplateArgument *Args, |
| 1960 | unsigned NumArgs, |
| 1961 | QualType Canon) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1962 | if (!Canon.isNull()) |
| 1963 | Canon = getCanonicalType(Canon); |
| 1964 | else { |
| 1965 | // Build the canonical template specialization type. |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1966 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); |
| 1967 | llvm::SmallVector<TemplateArgument, 4> CanonArgs; |
| 1968 | CanonArgs.reserve(NumArgs); |
| 1969 | for (unsigned I = 0; I != NumArgs; ++I) |
| 1970 | CanonArgs.push_back(getCanonicalTemplateArgument(Args[I])); |
| 1971 | |
| 1972 | // Determine whether this canonical template specialization type already |
| 1973 | // exists. |
| 1974 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1975 | TemplateSpecializationType::Profile(ID, CanonTemplate, |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 1976 | CanonArgs.data(), NumArgs, *this); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1977 | |
| 1978 | void *InsertPos = 0; |
| 1979 | TemplateSpecializationType *Spec |
| 1980 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1981 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1982 | if (!Spec) { |
| 1983 | // Allocate a new canonical template specialization type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1984 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1985 | sizeof(TemplateArgument) * NumArgs), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1986 | TypeAlignment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1987 | Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate, |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1988 | CanonArgs.data(), NumArgs, |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1989 | Canon); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1990 | Types.push_back(Spec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1991 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1992 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1993 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1994 | if (Canon.isNull()) |
| 1995 | Canon = QualType(Spec, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1996 | assert(Canon->isDependentType() && |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1997 | "Non-dependent template-id type must have a canonical type"); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1998 | } |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 1999 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2000 | // Allocate the (non-canonical) template specialization type, but don't |
| 2001 | // try to unique it: these types typically have location information that |
| 2002 | // we don't unique and don't want to lose. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2003 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2004 | sizeof(TemplateArgument) * NumArgs), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2005 | TypeAlignment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2006 | TemplateSpecializationType *Spec |
| 2007 | = new (Mem) TemplateSpecializationType(*this, Template, Args, NumArgs, |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 2008 | Canon); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2009 | |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2010 | Types.push_back(Spec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2011 | return QualType(Spec, 0); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2012 | } |
| 2013 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2014 | QualType |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 2015 | ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS, |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2016 | QualType NamedType) { |
| 2017 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 2018 | QualifiedNameType::Profile(ID, NNS, NamedType); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2019 | |
| 2020 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2021 | QualifiedNameType *T |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2022 | = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2023 | if (T) |
| 2024 | return QualType(T, 0); |
| 2025 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2026 | QualType Canon = NamedType; |
| 2027 | if (!Canon.isCanonical()) { |
| 2028 | Canon = getCanonicalType(NamedType); |
| 2029 | QualifiedNameType *CheckT |
| 2030 | = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2031 | assert(!CheckT && "Qualified name canonical type broken"); |
| 2032 | (void)CheckT; |
| 2033 | } |
| 2034 | |
| 2035 | T = new (*this) QualifiedNameType(NNS, NamedType, Canon); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2036 | Types.push_back(T); |
| 2037 | QualifiedNameTypes.InsertNode(T, InsertPos); |
| 2038 | return QualType(T, 0); |
| 2039 | } |
| 2040 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2041 | QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2042 | const IdentifierInfo *Name, |
| 2043 | QualType Canon) { |
| 2044 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 2045 | |
| 2046 | if (Canon.isNull()) { |
| 2047 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 2048 | if (CanonNNS != NNS) |
| 2049 | Canon = getTypenameType(CanonNNS, Name); |
| 2050 | } |
| 2051 | |
| 2052 | llvm::FoldingSetNodeID ID; |
| 2053 | TypenameType::Profile(ID, NNS, Name); |
| 2054 | |
| 2055 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2056 | TypenameType *T |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2057 | = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2058 | if (T) |
| 2059 | return QualType(T, 0); |
| 2060 | |
| 2061 | T = new (*this) TypenameType(NNS, Name, Canon); |
| 2062 | Types.push_back(T); |
| 2063 | TypenameTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2064 | return QualType(T, 0); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2067 | QualType |
| 2068 | ASTContext::getTypenameType(NestedNameSpecifier *NNS, |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2069 | const TemplateSpecializationType *TemplateId, |
| 2070 | QualType Canon) { |
| 2071 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 2072 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2073 | llvm::FoldingSetNodeID ID; |
| 2074 | TypenameType::Profile(ID, NNS, TemplateId); |
| 2075 | |
| 2076 | void *InsertPos = 0; |
| 2077 | TypenameType *T |
| 2078 | = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2079 | if (T) |
| 2080 | return QualType(T, 0); |
| 2081 | |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2082 | if (Canon.isNull()) { |
| 2083 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 2084 | QualType CanonType = getCanonicalType(QualType(TemplateId, 0)); |
| 2085 | if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) { |
| 2086 | const TemplateSpecializationType *CanonTemplateId |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2087 | = CanonType->getAs<TemplateSpecializationType>(); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2088 | assert(CanonTemplateId && |
| 2089 | "Canonical type must also be a template specialization type"); |
| 2090 | Canon = getTypenameType(CanonNNS, CanonTemplateId); |
| 2091 | } |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2092 | |
| 2093 | TypenameType *CheckT |
| 2094 | = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2095 | assert(!CheckT && "Typename canonical type is broken"); (void)CheckT; |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2096 | } |
| 2097 | |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2098 | T = new (*this) TypenameType(NNS, TemplateId, Canon); |
| 2099 | Types.push_back(T); |
| 2100 | TypenameTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2101 | return QualType(T, 0); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2102 | } |
| 2103 | |
John McCall | 7da2431 | 2009-09-05 00:15:47 +0000 | [diff] [blame] | 2104 | QualType |
| 2105 | ASTContext::getElaboratedType(QualType UnderlyingType, |
| 2106 | ElaboratedType::TagKind Tag) { |
| 2107 | llvm::FoldingSetNodeID ID; |
| 2108 | ElaboratedType::Profile(ID, UnderlyingType, Tag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2109 | |
John McCall | 7da2431 | 2009-09-05 00:15:47 +0000 | [diff] [blame] | 2110 | void *InsertPos = 0; |
| 2111 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2112 | if (T) |
| 2113 | return QualType(T, 0); |
| 2114 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2115 | QualType Canon = UnderlyingType; |
| 2116 | if (!Canon.isCanonical()) { |
| 2117 | Canon = getCanonicalType(Canon); |
| 2118 | ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2119 | assert(!CheckT && "Elaborated canonical type is broken"); (void)CheckT; |
| 2120 | } |
John McCall | 7da2431 | 2009-09-05 00:15:47 +0000 | [diff] [blame] | 2121 | |
| 2122 | T = new (*this) ElaboratedType(UnderlyingType, Tag, Canon); |
| 2123 | Types.push_back(T); |
| 2124 | ElaboratedTypes.InsertNode(T, InsertPos); |
| 2125 | return QualType(T, 0); |
| 2126 | } |
| 2127 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2128 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 2129 | /// alphabetically. |
| 2130 | static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, |
| 2131 | const ObjCProtocolDecl *RHS) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2132 | return LHS->getDeclName() < RHS->getDeclName(); |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2133 | } |
| 2134 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2135 | static bool areSortedAndUniqued(ObjCProtocolDecl **Protocols, |
| 2136 | unsigned NumProtocols) { |
| 2137 | if (NumProtocols == 0) return true; |
| 2138 | |
| 2139 | for (unsigned i = 1; i != NumProtocols; ++i) |
| 2140 | if (!CmpProtocolNames(Protocols[i-1], Protocols[i])) |
| 2141 | return false; |
| 2142 | return true; |
| 2143 | } |
| 2144 | |
| 2145 | static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols, |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2146 | unsigned &NumProtocols) { |
| 2147 | ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2148 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2149 | // Sort protocols, keyed by name. |
| 2150 | std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); |
| 2151 | |
| 2152 | // Remove duplicates. |
| 2153 | ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); |
| 2154 | NumProtocols = ProtocolsEnd-Protocols; |
| 2155 | } |
| 2156 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2157 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 2158 | /// the given interface decl and the conforming protocol list. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2159 | QualType ASTContext::getObjCObjectPointerType(QualType InterfaceT, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2160 | ObjCProtocolDecl **Protocols, |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2161 | unsigned NumProtocols) { |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2162 | llvm::FoldingSetNodeID ID; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2163 | ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2164 | |
| 2165 | void *InsertPos = 0; |
| 2166 | if (ObjCObjectPointerType *QT = |
| 2167 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2168 | return QualType(QT, 0); |
| 2169 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2170 | // Sort the protocol list alphabetically to canonicalize it. |
| 2171 | QualType Canonical; |
| 2172 | if (!InterfaceT.isCanonical() || |
| 2173 | !areSortedAndUniqued(Protocols, NumProtocols)) { |
| 2174 | if (!areSortedAndUniqued(Protocols, NumProtocols)) { |
| 2175 | llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(NumProtocols); |
| 2176 | unsigned UniqueCount = NumProtocols; |
| 2177 | |
| 2178 | std::copy(Protocols, Protocols + NumProtocols, Sorted.begin()); |
| 2179 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
| 2180 | |
| 2181 | Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT), |
| 2182 | &Sorted[0], UniqueCount); |
| 2183 | } else { |
| 2184 | Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT), |
| 2185 | Protocols, NumProtocols); |
| 2186 | } |
| 2187 | |
| 2188 | // Regenerate InsertPos. |
| 2189 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2190 | } |
| 2191 | |
Douglas Gregor | fd6a088 | 2010-02-08 22:59:26 +0000 | [diff] [blame] | 2192 | // No match. |
| 2193 | unsigned Size = sizeof(ObjCObjectPointerType) |
| 2194 | + NumProtocols * sizeof(ObjCProtocolDecl *); |
| 2195 | void *Mem = Allocate(Size, TypeAlignment); |
| 2196 | ObjCObjectPointerType *QType = new (Mem) ObjCObjectPointerType(Canonical, |
| 2197 | InterfaceT, |
| 2198 | Protocols, |
| 2199 | NumProtocols); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2200 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2201 | Types.push_back(QType); |
| 2202 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
| 2203 | return QualType(QType, 0); |
| 2204 | } |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2205 | |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 2206 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 2207 | /// specified ObjC interface decl. The list of protocols is optional. |
| 2208 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl, |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2209 | ObjCProtocolDecl **Protocols, unsigned NumProtocols) { |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2210 | llvm::FoldingSetNodeID ID; |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 2211 | ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2212 | |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2213 | void *InsertPos = 0; |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 2214 | if (ObjCInterfaceType *QT = |
| 2215 | ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2216 | return QualType(QT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2217 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2218 | // Sort the protocol list alphabetically to canonicalize it. |
| 2219 | QualType Canonical; |
| 2220 | if (NumProtocols && !areSortedAndUniqued(Protocols, NumProtocols)) { |
| 2221 | llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(NumProtocols); |
| 2222 | std::copy(Protocols, Protocols + NumProtocols, Sorted.begin()); |
| 2223 | |
| 2224 | unsigned UniqueCount = NumProtocols; |
| 2225 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
| 2226 | |
| 2227 | Canonical = getObjCInterfaceType(Decl, &Sorted[0], UniqueCount); |
| 2228 | |
| 2229 | ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2230 | } |
| 2231 | |
Douglas Gregor | fd6a088 | 2010-02-08 22:59:26 +0000 | [diff] [blame] | 2232 | unsigned Size = sizeof(ObjCInterfaceType) |
| 2233 | + NumProtocols * sizeof(ObjCProtocolDecl *); |
| 2234 | void *Mem = Allocate(Size, TypeAlignment); |
| 2235 | ObjCInterfaceType *QType = new (Mem) ObjCInterfaceType(Canonical, |
| 2236 | const_cast<ObjCInterfaceDecl*>(Decl), |
| 2237 | Protocols, |
| 2238 | NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2239 | |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2240 | Types.push_back(QType); |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 2241 | ObjCInterfaceTypes.InsertNode(QType, InsertPos); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2242 | return QualType(QType, 0); |
| 2243 | } |
| 2244 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2245 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 2246 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2247 | /// multiple declarations that refer to "typeof(x)" all contain different |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2248 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2249 | /// on canonical type's (which are always unique). |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2250 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2251 | TypeOfExprType *toe; |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2252 | if (tofExpr->isTypeDependent()) { |
| 2253 | llvm::FoldingSetNodeID ID; |
| 2254 | DependentTypeOfExprType::Profile(ID, *this, tofExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2255 | |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2256 | void *InsertPos = 0; |
| 2257 | DependentTypeOfExprType *Canon |
| 2258 | = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2259 | if (Canon) { |
| 2260 | // We already have a "canonical" version of an identical, dependent |
| 2261 | // typeof(expr) type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2262 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2263 | QualType((TypeOfExprType*)Canon, 0)); |
| 2264 | } |
| 2265 | else { |
| 2266 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2267 | Canon |
| 2268 | = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2269 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); |
| 2270 | toe = Canon; |
| 2271 | } |
| 2272 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2273 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2274 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2275 | } |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2276 | Types.push_back(toe); |
| 2277 | return QualType(toe, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2278 | } |
| 2279 | |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2280 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 2281 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 2282 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2283 | /// an issue. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2284 | /// on canonical type's (which are always unique). |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2285 | QualType ASTContext::getTypeOfType(QualType tofType) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2286 | QualType Canonical = getCanonicalType(tofType); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2287 | TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2288 | Types.push_back(tot); |
| 2289 | return QualType(tot, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2290 | } |
| 2291 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2292 | /// getDecltypeForExpr - Given an expr, will return the decltype for that |
| 2293 | /// expression, according to the rules in C++0x [dcl.type.simple]p4 |
| 2294 | static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) { |
Anders Carlsson | a07c33e | 2009-06-25 15:00:34 +0000 | [diff] [blame] | 2295 | if (e->isTypeDependent()) |
| 2296 | return Context.DependentTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2297 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2298 | // If e is an id expression or a class member access, decltype(e) is defined |
| 2299 | // as the type of the entity named by e. |
| 2300 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) { |
| 2301 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) |
| 2302 | return VD->getType(); |
| 2303 | } |
| 2304 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) { |
| 2305 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) |
| 2306 | return FD->getType(); |
| 2307 | } |
| 2308 | // If e is a function call or an invocation of an overloaded operator, |
| 2309 | // (parentheses around e are ignored), decltype(e) is defined as the |
| 2310 | // return type of that function. |
| 2311 | if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens())) |
| 2312 | return CE->getCallReturnType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2313 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2314 | QualType T = e->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2315 | |
| 2316 | // 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] | 2317 | // defined as T&, otherwise decltype(e) is defined as T. |
| 2318 | if (e->isLvalue(Context) == Expr::LV_Valid) |
| 2319 | T = Context.getLValueReferenceType(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2320 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2321 | return T; |
| 2322 | } |
| 2323 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2324 | /// getDecltypeType - Unlike many "get<Type>" functions, we don't unique |
| 2325 | /// DecltypeType AST's. The only motivation to unique these nodes would be |
| 2326 | /// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2327 | /// an issue. This doesn't effect the type checker, since it operates |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2328 | /// on canonical type's (which are always unique). |
| 2329 | QualType ASTContext::getDecltypeType(Expr *e) { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2330 | DecltypeType *dt; |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2331 | if (e->isTypeDependent()) { |
| 2332 | llvm::FoldingSetNodeID ID; |
| 2333 | DependentDecltypeType::Profile(ID, *this, e); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2334 | |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2335 | void *InsertPos = 0; |
| 2336 | DependentDecltypeType *Canon |
| 2337 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2338 | if (Canon) { |
| 2339 | // We already have a "canonical" version of an equivalent, dependent |
| 2340 | // decltype type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2341 | dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy, |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2342 | QualType((DecltypeType*)Canon, 0)); |
| 2343 | } |
| 2344 | else { |
| 2345 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2346 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2347 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); |
| 2348 | dt = Canon; |
| 2349 | } |
| 2350 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2351 | QualType T = getDecltypeForExpr(e, *this); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2352 | dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T)); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2353 | } |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2354 | Types.push_back(dt); |
| 2355 | return QualType(dt, 0); |
| 2356 | } |
| 2357 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2358 | /// getTagDeclType - Return the unique reference to the type for the |
| 2359 | /// specified TagDecl (struct/union/class/enum) decl. |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 2360 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) { |
Ted Kremenek | d778f88 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 2361 | assert (Decl); |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 2362 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 2363 | // away const? mutable? |
| 2364 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2365 | } |
| 2366 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2367 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 2368 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 2369 | /// needs to agree with the definition in <stddef.h>. |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 2370 | CanQualType ASTContext::getSizeType() const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2371 | return getFromTargetType(Target.getSizeType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2372 | } |
| 2373 | |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 2374 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 2375 | /// Used when in C++, as a GCC extension. |
| 2376 | QualType ASTContext::getSignedWCharType() const { |
| 2377 | // FIXME: derive from "Target" ? |
| 2378 | return WCharTy; |
| 2379 | } |
| 2380 | |
| 2381 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 2382 | /// Used when in C++, as a GCC extension. |
| 2383 | QualType ASTContext::getUnsignedWCharType() const { |
| 2384 | // FIXME: derive from "Target" ? |
| 2385 | return UnsignedIntTy; |
| 2386 | } |
| 2387 | |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 2388 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) |
| 2389 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 2390 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2391 | return getFromTargetType(Target.getPtrDiffType(0)); |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 2392 | } |
| 2393 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2394 | //===----------------------------------------------------------------------===// |
| 2395 | // Type Operators |
| 2396 | //===----------------------------------------------------------------------===// |
| 2397 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2398 | CanQualType ASTContext::getCanonicalParamType(QualType T) { |
| 2399 | // Push qualifiers into arrays, and then discard any remaining |
| 2400 | // qualifiers. |
| 2401 | T = getCanonicalType(T); |
| 2402 | const Type *Ty = T.getTypePtr(); |
| 2403 | |
| 2404 | QualType Result; |
| 2405 | if (isa<ArrayType>(Ty)) { |
| 2406 | Result = getArrayDecayedType(QualType(Ty,0)); |
| 2407 | } else if (isa<FunctionType>(Ty)) { |
| 2408 | Result = getPointerType(QualType(Ty, 0)); |
| 2409 | } else { |
| 2410 | Result = QualType(Ty, 0); |
| 2411 | } |
| 2412 | |
| 2413 | return CanQualType::CreateUnsafe(Result); |
| 2414 | } |
| 2415 | |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 2416 | /// getCanonicalType - Return the canonical (structural) type corresponding to |
| 2417 | /// the specified potentially non-canonical type. The non-canonical version |
| 2418 | /// of a type may have many "decorated" versions of types. Decorators can |
| 2419 | /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed |
| 2420 | /// to be free of any of these, allowing two canonical types to be compared |
| 2421 | /// for exact equality with a simple pointer comparison. |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2422 | CanQualType ASTContext::getCanonicalType(QualType T) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2423 | QualifierCollector Quals; |
| 2424 | const Type *Ptr = Quals.strip(T); |
| 2425 | QualType CanType = Ptr->getCanonicalTypeInternal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2426 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2427 | // The canonical internal type will be the canonical type *except* |
| 2428 | // that we push type qualifiers down through array types. |
| 2429 | |
| 2430 | // If there are no new qualifiers to push down, stop here. |
| 2431 | if (!Quals.hasQualifiers()) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2432 | return CanQualType::CreateUnsafe(CanType); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2433 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2434 | // If the type qualifiers are on an array type, get the canonical |
| 2435 | // type of the array with the qualifiers applied to the element |
| 2436 | // type. |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2437 | ArrayType *AT = dyn_cast<ArrayType>(CanType); |
| 2438 | if (!AT) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2439 | return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2440 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2441 | // Get the canonical version of the element with the extra qualifiers on it. |
| 2442 | // This can recursively sink qualifiers through multiple levels of arrays. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2443 | QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2444 | NewEltTy = getCanonicalType(NewEltTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2445 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2446 | if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2447 | return CanQualType::CreateUnsafe( |
| 2448 | getConstantArrayType(NewEltTy, CAT->getSize(), |
| 2449 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2450 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2451 | if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2452 | return CanQualType::CreateUnsafe( |
| 2453 | getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2454 | IAT->getIndexTypeCVRQualifiers())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2455 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2456 | if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2457 | return CanQualType::CreateUnsafe( |
| 2458 | getDependentSizedArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2459 | DSAT->getSizeExpr() ? |
| 2460 | DSAT->getSizeExpr()->Retain() : 0, |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2461 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2462 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 87a924e | 2009-10-30 22:56:57 +0000 | [diff] [blame] | 2463 | DSAT->getBracketsRange())->getCanonicalTypeInternal()); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2464 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2465 | VariableArrayType *VAT = cast<VariableArrayType>(AT); |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2466 | return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2467 | VAT->getSizeExpr() ? |
| 2468 | VAT->getSizeExpr()->Retain() : 0, |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2469 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2470 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2471 | VAT->getBracketsRange())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2472 | } |
| 2473 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2474 | QualType ASTContext::getUnqualifiedArrayType(QualType T, |
| 2475 | Qualifiers &Quals) { |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2476 | Quals = T.getQualifiers(); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2477 | if (!isa<ArrayType>(T)) { |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2478 | return T.getUnqualifiedType(); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2479 | } |
| 2480 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2481 | const ArrayType *AT = cast<ArrayType>(T); |
| 2482 | QualType Elt = AT->getElementType(); |
Zhongxing Xu | c1ae0a8 | 2010-01-05 08:15:06 +0000 | [diff] [blame] | 2483 | QualType UnqualElt = getUnqualifiedArrayType(Elt, Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2484 | if (Elt == UnqualElt) |
| 2485 | return T; |
| 2486 | |
| 2487 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T)) { |
| 2488 | return getConstantArrayType(UnqualElt, CAT->getSize(), |
| 2489 | CAT->getSizeModifier(), 0); |
| 2490 | } |
| 2491 | |
| 2492 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(T)) { |
| 2493 | return getIncompleteArrayType(UnqualElt, IAT->getSizeModifier(), 0); |
| 2494 | } |
| 2495 | |
| 2496 | const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(T); |
| 2497 | return getDependentSizedArrayType(UnqualElt, DSAT->getSizeExpr()->Retain(), |
| 2498 | DSAT->getSizeModifier(), 0, |
| 2499 | SourceRange()); |
| 2500 | } |
| 2501 | |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2502 | DeclarationName ASTContext::getNameForTemplate(TemplateName Name) { |
| 2503 | if (TemplateDecl *TD = Name.getAsTemplateDecl()) |
| 2504 | return TD->getDeclName(); |
| 2505 | |
| 2506 | if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) { |
| 2507 | if (DTN->isIdentifier()) { |
| 2508 | return DeclarationNames.getIdentifier(DTN->getIdentifier()); |
| 2509 | } else { |
| 2510 | return DeclarationNames.getCXXOperatorName(DTN->getOperator()); |
| 2511 | } |
| 2512 | } |
| 2513 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 2514 | OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate(); |
| 2515 | assert(Storage); |
| 2516 | return (*Storage->begin())->getDeclName(); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2517 | } |
| 2518 | |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2519 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) { |
| 2520 | // If this template name refers to a template, the canonical |
| 2521 | // template name merely stores the template itself. |
| 2522 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 2523 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2524 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 2525 | assert(!Name.getAsOverloadedTemplate()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2526 | |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2527 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 2528 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 2529 | return DTN->CanonicalTemplateName; |
| 2530 | } |
| 2531 | |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 2532 | bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) { |
| 2533 | X = getCanonicalTemplateName(X); |
| 2534 | Y = getCanonicalTemplateName(Y); |
| 2535 | return X.getAsVoidPointer() == Y.getAsVoidPointer(); |
| 2536 | } |
| 2537 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2538 | TemplateArgument |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2539 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) { |
| 2540 | switch (Arg.getKind()) { |
| 2541 | case TemplateArgument::Null: |
| 2542 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2543 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2544 | case TemplateArgument::Expression: |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2545 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2546 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2547 | case TemplateArgument::Declaration: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2548 | return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2549 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2550 | case TemplateArgument::Template: |
| 2551 | return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate())); |
| 2552 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2553 | case TemplateArgument::Integral: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2554 | return TemplateArgument(*Arg.getAsIntegral(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2555 | getCanonicalType(Arg.getIntegralType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2556 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2557 | case TemplateArgument::Type: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2558 | return TemplateArgument(getCanonicalType(Arg.getAsType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2559 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2560 | case TemplateArgument::Pack: { |
| 2561 | // FIXME: Allocate in ASTContext |
| 2562 | TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()]; |
| 2563 | unsigned Idx = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2564 | for (TemplateArgument::pack_iterator A = Arg.pack_begin(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2565 | AEnd = Arg.pack_end(); |
| 2566 | A != AEnd; (void)++A, ++Idx) |
| 2567 | CanonArgs[Idx] = getCanonicalTemplateArgument(*A); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2568 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2569 | TemplateArgument Result; |
| 2570 | Result.setArgumentPack(CanonArgs, Arg.pack_size(), false); |
| 2571 | return Result; |
| 2572 | } |
| 2573 | } |
| 2574 | |
| 2575 | // Silence GCC warning |
| 2576 | assert(false && "Unhandled template argument kind"); |
| 2577 | return TemplateArgument(); |
| 2578 | } |
| 2579 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2580 | NestedNameSpecifier * |
| 2581 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2582 | if (!NNS) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2583 | return 0; |
| 2584 | |
| 2585 | switch (NNS->getKind()) { |
| 2586 | case NestedNameSpecifier::Identifier: |
| 2587 | // Canonicalize the prefix but keep the identifier the same. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2588 | return NestedNameSpecifier::Create(*this, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2589 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 2590 | NNS->getAsIdentifier()); |
| 2591 | |
| 2592 | case NestedNameSpecifier::Namespace: |
| 2593 | // A namespace is canonical; build a nested-name-specifier with |
| 2594 | // this namespace and no prefix. |
| 2595 | return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace()); |
| 2596 | |
| 2597 | case NestedNameSpecifier::TypeSpec: |
| 2598 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 2599 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2600 | return NestedNameSpecifier::Create(*this, 0, |
| 2601 | NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2602 | T.getTypePtr()); |
| 2603 | } |
| 2604 | |
| 2605 | case NestedNameSpecifier::Global: |
| 2606 | // The global specifier is canonical and unique. |
| 2607 | return NNS; |
| 2608 | } |
| 2609 | |
| 2610 | // Required to silence a GCC warning |
| 2611 | return 0; |
| 2612 | } |
| 2613 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2614 | |
| 2615 | const ArrayType *ASTContext::getAsArrayType(QualType T) { |
| 2616 | // Handle the non-qualified case efficiently. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2617 | if (!T.hasLocalQualifiers()) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2618 | // Handle the common positive case fast. |
| 2619 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 2620 | return AT; |
| 2621 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2622 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2623 | // Handle the common negative case fast. |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2624 | QualType CType = T->getCanonicalTypeInternal(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2625 | if (!isa<ArrayType>(CType)) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2626 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2627 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2628 | // Apply any qualifiers from the array type to the element type. This |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2629 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 2630 | // 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] | 2631 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2632 | // If we get here, we either have type qualifiers on the type, or we have |
| 2633 | // 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] | 2634 | // we must propagate them down into the element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2635 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2636 | QualifierCollector Qs; |
| 2637 | const Type *Ty = Qs.strip(T.getDesugaredType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2638 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2639 | // If we have a simple case, just return now. |
| 2640 | const ArrayType *ATy = dyn_cast<ArrayType>(Ty); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2641 | if (ATy == 0 || Qs.empty()) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2642 | return ATy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2643 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2644 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 2645 | // qualifiers into the array element type and return a new array type. |
| 2646 | // Get the canonical version of the element with the extra qualifiers on it. |
| 2647 | // This can recursively sink qualifiers through multiple levels of arrays. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2648 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2649 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2650 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 2651 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 2652 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2653 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2654 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 2655 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 2656 | IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2657 | IAT->getIndexTypeCVRQualifiers())); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2658 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2659 | if (const DependentSizedArrayType *DSAT |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2660 | = dyn_cast<DependentSizedArrayType>(ATy)) |
| 2661 | return cast<ArrayType>( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2662 | getDependentSizedArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2663 | DSAT->getSizeExpr() ? |
| 2664 | DSAT->getSizeExpr()->Retain() : 0, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2665 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2666 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2667 | DSAT->getBracketsRange())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2668 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2669 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2670 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2671 | VAT->getSizeExpr() ? |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2672 | VAT->getSizeExpr()->Retain() : 0, |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2673 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2674 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2675 | VAT->getBracketsRange())); |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 2676 | } |
| 2677 | |
| 2678 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2679 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 2680 | /// specified array type to a pointer. This operation is non-trivial when |
| 2681 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 2682 | /// this returns a pointer to a properly qualified element of the array. |
| 2683 | /// |
| 2684 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
| 2685 | QualType ASTContext::getArrayDecayedType(QualType Ty) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2686 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 2687 | // typedefs in the element type of the array. This also handles propagation |
| 2688 | // of type qualifiers from the array type into the element type if present |
| 2689 | // (C99 6.7.3p8). |
| 2690 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 2691 | assert(PrettyArrayType && "Not an array type!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2692 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2693 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2694 | |
| 2695 | // int x[restrict 4] -> int *restrict |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2696 | return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2697 | } |
| 2698 | |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2699 | QualType ASTContext::getBaseElementType(QualType QT) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2700 | QualifierCollector Qs; |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2701 | while (true) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2702 | const Type *UT = Qs.strip(QT); |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2703 | if (const ArrayType *AT = getAsArrayType(QualType(UT,0))) { |
| 2704 | QT = AT->getElementType(); |
Mike Stump | 6dcbc29 | 2009-07-25 23:24:03 +0000 | [diff] [blame] | 2705 | } else { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2706 | return Qs.apply(QT); |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2707 | } |
| 2708 | } |
| 2709 | } |
| 2710 | |
Anders Carlsson | fbbce49 | 2009-09-25 01:23:32 +0000 | [diff] [blame] | 2711 | QualType ASTContext::getBaseElementType(const ArrayType *AT) { |
| 2712 | QualType ElemTy = AT->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2713 | |
Anders Carlsson | fbbce49 | 2009-09-25 01:23:32 +0000 | [diff] [blame] | 2714 | if (const ArrayType *AT = getAsArrayType(ElemTy)) |
| 2715 | return getBaseElementType(AT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2716 | |
Anders Carlsson | 6183a99 | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 2717 | return ElemTy; |
| 2718 | } |
| 2719 | |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 2720 | /// getConstantArrayElementCount - Returns number of constant array elements. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2721 | uint64_t |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 2722 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
| 2723 | uint64_t ElementCount = 1; |
| 2724 | do { |
| 2725 | ElementCount *= CA->getSize().getZExtValue(); |
| 2726 | CA = dyn_cast<ConstantArrayType>(CA->getElementType()); |
| 2727 | } while (CA); |
| 2728 | return ElementCount; |
| 2729 | } |
| 2730 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2731 | /// getFloatingRank - Return a relative rank for floating point types. |
| 2732 | /// 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] | 2733 | static FloatingRank getFloatingRank(QualType T) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2734 | if (const ComplexType *CT = T->getAs<ComplexType>()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2735 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2736 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2737 | assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"); |
| 2738 | switch (T->getAs<BuiltinType>()->getKind()) { |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2739 | default: assert(0 && "getFloatingRank(): not a floating type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2740 | case BuiltinType::Float: return FloatRank; |
| 2741 | case BuiltinType::Double: return DoubleRank; |
| 2742 | case BuiltinType::LongDouble: return LongDoubleRank; |
| 2743 | } |
| 2744 | } |
| 2745 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2746 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 2747 | /// point or a complex type (based on typeDomain/typeSize). |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 2748 | /// 'typeDomain' is a real floating point or complex type. |
| 2749 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 2750 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 2751 | QualType Domain) const { |
| 2752 | FloatingRank EltRank = getFloatingRank(Size); |
| 2753 | if (Domain->isComplexType()) { |
| 2754 | switch (EltRank) { |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 2755 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 2756 | case FloatRank: return FloatComplexTy; |
| 2757 | case DoubleRank: return DoubleComplexTy; |
| 2758 | case LongDoubleRank: return LongDoubleComplexTy; |
| 2759 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2760 | } |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 2761 | |
| 2762 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 2763 | switch (EltRank) { |
| 2764 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
| 2765 | case FloatRank: return FloatTy; |
| 2766 | case DoubleRank: return DoubleTy; |
| 2767 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 2768 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2769 | } |
| 2770 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2771 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 2772 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 2773 | /// '_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] | 2774 | /// LHS < RHS, return -1. |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2775 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) { |
| 2776 | FloatingRank LHSR = getFloatingRank(LHS); |
| 2777 | FloatingRank RHSR = getFloatingRank(RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2778 | |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2779 | if (LHSR == RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 2780 | return 0; |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2781 | if (LHSR > RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 2782 | return 1; |
| 2783 | return -1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2784 | } |
| 2785 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2786 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 2787 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 2788 | /// or if it is not canonicalized. |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2789 | unsigned ASTContext::getIntegerRank(Type *T) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2790 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2791 | if (EnumType* ET = dyn_cast<EnumType>(T)) |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 2792 | T = ET->getDecl()->getPromotionType().getTypePtr(); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2793 | |
Eli Friedman | a342675 | 2009-07-05 23:44:27 +0000 | [diff] [blame] | 2794 | if (T->isSpecificBuiltinType(BuiltinType::WChar)) |
| 2795 | T = getFromTargetType(Target.getWCharType()).getTypePtr(); |
| 2796 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2797 | if (T->isSpecificBuiltinType(BuiltinType::Char16)) |
| 2798 | T = getFromTargetType(Target.getChar16Type()).getTypePtr(); |
| 2799 | |
| 2800 | if (T->isSpecificBuiltinType(BuiltinType::Char32)) |
| 2801 | T = getFromTargetType(Target.getChar32Type()).getTypePtr(); |
| 2802 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2803 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2804 | default: assert(0 && "getIntegerRank(): not a built-in integer"); |
| 2805 | case BuiltinType::Bool: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2806 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2807 | case BuiltinType::Char_S: |
| 2808 | case BuiltinType::Char_U: |
| 2809 | case BuiltinType::SChar: |
| 2810 | case BuiltinType::UChar: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2811 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2812 | case BuiltinType::Short: |
| 2813 | case BuiltinType::UShort: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2814 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2815 | case BuiltinType::Int: |
| 2816 | case BuiltinType::UInt: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2817 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2818 | case BuiltinType::Long: |
| 2819 | case BuiltinType::ULong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2820 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2821 | case BuiltinType::LongLong: |
| 2822 | case BuiltinType::ULongLong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2823 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 2824 | case BuiltinType::Int128: |
| 2825 | case BuiltinType::UInt128: |
| 2826 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2827 | } |
| 2828 | } |
| 2829 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 2830 | /// \brief Whether this is a promotable bitfield reference according |
| 2831 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
| 2832 | /// |
| 2833 | /// \returns the type this bit-field will promote to, or NULL if no |
| 2834 | /// promotion occurs. |
| 2835 | QualType ASTContext::isPromotableBitField(Expr *E) { |
| 2836 | FieldDecl *Field = E->getBitField(); |
| 2837 | if (!Field) |
| 2838 | return QualType(); |
| 2839 | |
| 2840 | QualType FT = Field->getType(); |
| 2841 | |
| 2842 | llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this); |
| 2843 | uint64_t BitWidth = BitWidthAP.getZExtValue(); |
| 2844 | uint64_t IntSize = getTypeSize(IntTy); |
| 2845 | // GCC extension compatibility: if the bit-field size is less than or equal |
| 2846 | // to the size of int, it gets promoted no matter what its type is. |
| 2847 | // For instance, unsigned long bf : 4 gets promoted to signed int. |
| 2848 | if (BitWidth < IntSize) |
| 2849 | return IntTy; |
| 2850 | |
| 2851 | if (BitWidth == IntSize) |
| 2852 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
| 2853 | |
| 2854 | // Types bigger than int are not subject to promotions, and therefore act |
| 2855 | // like the base type. |
| 2856 | // FIXME: This doesn't quite match what gcc does, but what gcc does here |
| 2857 | // is ridiculous. |
| 2858 | return QualType(); |
| 2859 | } |
| 2860 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 2861 | /// getPromotedIntegerType - Returns the type that Promotable will |
| 2862 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
| 2863 | /// integer type. |
| 2864 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) { |
| 2865 | assert(!Promotable.isNull()); |
| 2866 | assert(Promotable->isPromotableIntegerType()); |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 2867 | if (const EnumType *ET = Promotable->getAs<EnumType>()) |
| 2868 | return ET->getDecl()->getPromotionType(); |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 2869 | if (Promotable->isSignedIntegerType()) |
| 2870 | return IntTy; |
| 2871 | uint64_t PromotableSize = getTypeSize(Promotable); |
| 2872 | uint64_t IntSize = getTypeSize(IntTy); |
| 2873 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
| 2874 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
| 2875 | } |
| 2876 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2877 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2878 | /// 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] | 2879 | /// LHS < RHS, return -1. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2880 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2881 | Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 2882 | Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2883 | if (LHSC == RHSC) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2884 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2885 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 2886 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2887 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2888 | unsigned LHSRank = getIntegerRank(LHSC); |
| 2889 | unsigned RHSRank = getIntegerRank(RHSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2890 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2891 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 2892 | if (LHSRank == RHSRank) return 0; |
| 2893 | return LHSRank > RHSRank ? 1 : -1; |
| 2894 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2895 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2896 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 2897 | if (LHSUnsigned) { |
| 2898 | // If the unsigned [LHS] type is larger, return it. |
| 2899 | if (LHSRank >= RHSRank) |
| 2900 | return 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2901 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2902 | // If the signed type can represent all values of the unsigned type, it |
| 2903 | // 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] | 2904 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2905 | return -1; |
| 2906 | } |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2907 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2908 | // If the unsigned [RHS] type is larger, return it. |
| 2909 | if (RHSRank >= LHSRank) |
| 2910 | return -1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2911 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2912 | // If the signed type can represent all values of the unsigned type, it |
| 2913 | // 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] | 2914 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2915 | return 1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2916 | } |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2917 | |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 2918 | static RecordDecl * |
| 2919 | CreateRecordDecl(ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC, |
| 2920 | SourceLocation L, IdentifierInfo *Id) { |
| 2921 | if (Ctx.getLangOptions().CPlusPlus) |
| 2922 | return CXXRecordDecl::Create(Ctx, TK, DC, L, Id); |
| 2923 | else |
| 2924 | return RecordDecl::Create(Ctx, TK, DC, L, Id); |
| 2925 | } |
| 2926 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2927 | // getCFConstantStringType - Return the type used for constant CFStrings. |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2928 | QualType ASTContext::getCFConstantStringType() { |
| 2929 | if (!CFConstantStringTypeDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2930 | CFConstantStringTypeDecl = |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 2931 | CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
| 2932 | &Idents.get("NSConstantString")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 2933 | CFConstantStringTypeDecl->startDefinition(); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 2934 | |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 2935 | QualType FieldTypes[4]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2936 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2937 | // const int *isa; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2938 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 2939 | // int flags; |
| 2940 | FieldTypes[1] = IntTy; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2941 | // const char *str; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2942 | FieldTypes[2] = getPointerType(CharTy.withConst()); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2943 | // long length; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2944 | FieldTypes[3] = LongTy; |
| 2945 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2946 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2947 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2948 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2949 | SourceLocation(), 0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2950 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2951 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2952 | /*Mutable=*/false); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2953 | CFConstantStringTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2954 | } |
| 2955 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2956 | CFConstantStringTypeDecl->completeDefinition(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2957 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2958 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2959 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 8467583 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 2960 | } |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 2961 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 2962 | void ASTContext::setCFConstantStringType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2963 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 2964 | assert(Rec && "Invalid CFConstantStringType"); |
| 2965 | CFConstantStringTypeDecl = Rec->getDecl(); |
| 2966 | } |
| 2967 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2968 | QualType ASTContext::getObjCFastEnumerationStateType() { |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2969 | if (!ObjCFastEnumerationStateTypeDecl) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2970 | ObjCFastEnumerationStateTypeDecl = |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 2971 | CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
| 2972 | &Idents.get("__objcFastEnumerationState")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 2973 | ObjCFastEnumerationStateTypeDecl->startDefinition(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2974 | |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2975 | QualType FieldTypes[] = { |
| 2976 | UnsignedLongTy, |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2977 | getPointerType(ObjCIdTypedefType), |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2978 | getPointerType(UnsignedLongTy), |
| 2979 | getConstantArrayType(UnsignedLongTy, |
| 2980 | llvm::APInt(32, 5), ArrayType::Normal, 0) |
| 2981 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2982 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2983 | for (size_t i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2984 | FieldDecl *Field = FieldDecl::Create(*this, |
| 2985 | ObjCFastEnumerationStateTypeDecl, |
| 2986 | SourceLocation(), 0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2987 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2988 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2989 | /*Mutable=*/false); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2990 | ObjCFastEnumerationStateTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2991 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2992 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2993 | ObjCFastEnumerationStateTypeDecl->completeDefinition(); |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2994 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2995 | |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2996 | return getTagDeclType(ObjCFastEnumerationStateTypeDecl); |
| 2997 | } |
| 2998 | |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2999 | QualType ASTContext::getBlockDescriptorType() { |
| 3000 | if (BlockDescriptorType) |
| 3001 | return getTagDeclType(BlockDescriptorType); |
| 3002 | |
| 3003 | RecordDecl *T; |
| 3004 | // FIXME: Needs the FlagAppleBlock bit. |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3005 | T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
| 3006 | &Idents.get("__block_descriptor")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3007 | T->startDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3008 | |
| 3009 | QualType FieldTypes[] = { |
| 3010 | UnsignedLongTy, |
| 3011 | UnsignedLongTy, |
| 3012 | }; |
| 3013 | |
| 3014 | const char *FieldNames[] = { |
| 3015 | "reserved", |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3016 | "Size" |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3017 | }; |
| 3018 | |
| 3019 | for (size_t i = 0; i < 2; ++i) { |
| 3020 | FieldDecl *Field = FieldDecl::Create(*this, |
| 3021 | T, |
| 3022 | SourceLocation(), |
| 3023 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3024 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3025 | /*BitWidth=*/0, |
| 3026 | /*Mutable=*/false); |
| 3027 | T->addDecl(Field); |
| 3028 | } |
| 3029 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3030 | T->completeDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3031 | |
| 3032 | BlockDescriptorType = T; |
| 3033 | |
| 3034 | return getTagDeclType(BlockDescriptorType); |
| 3035 | } |
| 3036 | |
| 3037 | void ASTContext::setBlockDescriptorType(QualType T) { |
| 3038 | const RecordType *Rec = T->getAs<RecordType>(); |
| 3039 | assert(Rec && "Invalid BlockDescriptorType"); |
| 3040 | BlockDescriptorType = Rec->getDecl(); |
| 3041 | } |
| 3042 | |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3043 | QualType ASTContext::getBlockDescriptorExtendedType() { |
| 3044 | if (BlockDescriptorExtendedType) |
| 3045 | return getTagDeclType(BlockDescriptorExtendedType); |
| 3046 | |
| 3047 | RecordDecl *T; |
| 3048 | // FIXME: Needs the FlagAppleBlock bit. |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3049 | T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
| 3050 | &Idents.get("__block_descriptor_withcopydispose")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3051 | T->startDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3052 | |
| 3053 | QualType FieldTypes[] = { |
| 3054 | UnsignedLongTy, |
| 3055 | UnsignedLongTy, |
| 3056 | getPointerType(VoidPtrTy), |
| 3057 | getPointerType(VoidPtrTy) |
| 3058 | }; |
| 3059 | |
| 3060 | const char *FieldNames[] = { |
| 3061 | "reserved", |
| 3062 | "Size", |
| 3063 | "CopyFuncPtr", |
| 3064 | "DestroyFuncPtr" |
| 3065 | }; |
| 3066 | |
| 3067 | for (size_t i = 0; i < 4; ++i) { |
| 3068 | FieldDecl *Field = FieldDecl::Create(*this, |
| 3069 | T, |
| 3070 | SourceLocation(), |
| 3071 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3072 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3073 | /*BitWidth=*/0, |
| 3074 | /*Mutable=*/false); |
| 3075 | T->addDecl(Field); |
| 3076 | } |
| 3077 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3078 | T->completeDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3079 | |
| 3080 | BlockDescriptorExtendedType = T; |
| 3081 | |
| 3082 | return getTagDeclType(BlockDescriptorExtendedType); |
| 3083 | } |
| 3084 | |
| 3085 | void ASTContext::setBlockDescriptorExtendedType(QualType T) { |
| 3086 | const RecordType *Rec = T->getAs<RecordType>(); |
| 3087 | assert(Rec && "Invalid BlockDescriptorType"); |
| 3088 | BlockDescriptorExtendedType = Rec->getDecl(); |
| 3089 | } |
| 3090 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3091 | bool ASTContext::BlockRequiresCopying(QualType Ty) { |
| 3092 | if (Ty->isBlockPointerType()) |
| 3093 | return true; |
| 3094 | if (isObjCNSObjectType(Ty)) |
| 3095 | return true; |
| 3096 | if (Ty->isObjCObjectPointerType()) |
| 3097 | return true; |
| 3098 | return false; |
| 3099 | } |
| 3100 | |
| 3101 | QualType ASTContext::BuildByRefType(const char *DeclName, QualType Ty) { |
| 3102 | // type = struct __Block_byref_1_X { |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3103 | // void *__isa; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3104 | // struct __Block_byref_1_X *__forwarding; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3105 | // unsigned int __flags; |
| 3106 | // unsigned int __size; |
Mike Stump | 38e1627 | 2009-10-21 22:01:24 +0000 | [diff] [blame] | 3107 | // void *__copy_helper; // as needed |
| 3108 | // void *__destroy_help // as needed |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3109 | // int X; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3110 | // } * |
| 3111 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3112 | bool HasCopyAndDispose = BlockRequiresCopying(Ty); |
| 3113 | |
| 3114 | // FIXME: Move up |
Fariborz Jahanian | 4d0d85c | 2009-10-24 00:16:42 +0000 | [diff] [blame] | 3115 | static unsigned int UniqueBlockByRefTypeID = 0; |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 3116 | llvm::SmallString<36> Name; |
| 3117 | llvm::raw_svector_ostream(Name) << "__Block_byref_" << |
| 3118 | ++UniqueBlockByRefTypeID << '_' << DeclName; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3119 | RecordDecl *T; |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3120 | T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
| 3121 | &Idents.get(Name.str())); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3122 | T->startDefinition(); |
| 3123 | QualType Int32Ty = IntTy; |
| 3124 | assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported"); |
| 3125 | QualType FieldTypes[] = { |
| 3126 | getPointerType(VoidPtrTy), |
| 3127 | getPointerType(getTagDeclType(T)), |
| 3128 | Int32Ty, |
| 3129 | Int32Ty, |
| 3130 | getPointerType(VoidPtrTy), |
| 3131 | getPointerType(VoidPtrTy), |
| 3132 | Ty |
| 3133 | }; |
| 3134 | |
| 3135 | const char *FieldNames[] = { |
| 3136 | "__isa", |
| 3137 | "__forwarding", |
| 3138 | "__flags", |
| 3139 | "__size", |
| 3140 | "__copy_helper", |
| 3141 | "__destroy_helper", |
| 3142 | DeclName, |
| 3143 | }; |
| 3144 | |
| 3145 | for (size_t i = 0; i < 7; ++i) { |
| 3146 | if (!HasCopyAndDispose && i >=4 && i <= 5) |
| 3147 | continue; |
| 3148 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
| 3149 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3150 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3151 | /*BitWidth=*/0, /*Mutable=*/false); |
| 3152 | T->addDecl(Field); |
| 3153 | } |
| 3154 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3155 | T->completeDefinition(); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3156 | |
| 3157 | return getPointerType(getTagDeclType(T)); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3158 | } |
| 3159 | |
| 3160 | |
| 3161 | QualType ASTContext::getBlockParmType( |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3162 | bool BlockHasCopyDispose, |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3163 | llvm::SmallVector<const Expr *, 8> &BlockDeclRefDecls) { |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3164 | // FIXME: Move up |
Fariborz Jahanian | 4d0d85c | 2009-10-24 00:16:42 +0000 | [diff] [blame] | 3165 | static unsigned int UniqueBlockParmTypeID = 0; |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 3166 | llvm::SmallString<36> Name; |
| 3167 | llvm::raw_svector_ostream(Name) << "__block_literal_" |
| 3168 | << ++UniqueBlockParmTypeID; |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3169 | RecordDecl *T; |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3170 | T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
| 3171 | &Idents.get(Name.str())); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3172 | T->startDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3173 | QualType FieldTypes[] = { |
| 3174 | getPointerType(VoidPtrTy), |
| 3175 | IntTy, |
| 3176 | IntTy, |
| 3177 | getPointerType(VoidPtrTy), |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3178 | (BlockHasCopyDispose ? |
| 3179 | getPointerType(getBlockDescriptorExtendedType()) : |
| 3180 | getPointerType(getBlockDescriptorType())) |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3181 | }; |
| 3182 | |
| 3183 | const char *FieldNames[] = { |
| 3184 | "__isa", |
| 3185 | "__flags", |
| 3186 | "__reserved", |
| 3187 | "__FuncPtr", |
| 3188 | "__descriptor" |
| 3189 | }; |
| 3190 | |
| 3191 | for (size_t i = 0; i < 5; ++i) { |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3192 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3193 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3194 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3195 | /*BitWidth=*/0, /*Mutable=*/false); |
| 3196 | T->addDecl(Field); |
| 3197 | } |
| 3198 | |
| 3199 | for (size_t i = 0; i < BlockDeclRefDecls.size(); ++i) { |
| 3200 | const Expr *E = BlockDeclRefDecls[i]; |
| 3201 | const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); |
| 3202 | clang::IdentifierInfo *Name = 0; |
| 3203 | if (BDRE) { |
| 3204 | const ValueDecl *D = BDRE->getDecl(); |
| 3205 | Name = &Idents.get(D->getName()); |
| 3206 | } |
| 3207 | QualType FieldType = E->getType(); |
| 3208 | |
| 3209 | if (BDRE && BDRE->isByRef()) |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3210 | FieldType = BuildByRefType(BDRE->getDecl()->getNameAsCString(), |
| 3211 | FieldType); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3212 | |
| 3213 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3214 | Name, FieldType, /*TInfo=*/0, |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3215 | /*BitWidth=*/0, /*Mutable=*/false); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3216 | T->addDecl(Field); |
| 3217 | } |
| 3218 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3219 | T->completeDefinition(); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3220 | |
| 3221 | return getPointerType(getTagDeclType(T)); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3222 | } |
| 3223 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3224 | void ASTContext::setObjCFastEnumerationStateType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3225 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3226 | assert(Rec && "Invalid ObjCFAstEnumerationStateType"); |
| 3227 | ObjCFastEnumerationStateTypeDecl = Rec->getDecl(); |
| 3228 | } |
| 3229 | |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3230 | // This returns true if a type has been typedefed to BOOL: |
| 3231 | // typedef <type> BOOL; |
Chris Lattner | 2d99833 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 3232 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3233 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | bb49c3e | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 3234 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 3235 | return II->isStr("BOOL"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3236 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3237 | return false; |
| 3238 | } |
| 3239 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3240 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3241 | /// purpose. |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3242 | CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) { |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3243 | CharUnits sz = getTypeSizeInChars(type); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3244 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3245 | // 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] | 3246 | if (sz.isPositive() && type->isIntegralType()) |
| 3247 | sz = std::max(sz, getTypeSizeInChars(IntTy)); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3248 | // Treat arrays as pointers, since that's how they're passed in. |
| 3249 | else if (type->isArrayType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3250 | sz = getTypeSizeInChars(VoidPtrTy); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3251 | return sz; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3252 | } |
| 3253 | |
| 3254 | static inline |
| 3255 | std::string charUnitsToString(const CharUnits &CU) { |
| 3256 | return llvm::itostr(CU.getQuantity()); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3257 | } |
| 3258 | |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3259 | /// getObjCEncodingForBlockDecl - Return the encoded type for this method |
| 3260 | /// declaration. |
| 3261 | void ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr, |
| 3262 | std::string& S) { |
| 3263 | const BlockDecl *Decl = Expr->getBlockDecl(); |
| 3264 | QualType BlockTy = |
| 3265 | Expr->getType()->getAs<BlockPointerType>()->getPointeeType(); |
| 3266 | // Encode result type. |
| 3267 | getObjCEncodingForType(cast<FunctionType>(BlockTy)->getResultType(), S); |
| 3268 | // Compute size of all parameters. |
| 3269 | // Start with computing size of a pointer in number of bytes. |
| 3270 | // FIXME: There might(should) be a better way of doing this computation! |
| 3271 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3272 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
| 3273 | CharUnits ParmOffset = PtrSize; |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3274 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
| 3275 | E = Decl->param_end(); PI != E; ++PI) { |
| 3276 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3277 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3278 | assert (sz.isPositive() && "BlockExpr - Incomplete param type"); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3279 | ParmOffset += sz; |
| 3280 | } |
| 3281 | // Size of the argument frame |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3282 | S += charUnitsToString(ParmOffset); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3283 | // Block pointer and offset. |
| 3284 | S += "@?0"; |
| 3285 | ParmOffset = PtrSize; |
| 3286 | |
| 3287 | // Argument types. |
| 3288 | ParmOffset = PtrSize; |
| 3289 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E = |
| 3290 | Decl->param_end(); PI != E; ++PI) { |
| 3291 | ParmVarDecl *PVDecl = *PI; |
| 3292 | QualType PType = PVDecl->getOriginalType(); |
| 3293 | if (const ArrayType *AT = |
| 3294 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3295 | // Use array's original type only if it has known number of |
| 3296 | // elements. |
| 3297 | if (!isa<ConstantArrayType>(AT)) |
| 3298 | PType = PVDecl->getType(); |
| 3299 | } else if (PType->isFunctionType()) |
| 3300 | PType = PVDecl->getType(); |
| 3301 | getObjCEncodingForType(PType, S); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3302 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3303 | ParmOffset += getObjCEncodingTypeSize(PType); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3304 | } |
| 3305 | } |
| 3306 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3307 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3308 | /// declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3309 | void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Chris Lattner | e6db3b0 | 2008-11-19 07:24:05 +0000 | [diff] [blame] | 3310 | std::string& S) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3311 | // FIXME: This is not very efficient. |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3312 | // Encode type qualifer, 'in', 'inout', etc. for the return type. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3313 | getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3314 | // Encode result type. |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3315 | getObjCEncodingForType(Decl->getResultType(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3316 | // Compute size of all parameters. |
| 3317 | // Start with computing size of a pointer in number of bytes. |
| 3318 | // FIXME: There might(should) be a better way of doing this computation! |
| 3319 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3320 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3321 | // The first two arguments (self and _cmd) are pointers; account for |
| 3322 | // their size. |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3323 | CharUnits ParmOffset = 2 * PtrSize; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3324 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
| 3325 | E = Decl->param_end(); PI != E; ++PI) { |
| 3326 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3327 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3328 | assert (sz.isPositive() && |
| 3329 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3330 | ParmOffset += sz; |
| 3331 | } |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3332 | S += charUnitsToString(ParmOffset); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3333 | S += "@0:"; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3334 | S += charUnitsToString(PtrSize); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3335 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3336 | // Argument types. |
| 3337 | ParmOffset = 2 * PtrSize; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3338 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
| 3339 | E = Decl->param_end(); PI != E; ++PI) { |
| 3340 | ParmVarDecl *PVDecl = *PI; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3341 | QualType PType = PVDecl->getOriginalType(); |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 3342 | if (const ArrayType *AT = |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 3343 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3344 | // Use array's original type only if it has known number of |
| 3345 | // elements. |
Steve Naroff | bb3fde3 | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 3346 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 3347 | PType = PVDecl->getType(); |
| 3348 | } else if (PType->isFunctionType()) |
| 3349 | PType = PVDecl->getType(); |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3350 | // Process argument qualifiers for user supplied arguments; such as, |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3351 | // 'in', 'inout', etc. |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 3352 | getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3353 | getObjCEncodingForType(PType, S); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3354 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3355 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3356 | } |
| 3357 | } |
| 3358 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3359 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 3360 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3361 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 3362 | /// NULL when getting encodings for protocol properties. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3363 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 3364 | /// attributes readonly and bycopy are encoded as single characters. The |
| 3365 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 3366 | /// encoded as single characters, followed by an identifier. Property types |
| 3367 | /// are also encoded as a parametrized attribute. The characters used to encode |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 3368 | /// these attributes are defined by the following enumeration: |
| 3369 | /// @code |
| 3370 | /// enum PropertyAttributes { |
| 3371 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 3372 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 3373 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 3374 | /// kPropertyDynamic = 'D', // property is dynamic |
| 3375 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 3376 | /// kPropertySetter = 'S', // followed by setter selector name |
| 3377 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
| 3378 | /// kPropertyType = 't' // followed by old-style type encoding. |
| 3379 | /// kPropertyWeak = 'W' // 'weak' property |
| 3380 | /// kPropertyStrong = 'P' // property GC'able |
| 3381 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 3382 | /// }; |
| 3383 | /// @endcode |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3384 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3385 | const Decl *Container, |
Chris Lattner | e6db3b0 | 2008-11-19 07:24:05 +0000 | [diff] [blame] | 3386 | std::string& S) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3387 | // Collect information from the property implementation decl(s). |
| 3388 | bool Dynamic = false; |
| 3389 | ObjCPropertyImplDecl *SynthesizePID = 0; |
| 3390 | |
| 3391 | // FIXME: Duplicated code due to poor abstraction. |
| 3392 | if (Container) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3393 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3394 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
| 3395 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3396 | i = CID->propimpl_begin(), e = CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3397 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3398 | ObjCPropertyImplDecl *PID = *i; |
| 3399 | if (PID->getPropertyDecl() == PD) { |
| 3400 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 3401 | Dynamic = true; |
| 3402 | } else { |
| 3403 | SynthesizePID = PID; |
| 3404 | } |
| 3405 | } |
| 3406 | } |
| 3407 | } else { |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3408 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3409 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3410 | i = OID->propimpl_begin(), e = OID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3411 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3412 | ObjCPropertyImplDecl *PID = *i; |
| 3413 | if (PID->getPropertyDecl() == PD) { |
| 3414 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 3415 | Dynamic = true; |
| 3416 | } else { |
| 3417 | SynthesizePID = PID; |
| 3418 | } |
| 3419 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3420 | } |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3421 | } |
| 3422 | } |
| 3423 | |
| 3424 | // FIXME: This is not very efficient. |
| 3425 | S = "T"; |
| 3426 | |
| 3427 | // Encode result type. |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3428 | // GCC has some special rules regarding encoding of properties which |
| 3429 | // closely resembles encoding of ivars. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3430 | getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3431 | true /* outermost type */, |
| 3432 | true /* encoding for property */); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3433 | |
| 3434 | if (PD->isReadOnly()) { |
| 3435 | S += ",R"; |
| 3436 | } else { |
| 3437 | switch (PD->getSetterKind()) { |
| 3438 | case ObjCPropertyDecl::Assign: break; |
| 3439 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3440 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3441 | } |
| 3442 | } |
| 3443 | |
| 3444 | // It really isn't clear at all what this means, since properties |
| 3445 | // are "dynamic by default". |
| 3446 | if (Dynamic) |
| 3447 | S += ",D"; |
| 3448 | |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3449 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 3450 | S += ",N"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3451 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3452 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 3453 | S += ",G"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3454 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3455 | } |
| 3456 | |
| 3457 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 3458 | S += ",S"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3459 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3460 | } |
| 3461 | |
| 3462 | if (SynthesizePID) { |
| 3463 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 3464 | S += ",V"; |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 3465 | S += OID->getNameAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3466 | } |
| 3467 | |
| 3468 | // FIXME: OBJCGC: weak & strong |
| 3469 | } |
| 3470 | |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3471 | /// getLegacyIntegralTypeEncoding - |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3472 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
| 3473 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3474 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
| 3475 | /// |
| 3476 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3477 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3478 | if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) { |
Fariborz Jahanian | c657eba | 2009-02-11 23:59:18 +0000 | [diff] [blame] | 3479 | if (BT->getKind() == BuiltinType::ULong && |
| 3480 | ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3481 | PointeeTy = UnsignedIntTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3482 | else |
Fariborz Jahanian | c657eba | 2009-02-11 23:59:18 +0000 | [diff] [blame] | 3483 | if (BT->getKind() == BuiltinType::Long && |
| 3484 | ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3485 | PointeeTy = IntTy; |
| 3486 | } |
| 3487 | } |
| 3488 | } |
| 3489 | |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3490 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 3491 | const FieldDecl *Field) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3492 | // We follow the behavior of gcc, expanding structures which are |
| 3493 | // directly pointed to, and expanding embedded structures. Note that |
| 3494 | // these rules are sufficient to prevent recursive encoding of the |
| 3495 | // same type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3496 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
Fariborz Jahanian | 5b8c7d9 | 2008-12-22 23:22:27 +0000 | [diff] [blame] | 3497 | true /* outermost type */); |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3498 | } |
| 3499 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3500 | static void EncodeBitField(const ASTContext *Context, std::string& S, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 3501 | const FieldDecl *FD) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3502 | const Expr *E = FD->getBitWidth(); |
| 3503 | assert(E && "bitfield width not there - getObjCEncodingForTypeImpl"); |
| 3504 | ASTContext *Ctx = const_cast<ASTContext*>(Context); |
Eli Friedman | 9a901bb | 2009-04-26 19:19:15 +0000 | [diff] [blame] | 3505 | unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue(); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3506 | S += 'b'; |
| 3507 | S += llvm::utostr(N); |
| 3508 | } |
| 3509 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 3510 | // FIXME: Use SmallString for accumulating string. |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3511 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 3512 | bool ExpandPointedToStructures, |
| 3513 | bool ExpandStructures, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 3514 | const FieldDecl *FD, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3515 | bool OutermostType, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3516 | bool EncodingProperty) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3517 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3518 | if (FD && FD->isBitField()) |
| 3519 | return EncodeBitField(this, S, FD); |
| 3520 | char encoding; |
| 3521 | switch (BT->getKind()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3522 | default: assert(0 && "Unhandled builtin type kind"); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3523 | case BuiltinType::Void: encoding = 'v'; break; |
| 3524 | case BuiltinType::Bool: encoding = 'B'; break; |
| 3525 | case BuiltinType::Char_U: |
| 3526 | case BuiltinType::UChar: encoding = 'C'; break; |
| 3527 | case BuiltinType::UShort: encoding = 'S'; break; |
| 3528 | case BuiltinType::UInt: encoding = 'I'; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3529 | case BuiltinType::ULong: |
| 3530 | encoding = |
| 3531 | (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q'; |
Fariborz Jahanian | 72696e1 | 2009-02-11 22:31:45 +0000 | [diff] [blame] | 3532 | break; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3533 | case BuiltinType::UInt128: encoding = 'T'; break; |
| 3534 | case BuiltinType::ULongLong: encoding = 'Q'; break; |
| 3535 | case BuiltinType::Char_S: |
| 3536 | case BuiltinType::SChar: encoding = 'c'; break; |
| 3537 | case BuiltinType::Short: encoding = 's'; break; |
| 3538 | case BuiltinType::Int: encoding = 'i'; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3539 | case BuiltinType::Long: |
| 3540 | encoding = |
| 3541 | (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3542 | break; |
| 3543 | case BuiltinType::LongLong: encoding = 'q'; break; |
| 3544 | case BuiltinType::Int128: encoding = 't'; break; |
| 3545 | case BuiltinType::Float: encoding = 'f'; break; |
| 3546 | case BuiltinType::Double: encoding = 'd'; break; |
| 3547 | case BuiltinType::LongDouble: encoding = 'd'; break; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3548 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3549 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3550 | S += encoding; |
| 3551 | return; |
| 3552 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3553 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3554 | if (const ComplexType *CT = T->getAs<ComplexType>()) { |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 3555 | S += 'j'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3556 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false, |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 3557 | false); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3558 | return; |
| 3559 | } |
Fariborz Jahanian | 60bce3e | 2009-11-23 20:40:50 +0000 | [diff] [blame] | 3560 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3561 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Fariborz Jahanian | 8d2c0a9 | 2009-11-30 18:43:52 +0000 | [diff] [blame] | 3562 | if (PT->isObjCSelType()) { |
| 3563 | S += ':'; |
| 3564 | return; |
| 3565 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3566 | QualType PointeeTy = PT->getPointeeType(); |
Fariborz Jahanian | 8d2c0a9 | 2009-11-30 18:43:52 +0000 | [diff] [blame] | 3567 | |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3568 | bool isReadOnly = false; |
| 3569 | // For historical/compatibility reasons, the read-only qualifier of the |
| 3570 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 3571 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3572 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3573 | if (isa<TypedefType>(T.getTypePtr())) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3574 | if (OutermostType && T.isConstQualified()) { |
| 3575 | isReadOnly = true; |
| 3576 | S += 'r'; |
| 3577 | } |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 3578 | } else if (OutermostType) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3579 | QualType P = PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3580 | while (P->getAs<PointerType>()) |
| 3581 | P = P->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3582 | if (P.isConstQualified()) { |
| 3583 | isReadOnly = true; |
| 3584 | S += 'r'; |
| 3585 | } |
| 3586 | } |
| 3587 | if (isReadOnly) { |
| 3588 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 3589 | // combinations need to be rearranged. |
| 3590 | // Rewrite "in const" from "nr" to "rn" |
| 3591 | const char * s = S.c_str(); |
| 3592 | int len = S.length(); |
| 3593 | if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') { |
| 3594 | std::string replace = "rn"; |
| 3595 | S.replace(S.end()-2, S.end(), replace); |
| 3596 | } |
| 3597 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3598 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3599 | if (PointeeTy->isCharType()) { |
| 3600 | // char pointer types should be encoded as '*' unless it is a |
| 3601 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3602 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3603 | S += '*'; |
| 3604 | return; |
| 3605 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3606 | } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) { |
Steve Naroff | 9533a7f | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 3607 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 3608 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 3609 | S += '#'; |
| 3610 | return; |
| 3611 | } |
| 3612 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 3613 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 3614 | S += '@'; |
| 3615 | return; |
| 3616 | } |
| 3617 | // fall through... |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3618 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3619 | S += '^'; |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3620 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 3621 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3622 | getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3623 | NULL); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3624 | return; |
| 3625 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3626 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3627 | if (const ArrayType *AT = |
| 3628 | // Ignore type qualifiers etc. |
| 3629 | dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3630 | if (isa<IncompleteArrayType>(AT)) { |
| 3631 | // Incomplete arrays are encoded as a pointer to the array element. |
| 3632 | S += '^'; |
| 3633 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3634 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3635 | false, ExpandStructures, FD); |
| 3636 | } else { |
| 3637 | S += '['; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3638 | |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3639 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 3640 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 3641 | else { |
| 3642 | //Variable length arrays are encoded as a regular array with 0 elements. |
| 3643 | assert(isa<VariableArrayType>(AT) && "Unknown array type!"); |
| 3644 | S += '0'; |
| 3645 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3646 | |
| 3647 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3648 | false, ExpandStructures, FD); |
| 3649 | S += ']'; |
| 3650 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3651 | return; |
| 3652 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3653 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3654 | if (T->getAs<FunctionType>()) { |
Anders Carlsson | c0a87b7 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 3655 | S += '?'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3656 | return; |
| 3657 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3658 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3659 | if (const RecordType *RTy = T->getAs<RecordType>()) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3660 | RecordDecl *RDecl = RTy->getDecl(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3661 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 3662 | // Anonymous structures print as '?' |
| 3663 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 3664 | S += II->getName(); |
| 3665 | } else { |
| 3666 | S += '?'; |
| 3667 | } |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3668 | if (ExpandStructures) { |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3669 | S += '='; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3670 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 3671 | FieldEnd = RDecl->field_end(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3672 | Field != FieldEnd; ++Field) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3673 | if (FD) { |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3674 | S += '"'; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3675 | S += Field->getNameAsString(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3676 | S += '"'; |
| 3677 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3678 | |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3679 | // Special case bit-fields. |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3680 | if (Field->isBitField()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3681 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3682 | (*Field)); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3683 | } else { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3684 | QualType qt = Field->getType(); |
| 3685 | getLegacyIntegralTypeEncoding(qt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3686 | getObjCEncodingForTypeImpl(qt, S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3687 | FD); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3688 | } |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3689 | } |
Fariborz Jahanian | 6de88a8 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 3690 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3691 | S += RDecl->isUnion() ? ')' : '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3692 | return; |
| 3693 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3694 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3695 | if (T->isEnumeralType()) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3696 | if (FD && FD->isBitField()) |
| 3697 | EncodeBitField(this, S, FD); |
| 3698 | else |
| 3699 | S += 'i'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3700 | return; |
| 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 | if (T->isBlockPointerType()) { |
Steve Naroff | 21a98b1 | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 3704 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3705 | return; |
| 3706 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3707 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3708 | if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3709 | // @encode(class_name) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3710 | ObjCInterfaceDecl *OI = OIT->getDecl(); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3711 | S += '{'; |
| 3712 | const IdentifierInfo *II = OI->getIdentifier(); |
| 3713 | S += II->getName(); |
| 3714 | S += '='; |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3715 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3716 | CollectObjCIvars(OI, RecFields); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3717 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3718 | if (RecFields[i]->isBitField()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3719 | getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3720 | RecFields[i]); |
| 3721 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3722 | getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3723 | FD); |
| 3724 | } |
| 3725 | S += '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3726 | return; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3727 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3728 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3729 | if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3730 | if (OPT->isObjCIdType()) { |
| 3731 | S += '@'; |
| 3732 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3733 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3734 | |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 3735 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
| 3736 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
| 3737 | // Since this is a binary compatibility issue, need to consult with runtime |
| 3738 | // folks. Fortunately, this is a *very* obsure construct. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3739 | S += '#'; |
| 3740 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3741 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3742 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3743 | if (OPT->isObjCQualifiedIdType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3744 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3745 | ExpandPointedToStructures, |
| 3746 | ExpandStructures, FD); |
| 3747 | if (FD || EncodingProperty) { |
| 3748 | // Note that we do extended encoding of protocol qualifer list |
| 3749 | // Only when doing ivar or property encoding. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3750 | S += '"'; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3751 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 3752 | E = OPT->qual_end(); I != E; ++I) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3753 | S += '<'; |
| 3754 | S += (*I)->getNameAsString(); |
| 3755 | S += '>'; |
| 3756 | } |
| 3757 | S += '"'; |
| 3758 | } |
| 3759 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3760 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3761 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3762 | QualType PointeeTy = OPT->getPointeeType(); |
| 3763 | if (!EncodingProperty && |
| 3764 | isa<TypedefType>(PointeeTy.getTypePtr())) { |
| 3765 | // Another historical/compatibility reason. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3766 | // We encode the underlying type which comes out as |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3767 | // {...}; |
| 3768 | S += '^'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3769 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 3770 | false, ExpandPointedToStructures, |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3771 | NULL); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3772 | return; |
| 3773 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3774 | |
| 3775 | S += '@'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 3776 | if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3777 | S += '"'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 3778 | S += OPT->getInterfaceDecl()->getIdentifier()->getName(); |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3779 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 3780 | E = OPT->qual_end(); I != E; ++I) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3781 | S += '<'; |
| 3782 | S += (*I)->getNameAsString(); |
| 3783 | S += '>'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3784 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3785 | S += '"'; |
| 3786 | } |
| 3787 | return; |
| 3788 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3789 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3790 | assert(0 && "@encode for type not implemented!"); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3791 | } |
| 3792 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3793 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3794 | std::string& S) const { |
| 3795 | if (QT & Decl::OBJC_TQ_In) |
| 3796 | S += 'n'; |
| 3797 | if (QT & Decl::OBJC_TQ_Inout) |
| 3798 | S += 'N'; |
| 3799 | if (QT & Decl::OBJC_TQ_Out) |
| 3800 | S += 'o'; |
| 3801 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 3802 | S += 'O'; |
| 3803 | if (QT & Decl::OBJC_TQ_Byref) |
| 3804 | S += 'R'; |
| 3805 | if (QT & Decl::OBJC_TQ_Oneway) |
| 3806 | S += 'V'; |
| 3807 | } |
| 3808 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3809 | void ASTContext::setBuiltinVaListType(QualType T) { |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 3810 | assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3811 | |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 3812 | BuiltinVaListType = T; |
| 3813 | } |
| 3814 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3815 | void ASTContext::setObjCIdType(QualType T) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3816 | ObjCIdTypedefType = T; |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 3817 | } |
| 3818 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3819 | void ASTContext::setObjCSelType(QualType T) { |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 3820 | ObjCSelTypedefType = T; |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 3821 | } |
| 3822 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3823 | void ASTContext::setObjCProtoType(QualType QT) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3824 | ObjCProtoType = QT; |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 3825 | } |
| 3826 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3827 | void ASTContext::setObjCClassType(QualType T) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3828 | ObjCClassTypedefType = T; |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 3829 | } |
| 3830 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3831 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3832 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 3833 | "'NSConstantString' type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3834 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3835 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 3836 | } |
| 3837 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 3838 | /// \brief Retrieve the template name that corresponds to a non-empty |
| 3839 | /// lookup. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3840 | TemplateName ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin, |
| 3841 | UnresolvedSetIterator End) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 3842 | unsigned size = End - Begin; |
| 3843 | assert(size > 1 && "set is not overloaded!"); |
| 3844 | |
| 3845 | void *memory = Allocate(sizeof(OverloadedTemplateStorage) + |
| 3846 | size * sizeof(FunctionTemplateDecl*)); |
| 3847 | OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size); |
| 3848 | |
| 3849 | NamedDecl **Storage = OT->getStorage(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3850 | for (UnresolvedSetIterator I = Begin; I != End; ++I) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 3851 | NamedDecl *D = *I; |
| 3852 | assert(isa<FunctionTemplateDecl>(D) || |
| 3853 | (isa<UsingShadowDecl>(D) && |
| 3854 | isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))); |
| 3855 | *Storage++ = D; |
| 3856 | } |
| 3857 | |
| 3858 | return TemplateName(OT); |
| 3859 | } |
| 3860 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3861 | /// \brief Retrieve the template name that represents a qualified |
| 3862 | /// template name such as \c std::vector. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3863 | TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3864 | bool TemplateKeyword, |
| 3865 | TemplateDecl *Template) { |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3866 | // FIXME: Canonicalization? |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3867 | llvm::FoldingSetNodeID ID; |
| 3868 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 3869 | |
| 3870 | void *InsertPos = 0; |
| 3871 | QualifiedTemplateName *QTN = |
| 3872 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 3873 | if (!QTN) { |
| 3874 | QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); |
| 3875 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 3876 | } |
| 3877 | |
| 3878 | return TemplateName(QTN); |
| 3879 | } |
| 3880 | |
| 3881 | /// \brief Retrieve the template name that represents a dependent |
| 3882 | /// template name such as \c MetaFun::template apply. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3883 | TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3884 | const IdentifierInfo *Name) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3885 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 3886 | "Nested name specifier must be dependent"); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3887 | |
| 3888 | llvm::FoldingSetNodeID ID; |
| 3889 | DependentTemplateName::Profile(ID, NNS, Name); |
| 3890 | |
| 3891 | void *InsertPos = 0; |
| 3892 | DependentTemplateName *QTN = |
| 3893 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 3894 | |
| 3895 | if (QTN) |
| 3896 | return TemplateName(QTN); |
| 3897 | |
| 3898 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 3899 | if (CanonNNS == NNS) { |
| 3900 | QTN = new (*this,4) DependentTemplateName(NNS, Name); |
| 3901 | } else { |
| 3902 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
| 3903 | QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3904 | DependentTemplateName *CheckQTN = |
| 3905 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 3906 | assert(!CheckQTN && "Dependent type name canonicalization broken"); |
| 3907 | (void)CheckQTN; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3908 | } |
| 3909 | |
| 3910 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 3911 | return TemplateName(QTN); |
| 3912 | } |
| 3913 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 3914 | /// \brief Retrieve the template name that represents a dependent |
| 3915 | /// template name such as \c MetaFun::template operator+. |
| 3916 | TemplateName |
| 3917 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
| 3918 | OverloadedOperatorKind Operator) { |
| 3919 | assert((!NNS || NNS->isDependent()) && |
| 3920 | "Nested name specifier must be dependent"); |
| 3921 | |
| 3922 | llvm::FoldingSetNodeID ID; |
| 3923 | DependentTemplateName::Profile(ID, NNS, Operator); |
| 3924 | |
| 3925 | void *InsertPos = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3926 | DependentTemplateName *QTN |
| 3927 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 3928 | |
| 3929 | if (QTN) |
| 3930 | return TemplateName(QTN); |
| 3931 | |
| 3932 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 3933 | if (CanonNNS == NNS) { |
| 3934 | QTN = new (*this,4) DependentTemplateName(NNS, Operator); |
| 3935 | } else { |
| 3936 | TemplateName Canon = getDependentTemplateName(CanonNNS, Operator); |
| 3937 | QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3938 | |
| 3939 | DependentTemplateName *CheckQTN |
| 3940 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 3941 | assert(!CheckQTN && "Dependent template name canonicalization broken"); |
| 3942 | (void)CheckQTN; |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 3943 | } |
| 3944 | |
| 3945 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 3946 | return TemplateName(QTN); |
| 3947 | } |
| 3948 | |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 3949 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | d934112 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 3950 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 3951 | /// is actually a value of type @c TargetInfo::IntType. |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 3952 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 3953 | switch (Type) { |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 3954 | case TargetInfo::NoInt: return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 3955 | case TargetInfo::SignedShort: return ShortTy; |
| 3956 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 3957 | case TargetInfo::SignedInt: return IntTy; |
| 3958 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 3959 | case TargetInfo::SignedLong: return LongTy; |
| 3960 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 3961 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 3962 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 3963 | } |
| 3964 | |
| 3965 | assert(false && "Unhandled TargetInfo::IntType value"); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 3966 | return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 3967 | } |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 3968 | |
| 3969 | //===----------------------------------------------------------------------===// |
| 3970 | // Type Predicates. |
| 3971 | //===----------------------------------------------------------------------===// |
| 3972 | |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3973 | /// isObjCNSObjectType - Return true if this is an NSObject object using |
| 3974 | /// NSObject attribute on a c-style pointer type. |
| 3975 | /// FIXME - Make it work directly on types. |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 3976 | /// FIXME: Move to Type. |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3977 | /// |
| 3978 | bool ASTContext::isObjCNSObjectType(QualType Ty) const { |
| 3979 | if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) { |
| 3980 | if (TypedefDecl *TD = TDT->getDecl()) |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 3981 | if (TD->getAttr<ObjCNSObjectAttr>()) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3982 | return true; |
| 3983 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3984 | return false; |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3985 | } |
| 3986 | |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3987 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 3988 | /// garbage collection attribute. |
| 3989 | /// |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3990 | Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const { |
| 3991 | Qualifiers::GC GCAttrs = Qualifiers::GCNone; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3992 | if (getLangOptions().ObjC1 && |
| 3993 | getLangOptions().getGCMode() != LangOptions::NonGC) { |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 3994 | GCAttrs = Ty.getObjCGCAttr(); |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3995 | // Default behavious under objective-c's gc is for objective-c pointers |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3996 | // (or pointers to them) be treated as though they were declared |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 3997 | // as __strong. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3998 | if (GCAttrs == Qualifiers::GCNone) { |
Fariborz Jahanian | 75212ee | 2009-09-10 23:38:45 +0000 | [diff] [blame] | 3999 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4000 | GCAttrs = Qualifiers::Strong; |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 4001 | else if (Ty->isPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4002 | return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 4003 | } |
Fariborz Jahanian | c211218 | 2009-04-11 00:00:54 +0000 | [diff] [blame] | 4004 | // Non-pointers have none gc'able attribute regardless of the attribute |
| 4005 | // set on them. |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 4006 | else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4007 | return Qualifiers::GCNone; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4008 | } |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 4009 | return GCAttrs; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 4010 | } |
| 4011 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4012 | //===----------------------------------------------------------------------===// |
| 4013 | // Type Compatibility Testing |
| 4014 | //===----------------------------------------------------------------------===// |
Chris Lattner | 770951b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 4015 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4016 | /// areCompatVectorTypes - Return true if the two specified vector types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4017 | /// compatible. |
| 4018 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 4019 | const VectorType *RHS) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 4020 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4021 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4022 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4023 | } |
| 4024 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4025 | //===----------------------------------------------------------------------===// |
| 4026 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 4027 | //===----------------------------------------------------------------------===// |
| 4028 | |
| 4029 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 4030 | /// inheritance hierarchy of 'rProto'. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 4031 | bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 4032 | ObjCProtocolDecl *rProto) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4033 | if (lProto == rProto) |
| 4034 | return true; |
| 4035 | for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), |
| 4036 | E = rProto->protocol_end(); PI != E; ++PI) |
| 4037 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 4038 | return true; |
| 4039 | return false; |
| 4040 | } |
| 4041 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4042 | /// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...> |
| 4043 | /// return true if lhs's protocols conform to rhs's protocol; false |
| 4044 | /// otherwise. |
| 4045 | bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) { |
| 4046 | if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType()) |
| 4047 | return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false); |
| 4048 | return false; |
| 4049 | } |
| 4050 | |
| 4051 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 4052 | /// ObjCQualifiedIDType. |
| 4053 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 4054 | bool compare) { |
| 4055 | // Allow id<P..> and an 'id' or void* type in all cases. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4056 | if (lhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4057 | lhs->isObjCIdType() || lhs->isObjCClassType()) |
| 4058 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4059 | else if (rhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4060 | rhs->isObjCIdType() || rhs->isObjCClassType()) |
| 4061 | return true; |
| 4062 | |
| 4063 | if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4064 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4065 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4066 | if (!rhsOPT) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4067 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4068 | if (rhsOPT->qual_empty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4069 | // If the RHS is a unqualified interface pointer "NSString*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4070 | // make sure we check the class hierarchy. |
| 4071 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 4072 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4073 | E = lhsQID->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 (!rhsID->ClassImplementsProtocol(*I, true)) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4078 | return false; |
| 4079 | } |
| 4080 | } |
| 4081 | // If there are no qualifiers and no interface, we have an 'id'. |
| 4082 | return true; |
| 4083 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4084 | // Both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4085 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4086 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4087 | ObjCProtocolDecl *lhsProto = *I; |
| 4088 | bool match = false; |
| 4089 | |
| 4090 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4091 | // see if static class implements all of id's protocols, directly or |
| 4092 | // through its super class and categories. |
| 4093 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 4094 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 4095 | ObjCProtocolDecl *rhsProto = *J; |
| 4096 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 4097 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 4098 | match = true; |
| 4099 | break; |
| 4100 | } |
| 4101 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4102 | // If the RHS is a qualified interface pointer "NSString<P>*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4103 | // make sure we check the class hierarchy. |
| 4104 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 4105 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4106 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4107 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4108 | // see if static class implements all of id's protocols, directly or |
| 4109 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 4110 | if (rhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4111 | match = true; |
| 4112 | break; |
| 4113 | } |
| 4114 | } |
| 4115 | } |
| 4116 | if (!match) |
| 4117 | return false; |
| 4118 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4119 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4120 | return true; |
| 4121 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4122 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4123 | const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 4124 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 4125 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4126 | if (const ObjCObjectPointerType *lhsOPT = |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4127 | lhs->getAsObjCInterfacePointerType()) { |
| 4128 | if (lhsOPT->qual_empty()) { |
| 4129 | bool match = false; |
| 4130 | if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { |
| 4131 | for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(), |
| 4132 | E = rhsQID->qual_end(); I != E; ++I) { |
| 4133 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4134 | // see if static class implements all of id's protocols, directly or |
| 4135 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 4136 | if (lhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4137 | match = true; |
| 4138 | break; |
| 4139 | } |
| 4140 | } |
| 4141 | if (!match) |
| 4142 | return false; |
| 4143 | } |
| 4144 | return true; |
| 4145 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4146 | // Both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4147 | for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(), |
| 4148 | E = lhsOPT->qual_end(); I != E; ++I) { |
| 4149 | ObjCProtocolDecl *lhsProto = *I; |
| 4150 | bool match = false; |
| 4151 | |
| 4152 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4153 | // see if static class implements all of id's protocols, directly or |
| 4154 | // through its super class and categories. |
| 4155 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 4156 | E = rhsQID->qual_end(); J != E; ++J) { |
| 4157 | ObjCProtocolDecl *rhsProto = *J; |
| 4158 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 4159 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 4160 | match = true; |
| 4161 | break; |
| 4162 | } |
| 4163 | } |
| 4164 | if (!match) |
| 4165 | return false; |
| 4166 | } |
| 4167 | return true; |
| 4168 | } |
| 4169 | return false; |
| 4170 | } |
| 4171 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4172 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4173 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 4174 | /// protocol qualifiers on the LHS or RHS. |
| 4175 | /// |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4176 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 4177 | const ObjCObjectPointerType *RHSOPT) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 4178 | // If either type represents the built-in 'id' or 'Class' types, return true. |
| 4179 | if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4180 | return true; |
| 4181 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4182 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4183 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 4184 | QualType(RHSOPT,0), |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4185 | false); |
| 4186 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4187 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 4188 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4189 | if (LHS && RHS) // We have 2 user-defined types. |
| 4190 | return canAssignObjCInterfaces(LHS, RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4191 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4192 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4193 | } |
| 4194 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4195 | /// getIntersectionOfProtocols - This routine finds the intersection of set |
| 4196 | /// of protocols inherited from two distinct objective-c pointer objects. |
| 4197 | /// It is used to build composite qualifier list of the composite type of |
| 4198 | /// the conditional expression involving two objective-c pointer objects. |
| 4199 | static |
| 4200 | void getIntersectionOfProtocols(ASTContext &Context, |
| 4201 | const ObjCObjectPointerType *LHSOPT, |
| 4202 | const ObjCObjectPointerType *RHSOPT, |
| 4203 | llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) { |
| 4204 | |
| 4205 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 4206 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 4207 | |
| 4208 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet; |
| 4209 | unsigned LHSNumProtocols = LHS->getNumProtocols(); |
| 4210 | if (LHSNumProtocols > 0) |
| 4211 | InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end()); |
| 4212 | else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 4213 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
| 4214 | Context.CollectInheritedProtocols(LHS->getDecl(), LHSInheritedProtocols); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4215 | InheritedProtocolSet.insert(LHSInheritedProtocols.begin(), |
| 4216 | LHSInheritedProtocols.end()); |
| 4217 | } |
| 4218 | |
| 4219 | unsigned RHSNumProtocols = RHS->getNumProtocols(); |
| 4220 | if (RHSNumProtocols > 0) { |
| 4221 | ObjCProtocolDecl **RHSProtocols = (ObjCProtocolDecl **)RHS->qual_begin(); |
| 4222 | for (unsigned i = 0; i < RHSNumProtocols; ++i) |
| 4223 | if (InheritedProtocolSet.count(RHSProtocols[i])) |
| 4224 | IntersectionOfProtocols.push_back(RHSProtocols[i]); |
| 4225 | } |
| 4226 | else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 4227 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols; |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4228 | Context.CollectInheritedProtocols(RHS->getDecl(), RHSInheritedProtocols); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 4229 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 4230 | RHSInheritedProtocols.begin(), |
| 4231 | E = RHSInheritedProtocols.end(); I != E; ++I) |
| 4232 | if (InheritedProtocolSet.count((*I))) |
| 4233 | IntersectionOfProtocols.push_back((*I)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4234 | } |
| 4235 | } |
| 4236 | |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4237 | /// areCommonBaseCompatible - Returns common base class of the two classes if |
| 4238 | /// one found. Note that this is O'2 algorithm. But it will be called as the |
| 4239 | /// last type comparison in a ?-exp of ObjC pointer types before a |
| 4240 | /// warning is issued. So, its invokation is extremely rare. |
| 4241 | QualType ASTContext::areCommonBaseCompatible( |
| 4242 | const ObjCObjectPointerType *LHSOPT, |
| 4243 | const ObjCObjectPointerType *RHSOPT) { |
| 4244 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 4245 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 4246 | if (!LHS || !RHS) |
| 4247 | return QualType(); |
| 4248 | |
| 4249 | while (const ObjCInterfaceDecl *LHSIDecl = LHS->getDecl()->getSuperClass()) { |
| 4250 | QualType LHSTy = getObjCInterfaceType(LHSIDecl); |
| 4251 | LHS = LHSTy->getAs<ObjCInterfaceType>(); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4252 | if (canAssignObjCInterfaces(LHS, RHS)) { |
| 4253 | llvm::SmallVector<ObjCProtocolDecl *, 8> IntersectionOfProtocols; |
| 4254 | getIntersectionOfProtocols(*this, |
| 4255 | LHSOPT, RHSOPT, IntersectionOfProtocols); |
| 4256 | if (IntersectionOfProtocols.empty()) |
| 4257 | LHSTy = getObjCObjectPointerType(LHSTy); |
| 4258 | else |
| 4259 | LHSTy = getObjCObjectPointerType(LHSTy, &IntersectionOfProtocols[0], |
| 4260 | IntersectionOfProtocols.size()); |
| 4261 | return LHSTy; |
| 4262 | } |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4263 | } |
| 4264 | |
| 4265 | return QualType(); |
| 4266 | } |
| 4267 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4268 | bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS, |
| 4269 | const ObjCInterfaceType *RHS) { |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4270 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 4271 | // the LHS. |
| 4272 | if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
| 4273 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4274 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4275 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 4276 | // protocol qualified at all, then we are good. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4277 | if (LHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4278 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4279 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4280 | // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it |
| 4281 | // isn't a superset. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4282 | if (RHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4283 | return true; // FIXME: should return false! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4284 | |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4285 | for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(), |
| 4286 | LHSPE = LHS->qual_end(); |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4287 | LHSPI != LHSPE; LHSPI++) { |
| 4288 | bool RHSImplementsProtocol = false; |
| 4289 | |
| 4290 | // If the RHS doesn't implement the protocol on the left, the types |
| 4291 | // are incompatible. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4292 | for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(), |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4293 | RHSPE = RHS->qual_end(); |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 4294 | RHSPI != RHSPE; RHSPI++) { |
| 4295 | if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) { |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4296 | RHSImplementsProtocol = true; |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 4297 | break; |
| 4298 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4299 | } |
| 4300 | // FIXME: For better diagnostics, consider passing back the protocol name. |
| 4301 | if (!RHSImplementsProtocol) |
| 4302 | return false; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4303 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4304 | // The RHS implements all protocols listed on the LHS. |
| 4305 | return true; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4306 | } |
| 4307 | |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4308 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 4309 | // get the "pointed to" types |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4310 | const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
| 4311 | const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4312 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4313 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4314 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4315 | |
| 4316 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 4317 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4318 | } |
| 4319 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4320 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4321 | /// both shall have the identically qualified version of a compatible type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4322 | /// 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] | 4323 | /// same. See 6.7.[2,3,5] for additional rules. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4324 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) { |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 4325 | if (getLangOptions().CPlusPlus) |
| 4326 | return hasSameType(LHS, RHS); |
| 4327 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4328 | return !mergeTypes(LHS, RHS).isNull(); |
| 4329 | } |
| 4330 | |
| 4331 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4332 | const FunctionType *lbase = lhs->getAs<FunctionType>(); |
| 4333 | const FunctionType *rbase = rhs->getAs<FunctionType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4334 | const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 4335 | const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4336 | bool allLTypes = true; |
| 4337 | bool allRTypes = true; |
| 4338 | |
| 4339 | // Check return type |
Fariborz Jahanian | 6de8b62 | 2010-02-10 00:32:12 +0000 | [diff] [blame] | 4340 | QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType()); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4341 | if (retType.isNull()) return QualType(); |
Fariborz Jahanian | 6de8b62 | 2010-02-10 00:32:12 +0000 | [diff] [blame] | 4342 | if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType())) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4343 | allLTypes = false; |
Fariborz Jahanian | 6de8b62 | 2010-02-10 00:32:12 +0000 | [diff] [blame] | 4344 | if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType())) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4345 | allRTypes = false; |
Mike Stump | 6dcbc29 | 2009-07-25 23:24:03 +0000 | [diff] [blame] | 4346 | // FIXME: double check this |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 4347 | bool NoReturn = lbase->getNoReturnAttr() || rbase->getNoReturnAttr(); |
| 4348 | if (NoReturn != lbase->getNoReturnAttr()) |
| 4349 | allLTypes = false; |
| 4350 | if (NoReturn != rbase->getNoReturnAttr()) |
| 4351 | allRTypes = false; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 4352 | CallingConv lcc = lbase->getCallConv(); |
| 4353 | CallingConv rcc = rbase->getCallConv(); |
| 4354 | // Compatible functions must have compatible calling conventions |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4355 | if (!isSameCallConv(lcc, rcc)) |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 4356 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4357 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4358 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 4359 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 4360 | "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4361 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 4362 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 4363 | |
| 4364 | // Compatible functions must have the same number of arguments |
| 4365 | if (lproto_nargs != rproto_nargs) |
| 4366 | return QualType(); |
| 4367 | |
| 4368 | // Variadic and non-variadic functions aren't compatible |
| 4369 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 4370 | return QualType(); |
| 4371 | |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 4372 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 4373 | return QualType(); |
| 4374 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4375 | // Check argument compatibility |
| 4376 | llvm::SmallVector<QualType, 10> types; |
| 4377 | for (unsigned i = 0; i < lproto_nargs; i++) { |
| 4378 | QualType largtype = lproto->getArgType(i).getUnqualifiedType(); |
| 4379 | QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); |
| 4380 | QualType argtype = mergeTypes(largtype, rargtype); |
| 4381 | if (argtype.isNull()) return QualType(); |
| 4382 | types.push_back(argtype); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4383 | if (getCanonicalType(argtype) != getCanonicalType(largtype)) |
| 4384 | allLTypes = false; |
| 4385 | if (getCanonicalType(argtype) != getCanonicalType(rargtype)) |
| 4386 | allRTypes = false; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4387 | } |
| 4388 | if (allLTypes) return lhs; |
| 4389 | if (allRTypes) return rhs; |
| 4390 | return getFunctionType(retType, types.begin(), types.size(), |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 4391 | lproto->isVariadic(), lproto->getTypeQuals(), |
Douglas Gregor | 1bf4024 | 2010-02-12 17:17:28 +0000 | [diff] [blame] | 4392 | false, false, 0, 0, NoReturn, lcc); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4393 | } |
| 4394 | |
| 4395 | if (lproto) allRTypes = false; |
| 4396 | if (rproto) allLTypes = false; |
| 4397 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4398 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4399 | if (proto) { |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 4400 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4401 | if (proto->isVariadic()) return QualType(); |
| 4402 | // Check that the types are compatible with the types that |
| 4403 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 4404 | // The only types actually affected are promotable integer |
| 4405 | // types and floats, which would be passed as a different |
| 4406 | // type depending on whether the prototype is visible. |
| 4407 | unsigned proto_nargs = proto->getNumArgs(); |
| 4408 | for (unsigned i = 0; i < proto_nargs; ++i) { |
| 4409 | QualType argTy = proto->getArgType(i); |
Douglas Gregor | b0f8eac | 2010-02-03 19:27:29 +0000 | [diff] [blame] | 4410 | |
| 4411 | // Look at the promotion type of enum types, since that is the type used |
| 4412 | // to pass enum values. |
| 4413 | if (const EnumType *Enum = argTy->getAs<EnumType>()) |
| 4414 | argTy = Enum->getDecl()->getPromotionType(); |
| 4415 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4416 | if (argTy->isPromotableIntegerType() || |
| 4417 | getCanonicalType(argTy).getUnqualifiedType() == FloatTy) |
| 4418 | return QualType(); |
| 4419 | } |
| 4420 | |
| 4421 | if (allLTypes) return lhs; |
| 4422 | if (allRTypes) return rhs; |
| 4423 | return getFunctionType(retType, proto->arg_type_begin(), |
Mike Stump | 2d3c191 | 2009-07-27 00:44:23 +0000 | [diff] [blame] | 4424 | proto->getNumArgs(), proto->isVariadic(), |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 4425 | proto->getTypeQuals(), |
| 4426 | false, false, 0, 0, NoReturn, lcc); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4427 | } |
| 4428 | |
| 4429 | if (allLTypes) return lhs; |
| 4430 | if (allRTypes) return rhs; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 4431 | return getFunctionNoProtoType(retType, NoReturn, lcc); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4432 | } |
| 4433 | |
| 4434 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) { |
Bill Wendling | 43d6975 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 4435 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 4436 | // type is adjusted to "T" prior to any further analysis, the expression |
| 4437 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4438 | // expression is an lvalue unless the reference is an rvalue reference and |
| 4439 | // the expression is a function call (possibly inside parentheses). |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 4440 | assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?"); |
| 4441 | assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?"); |
| 4442 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4443 | QualType LHSCan = getCanonicalType(LHS), |
| 4444 | RHSCan = getCanonicalType(RHS); |
| 4445 | |
| 4446 | // If two types are identical, they are compatible. |
| 4447 | if (LHSCan == RHSCan) |
| 4448 | return LHS; |
| 4449 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4450 | // If the qualifiers are different, the types aren't compatible... mostly. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4451 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 4452 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4453 | if (LQuals != RQuals) { |
| 4454 | // If any of these qualifiers are different, we have a type |
| 4455 | // mismatch. |
| 4456 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 4457 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 4458 | return QualType(); |
| 4459 | |
| 4460 | // Exactly one GC qualifier difference is allowed: __strong is |
| 4461 | // okay if the other type has no GC qualifier but is an Objective |
| 4462 | // C object pointer (i.e. implicitly strong by default). We fix |
| 4463 | // this by pretending that the unqualified type was actually |
| 4464 | // qualified __strong. |
| 4465 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 4466 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 4467 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 4468 | |
| 4469 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 4470 | return QualType(); |
| 4471 | |
| 4472 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
| 4473 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); |
| 4474 | } |
| 4475 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
| 4476 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); |
| 4477 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4478 | return QualType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4479 | } |
| 4480 | |
| 4481 | // Okay, qualifiers are equal. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4482 | |
Eli Friedman | 852d63b | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 4483 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 4484 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4485 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4486 | // We want to consider the two function types to be the same for these |
| 4487 | // comparisons, just force one to the other. |
| 4488 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 4489 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 4c721d3 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 4490 | |
| 4491 | // Same as above for arrays |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 4492 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 4493 | LHSClass = Type::ConstantArray; |
| 4494 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 4495 | RHSClass = Type::ConstantArray; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4496 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 4497 | // Canonicalize ExtVector -> Vector. |
| 4498 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 4499 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4500 | |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 4501 | // If the canonical type classes don't match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4502 | if (LHSClass != RHSClass) { |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4503 | // 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] | 4504 | // a signed integer type, or an unsigned integer type. |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 4505 | // Compatibility is based on the underlying type, not the promotion |
| 4506 | // type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4507 | if (const EnumType* ETy = LHS->getAs<EnumType>()) { |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4508 | if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType()) |
| 4509 | return RHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 4510 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4511 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4512 | if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType()) |
| 4513 | return LHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 4514 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4515 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4516 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4517 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4518 | |
Steve Naroff | 4a74678 | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 4519 | // The canonical type classes match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4520 | switch (LHSClass) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4521 | #define TYPE(Class, Base) |
| 4522 | #define ABSTRACT_TYPE(Class, Base) |
| 4523 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 4524 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 4525 | #include "clang/AST/TypeNodes.def" |
| 4526 | assert(false && "Non-canonical and dependent types shouldn't get here"); |
| 4527 | return QualType(); |
| 4528 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4529 | case Type::LValueReference: |
| 4530 | case Type::RValueReference: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4531 | case Type::MemberPointer: |
| 4532 | assert(false && "C++ should never be in mergeTypes"); |
| 4533 | return QualType(); |
| 4534 | |
| 4535 | case Type::IncompleteArray: |
| 4536 | case Type::VariableArray: |
| 4537 | case Type::FunctionProto: |
| 4538 | case Type::ExtVector: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4539 | assert(false && "Types are eliminated above"); |
| 4540 | return QualType(); |
| 4541 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4542 | case Type::Pointer: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4543 | { |
| 4544 | // Merge two pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4545 | QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); |
| 4546 | QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4547 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee); |
| 4548 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 4549 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4550 | return LHS; |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 4551 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4552 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4553 | return getPointerType(ResultType); |
| 4554 | } |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 4555 | case Type::BlockPointer: |
| 4556 | { |
| 4557 | // Merge two block pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4558 | QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); |
| 4559 | QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 4560 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee); |
| 4561 | if (ResultType.isNull()) return QualType(); |
| 4562 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 4563 | return LHS; |
| 4564 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 4565 | return RHS; |
| 4566 | return getBlockPointerType(ResultType); |
| 4567 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4568 | case Type::ConstantArray: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4569 | { |
| 4570 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 4571 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 4572 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 4573 | return QualType(); |
| 4574 | |
| 4575 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 4576 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
| 4577 | QualType ResultType = mergeTypes(LHSElem, RHSElem); |
| 4578 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4579 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 4580 | return LHS; |
| 4581 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 4582 | return RHS; |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 4583 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 4584 | ArrayType::ArraySizeModifier(), 0); |
| 4585 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 4586 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4587 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 4588 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4589 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 4590 | return LHS; |
| 4591 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 4592 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4593 | if (LVAT) { |
| 4594 | // FIXME: This isn't correct! But tricky to implement because |
| 4595 | // the array's size has to be the size of LHS, but the type |
| 4596 | // has to be different. |
| 4597 | return LHS; |
| 4598 | } |
| 4599 | if (RVAT) { |
| 4600 | // FIXME: This isn't correct! But tricky to implement because |
| 4601 | // the array's size has to be the size of RHS, but the type |
| 4602 | // has to be different. |
| 4603 | return RHS; |
| 4604 | } |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 4605 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 4606 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4607 | return getIncompleteArrayType(ResultType, |
| 4608 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4609 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4610 | case Type::FunctionNoProto: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4611 | return mergeFunctionTypes(LHS, RHS); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4612 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4613 | case Type::Enum: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4614 | return QualType(); |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4615 | case Type::Builtin: |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 4616 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4617 | return QualType(); |
Daniel Dunbar | 64cfdb7 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 4618 | case Type::Complex: |
| 4619 | // Distinct complex types are incompatible. |
| 4620 | return QualType(); |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 4621 | case Type::Vector: |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 4622 | // FIXME: The merged type should be an ExtVector! |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4623 | if (areCompatVectorTypes(LHS->getAs<VectorType>(), RHS->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: { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4639 | if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(), |
| 4640 | RHS->getAs<ObjCObjectPointerType>())) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4641 | return LHS; |
| 4642 | |
Steve Naroff | bc76dd0 | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 4643 | return QualType(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4644 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4645 | case Type::TemplateSpecialization: |
| 4646 | assert(false && "Dependent types have no size"); |
| 4647 | break; |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4648 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4649 | |
| 4650 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4651 | } |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 4652 | |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 4653 | //===----------------------------------------------------------------------===// |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4654 | // Integer Predicates |
| 4655 | //===----------------------------------------------------------------------===// |
Chris Lattner | 88054de | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 4656 | |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4657 | unsigned ASTContext::getIntWidth(QualType T) { |
Sebastian Redl | 632d772 | 2009-11-05 21:10:57 +0000 | [diff] [blame] | 4658 | if (T->isBooleanType()) |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4659 | return 1; |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 4660 | if (EnumType *ET = dyn_cast<EnumType>(T)) |
Eli Friedman | 29a7f33 | 2009-12-10 22:29:29 +0000 | [diff] [blame] | 4661 | T = ET->getDecl()->getIntegerType(); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4662 | // For builtin types, just use the standard type sizing method |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4663 | return (unsigned)getTypeSize(T); |
| 4664 | } |
| 4665 | |
| 4666 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) { |
| 4667 | assert(T->isSignedIntegerType() && "Unexpected type"); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4668 | |
| 4669 | // Turn <4 x signed int> -> <4 x unsigned int> |
| 4670 | if (const VectorType *VTy = T->getAs<VectorType>()) |
| 4671 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4672 | VTy->getNumElements(), VTy->isAltiVec(), VTy->isPixel()); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4673 | |
| 4674 | // For enums, we return the unsigned version of the base type. |
| 4675 | if (const EnumType *ETy = T->getAs<EnumType>()) |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4676 | T = ETy->getDecl()->getIntegerType(); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4677 | |
| 4678 | const BuiltinType *BTy = T->getAs<BuiltinType>(); |
| 4679 | assert(BTy && "Unexpected signed integer type"); |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4680 | switch (BTy->getKind()) { |
| 4681 | case BuiltinType::Char_S: |
| 4682 | case BuiltinType::SChar: |
| 4683 | return UnsignedCharTy; |
| 4684 | case BuiltinType::Short: |
| 4685 | return UnsignedShortTy; |
| 4686 | case BuiltinType::Int: |
| 4687 | return UnsignedIntTy; |
| 4688 | case BuiltinType::Long: |
| 4689 | return UnsignedLongTy; |
| 4690 | case BuiltinType::LongLong: |
| 4691 | return UnsignedLongLongTy; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 4692 | case BuiltinType::Int128: |
| 4693 | return UnsignedInt128Ty; |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4694 | default: |
| 4695 | assert(0 && "Unexpected signed integer type"); |
| 4696 | return QualType(); |
| 4697 | } |
| 4698 | } |
| 4699 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 4700 | ExternalASTSource::~ExternalASTSource() { } |
| 4701 | |
| 4702 | void ExternalASTSource::PrintStats() { } |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4703 | |
| 4704 | |
| 4705 | //===----------------------------------------------------------------------===// |
| 4706 | // Builtin Type Computation |
| 4707 | //===----------------------------------------------------------------------===// |
| 4708 | |
| 4709 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
| 4710 | /// pointer over the consumed characters. This returns the resultant type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4711 | static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context, |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4712 | ASTContext::GetBuiltinTypeError &Error, |
| 4713 | bool AllowTypeModifiers = true) { |
| 4714 | // Modifiers. |
| 4715 | int HowLong = 0; |
| 4716 | bool Signed = false, Unsigned = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4717 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4718 | // Read the modifiers first. |
| 4719 | bool Done = false; |
| 4720 | while (!Done) { |
| 4721 | switch (*Str++) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4722 | default: Done = true; --Str; break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4723 | case 'S': |
| 4724 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 4725 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 4726 | Signed = true; |
| 4727 | break; |
| 4728 | case 'U': |
| 4729 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
| 4730 | assert(!Unsigned && "Can't use 'S' modifier multiple times!"); |
| 4731 | Unsigned = true; |
| 4732 | break; |
| 4733 | case 'L': |
| 4734 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 4735 | ++HowLong; |
| 4736 | break; |
| 4737 | } |
| 4738 | } |
| 4739 | |
| 4740 | QualType Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4741 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4742 | // Read the base type. |
| 4743 | switch (*Str++) { |
| 4744 | default: assert(0 && "Unknown builtin type letter!"); |
| 4745 | case 'v': |
| 4746 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 4747 | "Bad modifiers used with 'v'!"); |
| 4748 | Type = Context.VoidTy; |
| 4749 | break; |
| 4750 | case 'f': |
| 4751 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 4752 | "Bad modifiers used with 'f'!"); |
| 4753 | Type = Context.FloatTy; |
| 4754 | break; |
| 4755 | case 'd': |
| 4756 | assert(HowLong < 2 && !Signed && !Unsigned && |
| 4757 | "Bad modifiers used with 'd'!"); |
| 4758 | if (HowLong) |
| 4759 | Type = Context.LongDoubleTy; |
| 4760 | else |
| 4761 | Type = Context.DoubleTy; |
| 4762 | break; |
| 4763 | case 's': |
| 4764 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 4765 | if (Unsigned) |
| 4766 | Type = Context.UnsignedShortTy; |
| 4767 | else |
| 4768 | Type = Context.ShortTy; |
| 4769 | break; |
| 4770 | case 'i': |
| 4771 | if (HowLong == 3) |
| 4772 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 4773 | else if (HowLong == 2) |
| 4774 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 4775 | else if (HowLong == 1) |
| 4776 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 4777 | else |
| 4778 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 4779 | break; |
| 4780 | case 'c': |
| 4781 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 4782 | if (Signed) |
| 4783 | Type = Context.SignedCharTy; |
| 4784 | else if (Unsigned) |
| 4785 | Type = Context.UnsignedCharTy; |
| 4786 | else |
| 4787 | Type = Context.CharTy; |
| 4788 | break; |
| 4789 | case 'b': // boolean |
| 4790 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 4791 | Type = Context.BoolTy; |
| 4792 | break; |
| 4793 | case 'z': // size_t. |
| 4794 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 4795 | Type = Context.getSizeType(); |
| 4796 | break; |
| 4797 | case 'F': |
| 4798 | Type = Context.getCFConstantStringType(); |
| 4799 | break; |
| 4800 | case 'a': |
| 4801 | Type = Context.getBuiltinVaListType(); |
| 4802 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 4803 | break; |
| 4804 | case 'A': |
| 4805 | // This is a "reference" to a va_list; however, what exactly |
| 4806 | // this means depends on how va_list is defined. There are two |
| 4807 | // different kinds of va_list: ones passed by value, and ones |
| 4808 | // passed by reference. An example of a by-value va_list is |
| 4809 | // x86, where va_list is a char*. An example of by-ref va_list |
| 4810 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 4811 | // we want this argument to be a char*&; for x86-64, we want |
| 4812 | // it to be a __va_list_tag*. |
| 4813 | Type = Context.getBuiltinVaListType(); |
| 4814 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 4815 | if (Type->isArrayType()) { |
| 4816 | Type = Context.getArrayDecayedType(Type); |
| 4817 | } else { |
| 4818 | Type = Context.getLValueReferenceType(Type); |
| 4819 | } |
| 4820 | break; |
| 4821 | case 'V': { |
| 4822 | char *End; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4823 | unsigned NumElements = strtoul(Str, &End, 10); |
| 4824 | assert(End != Str && "Missing vector size"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4825 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4826 | Str = End; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4827 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4828 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false); |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4829 | // FIXME: Don't know what to do about AltiVec. |
| 4830 | Type = Context.getVectorType(ElementType, NumElements, false, false); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4831 | break; |
| 4832 | } |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 4833 | case 'X': { |
| 4834 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false); |
| 4835 | Type = Context.getComplexType(ElementType); |
| 4836 | break; |
| 4837 | } |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 4838 | case 'P': |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 4839 | Type = Context.getFILEType(); |
| 4840 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 4841 | Error = ASTContext::GE_Missing_stdio; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4842 | return QualType(); |
| 4843 | } |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 4844 | break; |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 4845 | case 'J': |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 4846 | if (Signed) |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 4847 | Type = Context.getsigjmp_bufType(); |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 4848 | else |
| 4849 | Type = Context.getjmp_bufType(); |
| 4850 | |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 4851 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 4852 | Error = ASTContext::GE_Missing_setjmp; |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 4853 | return QualType(); |
| 4854 | } |
| 4855 | break; |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 4856 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4857 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4858 | if (!AllowTypeModifiers) |
| 4859 | return Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4860 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4861 | Done = false; |
| 4862 | while (!Done) { |
| 4863 | switch (*Str++) { |
| 4864 | default: Done = true; --Str; break; |
| 4865 | case '*': |
| 4866 | Type = Context.getPointerType(Type); |
| 4867 | break; |
| 4868 | case '&': |
| 4869 | Type = Context.getLValueReferenceType(Type); |
| 4870 | break; |
| 4871 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 4872 | case 'C': |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4873 | Type = Type.withConst(); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4874 | break; |
Fariborz Jahanian | 013af39 | 2010-01-26 22:48:42 +0000 | [diff] [blame] | 4875 | case 'D': |
| 4876 | Type = Context.getVolatileType(Type); |
| 4877 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4878 | } |
| 4879 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4880 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4881 | return Type; |
| 4882 | } |
| 4883 | |
| 4884 | /// GetBuiltinType - Return the type for the specified builtin. |
| 4885 | QualType ASTContext::GetBuiltinType(unsigned id, |
| 4886 | GetBuiltinTypeError &Error) { |
| 4887 | const char *TypeStr = BuiltinInfo.GetTypeString(id); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4888 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4889 | llvm::SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4890 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4891 | Error = GE_None; |
| 4892 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error); |
| 4893 | if (Error != GE_None) |
| 4894 | return QualType(); |
| 4895 | while (TypeStr[0] && TypeStr[0] != '.') { |
| 4896 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error); |
| 4897 | if (Error != GE_None) |
| 4898 | return QualType(); |
| 4899 | |
| 4900 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 4901 | if (Ty->isArrayType()) |
| 4902 | Ty = getArrayDecayedType(Ty); |
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 | ArgTypes.push_back(Ty); |
| 4905 | } |
| 4906 | |
| 4907 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 4908 | "'.' should only occur at end of builtin type list!"); |
| 4909 | |
| 4910 | // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);". |
| 4911 | if (ArgTypes.size() == 0 && TypeStr[0] == '.') |
| 4912 | return getFunctionNoProtoType(ResType); |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 4913 | |
| 4914 | // FIXME: Should we create noreturn types? |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4915 | return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 4916 | TypeStr[0] == '.', 0, false, false, 0, 0, |
| 4917 | false, CC_Default); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4918 | } |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4919 | |
| 4920 | QualType |
| 4921 | ASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) { |
| 4922 | // Perform the usual unary conversions. We do this early so that |
| 4923 | // integral promotions to "int" can allow us to exit early, in the |
| 4924 | // lhs == rhs check. Also, for conversion purposes, we ignore any |
| 4925 | // qualifiers. For example, "const float" and "float" are |
| 4926 | // equivalent. |
| 4927 | if (lhs->isPromotableIntegerType()) |
| 4928 | lhs = getPromotedIntegerType(lhs); |
| 4929 | else |
| 4930 | lhs = lhs.getUnqualifiedType(); |
| 4931 | if (rhs->isPromotableIntegerType()) |
| 4932 | rhs = getPromotedIntegerType(rhs); |
| 4933 | else |
| 4934 | rhs = rhs.getUnqualifiedType(); |
| 4935 | |
| 4936 | // If both types are identical, no conversion is needed. |
| 4937 | if (lhs == rhs) |
| 4938 | return lhs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4939 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4940 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 4941 | // The caller can deal with this (e.g. pointer + int). |
| 4942 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 4943 | return lhs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4944 | |
| 4945 | // At this point, we have two different arithmetic types. |
| 4946 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4947 | // Handle complex types first (C99 6.3.1.8p1). |
| 4948 | if (lhs->isComplexType() || rhs->isComplexType()) { |
| 4949 | // if we have an integer operand, the result is the complex type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4950 | if (rhs->isIntegerType() || rhs->isComplexIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4951 | // convert the rhs to the lhs complex type. |
| 4952 | return lhs; |
| 4953 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4954 | if (lhs->isIntegerType() || lhs->isComplexIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4955 | // convert the lhs to the rhs complex type. |
| 4956 | return rhs; |
| 4957 | } |
| 4958 | // This handles complex/complex, complex/float, or float/complex. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4959 | // When both operands are complex, the shorter operand is converted to the |
| 4960 | // type of the longer, and that is the type of the result. This corresponds |
| 4961 | // to what is done when combining two real floating-point operands. |
| 4962 | // The fun begins when size promotion occur across type domains. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4963 | // 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] | 4964 | // floating-point type, the less precise type is converted, within it's |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4965 | // 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] | 4966 | // when combining a "long double" with a "double _Complex", the |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4967 | // "double _Complex" is promoted to "long double _Complex". |
| 4968 | int result = getFloatingTypeOrder(lhs, rhs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4969 | |
| 4970 | if (result > 0) { // The left side is bigger, convert rhs. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4971 | rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4972 | } else if (result < 0) { // The right side is bigger, convert lhs. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4973 | lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4974 | } |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4975 | // At this point, lhs and rhs have the same rank/size. Now, make sure the |
| 4976 | // domains match. This is a requirement for our implementation, C99 |
| 4977 | // does not require this promotion. |
| 4978 | if (lhs != rhs) { // Domains don't match, we have complex/float mix. |
| 4979 | if (lhs->isRealFloatingType()) { // handle "double, _Complex double". |
| 4980 | return rhs; |
| 4981 | } else { // handle "_Complex double, double". |
| 4982 | return lhs; |
| 4983 | } |
| 4984 | } |
| 4985 | return lhs; // The domain/size match exactly. |
| 4986 | } |
| 4987 | // Now handle "real" floating types (i.e. float, double, long double). |
| 4988 | if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) { |
| 4989 | // if we have an integer operand, the result is the real floating type. |
| 4990 | if (rhs->isIntegerType()) { |
| 4991 | // convert rhs to the lhs floating point type. |
| 4992 | return lhs; |
| 4993 | } |
| 4994 | if (rhs->isComplexIntegerType()) { |
| 4995 | // convert rhs to the complex floating point type. |
| 4996 | return getComplexType(lhs); |
| 4997 | } |
| 4998 | if (lhs->isIntegerType()) { |
| 4999 | // convert lhs to the rhs floating point type. |
| 5000 | return rhs; |
| 5001 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5002 | if (lhs->isComplexIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5003 | // convert lhs to the complex floating point type. |
| 5004 | return getComplexType(rhs); |
| 5005 | } |
| 5006 | // We have two real floating types, float/complex combos were handled above. |
| 5007 | // Convert the smaller operand to the bigger result. |
| 5008 | int result = getFloatingTypeOrder(lhs, rhs); |
| 5009 | if (result > 0) // convert the rhs |
| 5010 | return lhs; |
| 5011 | assert(result < 0 && "illegal float comparison"); |
| 5012 | return rhs; // convert the lhs |
| 5013 | } |
| 5014 | if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) { |
| 5015 | // Handle GCC complex int extension. |
| 5016 | const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType(); |
| 5017 | const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType(); |
| 5018 | |
| 5019 | if (lhsComplexInt && rhsComplexInt) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5020 | if (getIntegerTypeOrder(lhsComplexInt->getElementType(), |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5021 | rhsComplexInt->getElementType()) >= 0) |
| 5022 | return lhs; // convert the rhs |
| 5023 | return rhs; |
| 5024 | } else if (lhsComplexInt && rhs->isIntegerType()) { |
| 5025 | // convert the rhs to the lhs complex type. |
| 5026 | return lhs; |
| 5027 | } else if (rhsComplexInt && lhs->isIntegerType()) { |
| 5028 | // convert the lhs to the rhs complex type. |
| 5029 | return rhs; |
| 5030 | } |
| 5031 | } |
| 5032 | // Finally, we have two differing integer types. |
| 5033 | // The rules for this case are in C99 6.3.1.8 |
| 5034 | int compare = getIntegerTypeOrder(lhs, rhs); |
| 5035 | bool lhsSigned = lhs->isSignedIntegerType(), |
| 5036 | rhsSigned = rhs->isSignedIntegerType(); |
| 5037 | QualType destType; |
| 5038 | if (lhsSigned == rhsSigned) { |
| 5039 | // Same signedness; use the higher-ranked type |
| 5040 | destType = compare >= 0 ? lhs : rhs; |
| 5041 | } else if (compare != (lhsSigned ? 1 : -1)) { |
| 5042 | // The unsigned type has greater than or equal rank to the |
| 5043 | // signed type, so use the unsigned type |
| 5044 | destType = lhsSigned ? rhs : lhs; |
| 5045 | } else if (getIntWidth(lhs) != getIntWidth(rhs)) { |
| 5046 | // The two types are different widths; if we are here, that |
| 5047 | // means the signed type is larger than the unsigned type, so |
| 5048 | // use the signed type. |
| 5049 | destType = lhsSigned ? lhs : rhs; |
| 5050 | } else { |
| 5051 | // The signed type is higher-ranked than the unsigned type, |
| 5052 | // but isn't actually any bigger (like unsigned int and long |
| 5053 | // on most 32-bit systems). Use the unsigned type corresponding |
| 5054 | // to the signed type. |
| 5055 | destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs); |
| 5056 | } |
| 5057 | return destType; |
| 5058 | } |