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 | bbfd68d | 2009-12-23 21:13:52 +0000 | [diff] [blame] | 59 | if (FreeMemory) { |
| 60 | // Deallocate all the types. |
| 61 | while (!Types.empty()) { |
| 62 | Types.back()->Destroy(*this); |
| 63 | Types.pop_back(); |
| 64 | } |
Eli Friedman | b26153c | 2008-05-27 03:08:09 +0000 | [diff] [blame] | 65 | |
Ted Kremenek | bbfd68d | 2009-12-23 21:13:52 +0000 | [diff] [blame] | 66 | for (llvm::FoldingSet<ExtQuals>::iterator |
| 67 | I = ExtQualNodes.begin(), E = ExtQualNodes.end(); I != E; ) { |
| 68 | // Increment in loop to prevent using deallocated memory. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 69 | Deallocate(&*I++); |
Nuno Lopes | b74668e | 2008-12-17 22:30:25 +0000 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
Ted Kremenek | bbfd68d | 2009-12-23 21:13:52 +0000 | [diff] [blame] | 73 | for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator |
| 74 | I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) { |
| 75 | // Increment in loop to prevent using deallocated memory. |
| 76 | ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second); |
| 77 | delete R; |
| 78 | } |
| 79 | |
| 80 | for (llvm::DenseMap<const ObjCContainerDecl*, |
| 81 | const ASTRecordLayout*>::iterator |
| 82 | I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) { |
| 83 | // Increment in loop to prevent using deallocated memory. |
| 84 | ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second); |
| 85 | delete R; |
Nuno Lopes | b74668e | 2008-12-17 22:30:25 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 88 | // Destroy nested-name-specifiers. |
Douglas Gregor | 1ae0afa | 2009-03-27 23:54:10 +0000 | [diff] [blame] | 89 | for (llvm::FoldingSet<NestedNameSpecifier>::iterator |
| 90 | NNS = NestedNameSpecifiers.begin(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | NNSEnd = NestedNameSpecifiers.end(); |
Ted Kremenek | bbfd68d | 2009-12-23 21:13:52 +0000 | [diff] [blame] | 92 | NNS != NNSEnd; ) { |
| 93 | // Increment in loop to prevent using deallocated memory. |
Douglas Gregor | 1ae0afa | 2009-03-27 23:54:10 +0000 | [diff] [blame] | 94 | (*NNS++).Destroy(*this); |
Ted Kremenek | bbfd68d | 2009-12-23 21:13:52 +0000 | [diff] [blame] | 95 | } |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 96 | |
| 97 | if (GlobalNestedNameSpecifier) |
| 98 | GlobalNestedNameSpecifier->Destroy(*this); |
| 99 | |
Eli Friedman | b26153c | 2008-05-27 03:08:09 +0000 | [diff] [blame] | 100 | TUDecl->Destroy(*this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 103 | void |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 104 | ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) { |
| 105 | ExternalSource.reset(Source.take()); |
| 106 | } |
| 107 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 108 | void ASTContext::PrintStats() const { |
| 109 | fprintf(stderr, "*** AST Context Stats:\n"); |
| 110 | fprintf(stderr, " %d types total.\n", (int)Types.size()); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 111 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 112 | unsigned counts[] = { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 113 | #define TYPE(Name, Parent) 0, |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 114 | #define ABSTRACT_TYPE(Name, Parent) |
| 115 | #include "clang/AST/TypeNodes.def" |
| 116 | 0 // Extra |
| 117 | }; |
Douglas Gregor | c2ee10d | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 118 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 119 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 120 | Type *T = Types[i]; |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 121 | counts[(unsigned)T->getTypeClass()]++; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 124 | unsigned Idx = 0; |
| 125 | unsigned TotalBytes = 0; |
| 126 | #define TYPE(Name, Parent) \ |
| 127 | if (counts[Idx]) \ |
| 128 | fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \ |
| 129 | TotalBytes += counts[Idx] * sizeof(Name##Type); \ |
| 130 | ++Idx; |
| 131 | #define ABSTRACT_TYPE(Name, Parent) |
| 132 | #include "clang/AST/TypeNodes.def" |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 133 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 134 | fprintf(stderr, "Total bytes = %d\n", int(TotalBytes)); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 135 | |
| 136 | if (ExternalSource.get()) { |
| 137 | fprintf(stderr, "\n"); |
| 138 | ExternalSource->PrintStats(); |
| 139 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 143 | void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 144 | BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 145 | R = CanQualType::CreateUnsafe(QualType(Ty, 0)); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 146 | Types.push_back(Ty); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 149 | void ASTContext::InitBuiltinTypes() { |
| 150 | assert(VoidTy.isNull() && "Context reinitialized?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 151 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 152 | // C99 6.2.5p19. |
| 153 | InitBuiltinType(VoidTy, BuiltinType::Void); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 154 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 155 | // C99 6.2.5p2. |
| 156 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
| 157 | // C99 6.2.5p3. |
Eli Friedman | 15b9176 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 158 | if (LangOpts.CharIsSigned) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 159 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 160 | else |
| 161 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
| 162 | // C99 6.2.5p4. |
| 163 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 164 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 165 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 166 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 167 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 168 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 169 | // C99 6.2.5p6. |
| 170 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 171 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 172 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 173 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 174 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 175 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 176 | // C99 6.2.5p10. |
| 177 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 178 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 179 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 180 | |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 181 | // GNU extension, 128-bit integers. |
| 182 | InitBuiltinType(Int128Ty, BuiltinType::Int128); |
| 183 | InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128); |
| 184 | |
Chris Lattner | 3a25032 | 2009-02-26 23:43:47 +0000 | [diff] [blame] | 185 | if (LangOpts.CPlusPlus) // C++ 3.9.1p5 |
| 186 | InitBuiltinType(WCharTy, BuiltinType::WChar); |
| 187 | else // C99 |
| 188 | WCharTy = getFromTargetType(Target.getWCharType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 189 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 190 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 191 | InitBuiltinType(Char16Ty, BuiltinType::Char16); |
| 192 | else // C99 |
| 193 | Char16Ty = getFromTargetType(Target.getChar16Type()); |
| 194 | |
| 195 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 196 | InitBuiltinType(Char32Ty, BuiltinType::Char32); |
| 197 | else // C99 |
| 198 | Char32Ty = getFromTargetType(Target.getChar32Type()); |
| 199 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 200 | // Placeholder type for functions. |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 201 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
| 202 | |
| 203 | // Placeholder type for type-dependent expressions whose type is |
| 204 | // completely unknown. No code should ever check a type against |
| 205 | // DependentTy and users should never see it; however, it is here to |
| 206 | // help diagnose failures to properly check for type-dependent |
| 207 | // expressions. |
| 208 | InitBuiltinType(DependentTy, BuiltinType::Dependent); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 209 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 210 | // Placeholder type for C++0x auto declarations whose real type has |
Anders Carlsson | e89d159 | 2009-06-26 18:41:36 +0000 | [diff] [blame] | 211 | // not yet been deduced. |
| 212 | InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 213 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 214 | // C99 6.2.5p11. |
| 215 | FloatComplexTy = getComplexType(FloatTy); |
| 216 | DoubleComplexTy = getComplexType(DoubleTy); |
| 217 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 218 | |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 219 | BuiltinVaListType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 220 | |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 221 | // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope(). |
| 222 | ObjCIdTypedefType = QualType(); |
| 223 | ObjCClassTypedefType = QualType(); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 224 | ObjCSelTypedefType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 225 | |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 226 | // Builtin types for 'id', 'Class', and 'SEL'. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 227 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); |
| 228 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 229 | InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 230 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 231 | ObjCConstantStringType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 232 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 233 | // void * type |
| 234 | VoidPtrTy = getPointerType(VoidTy); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 235 | |
| 236 | // nullptr type (C++0x 2.14.7) |
| 237 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 240 | MemberSpecializationInfo * |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 241 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 242 | assert(Var->isStaticDataMember() && "Not a static data member"); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 243 | llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 244 | = InstantiatedFromStaticDataMember.find(Var); |
| 245 | if (Pos == InstantiatedFromStaticDataMember.end()) |
| 246 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 247 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 248 | return Pos->second; |
| 249 | } |
| 250 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 251 | void |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 252 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, |
| 253 | TemplateSpecializationKind TSK) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 254 | assert(Inst->isStaticDataMember() && "Not a static data member"); |
| 255 | assert(Tmpl->isStaticDataMember() && "Not a static data member"); |
| 256 | assert(!InstantiatedFromStaticDataMember[Inst] && |
| 257 | "Already noted what static data member was instantiated from"); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 258 | InstantiatedFromStaticDataMember[Inst] |
| 259 | = new (*this) MemberSpecializationInfo(Tmpl, TSK); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 260 | } |
| 261 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 262 | NamedDecl * |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 263 | ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 264 | llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 265 | = InstantiatedFromUsingDecl.find(UUD); |
| 266 | if (Pos == InstantiatedFromUsingDecl.end()) |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 267 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 268 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 269 | return Pos->second; |
| 270 | } |
| 271 | |
| 272 | void |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 273 | ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) { |
| 274 | assert((isa<UsingDecl>(Pattern) || |
| 275 | isa<UnresolvedUsingValueDecl>(Pattern) || |
| 276 | isa<UnresolvedUsingTypenameDecl>(Pattern)) && |
| 277 | "pattern decl is not a using decl"); |
| 278 | assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists"); |
| 279 | InstantiatedFromUsingDecl[Inst] = Pattern; |
| 280 | } |
| 281 | |
| 282 | UsingShadowDecl * |
| 283 | ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) { |
| 284 | llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos |
| 285 | = InstantiatedFromUsingShadowDecl.find(Inst); |
| 286 | if (Pos == InstantiatedFromUsingShadowDecl.end()) |
| 287 | return 0; |
| 288 | |
| 289 | return Pos->second; |
| 290 | } |
| 291 | |
| 292 | void |
| 293 | ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, |
| 294 | UsingShadowDecl *Pattern) { |
| 295 | assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists"); |
| 296 | InstantiatedFromUsingShadowDecl[Inst] = Pattern; |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 299 | FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { |
| 300 | llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos |
| 301 | = InstantiatedFromUnnamedFieldDecl.find(Field); |
| 302 | if (Pos == InstantiatedFromUnnamedFieldDecl.end()) |
| 303 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 304 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 305 | return Pos->second; |
| 306 | } |
| 307 | |
| 308 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, |
| 309 | FieldDecl *Tmpl) { |
| 310 | assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed"); |
| 311 | assert(!Tmpl->getDeclName() && "Template field decl is not unnamed"); |
| 312 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] && |
| 313 | "Already noted what unnamed field was instantiated from"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 314 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 315 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; |
| 316 | } |
| 317 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 318 | namespace { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 319 | class BeforeInTranslationUnit |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 320 | : std::binary_function<SourceRange, SourceRange, bool> { |
| 321 | SourceManager *SourceMgr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 322 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 323 | public: |
| 324 | explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 325 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 326 | bool operator()(SourceRange X, SourceRange Y) { |
| 327 | return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin()); |
| 328 | } |
| 329 | }; |
| 330 | } |
| 331 | |
| 332 | /// \brief Determine whether the given comment is a Doxygen-style comment. |
| 333 | /// |
| 334 | /// \param Start the start of the comment text. |
| 335 | /// |
| 336 | /// \param End the end of the comment text. |
| 337 | /// |
| 338 | /// \param Member whether we want to check whether this is a member comment |
| 339 | /// (which requires a < after the Doxygen-comment delimiter). Otherwise, |
| 340 | /// we only return true when we find a non-member comment. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 341 | static bool |
| 342 | isDoxygenComment(SourceManager &SourceMgr, SourceRange Comment, |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 343 | bool Member = false) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 344 | const char *BufferStart |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 345 | = SourceMgr.getBufferData(SourceMgr.getFileID(Comment.getBegin())).first; |
| 346 | const char *Start = BufferStart + SourceMgr.getFileOffset(Comment.getBegin()); |
| 347 | const char* End = BufferStart + SourceMgr.getFileOffset(Comment.getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 348 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 349 | if (End - Start < 4) |
| 350 | return false; |
| 351 | |
| 352 | assert(Start[0] == '/' && "Not a comment?"); |
| 353 | if (Start[1] == '*' && !(Start[2] == '!' || Start[2] == '*')) |
| 354 | return false; |
| 355 | if (Start[1] == '/' && !(Start[2] == '!' || Start[2] == '/')) |
| 356 | return false; |
| 357 | |
| 358 | return (Start[3] == '<') == Member; |
| 359 | } |
| 360 | |
| 361 | /// \brief Retrieve the comment associated with the given declaration, if |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 362 | /// it has one. |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 363 | const char *ASTContext::getCommentForDecl(const Decl *D) { |
| 364 | if (!D) |
| 365 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 366 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 367 | // Check whether we have cached a comment string for this declaration |
| 368 | // already. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 369 | llvm::DenseMap<const Decl *, std::string>::iterator Pos |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 370 | = DeclComments.find(D); |
| 371 | if (Pos != DeclComments.end()) |
| 372 | return Pos->second.c_str(); |
| 373 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 374 | // 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] | 375 | // that source, do so now. |
| 376 | if (ExternalSource && !LoadedExternalComments) { |
| 377 | std::vector<SourceRange> LoadedComments; |
| 378 | ExternalSource->ReadComments(LoadedComments); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 379 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 380 | if (!LoadedComments.empty()) |
| 381 | Comments.insert(Comments.begin(), LoadedComments.begin(), |
| 382 | LoadedComments.end()); |
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 | LoadedExternalComments = true; |
| 385 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 386 | |
| 387 | // If there are no comments anywhere, we won't find anything. |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 388 | if (Comments.empty()) |
| 389 | return 0; |
| 390 | |
| 391 | // If the declaration doesn't map directly to a location in a file, we |
| 392 | // can't find the comment. |
| 393 | SourceLocation DeclStartLoc = D->getLocStart(); |
| 394 | if (DeclStartLoc.isInvalid() || !DeclStartLoc.isFileID()) |
| 395 | return 0; |
| 396 | |
| 397 | // Find the comment that occurs just before this declaration. |
| 398 | std::vector<SourceRange>::iterator LastComment |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 399 | = std::lower_bound(Comments.begin(), Comments.end(), |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 400 | SourceRange(DeclStartLoc), |
| 401 | BeforeInTranslationUnit(&SourceMgr)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 402 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 403 | // Decompose the location for the start of the declaration and find the |
| 404 | // beginning of the file buffer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 405 | std::pair<FileID, unsigned> DeclStartDecomp |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 406 | = SourceMgr.getDecomposedLoc(DeclStartLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 407 | const char *FileBufferStart |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 408 | = SourceMgr.getBufferData(DeclStartDecomp.first).first; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 409 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 410 | // First check whether we have a comment for a member. |
| 411 | if (LastComment != Comments.end() && |
| 412 | !isa<TagDecl>(D) && !isa<NamespaceDecl>(D) && |
| 413 | isDoxygenComment(SourceMgr, *LastComment, true)) { |
| 414 | std::pair<FileID, unsigned> LastCommentEndDecomp |
| 415 | = SourceMgr.getDecomposedLoc(LastComment->getEnd()); |
| 416 | if (DeclStartDecomp.first == LastCommentEndDecomp.first && |
| 417 | SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 418 | == SourceMgr.getLineNumber(LastCommentEndDecomp.first, |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 419 | LastCommentEndDecomp.second)) { |
| 420 | // The Doxygen member comment comes after the declaration starts and |
| 421 | // is on the same line and in the same file as the declaration. This |
| 422 | // is the comment we want. |
| 423 | std::string &Result = DeclComments[D]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 424 | Result.append(FileBufferStart + |
| 425 | SourceMgr.getFileOffset(LastComment->getBegin()), |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 426 | FileBufferStart + LastCommentEndDecomp.second + 1); |
| 427 | return Result.c_str(); |
| 428 | } |
| 429 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 430 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 431 | if (LastComment == Comments.begin()) |
| 432 | return 0; |
| 433 | --LastComment; |
| 434 | |
| 435 | // Decompose the end of the comment. |
| 436 | std::pair<FileID, unsigned> LastCommentEndDecomp |
| 437 | = SourceMgr.getDecomposedLoc(LastComment->getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 438 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 439 | // If the comment and the declaration aren't in the same file, then they |
| 440 | // aren't related. |
| 441 | if (DeclStartDecomp.first != LastCommentEndDecomp.first) |
| 442 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 443 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 444 | // Check that we actually have a Doxygen comment. |
| 445 | if (!isDoxygenComment(SourceMgr, *LastComment)) |
| 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 | // Compute the starting line for the declaration and for the end of the |
| 449 | // comment (this is expensive). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 450 | unsigned DeclStartLine |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 451 | = SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second); |
| 452 | unsigned CommentEndLine |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 453 | = SourceMgr.getLineNumber(LastCommentEndDecomp.first, |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 454 | LastCommentEndDecomp.second); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 455 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 456 | // If the comment does not end on the line prior to the declaration, then |
| 457 | // the comment is not associated with the declaration at all. |
| 458 | if (CommentEndLine + 1 != DeclStartLine) |
| 459 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 460 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 461 | // We have a comment, but there may be more comments on the previous lines. |
| 462 | // Keep looking so long as the comments are still Doxygen comments and are |
| 463 | // still adjacent. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 464 | unsigned ExpectedLine |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 465 | = SourceMgr.getSpellingLineNumber(LastComment->getBegin()) - 1; |
| 466 | std::vector<SourceRange>::iterator FirstComment = LastComment; |
| 467 | while (FirstComment != Comments.begin()) { |
| 468 | // Look at the previous comment |
| 469 | --FirstComment; |
| 470 | std::pair<FileID, unsigned> Decomp |
| 471 | = SourceMgr.getDecomposedLoc(FirstComment->getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 472 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 473 | // If this previous comment is in a different file, we're done. |
| 474 | if (Decomp.first != DeclStartDecomp.first) { |
| 475 | ++FirstComment; |
| 476 | break; |
| 477 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 478 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 479 | // If this comment is not a Doxygen comment, we're done. |
| 480 | if (!isDoxygenComment(SourceMgr, *FirstComment)) { |
| 481 | ++FirstComment; |
| 482 | break; |
| 483 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 484 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 485 | // If the line number is not what we expected, we're done. |
| 486 | unsigned Line = SourceMgr.getLineNumber(Decomp.first, Decomp.second); |
| 487 | if (Line != ExpectedLine) { |
| 488 | ++FirstComment; |
| 489 | break; |
| 490 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 491 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 492 | // Set the next expected line number. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 493 | ExpectedLine |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 494 | = SourceMgr.getSpellingLineNumber(FirstComment->getBegin()) - 1; |
| 495 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 496 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 497 | // The iterator range [FirstComment, LastComment] contains all of the |
| 498 | // BCPL comments that, together, are associated with this declaration. |
| 499 | // Form a single comment block string for this declaration that concatenates |
| 500 | // all of these comments. |
| 501 | std::string &Result = DeclComments[D]; |
| 502 | while (FirstComment != LastComment) { |
| 503 | std::pair<FileID, unsigned> DecompStart |
| 504 | = SourceMgr.getDecomposedLoc(FirstComment->getBegin()); |
| 505 | std::pair<FileID, unsigned> DecompEnd |
| 506 | = SourceMgr.getDecomposedLoc(FirstComment->getEnd()); |
| 507 | Result.append(FileBufferStart + DecompStart.second, |
| 508 | FileBufferStart + DecompEnd.second + 1); |
| 509 | ++FirstComment; |
| 510 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 511 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 512 | // Append the last comment line. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 513 | Result.append(FileBufferStart + |
| 514 | SourceMgr.getFileOffset(LastComment->getBegin()), |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 515 | FileBufferStart + LastCommentEndDecomp.second + 1); |
| 516 | return Result.c_str(); |
| 517 | } |
| 518 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 519 | //===----------------------------------------------------------------------===// |
| 520 | // Type Sizing and Analysis |
| 521 | //===----------------------------------------------------------------------===// |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 522 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 523 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 524 | /// scalar floating point type. |
| 525 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 526 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 527 | assert(BT && "Not a floating point type!"); |
| 528 | switch (BT->getKind()) { |
| 529 | default: assert(0 && "Not a floating point type!"); |
| 530 | case BuiltinType::Float: return Target.getFloatFormat(); |
| 531 | case BuiltinType::Double: return Target.getDoubleFormat(); |
| 532 | case BuiltinType::LongDouble: return Target.getLongDoubleFormat(); |
| 533 | } |
| 534 | } |
| 535 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 536 | /// getDeclAlign - Return a conservative estimate of the alignment of the |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 537 | /// specified decl. Note that bitfields do not have a valid alignment, so |
| 538 | /// this method will assert on them. |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 539 | /// If @p RefAsPointee, references are treated like their underlying type |
| 540 | /// (for alignof), else they're treated like pointers (for CodeGen). |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 541 | CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) { |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 542 | unsigned Align = Target.getCharWidth(); |
| 543 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 544 | if (const AlignedAttr* AA = D->getAttr<AlignedAttr>()) |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 545 | Align = std::max(Align, AA->getMaxAlignment()); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 546 | |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 547 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
| 548 | QualType T = VD->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 549 | if (const ReferenceType* RT = T->getAs<ReferenceType>()) { |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 550 | if (RefAsPointee) |
| 551 | T = RT->getPointeeType(); |
| 552 | else |
| 553 | T = getPointerType(RT->getPointeeType()); |
| 554 | } |
| 555 | if (!T->isIncompleteType() && !T->isFunctionType()) { |
Anders Carlsson | 4cc2cfd | 2009-04-10 04:47:03 +0000 | [diff] [blame] | 556 | // Incomplete or function types default to 1. |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 557 | while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T)) |
| 558 | T = cast<ArrayType>(T)->getElementType(); |
| 559 | |
| 560 | Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); |
| 561 | } |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 562 | } |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 563 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 564 | return CharUnits::fromQuantity(Align / Target.getCharWidth()); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 565 | } |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 566 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 567 | /// getTypeSize - Return the size of the specified type, in bits. This method |
| 568 | /// does not work on incomplete types. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 569 | /// |
| 570 | /// FIXME: Pointers into different addr spaces could have different sizes and |
| 571 | /// alignment requirements: getPointerInfo should take an AddrSpace, this |
| 572 | /// should take a QualType, &c. |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 573 | std::pair<uint64_t, unsigned> |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 574 | ASTContext::getTypeInfo(const Type *T) { |
Mike Stump | 5e30100 | 2009-02-27 18:32:39 +0000 | [diff] [blame] | 575 | uint64_t Width=0; |
| 576 | unsigned Align=8; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 577 | switch (T->getTypeClass()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 578 | #define TYPE(Class, Base) |
| 579 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 580 | #define NON_CANONICAL_TYPE(Class, Base) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 581 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 582 | #include "clang/AST/TypeNodes.def" |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 583 | assert(false && "Should not see dependent types"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 584 | break; |
| 585 | |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 586 | case Type::FunctionNoProto: |
| 587 | case Type::FunctionProto: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 588 | // GCC extension: alignof(function) = 32 bits |
| 589 | Width = 0; |
| 590 | Align = 32; |
| 591 | break; |
| 592 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 593 | case Type::IncompleteArray: |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 594 | case Type::VariableArray: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 595 | Width = 0; |
| 596 | Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); |
| 597 | break; |
| 598 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 599 | case Type::ConstantArray: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 600 | const ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 601 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 602 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 603 | Width = EltInfo.first*CAT->getSize().getZExtValue(); |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 604 | Align = EltInfo.second; |
| 605 | break; |
Christopher Lamb | 5c09a02 | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 606 | } |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 607 | case Type::ExtVector: |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 608 | case Type::Vector: { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 609 | const VectorType *VT = cast<VectorType>(T); |
| 610 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType()); |
| 611 | Width = EltInfo.first*VT->getNumElements(); |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 612 | Align = Width; |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 613 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 614 | // This happens for non-power-of-2 length vectors. |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 615 | if (VT->getNumElements() & (VT->getNumElements()-1)) { |
| 616 | Align = llvm::NextPowerOf2(Align); |
| 617 | Width = llvm::RoundUpToAlignment(Width, Align); |
| 618 | } |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 619 | break; |
| 620 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 621 | |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 622 | case Type::Builtin: |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 623 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 624 | default: assert(0 && "Unknown builtin type!"); |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 625 | case BuiltinType::Void: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 626 | // GCC extension: alignof(void) = 8 bits. |
| 627 | Width = 0; |
| 628 | Align = 8; |
| 629 | break; |
| 630 | |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 631 | case BuiltinType::Bool: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 632 | Width = Target.getBoolWidth(); |
| 633 | Align = Target.getBoolAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 634 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 635 | case BuiltinType::Char_S: |
| 636 | case BuiltinType::Char_U: |
| 637 | case BuiltinType::UChar: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 638 | case BuiltinType::SChar: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 639 | Width = Target.getCharWidth(); |
| 640 | Align = Target.getCharAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 641 | break; |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 642 | case BuiltinType::WChar: |
| 643 | Width = Target.getWCharWidth(); |
| 644 | Align = Target.getWCharAlign(); |
| 645 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 646 | case BuiltinType::Char16: |
| 647 | Width = Target.getChar16Width(); |
| 648 | Align = Target.getChar16Align(); |
| 649 | break; |
| 650 | case BuiltinType::Char32: |
| 651 | Width = Target.getChar32Width(); |
| 652 | Align = Target.getChar32Align(); |
| 653 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 654 | case BuiltinType::UShort: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 655 | case BuiltinType::Short: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 656 | Width = Target.getShortWidth(); |
| 657 | Align = Target.getShortAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 658 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 659 | case BuiltinType::UInt: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 660 | case BuiltinType::Int: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 661 | Width = Target.getIntWidth(); |
| 662 | Align = Target.getIntAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 663 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 664 | case BuiltinType::ULong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 665 | case BuiltinType::Long: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 666 | Width = Target.getLongWidth(); |
| 667 | Align = Target.getLongAlign(); |
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::ULongLong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 670 | case BuiltinType::LongLong: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 671 | Width = Target.getLongLongWidth(); |
| 672 | Align = Target.getLongLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 673 | break; |
Chris Lattner | ec16cb9 | 2009-04-30 02:55:13 +0000 | [diff] [blame] | 674 | case BuiltinType::Int128: |
| 675 | case BuiltinType::UInt128: |
| 676 | Width = 128; |
| 677 | Align = 128; // int128_t is 128-bit aligned on all targets. |
| 678 | break; |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 679 | case BuiltinType::Float: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 680 | Width = Target.getFloatWidth(); |
| 681 | Align = Target.getFloatAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 682 | break; |
| 683 | case BuiltinType::Double: |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 684 | Width = Target.getDoubleWidth(); |
| 685 | Align = Target.getDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 686 | break; |
| 687 | case BuiltinType::LongDouble: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 688 | Width = Target.getLongDoubleWidth(); |
| 689 | Align = Target.getLongDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 690 | break; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 691 | case BuiltinType::NullPtr: |
| 692 | Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) |
| 693 | Align = Target.getPointerAlign(0); // == sizeof(void*) |
Sebastian Redl | 1590d9c | 2009-05-27 19:34:06 +0000 | [diff] [blame] | 694 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 695 | } |
Chris Lattner | bfef6d7 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 696 | break; |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 697 | case Type::ObjCObjectPointer: |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 698 | Width = Target.getPointerWidth(0); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 699 | Align = Target.getPointerAlign(0); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 700 | break; |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 701 | case Type::BlockPointer: { |
| 702 | unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace(); |
| 703 | Width = Target.getPointerWidth(AS); |
| 704 | Align = Target.getPointerAlign(AS); |
| 705 | break; |
| 706 | } |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 707 | case Type::LValueReference: |
| 708 | case Type::RValueReference: { |
| 709 | // alignof and sizeof should never enter this code path here, so we go |
| 710 | // the pointer route. |
| 711 | unsigned AS = cast<ReferenceType>(T)->getPointeeType().getAddressSpace(); |
| 712 | Width = Target.getPointerWidth(AS); |
| 713 | Align = Target.getPointerAlign(AS); |
| 714 | break; |
| 715 | } |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 716 | case Type::Pointer: { |
| 717 | unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace(); |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 718 | Width = Target.getPointerWidth(AS); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 719 | Align = Target.getPointerAlign(AS); |
| 720 | break; |
| 721 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 722 | case Type::MemberPointer: { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 723 | QualType Pointee = cast<MemberPointerType>(T)->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 724 | std::pair<uint64_t, unsigned> PtrDiffInfo = |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 725 | getTypeInfo(getPointerDiffType()); |
| 726 | Width = PtrDiffInfo.first; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 727 | if (Pointee->isFunctionType()) |
| 728 | Width *= 2; |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 729 | Align = PtrDiffInfo.second; |
| 730 | break; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 731 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 732 | case Type::Complex: { |
| 733 | // Complex types have the same alignment as their elements, but twice the |
| 734 | // size. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 735 | std::pair<uint64_t, unsigned> EltInfo = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 736 | getTypeInfo(cast<ComplexType>(T)->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 737 | Width = EltInfo.first*2; |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 738 | Align = EltInfo.second; |
| 739 | break; |
| 740 | } |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 741 | case Type::ObjCInterface: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 742 | const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 743 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
| 744 | Width = Layout.getSize(); |
| 745 | Align = Layout.getAlignment(); |
| 746 | break; |
| 747 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 748 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 749 | case Type::Enum: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 750 | const TagType *TT = cast<TagType>(T); |
| 751 | |
| 752 | if (TT->getDecl()->isInvalidDecl()) { |
Chris Lattner | 8389eab | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 753 | Width = 1; |
| 754 | Align = 1; |
| 755 | break; |
| 756 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 757 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 758 | if (const EnumType *ET = dyn_cast<EnumType>(TT)) |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 759 | return getTypeInfo(ET->getDecl()->getIntegerType()); |
| 760 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 761 | const RecordType *RT = cast<RecordType>(TT); |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 762 | const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); |
| 763 | Width = Layout.getSize(); |
| 764 | Align = Layout.getAlignment(); |
Chris Lattner | dc0d73e | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 765 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 766 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 767 | |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 768 | case Type::SubstTemplateTypeParm: |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 769 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> |
| 770 | getReplacementType().getTypePtr()); |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 771 | |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 772 | case Type::Elaborated: |
| 773 | return getTypeInfo(cast<ElaboratedType>(T)->getUnderlyingType() |
| 774 | .getTypePtr()); |
John McCall | 7da2431 | 2009-09-05 00:15:47 +0000 | [diff] [blame] | 775 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 776 | case Type::Typedef: { |
| 777 | const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl(); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 778 | if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 779 | Align = std::max(Aligned->getMaxAlignment(), |
| 780 | getTypeAlign(Typedef->getUnderlyingType().getTypePtr())); |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 781 | Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr()); |
| 782 | } else |
| 783 | return getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 784 | break; |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 785 | } |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 786 | |
| 787 | case Type::TypeOfExpr: |
| 788 | return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType() |
| 789 | .getTypePtr()); |
| 790 | |
| 791 | case Type::TypeOf: |
| 792 | return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr()); |
| 793 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 794 | case Type::Decltype: |
| 795 | return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType() |
| 796 | .getTypePtr()); |
| 797 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 798 | case Type::QualifiedName: |
| 799 | return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 800 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 801 | case Type::TemplateSpecialization: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 802 | assert(getCanonicalType(T) != T && |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 803 | "Cannot request the size of a dependent type"); |
| 804 | // FIXME: this is likely to be wrong once we support template |
| 805 | // aliases, since a template alias could refer to a typedef that |
| 806 | // has an __aligned__ attribute on it. |
| 807 | return getTypeInfo(getCanonicalType(T)); |
| 808 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 809 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 810 | assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2"); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 811 | return std::make_pair(Width, Align); |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 812 | } |
| 813 | |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 814 | /// getTypeSizeInChars - Return the size of the specified type, in characters. |
| 815 | /// This method does not work on incomplete types. |
| 816 | CharUnits ASTContext::getTypeSizeInChars(QualType T) { |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 817 | return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth()); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 818 | } |
| 819 | CharUnits ASTContext::getTypeSizeInChars(const Type *T) { |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 820 | return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth()); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 821 | } |
| 822 | |
Ken Dyck | 16e20cc | 2010-01-26 17:25:18 +0000 | [diff] [blame] | 823 | /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 824 | /// characters. This method does not work on incomplete types. |
| 825 | CharUnits ASTContext::getTypeAlignInChars(QualType T) { |
| 826 | return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth()); |
| 827 | } |
| 828 | CharUnits ASTContext::getTypeAlignInChars(const Type *T) { |
| 829 | return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth()); |
| 830 | } |
| 831 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 832 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 833 | /// type for the current target in bits. This can be different than the ABI |
| 834 | /// alignment in cases where it is beneficial for performance to overalign |
| 835 | /// a data type. |
| 836 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) { |
| 837 | unsigned ABIAlign = getTypeAlign(T); |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 838 | |
| 839 | // Double and long long should be naturally aligned if possible. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 840 | if (const ComplexType* CT = T->getAs<ComplexType>()) |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 841 | T = CT->getElementType().getTypePtr(); |
| 842 | if (T->isSpecificBuiltinType(BuiltinType::Double) || |
| 843 | T->isSpecificBuiltinType(BuiltinType::LongLong)) |
| 844 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); |
| 845 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 846 | return ABIAlign; |
| 847 | } |
| 848 | |
Daniel Dunbar | a80a0f6 | 2009-04-22 17:43:55 +0000 | [diff] [blame] | 849 | static void CollectLocalObjCIvars(ASTContext *Ctx, |
| 850 | const ObjCInterfaceDecl *OI, |
| 851 | llvm::SmallVectorImpl<FieldDecl*> &Fields) { |
Fariborz Jahanian | a769c00 | 2008-12-17 21:40:49 +0000 | [diff] [blame] | 852 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 853 | E = OI->ivar_end(); I != E; ++I) { |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 854 | ObjCIvarDecl *IVDecl = *I; |
Fariborz Jahanian | a769c00 | 2008-12-17 21:40:49 +0000 | [diff] [blame] | 855 | if (!IVDecl->isInvalidDecl()) |
| 856 | Fields.push_back(cast<FieldDecl>(IVDecl)); |
| 857 | } |
| 858 | } |
| 859 | |
Daniel Dunbar | a80a0f6 | 2009-04-22 17:43:55 +0000 | [diff] [blame] | 860 | void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 861 | llvm::SmallVectorImpl<FieldDecl*> &Fields) { |
| 862 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
| 863 | CollectObjCIvars(SuperClass, Fields); |
| 864 | CollectLocalObjCIvars(this, OI, Fields); |
| 865 | } |
| 866 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 867 | /// ShallowCollectObjCIvars - |
| 868 | /// Collect all ivars, including those synthesized, in the current class. |
| 869 | /// |
| 870 | void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 871 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars, |
| 872 | bool CollectSynthesized) { |
| 873 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 874 | E = OI->ivar_end(); I != E; ++I) { |
| 875 | Ivars.push_back(*I); |
| 876 | } |
| 877 | if (CollectSynthesized) |
| 878 | CollectSynthesizedIvars(OI, Ivars); |
| 879 | } |
| 880 | |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 881 | void ASTContext::CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD, |
| 882 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 883 | for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(), |
| 884 | E = PD->prop_end(); I != E; ++I) |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 885 | if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl()) |
| 886 | Ivars.push_back(Ivar); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 887 | |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 888 | // Also look into nested protocols. |
| 889 | for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(), |
| 890 | E = PD->protocol_end(); P != E; ++P) |
| 891 | CollectProtocolSynthesizedIvars(*P, Ivars); |
| 892 | } |
| 893 | |
| 894 | /// CollectSynthesizedIvars - |
| 895 | /// This routine collect synthesized ivars for the designated class. |
| 896 | /// |
| 897 | void ASTContext::CollectSynthesizedIvars(const ObjCInterfaceDecl *OI, |
| 898 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 899 | for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(), |
| 900 | E = OI->prop_end(); I != E; ++I) { |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 901 | if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl()) |
| 902 | Ivars.push_back(Ivar); |
| 903 | } |
| 904 | // Also look into interface's protocol list for properties declared |
| 905 | // in the protocol and whose ivars are synthesized. |
| 906 | for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(), |
| 907 | PE = OI->protocol_end(); P != PE; ++P) { |
| 908 | ObjCProtocolDecl *PD = (*P); |
| 909 | CollectProtocolSynthesizedIvars(PD, Ivars); |
| 910 | } |
| 911 | } |
| 912 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 913 | /// CollectInheritedProtocols - Collect all protocols in current class and |
| 914 | /// those inherited by it. |
| 915 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, |
| 916 | llvm::SmallVectorImpl<ObjCProtocolDecl*> &Protocols) { |
| 917 | if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
| 918 | for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(), |
| 919 | PE = OI->protocol_end(); P != PE; ++P) { |
| 920 | ObjCProtocolDecl *Proto = (*P); |
| 921 | Protocols.push_back(Proto); |
| 922 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 923 | PE = Proto->protocol_end(); P != PE; ++P) |
| 924 | CollectInheritedProtocols(*P, Protocols); |
| 925 | } |
| 926 | |
| 927 | // Categories of this Interface. |
| 928 | for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList(); |
| 929 | CDeclChain; CDeclChain = CDeclChain->getNextClassCategory()) |
| 930 | CollectInheritedProtocols(CDeclChain, Protocols); |
| 931 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) |
| 932 | while (SD) { |
| 933 | CollectInheritedProtocols(SD, Protocols); |
| 934 | SD = SD->getSuperClass(); |
| 935 | } |
| 936 | return; |
| 937 | } |
| 938 | if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
| 939 | for (ObjCInterfaceDecl::protocol_iterator P = OC->protocol_begin(), |
| 940 | PE = OC->protocol_end(); P != PE; ++P) { |
| 941 | ObjCProtocolDecl *Proto = (*P); |
| 942 | Protocols.push_back(Proto); |
| 943 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 944 | PE = Proto->protocol_end(); P != PE; ++P) |
| 945 | CollectInheritedProtocols(*P, Protocols); |
| 946 | } |
| 947 | return; |
| 948 | } |
| 949 | if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
| 950 | for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(), |
| 951 | PE = OP->protocol_end(); P != PE; ++P) { |
| 952 | ObjCProtocolDecl *Proto = (*P); |
| 953 | Protocols.push_back(Proto); |
| 954 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 955 | PE = Proto->protocol_end(); P != PE; ++P) |
| 956 | CollectInheritedProtocols(*P, Protocols); |
| 957 | } |
| 958 | return; |
| 959 | } |
| 960 | } |
| 961 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 962 | unsigned ASTContext::CountProtocolSynthesizedIvars(const ObjCProtocolDecl *PD) { |
| 963 | unsigned count = 0; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 964 | for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(), |
| 965 | E = PD->prop_end(); I != E; ++I) |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 966 | if ((*I)->getPropertyIvarDecl()) |
| 967 | ++count; |
| 968 | |
| 969 | // Also look into nested protocols. |
| 970 | for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(), |
| 971 | E = PD->protocol_end(); P != E; ++P) |
| 972 | count += CountProtocolSynthesizedIvars(*P); |
| 973 | return count; |
| 974 | } |
| 975 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 976 | unsigned ASTContext::CountSynthesizedIvars(const ObjCInterfaceDecl *OI) { |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 977 | unsigned count = 0; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 978 | for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(), |
| 979 | E = OI->prop_end(); I != E; ++I) { |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 980 | if ((*I)->getPropertyIvarDecl()) |
| 981 | ++count; |
| 982 | } |
| 983 | // Also look into interface's protocol list for properties declared |
| 984 | // in the protocol and whose ivars are synthesized. |
| 985 | for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(), |
| 986 | PE = OI->protocol_end(); P != PE; ++P) { |
| 987 | ObjCProtocolDecl *PD = (*P); |
| 988 | count += CountProtocolSynthesizedIvars(PD); |
| 989 | } |
| 990 | return count; |
| 991 | } |
| 992 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 993 | /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. |
| 994 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
| 995 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 996 | I = ObjCImpls.find(D); |
| 997 | if (I != ObjCImpls.end()) |
| 998 | return cast<ObjCImplementationDecl>(I->second); |
| 999 | return 0; |
| 1000 | } |
| 1001 | /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. |
| 1002 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
| 1003 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1004 | I = ObjCImpls.find(D); |
| 1005 | if (I != ObjCImpls.end()) |
| 1006 | return cast<ObjCCategoryImplDecl>(I->second); |
| 1007 | return 0; |
| 1008 | } |
| 1009 | |
| 1010 | /// \brief Set the implementation of ObjCInterfaceDecl. |
| 1011 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
| 1012 | ObjCImplementationDecl *ImplD) { |
| 1013 | assert(IFaceD && ImplD && "Passed null params"); |
| 1014 | ObjCImpls[IFaceD] = ImplD; |
| 1015 | } |
| 1016 | /// \brief Set the implementation of ObjCCategoryDecl. |
| 1017 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
| 1018 | ObjCCategoryImplDecl *ImplD) { |
| 1019 | assert(CatD && ImplD && "Passed null params"); |
| 1020 | ObjCImpls[CatD] = ImplD; |
| 1021 | } |
| 1022 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1023 | /// \brief Allocate an uninitialized TypeSourceInfo. |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1024 | /// |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1025 | /// The caller should initialize the memory held by TypeSourceInfo using |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1026 | /// the TypeLoc wrappers. |
| 1027 | /// |
| 1028 | /// \param T the type that will be the basis for type source info. This type |
| 1029 | /// should refer to how the declarator was written in source code, not to |
| 1030 | /// what type semantic analysis resolved the declarator to. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1031 | TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T, |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1032 | unsigned DataSize) { |
| 1033 | if (!DataSize) |
| 1034 | DataSize = TypeLoc::getFullDataSizeForType(T); |
| 1035 | else |
| 1036 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1037 | "incorrect data size provided to CreateTypeSourceInfo!"); |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1038 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1039 | TypeSourceInfo *TInfo = |
| 1040 | (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8); |
| 1041 | new (TInfo) TypeSourceInfo(T); |
| 1042 | return TInfo; |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1045 | TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, |
John McCall | a4eb74d | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 1046 | SourceLocation L) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1047 | TypeSourceInfo *DI = CreateTypeSourceInfo(T); |
John McCall | a4eb74d | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 1048 | DI->getTypeLoc().initialize(L); |
| 1049 | return DI; |
| 1050 | } |
| 1051 | |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1052 | /// getInterfaceLayoutImpl - Get or compute information about the |
| 1053 | /// layout of the given interface. |
| 1054 | /// |
| 1055 | /// \param Impl - If given, also include the layout of the interface's |
| 1056 | /// implementation. This may differ by including synthesized ivars. |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1057 | const ASTRecordLayout & |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1058 | ASTContext::getObjCLayout(const ObjCInterfaceDecl *D, |
| 1059 | const ObjCImplementationDecl *Impl) { |
Daniel Dunbar | 532d4da | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 1060 | assert(!D->isForwardDecl() && "Invalid interface decl!"); |
| 1061 | |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1062 | // Look up this layout, if already laid out, return what we have. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1063 | ObjCContainerDecl *Key = |
Daniel Dunbar | d8fd6ff | 2009-05-03 11:41:43 +0000 | [diff] [blame] | 1064 | Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D; |
| 1065 | if (const ASTRecordLayout *Entry = ObjCLayouts[Key]) |
| 1066 | return *Entry; |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1067 | |
Daniel Dunbar | 453addb | 2009-05-03 11:16:44 +0000 | [diff] [blame] | 1068 | // Add in synthesized ivar count if laying out an implementation. |
| 1069 | if (Impl) { |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 1070 | unsigned SynthCount = CountSynthesizedIvars(D); |
Daniel Dunbar | d8fd6ff | 2009-05-03 11:41:43 +0000 | [diff] [blame] | 1071 | // If there aren't any sythesized ivars then reuse the interface |
Daniel Dunbar | 453addb | 2009-05-03 11:16:44 +0000 | [diff] [blame] | 1072 | // entry. Note we can't cache this because we simply free all |
| 1073 | // entries later; however we shouldn't look up implementations |
| 1074 | // frequently. |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 1075 | if (SynthCount == 0) |
Daniel Dunbar | 453addb | 2009-05-03 11:16:44 +0000 | [diff] [blame] | 1076 | return getObjCLayout(D, 0); |
| 1077 | } |
| 1078 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1079 | const ASTRecordLayout *NewEntry = |
Anders Carlsson | 29445a0 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 1080 | ASTRecordLayoutBuilder::ComputeLayout(*this, D, Impl); |
| 1081 | ObjCLayouts[Key] = NewEntry; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1082 | |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1083 | return *NewEntry; |
| 1084 | } |
| 1085 | |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1086 | const ASTRecordLayout & |
| 1087 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) { |
| 1088 | return getObjCLayout(D, 0); |
| 1089 | } |
| 1090 | |
| 1091 | const ASTRecordLayout & |
| 1092 | ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) { |
| 1093 | return getObjCLayout(D->getClassInterface(), D); |
| 1094 | } |
| 1095 | |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 1096 | /// getASTRecordLayout - Get or compute information about the layout of the |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 1097 | /// specified record (struct/union/class), which indicates its size and field |
| 1098 | /// position information. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1099 | const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) { |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1100 | D = D->getDefinition(*this); |
| 1101 | assert(D && "Cannot get layout of forward declarations!"); |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 1102 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 1103 | // Look up this layout, if already laid out, return what we have. |
Eli Friedman | ab22c43 | 2009-07-22 20:29:16 +0000 | [diff] [blame] | 1104 | // Note that we can't save a reference to the entry because this function |
| 1105 | // is recursive. |
| 1106 | const ASTRecordLayout *Entry = ASTRecordLayouts[D]; |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 1107 | if (Entry) return *Entry; |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 1108 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1109 | const ASTRecordLayout *NewEntry = |
Anders Carlsson | 29445a0 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 1110 | ASTRecordLayoutBuilder::ComputeLayout(*this, D); |
Eli Friedman | ab22c43 | 2009-07-22 20:29:16 +0000 | [diff] [blame] | 1111 | ASTRecordLayouts[D] = NewEntry; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1112 | |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1113 | return *NewEntry; |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
Anders Carlsson | f53df23 | 2009-12-07 04:35:11 +0000 | [diff] [blame] | 1116 | const CXXMethodDecl *ASTContext::getKeyFunction(const CXXRecordDecl *RD) { |
| 1117 | RD = cast<CXXRecordDecl>(RD->getDefinition(*this)); |
| 1118 | assert(RD && "Cannot get key function for forward declarations!"); |
| 1119 | |
| 1120 | const CXXMethodDecl *&Entry = KeyFunctions[RD]; |
| 1121 | if (!Entry) |
| 1122 | Entry = ASTRecordLayoutBuilder::ComputeKeyFunction(RD); |
| 1123 | else |
| 1124 | assert(Entry == ASTRecordLayoutBuilder::ComputeKeyFunction(RD) && |
| 1125 | "Key function changed!"); |
| 1126 | |
| 1127 | return Entry; |
| 1128 | } |
| 1129 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1130 | //===----------------------------------------------------------------------===// |
| 1131 | // Type creation/memoization methods |
| 1132 | //===----------------------------------------------------------------------===// |
| 1133 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1134 | QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) { |
| 1135 | unsigned Fast = Quals.getFastQualifiers(); |
| 1136 | Quals.removeFastQualifiers(); |
| 1137 | |
| 1138 | // Check if we've already instantiated this type. |
| 1139 | llvm::FoldingSetNodeID ID; |
| 1140 | ExtQuals::Profile(ID, TypeNode, Quals); |
| 1141 | void *InsertPos = 0; |
| 1142 | if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) { |
| 1143 | assert(EQ->getQualifiers() == Quals); |
| 1144 | QualType T = QualType(EQ, Fast); |
| 1145 | return T; |
| 1146 | } |
| 1147 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1148 | ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1149 | ExtQualNodes.InsertNode(New, InsertPos); |
| 1150 | QualType T = QualType(New, Fast); |
| 1151 | return T; |
| 1152 | } |
| 1153 | |
| 1154 | QualType ASTContext::getVolatileType(QualType T) { |
| 1155 | QualType CanT = getCanonicalType(T); |
| 1156 | if (CanT.isVolatileQualified()) return T; |
| 1157 | |
| 1158 | QualifierCollector Quals; |
| 1159 | const Type *TypeNode = Quals.strip(T); |
| 1160 | Quals.addVolatile(); |
| 1161 | |
| 1162 | return getExtQualType(TypeNode, Quals); |
| 1163 | } |
| 1164 | |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 1165 | QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1166 | QualType CanT = getCanonicalType(T); |
| 1167 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1168 | return T; |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1169 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1170 | // If we are composing extended qualifiers together, merge together |
| 1171 | // into one ExtQuals node. |
| 1172 | QualifierCollector Quals; |
| 1173 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1174 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1175 | // If this type already has an address space specified, it cannot get |
| 1176 | // another one. |
| 1177 | assert(!Quals.hasAddressSpace() && |
| 1178 | "Type cannot be in multiple addr spaces!"); |
| 1179 | Quals.addAddressSpace(AddressSpace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1180 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1181 | return getExtQualType(TypeNode, Quals); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1184 | QualType ASTContext::getObjCGCQualType(QualType T, |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1185 | Qualifiers::GC GCAttr) { |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1186 | QualType CanT = getCanonicalType(T); |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1187 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1188 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1189 | |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1190 | if (T->isPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1191 | QualType Pointee = T->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 1192 | if (Pointee->isAnyPointerType()) { |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1193 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 1194 | return getPointerType(ResultType); |
| 1195 | } |
| 1196 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1197 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1198 | // If we are composing extended qualifiers together, merge together |
| 1199 | // into one ExtQuals node. |
| 1200 | QualifierCollector Quals; |
| 1201 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1202 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1203 | // If this type already has an ObjCGC specified, it cannot get |
| 1204 | // another one. |
| 1205 | assert(!Quals.hasObjCGCAttr() && |
| 1206 | "Type cannot have multiple ObjCGCs!"); |
| 1207 | Quals.addObjCGCAttr(GCAttr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1208 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1209 | return getExtQualType(TypeNode, Quals); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1210 | } |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1211 | |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1212 | static QualType getNoReturnCallConvType(ASTContext& Context, QualType T, |
| 1213 | bool AddNoReturn, |
| 1214 | CallingConv CallConv) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1215 | QualType ResultType; |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1216 | if (const PointerType *Pointer = T->getAs<PointerType>()) { |
| 1217 | QualType Pointee = Pointer->getPointeeType(); |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1218 | ResultType = getNoReturnCallConvType(Context, Pointee, AddNoReturn, |
| 1219 | CallConv); |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1220 | if (ResultType == Pointee) |
| 1221 | return T; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1222 | |
| 1223 | ResultType = Context.getPointerType(ResultType); |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1224 | } else if (const BlockPointerType *BlockPointer |
| 1225 | = T->getAs<BlockPointerType>()) { |
| 1226 | QualType Pointee = BlockPointer->getPointeeType(); |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1227 | ResultType = getNoReturnCallConvType(Context, Pointee, AddNoReturn, |
| 1228 | CallConv); |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1229 | if (ResultType == Pointee) |
| 1230 | return T; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1231 | |
| 1232 | ResultType = Context.getBlockPointerType(ResultType); |
| 1233 | } else if (const FunctionType *F = T->getAs<FunctionType>()) { |
| 1234 | if (F->getNoReturnAttr() == AddNoReturn && F->getCallConv() == CallConv) |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1235 | return T; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1236 | |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1237 | if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(F)) { |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1238 | ResultType = Context.getFunctionNoProtoType(FNPT->getResultType(), |
| 1239 | AddNoReturn, CallConv); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1240 | } else { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1241 | const FunctionProtoType *FPT = cast<FunctionProtoType>(F); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1242 | ResultType |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1243 | = Context.getFunctionType(FPT->getResultType(), FPT->arg_type_begin(), |
| 1244 | FPT->getNumArgs(), FPT->isVariadic(), |
| 1245 | FPT->getTypeQuals(), |
| 1246 | FPT->hasExceptionSpec(), |
| 1247 | FPT->hasAnyExceptionSpec(), |
| 1248 | FPT->getNumExceptions(), |
| 1249 | FPT->exception_begin(), |
| 1250 | AddNoReturn, CallConv); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1251 | } |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1252 | } else |
| 1253 | return T; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1254 | |
| 1255 | return Context.getQualifiedType(ResultType, T.getLocalQualifiers()); |
| 1256 | } |
| 1257 | |
| 1258 | QualType ASTContext::getNoReturnType(QualType T, bool AddNoReturn) { |
| 1259 | return getNoReturnCallConvType(*this, T, AddNoReturn, T.getCallConv()); |
| 1260 | } |
| 1261 | |
| 1262 | QualType ASTContext::getCallConvType(QualType T, CallingConv CallConv) { |
| 1263 | return getNoReturnCallConvType(*this, T, T.getNoReturnAttr(), CallConv); |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1266 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 1267 | /// number with the specified element type. |
| 1268 | QualType ASTContext::getComplexType(QualType T) { |
| 1269 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1270 | // structure. |
| 1271 | llvm::FoldingSetNodeID ID; |
| 1272 | ComplexType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1273 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1274 | void *InsertPos = 0; |
| 1275 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1276 | return QualType(CT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1277 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1278 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1279 | // so fill in the canonical type field. |
| 1280 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1281 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1282 | Canonical = getComplexType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1283 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1284 | // Get the new insert position for the node we care about. |
| 1285 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1286 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1287 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1288 | ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1289 | Types.push_back(New); |
| 1290 | ComplexTypes.InsertNode(New, InsertPos); |
| 1291 | return QualType(New, 0); |
| 1292 | } |
| 1293 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1294 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 1295 | /// the specified type. |
| 1296 | QualType ASTContext::getPointerType(QualType T) { |
| 1297 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1298 | // structure. |
| 1299 | llvm::FoldingSetNodeID ID; |
| 1300 | PointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1301 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1302 | void *InsertPos = 0; |
| 1303 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1304 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1305 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1306 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1307 | // so fill in the canonical type field. |
| 1308 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1309 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1310 | Canonical = getPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1311 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1312 | // Get the new insert position for the node we care about. |
| 1313 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1314 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1315 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1316 | PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1317 | Types.push_back(New); |
| 1318 | PointerTypes.InsertNode(New, InsertPos); |
| 1319 | return QualType(New, 0); |
| 1320 | } |
| 1321 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1322 | /// getBlockPointerType - Return the uniqued reference to the type for |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1323 | /// a pointer to the specified block. |
| 1324 | QualType ASTContext::getBlockPointerType(QualType T) { |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1325 | assert(T->isFunctionType() && "block of function types only"); |
| 1326 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1327 | // structure. |
| 1328 | llvm::FoldingSetNodeID ID; |
| 1329 | BlockPointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1330 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1331 | void *InsertPos = 0; |
| 1332 | if (BlockPointerType *PT = |
| 1333 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1334 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1335 | |
| 1336 | // 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] | 1337 | // type either so fill in the canonical type field. |
| 1338 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1339 | if (!T.isCanonical()) { |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1340 | Canonical = getBlockPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1341 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1342 | // Get the new insert position for the node we care about. |
| 1343 | BlockPointerType *NewIP = |
| 1344 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1345 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1346 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1347 | BlockPointerType *New |
| 1348 | = new (*this, TypeAlignment) BlockPointerType(T, Canonical); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1349 | Types.push_back(New); |
| 1350 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 1351 | return QualType(New, 0); |
| 1352 | } |
| 1353 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1354 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 1355 | /// lvalue reference to the specified type. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1356 | QualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1357 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1358 | // structure. |
| 1359 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1360 | ReferenceType::Profile(ID, T, SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1361 | |
| 1362 | void *InsertPos = 0; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1363 | if (LValueReferenceType *RT = |
| 1364 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1365 | return QualType(RT, 0); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1366 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1367 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1368 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1369 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1370 | // either, so fill in the canonical type field. |
| 1371 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1372 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
| 1373 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1374 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1375 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1376 | // Get the new insert position for the node we care about. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1377 | LValueReferenceType *NewIP = |
| 1378 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1379 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1382 | LValueReferenceType *New |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1383 | = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, |
| 1384 | SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1385 | Types.push_back(New); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1386 | LValueReferenceTypes.InsertNode(New, InsertPos); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1387 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1388 | return QualType(New, 0); |
| 1389 | } |
| 1390 | |
| 1391 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 1392 | /// rvalue reference to the specified type. |
| 1393 | QualType ASTContext::getRValueReferenceType(QualType T) { |
| 1394 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1395 | // structure. |
| 1396 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1397 | ReferenceType::Profile(ID, T, false); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1398 | |
| 1399 | void *InsertPos = 0; |
| 1400 | if (RValueReferenceType *RT = |
| 1401 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1402 | return QualType(RT, 0); |
| 1403 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1404 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1405 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1406 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1407 | // either, so fill in the canonical type field. |
| 1408 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1409 | if (InnerRef || !T.isCanonical()) { |
| 1410 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1411 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1412 | |
| 1413 | // Get the new insert position for the node we care about. |
| 1414 | RValueReferenceType *NewIP = |
| 1415 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1416 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
| 1417 | } |
| 1418 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1419 | RValueReferenceType *New |
| 1420 | = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1421 | Types.push_back(New); |
| 1422 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1423 | return QualType(New, 0); |
| 1424 | } |
| 1425 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1426 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 1427 | /// member pointer to the specified type, in the specified class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1428 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1429 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1430 | // structure. |
| 1431 | llvm::FoldingSetNodeID ID; |
| 1432 | MemberPointerType::Profile(ID, T, Cls); |
| 1433 | |
| 1434 | void *InsertPos = 0; |
| 1435 | if (MemberPointerType *PT = |
| 1436 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1437 | return QualType(PT, 0); |
| 1438 | |
| 1439 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 1440 | // type either, so fill in the canonical type field. |
| 1441 | QualType Canonical; |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1442 | if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1443 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 1444 | |
| 1445 | // Get the new insert position for the node we care about. |
| 1446 | MemberPointerType *NewIP = |
| 1447 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1448 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
| 1449 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1450 | MemberPointerType *New |
| 1451 | = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1452 | Types.push_back(New); |
| 1453 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 1454 | return QualType(New, 0); |
| 1455 | } |
| 1456 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1457 | /// getConstantArrayType - Return the unique reference to the type for an |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1458 | /// array of the specified element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1459 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1460 | const llvm::APInt &ArySizeIn, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1461 | ArrayType::ArraySizeModifier ASM, |
| 1462 | unsigned EltTypeQuals) { |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1463 | assert((EltTy->isDependentType() || |
| 1464 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && |
Eli Friedman | 587cbdf | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1465 | "Constant array of VLAs is illegal!"); |
| 1466 | |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1467 | // Convert the array size into a canonical width matching the pointer size for |
| 1468 | // the target. |
| 1469 | llvm::APInt ArySize(ArySizeIn); |
| 1470 | ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1471 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1472 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 0be2ef2 | 2009-02-19 17:31:02 +0000 | [diff] [blame] | 1473 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1474 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1475 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1476 | if (ConstantArrayType *ATP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1477 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1478 | return QualType(ATP, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1479 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1480 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1481 | // so fill in the canonical type field. |
| 1482 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1483 | if (!EltTy.isCanonical()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1484 | Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1485 | ASM, EltTypeQuals); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1486 | // Get the new insert position for the node we care about. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1487 | ConstantArrayType *NewIP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1488 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1489 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1490 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1491 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1492 | ConstantArrayType *New = new(*this,TypeAlignment) |
| 1493 | ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1494 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1495 | Types.push_back(New); |
| 1496 | return QualType(New, 0); |
| 1497 | } |
| 1498 | |
Steve Naroff | bdbf7b0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 1499 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 1500 | /// variable array of the specified element type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1501 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 1502 | Expr *NumElts, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1503 | ArrayType::ArraySizeModifier ASM, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1504 | unsigned EltTypeQuals, |
| 1505 | SourceRange Brackets) { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1506 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 1507 | // that have an expression provided for their size. |
| 1508 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1509 | VariableArrayType *New = new(*this, TypeAlignment) |
| 1510 | VariableArrayType(EltTy, QualType(), NumElts, ASM, EltTypeQuals, Brackets); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1511 | |
| 1512 | VariableArrayTypes.push_back(New); |
| 1513 | Types.push_back(New); |
| 1514 | return QualType(New, 0); |
| 1515 | } |
| 1516 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1517 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 1518 | /// the type for a dependently-sized array of the specified element |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1519 | /// type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1520 | QualType ASTContext::getDependentSizedArrayType(QualType EltTy, |
| 1521 | Expr *NumElts, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1522 | ArrayType::ArraySizeModifier ASM, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1523 | unsigned EltTypeQuals, |
| 1524 | SourceRange Brackets) { |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1525 | assert((!NumElts || NumElts->isTypeDependent() || |
| 1526 | NumElts->isValueDependent()) && |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1527 | "Size must be type- or value-dependent!"); |
| 1528 | |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1529 | void *InsertPos = 0; |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1530 | DependentSizedArrayType *Canon = 0; |
| 1531 | |
| 1532 | if (NumElts) { |
| 1533 | // Dependently-sized array types that do not have a specified |
| 1534 | // number of elements will have their sizes deduced from an |
| 1535 | // initializer. |
| 1536 | llvm::FoldingSetNodeID ID; |
| 1537 | DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM, |
| 1538 | EltTypeQuals, NumElts); |
| 1539 | |
| 1540 | Canon = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1541 | } |
| 1542 | |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1543 | DependentSizedArrayType *New; |
| 1544 | if (Canon) { |
| 1545 | // We already have a canonical version of this array type; use it as |
| 1546 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1547 | New = new (*this, TypeAlignment) |
| 1548 | DependentSizedArrayType(*this, EltTy, QualType(Canon, 0), |
| 1549 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1550 | } else { |
| 1551 | QualType CanonEltTy = getCanonicalType(EltTy); |
| 1552 | if (CanonEltTy == EltTy) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1553 | New = new (*this, TypeAlignment) |
| 1554 | DependentSizedArrayType(*this, EltTy, QualType(), |
| 1555 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1556 | |
| 1557 | if (NumElts) |
| 1558 | DependentSizedArrayTypes.InsertNode(New, InsertPos); |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1559 | } else { |
| 1560 | QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts, |
| 1561 | ASM, EltTypeQuals, |
| 1562 | SourceRange()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1563 | New = new (*this, TypeAlignment) |
| 1564 | DependentSizedArrayType(*this, EltTy, Canon, |
| 1565 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1566 | } |
| 1567 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1568 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1569 | Types.push_back(New); |
| 1570 | return QualType(New, 0); |
| 1571 | } |
| 1572 | |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1573 | QualType ASTContext::getIncompleteArrayType(QualType EltTy, |
| 1574 | ArrayType::ArraySizeModifier ASM, |
| 1575 | unsigned EltTypeQuals) { |
| 1576 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 0be2ef2 | 2009-02-19 17:31:02 +0000 | [diff] [blame] | 1577 | IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1578 | |
| 1579 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1580 | if (IncompleteArrayType *ATP = |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1581 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1582 | return QualType(ATP, 0); |
| 1583 | |
| 1584 | // If the element type isn't canonical, this won't be a canonical type |
| 1585 | // either, so fill in the canonical type field. |
| 1586 | QualType Canonical; |
| 1587 | |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1588 | if (!EltTy.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1589 | Canonical = getIncompleteArrayType(getCanonicalType(EltTy), |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1590 | ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1591 | |
| 1592 | // Get the new insert position for the node we care about. |
| 1593 | IncompleteArrayType *NewIP = |
| 1594 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1595 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1596 | } |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1597 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1598 | IncompleteArrayType *New = new (*this, TypeAlignment) |
| 1599 | IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1600 | |
| 1601 | IncompleteArrayTypes.InsertNode(New, InsertPos); |
| 1602 | Types.push_back(New); |
| 1603 | return QualType(New, 0); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1606 | /// getVectorType - Return the unique reference to a vector type of |
| 1607 | /// the specified element type and size. VectorType must be a built-in type. |
| 1608 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1609 | BuiltinType *baseType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1610 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1611 | baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1612 | assert(baseType != 0 && "getVectorType(): Expecting a built-in type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1613 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1614 | // Check if we've already instantiated a vector of this type. |
| 1615 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1616 | VectorType::Profile(ID, vecType, NumElts, Type::Vector); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1617 | void *InsertPos = 0; |
| 1618 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1619 | return QualType(VTP, 0); |
| 1620 | |
| 1621 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1622 | // so fill in the canonical type field. |
| 1623 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1624 | if (!vecType.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1625 | Canonical = getVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1626 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1627 | // Get the new insert position for the node we care about. |
| 1628 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1629 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1630 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1631 | VectorType *New = new (*this, TypeAlignment) |
| 1632 | VectorType(vecType, NumElts, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1633 | VectorTypes.InsertNode(New, InsertPos); |
| 1634 | Types.push_back(New); |
| 1635 | return QualType(New, 0); |
| 1636 | } |
| 1637 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1638 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1639 | /// 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] | 1640 | QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) { |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1641 | BuiltinType *baseType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1642 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1643 | baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1644 | assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1645 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1646 | // Check if we've already instantiated a vector of this type. |
| 1647 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1648 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +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 McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1656 | if (!vecType.isCanonical()) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1657 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1658 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1659 | // Get the new insert position for the node we care about. |
| 1660 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1661 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1662 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1663 | ExtVectorType *New = new (*this, TypeAlignment) |
| 1664 | ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1665 | VectorTypes.InsertNode(New, InsertPos); |
| 1666 | Types.push_back(New); |
| 1667 | return QualType(New, 0); |
| 1668 | } |
| 1669 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1670 | QualType ASTContext::getDependentSizedExtVectorType(QualType vecType, |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1671 | Expr *SizeExpr, |
| 1672 | SourceLocation AttrLoc) { |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1673 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1674 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1675 | SizeExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1676 | |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1677 | void *InsertPos = 0; |
| 1678 | DependentSizedExtVectorType *Canon |
| 1679 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1680 | DependentSizedExtVectorType *New; |
| 1681 | if (Canon) { |
| 1682 | // We already have a canonical version of this array type; use it as |
| 1683 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1684 | New = new (*this, TypeAlignment) |
| 1685 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), |
| 1686 | SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1687 | } else { |
| 1688 | QualType CanonVecTy = getCanonicalType(vecType); |
| 1689 | if (CanonVecTy == vecType) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1690 | New = new (*this, TypeAlignment) |
| 1691 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, |
| 1692 | AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1693 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); |
| 1694 | } else { |
| 1695 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 1696 | SourceLocation()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1697 | New = new (*this, TypeAlignment) |
| 1698 | DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1699 | } |
| 1700 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1701 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1702 | Types.push_back(New); |
| 1703 | return QualType(New, 0); |
| 1704 | } |
| 1705 | |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1706 | static CallingConv getCanonicalCallingConv(CallingConv CC) { |
| 1707 | if (CC == CC_C) |
| 1708 | return CC_Default; |
| 1709 | return CC; |
| 1710 | } |
| 1711 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1712 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1713 | /// |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1714 | QualType ASTContext::getFunctionNoProtoType(QualType ResultTy, bool NoReturn, |
| 1715 | CallingConv CallConv) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1716 | // Unique functions, to guarantee there is only one function of a particular |
| 1717 | // structure. |
| 1718 | llvm::FoldingSetNodeID ID; |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1719 | FunctionNoProtoType::Profile(ID, ResultTy, NoReturn); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1720 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1721 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1722 | if (FunctionNoProtoType *FT = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1723 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1724 | return QualType(FT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1725 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1726 | QualType Canonical; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1727 | if (!ResultTy.isCanonical() || |
| 1728 | getCanonicalCallingConv(CallConv) != CallConv) { |
| 1729 | Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy), NoReturn, |
| 1730 | getCanonicalCallingConv(CallConv)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1731 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1732 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1733 | FunctionNoProtoType *NewIP = |
| 1734 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1735 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1736 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1737 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1738 | FunctionNoProtoType *New = new (*this, TypeAlignment) |
| 1739 | FunctionNoProtoType(ResultTy, Canonical, NoReturn); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1740 | Types.push_back(New); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1741 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1742 | return QualType(New, 0); |
| 1743 | } |
| 1744 | |
| 1745 | /// getFunctionType - Return a normal function type with a typed argument |
| 1746 | /// list. isVariadic indicates whether the argument list includes '...'. |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 1747 | QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray, |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1748 | unsigned NumArgs, bool isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1749 | unsigned TypeQuals, bool hasExceptionSpec, |
| 1750 | bool hasAnyExceptionSpec, unsigned NumExs, |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1751 | const QualType *ExArray, bool NoReturn, |
| 1752 | CallingConv CallConv) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1753 | // Unique functions, to guarantee there is only one function of a particular |
| 1754 | // structure. |
| 1755 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1756 | FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1757 | TypeQuals, hasExceptionSpec, hasAnyExceptionSpec, |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1758 | NumExs, ExArray, NoReturn); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1759 | |
| 1760 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1761 | if (FunctionProtoType *FTP = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1762 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1763 | return QualType(FTP, 0); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1764 | |
| 1765 | // Determine whether the type being created is already canonical or not. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1766 | bool isCanonical = !hasExceptionSpec && ResultTy.isCanonical(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1767 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1768 | if (!ArgArray[i].isCanonicalAsParam()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1769 | isCanonical = false; |
| 1770 | |
| 1771 | // If this type isn't canonical, get the canonical version of it. |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1772 | // The exception spec is not part of the canonical type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1773 | QualType Canonical; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1774 | if (!isCanonical || getCanonicalCallingConv(CallConv) != CallConv) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1775 | llvm::SmallVector<QualType, 16> CanonicalArgs; |
| 1776 | CanonicalArgs.reserve(NumArgs); |
| 1777 | for (unsigned i = 0; i != NumArgs; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1778 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1779 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1780 | Canonical = getFunctionType(getCanonicalType(ResultTy), |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1781 | CanonicalArgs.data(), NumArgs, |
Douglas Gregor | 47259d9 | 2009-08-05 19:03:35 +0000 | [diff] [blame] | 1782 | isVariadic, TypeQuals, false, |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1783 | false, 0, 0, NoReturn, |
| 1784 | getCanonicalCallingConv(CallConv)); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1785 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1786 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1787 | FunctionProtoType *NewIP = |
| 1788 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1789 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1790 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1791 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1792 | // FunctionProtoType objects are allocated with extra bytes after them |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1793 | // for two variable size arrays (for parameter and exception types) at the |
| 1794 | // end of them. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1795 | FunctionProtoType *FTP = |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1796 | (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) + |
| 1797 | NumArgs*sizeof(QualType) + |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1798 | NumExs*sizeof(QualType), TypeAlignment); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1799 | new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1800 | TypeQuals, hasExceptionSpec, hasAnyExceptionSpec, |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1801 | ExArray, NumExs, Canonical, NoReturn, CallConv); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1802 | Types.push_back(FTP); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1803 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1804 | return QualType(FTP, 0); |
| 1805 | } |
| 1806 | |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1807 | /// getTypeDeclType - Return the unique reference to the type for the |
| 1808 | /// specified type declaration. |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1809 | QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) { |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 1810 | assert(Decl && "Passed null for Decl param"); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1811 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1812 | |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 1813 | if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1814 | return getTypedefType(Typedef); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1815 | else if (isa<TemplateTypeParmDecl>(Decl)) { |
| 1816 | assert(false && "Template type parameter types are always available."); |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 1817 | } else if (ObjCInterfaceDecl *ObjCInterface |
| 1818 | = dyn_cast<ObjCInterfaceDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1819 | return getObjCInterfaceType(ObjCInterface); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1820 | |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1821 | if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
Ted Kremenek | 566c2ba | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 1822 | if (PrevDecl) |
| 1823 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
Steve Naroff | f83820b | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1824 | else |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1825 | Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Record); |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 1826 | } else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { |
Ted Kremenek | 566c2ba | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 1827 | if (PrevDecl) |
| 1828 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
Steve Naroff | f83820b | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1829 | else |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1830 | Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Enum); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1831 | } else if (UnresolvedUsingTypenameDecl *Using = |
| 1832 | dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) { |
| 1833 | Decl->TypeForDecl = new (*this, TypeAlignment) UnresolvedUsingType(Using); |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 1834 | } else |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1835 | assert(false && "TypeDecl without a type?"); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1836 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1837 | if (!PrevDecl) Types.push_back(Decl->TypeForDecl); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1838 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1839 | } |
| 1840 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1841 | /// getTypedefType - Return the unique reference to the type for the |
| 1842 | /// specified typename decl. |
| 1843 | QualType ASTContext::getTypedefType(TypedefDecl *Decl) { |
| 1844 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1845 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1846 | QualType Canonical = getCanonicalType(Decl->getUnderlyingType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1847 | Decl->TypeForDecl = new(*this, TypeAlignment) |
| 1848 | TypedefType(Type::Typedef, Decl, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1849 | Types.push_back(Decl->TypeForDecl); |
| 1850 | return QualType(Decl->TypeForDecl, 0); |
| 1851 | } |
| 1852 | |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1853 | /// \brief Retrieve a substitution-result type. |
| 1854 | QualType |
| 1855 | ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, |
| 1856 | QualType Replacement) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1857 | assert(Replacement.isCanonical() |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1858 | && "replacement types must always be canonical"); |
| 1859 | |
| 1860 | llvm::FoldingSetNodeID ID; |
| 1861 | SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); |
| 1862 | void *InsertPos = 0; |
| 1863 | SubstTemplateTypeParmType *SubstParm |
| 1864 | = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1865 | |
| 1866 | if (!SubstParm) { |
| 1867 | SubstParm = new (*this, TypeAlignment) |
| 1868 | SubstTemplateTypeParmType(Parm, Replacement); |
| 1869 | Types.push_back(SubstParm); |
| 1870 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 1871 | } |
| 1872 | |
| 1873 | return QualType(SubstParm, 0); |
| 1874 | } |
| 1875 | |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1876 | /// \brief Retrieve the template type parameter type for a template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1877 | /// parameter or parameter pack with the given depth, index, and (optionally) |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1878 | /// name. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1879 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1880 | bool ParameterPack, |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1881 | IdentifierInfo *Name) { |
| 1882 | llvm::FoldingSetNodeID ID; |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1883 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1884 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1885 | TemplateTypeParmType *TypeParm |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1886 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1887 | |
| 1888 | if (TypeParm) |
| 1889 | return QualType(TypeParm, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1890 | |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1891 | if (Name) { |
| 1892 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1893 | TypeParm = new (*this, TypeAlignment) |
| 1894 | TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon); |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1895 | } else |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1896 | TypeParm = new (*this, TypeAlignment) |
| 1897 | TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1898 | |
| 1899 | Types.push_back(TypeParm); |
| 1900 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 1901 | |
| 1902 | return QualType(TypeParm, 0); |
| 1903 | } |
| 1904 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1905 | QualType |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1906 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1907 | const TemplateArgumentListInfo &Args, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1908 | QualType Canon) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1909 | unsigned NumArgs = Args.size(); |
| 1910 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1911 | llvm::SmallVector<TemplateArgument, 4> ArgVec; |
| 1912 | ArgVec.reserve(NumArgs); |
| 1913 | for (unsigned i = 0; i != NumArgs; ++i) |
| 1914 | ArgVec.push_back(Args[i].getArgument()); |
| 1915 | |
| 1916 | return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, Canon); |
| 1917 | } |
| 1918 | |
| 1919 | QualType |
| 1920 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1921 | const TemplateArgument *Args, |
| 1922 | unsigned NumArgs, |
| 1923 | QualType Canon) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1924 | if (!Canon.isNull()) |
| 1925 | Canon = getCanonicalType(Canon); |
| 1926 | else { |
| 1927 | // Build the canonical template specialization type. |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1928 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); |
| 1929 | llvm::SmallVector<TemplateArgument, 4> CanonArgs; |
| 1930 | CanonArgs.reserve(NumArgs); |
| 1931 | for (unsigned I = 0; I != NumArgs; ++I) |
| 1932 | CanonArgs.push_back(getCanonicalTemplateArgument(Args[I])); |
| 1933 | |
| 1934 | // Determine whether this canonical template specialization type already |
| 1935 | // exists. |
| 1936 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1937 | TemplateSpecializationType::Profile(ID, CanonTemplate, |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 1938 | CanonArgs.data(), NumArgs, *this); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1939 | |
| 1940 | void *InsertPos = 0; |
| 1941 | TemplateSpecializationType *Spec |
| 1942 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1943 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1944 | if (!Spec) { |
| 1945 | // Allocate a new canonical template specialization type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1946 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1947 | sizeof(TemplateArgument) * NumArgs), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1948 | TypeAlignment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1949 | Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate, |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1950 | CanonArgs.data(), NumArgs, |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1951 | Canon); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1952 | Types.push_back(Spec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1953 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1954 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1955 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1956 | if (Canon.isNull()) |
| 1957 | Canon = QualType(Spec, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1958 | assert(Canon->isDependentType() && |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1959 | "Non-dependent template-id type must have a canonical type"); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1960 | } |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 1961 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1962 | // Allocate the (non-canonical) template specialization type, but don't |
| 1963 | // try to unique it: these types typically have location information that |
| 1964 | // we don't unique and don't want to lose. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1965 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1966 | sizeof(TemplateArgument) * NumArgs), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1967 | TypeAlignment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1968 | TemplateSpecializationType *Spec |
| 1969 | = new (Mem) TemplateSpecializationType(*this, Template, Args, NumArgs, |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 1970 | Canon); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1971 | |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1972 | Types.push_back(Spec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1973 | return QualType(Spec, 0); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1974 | } |
| 1975 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1976 | QualType |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1977 | ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS, |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1978 | QualType NamedType) { |
| 1979 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1980 | QualifiedNameType::Profile(ID, NNS, NamedType); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1981 | |
| 1982 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1983 | QualifiedNameType *T |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1984 | = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1985 | if (T) |
| 1986 | return QualType(T, 0); |
| 1987 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1988 | T = new (*this) QualifiedNameType(NNS, NamedType, |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1989 | getCanonicalType(NamedType)); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1990 | Types.push_back(T); |
| 1991 | QualifiedNameTypes.InsertNode(T, InsertPos); |
| 1992 | return QualType(T, 0); |
| 1993 | } |
| 1994 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1995 | QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1996 | const IdentifierInfo *Name, |
| 1997 | QualType Canon) { |
| 1998 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 1999 | |
| 2000 | if (Canon.isNull()) { |
| 2001 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 2002 | if (CanonNNS != NNS) |
| 2003 | Canon = getTypenameType(CanonNNS, Name); |
| 2004 | } |
| 2005 | |
| 2006 | llvm::FoldingSetNodeID ID; |
| 2007 | TypenameType::Profile(ID, NNS, Name); |
| 2008 | |
| 2009 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2010 | TypenameType *T |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2011 | = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2012 | if (T) |
| 2013 | return QualType(T, 0); |
| 2014 | |
| 2015 | T = new (*this) TypenameType(NNS, Name, Canon); |
| 2016 | Types.push_back(T); |
| 2017 | TypenameTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2018 | return QualType(T, 0); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2019 | } |
| 2020 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2021 | QualType |
| 2022 | ASTContext::getTypenameType(NestedNameSpecifier *NNS, |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2023 | const TemplateSpecializationType *TemplateId, |
| 2024 | QualType Canon) { |
| 2025 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 2026 | |
| 2027 | if (Canon.isNull()) { |
| 2028 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 2029 | QualType CanonType = getCanonicalType(QualType(TemplateId, 0)); |
| 2030 | if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) { |
| 2031 | const TemplateSpecializationType *CanonTemplateId |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2032 | = CanonType->getAs<TemplateSpecializationType>(); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2033 | assert(CanonTemplateId && |
| 2034 | "Canonical type must also be a template specialization type"); |
| 2035 | Canon = getTypenameType(CanonNNS, CanonTemplateId); |
| 2036 | } |
| 2037 | } |
| 2038 | |
| 2039 | llvm::FoldingSetNodeID ID; |
| 2040 | TypenameType::Profile(ID, NNS, TemplateId); |
| 2041 | |
| 2042 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2043 | TypenameType *T |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2044 | = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2045 | if (T) |
| 2046 | return QualType(T, 0); |
| 2047 | |
| 2048 | T = new (*this) TypenameType(NNS, TemplateId, Canon); |
| 2049 | Types.push_back(T); |
| 2050 | TypenameTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2051 | return QualType(T, 0); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2052 | } |
| 2053 | |
John McCall | 7da2431 | 2009-09-05 00:15:47 +0000 | [diff] [blame] | 2054 | QualType |
| 2055 | ASTContext::getElaboratedType(QualType UnderlyingType, |
| 2056 | ElaboratedType::TagKind Tag) { |
| 2057 | llvm::FoldingSetNodeID ID; |
| 2058 | ElaboratedType::Profile(ID, UnderlyingType, Tag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2059 | |
John McCall | 7da2431 | 2009-09-05 00:15:47 +0000 | [diff] [blame] | 2060 | void *InsertPos = 0; |
| 2061 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2062 | if (T) |
| 2063 | return QualType(T, 0); |
| 2064 | |
| 2065 | QualType Canon = getCanonicalType(UnderlyingType); |
| 2066 | |
| 2067 | T = new (*this) ElaboratedType(UnderlyingType, Tag, Canon); |
| 2068 | Types.push_back(T); |
| 2069 | ElaboratedTypes.InsertNode(T, InsertPos); |
| 2070 | return QualType(T, 0); |
| 2071 | } |
| 2072 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2073 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 2074 | /// alphabetically. |
| 2075 | static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, |
| 2076 | const ObjCProtocolDecl *RHS) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2077 | return LHS->getDeclName() < RHS->getDeclName(); |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2078 | } |
| 2079 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2080 | static bool areSortedAndUniqued(ObjCProtocolDecl **Protocols, |
| 2081 | unsigned NumProtocols) { |
| 2082 | if (NumProtocols == 0) return true; |
| 2083 | |
| 2084 | for (unsigned i = 1; i != NumProtocols; ++i) |
| 2085 | if (!CmpProtocolNames(Protocols[i-1], Protocols[i])) |
| 2086 | return false; |
| 2087 | return true; |
| 2088 | } |
| 2089 | |
| 2090 | static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols, |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2091 | unsigned &NumProtocols) { |
| 2092 | ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2093 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2094 | // Sort protocols, keyed by name. |
| 2095 | std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); |
| 2096 | |
| 2097 | // Remove duplicates. |
| 2098 | ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); |
| 2099 | NumProtocols = ProtocolsEnd-Protocols; |
| 2100 | } |
| 2101 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2102 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 2103 | /// the given interface decl and the conforming protocol list. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2104 | QualType ASTContext::getObjCObjectPointerType(QualType InterfaceT, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2105 | ObjCProtocolDecl **Protocols, |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2106 | unsigned NumProtocols) { |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2107 | llvm::FoldingSetNodeID ID; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2108 | ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2109 | |
| 2110 | void *InsertPos = 0; |
| 2111 | if (ObjCObjectPointerType *QT = |
| 2112 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2113 | return QualType(QT, 0); |
| 2114 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2115 | // Sort the protocol list alphabetically to canonicalize it. |
| 2116 | QualType Canonical; |
| 2117 | if (!InterfaceT.isCanonical() || |
| 2118 | !areSortedAndUniqued(Protocols, NumProtocols)) { |
| 2119 | if (!areSortedAndUniqued(Protocols, NumProtocols)) { |
| 2120 | llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(NumProtocols); |
| 2121 | unsigned UniqueCount = NumProtocols; |
| 2122 | |
| 2123 | std::copy(Protocols, Protocols + NumProtocols, Sorted.begin()); |
| 2124 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
| 2125 | |
| 2126 | Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT), |
| 2127 | &Sorted[0], UniqueCount); |
| 2128 | } else { |
| 2129 | Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT), |
| 2130 | Protocols, NumProtocols); |
| 2131 | } |
| 2132 | |
| 2133 | // Regenerate InsertPos. |
| 2134 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2135 | } |
| 2136 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2137 | // No Match; |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2138 | ObjCObjectPointerType *QType = new (*this, TypeAlignment) |
Ted Kremenek | 71842cc | 2010-01-21 19:22:34 +0000 | [diff] [blame] | 2139 | ObjCObjectPointerType(*this, Canonical, InterfaceT, Protocols, |
| 2140 | NumProtocols); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2141 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2142 | Types.push_back(QType); |
| 2143 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
| 2144 | return QualType(QType, 0); |
| 2145 | } |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2146 | |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 2147 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 2148 | /// specified ObjC interface decl. The list of protocols is optional. |
| 2149 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl, |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2150 | ObjCProtocolDecl **Protocols, unsigned NumProtocols) { |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2151 | llvm::FoldingSetNodeID ID; |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 2152 | ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2153 | |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2154 | void *InsertPos = 0; |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 2155 | if (ObjCInterfaceType *QT = |
| 2156 | ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2157 | return QualType(QT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2158 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2159 | // Sort the protocol list alphabetically to canonicalize it. |
| 2160 | QualType Canonical; |
| 2161 | if (NumProtocols && !areSortedAndUniqued(Protocols, NumProtocols)) { |
| 2162 | llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(NumProtocols); |
| 2163 | std::copy(Protocols, Protocols + NumProtocols, Sorted.begin()); |
| 2164 | |
| 2165 | unsigned UniqueCount = NumProtocols; |
| 2166 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
| 2167 | |
| 2168 | Canonical = getObjCInterfaceType(Decl, &Sorted[0], UniqueCount); |
| 2169 | |
| 2170 | ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2171 | } |
| 2172 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2173 | ObjCInterfaceType *QType = new (*this, TypeAlignment) |
Ted Kremenek | 71842cc | 2010-01-21 19:22:34 +0000 | [diff] [blame] | 2174 | ObjCInterfaceType(*this, Canonical, const_cast<ObjCInterfaceDecl*>(Decl), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2175 | Protocols, NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2176 | |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2177 | Types.push_back(QType); |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 2178 | ObjCInterfaceTypes.InsertNode(QType, InsertPos); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2179 | return QualType(QType, 0); |
| 2180 | } |
| 2181 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2182 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 2183 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2184 | /// multiple declarations that refer to "typeof(x)" all contain different |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2185 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2186 | /// on canonical type's (which are always unique). |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2187 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2188 | TypeOfExprType *toe; |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2189 | if (tofExpr->isTypeDependent()) { |
| 2190 | llvm::FoldingSetNodeID ID; |
| 2191 | DependentTypeOfExprType::Profile(ID, *this, tofExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2192 | |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2193 | void *InsertPos = 0; |
| 2194 | DependentTypeOfExprType *Canon |
| 2195 | = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2196 | if (Canon) { |
| 2197 | // We already have a "canonical" version of an identical, dependent |
| 2198 | // typeof(expr) type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2199 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2200 | QualType((TypeOfExprType*)Canon, 0)); |
| 2201 | } |
| 2202 | else { |
| 2203 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2204 | Canon |
| 2205 | = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2206 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); |
| 2207 | toe = Canon; |
| 2208 | } |
| 2209 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2210 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2211 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2212 | } |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2213 | Types.push_back(toe); |
| 2214 | return QualType(toe, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2215 | } |
| 2216 | |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2217 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 2218 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 2219 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2220 | /// an issue. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2221 | /// on canonical type's (which are always unique). |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2222 | QualType ASTContext::getTypeOfType(QualType tofType) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2223 | QualType Canonical = getCanonicalType(tofType); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2224 | TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2225 | Types.push_back(tot); |
| 2226 | return QualType(tot, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2227 | } |
| 2228 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2229 | /// getDecltypeForExpr - Given an expr, will return the decltype for that |
| 2230 | /// expression, according to the rules in C++0x [dcl.type.simple]p4 |
| 2231 | static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) { |
Anders Carlsson | a07c33e | 2009-06-25 15:00:34 +0000 | [diff] [blame] | 2232 | if (e->isTypeDependent()) |
| 2233 | return Context.DependentTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2234 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2235 | // If e is an id expression or a class member access, decltype(e) is defined |
| 2236 | // as the type of the entity named by e. |
| 2237 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) { |
| 2238 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) |
| 2239 | return VD->getType(); |
| 2240 | } |
| 2241 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) { |
| 2242 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) |
| 2243 | return FD->getType(); |
| 2244 | } |
| 2245 | // If e is a function call or an invocation of an overloaded operator, |
| 2246 | // (parentheses around e are ignored), decltype(e) is defined as the |
| 2247 | // return type of that function. |
| 2248 | if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens())) |
| 2249 | return CE->getCallReturnType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2250 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2251 | QualType T = e->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2252 | |
| 2253 | // 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] | 2254 | // defined as T&, otherwise decltype(e) is defined as T. |
| 2255 | if (e->isLvalue(Context) == Expr::LV_Valid) |
| 2256 | T = Context.getLValueReferenceType(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2257 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2258 | return T; |
| 2259 | } |
| 2260 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2261 | /// getDecltypeType - Unlike many "get<Type>" functions, we don't unique |
| 2262 | /// DecltypeType AST's. The only motivation to unique these nodes would be |
| 2263 | /// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2264 | /// an issue. This doesn't effect the type checker, since it operates |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2265 | /// on canonical type's (which are always unique). |
| 2266 | QualType ASTContext::getDecltypeType(Expr *e) { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2267 | DecltypeType *dt; |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2268 | if (e->isTypeDependent()) { |
| 2269 | llvm::FoldingSetNodeID ID; |
| 2270 | DependentDecltypeType::Profile(ID, *this, e); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2271 | |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2272 | void *InsertPos = 0; |
| 2273 | DependentDecltypeType *Canon |
| 2274 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2275 | if (Canon) { |
| 2276 | // We already have a "canonical" version of an equivalent, dependent |
| 2277 | // decltype type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2278 | dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy, |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2279 | QualType((DecltypeType*)Canon, 0)); |
| 2280 | } |
| 2281 | else { |
| 2282 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2283 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2284 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); |
| 2285 | dt = Canon; |
| 2286 | } |
| 2287 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2288 | QualType T = getDecltypeForExpr(e, *this); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2289 | dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T)); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2290 | } |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2291 | Types.push_back(dt); |
| 2292 | return QualType(dt, 0); |
| 2293 | } |
| 2294 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2295 | /// getTagDeclType - Return the unique reference to the type for the |
| 2296 | /// specified TagDecl (struct/union/class/enum) decl. |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 2297 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) { |
Ted Kremenek | d778f88 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 2298 | assert (Decl); |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 2299 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 2300 | // away const? mutable? |
| 2301 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2302 | } |
| 2303 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2304 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 2305 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 2306 | /// needs to agree with the definition in <stddef.h>. |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 2307 | CanQualType ASTContext::getSizeType() const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2308 | return getFromTargetType(Target.getSizeType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2309 | } |
| 2310 | |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 2311 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 2312 | /// Used when in C++, as a GCC extension. |
| 2313 | QualType ASTContext::getSignedWCharType() const { |
| 2314 | // FIXME: derive from "Target" ? |
| 2315 | return WCharTy; |
| 2316 | } |
| 2317 | |
| 2318 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 2319 | /// Used when in C++, as a GCC extension. |
| 2320 | QualType ASTContext::getUnsignedWCharType() const { |
| 2321 | // FIXME: derive from "Target" ? |
| 2322 | return UnsignedIntTy; |
| 2323 | } |
| 2324 | |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 2325 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) |
| 2326 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 2327 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2328 | return getFromTargetType(Target.getPtrDiffType(0)); |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 2329 | } |
| 2330 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2331 | //===----------------------------------------------------------------------===// |
| 2332 | // Type Operators |
| 2333 | //===----------------------------------------------------------------------===// |
| 2334 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2335 | CanQualType ASTContext::getCanonicalParamType(QualType T) { |
| 2336 | // Push qualifiers into arrays, and then discard any remaining |
| 2337 | // qualifiers. |
| 2338 | T = getCanonicalType(T); |
| 2339 | const Type *Ty = T.getTypePtr(); |
| 2340 | |
| 2341 | QualType Result; |
| 2342 | if (isa<ArrayType>(Ty)) { |
| 2343 | Result = getArrayDecayedType(QualType(Ty,0)); |
| 2344 | } else if (isa<FunctionType>(Ty)) { |
| 2345 | Result = getPointerType(QualType(Ty, 0)); |
| 2346 | } else { |
| 2347 | Result = QualType(Ty, 0); |
| 2348 | } |
| 2349 | |
| 2350 | return CanQualType::CreateUnsafe(Result); |
| 2351 | } |
| 2352 | |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 2353 | /// getCanonicalType - Return the canonical (structural) type corresponding to |
| 2354 | /// the specified potentially non-canonical type. The non-canonical version |
| 2355 | /// of a type may have many "decorated" versions of types. Decorators can |
| 2356 | /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed |
| 2357 | /// to be free of any of these, allowing two canonical types to be compared |
| 2358 | /// for exact equality with a simple pointer comparison. |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2359 | CanQualType ASTContext::getCanonicalType(QualType T) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2360 | QualifierCollector Quals; |
| 2361 | const Type *Ptr = Quals.strip(T); |
| 2362 | QualType CanType = Ptr->getCanonicalTypeInternal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2363 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2364 | // The canonical internal type will be the canonical type *except* |
| 2365 | // that we push type qualifiers down through array types. |
| 2366 | |
| 2367 | // If there are no new qualifiers to push down, stop here. |
| 2368 | if (!Quals.hasQualifiers()) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2369 | return CanQualType::CreateUnsafe(CanType); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2370 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2371 | // If the type qualifiers are on an array type, get the canonical |
| 2372 | // type of the array with the qualifiers applied to the element |
| 2373 | // type. |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2374 | ArrayType *AT = dyn_cast<ArrayType>(CanType); |
| 2375 | if (!AT) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2376 | return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2377 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2378 | // Get the canonical version of the element with the extra qualifiers on it. |
| 2379 | // This can recursively sink qualifiers through multiple levels of arrays. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2380 | QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2381 | NewEltTy = getCanonicalType(NewEltTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2382 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2383 | if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2384 | return CanQualType::CreateUnsafe( |
| 2385 | getConstantArrayType(NewEltTy, CAT->getSize(), |
| 2386 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2387 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2388 | if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2389 | return CanQualType::CreateUnsafe( |
| 2390 | getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2391 | IAT->getIndexTypeCVRQualifiers())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2392 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2393 | if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2394 | return CanQualType::CreateUnsafe( |
| 2395 | getDependentSizedArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2396 | DSAT->getSizeExpr() ? |
| 2397 | DSAT->getSizeExpr()->Retain() : 0, |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2398 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2399 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 87a924e | 2009-10-30 22:56:57 +0000 | [diff] [blame] | 2400 | DSAT->getBracketsRange())->getCanonicalTypeInternal()); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2401 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2402 | VariableArrayType *VAT = cast<VariableArrayType>(AT); |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2403 | return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2404 | VAT->getSizeExpr() ? |
| 2405 | VAT->getSizeExpr()->Retain() : 0, |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2406 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2407 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2408 | VAT->getBracketsRange())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2409 | } |
| 2410 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2411 | QualType ASTContext::getUnqualifiedArrayType(QualType T, |
| 2412 | Qualifiers &Quals) { |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2413 | Quals = T.getQualifiers(); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2414 | if (!isa<ArrayType>(T)) { |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2415 | return T.getUnqualifiedType(); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2416 | } |
| 2417 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2418 | const ArrayType *AT = cast<ArrayType>(T); |
| 2419 | QualType Elt = AT->getElementType(); |
Zhongxing Xu | c1ae0a8 | 2010-01-05 08:15:06 +0000 | [diff] [blame] | 2420 | QualType UnqualElt = getUnqualifiedArrayType(Elt, Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2421 | if (Elt == UnqualElt) |
| 2422 | return T; |
| 2423 | |
| 2424 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T)) { |
| 2425 | return getConstantArrayType(UnqualElt, CAT->getSize(), |
| 2426 | CAT->getSizeModifier(), 0); |
| 2427 | } |
| 2428 | |
| 2429 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(T)) { |
| 2430 | return getIncompleteArrayType(UnqualElt, IAT->getSizeModifier(), 0); |
| 2431 | } |
| 2432 | |
| 2433 | const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(T); |
| 2434 | return getDependentSizedArrayType(UnqualElt, DSAT->getSizeExpr()->Retain(), |
| 2435 | DSAT->getSizeModifier(), 0, |
| 2436 | SourceRange()); |
| 2437 | } |
| 2438 | |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2439 | DeclarationName ASTContext::getNameForTemplate(TemplateName Name) { |
| 2440 | if (TemplateDecl *TD = Name.getAsTemplateDecl()) |
| 2441 | return TD->getDeclName(); |
| 2442 | |
| 2443 | if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) { |
| 2444 | if (DTN->isIdentifier()) { |
| 2445 | return DeclarationNames.getIdentifier(DTN->getIdentifier()); |
| 2446 | } else { |
| 2447 | return DeclarationNames.getCXXOperatorName(DTN->getOperator()); |
| 2448 | } |
| 2449 | } |
| 2450 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 2451 | OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate(); |
| 2452 | assert(Storage); |
| 2453 | return (*Storage->begin())->getDeclName(); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2454 | } |
| 2455 | |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2456 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) { |
| 2457 | // If this template name refers to a template, the canonical |
| 2458 | // template name merely stores the template itself. |
| 2459 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 2460 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2461 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 2462 | assert(!Name.getAsOverloadedTemplate()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2463 | |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2464 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 2465 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 2466 | return DTN->CanonicalTemplateName; |
| 2467 | } |
| 2468 | |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 2469 | bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) { |
| 2470 | X = getCanonicalTemplateName(X); |
| 2471 | Y = getCanonicalTemplateName(Y); |
| 2472 | return X.getAsVoidPointer() == Y.getAsVoidPointer(); |
| 2473 | } |
| 2474 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2475 | TemplateArgument |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2476 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) { |
| 2477 | switch (Arg.getKind()) { |
| 2478 | case TemplateArgument::Null: |
| 2479 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2480 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2481 | case TemplateArgument::Expression: |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2482 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2483 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2484 | case TemplateArgument::Declaration: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2485 | return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2486 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2487 | case TemplateArgument::Template: |
| 2488 | return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate())); |
| 2489 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2490 | case TemplateArgument::Integral: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2491 | return TemplateArgument(*Arg.getAsIntegral(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2492 | getCanonicalType(Arg.getIntegralType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2493 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2494 | case TemplateArgument::Type: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2495 | return TemplateArgument(getCanonicalType(Arg.getAsType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2496 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2497 | case TemplateArgument::Pack: { |
| 2498 | // FIXME: Allocate in ASTContext |
| 2499 | TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()]; |
| 2500 | unsigned Idx = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2501 | for (TemplateArgument::pack_iterator A = Arg.pack_begin(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2502 | AEnd = Arg.pack_end(); |
| 2503 | A != AEnd; (void)++A, ++Idx) |
| 2504 | CanonArgs[Idx] = getCanonicalTemplateArgument(*A); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2505 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2506 | TemplateArgument Result; |
| 2507 | Result.setArgumentPack(CanonArgs, Arg.pack_size(), false); |
| 2508 | return Result; |
| 2509 | } |
| 2510 | } |
| 2511 | |
| 2512 | // Silence GCC warning |
| 2513 | assert(false && "Unhandled template argument kind"); |
| 2514 | return TemplateArgument(); |
| 2515 | } |
| 2516 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2517 | NestedNameSpecifier * |
| 2518 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2519 | if (!NNS) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2520 | return 0; |
| 2521 | |
| 2522 | switch (NNS->getKind()) { |
| 2523 | case NestedNameSpecifier::Identifier: |
| 2524 | // Canonicalize the prefix but keep the identifier the same. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2525 | return NestedNameSpecifier::Create(*this, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2526 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 2527 | NNS->getAsIdentifier()); |
| 2528 | |
| 2529 | case NestedNameSpecifier::Namespace: |
| 2530 | // A namespace is canonical; build a nested-name-specifier with |
| 2531 | // this namespace and no prefix. |
| 2532 | return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace()); |
| 2533 | |
| 2534 | case NestedNameSpecifier::TypeSpec: |
| 2535 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 2536 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2537 | return NestedNameSpecifier::Create(*this, 0, |
| 2538 | NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2539 | T.getTypePtr()); |
| 2540 | } |
| 2541 | |
| 2542 | case NestedNameSpecifier::Global: |
| 2543 | // The global specifier is canonical and unique. |
| 2544 | return NNS; |
| 2545 | } |
| 2546 | |
| 2547 | // Required to silence a GCC warning |
| 2548 | return 0; |
| 2549 | } |
| 2550 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2551 | |
| 2552 | const ArrayType *ASTContext::getAsArrayType(QualType T) { |
| 2553 | // Handle the non-qualified case efficiently. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2554 | if (!T.hasLocalQualifiers()) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2555 | // Handle the common positive case fast. |
| 2556 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 2557 | return AT; |
| 2558 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2559 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2560 | // Handle the common negative case fast. |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2561 | QualType CType = T->getCanonicalTypeInternal(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2562 | if (!isa<ArrayType>(CType)) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2563 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2564 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2565 | // Apply any qualifiers from the array type to the element type. This |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2566 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 2567 | // 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] | 2568 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2569 | // If we get here, we either have type qualifiers on the type, or we have |
| 2570 | // 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] | 2571 | // we must propagate them down into the element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2572 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2573 | QualifierCollector Qs; |
| 2574 | const Type *Ty = Qs.strip(T.getDesugaredType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2575 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2576 | // If we have a simple case, just return now. |
| 2577 | const ArrayType *ATy = dyn_cast<ArrayType>(Ty); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2578 | if (ATy == 0 || Qs.empty()) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2579 | return ATy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2580 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2581 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 2582 | // qualifiers into the array element type and return a new array type. |
| 2583 | // Get the canonical version of the element with the extra qualifiers on it. |
| 2584 | // This can recursively sink qualifiers through multiple levels of arrays. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2585 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2586 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2587 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 2588 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 2589 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2590 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2591 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 2592 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 2593 | IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2594 | IAT->getIndexTypeCVRQualifiers())); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2595 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2596 | if (const DependentSizedArrayType *DSAT |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2597 | = dyn_cast<DependentSizedArrayType>(ATy)) |
| 2598 | return cast<ArrayType>( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2599 | getDependentSizedArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2600 | DSAT->getSizeExpr() ? |
| 2601 | DSAT->getSizeExpr()->Retain() : 0, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2602 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2603 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2604 | DSAT->getBracketsRange())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2605 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2606 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2607 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2608 | VAT->getSizeExpr() ? |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2609 | VAT->getSizeExpr()->Retain() : 0, |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2610 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2611 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2612 | VAT->getBracketsRange())); |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 2613 | } |
| 2614 | |
| 2615 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2616 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 2617 | /// specified array type to a pointer. This operation is non-trivial when |
| 2618 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 2619 | /// this returns a pointer to a properly qualified element of the array. |
| 2620 | /// |
| 2621 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
| 2622 | QualType ASTContext::getArrayDecayedType(QualType Ty) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2623 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 2624 | // typedefs in the element type of the array. This also handles propagation |
| 2625 | // of type qualifiers from the array type into the element type if present |
| 2626 | // (C99 6.7.3p8). |
| 2627 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 2628 | assert(PrettyArrayType && "Not an array type!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2629 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2630 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2631 | |
| 2632 | // int x[restrict 4] -> int *restrict |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2633 | return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2634 | } |
| 2635 | |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2636 | QualType ASTContext::getBaseElementType(QualType QT) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2637 | QualifierCollector Qs; |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2638 | while (true) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2639 | const Type *UT = Qs.strip(QT); |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2640 | if (const ArrayType *AT = getAsArrayType(QualType(UT,0))) { |
| 2641 | QT = AT->getElementType(); |
Mike Stump | 6dcbc29 | 2009-07-25 23:24:03 +0000 | [diff] [blame] | 2642 | } else { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2643 | return Qs.apply(QT); |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2644 | } |
| 2645 | } |
| 2646 | } |
| 2647 | |
Anders Carlsson | fbbce49 | 2009-09-25 01:23:32 +0000 | [diff] [blame] | 2648 | QualType ASTContext::getBaseElementType(const ArrayType *AT) { |
| 2649 | QualType ElemTy = AT->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2650 | |
Anders Carlsson | fbbce49 | 2009-09-25 01:23:32 +0000 | [diff] [blame] | 2651 | if (const ArrayType *AT = getAsArrayType(ElemTy)) |
| 2652 | return getBaseElementType(AT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2653 | |
Anders Carlsson | 6183a99 | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 2654 | return ElemTy; |
| 2655 | } |
| 2656 | |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 2657 | /// getConstantArrayElementCount - Returns number of constant array elements. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2658 | uint64_t |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 2659 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
| 2660 | uint64_t ElementCount = 1; |
| 2661 | do { |
| 2662 | ElementCount *= CA->getSize().getZExtValue(); |
| 2663 | CA = dyn_cast<ConstantArrayType>(CA->getElementType()); |
| 2664 | } while (CA); |
| 2665 | return ElementCount; |
| 2666 | } |
| 2667 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2668 | /// getFloatingRank - Return a relative rank for floating point types. |
| 2669 | /// 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] | 2670 | static FloatingRank getFloatingRank(QualType T) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2671 | if (const ComplexType *CT = T->getAs<ComplexType>()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2672 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2673 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2674 | assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"); |
| 2675 | switch (T->getAs<BuiltinType>()->getKind()) { |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2676 | default: assert(0 && "getFloatingRank(): not a floating type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2677 | case BuiltinType::Float: return FloatRank; |
| 2678 | case BuiltinType::Double: return DoubleRank; |
| 2679 | case BuiltinType::LongDouble: return LongDoubleRank; |
| 2680 | } |
| 2681 | } |
| 2682 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2683 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 2684 | /// point or a complex type (based on typeDomain/typeSize). |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 2685 | /// 'typeDomain' is a real floating point or complex type. |
| 2686 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 2687 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 2688 | QualType Domain) const { |
| 2689 | FloatingRank EltRank = getFloatingRank(Size); |
| 2690 | if (Domain->isComplexType()) { |
| 2691 | switch (EltRank) { |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 2692 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 2693 | case FloatRank: return FloatComplexTy; |
| 2694 | case DoubleRank: return DoubleComplexTy; |
| 2695 | case LongDoubleRank: return LongDoubleComplexTy; |
| 2696 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2697 | } |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 2698 | |
| 2699 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 2700 | switch (EltRank) { |
| 2701 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
| 2702 | case FloatRank: return FloatTy; |
| 2703 | case DoubleRank: return DoubleTy; |
| 2704 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 2705 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2706 | } |
| 2707 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2708 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 2709 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 2710 | /// '_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] | 2711 | /// LHS < RHS, return -1. |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2712 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) { |
| 2713 | FloatingRank LHSR = getFloatingRank(LHS); |
| 2714 | FloatingRank RHSR = getFloatingRank(RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2715 | |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2716 | if (LHSR == RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 2717 | return 0; |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2718 | if (LHSR > RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 2719 | return 1; |
| 2720 | return -1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2721 | } |
| 2722 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2723 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 2724 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 2725 | /// or if it is not canonicalized. |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2726 | unsigned ASTContext::getIntegerRank(Type *T) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2727 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2728 | if (EnumType* ET = dyn_cast<EnumType>(T)) |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 2729 | T = ET->getDecl()->getPromotionType().getTypePtr(); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2730 | |
Eli Friedman | a342675 | 2009-07-05 23:44:27 +0000 | [diff] [blame] | 2731 | if (T->isSpecificBuiltinType(BuiltinType::WChar)) |
| 2732 | T = getFromTargetType(Target.getWCharType()).getTypePtr(); |
| 2733 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2734 | if (T->isSpecificBuiltinType(BuiltinType::Char16)) |
| 2735 | T = getFromTargetType(Target.getChar16Type()).getTypePtr(); |
| 2736 | |
| 2737 | if (T->isSpecificBuiltinType(BuiltinType::Char32)) |
| 2738 | T = getFromTargetType(Target.getChar32Type()).getTypePtr(); |
| 2739 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2740 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2741 | default: assert(0 && "getIntegerRank(): not a built-in integer"); |
| 2742 | case BuiltinType::Bool: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2743 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2744 | case BuiltinType::Char_S: |
| 2745 | case BuiltinType::Char_U: |
| 2746 | case BuiltinType::SChar: |
| 2747 | case BuiltinType::UChar: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2748 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2749 | case BuiltinType::Short: |
| 2750 | case BuiltinType::UShort: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2751 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2752 | case BuiltinType::Int: |
| 2753 | case BuiltinType::UInt: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2754 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2755 | case BuiltinType::Long: |
| 2756 | case BuiltinType::ULong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2757 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2758 | case BuiltinType::LongLong: |
| 2759 | case BuiltinType::ULongLong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2760 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 2761 | case BuiltinType::Int128: |
| 2762 | case BuiltinType::UInt128: |
| 2763 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2764 | } |
| 2765 | } |
| 2766 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 2767 | /// \brief Whether this is a promotable bitfield reference according |
| 2768 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
| 2769 | /// |
| 2770 | /// \returns the type this bit-field will promote to, or NULL if no |
| 2771 | /// promotion occurs. |
| 2772 | QualType ASTContext::isPromotableBitField(Expr *E) { |
| 2773 | FieldDecl *Field = E->getBitField(); |
| 2774 | if (!Field) |
| 2775 | return QualType(); |
| 2776 | |
| 2777 | QualType FT = Field->getType(); |
| 2778 | |
| 2779 | llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this); |
| 2780 | uint64_t BitWidth = BitWidthAP.getZExtValue(); |
| 2781 | uint64_t IntSize = getTypeSize(IntTy); |
| 2782 | // GCC extension compatibility: if the bit-field size is less than or equal |
| 2783 | // to the size of int, it gets promoted no matter what its type is. |
| 2784 | // For instance, unsigned long bf : 4 gets promoted to signed int. |
| 2785 | if (BitWidth < IntSize) |
| 2786 | return IntTy; |
| 2787 | |
| 2788 | if (BitWidth == IntSize) |
| 2789 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
| 2790 | |
| 2791 | // Types bigger than int are not subject to promotions, and therefore act |
| 2792 | // like the base type. |
| 2793 | // FIXME: This doesn't quite match what gcc does, but what gcc does here |
| 2794 | // is ridiculous. |
| 2795 | return QualType(); |
| 2796 | } |
| 2797 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 2798 | /// getPromotedIntegerType - Returns the type that Promotable will |
| 2799 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
| 2800 | /// integer type. |
| 2801 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) { |
| 2802 | assert(!Promotable.isNull()); |
| 2803 | assert(Promotable->isPromotableIntegerType()); |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 2804 | if (const EnumType *ET = Promotable->getAs<EnumType>()) |
| 2805 | return ET->getDecl()->getPromotionType(); |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 2806 | if (Promotable->isSignedIntegerType()) |
| 2807 | return IntTy; |
| 2808 | uint64_t PromotableSize = getTypeSize(Promotable); |
| 2809 | uint64_t IntSize = getTypeSize(IntTy); |
| 2810 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
| 2811 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
| 2812 | } |
| 2813 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2814 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2815 | /// 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] | 2816 | /// LHS < RHS, return -1. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2817 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2818 | Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 2819 | Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2820 | if (LHSC == RHSC) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2821 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2822 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 2823 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2824 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2825 | unsigned LHSRank = getIntegerRank(LHSC); |
| 2826 | unsigned RHSRank = getIntegerRank(RHSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2827 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2828 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 2829 | if (LHSRank == RHSRank) return 0; |
| 2830 | return LHSRank > RHSRank ? 1 : -1; |
| 2831 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2832 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2833 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 2834 | if (LHSUnsigned) { |
| 2835 | // If the unsigned [LHS] type is larger, return it. |
| 2836 | if (LHSRank >= RHSRank) |
| 2837 | return 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2838 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2839 | // If the signed type can represent all values of the unsigned type, it |
| 2840 | // 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] | 2841 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2842 | return -1; |
| 2843 | } |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2844 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2845 | // If the unsigned [RHS] type is larger, return it. |
| 2846 | if (RHSRank >= LHSRank) |
| 2847 | return -1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2848 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2849 | // If the signed type can represent all values of the unsigned type, it |
| 2850 | // 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] | 2851 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2852 | return 1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2853 | } |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2854 | |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 2855 | static RecordDecl * |
| 2856 | CreateRecordDecl(ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC, |
| 2857 | SourceLocation L, IdentifierInfo *Id) { |
| 2858 | if (Ctx.getLangOptions().CPlusPlus) |
| 2859 | return CXXRecordDecl::Create(Ctx, TK, DC, L, Id); |
| 2860 | else |
| 2861 | return RecordDecl::Create(Ctx, TK, DC, L, Id); |
| 2862 | } |
| 2863 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2864 | // getCFConstantStringType - Return the type used for constant CFStrings. |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2865 | QualType ASTContext::getCFConstantStringType() { |
| 2866 | if (!CFConstantStringTypeDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2867 | CFConstantStringTypeDecl = |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 2868 | CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
| 2869 | &Idents.get("NSConstantString")); |
| 2870 | |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 2871 | QualType FieldTypes[4]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2872 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2873 | // const int *isa; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2874 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 2875 | // int flags; |
| 2876 | FieldTypes[1] = IntTy; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2877 | // const char *str; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2878 | FieldTypes[2] = getPointerType(CharTy.withConst()); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2879 | // long length; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2880 | FieldTypes[3] = LongTy; |
| 2881 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2882 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2883 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2884 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2885 | SourceLocation(), 0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2886 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2887 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2888 | /*Mutable=*/false); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2889 | CFConstantStringTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2890 | } |
| 2891 | |
| 2892 | CFConstantStringTypeDecl->completeDefinition(*this); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2893 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2894 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2895 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 8467583 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 2896 | } |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 2897 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 2898 | void ASTContext::setCFConstantStringType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2899 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 2900 | assert(Rec && "Invalid CFConstantStringType"); |
| 2901 | CFConstantStringTypeDecl = Rec->getDecl(); |
| 2902 | } |
| 2903 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2904 | QualType ASTContext::getObjCFastEnumerationStateType() { |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2905 | if (!ObjCFastEnumerationStateTypeDecl) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2906 | ObjCFastEnumerationStateTypeDecl = |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 2907 | CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
| 2908 | &Idents.get("__objcFastEnumerationState")); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2909 | |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2910 | QualType FieldTypes[] = { |
| 2911 | UnsignedLongTy, |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2912 | getPointerType(ObjCIdTypedefType), |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2913 | getPointerType(UnsignedLongTy), |
| 2914 | getConstantArrayType(UnsignedLongTy, |
| 2915 | llvm::APInt(32, 5), ArrayType::Normal, 0) |
| 2916 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2917 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2918 | for (size_t i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2919 | FieldDecl *Field = FieldDecl::Create(*this, |
| 2920 | ObjCFastEnumerationStateTypeDecl, |
| 2921 | SourceLocation(), 0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2922 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2923 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2924 | /*Mutable=*/false); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2925 | ObjCFastEnumerationStateTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2926 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2927 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2928 | ObjCFastEnumerationStateTypeDecl->completeDefinition(*this); |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2929 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2930 | |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2931 | return getTagDeclType(ObjCFastEnumerationStateTypeDecl); |
| 2932 | } |
| 2933 | |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2934 | QualType ASTContext::getBlockDescriptorType() { |
| 2935 | if (BlockDescriptorType) |
| 2936 | return getTagDeclType(BlockDescriptorType); |
| 2937 | |
| 2938 | RecordDecl *T; |
| 2939 | // FIXME: Needs the FlagAppleBlock bit. |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 2940 | T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
| 2941 | &Idents.get("__block_descriptor")); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2942 | |
| 2943 | QualType FieldTypes[] = { |
| 2944 | UnsignedLongTy, |
| 2945 | UnsignedLongTy, |
| 2946 | }; |
| 2947 | |
| 2948 | const char *FieldNames[] = { |
| 2949 | "reserved", |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 2950 | "Size" |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2951 | }; |
| 2952 | |
| 2953 | for (size_t i = 0; i < 2; ++i) { |
| 2954 | FieldDecl *Field = FieldDecl::Create(*this, |
| 2955 | T, |
| 2956 | SourceLocation(), |
| 2957 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2958 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2959 | /*BitWidth=*/0, |
| 2960 | /*Mutable=*/false); |
| 2961 | T->addDecl(Field); |
| 2962 | } |
| 2963 | |
| 2964 | T->completeDefinition(*this); |
| 2965 | |
| 2966 | BlockDescriptorType = T; |
| 2967 | |
| 2968 | return getTagDeclType(BlockDescriptorType); |
| 2969 | } |
| 2970 | |
| 2971 | void ASTContext::setBlockDescriptorType(QualType T) { |
| 2972 | const RecordType *Rec = T->getAs<RecordType>(); |
| 2973 | assert(Rec && "Invalid BlockDescriptorType"); |
| 2974 | BlockDescriptorType = Rec->getDecl(); |
| 2975 | } |
| 2976 | |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 2977 | QualType ASTContext::getBlockDescriptorExtendedType() { |
| 2978 | if (BlockDescriptorExtendedType) |
| 2979 | return getTagDeclType(BlockDescriptorExtendedType); |
| 2980 | |
| 2981 | RecordDecl *T; |
| 2982 | // FIXME: Needs the FlagAppleBlock bit. |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 2983 | T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
| 2984 | &Idents.get("__block_descriptor_withcopydispose")); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 2985 | |
| 2986 | QualType FieldTypes[] = { |
| 2987 | UnsignedLongTy, |
| 2988 | UnsignedLongTy, |
| 2989 | getPointerType(VoidPtrTy), |
| 2990 | getPointerType(VoidPtrTy) |
| 2991 | }; |
| 2992 | |
| 2993 | const char *FieldNames[] = { |
| 2994 | "reserved", |
| 2995 | "Size", |
| 2996 | "CopyFuncPtr", |
| 2997 | "DestroyFuncPtr" |
| 2998 | }; |
| 2999 | |
| 3000 | for (size_t i = 0; i < 4; ++i) { |
| 3001 | FieldDecl *Field = FieldDecl::Create(*this, |
| 3002 | T, |
| 3003 | SourceLocation(), |
| 3004 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3005 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3006 | /*BitWidth=*/0, |
| 3007 | /*Mutable=*/false); |
| 3008 | T->addDecl(Field); |
| 3009 | } |
| 3010 | |
| 3011 | T->completeDefinition(*this); |
| 3012 | |
| 3013 | BlockDescriptorExtendedType = T; |
| 3014 | |
| 3015 | return getTagDeclType(BlockDescriptorExtendedType); |
| 3016 | } |
| 3017 | |
| 3018 | void ASTContext::setBlockDescriptorExtendedType(QualType T) { |
| 3019 | const RecordType *Rec = T->getAs<RecordType>(); |
| 3020 | assert(Rec && "Invalid BlockDescriptorType"); |
| 3021 | BlockDescriptorExtendedType = Rec->getDecl(); |
| 3022 | } |
| 3023 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3024 | bool ASTContext::BlockRequiresCopying(QualType Ty) { |
| 3025 | if (Ty->isBlockPointerType()) |
| 3026 | return true; |
| 3027 | if (isObjCNSObjectType(Ty)) |
| 3028 | return true; |
| 3029 | if (Ty->isObjCObjectPointerType()) |
| 3030 | return true; |
| 3031 | return false; |
| 3032 | } |
| 3033 | |
| 3034 | QualType ASTContext::BuildByRefType(const char *DeclName, QualType Ty) { |
| 3035 | // type = struct __Block_byref_1_X { |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3036 | // void *__isa; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3037 | // struct __Block_byref_1_X *__forwarding; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3038 | // unsigned int __flags; |
| 3039 | // unsigned int __size; |
Mike Stump | 38e1627 | 2009-10-21 22:01:24 +0000 | [diff] [blame] | 3040 | // void *__copy_helper; // as needed |
| 3041 | // void *__destroy_help // as needed |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3042 | // int X; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3043 | // } * |
| 3044 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3045 | bool HasCopyAndDispose = BlockRequiresCopying(Ty); |
| 3046 | |
| 3047 | // FIXME: Move up |
Fariborz Jahanian | 4d0d85c | 2009-10-24 00:16:42 +0000 | [diff] [blame] | 3048 | static unsigned int UniqueBlockByRefTypeID = 0; |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 3049 | llvm::SmallString<36> Name; |
| 3050 | llvm::raw_svector_ostream(Name) << "__Block_byref_" << |
| 3051 | ++UniqueBlockByRefTypeID << '_' << DeclName; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3052 | RecordDecl *T; |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3053 | T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
| 3054 | &Idents.get(Name.str())); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3055 | T->startDefinition(); |
| 3056 | QualType Int32Ty = IntTy; |
| 3057 | assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported"); |
| 3058 | QualType FieldTypes[] = { |
| 3059 | getPointerType(VoidPtrTy), |
| 3060 | getPointerType(getTagDeclType(T)), |
| 3061 | Int32Ty, |
| 3062 | Int32Ty, |
| 3063 | getPointerType(VoidPtrTy), |
| 3064 | getPointerType(VoidPtrTy), |
| 3065 | Ty |
| 3066 | }; |
| 3067 | |
| 3068 | const char *FieldNames[] = { |
| 3069 | "__isa", |
| 3070 | "__forwarding", |
| 3071 | "__flags", |
| 3072 | "__size", |
| 3073 | "__copy_helper", |
| 3074 | "__destroy_helper", |
| 3075 | DeclName, |
| 3076 | }; |
| 3077 | |
| 3078 | for (size_t i = 0; i < 7; ++i) { |
| 3079 | if (!HasCopyAndDispose && i >=4 && i <= 5) |
| 3080 | continue; |
| 3081 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
| 3082 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3083 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3084 | /*BitWidth=*/0, /*Mutable=*/false); |
| 3085 | T->addDecl(Field); |
| 3086 | } |
| 3087 | |
| 3088 | T->completeDefinition(*this); |
| 3089 | |
| 3090 | return getPointerType(getTagDeclType(T)); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3091 | } |
| 3092 | |
| 3093 | |
| 3094 | QualType ASTContext::getBlockParmType( |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3095 | bool BlockHasCopyDispose, |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3096 | llvm::SmallVector<const Expr *, 8> &BlockDeclRefDecls) { |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3097 | // FIXME: Move up |
Fariborz Jahanian | 4d0d85c | 2009-10-24 00:16:42 +0000 | [diff] [blame] | 3098 | static unsigned int UniqueBlockParmTypeID = 0; |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 3099 | llvm::SmallString<36> Name; |
| 3100 | llvm::raw_svector_ostream(Name) << "__block_literal_" |
| 3101 | << ++UniqueBlockParmTypeID; |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3102 | RecordDecl *T; |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3103 | T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
| 3104 | &Idents.get(Name.str())); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3105 | QualType FieldTypes[] = { |
| 3106 | getPointerType(VoidPtrTy), |
| 3107 | IntTy, |
| 3108 | IntTy, |
| 3109 | getPointerType(VoidPtrTy), |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3110 | (BlockHasCopyDispose ? |
| 3111 | getPointerType(getBlockDescriptorExtendedType()) : |
| 3112 | getPointerType(getBlockDescriptorType())) |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3113 | }; |
| 3114 | |
| 3115 | const char *FieldNames[] = { |
| 3116 | "__isa", |
| 3117 | "__flags", |
| 3118 | "__reserved", |
| 3119 | "__FuncPtr", |
| 3120 | "__descriptor" |
| 3121 | }; |
| 3122 | |
| 3123 | for (size_t i = 0; i < 5; ++i) { |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3124 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3125 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3126 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3127 | /*BitWidth=*/0, /*Mutable=*/false); |
| 3128 | T->addDecl(Field); |
| 3129 | } |
| 3130 | |
| 3131 | for (size_t i = 0; i < BlockDeclRefDecls.size(); ++i) { |
| 3132 | const Expr *E = BlockDeclRefDecls[i]; |
| 3133 | const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); |
| 3134 | clang::IdentifierInfo *Name = 0; |
| 3135 | if (BDRE) { |
| 3136 | const ValueDecl *D = BDRE->getDecl(); |
| 3137 | Name = &Idents.get(D->getName()); |
| 3138 | } |
| 3139 | QualType FieldType = E->getType(); |
| 3140 | |
| 3141 | if (BDRE && BDRE->isByRef()) |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3142 | FieldType = BuildByRefType(BDRE->getDecl()->getNameAsCString(), |
| 3143 | FieldType); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3144 | |
| 3145 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3146 | Name, FieldType, /*TInfo=*/0, |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3147 | /*BitWidth=*/0, /*Mutable=*/false); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3148 | T->addDecl(Field); |
| 3149 | } |
| 3150 | |
| 3151 | T->completeDefinition(*this); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3152 | |
| 3153 | return getPointerType(getTagDeclType(T)); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3154 | } |
| 3155 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3156 | void ASTContext::setObjCFastEnumerationStateType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3157 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3158 | assert(Rec && "Invalid ObjCFAstEnumerationStateType"); |
| 3159 | ObjCFastEnumerationStateTypeDecl = Rec->getDecl(); |
| 3160 | } |
| 3161 | |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3162 | // This returns true if a type has been typedefed to BOOL: |
| 3163 | // typedef <type> BOOL; |
Chris Lattner | 2d99833 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 3164 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3165 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | bb49c3e | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 3166 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 3167 | return II->isStr("BOOL"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3168 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3169 | return false; |
| 3170 | } |
| 3171 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3172 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3173 | /// purpose. |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3174 | CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) { |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3175 | CharUnits sz = getTypeSizeInChars(type); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3176 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3177 | // 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] | 3178 | if (sz.isPositive() && type->isIntegralType()) |
| 3179 | sz = std::max(sz, getTypeSizeInChars(IntTy)); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3180 | // Treat arrays as pointers, since that's how they're passed in. |
| 3181 | else if (type->isArrayType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3182 | sz = getTypeSizeInChars(VoidPtrTy); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3183 | return sz; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3184 | } |
| 3185 | |
| 3186 | static inline |
| 3187 | std::string charUnitsToString(const CharUnits &CU) { |
| 3188 | return llvm::itostr(CU.getQuantity()); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3189 | } |
| 3190 | |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3191 | /// getObjCEncodingForBlockDecl - Return the encoded type for this method |
| 3192 | /// declaration. |
| 3193 | void ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr, |
| 3194 | std::string& S) { |
| 3195 | const BlockDecl *Decl = Expr->getBlockDecl(); |
| 3196 | QualType BlockTy = |
| 3197 | Expr->getType()->getAs<BlockPointerType>()->getPointeeType(); |
| 3198 | // Encode result type. |
| 3199 | getObjCEncodingForType(cast<FunctionType>(BlockTy)->getResultType(), S); |
| 3200 | // Compute size of all parameters. |
| 3201 | // Start with computing size of a pointer in number of bytes. |
| 3202 | // FIXME: There might(should) be a better way of doing this computation! |
| 3203 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3204 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
| 3205 | CharUnits ParmOffset = PtrSize; |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3206 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
| 3207 | E = Decl->param_end(); PI != E; ++PI) { |
| 3208 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3209 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3210 | assert (sz.isPositive() && "BlockExpr - Incomplete param type"); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3211 | ParmOffset += sz; |
| 3212 | } |
| 3213 | // Size of the argument frame |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3214 | S += charUnitsToString(ParmOffset); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3215 | // Block pointer and offset. |
| 3216 | S += "@?0"; |
| 3217 | ParmOffset = PtrSize; |
| 3218 | |
| 3219 | // Argument types. |
| 3220 | ParmOffset = PtrSize; |
| 3221 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E = |
| 3222 | Decl->param_end(); PI != E; ++PI) { |
| 3223 | ParmVarDecl *PVDecl = *PI; |
| 3224 | QualType PType = PVDecl->getOriginalType(); |
| 3225 | if (const ArrayType *AT = |
| 3226 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3227 | // Use array's original type only if it has known number of |
| 3228 | // elements. |
| 3229 | if (!isa<ConstantArrayType>(AT)) |
| 3230 | PType = PVDecl->getType(); |
| 3231 | } else if (PType->isFunctionType()) |
| 3232 | PType = PVDecl->getType(); |
| 3233 | getObjCEncodingForType(PType, S); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3234 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3235 | ParmOffset += getObjCEncodingTypeSize(PType); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3236 | } |
| 3237 | } |
| 3238 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3239 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3240 | /// declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3241 | void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Chris Lattner | e6db3b0 | 2008-11-19 07:24:05 +0000 | [diff] [blame] | 3242 | std::string& S) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3243 | // FIXME: This is not very efficient. |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3244 | // Encode type qualifer, 'in', 'inout', etc. for the return type. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3245 | getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3246 | // Encode result type. |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3247 | getObjCEncodingForType(Decl->getResultType(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3248 | // Compute size of all parameters. |
| 3249 | // Start with computing size of a pointer in number of bytes. |
| 3250 | // FIXME: There might(should) be a better way of doing this computation! |
| 3251 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3252 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3253 | // The first two arguments (self and _cmd) are pointers; account for |
| 3254 | // their size. |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3255 | CharUnits ParmOffset = 2 * PtrSize; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3256 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
| 3257 | E = Decl->param_end(); PI != E; ++PI) { |
| 3258 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3259 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3260 | assert (sz.isPositive() && |
| 3261 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3262 | ParmOffset += sz; |
| 3263 | } |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3264 | S += charUnitsToString(ParmOffset); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3265 | S += "@0:"; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3266 | S += charUnitsToString(PtrSize); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3267 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3268 | // Argument types. |
| 3269 | ParmOffset = 2 * PtrSize; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3270 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
| 3271 | E = Decl->param_end(); PI != E; ++PI) { |
| 3272 | ParmVarDecl *PVDecl = *PI; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3273 | QualType PType = PVDecl->getOriginalType(); |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 3274 | if (const ArrayType *AT = |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 3275 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3276 | // Use array's original type only if it has known number of |
| 3277 | // elements. |
Steve Naroff | bb3fde3 | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 3278 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 3279 | PType = PVDecl->getType(); |
| 3280 | } else if (PType->isFunctionType()) |
| 3281 | PType = PVDecl->getType(); |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3282 | // Process argument qualifiers for user supplied arguments; such as, |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3283 | // 'in', 'inout', etc. |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 3284 | getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3285 | getObjCEncodingForType(PType, S); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 3286 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 3287 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3288 | } |
| 3289 | } |
| 3290 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3291 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 3292 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3293 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 3294 | /// NULL when getting encodings for protocol properties. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3295 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 3296 | /// attributes readonly and bycopy are encoded as single characters. The |
| 3297 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 3298 | /// encoded as single characters, followed by an identifier. Property types |
| 3299 | /// are also encoded as a parametrized attribute. The characters used to encode |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 3300 | /// these attributes are defined by the following enumeration: |
| 3301 | /// @code |
| 3302 | /// enum PropertyAttributes { |
| 3303 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 3304 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 3305 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 3306 | /// kPropertyDynamic = 'D', // property is dynamic |
| 3307 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 3308 | /// kPropertySetter = 'S', // followed by setter selector name |
| 3309 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
| 3310 | /// kPropertyType = 't' // followed by old-style type encoding. |
| 3311 | /// kPropertyWeak = 'W' // 'weak' property |
| 3312 | /// kPropertyStrong = 'P' // property GC'able |
| 3313 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 3314 | /// }; |
| 3315 | /// @endcode |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3316 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3317 | const Decl *Container, |
Chris Lattner | e6db3b0 | 2008-11-19 07:24:05 +0000 | [diff] [blame] | 3318 | std::string& S) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3319 | // Collect information from the property implementation decl(s). |
| 3320 | bool Dynamic = false; |
| 3321 | ObjCPropertyImplDecl *SynthesizePID = 0; |
| 3322 | |
| 3323 | // FIXME: Duplicated code due to poor abstraction. |
| 3324 | if (Container) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3325 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3326 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
| 3327 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3328 | i = CID->propimpl_begin(), e = CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3329 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3330 | ObjCPropertyImplDecl *PID = *i; |
| 3331 | if (PID->getPropertyDecl() == PD) { |
| 3332 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 3333 | Dynamic = true; |
| 3334 | } else { |
| 3335 | SynthesizePID = PID; |
| 3336 | } |
| 3337 | } |
| 3338 | } |
| 3339 | } else { |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3340 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3341 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3342 | i = OID->propimpl_begin(), e = OID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3343 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3344 | ObjCPropertyImplDecl *PID = *i; |
| 3345 | if (PID->getPropertyDecl() == PD) { |
| 3346 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 3347 | Dynamic = true; |
| 3348 | } else { |
| 3349 | SynthesizePID = PID; |
| 3350 | } |
| 3351 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3352 | } |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3353 | } |
| 3354 | } |
| 3355 | |
| 3356 | // FIXME: This is not very efficient. |
| 3357 | S = "T"; |
| 3358 | |
| 3359 | // Encode result type. |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3360 | // GCC has some special rules regarding encoding of properties which |
| 3361 | // closely resembles encoding of ivars. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3362 | getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3363 | true /* outermost type */, |
| 3364 | true /* encoding for property */); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3365 | |
| 3366 | if (PD->isReadOnly()) { |
| 3367 | S += ",R"; |
| 3368 | } else { |
| 3369 | switch (PD->getSetterKind()) { |
| 3370 | case ObjCPropertyDecl::Assign: break; |
| 3371 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3372 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3373 | } |
| 3374 | } |
| 3375 | |
| 3376 | // It really isn't clear at all what this means, since properties |
| 3377 | // are "dynamic by default". |
| 3378 | if (Dynamic) |
| 3379 | S += ",D"; |
| 3380 | |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3381 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 3382 | S += ",N"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3383 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3384 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 3385 | S += ",G"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3386 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3387 | } |
| 3388 | |
| 3389 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 3390 | S += ",S"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3391 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3392 | } |
| 3393 | |
| 3394 | if (SynthesizePID) { |
| 3395 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 3396 | S += ",V"; |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 3397 | S += OID->getNameAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3398 | } |
| 3399 | |
| 3400 | // FIXME: OBJCGC: weak & strong |
| 3401 | } |
| 3402 | |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3403 | /// getLegacyIntegralTypeEncoding - |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3404 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
| 3405 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3406 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
| 3407 | /// |
| 3408 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3409 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3410 | if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) { |
Fariborz Jahanian | c657eba | 2009-02-11 23:59:18 +0000 | [diff] [blame] | 3411 | if (BT->getKind() == BuiltinType::ULong && |
| 3412 | ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3413 | PointeeTy = UnsignedIntTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3414 | else |
Fariborz Jahanian | c657eba | 2009-02-11 23:59:18 +0000 | [diff] [blame] | 3415 | if (BT->getKind() == BuiltinType::Long && |
| 3416 | ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3417 | PointeeTy = IntTy; |
| 3418 | } |
| 3419 | } |
| 3420 | } |
| 3421 | |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3422 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 3423 | const FieldDecl *Field) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3424 | // We follow the behavior of gcc, expanding structures which are |
| 3425 | // directly pointed to, and expanding embedded structures. Note that |
| 3426 | // these rules are sufficient to prevent recursive encoding of the |
| 3427 | // same type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3428 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
Fariborz Jahanian | 5b8c7d9 | 2008-12-22 23:22:27 +0000 | [diff] [blame] | 3429 | true /* outermost type */); |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3430 | } |
| 3431 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3432 | static void EncodeBitField(const ASTContext *Context, std::string& S, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 3433 | const FieldDecl *FD) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3434 | const Expr *E = FD->getBitWidth(); |
| 3435 | assert(E && "bitfield width not there - getObjCEncodingForTypeImpl"); |
| 3436 | ASTContext *Ctx = const_cast<ASTContext*>(Context); |
Eli Friedman | 9a901bb | 2009-04-26 19:19:15 +0000 | [diff] [blame] | 3437 | unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue(); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3438 | S += 'b'; |
| 3439 | S += llvm::utostr(N); |
| 3440 | } |
| 3441 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 3442 | // FIXME: Use SmallString for accumulating string. |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3443 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 3444 | bool ExpandPointedToStructures, |
| 3445 | bool ExpandStructures, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 3446 | const FieldDecl *FD, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3447 | bool OutermostType, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3448 | bool EncodingProperty) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3449 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3450 | if (FD && FD->isBitField()) |
| 3451 | return EncodeBitField(this, S, FD); |
| 3452 | char encoding; |
| 3453 | switch (BT->getKind()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3454 | default: assert(0 && "Unhandled builtin type kind"); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3455 | case BuiltinType::Void: encoding = 'v'; break; |
| 3456 | case BuiltinType::Bool: encoding = 'B'; break; |
| 3457 | case BuiltinType::Char_U: |
| 3458 | case BuiltinType::UChar: encoding = 'C'; break; |
| 3459 | case BuiltinType::UShort: encoding = 'S'; break; |
| 3460 | case BuiltinType::UInt: encoding = 'I'; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3461 | case BuiltinType::ULong: |
| 3462 | encoding = |
| 3463 | (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q'; |
Fariborz Jahanian | 72696e1 | 2009-02-11 22:31:45 +0000 | [diff] [blame] | 3464 | break; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3465 | case BuiltinType::UInt128: encoding = 'T'; break; |
| 3466 | case BuiltinType::ULongLong: encoding = 'Q'; break; |
| 3467 | case BuiltinType::Char_S: |
| 3468 | case BuiltinType::SChar: encoding = 'c'; break; |
| 3469 | case BuiltinType::Short: encoding = 's'; break; |
| 3470 | case BuiltinType::Int: encoding = 'i'; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3471 | case BuiltinType::Long: |
| 3472 | encoding = |
| 3473 | (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3474 | break; |
| 3475 | case BuiltinType::LongLong: encoding = 'q'; break; |
| 3476 | case BuiltinType::Int128: encoding = 't'; break; |
| 3477 | case BuiltinType::Float: encoding = 'f'; break; |
| 3478 | case BuiltinType::Double: encoding = 'd'; break; |
| 3479 | case BuiltinType::LongDouble: encoding = 'd'; break; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3480 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3481 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3482 | S += encoding; |
| 3483 | return; |
| 3484 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3485 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3486 | if (const ComplexType *CT = T->getAs<ComplexType>()) { |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 3487 | S += 'j'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3488 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false, |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 3489 | false); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3490 | return; |
| 3491 | } |
Fariborz Jahanian | 60bce3e | 2009-11-23 20:40:50 +0000 | [diff] [blame] | 3492 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3493 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Fariborz Jahanian | 8d2c0a9 | 2009-11-30 18:43:52 +0000 | [diff] [blame] | 3494 | if (PT->isObjCSelType()) { |
| 3495 | S += ':'; |
| 3496 | return; |
| 3497 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3498 | QualType PointeeTy = PT->getPointeeType(); |
Fariborz Jahanian | 8d2c0a9 | 2009-11-30 18:43:52 +0000 | [diff] [blame] | 3499 | |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3500 | bool isReadOnly = false; |
| 3501 | // For historical/compatibility reasons, the read-only qualifier of the |
| 3502 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 3503 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3504 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3505 | if (isa<TypedefType>(T.getTypePtr())) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3506 | if (OutermostType && T.isConstQualified()) { |
| 3507 | isReadOnly = true; |
| 3508 | S += 'r'; |
| 3509 | } |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 3510 | } else if (OutermostType) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3511 | QualType P = PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3512 | while (P->getAs<PointerType>()) |
| 3513 | P = P->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3514 | if (P.isConstQualified()) { |
| 3515 | isReadOnly = true; |
| 3516 | S += 'r'; |
| 3517 | } |
| 3518 | } |
| 3519 | if (isReadOnly) { |
| 3520 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 3521 | // combinations need to be rearranged. |
| 3522 | // Rewrite "in const" from "nr" to "rn" |
| 3523 | const char * s = S.c_str(); |
| 3524 | int len = S.length(); |
| 3525 | if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') { |
| 3526 | std::string replace = "rn"; |
| 3527 | S.replace(S.end()-2, S.end(), replace); |
| 3528 | } |
| 3529 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3530 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3531 | if (PointeeTy->isCharType()) { |
| 3532 | // char pointer types should be encoded as '*' unless it is a |
| 3533 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3534 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3535 | S += '*'; |
| 3536 | return; |
| 3537 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3538 | } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) { |
Steve Naroff | 9533a7f | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 3539 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 3540 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 3541 | S += '#'; |
| 3542 | return; |
| 3543 | } |
| 3544 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 3545 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 3546 | S += '@'; |
| 3547 | return; |
| 3548 | } |
| 3549 | // fall through... |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3550 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3551 | S += '^'; |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3552 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 3553 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3554 | getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3555 | NULL); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3556 | return; |
| 3557 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3558 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3559 | if (const ArrayType *AT = |
| 3560 | // Ignore type qualifiers etc. |
| 3561 | dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3562 | if (isa<IncompleteArrayType>(AT)) { |
| 3563 | // Incomplete arrays are encoded as a pointer to the array element. |
| 3564 | S += '^'; |
| 3565 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3566 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3567 | false, ExpandStructures, FD); |
| 3568 | } else { |
| 3569 | S += '['; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3570 | |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3571 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 3572 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 3573 | else { |
| 3574 | //Variable length arrays are encoded as a regular array with 0 elements. |
| 3575 | assert(isa<VariableArrayType>(AT) && "Unknown array type!"); |
| 3576 | S += '0'; |
| 3577 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3578 | |
| 3579 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3580 | false, ExpandStructures, FD); |
| 3581 | S += ']'; |
| 3582 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3583 | return; |
| 3584 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3585 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3586 | if (T->getAs<FunctionType>()) { |
Anders Carlsson | c0a87b7 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 3587 | S += '?'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3588 | return; |
| 3589 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3590 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3591 | if (const RecordType *RTy = T->getAs<RecordType>()) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3592 | RecordDecl *RDecl = RTy->getDecl(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3593 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 3594 | // Anonymous structures print as '?' |
| 3595 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 3596 | S += II->getName(); |
| 3597 | } else { |
| 3598 | S += '?'; |
| 3599 | } |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3600 | if (ExpandStructures) { |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3601 | S += '='; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3602 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 3603 | FieldEnd = RDecl->field_end(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3604 | Field != FieldEnd; ++Field) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3605 | if (FD) { |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3606 | S += '"'; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3607 | S += Field->getNameAsString(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3608 | S += '"'; |
| 3609 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3610 | |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3611 | // Special case bit-fields. |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3612 | if (Field->isBitField()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3613 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3614 | (*Field)); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3615 | } else { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3616 | QualType qt = Field->getType(); |
| 3617 | getLegacyIntegralTypeEncoding(qt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3618 | getObjCEncodingForTypeImpl(qt, S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3619 | FD); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3620 | } |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3621 | } |
Fariborz Jahanian | 6de88a8 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 3622 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3623 | S += RDecl->isUnion() ? ')' : '}'; |
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 (T->isEnumeralType()) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3628 | if (FD && FD->isBitField()) |
| 3629 | EncodeBitField(this, S, FD); |
| 3630 | else |
| 3631 | S += 'i'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3632 | return; |
| 3633 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3634 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3635 | if (T->isBlockPointerType()) { |
Steve Naroff | 21a98b1 | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 3636 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3637 | return; |
| 3638 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3639 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3640 | if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3641 | // @encode(class_name) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3642 | ObjCInterfaceDecl *OI = OIT->getDecl(); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3643 | S += '{'; |
| 3644 | const IdentifierInfo *II = OI->getIdentifier(); |
| 3645 | S += II->getName(); |
| 3646 | S += '='; |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3647 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3648 | CollectObjCIvars(OI, RecFields); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3649 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3650 | if (RecFields[i]->isBitField()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3651 | getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3652 | RecFields[i]); |
| 3653 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3654 | getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3655 | FD); |
| 3656 | } |
| 3657 | S += '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3658 | return; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3659 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3660 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3661 | if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3662 | if (OPT->isObjCIdType()) { |
| 3663 | S += '@'; |
| 3664 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3665 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3666 | |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 3667 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
| 3668 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
| 3669 | // Since this is a binary compatibility issue, need to consult with runtime |
| 3670 | // folks. Fortunately, this is a *very* obsure construct. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3671 | S += '#'; |
| 3672 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3673 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3674 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3675 | if (OPT->isObjCQualifiedIdType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3676 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3677 | ExpandPointedToStructures, |
| 3678 | ExpandStructures, FD); |
| 3679 | if (FD || EncodingProperty) { |
| 3680 | // Note that we do extended encoding of protocol qualifer list |
| 3681 | // Only when doing ivar or property encoding. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3682 | S += '"'; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3683 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 3684 | E = OPT->qual_end(); I != E; ++I) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3685 | S += '<'; |
| 3686 | S += (*I)->getNameAsString(); |
| 3687 | S += '>'; |
| 3688 | } |
| 3689 | S += '"'; |
| 3690 | } |
| 3691 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3692 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3693 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3694 | QualType PointeeTy = OPT->getPointeeType(); |
| 3695 | if (!EncodingProperty && |
| 3696 | isa<TypedefType>(PointeeTy.getTypePtr())) { |
| 3697 | // Another historical/compatibility reason. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3698 | // We encode the underlying type which comes out as |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3699 | // {...}; |
| 3700 | S += '^'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3701 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 3702 | false, ExpandPointedToStructures, |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3703 | NULL); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3704 | return; |
| 3705 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3706 | |
| 3707 | S += '@'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 3708 | if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3709 | S += '"'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 3710 | S += OPT->getInterfaceDecl()->getIdentifier()->getName(); |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3711 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 3712 | E = OPT->qual_end(); I != E; ++I) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3713 | S += '<'; |
| 3714 | S += (*I)->getNameAsString(); |
| 3715 | S += '>'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3716 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3717 | S += '"'; |
| 3718 | } |
| 3719 | return; |
| 3720 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3721 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3722 | assert(0 && "@encode for type not implemented!"); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3723 | } |
| 3724 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3725 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3726 | std::string& S) const { |
| 3727 | if (QT & Decl::OBJC_TQ_In) |
| 3728 | S += 'n'; |
| 3729 | if (QT & Decl::OBJC_TQ_Inout) |
| 3730 | S += 'N'; |
| 3731 | if (QT & Decl::OBJC_TQ_Out) |
| 3732 | S += 'o'; |
| 3733 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 3734 | S += 'O'; |
| 3735 | if (QT & Decl::OBJC_TQ_Byref) |
| 3736 | S += 'R'; |
| 3737 | if (QT & Decl::OBJC_TQ_Oneway) |
| 3738 | S += 'V'; |
| 3739 | } |
| 3740 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3741 | void ASTContext::setBuiltinVaListType(QualType T) { |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 3742 | assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3743 | |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 3744 | BuiltinVaListType = T; |
| 3745 | } |
| 3746 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3747 | void ASTContext::setObjCIdType(QualType T) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3748 | ObjCIdTypedefType = T; |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 3749 | } |
| 3750 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3751 | void ASTContext::setObjCSelType(QualType T) { |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 3752 | ObjCSelTypedefType = T; |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 3753 | } |
| 3754 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3755 | void ASTContext::setObjCProtoType(QualType QT) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3756 | ObjCProtoType = QT; |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 3757 | } |
| 3758 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3759 | void ASTContext::setObjCClassType(QualType T) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3760 | ObjCClassTypedefType = T; |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 3761 | } |
| 3762 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3763 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3764 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 3765 | "'NSConstantString' type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3766 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3767 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 3768 | } |
| 3769 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 3770 | /// \brief Retrieve the template name that corresponds to a non-empty |
| 3771 | /// lookup. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3772 | TemplateName ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin, |
| 3773 | UnresolvedSetIterator End) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 3774 | unsigned size = End - Begin; |
| 3775 | assert(size > 1 && "set is not overloaded!"); |
| 3776 | |
| 3777 | void *memory = Allocate(sizeof(OverloadedTemplateStorage) + |
| 3778 | size * sizeof(FunctionTemplateDecl*)); |
| 3779 | OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size); |
| 3780 | |
| 3781 | NamedDecl **Storage = OT->getStorage(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3782 | for (UnresolvedSetIterator I = Begin; I != End; ++I) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 3783 | NamedDecl *D = *I; |
| 3784 | assert(isa<FunctionTemplateDecl>(D) || |
| 3785 | (isa<UsingShadowDecl>(D) && |
| 3786 | isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))); |
| 3787 | *Storage++ = D; |
| 3788 | } |
| 3789 | |
| 3790 | return TemplateName(OT); |
| 3791 | } |
| 3792 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3793 | /// \brief Retrieve the template name that represents a qualified |
| 3794 | /// template name such as \c std::vector. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3795 | TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3796 | bool TemplateKeyword, |
| 3797 | TemplateDecl *Template) { |
| 3798 | llvm::FoldingSetNodeID ID; |
| 3799 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 3800 | |
| 3801 | void *InsertPos = 0; |
| 3802 | QualifiedTemplateName *QTN = |
| 3803 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 3804 | if (!QTN) { |
| 3805 | QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); |
| 3806 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 3807 | } |
| 3808 | |
| 3809 | return TemplateName(QTN); |
| 3810 | } |
| 3811 | |
| 3812 | /// \brief Retrieve the template name that represents a dependent |
| 3813 | /// template name such as \c MetaFun::template apply. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3814 | TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3815 | const IdentifierInfo *Name) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3816 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 3817 | "Nested name specifier must be dependent"); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3818 | |
| 3819 | llvm::FoldingSetNodeID ID; |
| 3820 | DependentTemplateName::Profile(ID, NNS, Name); |
| 3821 | |
| 3822 | void *InsertPos = 0; |
| 3823 | DependentTemplateName *QTN = |
| 3824 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 3825 | |
| 3826 | if (QTN) |
| 3827 | return TemplateName(QTN); |
| 3828 | |
| 3829 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 3830 | if (CanonNNS == NNS) { |
| 3831 | QTN = new (*this,4) DependentTemplateName(NNS, Name); |
| 3832 | } else { |
| 3833 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
| 3834 | QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon); |
| 3835 | } |
| 3836 | |
| 3837 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 3838 | return TemplateName(QTN); |
| 3839 | } |
| 3840 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 3841 | /// \brief Retrieve the template name that represents a dependent |
| 3842 | /// template name such as \c MetaFun::template operator+. |
| 3843 | TemplateName |
| 3844 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
| 3845 | OverloadedOperatorKind Operator) { |
| 3846 | assert((!NNS || NNS->isDependent()) && |
| 3847 | "Nested name specifier must be dependent"); |
| 3848 | |
| 3849 | llvm::FoldingSetNodeID ID; |
| 3850 | DependentTemplateName::Profile(ID, NNS, Operator); |
| 3851 | |
| 3852 | void *InsertPos = 0; |
| 3853 | DependentTemplateName *QTN = |
| 3854 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 3855 | |
| 3856 | if (QTN) |
| 3857 | return TemplateName(QTN); |
| 3858 | |
| 3859 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 3860 | if (CanonNNS == NNS) { |
| 3861 | QTN = new (*this,4) DependentTemplateName(NNS, Operator); |
| 3862 | } else { |
| 3863 | TemplateName Canon = getDependentTemplateName(CanonNNS, Operator); |
| 3864 | QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon); |
| 3865 | } |
| 3866 | |
| 3867 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 3868 | return TemplateName(QTN); |
| 3869 | } |
| 3870 | |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 3871 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | d934112 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 3872 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 3873 | /// is actually a value of type @c TargetInfo::IntType. |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 3874 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 3875 | switch (Type) { |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 3876 | case TargetInfo::NoInt: return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 3877 | case TargetInfo::SignedShort: return ShortTy; |
| 3878 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 3879 | case TargetInfo::SignedInt: return IntTy; |
| 3880 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 3881 | case TargetInfo::SignedLong: return LongTy; |
| 3882 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 3883 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 3884 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 3885 | } |
| 3886 | |
| 3887 | assert(false && "Unhandled TargetInfo::IntType value"); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 3888 | return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 3889 | } |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 3890 | |
| 3891 | //===----------------------------------------------------------------------===// |
| 3892 | // Type Predicates. |
| 3893 | //===----------------------------------------------------------------------===// |
| 3894 | |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3895 | /// isObjCNSObjectType - Return true if this is an NSObject object using |
| 3896 | /// NSObject attribute on a c-style pointer type. |
| 3897 | /// FIXME - Make it work directly on types. |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 3898 | /// FIXME: Move to Type. |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3899 | /// |
| 3900 | bool ASTContext::isObjCNSObjectType(QualType Ty) const { |
| 3901 | if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) { |
| 3902 | if (TypedefDecl *TD = TDT->getDecl()) |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 3903 | if (TD->getAttr<ObjCNSObjectAttr>()) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3904 | return true; |
| 3905 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3906 | return false; |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3907 | } |
| 3908 | |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3909 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 3910 | /// garbage collection attribute. |
| 3911 | /// |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3912 | Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const { |
| 3913 | Qualifiers::GC GCAttrs = Qualifiers::GCNone; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3914 | if (getLangOptions().ObjC1 && |
| 3915 | getLangOptions().getGCMode() != LangOptions::NonGC) { |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 3916 | GCAttrs = Ty.getObjCGCAttr(); |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3917 | // Default behavious under objective-c's gc is for objective-c pointers |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3918 | // (or pointers to them) be treated as though they were declared |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 3919 | // as __strong. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3920 | if (GCAttrs == Qualifiers::GCNone) { |
Fariborz Jahanian | 75212ee | 2009-09-10 23:38:45 +0000 | [diff] [blame] | 3921 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3922 | GCAttrs = Qualifiers::Strong; |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 3923 | else if (Ty->isPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3924 | return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 3925 | } |
Fariborz Jahanian | c211218 | 2009-04-11 00:00:54 +0000 | [diff] [blame] | 3926 | // Non-pointers have none gc'able attribute regardless of the attribute |
| 3927 | // set on them. |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 3928 | else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3929 | return Qualifiers::GCNone; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3930 | } |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 3931 | return GCAttrs; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3932 | } |
| 3933 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3934 | //===----------------------------------------------------------------------===// |
| 3935 | // Type Compatibility Testing |
| 3936 | //===----------------------------------------------------------------------===// |
Chris Lattner | 770951b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 3937 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3938 | /// areCompatVectorTypes - Return true if the two specified vector types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3939 | /// compatible. |
| 3940 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 3941 | const VectorType *RHS) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 3942 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3943 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3944 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3945 | } |
| 3946 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3947 | //===----------------------------------------------------------------------===// |
| 3948 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 3949 | //===----------------------------------------------------------------------===// |
| 3950 | |
| 3951 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 3952 | /// inheritance hierarchy of 'rProto'. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 3953 | bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 3954 | ObjCProtocolDecl *rProto) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3955 | if (lProto == rProto) |
| 3956 | return true; |
| 3957 | for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), |
| 3958 | E = rProto->protocol_end(); PI != E; ++PI) |
| 3959 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 3960 | return true; |
| 3961 | return false; |
| 3962 | } |
| 3963 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3964 | /// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...> |
| 3965 | /// return true if lhs's protocols conform to rhs's protocol; false |
| 3966 | /// otherwise. |
| 3967 | bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) { |
| 3968 | if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType()) |
| 3969 | return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false); |
| 3970 | return false; |
| 3971 | } |
| 3972 | |
| 3973 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 3974 | /// ObjCQualifiedIDType. |
| 3975 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 3976 | bool compare) { |
| 3977 | // Allow id<P..> and an 'id' or void* type in all cases. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3978 | if (lhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3979 | lhs->isObjCIdType() || lhs->isObjCClassType()) |
| 3980 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3981 | else if (rhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3982 | rhs->isObjCIdType() || rhs->isObjCClassType()) |
| 3983 | return true; |
| 3984 | |
| 3985 | if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3986 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3987 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3988 | if (!rhsOPT) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3989 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3990 | if (rhsOPT->qual_empty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3991 | // If the RHS is a unqualified interface pointer "NSString*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3992 | // make sure we check the class hierarchy. |
| 3993 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 3994 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 3995 | E = lhsQID->qual_end(); I != E; ++I) { |
| 3996 | // when comparing an id<P> on lhs with a static type on rhs, |
| 3997 | // see if static class implements all of id's protocols, directly or |
| 3998 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 3999 | if (!rhsID->ClassImplementsProtocol(*I, true)) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4000 | return false; |
| 4001 | } |
| 4002 | } |
| 4003 | // If there are no qualifiers and no interface, we have an 'id'. |
| 4004 | return true; |
| 4005 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4006 | // Both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4007 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4008 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4009 | ObjCProtocolDecl *lhsProto = *I; |
| 4010 | bool match = false; |
| 4011 | |
| 4012 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4013 | // see if static class implements all of id's protocols, directly or |
| 4014 | // through its super class and categories. |
| 4015 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 4016 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 4017 | ObjCProtocolDecl *rhsProto = *J; |
| 4018 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 4019 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 4020 | match = true; |
| 4021 | break; |
| 4022 | } |
| 4023 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4024 | // If the RHS is a qualified interface pointer "NSString<P>*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4025 | // make sure we check the class hierarchy. |
| 4026 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 4027 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 4028 | E = lhsQID->qual_end(); I != E; ++I) { |
| 4029 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4030 | // see if static class implements all of id's protocols, directly or |
| 4031 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 4032 | if (rhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4033 | match = true; |
| 4034 | break; |
| 4035 | } |
| 4036 | } |
| 4037 | } |
| 4038 | if (!match) |
| 4039 | return false; |
| 4040 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4041 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4042 | return true; |
| 4043 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4044 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4045 | const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 4046 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 4047 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4048 | if (const ObjCObjectPointerType *lhsOPT = |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4049 | lhs->getAsObjCInterfacePointerType()) { |
| 4050 | if (lhsOPT->qual_empty()) { |
| 4051 | bool match = false; |
| 4052 | if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { |
| 4053 | for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(), |
| 4054 | E = rhsQID->qual_end(); I != E; ++I) { |
| 4055 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4056 | // see if static class implements all of id's protocols, directly or |
| 4057 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 4058 | if (lhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4059 | match = true; |
| 4060 | break; |
| 4061 | } |
| 4062 | } |
| 4063 | if (!match) |
| 4064 | return false; |
| 4065 | } |
| 4066 | return true; |
| 4067 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4068 | // Both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4069 | for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(), |
| 4070 | E = lhsOPT->qual_end(); I != E; ++I) { |
| 4071 | ObjCProtocolDecl *lhsProto = *I; |
| 4072 | bool match = false; |
| 4073 | |
| 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. |
| 4077 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 4078 | E = rhsQID->qual_end(); J != E; ++J) { |
| 4079 | ObjCProtocolDecl *rhsProto = *J; |
| 4080 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 4081 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 4082 | match = true; |
| 4083 | break; |
| 4084 | } |
| 4085 | } |
| 4086 | if (!match) |
| 4087 | return false; |
| 4088 | } |
| 4089 | return true; |
| 4090 | } |
| 4091 | return false; |
| 4092 | } |
| 4093 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4094 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4095 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 4096 | /// protocol qualifiers on the LHS or RHS. |
| 4097 | /// |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4098 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 4099 | const ObjCObjectPointerType *RHSOPT) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 4100 | // If either type represents the built-in 'id' or 'Class' types, return true. |
| 4101 | if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4102 | return true; |
| 4103 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4104 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4105 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 4106 | QualType(RHSOPT,0), |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4107 | false); |
| 4108 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4109 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 4110 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4111 | if (LHS && RHS) // We have 2 user-defined types. |
| 4112 | return canAssignObjCInterfaces(LHS, RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4113 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4114 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4115 | } |
| 4116 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4117 | /// getIntersectionOfProtocols - This routine finds the intersection of set |
| 4118 | /// of protocols inherited from two distinct objective-c pointer objects. |
| 4119 | /// It is used to build composite qualifier list of the composite type of |
| 4120 | /// the conditional expression involving two objective-c pointer objects. |
| 4121 | static |
| 4122 | void getIntersectionOfProtocols(ASTContext &Context, |
| 4123 | const ObjCObjectPointerType *LHSOPT, |
| 4124 | const ObjCObjectPointerType *RHSOPT, |
| 4125 | llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) { |
| 4126 | |
| 4127 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 4128 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 4129 | |
| 4130 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet; |
| 4131 | unsigned LHSNumProtocols = LHS->getNumProtocols(); |
| 4132 | if (LHSNumProtocols > 0) |
| 4133 | InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end()); |
| 4134 | else { |
| 4135 | llvm::SmallVector<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
| 4136 | Context.CollectInheritedProtocols(LHS->getDecl(), LHSInheritedProtocols); |
| 4137 | InheritedProtocolSet.insert(LHSInheritedProtocols.begin(), |
| 4138 | LHSInheritedProtocols.end()); |
| 4139 | } |
| 4140 | |
| 4141 | unsigned RHSNumProtocols = RHS->getNumProtocols(); |
| 4142 | if (RHSNumProtocols > 0) { |
| 4143 | ObjCProtocolDecl **RHSProtocols = (ObjCProtocolDecl **)RHS->qual_begin(); |
| 4144 | for (unsigned i = 0; i < RHSNumProtocols; ++i) |
| 4145 | if (InheritedProtocolSet.count(RHSProtocols[i])) |
| 4146 | IntersectionOfProtocols.push_back(RHSProtocols[i]); |
| 4147 | } |
| 4148 | else { |
| 4149 | llvm::SmallVector<ObjCProtocolDecl *, 8> RHSInheritedProtocols; |
| 4150 | Context.CollectInheritedProtocols(RHS->getDecl(), RHSInheritedProtocols); |
| 4151 | // FIXME. This may cause duplication of protocols in the list, but should |
| 4152 | // be harmless. |
| 4153 | for (unsigned i = 0, len = RHSInheritedProtocols.size(); i < len; ++i) |
| 4154 | if (InheritedProtocolSet.count(RHSInheritedProtocols[i])) |
| 4155 | IntersectionOfProtocols.push_back(RHSInheritedProtocols[i]); |
| 4156 | } |
| 4157 | } |
| 4158 | |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4159 | /// areCommonBaseCompatible - Returns common base class of the two classes if |
| 4160 | /// one found. Note that this is O'2 algorithm. But it will be called as the |
| 4161 | /// last type comparison in a ?-exp of ObjC pointer types before a |
| 4162 | /// warning is issued. So, its invokation is extremely rare. |
| 4163 | QualType ASTContext::areCommonBaseCompatible( |
| 4164 | const ObjCObjectPointerType *LHSOPT, |
| 4165 | const ObjCObjectPointerType *RHSOPT) { |
| 4166 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 4167 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 4168 | if (!LHS || !RHS) |
| 4169 | return QualType(); |
| 4170 | |
| 4171 | while (const ObjCInterfaceDecl *LHSIDecl = LHS->getDecl()->getSuperClass()) { |
| 4172 | QualType LHSTy = getObjCInterfaceType(LHSIDecl); |
| 4173 | LHS = LHSTy->getAs<ObjCInterfaceType>(); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4174 | if (canAssignObjCInterfaces(LHS, RHS)) { |
| 4175 | llvm::SmallVector<ObjCProtocolDecl *, 8> IntersectionOfProtocols; |
| 4176 | getIntersectionOfProtocols(*this, |
| 4177 | LHSOPT, RHSOPT, IntersectionOfProtocols); |
| 4178 | if (IntersectionOfProtocols.empty()) |
| 4179 | LHSTy = getObjCObjectPointerType(LHSTy); |
| 4180 | else |
| 4181 | LHSTy = getObjCObjectPointerType(LHSTy, &IntersectionOfProtocols[0], |
| 4182 | IntersectionOfProtocols.size()); |
| 4183 | return LHSTy; |
| 4184 | } |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4185 | } |
| 4186 | |
| 4187 | return QualType(); |
| 4188 | } |
| 4189 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4190 | bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS, |
| 4191 | const ObjCInterfaceType *RHS) { |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4192 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 4193 | // the LHS. |
| 4194 | if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
| 4195 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4196 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4197 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 4198 | // protocol qualified at all, then we are good. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4199 | if (LHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4200 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4201 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4202 | // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it |
| 4203 | // isn't a superset. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4204 | if (RHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4205 | return true; // FIXME: should return false! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4206 | |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4207 | for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(), |
| 4208 | LHSPE = LHS->qual_end(); |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4209 | LHSPI != LHSPE; LHSPI++) { |
| 4210 | bool RHSImplementsProtocol = false; |
| 4211 | |
| 4212 | // If the RHS doesn't implement the protocol on the left, the types |
| 4213 | // are incompatible. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4214 | for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(), |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4215 | RHSPE = RHS->qual_end(); |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 4216 | RHSPI != RHSPE; RHSPI++) { |
| 4217 | if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) { |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4218 | RHSImplementsProtocol = true; |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 4219 | break; |
| 4220 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4221 | } |
| 4222 | // FIXME: For better diagnostics, consider passing back the protocol name. |
| 4223 | if (!RHSImplementsProtocol) |
| 4224 | return false; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4225 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4226 | // The RHS implements all protocols listed on the LHS. |
| 4227 | return true; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4228 | } |
| 4229 | |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4230 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 4231 | // get the "pointed to" types |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4232 | const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
| 4233 | const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4234 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4235 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4236 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4237 | |
| 4238 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 4239 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4240 | } |
| 4241 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4242 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4243 | /// both shall have the identically qualified version of a compatible type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4244 | /// 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] | 4245 | /// same. See 6.7.[2,3,5] for additional rules. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4246 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) { |
| 4247 | return !mergeTypes(LHS, RHS).isNull(); |
| 4248 | } |
| 4249 | |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 4250 | static bool isSameCallingConvention(CallingConv lcc, CallingConv rcc) { |
| 4251 | return (getCanonicalCallingConv(lcc) == getCanonicalCallingConv(rcc)); |
| 4252 | } |
| 4253 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4254 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4255 | const FunctionType *lbase = lhs->getAs<FunctionType>(); |
| 4256 | const FunctionType *rbase = rhs->getAs<FunctionType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4257 | const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 4258 | const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4259 | bool allLTypes = true; |
| 4260 | bool allRTypes = true; |
| 4261 | |
| 4262 | // Check return type |
| 4263 | QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType()); |
| 4264 | if (retType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4265 | if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType())) |
| 4266 | allLTypes = false; |
| 4267 | if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType())) |
| 4268 | allRTypes = false; |
Mike Stump | 6dcbc29 | 2009-07-25 23:24:03 +0000 | [diff] [blame] | 4269 | // FIXME: double check this |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 4270 | bool NoReturn = lbase->getNoReturnAttr() || rbase->getNoReturnAttr(); |
| 4271 | if (NoReturn != lbase->getNoReturnAttr()) |
| 4272 | allLTypes = false; |
| 4273 | if (NoReturn != rbase->getNoReturnAttr()) |
| 4274 | allRTypes = false; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 4275 | CallingConv lcc = lbase->getCallConv(); |
| 4276 | CallingConv rcc = rbase->getCallConv(); |
| 4277 | // Compatible functions must have compatible calling conventions |
| 4278 | if (!isSameCallingConvention(lcc, rcc)) |
| 4279 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4280 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4281 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 4282 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 4283 | "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4284 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 4285 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 4286 | |
| 4287 | // Compatible functions must have the same number of arguments |
| 4288 | if (lproto_nargs != rproto_nargs) |
| 4289 | return QualType(); |
| 4290 | |
| 4291 | // Variadic and non-variadic functions aren't compatible |
| 4292 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 4293 | return QualType(); |
| 4294 | |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 4295 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 4296 | return QualType(); |
| 4297 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4298 | // Check argument compatibility |
| 4299 | llvm::SmallVector<QualType, 10> types; |
| 4300 | for (unsigned i = 0; i < lproto_nargs; i++) { |
| 4301 | QualType largtype = lproto->getArgType(i).getUnqualifiedType(); |
| 4302 | QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); |
| 4303 | QualType argtype = mergeTypes(largtype, rargtype); |
| 4304 | if (argtype.isNull()) return QualType(); |
| 4305 | types.push_back(argtype); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4306 | if (getCanonicalType(argtype) != getCanonicalType(largtype)) |
| 4307 | allLTypes = false; |
| 4308 | if (getCanonicalType(argtype) != getCanonicalType(rargtype)) |
| 4309 | allRTypes = false; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4310 | } |
| 4311 | if (allLTypes) return lhs; |
| 4312 | if (allRTypes) return rhs; |
| 4313 | return getFunctionType(retType, types.begin(), types.size(), |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 4314 | lproto->isVariadic(), lproto->getTypeQuals(), |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 4315 | NoReturn, lcc); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4316 | } |
| 4317 | |
| 4318 | if (lproto) allRTypes = false; |
| 4319 | if (rproto) allLTypes = false; |
| 4320 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4321 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4322 | if (proto) { |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 4323 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4324 | if (proto->isVariadic()) return QualType(); |
| 4325 | // Check that the types are compatible with the types that |
| 4326 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 4327 | // The only types actually affected are promotable integer |
| 4328 | // types and floats, which would be passed as a different |
| 4329 | // type depending on whether the prototype is visible. |
| 4330 | unsigned proto_nargs = proto->getNumArgs(); |
| 4331 | for (unsigned i = 0; i < proto_nargs; ++i) { |
| 4332 | QualType argTy = proto->getArgType(i); |
Douglas Gregor | b0f8eac | 2010-02-03 19:27:29 +0000 | [diff] [blame^] | 4333 | |
| 4334 | // Look at the promotion type of enum types, since that is the type used |
| 4335 | // to pass enum values. |
| 4336 | if (const EnumType *Enum = argTy->getAs<EnumType>()) |
| 4337 | argTy = Enum->getDecl()->getPromotionType(); |
| 4338 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4339 | if (argTy->isPromotableIntegerType() || |
| 4340 | getCanonicalType(argTy).getUnqualifiedType() == FloatTy) |
| 4341 | return QualType(); |
| 4342 | } |
| 4343 | |
| 4344 | if (allLTypes) return lhs; |
| 4345 | if (allRTypes) return rhs; |
| 4346 | return getFunctionType(retType, proto->arg_type_begin(), |
Mike Stump | 2d3c191 | 2009-07-27 00:44:23 +0000 | [diff] [blame] | 4347 | proto->getNumArgs(), proto->isVariadic(), |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 4348 | proto->getTypeQuals(), NoReturn, lcc); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4349 | } |
| 4350 | |
| 4351 | if (allLTypes) return lhs; |
| 4352 | if (allRTypes) return rhs; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 4353 | return getFunctionNoProtoType(retType, NoReturn, lcc); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4354 | } |
| 4355 | |
| 4356 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) { |
Bill Wendling | 43d6975 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 4357 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 4358 | // type is adjusted to "T" prior to any further analysis, the expression |
| 4359 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4360 | // expression is an lvalue unless the reference is an rvalue reference and |
| 4361 | // the expression is a function call (possibly inside parentheses). |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4362 | // FIXME: C++ shouldn't be going through here! The rules are different |
| 4363 | // enough that they should be handled separately. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4364 | // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really* |
| 4365 | // shouldn't be going through here! |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4366 | if (const ReferenceType *RT = LHS->getAs<ReferenceType>()) |
Chris Lattner | c4e4059 | 2008-04-07 04:07:56 +0000 | [diff] [blame] | 4367 | LHS = RT->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4368 | if (const ReferenceType *RT = RHS->getAs<ReferenceType>()) |
Chris Lattner | c4e4059 | 2008-04-07 04:07:56 +0000 | [diff] [blame] | 4369 | RHS = RT->getPointeeType(); |
Chris Lattner | f3692dc | 2008-04-07 05:37:56 +0000 | [diff] [blame] | 4370 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4371 | QualType LHSCan = getCanonicalType(LHS), |
| 4372 | RHSCan = getCanonicalType(RHS); |
| 4373 | |
| 4374 | // If two types are identical, they are compatible. |
| 4375 | if (LHSCan == RHSCan) |
| 4376 | return LHS; |
| 4377 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4378 | // If the qualifiers are different, the types aren't compatible... mostly. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4379 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 4380 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4381 | if (LQuals != RQuals) { |
| 4382 | // If any of these qualifiers are different, we have a type |
| 4383 | // mismatch. |
| 4384 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 4385 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 4386 | return QualType(); |
| 4387 | |
| 4388 | // Exactly one GC qualifier difference is allowed: __strong is |
| 4389 | // okay if the other type has no GC qualifier but is an Objective |
| 4390 | // C object pointer (i.e. implicitly strong by default). We fix |
| 4391 | // this by pretending that the unqualified type was actually |
| 4392 | // qualified __strong. |
| 4393 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 4394 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 4395 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 4396 | |
| 4397 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 4398 | return QualType(); |
| 4399 | |
| 4400 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
| 4401 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); |
| 4402 | } |
| 4403 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
| 4404 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); |
| 4405 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4406 | return QualType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4407 | } |
| 4408 | |
| 4409 | // Okay, qualifiers are equal. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4410 | |
Eli Friedman | 852d63b | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 4411 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 4412 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4413 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4414 | // We want to consider the two function types to be the same for these |
| 4415 | // comparisons, just force one to the other. |
| 4416 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 4417 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 4c721d3 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 4418 | |
| 4419 | // Same as above for arrays |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 4420 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 4421 | LHSClass = Type::ConstantArray; |
| 4422 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 4423 | RHSClass = Type::ConstantArray; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4424 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 4425 | // Canonicalize ExtVector -> Vector. |
| 4426 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 4427 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4428 | |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 4429 | // If the canonical type classes don't match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4430 | if (LHSClass != RHSClass) { |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4431 | // 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] | 4432 | // a signed integer type, or an unsigned integer type. |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 4433 | // Compatibility is based on the underlying type, not the promotion |
| 4434 | // type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4435 | if (const EnumType* ETy = LHS->getAs<EnumType>()) { |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4436 | if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType()) |
| 4437 | return RHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 4438 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4439 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4440 | if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType()) |
| 4441 | return LHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 4442 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4443 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4444 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4445 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4446 | |
Steve Naroff | 4a74678 | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 4447 | // The canonical type classes match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4448 | switch (LHSClass) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4449 | #define TYPE(Class, Base) |
| 4450 | #define ABSTRACT_TYPE(Class, Base) |
| 4451 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 4452 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 4453 | #include "clang/AST/TypeNodes.def" |
| 4454 | assert(false && "Non-canonical and dependent types shouldn't get here"); |
| 4455 | return QualType(); |
| 4456 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4457 | case Type::LValueReference: |
| 4458 | case Type::RValueReference: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4459 | case Type::MemberPointer: |
| 4460 | assert(false && "C++ should never be in mergeTypes"); |
| 4461 | return QualType(); |
| 4462 | |
| 4463 | case Type::IncompleteArray: |
| 4464 | case Type::VariableArray: |
| 4465 | case Type::FunctionProto: |
| 4466 | case Type::ExtVector: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4467 | assert(false && "Types are eliminated above"); |
| 4468 | return QualType(); |
| 4469 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4470 | case Type::Pointer: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4471 | { |
| 4472 | // Merge two pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4473 | QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); |
| 4474 | QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4475 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee); |
| 4476 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 4477 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4478 | return LHS; |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 4479 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4480 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4481 | return getPointerType(ResultType); |
| 4482 | } |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 4483 | case Type::BlockPointer: |
| 4484 | { |
| 4485 | // Merge two block pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4486 | QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); |
| 4487 | QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 4488 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee); |
| 4489 | if (ResultType.isNull()) return QualType(); |
| 4490 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 4491 | return LHS; |
| 4492 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 4493 | return RHS; |
| 4494 | return getBlockPointerType(ResultType); |
| 4495 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4496 | case Type::ConstantArray: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4497 | { |
| 4498 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 4499 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 4500 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 4501 | return QualType(); |
| 4502 | |
| 4503 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 4504 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
| 4505 | QualType ResultType = mergeTypes(LHSElem, RHSElem); |
| 4506 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4507 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 4508 | return LHS; |
| 4509 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 4510 | return RHS; |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 4511 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 4512 | ArrayType::ArraySizeModifier(), 0); |
| 4513 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 4514 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4515 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 4516 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4517 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 4518 | return LHS; |
| 4519 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 4520 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4521 | if (LVAT) { |
| 4522 | // FIXME: This isn't correct! But tricky to implement because |
| 4523 | // the array's size has to be the size of LHS, but the type |
| 4524 | // has to be different. |
| 4525 | return LHS; |
| 4526 | } |
| 4527 | if (RVAT) { |
| 4528 | // FIXME: This isn't correct! But tricky to implement because |
| 4529 | // the array's size has to be the size of RHS, but the type |
| 4530 | // has to be different. |
| 4531 | return RHS; |
| 4532 | } |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 4533 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 4534 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4535 | return getIncompleteArrayType(ResultType, |
| 4536 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4537 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4538 | case Type::FunctionNoProto: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4539 | return mergeFunctionTypes(LHS, RHS); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4540 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4541 | case Type::Enum: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4542 | return QualType(); |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4543 | case Type::Builtin: |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 4544 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4545 | return QualType(); |
Daniel Dunbar | 64cfdb7 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 4546 | case Type::Complex: |
| 4547 | // Distinct complex types are incompatible. |
| 4548 | return QualType(); |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 4549 | case Type::Vector: |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 4550 | // FIXME: The merged type should be an ExtVector! |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4551 | if (areCompatVectorTypes(LHS->getAs<VectorType>(), RHS->getAs<VectorType>())) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4552 | return LHS; |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4553 | return QualType(); |
Cedric Venet | 61490e9 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 4554 | case Type::ObjCInterface: { |
Steve Naroff | 5fd659d | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 4555 | // Check if the interfaces are assignment compatible. |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 4556 | // FIXME: This should be type compatibility, e.g. whether |
| 4557 | // "LHS x; RHS x;" at global scope is legal. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4558 | const ObjCInterfaceType* LHSIface = LHS->getAs<ObjCInterfaceType>(); |
| 4559 | const ObjCInterfaceType* RHSIface = RHS->getAs<ObjCInterfaceType>(); |
Steve Naroff | 5fd659d | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 4560 | if (LHSIface && RHSIface && |
| 4561 | canAssignObjCInterfaces(LHSIface, RHSIface)) |
| 4562 | return LHS; |
| 4563 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4564 | return QualType(); |
Cedric Venet | 61490e9 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 4565 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4566 | case Type::ObjCObjectPointer: { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4567 | if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(), |
| 4568 | RHS->getAs<ObjCObjectPointerType>())) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4569 | return LHS; |
| 4570 | |
Steve Naroff | bc76dd0 | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 4571 | return QualType(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4572 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4573 | case Type::TemplateSpecialization: |
| 4574 | assert(false && "Dependent types have no size"); |
| 4575 | break; |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4576 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4577 | |
| 4578 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4579 | } |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 4580 | |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 4581 | //===----------------------------------------------------------------------===// |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4582 | // Integer Predicates |
| 4583 | //===----------------------------------------------------------------------===// |
Chris Lattner | 88054de | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 4584 | |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4585 | unsigned ASTContext::getIntWidth(QualType T) { |
Sebastian Redl | 632d772 | 2009-11-05 21:10:57 +0000 | [diff] [blame] | 4586 | if (T->isBooleanType()) |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4587 | return 1; |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 4588 | if (EnumType *ET = dyn_cast<EnumType>(T)) |
Eli Friedman | 29a7f33 | 2009-12-10 22:29:29 +0000 | [diff] [blame] | 4589 | T = ET->getDecl()->getIntegerType(); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4590 | // For builtin types, just use the standard type sizing method |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4591 | return (unsigned)getTypeSize(T); |
| 4592 | } |
| 4593 | |
| 4594 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) { |
| 4595 | assert(T->isSignedIntegerType() && "Unexpected type"); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4596 | |
| 4597 | // Turn <4 x signed int> -> <4 x unsigned int> |
| 4598 | if (const VectorType *VTy = T->getAs<VectorType>()) |
| 4599 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), |
| 4600 | VTy->getNumElements()); |
| 4601 | |
| 4602 | // For enums, we return the unsigned version of the base type. |
| 4603 | if (const EnumType *ETy = T->getAs<EnumType>()) |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4604 | T = ETy->getDecl()->getIntegerType(); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4605 | |
| 4606 | const BuiltinType *BTy = T->getAs<BuiltinType>(); |
| 4607 | assert(BTy && "Unexpected signed integer type"); |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4608 | switch (BTy->getKind()) { |
| 4609 | case BuiltinType::Char_S: |
| 4610 | case BuiltinType::SChar: |
| 4611 | return UnsignedCharTy; |
| 4612 | case BuiltinType::Short: |
| 4613 | return UnsignedShortTy; |
| 4614 | case BuiltinType::Int: |
| 4615 | return UnsignedIntTy; |
| 4616 | case BuiltinType::Long: |
| 4617 | return UnsignedLongTy; |
| 4618 | case BuiltinType::LongLong: |
| 4619 | return UnsignedLongLongTy; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 4620 | case BuiltinType::Int128: |
| 4621 | return UnsignedInt128Ty; |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4622 | default: |
| 4623 | assert(0 && "Unexpected signed integer type"); |
| 4624 | return QualType(); |
| 4625 | } |
| 4626 | } |
| 4627 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 4628 | ExternalASTSource::~ExternalASTSource() { } |
| 4629 | |
| 4630 | void ExternalASTSource::PrintStats() { } |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4631 | |
| 4632 | |
| 4633 | //===----------------------------------------------------------------------===// |
| 4634 | // Builtin Type Computation |
| 4635 | //===----------------------------------------------------------------------===// |
| 4636 | |
| 4637 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
| 4638 | /// pointer over the consumed characters. This returns the resultant type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4639 | static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context, |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4640 | ASTContext::GetBuiltinTypeError &Error, |
| 4641 | bool AllowTypeModifiers = true) { |
| 4642 | // Modifiers. |
| 4643 | int HowLong = 0; |
| 4644 | bool Signed = false, Unsigned = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4645 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4646 | // Read the modifiers first. |
| 4647 | bool Done = false; |
| 4648 | while (!Done) { |
| 4649 | switch (*Str++) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4650 | default: Done = true; --Str; break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4651 | case 'S': |
| 4652 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 4653 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 4654 | Signed = true; |
| 4655 | break; |
| 4656 | case 'U': |
| 4657 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
| 4658 | assert(!Unsigned && "Can't use 'S' modifier multiple times!"); |
| 4659 | Unsigned = true; |
| 4660 | break; |
| 4661 | case 'L': |
| 4662 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 4663 | ++HowLong; |
| 4664 | break; |
| 4665 | } |
| 4666 | } |
| 4667 | |
| 4668 | QualType Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4669 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4670 | // Read the base type. |
| 4671 | switch (*Str++) { |
| 4672 | default: assert(0 && "Unknown builtin type letter!"); |
| 4673 | case 'v': |
| 4674 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 4675 | "Bad modifiers used with 'v'!"); |
| 4676 | Type = Context.VoidTy; |
| 4677 | break; |
| 4678 | case 'f': |
| 4679 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 4680 | "Bad modifiers used with 'f'!"); |
| 4681 | Type = Context.FloatTy; |
| 4682 | break; |
| 4683 | case 'd': |
| 4684 | assert(HowLong < 2 && !Signed && !Unsigned && |
| 4685 | "Bad modifiers used with 'd'!"); |
| 4686 | if (HowLong) |
| 4687 | Type = Context.LongDoubleTy; |
| 4688 | else |
| 4689 | Type = Context.DoubleTy; |
| 4690 | break; |
| 4691 | case 's': |
| 4692 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 4693 | if (Unsigned) |
| 4694 | Type = Context.UnsignedShortTy; |
| 4695 | else |
| 4696 | Type = Context.ShortTy; |
| 4697 | break; |
| 4698 | case 'i': |
| 4699 | if (HowLong == 3) |
| 4700 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 4701 | else if (HowLong == 2) |
| 4702 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 4703 | else if (HowLong == 1) |
| 4704 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 4705 | else |
| 4706 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 4707 | break; |
| 4708 | case 'c': |
| 4709 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 4710 | if (Signed) |
| 4711 | Type = Context.SignedCharTy; |
| 4712 | else if (Unsigned) |
| 4713 | Type = Context.UnsignedCharTy; |
| 4714 | else |
| 4715 | Type = Context.CharTy; |
| 4716 | break; |
| 4717 | case 'b': // boolean |
| 4718 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 4719 | Type = Context.BoolTy; |
| 4720 | break; |
| 4721 | case 'z': // size_t. |
| 4722 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 4723 | Type = Context.getSizeType(); |
| 4724 | break; |
| 4725 | case 'F': |
| 4726 | Type = Context.getCFConstantStringType(); |
| 4727 | break; |
| 4728 | case 'a': |
| 4729 | Type = Context.getBuiltinVaListType(); |
| 4730 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 4731 | break; |
| 4732 | case 'A': |
| 4733 | // This is a "reference" to a va_list; however, what exactly |
| 4734 | // this means depends on how va_list is defined. There are two |
| 4735 | // different kinds of va_list: ones passed by value, and ones |
| 4736 | // passed by reference. An example of a by-value va_list is |
| 4737 | // x86, where va_list is a char*. An example of by-ref va_list |
| 4738 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 4739 | // we want this argument to be a char*&; for x86-64, we want |
| 4740 | // it to be a __va_list_tag*. |
| 4741 | Type = Context.getBuiltinVaListType(); |
| 4742 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 4743 | if (Type->isArrayType()) { |
| 4744 | Type = Context.getArrayDecayedType(Type); |
| 4745 | } else { |
| 4746 | Type = Context.getLValueReferenceType(Type); |
| 4747 | } |
| 4748 | break; |
| 4749 | case 'V': { |
| 4750 | char *End; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4751 | unsigned NumElements = strtoul(Str, &End, 10); |
| 4752 | assert(End != Str && "Missing vector size"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4753 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4754 | Str = End; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4755 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4756 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false); |
| 4757 | Type = Context.getVectorType(ElementType, NumElements); |
| 4758 | break; |
| 4759 | } |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 4760 | case 'X': { |
| 4761 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false); |
| 4762 | Type = Context.getComplexType(ElementType); |
| 4763 | break; |
| 4764 | } |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 4765 | case 'P': |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 4766 | Type = Context.getFILEType(); |
| 4767 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 4768 | Error = ASTContext::GE_Missing_stdio; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4769 | return QualType(); |
| 4770 | } |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 4771 | break; |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 4772 | case 'J': |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 4773 | if (Signed) |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 4774 | Type = Context.getsigjmp_bufType(); |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 4775 | else |
| 4776 | Type = Context.getjmp_bufType(); |
| 4777 | |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 4778 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 4779 | Error = ASTContext::GE_Missing_setjmp; |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 4780 | return QualType(); |
| 4781 | } |
| 4782 | break; |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 4783 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4784 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4785 | if (!AllowTypeModifiers) |
| 4786 | return Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4787 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4788 | Done = false; |
| 4789 | while (!Done) { |
| 4790 | switch (*Str++) { |
| 4791 | default: Done = true; --Str; break; |
| 4792 | case '*': |
| 4793 | Type = Context.getPointerType(Type); |
| 4794 | break; |
| 4795 | case '&': |
| 4796 | Type = Context.getLValueReferenceType(Type); |
| 4797 | break; |
| 4798 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 4799 | case 'C': |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4800 | Type = Type.withConst(); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4801 | break; |
Fariborz Jahanian | 013af39 | 2010-01-26 22:48:42 +0000 | [diff] [blame] | 4802 | case 'D': |
| 4803 | Type = Context.getVolatileType(Type); |
| 4804 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4805 | } |
| 4806 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4807 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4808 | return Type; |
| 4809 | } |
| 4810 | |
| 4811 | /// GetBuiltinType - Return the type for the specified builtin. |
| 4812 | QualType ASTContext::GetBuiltinType(unsigned id, |
| 4813 | GetBuiltinTypeError &Error) { |
| 4814 | const char *TypeStr = BuiltinInfo.GetTypeString(id); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4815 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4816 | llvm::SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4817 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4818 | Error = GE_None; |
| 4819 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error); |
| 4820 | if (Error != GE_None) |
| 4821 | return QualType(); |
| 4822 | while (TypeStr[0] && TypeStr[0] != '.') { |
| 4823 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error); |
| 4824 | if (Error != GE_None) |
| 4825 | return QualType(); |
| 4826 | |
| 4827 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 4828 | if (Ty->isArrayType()) |
| 4829 | Ty = getArrayDecayedType(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4830 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4831 | ArgTypes.push_back(Ty); |
| 4832 | } |
| 4833 | |
| 4834 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 4835 | "'.' should only occur at end of builtin type list!"); |
| 4836 | |
| 4837 | // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);". |
| 4838 | if (ArgTypes.size() == 0 && TypeStr[0] == '.') |
| 4839 | return getFunctionNoProtoType(ResType); |
| 4840 | return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), |
| 4841 | TypeStr[0] == '.', 0); |
| 4842 | } |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4843 | |
| 4844 | QualType |
| 4845 | ASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) { |
| 4846 | // Perform the usual unary conversions. We do this early so that |
| 4847 | // integral promotions to "int" can allow us to exit early, in the |
| 4848 | // lhs == rhs check. Also, for conversion purposes, we ignore any |
| 4849 | // qualifiers. For example, "const float" and "float" are |
| 4850 | // equivalent. |
| 4851 | if (lhs->isPromotableIntegerType()) |
| 4852 | lhs = getPromotedIntegerType(lhs); |
| 4853 | else |
| 4854 | lhs = lhs.getUnqualifiedType(); |
| 4855 | if (rhs->isPromotableIntegerType()) |
| 4856 | rhs = getPromotedIntegerType(rhs); |
| 4857 | else |
| 4858 | rhs = rhs.getUnqualifiedType(); |
| 4859 | |
| 4860 | // If both types are identical, no conversion is needed. |
| 4861 | if (lhs == rhs) |
| 4862 | return lhs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4863 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4864 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 4865 | // The caller can deal with this (e.g. pointer + int). |
| 4866 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 4867 | return lhs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4868 | |
| 4869 | // At this point, we have two different arithmetic types. |
| 4870 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4871 | // Handle complex types first (C99 6.3.1.8p1). |
| 4872 | if (lhs->isComplexType() || rhs->isComplexType()) { |
| 4873 | // if we have an integer operand, the result is the complex type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4874 | if (rhs->isIntegerType() || rhs->isComplexIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4875 | // convert the rhs to the lhs complex type. |
| 4876 | return lhs; |
| 4877 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4878 | if (lhs->isIntegerType() || lhs->isComplexIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4879 | // convert the lhs to the rhs complex type. |
| 4880 | return rhs; |
| 4881 | } |
| 4882 | // This handles complex/complex, complex/float, or float/complex. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4883 | // When both operands are complex, the shorter operand is converted to the |
| 4884 | // type of the longer, and that is the type of the result. This corresponds |
| 4885 | // to what is done when combining two real floating-point operands. |
| 4886 | // The fun begins when size promotion occur across type domains. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4887 | // 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] | 4888 | // floating-point type, the less precise type is converted, within it's |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4889 | // 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] | 4890 | // when combining a "long double" with a "double _Complex", the |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4891 | // "double _Complex" is promoted to "long double _Complex". |
| 4892 | int result = getFloatingTypeOrder(lhs, rhs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4893 | |
| 4894 | if (result > 0) { // The left side is bigger, convert rhs. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4895 | rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4896 | } else if (result < 0) { // The right side is bigger, convert lhs. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4897 | lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4898 | } |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4899 | // At this point, lhs and rhs have the same rank/size. Now, make sure the |
| 4900 | // domains match. This is a requirement for our implementation, C99 |
| 4901 | // does not require this promotion. |
| 4902 | if (lhs != rhs) { // Domains don't match, we have complex/float mix. |
| 4903 | if (lhs->isRealFloatingType()) { // handle "double, _Complex double". |
| 4904 | return rhs; |
| 4905 | } else { // handle "_Complex double, double". |
| 4906 | return lhs; |
| 4907 | } |
| 4908 | } |
| 4909 | return lhs; // The domain/size match exactly. |
| 4910 | } |
| 4911 | // Now handle "real" floating types (i.e. float, double, long double). |
| 4912 | if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) { |
| 4913 | // if we have an integer operand, the result is the real floating type. |
| 4914 | if (rhs->isIntegerType()) { |
| 4915 | // convert rhs to the lhs floating point type. |
| 4916 | return lhs; |
| 4917 | } |
| 4918 | if (rhs->isComplexIntegerType()) { |
| 4919 | // convert rhs to the complex floating point type. |
| 4920 | return getComplexType(lhs); |
| 4921 | } |
| 4922 | if (lhs->isIntegerType()) { |
| 4923 | // convert lhs to the rhs floating point type. |
| 4924 | return rhs; |
| 4925 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4926 | if (lhs->isComplexIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4927 | // convert lhs to the complex floating point type. |
| 4928 | return getComplexType(rhs); |
| 4929 | } |
| 4930 | // We have two real floating types, float/complex combos were handled above. |
| 4931 | // Convert the smaller operand to the bigger result. |
| 4932 | int result = getFloatingTypeOrder(lhs, rhs); |
| 4933 | if (result > 0) // convert the rhs |
| 4934 | return lhs; |
| 4935 | assert(result < 0 && "illegal float comparison"); |
| 4936 | return rhs; // convert the lhs |
| 4937 | } |
| 4938 | if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) { |
| 4939 | // Handle GCC complex int extension. |
| 4940 | const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType(); |
| 4941 | const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType(); |
| 4942 | |
| 4943 | if (lhsComplexInt && rhsComplexInt) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4944 | if (getIntegerTypeOrder(lhsComplexInt->getElementType(), |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4945 | rhsComplexInt->getElementType()) >= 0) |
| 4946 | return lhs; // convert the rhs |
| 4947 | return rhs; |
| 4948 | } else if (lhsComplexInt && rhs->isIntegerType()) { |
| 4949 | // convert the rhs to the lhs complex type. |
| 4950 | return lhs; |
| 4951 | } else if (rhsComplexInt && lhs->isIntegerType()) { |
| 4952 | // convert the lhs to the rhs complex type. |
| 4953 | return rhs; |
| 4954 | } |
| 4955 | } |
| 4956 | // Finally, we have two differing integer types. |
| 4957 | // The rules for this case are in C99 6.3.1.8 |
| 4958 | int compare = getIntegerTypeOrder(lhs, rhs); |
| 4959 | bool lhsSigned = lhs->isSignedIntegerType(), |
| 4960 | rhsSigned = rhs->isSignedIntegerType(); |
| 4961 | QualType destType; |
| 4962 | if (lhsSigned == rhsSigned) { |
| 4963 | // Same signedness; use the higher-ranked type |
| 4964 | destType = compare >= 0 ? lhs : rhs; |
| 4965 | } else if (compare != (lhsSigned ? 1 : -1)) { |
| 4966 | // The unsigned type has greater than or equal rank to the |
| 4967 | // signed type, so use the unsigned type |
| 4968 | destType = lhsSigned ? rhs : lhs; |
| 4969 | } else if (getIntWidth(lhs) != getIntWidth(rhs)) { |
| 4970 | // The two types are different widths; if we are here, that |
| 4971 | // means the signed type is larger than the unsigned type, so |
| 4972 | // use the signed type. |
| 4973 | destType = lhsSigned ? lhs : rhs; |
| 4974 | } else { |
| 4975 | // The signed type is higher-ranked than the unsigned type, |
| 4976 | // but isn't actually any bigger (like unsigned int and long |
| 4977 | // on most 32-bit systems). Use the unsigned type corresponding |
| 4978 | // to the signed type. |
| 4979 | destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs); |
| 4980 | } |
| 4981 | return destType; |
| 4982 | } |